blob: 281967054d10b2061c199e2e8e385819b670a9f4 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
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
Tong Shen62d1ca32014-09-03 17:24:56 -070019#include <inttypes.h>
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000020#include <sys/types.h>
21#include <unistd.h>
22
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070027#include "base/unix_file/fd_file.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070028#include "elf_file_impl.h"
29#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070030#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "utils.h"
32
33namespace art {
34
Mark Mendellae9fd932014-02-10 16:14:35 -080035// -------------------------------------------------------------------
36// Binary GDB JIT Interface as described in
37// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
38extern "C" {
39 typedef enum {
40 JIT_NOACTION = 0,
41 JIT_REGISTER_FN,
42 JIT_UNREGISTER_FN
43 } JITAction;
44
45 struct JITCodeEntry {
46 JITCodeEntry* next_;
47 JITCodeEntry* prev_;
Ian Rogers13735952014-10-08 12:43:28 -070048 const uint8_t *symfile_addr_;
Mark Mendellae9fd932014-02-10 16:14:35 -080049 uint64_t symfile_size_;
50 };
51
52 struct JITDescriptor {
53 uint32_t version_;
54 uint32_t action_flag_;
55 JITCodeEntry* relevant_entry_;
56 JITCodeEntry* first_entry_;
57 };
58
59 // GDB will place breakpoint into this function.
60 // To prevent GCC from inlining or removing it we place noinline attribute
61 // and inline assembler statement inside.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080062 void __attribute__((noinline)) __jit_debug_register_code();
Mark Mendellae9fd932014-02-10 16:14:35 -080063 void __attribute__((noinline)) __jit_debug_register_code() {
64 __asm__("");
65 }
66
67 // GDB will inspect contents of this descriptor.
68 // Static initialization is necessary to prevent GDB from seeing
69 // uninitialized descriptor.
70 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
71}
72
73
Ian Rogers13735952014-10-08 12:43:28 -070074static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr,
Mark Mendellae9fd932014-02-10 16:14:35 -080075 uintptr_t symfile_size) {
76 JITCodeEntry* entry = new JITCodeEntry;
77 entry->symfile_addr_ = symfile_addr;
78 entry->symfile_size_ = symfile_size;
79 entry->prev_ = nullptr;
80
81 // TODO: Do we need a lock here?
82 entry->next_ = __jit_debug_descriptor.first_entry_;
83 if (entry->next_ != nullptr) {
84 entry->next_->prev_ = entry;
85 }
86 __jit_debug_descriptor.first_entry_ = entry;
87 __jit_debug_descriptor.relevant_entry_ = entry;
88
89 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
90 __jit_debug_register_code();
91 return entry;
92}
93
94
95static void UnregisterCodeEntry(JITCodeEntry* entry) {
96 // TODO: Do we need a lock here?
97 if (entry->prev_ != nullptr) {
98 entry->prev_->next_ = entry->next_;
99 } else {
100 __jit_debug_descriptor.first_entry_ = entry->next_;
101 }
102
103 if (entry->next_ != nullptr) {
104 entry->next_->prev_ = entry->prev_;
105 }
106
107 __jit_debug_descriptor.relevant_entry_ = entry;
108 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
109 __jit_debug_register_code();
110 delete entry;
111}
112
David Srbecky533c2072015-04-22 12:20:22 +0100113template <typename ElfTypes>
114ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable,
115 bool program_header_only,
116 uint8_t* requested_base)
Brian Carlstromc1409452014-02-26 14:06:23 -0800117 : file_(file),
118 writable_(writable),
119 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700120 header_(nullptr),
121 base_address_(nullptr),
122 program_headers_start_(nullptr),
123 section_headers_start_(nullptr),
124 dynamic_program_header_(nullptr),
125 dynamic_section_start_(nullptr),
126 symtab_section_start_(nullptr),
127 dynsym_section_start_(nullptr),
128 strtab_section_start_(nullptr),
129 dynstr_section_start_(nullptr),
130 hash_section_start_(nullptr),
131 symtab_symbol_table_(nullptr),
132 dynsym_symbol_table_(nullptr),
133 jit_elf_image_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -0700134 jit_gdb_entry_(nullptr),
135 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -0700136 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800137}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800138
David Srbecky533c2072015-04-22 12:20:22 +0100139template <typename ElfTypes>
140ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(
141 File* file, bool writable, bool program_header_only,
142 std::string* error_msg, uint8_t* requested_base) {
143 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
144 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145 int prot;
146 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700147 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 prot = PROT_READ | PROT_WRITE;
149 flags = MAP_SHARED;
150 } else {
151 prot = PROT_READ;
152 flags = MAP_PRIVATE;
153 }
Alex Light3470ab42014-06-18 10:35:45 -0700154 if (!elf_file->Setup(prot, flags, error_msg)) {
155 return nullptr;
156 }
157 return elf_file.release();
158}
159
David Srbecky533c2072015-04-22 12:20:22 +0100160template <typename ElfTypes>
161ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(
162 File* file, int prot, int flags, std::string* error_msg) {
163 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
164 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
165 /*requested_base*/nullptr));
Alex Light3470ab42014-06-18 10:35:45 -0700166 if (!elf_file->Setup(prot, flags, error_msg)) {
167 return nullptr;
168 }
169 return elf_file.release();
170}
171
David Srbecky533c2072015-04-22 12:20:22 +0100172template <typename ElfTypes>
173bool ElfFileImpl<ElfTypes>::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800174 int64_t temp_file_length = file_->GetLength();
175 if (temp_file_length < 0) {
176 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
178 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800179 return false;
180 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800181 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700182 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800183 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700184 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700185 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186 return false;
187 }
188
Brian Carlstromc1409452014-02-26 14:06:23 -0800189 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800190 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700191 size_t elf_header_size = sizeof(Elf_Ehdr);
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800192 if (!SetMap(MemMap::MapFile(elf_header_size,
193 prot,
194 flags,
195 file_->Fd(),
196 0,
197 /*low4_gb*/false,
198 file_->GetPath().c_str(),
199 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800200 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203 // then remap to cover program header
204 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700205 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800206 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207 "header of %zd bytes: '%s'", file_length,
Tong Shen62d1ca32014-09-03 17:24:56 -0700208 sizeof(Elf_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700209 return false;
210 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800211 if (!SetMap(MemMap::MapFile(program_header_size,
212 prot,
213 flags,
214 file_->Fd(),
215 0,
216 /*low4_gb*/false,
217 file_->GetPath().c_str(),
218 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800219 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 return false;
222 }
223 } else {
224 // otherwise map entire file
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800225 if (!SetMap(MemMap::MapFile(file_->GetLength(),
226 prot,
227 flags,
228 file_->Fd(),
229 0,
230 /*low4_gb*/false,
231 file_->GetPath().c_str(),
232 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800233 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800235 return false;
236 }
237 }
238
Andreas Gampedaab38c2014-09-12 18:38:24 -0700239 if (program_header_only_) {
240 program_headers_start_ = Begin() + GetHeader().e_phoff;
241 } else {
242 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
243 return false;
244 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800245
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800246 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700247 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
248 return false;
249 }
250
251 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700252 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700253 if (shstrtab_section_header == nullptr) {
254 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
255 file_->GetPath().c_str());
256 return false;
257 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800258
259 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000260 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700261 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
263 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800264 return false;
265 }
266
Andreas Gampedaab38c2014-09-12 18:38:24 -0700267 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700268 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700269 return false;
270 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800271
272 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700273 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
274 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700275 if (section_header == nullptr) {
276 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
277 i, file_->GetPath().c_str());
278 return false;
279 }
280 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000281 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700282 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700283 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700284 return false;
285 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800286 break;
287 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000288 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700289 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700290 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700291 return false;
292 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800293 break;
294 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000295 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800296 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700297 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
298 // Check that this is named ".dynstr" and ignore otherwise.
299 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
300 if (strncmp(".dynstr", header_name, 8) == 0) {
301 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700302 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700303 return false;
304 }
305 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800306 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700307 // Check that this is named ".strtab" and ignore otherwise.
308 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
309 if (strncmp(".strtab", header_name, 8) == 0) {
310 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700311 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700312 return false;
313 }
314 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800315 }
316 break;
317 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000318 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700319 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700320 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800321 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800322 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800323 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700324 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800325 return false;
326 }
327 break;
328 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000329 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700330 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700331 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700332 return false;
333 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800334 break;
335 }
336 }
337 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700338
339 // Check for the existence of some sections.
340 if (!CheckSectionsExist(error_msg)) {
341 return false;
342 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800343 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700344
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800345 return true;
346}
347
David Srbecky533c2072015-04-22 12:20:22 +0100348template <typename ElfTypes>
349ElfFileImpl<ElfTypes>::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800350 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800351 delete symtab_symbol_table_;
352 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800353 delete jit_elf_image_;
354 if (jit_gdb_entry_) {
355 UnregisterCodeEntry(jit_gdb_entry_);
356 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800357}
358
David Srbecky533c2072015-04-22 12:20:22 +0100359template <typename ElfTypes>
360bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
361 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700362 if (Begin() + offset >= End()) {
363 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
364 file_->GetPath().c_str());
365 return false;
366 }
367 *target = Begin() + offset;
368 return true;
369}
370
David Srbecky533c2072015-04-22 12:20:22 +0100371template <typename ElfTypes>
372bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
373 const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700374 // Only works in whole-program mode, as we need to iterate over the sections.
375 // Note that we normally can't search by type, as duplicates are allowed for most section types.
376 if (program_header_only_) {
377 return true;
378 }
379
Tong Shen62d1ca32014-09-03 17:24:56 -0700380 Elf_Shdr* source_section = nullptr;
381 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700382 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700383 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
384 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700385
386 if (Begin() + section_header->sh_offset == source) {
387 // Found the source.
388 source_section = section_header;
389 if (target_index) {
390 break;
391 }
392 } else if (Begin() + section_header->sh_offset == target) {
393 target_index = i;
394 target_found = true;
395 if (source_section != nullptr) {
396 break;
397 }
398 }
399 }
400
401 return target_found && source_section != nullptr && source_section->sh_link == target_index;
402}
403
David Srbecky533c2072015-04-22 12:20:22 +0100404template <typename ElfTypes>
405bool ElfFileImpl<ElfTypes>::CheckSectionsExist(std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700406 if (!program_header_only_) {
407 // If in full mode, need section headers.
408 if (section_headers_start_ == nullptr) {
409 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
410 return false;
411 }
412 }
413
414 // This is redundant, but defensive.
415 if (dynamic_program_header_ == nullptr) {
416 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
417 file_->GetPath().c_str());
418 return false;
419 }
420
421 // Need a dynamic section. This is redundant, but defensive.
422 if (dynamic_section_start_ == nullptr) {
423 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
424 file_->GetPath().c_str());
425 return false;
426 }
427
428 // Symtab validation. These is not really a hard failure, as we are currently not using the
429 // symtab internally, but it's nice to be defensive.
430 if (symtab_section_start_ != nullptr) {
431 // When there's a symtab, there should be a strtab.
432 if (strtab_section_start_ == nullptr) {
433 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
434 return false;
435 }
436
437 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700438 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
439 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700440 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
441 file_->GetPath().c_str());
442 return false;
443 }
444 }
445
446 // We always need a dynstr & dynsym.
447 if (dynstr_section_start_ == nullptr) {
448 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
449 return false;
450 }
451 if (dynsym_section_start_ == nullptr) {
452 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
453 return false;
454 }
455
456 // Need a hash section for dynamic symbol lookup.
457 if (hash_section_start_ == nullptr) {
458 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
459 file_->GetPath().c_str());
460 return false;
461 }
462
463 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700464 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
465 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700466 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
467 file_->GetPath().c_str());
468 return false;
469 }
470
Andreas Gampea696c0a2014-12-10 20:51:45 -0800471 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
472 // us). This is usually the last in an oat file, and a good indicator of whether writing was
473 // successful (or the process crashed and left garbage).
474 if (program_header_only_) {
475 // It might not be mapped, but we can compare against the file size.
476 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
477 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
478 if (offset >= file_->GetLength()) {
479 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
480 file_->GetPath().c_str());
481 return false;
482 }
483 }
484
Andreas Gampedaab38c2014-09-12 18:38:24 -0700485 return true;
486}
487
David Srbecky533c2072015-04-22 12:20:22 +0100488template <typename ElfTypes>
489bool ElfFileImpl<ElfTypes>::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700490 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800491 // MemMap::Open should have already set an error.
492 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800493 return false;
494 }
495 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700496 CHECK(map_.get() != nullptr) << file_->GetPath();
497 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800498
Tong Shen62d1ca32014-09-03 17:24:56 -0700499 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000500 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
501 || (ELFMAG1 != header_->e_ident[EI_MAG1])
502 || (ELFMAG2 != header_->e_ident[EI_MAG2])
503 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800504 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
505 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800506 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000507 header_->e_ident[EI_MAG0],
508 header_->e_ident[EI_MAG1],
509 header_->e_ident[EI_MAG2],
510 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800511 return false;
512 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700513 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
514 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800515 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700516 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800517 file_->GetPath().c_str(),
518 header_->e_ident[EI_CLASS]);
519 return false;
520 }
521 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
522 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
523 ELFDATA2LSB,
524 file_->GetPath().c_str(),
525 header_->e_ident[EI_CLASS]);
526 return false;
527 }
528 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
529 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
530 EV_CURRENT,
531 file_->GetPath().c_str(),
532 header_->e_ident[EI_CLASS]);
533 return false;
534 }
535 if (ET_DYN != header_->e_type) {
536 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
537 ET_DYN,
538 file_->GetPath().c_str(),
539 header_->e_type);
540 return false;
541 }
542 if (EV_CURRENT != header_->e_version) {
543 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
544 EV_CURRENT,
545 file_->GetPath().c_str(),
546 header_->e_version);
547 return false;
548 }
549 if (0 != header_->e_entry) {
550 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
551 0,
552 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700553 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800554 return false;
555 }
556 if (0 == header_->e_phoff) {
557 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
558 file_->GetPath().c_str());
559 return false;
560 }
561 if (0 == header_->e_shoff) {
562 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
563 file_->GetPath().c_str());
564 return false;
565 }
566 if (0 == header_->e_ehsize) {
567 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
568 file_->GetPath().c_str());
569 return false;
570 }
571 if (0 == header_->e_phentsize) {
572 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
573 file_->GetPath().c_str());
574 return false;
575 }
576 if (0 == header_->e_phnum) {
577 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
578 file_->GetPath().c_str());
579 return false;
580 }
581 if (0 == header_->e_shentsize) {
582 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
583 file_->GetPath().c_str());
584 return false;
585 }
586 if (0 == header_->e_shnum) {
587 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
588 file_->GetPath().c_str());
589 return false;
590 }
591 if (0 == header_->e_shstrndx) {
592 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
593 file_->GetPath().c_str());
594 return false;
595 }
596 if (header_->e_shstrndx >= header_->e_shnum) {
597 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
598 header_->e_shstrndx,
599 header_->e_shnum,
600 file_->GetPath().c_str());
601 return false;
602 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800603
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800604 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800605 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700606 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
607 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800608 Size(),
609 file_->GetPath().c_str());
610 return false;
611 }
612 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700613 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
614 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800615 Size(),
616 file_->GetPath().c_str());
617 return false;
618 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800619 }
620 return true;
621}
622
David Srbecky533c2072015-04-22 12:20:22 +0100623template <typename ElfTypes>
624typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700625 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800626 return *header_;
627}
628
David Srbecky533c2072015-04-22 12:20:22 +0100629template <typename ElfTypes>
630uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700631 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
632 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800633 return program_headers_start_;
634}
635
David Srbecky533c2072015-04-22 12:20:22 +0100636template <typename ElfTypes>
637uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700638 CHECK(!program_header_only_); // Only used in "full" mode.
639 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800640 return section_headers_start_;
641}
642
David Srbecky533c2072015-04-22 12:20:22 +0100643template <typename ElfTypes>
644typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700645 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800646 return *dynamic_program_header_;
647}
648
David Srbecky533c2072015-04-22 12:20:22 +0100649template <typename ElfTypes>
650typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700651 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800652 return dynamic_section_start_;
653}
654
David Srbecky533c2072015-04-22 12:20:22 +0100655template <typename ElfTypes>
656typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
657 Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800658 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800659 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000660 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700661 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800662 break;
663 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000664 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700665 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800666 break;
667 }
668 default: {
669 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700670 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800671 }
672 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800673}
674
David Srbecky533c2072015-04-22 12:20:22 +0100675template <typename ElfTypes>
676const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
677 Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800678 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800679 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000680 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700681 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800682 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000683 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700684 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800685 }
686 default: {
687 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700688 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800689 }
690 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800691}
692
David Srbecky533c2072015-04-22 12:20:22 +0100693template <typename ElfTypes>
694const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
695 Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800696 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
697 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700698 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800699 }
700 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700701 if (string_section_start == nullptr) {
702 return nullptr;
703 }
704 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800705}
706
Andreas Gampedaab38c2014-09-12 18:38:24 -0700707// WARNING: The following methods do not check for an error condition (non-existent hash section).
708// It is the caller's job to do this.
709
David Srbecky533c2072015-04-22 12:20:22 +0100710template <typename ElfTypes>
711typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800712 return hash_section_start_;
713}
714
David Srbecky533c2072015-04-22 12:20:22 +0100715template <typename ElfTypes>
716typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800717 return GetHashSectionStart()[0];
718}
719
David Srbecky533c2072015-04-22 12:20:22 +0100720template <typename ElfTypes>
721typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800722 return GetHashSectionStart()[1];
723}
724
David Srbecky533c2072015-04-22 12:20:22 +0100725template <typename ElfTypes>
726typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700727 if (i >= GetHashBucketNum()) {
728 *ok = false;
729 return 0;
730 }
731 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800732 // 0 is nbucket, 1 is nchain
733 return GetHashSectionStart()[2 + i];
734}
735
David Srbecky533c2072015-04-22 12:20:22 +0100736template <typename ElfTypes>
737typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600738 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700739 *ok = false;
740 return 0;
741 }
742 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800743 // 0 is nbucket, 1 is nchain, & chains are after buckets
744 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
745}
746
David Srbecky533c2072015-04-22 12:20:22 +0100747template <typename ElfTypes>
748typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800749 return GetHeader().e_phnum;
750}
751
David Srbecky533c2072015-04-22 12:20:22 +0100752template <typename ElfTypes>
753typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700754 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700755 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700756 if (program_header >= End()) {
757 return nullptr; // Failure condition.
758 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700759 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800760}
761
David Srbecky533c2072015-04-22 12:20:22 +0100762template <typename ElfTypes>
763typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700764 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
765 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700766 if (program_header->p_type == type) {
767 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800768 }
769 }
Alex Light3470ab42014-06-18 10:35:45 -0700770 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800771}
772
David Srbecky533c2072015-04-22 12:20:22 +0100773template <typename ElfTypes>
774typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800775 return GetHeader().e_shnum;
776}
777
David Srbecky533c2072015-04-22 12:20:22 +0100778template <typename ElfTypes>
779typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800780 // Can only access arbitrary sections when we have the whole file, not just program header.
781 // Even if we Load(), it doesn't bring in all the sections.
782 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700783 if (i >= GetSectionHeaderNum()) {
784 return nullptr; // Failure condition.
785 }
Ian Rogers13735952014-10-08 12:43:28 -0700786 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700787 if (section_header >= End()) {
788 return nullptr; // Failure condition.
789 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700790 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800791}
792
David Srbecky533c2072015-04-22 12:20:22 +0100793template <typename ElfTypes>
794typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800795 // Can only access arbitrary sections when we have the whole file, not just program header.
796 // We could change this to switch on known types if they were detected during loading.
797 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700798 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
799 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700800 if (section_header->sh_type == type) {
801 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800802 }
803 }
Alex Light3470ab42014-06-18 10:35:45 -0700804 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800805}
806
807// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800808static unsigned elfhash(const char *_name) {
809 const unsigned char *name = (const unsigned char *) _name;
810 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800811
Brian Carlstromdf629502013-07-17 22:39:56 -0700812 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800813 h = (h << 4) + *name++;
814 g = h & 0xf0000000;
815 h ^= g;
816 h ^= g >> 24;
817 }
818 return h;
819}
820
David Srbecky533c2072015-04-22 12:20:22 +0100821template <typename ElfTypes>
822typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800823 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800824}
825
David Srbecky533c2072015-04-22 12:20:22 +0100826template <typename ElfTypes>
827const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
828 const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700829 // Check that we have a hash section.
830 if (GetHashSectionStart() == nullptr) {
831 return nullptr; // Failure condition.
832 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700833 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700834 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700835 // TODO: we need to change this to calculate base_address_ in ::Open,
836 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700837 return base_address_ + sym->st_value;
838 } else {
839 return nullptr;
840 }
841}
842
Andreas Gampedaab38c2014-09-12 18:38:24 -0700843// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
David Srbecky533c2072015-04-22 12:20:22 +0100844template <typename ElfTypes>
845const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
846 const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700847 if (GetHashBucketNum() == 0) {
848 // No dynamic symbols at all.
849 return nullptr;
850 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700851 Elf_Word hash = elfhash(symbol_name.c_str());
852 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700853 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700854 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700855 if (!ok) {
856 return nullptr;
857 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800858 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700859 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700860 if (symbol == nullptr) {
861 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800862 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700863 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
864 if (symbol_name == name) {
865 return symbol;
866 }
867 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
868 if (!ok) {
869 return nullptr;
870 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800871 }
Alex Light3470ab42014-06-18 10:35:45 -0700872 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800873}
874
David Srbecky533c2072015-04-22 12:20:22 +0100875template <typename ElfTypes>
876bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700877 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
878}
879
David Srbecky533c2072015-04-22 12:20:22 +0100880template <typename ElfTypes>
881typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800882 CHECK(IsSymbolSectionType(section_header.sh_type))
883 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
885 return section_header.sh_size / section_header.sh_entsize;
886}
887
David Srbecky533c2072015-04-22 12:20:22 +0100888template <typename ElfTypes>
889typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700890 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700891 if (sym_start == nullptr) {
892 return nullptr;
893 }
894 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800895}
896
David Srbecky533c2072015-04-22 12:20:22 +0100897template <typename ElfTypes>
898typename ElfFileImpl<ElfTypes>::SymbolTable**
899ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800900 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
901 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000902 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800903 return &symtab_symbol_table_;
904 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000905 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800906 return &dynsym_symbol_table_;
907 }
908 default: {
909 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700910 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800911 }
912 }
913}
914
David Srbecky533c2072015-04-22 12:20:22 +0100915template <typename ElfTypes>
916typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
917 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800918 CHECK(!program_header_only_) << file_->GetPath();
919 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800920
921 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700922 if (*symbol_table != nullptr || build_map) {
923 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800924 DCHECK(build_map);
925 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -0700926 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700927 if (symbol_section == nullptr) {
928 return nullptr; // Failure condition.
929 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700930 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700931 if (string_section == nullptr) {
932 return nullptr; // Failure condition.
933 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800934 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700935 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700936 if (symbol == nullptr) {
937 return nullptr; // Failure condition.
938 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700939 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
940 ? ELF64_ST_TYPE(symbol->st_info)
941 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000942 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800943 continue;
944 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700945 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700946 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800947 continue;
948 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700949 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -0700950 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800951 if (!result.second) {
952 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700953 if ((symbol->st_value != result.first->second->st_value) ||
954 (symbol->st_size != result.first->second->st_size) ||
955 (symbol->st_info != result.first->second->st_info) ||
956 (symbol->st_other != result.first->second->st_other) ||
957 (symbol->st_shndx != result.first->second->st_shndx)) {
958 return nullptr; // Failure condition.
959 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800960 }
961 }
962 }
Alex Light3470ab42014-06-18 10:35:45 -0700963 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -0700964 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800965 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700966 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800967 }
968 return it->second;
969 }
970
971 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -0700972 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700973 if (symbol_section == nullptr) {
974 return nullptr;
975 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700976 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700977 if (string_section == nullptr) {
978 return nullptr;
979 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800980 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700981 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700982 if (symbol == nullptr) {
983 return nullptr; // Failure condition.
984 }
985 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700986 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800987 continue;
988 }
989 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700990 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800991 }
992 }
Alex Light3470ab42014-06-18 10:35:45 -0700993 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800994}
995
David Srbecky533c2072015-04-22 12:20:22 +0100996template <typename ElfTypes>
997typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
998 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700999 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001000 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001001 return 0;
1002 }
1003 return symbol->st_value;
1004}
1005
David Srbecky533c2072015-04-22 12:20:22 +01001006template <typename ElfTypes>
1007const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
1008 Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001009 CHECK(!program_header_only_) << file_->GetPath();
1010 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001011 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001012 return nullptr; // Failure condition.
1013 }
1014 if (i >= string_section.sh_size) {
1015 return nullptr;
1016 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001017 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001018 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001019 }
Ian Rogers13735952014-10-08 12:43:28 -07001020 uint8_t* strings = Begin() + string_section.sh_offset;
1021 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001022 if (string >= End()) {
1023 return nullptr;
1024 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001025 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001026}
1027
David Srbecky533c2072015-04-22 12:20:22 +01001028template <typename ElfTypes>
1029typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001030 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001031}
1032
David Srbecky533c2072015-04-22 12:20:22 +01001033template <typename ElfTypes>
1034typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001035 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1036 return *(GetDynamicSectionStart() + i);
1037}
1038
David Srbecky533c2072015-04-22 12:20:22 +01001039template <typename ElfTypes>
1040typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001041 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1042 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001043 if (dyn->d_tag == type) {
1044 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001045 }
1046 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001047 return nullptr;
Alex Light53cb16b2014-06-12 11:26:29 -07001048}
1049
David Srbecky533c2072015-04-22 12:20:22 +01001050template <typename ElfTypes>
1051typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001052 Elf_Dyn* dyn = FindDynamicByType(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001053 if (dyn == nullptr) {
Alex Light53cb16b2014-06-12 11:26:29 -07001054 return 0;
1055 } else {
1056 return dyn->d_un.d_val;
1057 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001058}
1059
David Srbecky533c2072015-04-22 12:20:22 +01001060template <typename ElfTypes>
1061typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001062 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001063 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001064}
1065
David Srbecky533c2072015-04-22 12:20:22 +01001066template <typename ElfTypes>
1067typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001068 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001069 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1070 return section_header.sh_size / section_header.sh_entsize;
1071}
1072
David Srbecky533c2072015-04-22 12:20:22 +01001073template <typename ElfTypes>
1074typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001075 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001076 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1077 return *(GetRelSectionStart(section_header) + i);
1078}
1079
David Srbecky533c2072015-04-22 12:20:22 +01001080template <typename ElfTypes>
1081typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001082 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001083 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001084}
1085
David Srbecky533c2072015-04-22 12:20:22 +01001086template <typename ElfTypes>
1087typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001088 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001089 return section_header.sh_size / section_header.sh_entsize;
1090}
1091
David Srbecky533c2072015-04-22 12:20:22 +01001092template <typename ElfTypes>
1093typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001094 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001095 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1096 return *(GetRelaSectionStart(section_header) + i);
1097}
1098
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001099// Base on bionic phdr_table_get_load_size
David Srbecky533c2072015-04-22 12:20:22 +01001100template <typename ElfTypes>
Vladimir Marko3fc99032015-05-13 19:06:30 +01001101bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
1102 Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1);
1103 Elf_Addr max_vaddr = 0u;
Tong Shen62d1ca32014-09-03 17:24:56 -07001104 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1105 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001106 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001107 continue;
1108 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001109 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110 if (begin_vaddr < min_vaddr) {
1111 min_vaddr = begin_vaddr;
1112 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001113 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001114 if (UNLIKELY(begin_vaddr > end_vaddr)) {
1115 std::ostringstream oss;
1116 oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex
1117 << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr
1118 << " in ELF file \"" << file_->GetPath() << "\"";
1119 *error_msg = oss.str();
1120 *size = static_cast<size_t>(-1);
1121 return false;
1122 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001123 if (end_vaddr > max_vaddr) {
1124 max_vaddr = end_vaddr;
1125 }
1126 }
1127 min_vaddr = RoundDown(min_vaddr, kPageSize);
1128 max_vaddr = RoundUp(max_vaddr, kPageSize);
1129 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
Vladimir Marko3fc99032015-05-13 19:06:30 +01001130 Elf_Addr loaded_size = max_vaddr - min_vaddr;
1131 // Check that the loaded_size fits in size_t.
1132 if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) {
1133 std::ostringstream oss;
1134 oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x"
1135 << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_->GetPath() << "\"";
1136 *error_msg = oss.str();
1137 *size = static_cast<size_t>(-1);
1138 return false;
1139 }
1140 *size = loaded_size;
1141 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001142}
1143
David Srbecky533c2072015-04-22 12:20:22 +01001144template <typename ElfTypes>
1145bool ElfFileImpl<ElfTypes>::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001146 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001147
1148 if (executable) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001149 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001150 if (elf_ISA != kRuntimeISA) {
1151 std::ostringstream oss;
1152 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1153 *error_msg = oss.str();
1154 return false;
1155 }
1156 }
1157
Jim_Guoa62a5882014-04-28 11:11:57 +08001158 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001159 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1160 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001161 if (program_header == nullptr) {
1162 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1163 i, file_->GetPath().c_str());
1164 return false;
1165 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001166
1167 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001168 if (program_header->p_type == PT_DYNAMIC) {
1169 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001170 continue;
1171 }
1172
1173 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001174 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001175 continue;
1176 }
1177
1178 // Found something to load.
1179
Jim_Guoa62a5882014-04-28 11:11:57 +08001180 // Before load the actual segments, reserve a contiguous chunk
1181 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001182 // permissions. We'll then carve that up with the proper
1183 // permissions as we load the actual segments. If p_vaddr is
1184 // non-zero, the segments require the specific address specified,
1185 // which either was specified in the file because we already set
1186 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001187 int64_t temp_file_length = file_->GetLength();
1188 if (temp_file_length < 0) {
1189 errno = -temp_file_length;
1190 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1191 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1192 return false;
1193 }
1194 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001195 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001196 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1197 uint8_t* reserve_base_override = reserve_base;
1198 // Override the base (e.g. when compiling with --compile-pic)
1199 if (requested_base_ != nullptr) {
1200 reserve_base_override = requested_base_;
1201 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001202 std::string reservation_name("ElfFile reservation for ");
1203 reservation_name += file_->GetPath();
Vladimir Marko3fc99032015-05-13 19:06:30 +01001204 size_t loaded_size;
1205 if (!GetLoadedSize(&loaded_size, error_msg)) {
1206 DCHECK(!error_msg->empty());
1207 return false;
1208 }
Ian Rogers700a4022014-05-19 16:49:03 -07001209 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001210 reserve_base_override,
Vladimir Marko3fc99032015-05-13 19:06:30 +01001211 loaded_size, PROT_NONE, false, false,
Jim_Guoa62a5882014-04-28 11:11:57 +08001212 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001213 if (reserve.get() == nullptr) {
1214 *error_msg = StringPrintf("Failed to allocate %s: %s",
1215 reservation_name.c_str(), error_msg->c_str());
1216 return false;
1217 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001218 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001219
1220 // Base address is the difference of actual mapped location and the p_vaddr
1221 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1222 - reinterpret_cast<uintptr_t>(reserve_base));
1223 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1224 // dynamic memory address of where that object is actually mapped
1225 //
1226 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1227 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001228 segments_.push_back(reserve.release());
1229 }
1230 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001231 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001232 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001233 }
Ian Rogers13735952014-10-08 12:43:28 -07001234 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001235 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001236 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001237 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001238 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001239 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001240 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001241 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001242 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001243 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001244 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001245 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001246 if (writable_) {
1247 prot |= PROT_WRITE;
1248 flags |= MAP_SHARED;
1249 } else {
1250 flags |= MAP_PRIVATE;
1251 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001252 if (program_header->p_filesz > program_header->p_memsz) {
1253 *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1254 static_cast<uint64_t>(program_header->p_filesz),
1255 static_cast<uint64_t>(program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001256 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001257 return false;
1258 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001259 if (program_header->p_filesz < program_header->p_memsz &&
1260 !IsAligned<kPageSize>(program_header->p_filesz)) {
1261 *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1262 " < %" PRIu64 "): %s",
1263 static_cast<uint64_t>(program_header->p_filesz),
1264 static_cast<uint64_t>(program_header->p_memsz),
1265 file_->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001266 return false;
1267 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001268 if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1269 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1270 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1271 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
1272 file_->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001273 return false;
1274 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001275 if (program_header->p_filesz != 0u) {
1276 std::unique_ptr<MemMap> segment(
1277 MemMap::MapFileAtAddress(p_vaddr,
1278 program_header->p_filesz,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001279 prot,
1280 flags,
1281 file_->Fd(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001282 program_header->p_offset,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001283 /*low4_gb*/false,
1284 /*reuse*/true, // implies MAP_FIXED
Vladimir Marko5c42c292015-02-25 12:02:49 +00001285 file_->GetPath().c_str(),
1286 error_msg));
1287 if (segment.get() == nullptr) {
1288 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1289 i, file_->GetPath().c_str(), error_msg->c_str());
1290 return false;
1291 }
1292 if (segment->Begin() != p_vaddr) {
1293 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1294 "instead mapped to %p",
1295 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1296 return false;
1297 }
1298 segments_.push_back(segment.release());
1299 }
1300 if (program_header->p_filesz < program_header->p_memsz) {
1301 std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
1302 static_cast<uint64_t>(i), file_->GetPath().c_str());
1303 std::unique_ptr<MemMap> segment(
1304 MemMap::MapAnonymous(name.c_str(),
1305 p_vaddr + program_header->p_filesz,
1306 program_header->p_memsz - program_header->p_filesz,
1307 prot, false, true /* reuse */, error_msg));
1308 if (segment == nullptr) {
1309 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
1310 i, file_->GetPath().c_str(), error_msg->c_str());
1311 return false;
1312 }
1313 if (segment->Begin() != p_vaddr) {
1314 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1315 "at expected address %p, instead mapped to %p",
1316 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1317 return false;
1318 }
1319 segments_.push_back(segment.release());
1320 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001321 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001322
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001323 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001324 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001325 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1326 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1327 file_->GetPath().c_str());
1328 return false;
1329 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001330 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001331
Tong Shen62d1ca32014-09-03 17:24:56 -07001332 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1333 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001334 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001335 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001336 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001337 if (!ValidPointer(d_ptr)) {
1338 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1339 d_ptr, file_->GetPath().c_str());
1340 return false;
1341 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001342 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001343 break;
1344 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001345 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001346 if (!ValidPointer(d_ptr)) {
1347 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1348 d_ptr, file_->GetPath().c_str());
1349 return false;
1350 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001351 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1352 break;
1353 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001354 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001355 if (!ValidPointer(d_ptr)) {
1356 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1357 d_ptr, file_->GetPath().c_str());
1358 return false;
1359 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001360 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001361 break;
1362 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001363 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001364 if (GetDynamicNum() != i+1) {
1365 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1366 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1367 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1368 return false;
1369 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001370 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001371 }
1372 }
1373 }
1374
Andreas Gampedaab38c2014-09-12 18:38:24 -07001375 // Check for the existence of some sections.
1376 if (!CheckSectionsExist(error_msg)) {
1377 return false;
1378 }
1379
Mark Mendellae9fd932014-02-10 16:14:35 -08001380 // Use GDB JIT support to do stack backtrace, etc.
1381 if (executable) {
1382 GdbJITSupport();
1383 }
1384
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001385 return true;
1386}
1387
David Srbecky533c2072015-04-22 12:20:22 +01001388template <typename ElfTypes>
1389bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001390 for (size_t i = 0; i < segments_.size(); ++i) {
1391 const MemMap* segment = segments_[i];
1392 if (segment->Begin() <= start && start < segment->End()) {
1393 return true;
1394 }
1395 }
1396 return false;
1397}
1398
Alex Light3470ab42014-06-18 10:35:45 -07001399
David Srbecky533c2072015-04-22 12:20:22 +01001400template <typename ElfTypes>
1401typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1402 const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001403 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001404 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001405 if (shstrtab_sec == nullptr) {
1406 return nullptr;
1407 }
Alex Light3470ab42014-06-18 10:35:45 -07001408 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001409 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001410 if (shdr == nullptr) {
1411 return nullptr;
1412 }
1413 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001414 if (sec_name == nullptr) {
1415 continue;
1416 }
1417 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001418 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001419 }
1420 }
1421 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001422}
1423
David Srbecky533c2072015-04-22 12:20:22 +01001424template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001425bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001426 if (base_address_delta == 0) {
1427 return true;
1428 }
David Srbeckyf8980872015-05-22 17:04:47 +01001429 return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1430 ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1431 ApplyOatPatchesTo(".debug_line", base_address_delta);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001432}
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001433
David Srbecky533c2072015-04-22 12:20:22 +01001434template <typename ElfTypes>
1435bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
David Srbeckyf8980872015-05-22 17:04:47 +01001436 const char* target_section_name, Elf_Addr delta) {
1437 auto target_section = FindSectionByName(target_section_name);
1438 if (target_section == nullptr) {
1439 return true;
1440 }
1441 std::string patches_name = target_section_name + std::string(".oat_patches");
1442 auto patches_section = FindSectionByName(patches_name.c_str());
David Srbecky2f6cdb02015-04-11 00:17:53 +01001443 if (patches_section == nullptr) {
David Srbeckyf8980872015-05-22 17:04:47 +01001444 LOG(ERROR) << patches_name << " section not found.";
Alex Light3470ab42014-06-18 10:35:45 -07001445 return false;
1446 }
David Srbecky2f6cdb02015-04-11 00:17:53 +01001447 if (patches_section->sh_type != SHT_OAT_PATCH) {
David Srbeckyf8980872015-05-22 17:04:47 +01001448 LOG(ERROR) << "Unexpected type of " << patches_name;
Alex Light3470ab42014-06-18 10:35:45 -07001449 return false;
1450 }
David Srbeckyf8980872015-05-22 17:04:47 +01001451 ApplyOatPatches(
David Srbecky2f6cdb02015-04-11 00:17:53 +01001452 Begin() + patches_section->sh_offset,
1453 Begin() + patches_section->sh_offset + patches_section->sh_size,
David Srbeckyf8980872015-05-22 17:04:47 +01001454 delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +01001455 Begin() + target_section->sh_offset,
David Srbeckyf8980872015-05-22 17:04:47 +01001456 Begin() + target_section->sh_offset + target_section->sh_size);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001457 return true;
1458}
1459
David Srbeckyf8980872015-05-22 17:04:47 +01001460// Apply LEB128 encoded patches to given section.
David Srbecky533c2072015-04-22 12:20:22 +01001461template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001462void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1463 const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky533c2072015-04-22 12:20:22 +01001464 uint8_t* to_patch, const uint8_t* to_patch_end) {
David Srbeckyf8980872015-05-22 17:04:47 +01001465 typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress;
1466 while (patches < patches_end) {
1467 to_patch += DecodeUnsignedLeb128(&patches);
1468 DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1469 DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1470 *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
David Srbecky2f6cdb02015-04-11 00:17:53 +01001471 }
Alex Light3470ab42014-06-18 10:35:45 -07001472}
Mark Mendellae9fd932014-02-10 16:14:35 -08001473
David Srbecky533c2072015-04-22 12:20:22 +01001474template <typename ElfTypes>
1475void ElfFileImpl<ElfTypes>::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08001476 // We only get here if we only are mapping the program header.
1477 DCHECK(program_header_only_);
1478
1479 // Well, we need the whole file to do this.
1480 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07001481 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
1482 // sections are there.
David Srbecky533c2072015-04-22 12:20:22 +01001483 std::unique_ptr<ElfFileImpl<ElfTypes>> all_ptr(
1484 Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07001485 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001486 return;
1487 }
David Srbecky533c2072015-04-22 12:20:22 +01001488 ElfFileImpl<ElfTypes>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07001489
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001490 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07001491 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001492 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001493 return;
1494 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001495
1496 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07001497 // We need to add in a strtab and symtab to the image.
1498 // all is MAP_PRIVATE so it can be written to freely.
1499 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07001500 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08001501 elf_hdr.e_entry = 0;
1502 elf_hdr.e_phoff = 0;
1503 elf_hdr.e_phnum = 0;
1504 elf_hdr.e_phentsize = 0;
1505 elf_hdr.e_type = ET_EXEC;
1506
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001507 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
1508 // and the actual address stuff starts at in regular files this is good.
1509 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07001510 LOG(ERROR) << "Failed to load GDB data";
1511 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08001512 }
1513
Alex Light3470ab42014-06-18 10:35:45 -07001514 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
1515 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08001516}
1517
David Srbecky533c2072015-04-22 12:20:22 +01001518template <typename ElfTypes>
1519bool ElfFileImpl<ElfTypes>::Strip(std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001520 // ELF files produced by MCLinker look roughly like this
1521 //
1522 // +------------+
1523 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
1524 // +------------+
1525 // | Elf_Phdr | program headers
1526 // | Elf_Phdr |
1527 // | ... |
1528 // | Elf_Phdr |
1529 // +------------+
1530 // | section | mixture of needed and unneeded sections
1531 // +------------+
1532 // | section |
1533 // +------------+
1534 // | ... |
1535 // +------------+
1536 // | section |
1537 // +------------+
1538 // | Elf_Shdr | section headers
1539 // | Elf_Shdr |
1540 // | ... | contains offset to section start
1541 // | Elf_Shdr |
1542 // +------------+
1543 //
1544 // To strip:
1545 // - leave the Elf_Ehdr and Elf_Phdr values in place.
1546 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1547 // - move the sections are keeping up to fill in gaps of sections we want to strip
1548 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1549 // - truncate rest of file
1550 //
1551
1552 std::vector<Elf_Shdr> section_headers;
1553 std::vector<Elf_Word> section_headers_original_indexes;
1554 section_headers.reserve(GetSectionHeaderNum());
1555
1556
1557 Elf_Shdr* string_section = GetSectionNameStringSection();
1558 CHECK(string_section != nullptr);
1559 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1560 Elf_Shdr* sh = GetSectionHeader(i);
1561 CHECK(sh != nullptr);
1562 const char* name = GetString(*string_section, sh->sh_name);
1563 if (name == nullptr) {
1564 CHECK_EQ(0U, i);
1565 section_headers.push_back(*sh);
1566 section_headers_original_indexes.push_back(0);
1567 continue;
1568 }
1569 if (StartsWith(name, ".debug")
1570 || (strcmp(name, ".strtab") == 0)
1571 || (strcmp(name, ".symtab") == 0)) {
1572 continue;
1573 }
1574 section_headers.push_back(*sh);
1575 section_headers_original_indexes.push_back(i);
1576 }
1577 CHECK_NE(0U, section_headers.size());
1578 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1579
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001580 // section 0 is the null section, sections start at offset of first section
Tong Shen62d1ca32014-09-03 17:24:56 -07001581 CHECK(GetSectionHeader(1) != nullptr);
1582 Elf_Off offset = GetSectionHeader(1)->sh_offset;
1583 for (size_t i = 1; i < section_headers.size(); i++) {
1584 Elf_Shdr& new_sh = section_headers[i];
1585 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1586 CHECK(old_sh != nullptr);
1587 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1588 if (old_sh->sh_addralign > 1) {
1589 offset = RoundUp(offset, old_sh->sh_addralign);
1590 }
1591 if (old_sh->sh_offset == offset) {
1592 // already in place
1593 offset += old_sh->sh_size;
1594 continue;
1595 }
1596 // shift section earlier
1597 memmove(Begin() + offset,
1598 Begin() + old_sh->sh_offset,
1599 old_sh->sh_size);
1600 new_sh.sh_offset = offset;
1601 offset += old_sh->sh_size;
1602 }
1603
1604 Elf_Off shoff = offset;
1605 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1606 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1607 offset += section_headers_size_in_bytes;
1608
1609 GetHeader().e_shnum = section_headers.size();
1610 GetHeader().e_shoff = shoff;
1611 int result = ftruncate(file_->Fd(), offset);
1612 if (result != 0) {
1613 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
1614 file_->GetPath().c_str(), strerror(errno));
1615 return false;
1616 }
1617 return true;
1618}
1619
1620static const bool DEBUG_FIXUP = false;
1621
David Srbecky533c2072015-04-22 12:20:22 +01001622template <typename ElfTypes>
1623bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001624 if (!FixupDynamic(base_address)) {
1625 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
1626 return false;
1627 }
1628 if (!FixupSectionHeaders(base_address)) {
1629 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
1630 return false;
1631 }
1632 if (!FixupProgramHeaders(base_address)) {
1633 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
1634 return false;
1635 }
1636 if (!FixupSymbols(base_address, true)) {
1637 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
1638 return false;
1639 }
1640 if (!FixupSymbols(base_address, false)) {
1641 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
1642 return false;
1643 }
1644 if (!FixupRelocations(base_address)) {
1645 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
1646 return false;
1647 }
Andreas Gampe3c54b002015-04-07 16:09:30 -07001648 static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1649 if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001650 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
1651 return false;
1652 }
1653 return true;
1654}
1655
David Srbecky533c2072015-04-22 12:20:22 +01001656template <typename ElfTypes>
1657bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001658 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1659 Elf_Dyn& elf_dyn = GetDynamic(i);
1660 Elf_Word d_tag = elf_dyn.d_tag;
1661 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1662 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1663 if (DEBUG_FIXUP) {
1664 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1665 GetFile().GetPath().c_str(), i,
1666 static_cast<uint64_t>(d_ptr),
1667 static_cast<uint64_t>(d_ptr + base_address));
1668 }
1669 d_ptr += base_address;
1670 elf_dyn.d_un.d_ptr = d_ptr;
1671 }
1672 }
1673 return true;
1674}
1675
David Srbecky533c2072015-04-22 12:20:22 +01001676template <typename ElfTypes>
1677bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001678 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1679 Elf_Shdr* sh = GetSectionHeader(i);
1680 CHECK(sh != nullptr);
1681 // 0 implies that the section will not exist in the memory of the process
1682 if (sh->sh_addr == 0) {
1683 continue;
1684 }
1685 if (DEBUG_FIXUP) {
1686 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1687 GetFile().GetPath().c_str(), i,
1688 static_cast<uint64_t>(sh->sh_addr),
1689 static_cast<uint64_t>(sh->sh_addr + base_address));
1690 }
1691 sh->sh_addr += base_address;
1692 }
1693 return true;
1694}
1695
David Srbecky533c2072015-04-22 12:20:22 +01001696template <typename ElfTypes>
1697bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001698 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1699 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1700 Elf_Phdr* ph = GetProgramHeader(i);
1701 CHECK(ph != nullptr);
1702 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
1703 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
1704 << GetFile().GetPath() << " i=" << i;
1705 if (DEBUG_FIXUP) {
1706 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1707 GetFile().GetPath().c_str(), i,
1708 static_cast<uint64_t>(ph->p_vaddr),
1709 static_cast<uint64_t>(ph->p_vaddr + base_address));
1710 }
1711 ph->p_vaddr += base_address;
1712 ph->p_paddr += base_address;
1713 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
1714 << GetFile().GetPath() << " i=" << i;
1715 }
1716 return true;
1717}
1718
David Srbecky533c2072015-04-22 12:20:22 +01001719template <typename ElfTypes>
1720bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001721 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1722 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1723 Elf_Shdr* symbol_section = FindSectionByType(section_type);
1724 if (symbol_section == nullptr) {
1725 // file is missing optional .symtab
1726 CHECK(!dynamic) << GetFile().GetPath();
1727 return true;
1728 }
1729 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1730 Elf_Sym* symbol = GetSymbol(section_type, i);
1731 CHECK(symbol != nullptr);
1732 if (symbol->st_value != 0) {
1733 if (DEBUG_FIXUP) {
1734 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1735 GetFile().GetPath().c_str(), i,
1736 static_cast<uint64_t>(symbol->st_value),
1737 static_cast<uint64_t>(symbol->st_value + base_address));
1738 }
1739 symbol->st_value += base_address;
1740 }
1741 }
1742 return true;
1743}
1744
David Srbecky533c2072015-04-22 12:20:22 +01001745template <typename ElfTypes>
1746bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001747 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1748 Elf_Shdr* sh = GetSectionHeader(i);
1749 CHECK(sh != nullptr);
1750 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001751 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1752 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001753 if (DEBUG_FIXUP) {
1754 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001755 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001756 static_cast<uint64_t>(rel.r_offset),
1757 static_cast<uint64_t>(rel.r_offset + base_address));
1758 }
1759 rel.r_offset += base_address;
1760 }
1761 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001762 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1763 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001764 if (DEBUG_FIXUP) {
1765 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001766 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001767 static_cast<uint64_t>(rela.r_offset),
1768 static_cast<uint64_t>(rela.r_offset + base_address));
1769 }
1770 rela.r_offset += base_address;
1771 }
1772 }
1773 }
1774 return true;
1775}
1776
1777// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +01001778template class ElfFileImpl<ElfTypes32>;
1779template class ElfFileImpl<ElfTypes64>;
Tong Shen62d1ca32014-09-03 17:24:56 -07001780
Ian Rogersd4c4d952014-10-16 20:31:53 -07001781ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001782}
1783
Ian Rogersd4c4d952014-10-16 20:31:53 -07001784ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001785}
1786
1787ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001788 // Should never have 32 and 64-bit impls.
1789 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001790}
1791
Igor Murashkin46774762014-10-22 11:37:02 -07001792ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
1793 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001794 if (file->GetLength() < EI_NIDENT) {
1795 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1796 file->GetPath().c_str());
1797 return nullptr;
1798 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001799 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1800 PROT_READ,
1801 MAP_PRIVATE,
1802 file->Fd(),
1803 0,
1804 /*low4_gb*/false,
1805 file->GetPath().c_str(),
1806 error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001807 if (map == nullptr && map->Size() != EI_NIDENT) {
1808 return nullptr;
1809 }
Ian Rogers13735952014-10-08 12:43:28 -07001810 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001811 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07001812 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
1813 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07001814 if (elf_file_impl == nullptr)
1815 return nullptr;
1816 return new ElfFile(elf_file_impl);
1817 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07001818 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
1819 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001820 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001821 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001822 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001823 return new ElfFile(elf_file_impl);
1824 } else {
1825 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1826 ELFCLASS32, ELFCLASS64,
1827 file->GetPath().c_str(),
1828 header[EI_CLASS]);
1829 return nullptr;
1830 }
1831}
1832
1833ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
1834 if (file->GetLength() < EI_NIDENT) {
1835 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1836 file->GetPath().c_str());
1837 return nullptr;
1838 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001839 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1840 PROT_READ,
1841 MAP_PRIVATE,
1842 file->Fd(),
1843 0,
1844 /*low4_gb*/false,
1845 file->GetPath().c_str(),
1846 error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001847 if (map == nullptr && map->Size() != EI_NIDENT) {
1848 return nullptr;
1849 }
Ian Rogers13735952014-10-08 12:43:28 -07001850 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001851 if (header[EI_CLASS] == ELFCLASS64) {
1852 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001853 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001854 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001855 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001856 return new ElfFile(elf_file_impl);
1857 } else if (header[EI_CLASS] == ELFCLASS32) {
1858 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001859 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001860 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001861 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001862 return new ElfFile(elf_file_impl);
1863 } else {
1864 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1865 ELFCLASS32, ELFCLASS64,
1866 file->GetPath().c_str(),
1867 header[EI_CLASS]);
1868 return nullptr;
1869 }
1870}
1871
1872#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001873 if (elf64_.get() != nullptr) { \
1874 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001875 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001876 DCHECK(elf32_.get() != nullptr); \
1877 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001878 }
1879
1880bool ElfFile::Load(bool executable, std::string* error_msg) {
1881 DELEGATE_TO_IMPL(Load, executable, error_msg);
1882}
1883
Ian Rogers13735952014-10-08 12:43:28 -07001884const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001885 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1886}
1887
1888size_t ElfFile::Size() const {
1889 DELEGATE_TO_IMPL(Size);
1890}
1891
Ian Rogers13735952014-10-08 12:43:28 -07001892uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001893 DELEGATE_TO_IMPL(Begin);
1894}
1895
Ian Rogers13735952014-10-08 12:43:28 -07001896uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001897 DELEGATE_TO_IMPL(End);
1898}
1899
1900const File& ElfFile::GetFile() const {
1901 DELEGATE_TO_IMPL(GetFile);
1902}
1903
Alex Light0eb76d22015-08-11 18:03:47 -07001904bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1905 uint64_t* size) const {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001906 if (elf32_.get() == nullptr) {
1907 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001908
Ian Rogersd4c4d952014-10-16 20:31:53 -07001909 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1910 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001911 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001912 }
1913 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001914 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001915 }
1916 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001917 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001918 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001919 return true;
1920 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001921 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1922 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001923 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001924 }
1925 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001926 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001927 }
1928 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001929 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001930 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001931 return true;
1932 }
1933}
1934
1935uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1936 const std::string& symbol_name,
1937 bool build_map) {
1938 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1939}
1940
Vladimir Marko3fc99032015-05-13 19:06:30 +01001941bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1942 DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001943}
1944
1945bool ElfFile::Strip(File* file, std::string* error_msg) {
1946 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
1947 if (elf_file.get() == nullptr) {
1948 return false;
1949 }
1950
Ian Rogersd4c4d952014-10-16 20:31:53 -07001951 if (elf_file->elf64_.get() != nullptr)
1952 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001953 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07001954 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001955}
1956
Andreas Gampe3c54b002015-04-07 16:09:30 -07001957bool ElfFile::Fixup(uint64_t base_address) {
1958 if (elf64_.get() != nullptr) {
1959 return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1960 } else {
1961 DCHECK(elf32_.get() != nullptr);
1962 CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1963 return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1964 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001965 DELEGATE_TO_IMPL(Fixup, base_address);
1966}
1967
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001968} // namespace art