ElfInterface.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <elf.h>
  17. #include <stdint.h>
  18. #include <memory>
  19. #include <string>
  20. #include <utility>
  21. #include <7zCrc.h>
  22. #include <Xz.h>
  23. #include <XzCrc64.h>
  24. #include <unwindstack/DwarfError.h>
  25. #include <unwindstack/DwarfSection.h>
  26. #include <unwindstack/ElfInterface.h>
  27. #include <unwindstack/Log.h>
  28. #include <unwindstack/Memory.h>
  29. #include <unwindstack/Regs.h>
  30. #include "DwarfDebugFrame.h"
  31. #include "DwarfEhFrame.h"
  32. #include "DwarfEhFrameWithHdr.h"
  33. #include "Symbols.h"
  34. namespace unwindstack {
  35. ElfInterface::~ElfInterface() {
  36. for (auto symbol : symbols_) {
  37. delete symbol;
  38. }
  39. }
  40. bool ElfInterface::IsValidPc(uint64_t pc) {
  41. if (!pt_loads_.empty()) {
  42. for (auto& entry : pt_loads_) {
  43. uint64_t start = entry.second.table_offset;
  44. uint64_t end = start + entry.second.table_size;
  45. if (pc >= start && pc < end) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. // No PT_LOAD data, look for a fde for this pc in the section data.
  52. if (debug_frame_ != nullptr && debug_frame_->GetFdeFromPc(pc) != nullptr) {
  53. return true;
  54. }
  55. if (eh_frame_ != nullptr && eh_frame_->GetFdeFromPc(pc) != nullptr) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. Memory* ElfInterface::CreateGnuDebugdataMemory() {
  61. if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) {
  62. return nullptr;
  63. }
  64. // TODO: Only call these initialization functions once.
  65. CrcGenerateTable();
  66. Crc64GenerateTable();
  67. std::vector<uint8_t> src(gnu_debugdata_size_);
  68. if (!memory_->ReadFully(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) {
  69. gnu_debugdata_offset_ = 0;
  70. gnu_debugdata_size_ = static_cast<uint64_t>(-1);
  71. return nullptr;
  72. }
  73. ISzAlloc alloc;
  74. CXzUnpacker state;
  75. alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
  76. alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
  77. XzUnpacker_Construct(&state, &alloc);
  78. std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer);
  79. int return_val;
  80. size_t src_offset = 0;
  81. size_t dst_offset = 0;
  82. ECoderStatus status;
  83. dst->Resize(5 * gnu_debugdata_size_);
  84. do {
  85. size_t src_remaining = src.size() - src_offset;
  86. size_t dst_remaining = dst->Size() - dst_offset;
  87. if (dst_remaining < 2 * gnu_debugdata_size_) {
  88. dst->Resize(dst->Size() + 2 * gnu_debugdata_size_);
  89. dst_remaining += 2 * gnu_debugdata_size_;
  90. }
  91. return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
  92. &src_remaining, true, CODER_FINISH_ANY, &status);
  93. src_offset += src_remaining;
  94. dst_offset += dst_remaining;
  95. } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
  96. XzUnpacker_Free(&state);
  97. if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) {
  98. gnu_debugdata_offset_ = 0;
  99. gnu_debugdata_size_ = static_cast<uint64_t>(-1);
  100. return nullptr;
  101. }
  102. // Shrink back down to the exact size.
  103. dst->Resize(dst_offset);
  104. return dst.release();
  105. }
  106. template <typename AddressType>
  107. void ElfInterface::InitHeadersWithTemplate(uint64_t load_bias) {
  108. if (eh_frame_hdr_offset_ != 0) {
  109. eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
  110. if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, load_bias)) {
  111. eh_frame_.reset(nullptr);
  112. }
  113. }
  114. if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) {
  115. // If there is an eh_frame section without an eh_frame_hdr section,
  116. // or using the frame hdr object failed to init.
  117. eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
  118. if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, load_bias)) {
  119. eh_frame_.reset(nullptr);
  120. }
  121. }
  122. if (eh_frame_.get() == nullptr) {
  123. eh_frame_hdr_offset_ = 0;
  124. eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
  125. eh_frame_offset_ = 0;
  126. eh_frame_size_ = static_cast<uint64_t>(-1);
  127. }
  128. if (debug_frame_offset_ != 0) {
  129. debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
  130. if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, load_bias)) {
  131. debug_frame_.reset(nullptr);
  132. debug_frame_offset_ = 0;
  133. debug_frame_size_ = static_cast<uint64_t>(-1);
  134. }
  135. }
  136. }
  137. template <typename EhdrType, typename PhdrType, typename ShdrType>
  138. bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) {
  139. EhdrType ehdr;
  140. if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
  141. last_error_.code = ERROR_MEMORY_INVALID;
  142. last_error_.address = 0;
  143. return false;
  144. }
  145. // If we have enough information that this is an elf file, then allow
  146. // malformed program and section headers.
  147. ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
  148. ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
  149. return true;
  150. }
  151. template <typename EhdrType, typename PhdrType>
  152. uint64_t ElfInterface::GetLoadBias(Memory* memory) {
  153. EhdrType ehdr;
  154. if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
  155. return false;
  156. }
  157. uint64_t offset = ehdr.e_phoff;
  158. for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
  159. PhdrType phdr;
  160. if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
  161. return 0;
  162. }
  163. if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
  164. return phdr.p_vaddr;
  165. }
  166. }
  167. return 0;
  168. }
  169. template <typename EhdrType, typename PhdrType>
  170. void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
  171. uint64_t offset = ehdr.e_phoff;
  172. for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
  173. PhdrType phdr;
  174. if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
  175. return;
  176. }
  177. switch (phdr.p_type) {
  178. case PT_LOAD:
  179. {
  180. if ((phdr.p_flags & PF_X) == 0) {
  181. continue;
  182. }
  183. pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
  184. static_cast<size_t>(phdr.p_memsz)};
  185. if (phdr.p_offset == 0) {
  186. *load_bias = phdr.p_vaddr;
  187. }
  188. break;
  189. }
  190. case PT_GNU_EH_FRAME:
  191. // This is really the pointer to the .eh_frame_hdr section.
  192. eh_frame_hdr_offset_ = phdr.p_offset;
  193. eh_frame_hdr_size_ = phdr.p_memsz;
  194. break;
  195. case PT_DYNAMIC:
  196. dynamic_offset_ = phdr.p_offset;
  197. dynamic_vaddr_ = phdr.p_vaddr;
  198. dynamic_size_ = phdr.p_memsz;
  199. break;
  200. default:
  201. HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
  202. break;
  203. }
  204. }
  205. }
  206. template <typename NhdrType>
  207. std::string ElfInterface::ReadBuildID() {
  208. // Ensure there is no overflow in any of the calulations below.
  209. uint64_t tmp;
  210. if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
  211. return "";
  212. }
  213. uint64_t offset = 0;
  214. while (offset < gnu_build_id_size_) {
  215. if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
  216. return "";
  217. }
  218. NhdrType hdr;
  219. if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
  220. return "";
  221. }
  222. offset += sizeof(hdr);
  223. if (gnu_build_id_size_ - offset < hdr.n_namesz) {
  224. return "";
  225. }
  226. if (hdr.n_namesz > 0) {
  227. std::string name(hdr.n_namesz, '\0');
  228. if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
  229. return "";
  230. }
  231. // Trim trailing \0 as GNU is stored as a C string in the ELF file.
  232. if (name.back() == '\0')
  233. name.resize(name.size() - 1);
  234. // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
  235. offset += (hdr.n_namesz + 3) & ~3;
  236. if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
  237. if (gnu_build_id_size_ - offset < hdr.n_descsz || hdr.n_descsz == 0) {
  238. return "";
  239. }
  240. std::string build_id(hdr.n_descsz, '\0');
  241. if (memory_->ReadFully(gnu_build_id_offset_ + offset, &build_id[0], hdr.n_descsz)) {
  242. return build_id;
  243. }
  244. return "";
  245. }
  246. }
  247. // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
  248. offset += (hdr.n_descsz + 3) & ~3;
  249. }
  250. return "";
  251. }
  252. template <typename EhdrType, typename ShdrType>
  253. void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
  254. uint64_t offset = ehdr.e_shoff;
  255. uint64_t sec_offset = 0;
  256. uint64_t sec_size = 0;
  257. // Get the location of the section header names.
  258. // If something is malformed in the header table data, we aren't going
  259. // to terminate, we'll simply ignore this part.
  260. ShdrType shdr;
  261. if (ehdr.e_shstrndx < ehdr.e_shnum) {
  262. uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
  263. if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
  264. sec_offset = shdr.sh_offset;
  265. sec_size = shdr.sh_size;
  266. }
  267. }
  268. // Skip the first header, it's always going to be NULL.
  269. offset += ehdr.e_shentsize;
  270. for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
  271. if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) {
  272. return;
  273. }
  274. if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
  275. // Need to go get the information about the section that contains
  276. // the string terminated names.
  277. ShdrType str_shdr;
  278. if (shdr.sh_link >= ehdr.e_shnum) {
  279. continue;
  280. }
  281. uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
  282. if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) {
  283. continue;
  284. }
  285. if (str_shdr.sh_type != SHT_STRTAB) {
  286. continue;
  287. }
  288. symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
  289. str_shdr.sh_offset, str_shdr.sh_size));
  290. } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
  291. // Look for the .debug_frame and .gnu_debugdata.
  292. if (shdr.sh_name < sec_size) {
  293. std::string name;
  294. if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
  295. uint64_t* offset_ptr = nullptr;
  296. uint64_t* size_ptr = nullptr;
  297. if (name == ".debug_frame") {
  298. offset_ptr = &debug_frame_offset_;
  299. size_ptr = &debug_frame_size_;
  300. } else if (name == ".gnu_debugdata") {
  301. offset_ptr = &gnu_debugdata_offset_;
  302. size_ptr = &gnu_debugdata_size_;
  303. } else if (name == ".eh_frame") {
  304. offset_ptr = &eh_frame_offset_;
  305. size_ptr = &eh_frame_size_;
  306. } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
  307. offset_ptr = &eh_frame_hdr_offset_;
  308. size_ptr = &eh_frame_hdr_size_;
  309. }
  310. if (offset_ptr != nullptr) {
  311. *offset_ptr = shdr.sh_offset;
  312. *size_ptr = shdr.sh_size;
  313. }
  314. }
  315. }
  316. } else if (shdr.sh_type == SHT_STRTAB) {
  317. // In order to read soname, keep track of address to offset mapping.
  318. strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
  319. static_cast<uint64_t>(shdr.sh_offset)));
  320. } else if (shdr.sh_type == SHT_NOTE) {
  321. if (shdr.sh_name < sec_size) {
  322. std::string name;
  323. if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
  324. name == ".note.gnu.build-id") {
  325. gnu_build_id_offset_ = shdr.sh_offset;
  326. gnu_build_id_size_ = shdr.sh_size;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. template <typename DynType>
  333. std::string ElfInterface::GetSonameWithTemplate() {
  334. if (soname_type_ == SONAME_INVALID) {
  335. return "";
  336. }
  337. if (soname_type_ == SONAME_VALID) {
  338. return soname_;
  339. }
  340. soname_type_ = SONAME_INVALID;
  341. uint64_t soname_offset = 0;
  342. uint64_t strtab_addr = 0;
  343. uint64_t strtab_size = 0;
  344. // Find the soname location from the dynamic headers section.
  345. DynType dyn;
  346. uint64_t offset = dynamic_offset_;
  347. uint64_t max_offset = offset + dynamic_size_;
  348. for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
  349. if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
  350. last_error_.code = ERROR_MEMORY_INVALID;
  351. last_error_.address = offset;
  352. return "";
  353. }
  354. if (dyn.d_tag == DT_STRTAB) {
  355. strtab_addr = dyn.d_un.d_ptr;
  356. } else if (dyn.d_tag == DT_STRSZ) {
  357. strtab_size = dyn.d_un.d_val;
  358. } else if (dyn.d_tag == DT_SONAME) {
  359. soname_offset = dyn.d_un.d_val;
  360. } else if (dyn.d_tag == DT_NULL) {
  361. break;
  362. }
  363. }
  364. // Need to map the strtab address to the real offset.
  365. for (const auto& entry : strtabs_) {
  366. if (entry.first == strtab_addr) {
  367. soname_offset = entry.second + soname_offset;
  368. if (soname_offset >= entry.second + strtab_size) {
  369. return "";
  370. }
  371. if (!memory_->ReadString(soname_offset, &soname_)) {
  372. return "";
  373. }
  374. soname_type_ = SONAME_VALID;
  375. return soname_;
  376. }
  377. }
  378. return "";
  379. }
  380. template <typename SymType>
  381. bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
  382. uint64_t* func_offset) {
  383. if (symbols_.empty()) {
  384. return false;
  385. }
  386. for (const auto symbol : symbols_) {
  387. if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. template <typename SymType>
  394. bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
  395. if (symbols_.empty()) {
  396. return false;
  397. }
  398. for (const auto symbol : symbols_) {
  399. if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
  406. last_error_.code = ERROR_NONE;
  407. last_error_.address = 0;
  408. // Try the debug_frame first since it contains the most specific unwind
  409. // information.
  410. DwarfSection* debug_frame = debug_frame_.get();
  411. if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
  412. return true;
  413. }
  414. // Try the eh_frame next.
  415. DwarfSection* eh_frame = eh_frame_.get();
  416. if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
  417. return true;
  418. }
  419. if (gnu_debugdata_interface_ != nullptr &&
  420. gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
  421. return true;
  422. }
  423. // Set the error code based on the first error encountered.
  424. DwarfSection* section = nullptr;
  425. if (debug_frame_ != nullptr) {
  426. section = debug_frame_.get();
  427. } else if (eh_frame_ != nullptr) {
  428. section = eh_frame_.get();
  429. } else if (gnu_debugdata_interface_ != nullptr) {
  430. last_error_ = gnu_debugdata_interface_->last_error();
  431. return false;
  432. } else {
  433. return false;
  434. }
  435. // Convert the DWARF ERROR to an external error.
  436. DwarfErrorCode code = section->LastErrorCode();
  437. switch (code) {
  438. case DWARF_ERROR_NONE:
  439. last_error_.code = ERROR_NONE;
  440. break;
  441. case DWARF_ERROR_MEMORY_INVALID:
  442. last_error_.code = ERROR_MEMORY_INVALID;
  443. last_error_.address = section->LastErrorAddress();
  444. break;
  445. case DWARF_ERROR_ILLEGAL_VALUE:
  446. case DWARF_ERROR_ILLEGAL_STATE:
  447. case DWARF_ERROR_STACK_INDEX_NOT_VALID:
  448. case DWARF_ERROR_TOO_MANY_ITERATIONS:
  449. case DWARF_ERROR_CFA_NOT_DEFINED:
  450. case DWARF_ERROR_NO_FDES:
  451. last_error_.code = ERROR_UNWIND_INFO;
  452. break;
  453. case DWARF_ERROR_NOT_IMPLEMENTED:
  454. case DWARF_ERROR_UNSUPPORTED_VERSION:
  455. last_error_.code = ERROR_UNSUPPORTED;
  456. break;
  457. }
  458. return false;
  459. }
  460. // This is an estimation of the size of the elf file using the location
  461. // of the section headers and size. This assumes that the section headers
  462. // are at the end of the elf file. If the elf has a load bias, the size
  463. // will be too large, but this is acceptable.
  464. template <typename EhdrType>
  465. void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
  466. EhdrType ehdr;
  467. if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
  468. return;
  469. }
  470. if (ehdr.e_shnum == 0) {
  471. return;
  472. }
  473. *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
  474. }
  475. template <typename EhdrType, typename ShdrType>
  476. bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) {
  477. EhdrType ehdr;
  478. if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
  479. return false;
  480. }
  481. uint64_t offset = ehdr.e_shoff;
  482. uint64_t sec_offset;
  483. uint64_t sec_size;
  484. ShdrType shdr;
  485. if (ehdr.e_shstrndx >= ehdr.e_shnum) {
  486. return false;
  487. }
  488. uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
  489. if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
  490. return false;
  491. }
  492. sec_offset = shdr.sh_offset;
  493. sec_size = shdr.sh_size;
  494. // Skip the first header, it's always going to be NULL.
  495. offset += ehdr.e_shentsize;
  496. for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
  497. if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) {
  498. return false;
  499. }
  500. std::string name;
  501. if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size &&
  502. memory->ReadString(sec_offset + shdr.sh_name, &name) && name == ".note.gnu.build-id") {
  503. *build_id_offset = shdr.sh_offset;
  504. *build_id_size = shdr.sh_size;
  505. return true;
  506. }
  507. }
  508. return false;
  509. }
  510. template <typename EhdrType, typename ShdrType, typename NhdrType>
  511. std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) {
  512. uint64_t note_offset;
  513. uint64_t note_size;
  514. if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, &note_offset, &note_size)) {
  515. return "";
  516. }
  517. // Ensure there is no overflow in any of the calculations below.
  518. uint64_t tmp;
  519. if (__builtin_add_overflow(note_offset, note_size, &tmp)) {
  520. return "";
  521. }
  522. uint64_t offset = 0;
  523. while (offset < note_size) {
  524. if (note_size - offset < sizeof(NhdrType)) {
  525. return "";
  526. }
  527. NhdrType hdr;
  528. if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) {
  529. return "";
  530. }
  531. offset += sizeof(hdr);
  532. if (note_size - offset < hdr.n_namesz) {
  533. return "";
  534. }
  535. if (hdr.n_namesz > 0) {
  536. std::string name(hdr.n_namesz, '\0');
  537. if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) {
  538. return "";
  539. }
  540. // Trim trailing \0 as GNU is stored as a C string in the ELF file.
  541. if (name.back() == '\0') name.resize(name.size() - 1);
  542. // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
  543. offset += (hdr.n_namesz + 3) & ~3;
  544. if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
  545. if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) {
  546. return "";
  547. }
  548. std::string build_id(hdr.n_descsz - 1, '\0');
  549. if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) {
  550. return build_id;
  551. }
  552. return "";
  553. }
  554. }
  555. // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
  556. offset += (hdr.n_descsz + 3) & ~3;
  557. }
  558. return "";
  559. }
  560. // Instantiate all of the needed template functions.
  561. template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
  562. template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
  563. template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
  564. template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
  565. template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&,
  566. uint64_t*);
  567. template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&,
  568. uint64_t*);
  569. template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
  570. template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
  571. template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>();
  572. template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>();
  573. template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>();
  574. template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>();
  575. template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
  576. uint64_t*);
  577. template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
  578. uint64_t*);
  579. template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
  580. template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
  581. template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
  582. template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
  583. template uint64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
  584. template uint64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
  585. template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(
  586. Memory*);
  587. template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(
  588. Memory*);
  589. } // namespace unwindstack