Elf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (C) 2016 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 <string.h>
  18. #include <memory>
  19. #include <mutex>
  20. #include <string>
  21. #include <utility>
  22. #define LOG_TAG "unwind"
  23. #include <log/log.h>
  24. #include <unwindstack/Elf.h>
  25. #include <unwindstack/ElfInterface.h>
  26. #include <unwindstack/MapInfo.h>
  27. #include <unwindstack/Memory.h>
  28. #include <unwindstack/Regs.h>
  29. #include "ElfInterfaceArm.h"
  30. #include "Symbols.h"
  31. namespace unwindstack {
  32. bool Elf::cache_enabled_;
  33. std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* Elf::cache_;
  34. std::mutex* Elf::cache_lock_;
  35. bool Elf::Init() {
  36. load_bias_ = 0;
  37. if (!memory_) {
  38. return false;
  39. }
  40. interface_.reset(CreateInterfaceFromMemory(memory_.get()));
  41. if (!interface_) {
  42. return false;
  43. }
  44. valid_ = interface_->Init(&load_bias_);
  45. if (valid_) {
  46. interface_->InitHeaders(load_bias_);
  47. InitGnuDebugdata();
  48. } else {
  49. interface_.reset(nullptr);
  50. }
  51. return valid_;
  52. }
  53. // It is expensive to initialize the .gnu_debugdata section. Provide a method
  54. // to initialize this data separately.
  55. void Elf::InitGnuDebugdata() {
  56. if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
  57. return;
  58. }
  59. gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
  60. gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
  61. ElfInterface* gnu = gnu_debugdata_interface_.get();
  62. if (gnu == nullptr) {
  63. return;
  64. }
  65. // Ignore the load_bias from the compressed section, the correct load bias
  66. // is in the uncompressed data.
  67. uint64_t load_bias;
  68. if (gnu->Init(&load_bias)) {
  69. gnu->InitHeaders(load_bias);
  70. interface_->SetGnuDebugdataInterface(gnu);
  71. } else {
  72. // Free all of the memory associated with the gnu_debugdata section.
  73. gnu_debugdata_memory_.reset(nullptr);
  74. gnu_debugdata_interface_.reset(nullptr);
  75. }
  76. }
  77. void Elf::Invalidate() {
  78. interface_.reset(nullptr);
  79. valid_ = false;
  80. }
  81. std::string Elf::GetSoname() {
  82. std::lock_guard<std::mutex> guard(lock_);
  83. if (!valid_) {
  84. return "";
  85. }
  86. return interface_->GetSoname();
  87. }
  88. uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
  89. return pc - map_info->start + load_bias_ + map_info->elf_offset;
  90. }
  91. bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
  92. std::lock_guard<std::mutex> guard(lock_);
  93. return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
  94. (gnu_debugdata_interface_ &&
  95. gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
  96. }
  97. bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
  98. if (!valid_) {
  99. return false;
  100. }
  101. if (!interface_->GetGlobalVariable(name, memory_address) &&
  102. (gnu_debugdata_interface_ == nullptr ||
  103. !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) {
  104. return false;
  105. }
  106. // Adjust by the load bias.
  107. if (*memory_address < load_bias_) {
  108. return false;
  109. }
  110. *memory_address -= load_bias_;
  111. // If this winds up in the dynamic section, then we might need to adjust
  112. // the address.
  113. uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size();
  114. if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) {
  115. if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) {
  116. *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset();
  117. } else {
  118. *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr();
  119. }
  120. }
  121. return true;
  122. }
  123. std::string Elf::GetBuildID() {
  124. if (!valid_) {
  125. return "";
  126. }
  127. return interface_->GetBuildID();
  128. }
  129. void Elf::GetLastError(ErrorData* data) {
  130. if (valid_) {
  131. *data = interface_->last_error();
  132. }
  133. }
  134. ErrorCode Elf::GetLastErrorCode() {
  135. if (valid_) {
  136. return interface_->LastErrorCode();
  137. }
  138. return ERROR_INVALID_ELF;
  139. }
  140. uint64_t Elf::GetLastErrorAddress() {
  141. if (valid_) {
  142. return interface_->LastErrorAddress();
  143. }
  144. return 0;
  145. }
  146. // The relative pc expectd by this function is relative to the start of the elf.
  147. bool Elf::StepIfSignalHandler(uint64_t rel_pc, Regs* regs, Memory* process_memory) {
  148. if (!valid_) {
  149. return false;
  150. }
  151. return regs->StepIfSignalHandler(rel_pc, this, process_memory);
  152. }
  153. // The relative pc is always relative to the start of the map from which it comes.
  154. bool Elf::Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished) {
  155. if (!valid_) {
  156. return false;
  157. }
  158. // Lock during the step which can update information in the object.
  159. std::lock_guard<std::mutex> guard(lock_);
  160. return interface_->Step(rel_pc, regs, process_memory, finished);
  161. }
  162. bool Elf::IsValidElf(Memory* memory) {
  163. if (memory == nullptr) {
  164. return false;
  165. }
  166. // Verify that this is a valid elf file.
  167. uint8_t e_ident[SELFMAG + 1];
  168. if (!memory->ReadFully(0, e_ident, SELFMAG)) {
  169. return false;
  170. }
  171. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. bool Elf::GetInfo(Memory* memory, uint64_t* size) {
  177. if (!IsValidElf(memory)) {
  178. return false;
  179. }
  180. *size = 0;
  181. uint8_t class_type;
  182. if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
  183. return false;
  184. }
  185. // Get the maximum size of the elf data from the header.
  186. if (class_type == ELFCLASS32) {
  187. ElfInterface32::GetMaxSize(memory, size);
  188. } else if (class_type == ELFCLASS64) {
  189. ElfInterface64::GetMaxSize(memory, size);
  190. } else {
  191. return false;
  192. }
  193. return true;
  194. }
  195. bool Elf::IsValidPc(uint64_t pc) {
  196. if (!valid_ || pc < load_bias_) {
  197. return false;
  198. }
  199. if (interface_->IsValidPc(pc)) {
  200. return true;
  201. }
  202. if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
  208. if (!IsValidElf(memory)) {
  209. return nullptr;
  210. }
  211. std::unique_ptr<ElfInterface> interface;
  212. if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
  213. return nullptr;
  214. }
  215. if (class_type_ == ELFCLASS32) {
  216. Elf32_Half e_machine;
  217. if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
  218. return nullptr;
  219. }
  220. machine_type_ = e_machine;
  221. if (e_machine == EM_ARM) {
  222. arch_ = ARCH_ARM;
  223. interface.reset(new ElfInterfaceArm(memory));
  224. } else if (e_machine == EM_386) {
  225. arch_ = ARCH_X86;
  226. interface.reset(new ElfInterface32(memory));
  227. } else if (e_machine == EM_MIPS) {
  228. arch_ = ARCH_MIPS;
  229. interface.reset(new ElfInterface32(memory));
  230. } else {
  231. // Unsupported.
  232. ALOGI("32 bit elf that is neither arm nor x86 nor mips: e_machine = %d\n", e_machine);
  233. return nullptr;
  234. }
  235. } else if (class_type_ == ELFCLASS64) {
  236. Elf64_Half e_machine;
  237. if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
  238. return nullptr;
  239. }
  240. machine_type_ = e_machine;
  241. if (e_machine == EM_AARCH64) {
  242. arch_ = ARCH_ARM64;
  243. } else if (e_machine == EM_X86_64) {
  244. arch_ = ARCH_X86_64;
  245. } else if (e_machine == EM_MIPS) {
  246. arch_ = ARCH_MIPS64;
  247. } else {
  248. // Unsupported.
  249. ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
  250. e_machine);
  251. return nullptr;
  252. }
  253. interface.reset(new ElfInterface64(memory));
  254. }
  255. return interface.release();
  256. }
  257. uint64_t Elf::GetLoadBias(Memory* memory) {
  258. if (!IsValidElf(memory)) {
  259. return 0;
  260. }
  261. uint8_t class_type;
  262. if (!memory->Read(EI_CLASS, &class_type, 1)) {
  263. return 0;
  264. }
  265. if (class_type == ELFCLASS32) {
  266. return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
  267. } else if (class_type == ELFCLASS64) {
  268. return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
  269. }
  270. return 0;
  271. }
  272. void Elf::SetCachingEnabled(bool enable) {
  273. if (!cache_enabled_ && enable) {
  274. cache_enabled_ = true;
  275. cache_ = new std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>;
  276. cache_lock_ = new std::mutex;
  277. } else if (cache_enabled_ && !enable) {
  278. cache_enabled_ = false;
  279. delete cache_;
  280. delete cache_lock_;
  281. }
  282. }
  283. void Elf::CacheLock() {
  284. cache_lock_->lock();
  285. }
  286. void Elf::CacheUnlock() {
  287. cache_lock_->unlock();
  288. }
  289. void Elf::CacheAdd(MapInfo* info) {
  290. // If elf_offset != 0, then cache both name:offset and name.
  291. // The cached name is used to do lookups if multiple maps for the same
  292. // named elf file exist.
  293. // For example, if there are two maps boot.odex:1000 and boot.odex:2000
  294. // where each reference the entire boot.odex, the cache will properly
  295. // use the same cached elf object.
  296. if (info->offset == 0 || info->elf_offset != 0) {
  297. (*cache_)[info->name] = std::make_pair(info->elf, true);
  298. }
  299. if (info->offset != 0) {
  300. // The second element in the pair indicates whether elf_offset should
  301. // be set to offset when getting out of the cache.
  302. (*cache_)[info->name + ':' + std::to_string(info->offset)] =
  303. std::make_pair(info->elf, info->elf_offset != 0);
  304. }
  305. }
  306. bool Elf::CacheAfterCreateMemory(MapInfo* info) {
  307. if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) {
  308. return false;
  309. }
  310. auto entry = cache_->find(info->name);
  311. if (entry == cache_->end()) {
  312. return false;
  313. }
  314. // In this case, the whole file is the elf, and the name has already
  315. // been cached. Add an entry at name:offset to get this directly out
  316. // of the cache next time.
  317. info->elf = entry->second.first;
  318. (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true);
  319. return true;
  320. }
  321. bool Elf::CacheGet(MapInfo* info) {
  322. std::string name(info->name);
  323. if (info->offset != 0) {
  324. name += ':' + std::to_string(info->offset);
  325. }
  326. auto entry = cache_->find(name);
  327. if (entry != cache_->end()) {
  328. info->elf = entry->second.first;
  329. if (entry->second.second) {
  330. info->elf_offset = info->offset;
  331. }
  332. return true;
  333. }
  334. return false;
  335. }
  336. std::string Elf::GetBuildID(Memory* memory) {
  337. if (!IsValidElf(memory)) {
  338. return "";
  339. }
  340. uint8_t class_type;
  341. if (!memory->Read(EI_CLASS, &class_type, 1)) {
  342. return "";
  343. }
  344. if (class_type == ELFCLASS32) {
  345. return ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(memory);
  346. } else if (class_type == ELFCLASS64) {
  347. return ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(memory);
  348. }
  349. return "";
  350. }
  351. } // namespace unwindstack