1 /*
2 * Copyright (C) 2012 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
17 #include "elf_file.h"
18
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include "base/logging.h"
23 #include "base/stringprintf.h"
24 #include "base/stl_util.h"
25 #include "dwarf.h"
26 #include "leb128.h"
27 #include "utils.h"
28 #include "instruction_set.h"
29
30 namespace art {
31
32 // -------------------------------------------------------------------
33 // Binary GDB JIT Interface as described in
34 // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
35 extern "C" {
36 typedef enum {
37 JIT_NOACTION = 0,
38 JIT_REGISTER_FN,
39 JIT_UNREGISTER_FN
40 } JITAction;
41
42 struct JITCodeEntry {
43 JITCodeEntry* next_;
44 JITCodeEntry* prev_;
45 const byte *symfile_addr_;
46 uint64_t symfile_size_;
47 };
48
49 struct JITDescriptor {
50 uint32_t version_;
51 uint32_t action_flag_;
52 JITCodeEntry* relevant_entry_;
53 JITCodeEntry* first_entry_;
54 };
55
56 // GDB will place breakpoint into this function.
57 // To prevent GCC from inlining or removing it we place noinline attribute
58 // and inline assembler statement inside.
__jit_debug_register_code()59 void __attribute__((noinline)) __jit_debug_register_code() {
60 __asm__("");
61 }
62
63 // GDB will inspect contents of this descriptor.
64 // Static initialization is necessary to prevent GDB from seeing
65 // uninitialized descriptor.
66 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
67 }
68
69
CreateCodeEntry(const byte * symfile_addr,uintptr_t symfile_size)70 static JITCodeEntry* CreateCodeEntry(const byte *symfile_addr,
71 uintptr_t symfile_size) {
72 JITCodeEntry* entry = new JITCodeEntry;
73 entry->symfile_addr_ = symfile_addr;
74 entry->symfile_size_ = symfile_size;
75 entry->prev_ = nullptr;
76
77 // TODO: Do we need a lock here?
78 entry->next_ = __jit_debug_descriptor.first_entry_;
79 if (entry->next_ != nullptr) {
80 entry->next_->prev_ = entry;
81 }
82 __jit_debug_descriptor.first_entry_ = entry;
83 __jit_debug_descriptor.relevant_entry_ = entry;
84
85 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
86 __jit_debug_register_code();
87 return entry;
88 }
89
90
UnregisterCodeEntry(JITCodeEntry * entry)91 static void UnregisterCodeEntry(JITCodeEntry* entry) {
92 // TODO: Do we need a lock here?
93 if (entry->prev_ != nullptr) {
94 entry->prev_->next_ = entry->next_;
95 } else {
96 __jit_debug_descriptor.first_entry_ = entry->next_;
97 }
98
99 if (entry->next_ != nullptr) {
100 entry->next_->prev_ = entry->prev_;
101 }
102
103 __jit_debug_descriptor.relevant_entry_ = entry;
104 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
105 __jit_debug_register_code();
106 delete entry;
107 }
108
ElfFile(File * file,bool writable,bool program_header_only,uint8_t * requested_base)109 ElfFile::ElfFile(File* file, bool writable, bool program_header_only, uint8_t* requested_base)
110 : file_(file),
111 writable_(writable),
112 program_header_only_(program_header_only),
113 header_(nullptr),
114 base_address_(nullptr),
115 program_headers_start_(nullptr),
116 section_headers_start_(nullptr),
117 dynamic_program_header_(nullptr),
118 dynamic_section_start_(nullptr),
119 symtab_section_start_(nullptr),
120 dynsym_section_start_(nullptr),
121 strtab_section_start_(nullptr),
122 dynstr_section_start_(nullptr),
123 hash_section_start_(nullptr),
124 symtab_symbol_table_(nullptr),
125 dynsym_symbol_table_(nullptr),
126 jit_elf_image_(nullptr),
127 jit_gdb_entry_(nullptr),
128 requested_base_(requested_base) {
129 CHECK(file != nullptr);
130 }
131
Open(File * file,bool writable,bool program_header_only,std::string * error_msg,uint8_t * requested_base)132 ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
133 std::string* error_msg, uint8_t* requested_base) {
134 std::unique_ptr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only,
135 requested_base));
136 int prot;
137 int flags;
138 if (writable) {
139 prot = PROT_READ | PROT_WRITE;
140 flags = MAP_SHARED;
141 } else {
142 prot = PROT_READ;
143 flags = MAP_PRIVATE;
144 }
145 if (!elf_file->Setup(prot, flags, error_msg)) {
146 return nullptr;
147 }
148 return elf_file.release();
149 }
150
Open(File * file,int prot,int flags,std::string * error_msg)151 ElfFile* ElfFile::Open(File* file, int prot, int flags, std::string* error_msg) {
152 std::unique_ptr<ElfFile> elf_file(new ElfFile(file, (prot & PROT_WRITE) == PROT_WRITE, false,
153 /*requested_base*/nullptr));
154 if (!elf_file->Setup(prot, flags, error_msg)) {
155 return nullptr;
156 }
157 return elf_file.release();
158 }
159
Setup(int prot,int flags,std::string * error_msg)160 bool ElfFile::Setup(int prot, int flags, std::string* error_msg) {
161 int64_t temp_file_length = file_->GetLength();
162 if (temp_file_length < 0) {
163 errno = -temp_file_length;
164 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
165 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
166 return false;
167 }
168 size_t file_length = static_cast<size_t>(temp_file_length);
169 if (file_length < sizeof(Elf32_Ehdr)) {
170 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
171 "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr),
172 file_->GetPath().c_str());
173 return false;
174 }
175
176 if (program_header_only_) {
177 // first just map ELF header to get program header size information
178 size_t elf_header_size = sizeof(Elf32_Ehdr);
179 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
180 file_->GetPath().c_str(), error_msg),
181 error_msg)) {
182 return false;
183 }
184 // then remap to cover program header
185 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
186 if (file_length < program_header_size) {
187 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
188 "header of %zd bytes: '%s'", file_length,
189 sizeof(Elf32_Ehdr), file_->GetPath().c_str());
190 return false;
191 }
192 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
193 file_->GetPath().c_str(), error_msg),
194 error_msg)) {
195 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
196 return false;
197 }
198 } else {
199 // otherwise map entire file
200 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
201 file_->GetPath().c_str(), error_msg),
202 error_msg)) {
203 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
204 return false;
205 }
206 }
207
208 if (program_header_only_) {
209 program_headers_start_ = Begin() + GetHeader().e_phoff;
210 } else {
211 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
212 return false;
213 }
214
215 // Setup section headers.
216 if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) {
217 return false;
218 }
219
220 // Find shstrtab.
221 Elf32_Shdr* shstrtab_section_header = GetSectionNameStringSection();
222 if (shstrtab_section_header == nullptr) {
223 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
224 file_->GetPath().c_str());
225 return false;
226 }
227
228 // Find .dynamic section info from program header
229 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
230 if (dynamic_program_header_ == nullptr) {
231 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
232 file_->GetPath().c_str());
233 return false;
234 }
235
236 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
237 reinterpret_cast<byte**>(&dynamic_section_start_), error_msg)) {
238 return false;
239 }
240
241 // Find other sections from section headers
242 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
243 Elf32_Shdr* section_header = GetSectionHeader(i);
244 if (section_header == nullptr) {
245 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
246 i, file_->GetPath().c_str());
247 return false;
248 }
249 switch (section_header->sh_type) {
250 case SHT_SYMTAB: {
251 if (!CheckAndSet(section_header->sh_offset, "symtab",
252 reinterpret_cast<byte**>(&symtab_section_start_), error_msg)) {
253 return false;
254 }
255 break;
256 }
257 case SHT_DYNSYM: {
258 if (!CheckAndSet(section_header->sh_offset, "dynsym",
259 reinterpret_cast<byte**>(&dynsym_section_start_), error_msg)) {
260 return false;
261 }
262 break;
263 }
264 case SHT_STRTAB: {
265 // TODO: base these off of sh_link from .symtab and .dynsym above
266 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
267 // Check that this is named ".dynstr" and ignore otherwise.
268 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
269 if (strncmp(".dynstr", header_name, 8) == 0) {
270 if (!CheckAndSet(section_header->sh_offset, "dynstr",
271 reinterpret_cast<byte**>(&dynstr_section_start_), error_msg)) {
272 return false;
273 }
274 }
275 } else {
276 // Check that this is named ".strtab" and ignore otherwise.
277 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
278 if (strncmp(".strtab", header_name, 8) == 0) {
279 if (!CheckAndSet(section_header->sh_offset, "strtab",
280 reinterpret_cast<byte**>(&strtab_section_start_), error_msg)) {
281 return false;
282 }
283 }
284 }
285 break;
286 }
287 case SHT_DYNAMIC: {
288 if (reinterpret_cast<byte*>(dynamic_section_start_) !=
289 Begin() + section_header->sh_offset) {
290 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
291 << file_->GetPath() << ": " << std::hex
292 << reinterpret_cast<void*>(dynamic_section_start_)
293 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
294 return false;
295 }
296 break;
297 }
298 case SHT_HASH: {
299 if (!CheckAndSet(section_header->sh_offset, "hash section",
300 reinterpret_cast<byte**>(&hash_section_start_), error_msg)) {
301 return false;
302 }
303 break;
304 }
305 }
306 }
307
308 // Check for the existence of some sections.
309 if (!CheckSectionsExist(error_msg)) {
310 return false;
311 }
312 }
313
314 return true;
315 }
316
~ElfFile()317 ElfFile::~ElfFile() {
318 STLDeleteElements(&segments_);
319 delete symtab_symbol_table_;
320 delete dynsym_symbol_table_;
321 delete jit_elf_image_;
322 if (jit_gdb_entry_) {
323 UnregisterCodeEntry(jit_gdb_entry_);
324 }
325 }
326
CheckAndSet(Elf32_Off offset,const char * label,byte ** target,std::string * error_msg)327 bool ElfFile::CheckAndSet(Elf32_Off offset, const char* label,
328 byte** target, std::string* error_msg) {
329 if (Begin() + offset >= End()) {
330 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
331 file_->GetPath().c_str());
332 return false;
333 }
334 *target = Begin() + offset;
335 return true;
336 }
337
CheckSectionsLinked(const byte * source,const byte * target) const338 bool ElfFile::CheckSectionsLinked(const byte* source, const byte* target) const {
339 // Only works in whole-program mode, as we need to iterate over the sections.
340 // Note that we normally can't search by type, as duplicates are allowed for most section types.
341 if (program_header_only_) {
342 return true;
343 }
344
345 Elf32_Shdr* source_section = nullptr;
346 Elf32_Word target_index = 0;
347 bool target_found = false;
348 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
349 Elf32_Shdr* section_header = GetSectionHeader(i);
350
351 if (Begin() + section_header->sh_offset == source) {
352 // Found the source.
353 source_section = section_header;
354 if (target_index) {
355 break;
356 }
357 } else if (Begin() + section_header->sh_offset == target) {
358 target_index = i;
359 target_found = true;
360 if (source_section != nullptr) {
361 break;
362 }
363 }
364 }
365
366 return target_found && source_section != nullptr && source_section->sh_link == target_index;
367 }
368
CheckSectionsExist(std::string * error_msg) const369 bool ElfFile::CheckSectionsExist(std::string* error_msg) const {
370 if (!program_header_only_) {
371 // If in full mode, need section headers.
372 if (section_headers_start_ == nullptr) {
373 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
374 return false;
375 }
376 }
377
378 // This is redundant, but defensive.
379 if (dynamic_program_header_ == nullptr) {
380 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
381 file_->GetPath().c_str());
382 return false;
383 }
384
385 // Need a dynamic section. This is redundant, but defensive.
386 if (dynamic_section_start_ == nullptr) {
387 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
388 file_->GetPath().c_str());
389 return false;
390 }
391
392 // Symtab validation. These is not really a hard failure, as we are currently not using the
393 // symtab internally, but it's nice to be defensive.
394 if (symtab_section_start_ != nullptr) {
395 // When there's a symtab, there should be a strtab.
396 if (strtab_section_start_ == nullptr) {
397 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
398 return false;
399 }
400
401 // The symtab should link to the strtab.
402 if (!CheckSectionsLinked(reinterpret_cast<const byte*>(symtab_section_start_),
403 reinterpret_cast<const byte*>(strtab_section_start_))) {
404 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
405 file_->GetPath().c_str());
406 return false;
407 }
408 }
409
410 // We always need a dynstr & dynsym.
411 if (dynstr_section_start_ == nullptr) {
412 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
413 return false;
414 }
415 if (dynsym_section_start_ == nullptr) {
416 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
417 return false;
418 }
419
420 // Need a hash section for dynamic symbol lookup.
421 if (hash_section_start_ == nullptr) {
422 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
423 file_->GetPath().c_str());
424 return false;
425 }
426
427 // And the hash section should be linking to the dynsym.
428 if (!CheckSectionsLinked(reinterpret_cast<const byte*>(hash_section_start_),
429 reinterpret_cast<const byte*>(dynsym_section_start_))) {
430 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
431 file_->GetPath().c_str());
432 return false;
433 }
434
435 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
436 // us). This is usually the last in an oat file, and a good indicator of whether writing was
437 // successful (or the process crashed and left garbage).
438 if (program_header_only_) {
439 // It might not be mapped, but we can compare against the file size.
440 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
441 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
442 if (offset >= file_->GetLength()) {
443 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
444 file_->GetPath().c_str());
445 return false;
446 }
447 }
448
449 return true;
450 }
451
SetMap(MemMap * map,std::string * error_msg)452 bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
453 if (map == nullptr) {
454 // MemMap::Open should have already set an error.
455 DCHECK(!error_msg->empty());
456 return false;
457 }
458 map_.reset(map);
459 CHECK(map_.get() != nullptr) << file_->GetPath();
460 CHECK(map_->Begin() != nullptr) << file_->GetPath();
461
462 header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin());
463 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
464 || (ELFMAG1 != header_->e_ident[EI_MAG1])
465 || (ELFMAG2 != header_->e_ident[EI_MAG2])
466 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
467 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
468 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
469 file_->GetPath().c_str(),
470 header_->e_ident[EI_MAG0],
471 header_->e_ident[EI_MAG1],
472 header_->e_ident[EI_MAG2],
473 header_->e_ident[EI_MAG3]);
474 return false;
475 }
476 if (ELFCLASS32 != header_->e_ident[EI_CLASS]) {
477 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
478 ELFCLASS32,
479 file_->GetPath().c_str(),
480 header_->e_ident[EI_CLASS]);
481 return false;
482 }
483 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
484 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
485 ELFDATA2LSB,
486 file_->GetPath().c_str(),
487 header_->e_ident[EI_CLASS]);
488 return false;
489 }
490 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
491 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
492 EV_CURRENT,
493 file_->GetPath().c_str(),
494 header_->e_ident[EI_CLASS]);
495 return false;
496 }
497 if (ET_DYN != header_->e_type) {
498 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
499 ET_DYN,
500 file_->GetPath().c_str(),
501 header_->e_type);
502 return false;
503 }
504 if (EV_CURRENT != header_->e_version) {
505 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
506 EV_CURRENT,
507 file_->GetPath().c_str(),
508 header_->e_version);
509 return false;
510 }
511 if (0 != header_->e_entry) {
512 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
513 0,
514 file_->GetPath().c_str(),
515 header_->e_entry);
516 return false;
517 }
518 if (0 == header_->e_phoff) {
519 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
520 file_->GetPath().c_str());
521 return false;
522 }
523 if (0 == header_->e_shoff) {
524 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
525 file_->GetPath().c_str());
526 return false;
527 }
528 if (0 == header_->e_ehsize) {
529 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
530 file_->GetPath().c_str());
531 return false;
532 }
533 if (0 == header_->e_phentsize) {
534 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
535 file_->GetPath().c_str());
536 return false;
537 }
538 if (0 == header_->e_phnum) {
539 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
540 file_->GetPath().c_str());
541 return false;
542 }
543 if (0 == header_->e_shentsize) {
544 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
545 file_->GetPath().c_str());
546 return false;
547 }
548 if (0 == header_->e_shnum) {
549 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
550 file_->GetPath().c_str());
551 return false;
552 }
553 if (0 == header_->e_shstrndx) {
554 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
555 file_->GetPath().c_str());
556 return false;
557 }
558 if (header_->e_shstrndx >= header_->e_shnum) {
559 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
560 header_->e_shstrndx,
561 header_->e_shnum,
562 file_->GetPath().c_str());
563 return false;
564 }
565
566 if (!program_header_only_) {
567 if (header_->e_phoff >= Size()) {
568 *error_msg = StringPrintf("Failed to find e_phoff value %d less than %zd in %s",
569 header_->e_phoff,
570 Size(),
571 file_->GetPath().c_str());
572 return false;
573 }
574 if (header_->e_shoff >= Size()) {
575 *error_msg = StringPrintf("Failed to find e_shoff value %d less than %zd in %s",
576 header_->e_shoff,
577 Size(),
578 file_->GetPath().c_str());
579 return false;
580 }
581 }
582 return true;
583 }
584
585
GetHeader() const586 Elf32_Ehdr& ElfFile::GetHeader() const {
587 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
588 return *header_;
589 }
590
GetProgramHeadersStart() const591 byte* ElfFile::GetProgramHeadersStart() const {
592 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
593 // check.
594 return program_headers_start_;
595 }
596
GetSectionHeadersStart() const597 byte* ElfFile::GetSectionHeadersStart() const {
598 CHECK(!program_header_only_); // Only used in "full" mode.
599 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
600 return section_headers_start_;
601 }
602
GetDynamicProgramHeader() const603 Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const {
604 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
605 return *dynamic_program_header_;
606 }
607
GetDynamicSectionStart() const608 Elf32_Dyn* ElfFile::GetDynamicSectionStart() const {
609 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
610 return dynamic_section_start_;
611 }
612
IsSymbolSectionType(Elf32_Word section_type)613 static bool IsSymbolSectionType(Elf32_Word section_type) {
614 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
615 }
616
GetSymbolSectionStart(Elf32_Word section_type) const617 Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const {
618 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
619 switch (section_type) {
620 case SHT_SYMTAB: {
621 return symtab_section_start_;
622 break;
623 }
624 case SHT_DYNSYM: {
625 return dynsym_section_start_;
626 break;
627 }
628 default: {
629 LOG(FATAL) << section_type;
630 return nullptr;
631 }
632 }
633 }
634
GetStringSectionStart(Elf32_Word section_type) const635 const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const {
636 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
637 switch (section_type) {
638 case SHT_SYMTAB: {
639 return strtab_section_start_;
640 }
641 case SHT_DYNSYM: {
642 return dynstr_section_start_;
643 }
644 default: {
645 LOG(FATAL) << section_type;
646 return nullptr;
647 }
648 }
649 }
650
GetString(Elf32_Word section_type,Elf32_Word i) const651 const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const {
652 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
653 if (i == 0) {
654 return nullptr;
655 }
656 const char* string_section_start = GetStringSectionStart(section_type);
657 if (string_section_start == nullptr) {
658 return nullptr;
659 }
660 return string_section_start + i;
661 }
662
663 // WARNING: The following methods do not check for an error condition (non-existent hash section).
664 // It is the caller's job to do this.
665
GetHashSectionStart() const666 Elf32_Word* ElfFile::GetHashSectionStart() const {
667 return hash_section_start_;
668 }
669
GetHashBucketNum() const670 Elf32_Word ElfFile::GetHashBucketNum() const {
671 return GetHashSectionStart()[0];
672 }
673
GetHashChainNum() const674 Elf32_Word ElfFile::GetHashChainNum() const {
675 return GetHashSectionStart()[1];
676 }
677
GetHashBucket(size_t i,bool * ok) const678 Elf32_Word ElfFile::GetHashBucket(size_t i, bool* ok) const {
679 if (i >= GetHashBucketNum()) {
680 *ok = false;
681 return 0;
682 }
683 *ok = true;
684 // 0 is nbucket, 1 is nchain
685 return GetHashSectionStart()[2 + i];
686 }
687
GetHashChain(size_t i,bool * ok) const688 Elf32_Word ElfFile::GetHashChain(size_t i, bool* ok) const {
689 if (i >= GetHashBucketNum()) {
690 *ok = false;
691 return 0;
692 }
693 *ok = true;
694 // 0 is nbucket, 1 is nchain, & chains are after buckets
695 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
696 }
697
GetProgramHeaderNum() const698 Elf32_Word ElfFile::GetProgramHeaderNum() const {
699 return GetHeader().e_phnum;
700 }
701
GetProgramHeader(Elf32_Word i) const702 Elf32_Phdr* ElfFile::GetProgramHeader(Elf32_Word i) const {
703 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
704 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
705 if (program_header >= End()) {
706 return nullptr; // Failure condition.
707 }
708 return reinterpret_cast<Elf32_Phdr*>(program_header);
709 }
710
FindProgamHeaderByType(Elf32_Word type) const711 Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const {
712 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
713 Elf32_Phdr* program_header = GetProgramHeader(i);
714 if (program_header->p_type == type) {
715 return program_header;
716 }
717 }
718 return nullptr;
719 }
720
GetSectionHeaderNum() const721 Elf32_Word ElfFile::GetSectionHeaderNum() const {
722 return GetHeader().e_shnum;
723 }
724
GetSectionHeader(Elf32_Word i) const725 Elf32_Shdr* ElfFile::GetSectionHeader(Elf32_Word i) const {
726 // Can only access arbitrary sections when we have the whole file, not just program header.
727 // Even if we Load(), it doesn't bring in all the sections.
728 CHECK(!program_header_only_) << file_->GetPath();
729 if (i >= GetSectionHeaderNum()) {
730 return nullptr; // Failure condition.
731 }
732 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
733 if (section_header >= End()) {
734 return nullptr; // Failure condition.
735 }
736 return reinterpret_cast<Elf32_Shdr*>(section_header);
737 }
738
FindSectionByType(Elf32_Word type) const739 Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const {
740 // Can only access arbitrary sections when we have the whole file, not just program header.
741 // We could change this to switch on known types if they were detected during loading.
742 CHECK(!program_header_only_) << file_->GetPath();
743 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
744 Elf32_Shdr* section_header = GetSectionHeader(i);
745 if (section_header->sh_type == type) {
746 return section_header;
747 }
748 }
749 return nullptr;
750 }
751
752 // from bionic
elfhash(const char * _name)753 static unsigned elfhash(const char *_name) {
754 const unsigned char *name = (const unsigned char *) _name;
755 unsigned h = 0, g;
756
757 while (*name) {
758 h = (h << 4) + *name++;
759 g = h & 0xf0000000;
760 h ^= g;
761 h ^= g >> 24;
762 }
763 return h;
764 }
765
GetSectionNameStringSection() const766 Elf32_Shdr* ElfFile::GetSectionNameStringSection() const {
767 return GetSectionHeader(GetHeader().e_shstrndx);
768 }
769
FindDynamicSymbolAddress(const std::string & symbol_name) const770 const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
771 // Check that we have a hash section.
772 if (GetHashSectionStart() == nullptr) {
773 return nullptr; // Failure condition.
774 }
775 const Elf32_Sym* sym = FindDynamicSymbol(symbol_name);
776 if (sym != nullptr) {
777 // TODO: we need to change this to calculate base_address_ in ::Open,
778 // otherwise it will be wrongly 0 if ::Load has not yet been called.
779 return base_address_ + sym->st_value;
780 } else {
781 return nullptr;
782 }
783 }
784
785 // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
FindDynamicSymbol(const std::string & symbol_name) const786 const Elf32_Sym* ElfFile::FindDynamicSymbol(const std::string& symbol_name) const {
787 if (GetHashBucketNum() == 0) {
788 // No dynamic symbols at all.
789 return nullptr;
790 }
791 Elf32_Word hash = elfhash(symbol_name.c_str());
792 Elf32_Word bucket_index = hash % GetHashBucketNum();
793 bool ok;
794 Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
795 if (!ok) {
796 return nullptr;
797 }
798 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
799 Elf32_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
800 if (symbol == nullptr) {
801 return nullptr; // Failure condition.
802 }
803 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
804 if (symbol_name == name) {
805 return symbol;
806 }
807 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
808 if (!ok) {
809 return nullptr;
810 }
811 }
812 return nullptr;
813 }
814
GetSymbolNum(Elf32_Shdr & section_header) const815 Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const {
816 CHECK(IsSymbolSectionType(section_header.sh_type))
817 << file_->GetPath() << " " << section_header.sh_type;
818 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
819 return section_header.sh_size / section_header.sh_entsize;
820 }
821
GetSymbol(Elf32_Word section_type,Elf32_Word i) const822 Elf32_Sym* ElfFile::GetSymbol(Elf32_Word section_type,
823 Elf32_Word i) const {
824 Elf32_Sym* sym_start = GetSymbolSectionStart(section_type);
825 if (sym_start == nullptr) {
826 return nullptr;
827 }
828 return sym_start + i;
829 }
830
GetSymbolTable(Elf32_Word section_type)831 ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) {
832 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
833 switch (section_type) {
834 case SHT_SYMTAB: {
835 return &symtab_symbol_table_;
836 }
837 case SHT_DYNSYM: {
838 return &dynsym_symbol_table_;
839 }
840 default: {
841 LOG(FATAL) << section_type;
842 return nullptr;
843 }
844 }
845 }
846
FindSymbolByName(Elf32_Word section_type,const std::string & symbol_name,bool build_map)847 Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type,
848 const std::string& symbol_name,
849 bool build_map) {
850 CHECK(!program_header_only_) << file_->GetPath();
851 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
852
853 SymbolTable** symbol_table = GetSymbolTable(section_type);
854 if (*symbol_table != nullptr || build_map) {
855 if (*symbol_table == nullptr) {
856 DCHECK(build_map);
857 *symbol_table = new SymbolTable;
858 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
859 if (symbol_section == nullptr) {
860 return nullptr; // Failure condition.
861 }
862 Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
863 if (string_section == nullptr) {
864 return nullptr; // Failure condition.
865 }
866 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
867 Elf32_Sym* symbol = GetSymbol(section_type, i);
868 if (symbol == nullptr) {
869 return nullptr; // Failure condition.
870 }
871 unsigned char type = ELF32_ST_TYPE(symbol->st_info);
872 if (type == STT_NOTYPE) {
873 continue;
874 }
875 const char* name = GetString(*string_section, symbol->st_name);
876 if (name == nullptr) {
877 continue;
878 }
879 std::pair<SymbolTable::iterator, bool> result =
880 (*symbol_table)->insert(std::make_pair(name, symbol));
881 if (!result.second) {
882 // If a duplicate, make sure it has the same logical value. Seen on x86.
883 if ((symbol->st_value != result.first->second->st_value) ||
884 (symbol->st_size != result.first->second->st_size) ||
885 (symbol->st_info != result.first->second->st_info) ||
886 (symbol->st_other != result.first->second->st_other) ||
887 (symbol->st_shndx != result.first->second->st_shndx)) {
888 return nullptr; // Failure condition.
889 }
890 }
891 }
892 }
893 CHECK(*symbol_table != nullptr);
894 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
895 if (it == (*symbol_table)->end()) {
896 return nullptr;
897 }
898 return it->second;
899 }
900
901 // Fall back to linear search
902 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
903 if (symbol_section == nullptr) {
904 return nullptr;
905 }
906 Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
907 if (string_section == nullptr) {
908 return nullptr;
909 }
910 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
911 Elf32_Sym* symbol = GetSymbol(section_type, i);
912 if (symbol == nullptr) {
913 return nullptr; // Failure condition.
914 }
915 const char* name = GetString(*string_section, symbol->st_name);
916 if (name == nullptr) {
917 continue;
918 }
919 if (symbol_name == name) {
920 return symbol;
921 }
922 }
923 return nullptr;
924 }
925
FindSymbolAddress(Elf32_Word section_type,const std::string & symbol_name,bool build_map)926 Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type,
927 const std::string& symbol_name,
928 bool build_map) {
929 Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
930 if (symbol == nullptr) {
931 return 0;
932 }
933 return symbol->st_value;
934 }
935
GetString(Elf32_Shdr & string_section,Elf32_Word i) const936 const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const {
937 CHECK(!program_header_only_) << file_->GetPath();
938 // TODO: remove this static_cast from enum when using -std=gnu++0x
939 if (static_cast<Elf32_Word>(SHT_STRTAB) != string_section.sh_type) {
940 return nullptr; // Failure condition.
941 }
942 if (i >= string_section.sh_size) {
943 return nullptr;
944 }
945 if (i == 0) {
946 return nullptr;
947 }
948 byte* strings = Begin() + string_section.sh_offset;
949 byte* string = strings + i;
950 if (string >= End()) {
951 return nullptr;
952 }
953 return reinterpret_cast<const char*>(string);
954 }
955
GetDynamicNum() const956 Elf32_Word ElfFile::GetDynamicNum() const {
957 return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn);
958 }
959
GetDynamic(Elf32_Word i) const960 Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const {
961 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
962 return *(GetDynamicSectionStart() + i);
963 }
964
FindDynamicByType(Elf32_Sword type) const965 Elf32_Dyn* ElfFile::FindDynamicByType(Elf32_Sword type) const {
966 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
967 Elf32_Dyn* dyn = &GetDynamic(i);
968 if (dyn->d_tag == type) {
969 return dyn;
970 }
971 }
972 return NULL;
973 }
974
FindDynamicValueByType(Elf32_Sword type) const975 Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
976 Elf32_Dyn* dyn = FindDynamicByType(type);
977 if (dyn == NULL) {
978 return 0;
979 } else {
980 return dyn->d_un.d_val;
981 }
982 }
983
GetRelSectionStart(Elf32_Shdr & section_header) const984 Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
985 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
986 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
987 }
988
GetRelNum(Elf32_Shdr & section_header) const989 Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
990 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
991 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
992 return section_header.sh_size / section_header.sh_entsize;
993 }
994
GetRel(Elf32_Shdr & section_header,Elf32_Word i) const995 Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
996 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
997 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
998 return *(GetRelSectionStart(section_header) + i);
999 }
1000
GetRelaSectionStart(Elf32_Shdr & section_header) const1001 Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
1002 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
1003 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
1004 }
1005
GetRelaNum(Elf32_Shdr & section_header) const1006 Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
1007 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
1008 return section_header.sh_size / section_header.sh_entsize;
1009 }
1010
GetRela(Elf32_Shdr & section_header,Elf32_Word i) const1011 Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
1012 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
1013 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1014 return *(GetRelaSectionStart(section_header) + i);
1015 }
1016
1017 // Base on bionic phdr_table_get_load_size
GetLoadedSize() const1018 size_t ElfFile::GetLoadedSize() const {
1019 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
1020 Elf32_Addr max_vaddr = 0x00000000u;
1021 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
1022 Elf32_Phdr* program_header = GetProgramHeader(i);
1023 if (program_header->p_type != PT_LOAD) {
1024 continue;
1025 }
1026 Elf32_Addr begin_vaddr = program_header->p_vaddr;
1027 if (begin_vaddr < min_vaddr) {
1028 min_vaddr = begin_vaddr;
1029 }
1030 Elf32_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
1031 if (end_vaddr > max_vaddr) {
1032 max_vaddr = end_vaddr;
1033 }
1034 }
1035 min_vaddr = RoundDown(min_vaddr, kPageSize);
1036 max_vaddr = RoundUp(max_vaddr, kPageSize);
1037 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1038 size_t loaded_size = max_vaddr - min_vaddr;
1039 return loaded_size;
1040 }
1041
Load(bool executable,std::string * error_msg)1042 bool ElfFile::Load(bool executable, std::string* error_msg) {
1043 CHECK(program_header_only_) << file_->GetPath();
1044
1045 if (executable) {
1046 InstructionSet elf_ISA = kNone;
1047 switch (GetHeader().e_machine) {
1048 case EM_ARM: {
1049 elf_ISA = kArm;
1050 break;
1051 }
1052 case EM_AARCH64: {
1053 elf_ISA = kArm64;
1054 break;
1055 }
1056 case EM_386: {
1057 elf_ISA = kX86;
1058 break;
1059 }
1060 case EM_X86_64: {
1061 elf_ISA = kX86_64;
1062 break;
1063 }
1064 case EM_MIPS: {
1065 elf_ISA = kMips;
1066 break;
1067 }
1068 }
1069
1070 if (elf_ISA != kRuntimeISA) {
1071 std::ostringstream oss;
1072 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1073 *error_msg = oss.str();
1074 return false;
1075 }
1076 }
1077
1078 bool reserved = false;
1079 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
1080 Elf32_Phdr* program_header = GetProgramHeader(i);
1081 if (program_header == nullptr) {
1082 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1083 i, file_->GetPath().c_str());
1084 return false;
1085 }
1086
1087 // Record .dynamic header information for later use
1088 if (program_header->p_type == PT_DYNAMIC) {
1089 dynamic_program_header_ = program_header;
1090 continue;
1091 }
1092
1093 // Not something to load, move on.
1094 if (program_header->p_type != PT_LOAD) {
1095 continue;
1096 }
1097
1098 // Found something to load.
1099
1100 // Before load the actual segments, reserve a contiguous chunk
1101 // of required size and address for all segments, but with no
1102 // permissions. We'll then carve that up with the proper
1103 // permissions as we load the actual segments. If p_vaddr is
1104 // non-zero, the segments require the specific address specified,
1105 // which either was specified in the file because we already set
1106 // base_address_ after the first zero segment).
1107 int64_t temp_file_length = file_->GetLength();
1108 if (temp_file_length < 0) {
1109 errno = -temp_file_length;
1110 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1111 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1112 return false;
1113 }
1114 size_t file_length = static_cast<size_t>(temp_file_length);
1115 if (!reserved) {
1116 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1117 uint8_t* reserve_base_override = reserve_base;
1118 // Override the base (e.g. when compiling with --compile-pic)
1119 if (requested_base_ != nullptr) {
1120 reserve_base_override = requested_base_;
1121 }
1122 std::string reservation_name("ElfFile reservation for ");
1123 reservation_name += file_->GetPath();
1124 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
1125 reserve_base_override,
1126 GetLoadedSize(), PROT_NONE, false,
1127 error_msg));
1128 if (reserve.get() == nullptr) {
1129 *error_msg = StringPrintf("Failed to allocate %s: %s",
1130 reservation_name.c_str(), error_msg->c_str());
1131 return false;
1132 }
1133 reserved = true;
1134
1135 // Base address is the difference of actual mapped location and the p_vaddr
1136 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1137 - reinterpret_cast<uintptr_t>(reserve_base));
1138 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1139 // dynamic memory address of where that object is actually mapped
1140 //
1141 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1142 // FindDynamicSymbolAddress returns the wrong values until Load is called.
1143 segments_.push_back(reserve.release());
1144 }
1145 // empty segment, nothing to map
1146 if (program_header->p_memsz == 0) {
1147 continue;
1148 }
1149 byte* p_vaddr = base_address_ + program_header->p_vaddr;
1150 int prot = 0;
1151 if (executable && ((program_header->p_flags & PF_X) != 0)) {
1152 prot |= PROT_EXEC;
1153 }
1154 if ((program_header->p_flags & PF_W) != 0) {
1155 prot |= PROT_WRITE;
1156 }
1157 if ((program_header->p_flags & PF_R) != 0) {
1158 prot |= PROT_READ;
1159 }
1160 int flags = 0;
1161 if (writable_) {
1162 prot |= PROT_WRITE;
1163 flags |= MAP_SHARED;
1164 } else {
1165 flags |= MAP_PRIVATE;
1166 }
1167 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
1168 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1169 "%d of %d bytes: '%s'", file_length, i,
1170 program_header->p_offset + program_header->p_memsz,
1171 file_->GetPath().c_str());
1172 return false;
1173 }
1174 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
1175 program_header->p_memsz,
1176 prot, flags, file_->Fd(),
1177 program_header->p_offset,
1178 true, // implies MAP_FIXED
1179 file_->GetPath().c_str(),
1180 error_msg));
1181 if (segment.get() == nullptr) {
1182 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1183 i, file_->GetPath().c_str(), error_msg->c_str());
1184 return false;
1185 }
1186 if (segment->Begin() != p_vaddr) {
1187 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1188 "instead mapped to %p",
1189 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1190 return false;
1191 }
1192 segments_.push_back(segment.release());
1193 }
1194
1195 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
1196 byte* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
1197 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1198 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1199 file_->GetPath().c_str());
1200 return false;
1201 }
1202 dynamic_section_start_ = reinterpret_cast<Elf32_Dyn*>(dsptr);
1203
1204 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
1205 Elf32_Dyn& elf_dyn = GetDynamic(i);
1206 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
1207 switch (elf_dyn.d_tag) {
1208 case DT_HASH: {
1209 if (!ValidPointer(d_ptr)) {
1210 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1211 d_ptr, file_->GetPath().c_str());
1212 return false;
1213 }
1214 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
1215 break;
1216 }
1217 case DT_STRTAB: {
1218 if (!ValidPointer(d_ptr)) {
1219 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1220 d_ptr, file_->GetPath().c_str());
1221 return false;
1222 }
1223 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1224 break;
1225 }
1226 case DT_SYMTAB: {
1227 if (!ValidPointer(d_ptr)) {
1228 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1229 d_ptr, file_->GetPath().c_str());
1230 return false;
1231 }
1232 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
1233 break;
1234 }
1235 case DT_NULL: {
1236 if (GetDynamicNum() != i+1) {
1237 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1238 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1239 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1240 return false;
1241 }
1242 break;
1243 }
1244 }
1245 }
1246
1247 // Check for the existence of some sections.
1248 if (!CheckSectionsExist(error_msg)) {
1249 return false;
1250 }
1251
1252 // Use GDB JIT support to do stack backtrace, etc.
1253 if (executable) {
1254 GdbJITSupport();
1255 }
1256
1257 return true;
1258 }
1259
ValidPointer(const byte * start) const1260 bool ElfFile::ValidPointer(const byte* start) const {
1261 for (size_t i = 0; i < segments_.size(); ++i) {
1262 const MemMap* segment = segments_[i];
1263 if (segment->Begin() <= start && start < segment->End()) {
1264 return true;
1265 }
1266 }
1267 return false;
1268 }
1269
1270
FindSectionByName(const std::string & name) const1271 Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const {
1272 CHECK(!program_header_only_);
1273 Elf32_Shdr* shstrtab_sec = GetSectionNameStringSection();
1274 if (shstrtab_sec == nullptr) {
1275 return nullptr;
1276 }
1277 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
1278 Elf32_Shdr* shdr = GetSectionHeader(i);
1279 if (shdr == nullptr) {
1280 return nullptr;
1281 }
1282 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
1283 if (sec_name == nullptr) {
1284 continue;
1285 }
1286 if (name == sec_name) {
1287 return shdr;
1288 }
1289 }
1290 return nullptr;
1291 }
1292
1293 struct PACKED(1) FDE {
1294 uint32_t raw_length_;
GetLengthart::FDE1295 uint32_t GetLength() {
1296 return raw_length_ + sizeof(raw_length_);
1297 }
1298 uint32_t CIE_pointer;
1299 uint32_t initial_location;
1300 uint32_t address_range;
1301 uint8_t instructions[0];
1302 };
1303
NextFDE(FDE * frame)1304 static FDE* NextFDE(FDE* frame) {
1305 byte* fde_bytes = reinterpret_cast<byte*>(frame);
1306 fde_bytes += frame->GetLength();
1307 return reinterpret_cast<FDE*>(fde_bytes);
1308 }
1309
IsFDE(FDE * frame)1310 static bool IsFDE(FDE* frame) {
1311 return frame->CIE_pointer != 0;
1312 }
1313
1314 // TODO This only works for 32-bit Elf Files.
FixupEHFrame(uintptr_t text_start,byte * eh_frame,size_t eh_frame_size)1315 static bool FixupEHFrame(uintptr_t text_start, byte* eh_frame, size_t eh_frame_size) {
1316 FDE* last_frame = reinterpret_cast<FDE*>(eh_frame + eh_frame_size);
1317 FDE* frame = NextFDE(reinterpret_cast<FDE*>(eh_frame));
1318 for (; frame < last_frame; frame = NextFDE(frame)) {
1319 if (!IsFDE(frame)) {
1320 return false;
1321 }
1322 frame->initial_location += text_start;
1323 }
1324 return true;
1325 }
1326
1327 struct PACKED(1) DebugInfoHeader {
1328 uint32_t unit_length; // TODO 32-bit specific size
1329 uint16_t version;
1330 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1331 uint8_t address_size;
1332 };
1333
1334 // Returns -1 if it is variable length, which we will just disallow for now.
FormLength(uint32_t att)1335 static int32_t FormLength(uint32_t att) {
1336 switch (att) {
1337 case DW_FORM_data1:
1338 case DW_FORM_flag:
1339 case DW_FORM_flag_present:
1340 case DW_FORM_ref1:
1341 return 1;
1342
1343 case DW_FORM_data2:
1344 case DW_FORM_ref2:
1345 return 2;
1346
1347 case DW_FORM_addr: // TODO 32-bit only
1348 case DW_FORM_ref_addr: // TODO 32-bit only
1349 case DW_FORM_sec_offset: // TODO 32-bit only
1350 case DW_FORM_strp: // TODO 32-bit only
1351 case DW_FORM_data4:
1352 case DW_FORM_ref4:
1353 return 4;
1354
1355 case DW_FORM_data8:
1356 case DW_FORM_ref8:
1357 case DW_FORM_ref_sig8:
1358 return 8;
1359
1360 case DW_FORM_block:
1361 case DW_FORM_block1:
1362 case DW_FORM_block2:
1363 case DW_FORM_block4:
1364 case DW_FORM_exprloc:
1365 case DW_FORM_indirect:
1366 case DW_FORM_ref_udata:
1367 case DW_FORM_sdata:
1368 case DW_FORM_string:
1369 case DW_FORM_udata:
1370 default:
1371 return -1;
1372 }
1373 }
1374
1375 class DebugTag {
1376 public:
1377 const uint32_t index_;
~DebugTag()1378 ~DebugTag() {}
1379 // Creates a new tag and moves data pointer up to the start of the next one.
1380 // nullptr means error.
Create(const byte ** data_pointer)1381 static DebugTag* Create(const byte** data_pointer) {
1382 const byte* data = *data_pointer;
1383 uint32_t index = DecodeUnsignedLeb128(&data);
1384 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1385 tag->size_ = static_cast<uint32_t>(
1386 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1387 // skip the abbrev
1388 tag->tag_ = DecodeUnsignedLeb128(&data);
1389 tag->has_child_ = (*data == 0);
1390 data++;
1391 while (true) {
1392 uint32_t attr = DecodeUnsignedLeb128(&data);
1393 uint32_t form = DecodeUnsignedLeb128(&data);
1394 if (attr == 0 && form == 0) {
1395 break;
1396 } else if (attr == 0 || form == 0) {
1397 // Bad abbrev.
1398 return nullptr;
1399 }
1400 int32_t size = FormLength(form);
1401 if (size == -1) {
1402 return nullptr;
1403 }
1404 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1405 }
1406 *data_pointer = data;
1407 return tag.release();
1408 }
1409
GetSize() const1410 uint32_t GetSize() const {
1411 return size_;
1412 }
1413
HasChild()1414 bool HasChild() {
1415 return has_child_;
1416 }
1417
GetTagNumber()1418 uint32_t GetTagNumber() {
1419 return tag_;
1420 }
1421
1422 // Gets the offset of a particular attribute in this tag structure.
1423 // Interpretation of the data is left to the consumer. 0 is returned if the
1424 // tag does not contain the attribute.
GetOffsetOf(uint32_t dwarf_attribute) const1425 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1426 auto it = off_map_.find(dwarf_attribute);
1427 if (it == off_map_.end()) {
1428 return 0;
1429 } else {
1430 return it->second;
1431 }
1432 }
1433
1434 // Gets the size of attribute
GetAttrSize(uint32_t dwarf_attribute) const1435 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1436 auto it = size_map_.find(dwarf_attribute);
1437 if (it == size_map_.end()) {
1438 return 0;
1439 } else {
1440 return it->second;
1441 }
1442 }
1443
1444 private:
DebugTag(uint32_t index)1445 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
AddAttribute(uint32_t type,uint32_t attr_size)1446 void AddAttribute(uint32_t type, uint32_t attr_size) {
1447 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1448 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1449 size_ += attr_size;
1450 }
1451 std::map<uint32_t, uint32_t> off_map_;
1452 std::map<uint32_t, uint32_t> size_map_;
1453 uint32_t size_;
1454 uint32_t tag_;
1455 bool has_child_;
1456 };
1457
1458 class DebugAbbrev {
1459 public:
~DebugAbbrev()1460 ~DebugAbbrev() {}
Create(const byte * dbg_abbrev,size_t dbg_abbrev_size)1461 static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) {
1462 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev);
1463 const byte* last = dbg_abbrev + dbg_abbrev_size;
1464 while (dbg_abbrev < last) {
1465 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1466 if (tag.get() == nullptr) {
1467 return nullptr;
1468 } else {
1469 abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size()));
1470 abbrev->tag_list_.push_back(std::move(tag));
1471 }
1472 }
1473 return abbrev.release();
1474 }
1475
ReadTag(const byte * entry)1476 DebugTag* ReadTag(const byte* entry) {
1477 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1478 auto it = tags_.find(tag_num);
1479 if (it == tags_.end()) {
1480 return nullptr;
1481 } else {
1482 CHECK_GT(tag_list_.size(), it->second);
1483 return tag_list_.at(it->second).get();
1484 }
1485 }
1486
1487 private:
DebugAbbrev()1488 DebugAbbrev() {}
1489 std::map<uint32_t, uint32_t> tags_;
1490 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1491 };
1492
1493 class DebugInfoIterator {
1494 public:
Create(DebugInfoHeader * header,size_t frame_size,DebugAbbrev * abbrev)1495 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1496 DebugAbbrev* abbrev) {
1497 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1498 if (iter->GetCurrentTag() == nullptr) {
1499 return nullptr;
1500 } else {
1501 return iter.release();
1502 }
1503 }
~DebugInfoIterator()1504 ~DebugInfoIterator() {}
1505
1506 // Moves to the next DIE. Returns false if at last entry.
1507 // TODO Handle variable length attributes.
next()1508 bool next() {
1509 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1510 return false;
1511 }
1512 current_entry_ += current_tag_->GetSize();
1513 if (current_entry_ >= last_entry_) {
1514 current_entry_ = nullptr;
1515 return false;
1516 }
1517 current_tag_ = abbrev_->ReadTag(current_entry_);
1518 if (current_tag_ == nullptr) {
1519 current_entry_ = nullptr;
1520 return false;
1521 } else {
1522 return true;
1523 }
1524 }
1525
GetCurrentTag()1526 const DebugTag* GetCurrentTag() {
1527 return const_cast<DebugTag*>(current_tag_);
1528 }
GetPointerToField(uint8_t dwarf_field)1529 byte* GetPointerToField(uint8_t dwarf_field) {
1530 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1531 return nullptr;
1532 }
1533 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1534 if (off == 0) {
1535 // tag does not have that field.
1536 return nullptr;
1537 } else {
1538 DCHECK_LT(off, current_tag_->GetSize());
1539 return current_entry_ + off;
1540 }
1541 }
1542
1543 private:
DebugInfoIterator(DebugInfoHeader * header,size_t frame_size,DebugAbbrev * abbrev)1544 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
1545 : abbrev_(abbrev),
1546 last_entry_(reinterpret_cast<byte*>(header) + frame_size),
1547 current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)),
1548 current_tag_(abbrev_->ReadTag(current_entry_)) {}
1549 DebugAbbrev* abbrev_;
1550 byte* last_entry_;
1551 byte* current_entry_;
1552 DebugTag* current_tag_;
1553 };
1554
FixupDebugInfo(uint32_t text_start,DebugInfoIterator * iter)1555 static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) {
1556 do {
1557 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
1558 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
1559 return false;
1560 }
1561 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
1562 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
1563 if (PC_low != nullptr && PC_high != nullptr) {
1564 *PC_low += text_start;
1565 *PC_high += text_start;
1566 }
1567 } while (iter->next());
1568 return true;
1569 }
1570
FixupDebugSections(const byte * dbg_abbrev,size_t dbg_abbrev_size,uintptr_t text_start,byte * dbg_info,size_t dbg_info_size,byte * eh_frame,size_t eh_frame_size)1571 static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size,
1572 uintptr_t text_start,
1573 byte* dbg_info, size_t dbg_info_size,
1574 byte* eh_frame, size_t eh_frame_size) {
1575 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size));
1576 if (abbrev.get() == nullptr) {
1577 return false;
1578 }
1579 std::unique_ptr<DebugInfoIterator> iter(
1580 DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info),
1581 dbg_info_size, abbrev.get()));
1582 if (iter.get() == nullptr) {
1583 return false;
1584 }
1585 return FixupDebugInfo(text_start, iter.get())
1586 && FixupEHFrame(text_start, eh_frame, eh_frame_size);
1587 }
1588
GdbJITSupport()1589 void ElfFile::GdbJITSupport() {
1590 // We only get here if we only are mapping the program header.
1591 DCHECK(program_header_only_);
1592
1593 // Well, we need the whole file to do this.
1594 std::string error_msg;
1595 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
1596 // sections are there.
1597 std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
1598 MAP_PRIVATE, &error_msg));
1599 if (all_ptr.get() == nullptr) {
1600 return;
1601 }
1602 ElfFile& all = *all_ptr;
1603
1604 // Do we have interesting sections?
1605 const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info");
1606 const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev");
1607 const Elf32_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
1608 const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str");
1609 const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab");
1610 const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab");
1611 Elf32_Shdr* text_sec = all.FindSectionByName(".text");
1612 if (debug_info == nullptr || debug_abbrev == nullptr || eh_frame == nullptr ||
1613 debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr ||
1614 symtab_sec == nullptr) {
1615 return;
1616 }
1617 // We need to add in a strtab and symtab to the image.
1618 // all is MAP_PRIVATE so it can be written to freely.
1619 // We also already have strtab and symtab so we are fine there.
1620 Elf32_Ehdr& elf_hdr = all.GetHeader();
1621 elf_hdr.e_entry = 0;
1622 elf_hdr.e_phoff = 0;
1623 elf_hdr.e_phnum = 0;
1624 elf_hdr.e_phentsize = 0;
1625 elf_hdr.e_type = ET_EXEC;
1626
1627 text_sec->sh_type = SHT_NOBITS;
1628 text_sec->sh_offset = 0;
1629
1630 if (!FixupDebugSections(
1631 all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr,
1632 all.Begin() + debug_info->sh_offset, debug_info->sh_size,
1633 all.Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
1634 LOG(ERROR) << "Failed to load GDB data";
1635 return;
1636 }
1637
1638 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
1639 gdb_file_mapping_.reset(all_ptr.release());
1640 }
1641
1642 } // namespace art
1643