blob: b6df6097bd07514c697fe46f2f6130f0ad4ca195 [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"
Alex Light3470ab42014-06-18 10:35:45 -070028#include "dwarf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070029#include "elf_file_impl.h"
30#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070031#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032#include "utils.h"
33
34namespace art {
35
Mark Mendellae9fd932014-02-10 16:14:35 -080036// -------------------------------------------------------------------
37// Binary GDB JIT Interface as described in
38// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
39extern "C" {
40 typedef enum {
41 JIT_NOACTION = 0,
42 JIT_REGISTER_FN,
43 JIT_UNREGISTER_FN
44 } JITAction;
45
46 struct JITCodeEntry {
47 JITCodeEntry* next_;
48 JITCodeEntry* prev_;
Ian Rogers13735952014-10-08 12:43:28 -070049 const uint8_t *symfile_addr_;
Mark Mendellae9fd932014-02-10 16:14:35 -080050 uint64_t symfile_size_;
51 };
52
53 struct JITDescriptor {
54 uint32_t version_;
55 uint32_t action_flag_;
56 JITCodeEntry* relevant_entry_;
57 JITCodeEntry* first_entry_;
58 };
59
60 // GDB will place breakpoint into this function.
61 // To prevent GCC from inlining or removing it we place noinline attribute
62 // and inline assembler statement inside.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080063 void __attribute__((noinline)) __jit_debug_register_code();
Mark Mendellae9fd932014-02-10 16:14:35 -080064 void __attribute__((noinline)) __jit_debug_register_code() {
65 __asm__("");
66 }
67
68 // GDB will inspect contents of this descriptor.
69 // Static initialization is necessary to prevent GDB from seeing
70 // uninitialized descriptor.
71 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
72}
73
74
Ian Rogers13735952014-10-08 12:43:28 -070075static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr,
Mark Mendellae9fd932014-02-10 16:14:35 -080076 uintptr_t symfile_size) {
77 JITCodeEntry* entry = new JITCodeEntry;
78 entry->symfile_addr_ = symfile_addr;
79 entry->symfile_size_ = symfile_size;
80 entry->prev_ = nullptr;
81
82 // TODO: Do we need a lock here?
83 entry->next_ = __jit_debug_descriptor.first_entry_;
84 if (entry->next_ != nullptr) {
85 entry->next_->prev_ = entry;
86 }
87 __jit_debug_descriptor.first_entry_ = entry;
88 __jit_debug_descriptor.relevant_entry_ = entry;
89
90 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
91 __jit_debug_register_code();
92 return entry;
93}
94
95
96static void UnregisterCodeEntry(JITCodeEntry* entry) {
97 // TODO: Do we need a lock here?
98 if (entry->prev_ != nullptr) {
99 entry->prev_->next_ = entry->next_;
100 } else {
101 __jit_debug_descriptor.first_entry_ = entry->next_;
102 }
103
104 if (entry->next_ != nullptr) {
105 entry->next_->prev_ = entry->prev_;
106 }
107
108 __jit_debug_descriptor.relevant_entry_ = entry;
109 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
110 __jit_debug_register_code();
111 delete entry;
112}
113
Tong Shen62d1ca32014-09-03 17:24:56 -0700114template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
115 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
116 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
117ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
118 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700119 ::ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base)
Brian Carlstromc1409452014-02-26 14:06:23 -0800120 : file_(file),
121 writable_(writable),
122 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700123 header_(nullptr),
124 base_address_(nullptr),
125 program_headers_start_(nullptr),
126 section_headers_start_(nullptr),
127 dynamic_program_header_(nullptr),
128 dynamic_section_start_(nullptr),
129 symtab_section_start_(nullptr),
130 dynsym_section_start_(nullptr),
131 strtab_section_start_(nullptr),
132 dynstr_section_start_(nullptr),
133 hash_section_start_(nullptr),
134 symtab_symbol_table_(nullptr),
135 dynsym_symbol_table_(nullptr),
136 jit_elf_image_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -0700137 jit_gdb_entry_(nullptr),
138 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -0700139 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800140}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800141
Tong Shen62d1ca32014-09-03 17:24:56 -0700142template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
143 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
144 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
145ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
146 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
147 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
148 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
149 ::Open(File* file, bool writable, bool program_header_only,
Igor Murashkin46774762014-10-22 11:37:02 -0700150 std::string* error_msg, uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700151 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
152 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
153 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
154 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700155 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 int prot;
157 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700158 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 prot = PROT_READ | PROT_WRITE;
160 flags = MAP_SHARED;
161 } else {
162 prot = PROT_READ;
163 flags = MAP_PRIVATE;
164 }
Alex Light3470ab42014-06-18 10:35:45 -0700165 if (!elf_file->Setup(prot, flags, error_msg)) {
166 return nullptr;
167 }
168 return elf_file.release();
169}
170
Tong Shen62d1ca32014-09-03 17:24:56 -0700171template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
172 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
173 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
174ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
175 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
176 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
177 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
178 ::Open(File* file, int prot, int flags, std::string* error_msg) {
179 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
180 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
181 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
182 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700183 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
184 /*requested_base*/nullptr));
Alex Light3470ab42014-06-18 10:35:45 -0700185 if (!elf_file->Setup(prot, flags, error_msg)) {
186 return nullptr;
187 }
188 return elf_file.release();
189}
190
Tong Shen62d1ca32014-09-03 17:24:56 -0700191template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
192 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
193 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
194bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
195 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
196 ::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800197 int64_t temp_file_length = file_->GetLength();
198 if (temp_file_length < 0) {
199 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
201 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800202 return false;
203 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800204 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700205 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800206 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700207 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 return false;
210 }
211
Brian Carlstromc1409452014-02-26 14:06:23 -0800212 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700214 size_t elf_header_size = sizeof(Elf_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800216 file_->GetPath().c_str(), error_msg),
217 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 return false;
219 }
220 // then remap to cover program header
221 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700222 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800223 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700224 "header of %zd bytes: '%s'", file_length,
Tong Shen62d1ca32014-09-03 17:24:56 -0700225 sizeof(Elf_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700226 return false;
227 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800229 file_->GetPath().c_str(), error_msg),
230 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232 return false;
233 }
234 } else {
235 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800237 file_->GetPath().c_str(), error_msg),
238 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800240 return false;
241 }
242 }
243
Andreas Gampedaab38c2014-09-12 18:38:24 -0700244 if (program_header_only_) {
245 program_headers_start_ = Begin() + GetHeader().e_phoff;
246 } else {
247 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
248 return false;
249 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700252 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
253 return false;
254 }
255
256 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700257 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700258 if (shstrtab_section_header == nullptr) {
259 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
260 file_->GetPath().c_str());
261 return false;
262 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263
264 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000265 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700266 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
268 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269 return false;
270 }
271
Andreas Gampedaab38c2014-09-12 18:38:24 -0700272 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700273 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700274 return false;
275 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276
277 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700278 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
279 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700280 if (section_header == nullptr) {
281 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
282 i, file_->GetPath().c_str());
283 return false;
284 }
285 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000286 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700287 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700288 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700289 return false;
290 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800291 break;
292 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000293 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700294 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700295 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700296 return false;
297 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800298 break;
299 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000300 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800301 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700302 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
303 // Check that this is named ".dynstr" and ignore otherwise.
304 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
305 if (strncmp(".dynstr", header_name, 8) == 0) {
306 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700307 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700308 return false;
309 }
310 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800311 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700312 // Check that this is named ".strtab" and ignore otherwise.
313 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
314 if (strncmp(".strtab", header_name, 8) == 0) {
315 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700316 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700317 return false;
318 }
319 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800320 }
321 break;
322 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000323 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700324 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700325 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800326 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800327 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800328 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700329 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800330 return false;
331 }
332 break;
333 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000334 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700335 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700336 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700337 return false;
338 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 break;
340 }
341 }
342 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700343
344 // Check for the existence of some sections.
345 if (!CheckSectionsExist(error_msg)) {
346 return false;
347 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800348 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700349
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800350 return true;
351}
352
Tong Shen62d1ca32014-09-03 17:24:56 -0700353template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
354 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
355 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
356ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
357 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
358 ::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800359 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800360 delete symtab_symbol_table_;
361 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800362 delete jit_elf_image_;
363 if (jit_gdb_entry_) {
364 UnregisterCodeEntry(jit_gdb_entry_);
365 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800366}
367
Tong Shen62d1ca32014-09-03 17:24:56 -0700368template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
369 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
370 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
371bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
372 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
373 ::CheckAndSet(Elf32_Off offset, const char* label,
Ian Rogers13735952014-10-08 12:43:28 -0700374 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700375 if (Begin() + offset >= End()) {
376 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
377 file_->GetPath().c_str());
378 return false;
379 }
380 *target = Begin() + offset;
381 return true;
382}
383
Tong Shen62d1ca32014-09-03 17:24:56 -0700384template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
385 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
386 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
387bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
388 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700389 ::CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700390 // Only works in whole-program mode, as we need to iterate over the sections.
391 // Note that we normally can't search by type, as duplicates are allowed for most section types.
392 if (program_header_only_) {
393 return true;
394 }
395
Tong Shen62d1ca32014-09-03 17:24:56 -0700396 Elf_Shdr* source_section = nullptr;
397 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700398 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700399 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
400 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700401
402 if (Begin() + section_header->sh_offset == source) {
403 // Found the source.
404 source_section = section_header;
405 if (target_index) {
406 break;
407 }
408 } else if (Begin() + section_header->sh_offset == target) {
409 target_index = i;
410 target_found = true;
411 if (source_section != nullptr) {
412 break;
413 }
414 }
415 }
416
417 return target_found && source_section != nullptr && source_section->sh_link == target_index;
418}
419
Tong Shen62d1ca32014-09-03 17:24:56 -0700420template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
421 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
422 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
423bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
424 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
425 ::CheckSectionsExist(std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700426 if (!program_header_only_) {
427 // If in full mode, need section headers.
428 if (section_headers_start_ == nullptr) {
429 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
430 return false;
431 }
432 }
433
434 // This is redundant, but defensive.
435 if (dynamic_program_header_ == nullptr) {
436 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
437 file_->GetPath().c_str());
438 return false;
439 }
440
441 // Need a dynamic section. This is redundant, but defensive.
442 if (dynamic_section_start_ == nullptr) {
443 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
444 file_->GetPath().c_str());
445 return false;
446 }
447
448 // Symtab validation. These is not really a hard failure, as we are currently not using the
449 // symtab internally, but it's nice to be defensive.
450 if (symtab_section_start_ != nullptr) {
451 // When there's a symtab, there should be a strtab.
452 if (strtab_section_start_ == nullptr) {
453 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
454 return false;
455 }
456
457 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700458 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
459 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700460 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
461 file_->GetPath().c_str());
462 return false;
463 }
464 }
465
466 // We always need a dynstr & dynsym.
467 if (dynstr_section_start_ == nullptr) {
468 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
469 return false;
470 }
471 if (dynsym_section_start_ == nullptr) {
472 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
473 return false;
474 }
475
476 // Need a hash section for dynamic symbol lookup.
477 if (hash_section_start_ == nullptr) {
478 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
479 file_->GetPath().c_str());
480 return false;
481 }
482
483 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700484 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
485 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700486 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
487 file_->GetPath().c_str());
488 return false;
489 }
490
Andreas Gampea696c0a2014-12-10 20:51:45 -0800491 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
492 // us). This is usually the last in an oat file, and a good indicator of whether writing was
493 // successful (or the process crashed and left garbage).
494 if (program_header_only_) {
495 // It might not be mapped, but we can compare against the file size.
496 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
497 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
498 if (offset >= file_->GetLength()) {
499 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
500 file_->GetPath().c_str());
501 return false;
502 }
503 }
504
Andreas Gampedaab38c2014-09-12 18:38:24 -0700505 return true;
506}
507
Tong Shen62d1ca32014-09-03 17:24:56 -0700508template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
509 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
510 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
511bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
512 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
513 ::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700514 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800515 // MemMap::Open should have already set an error.
516 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800517 return false;
518 }
519 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700520 CHECK(map_.get() != nullptr) << file_->GetPath();
521 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800522
Tong Shen62d1ca32014-09-03 17:24:56 -0700523 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000524 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
525 || (ELFMAG1 != header_->e_ident[EI_MAG1])
526 || (ELFMAG2 != header_->e_ident[EI_MAG2])
527 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800528 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
529 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800530 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000531 header_->e_ident[EI_MAG0],
532 header_->e_ident[EI_MAG1],
533 header_->e_ident[EI_MAG2],
534 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800535 return false;
536 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700537 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
538 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800539 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700540 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800541 file_->GetPath().c_str(),
542 header_->e_ident[EI_CLASS]);
543 return false;
544 }
545 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
546 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
547 ELFDATA2LSB,
548 file_->GetPath().c_str(),
549 header_->e_ident[EI_CLASS]);
550 return false;
551 }
552 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
553 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
554 EV_CURRENT,
555 file_->GetPath().c_str(),
556 header_->e_ident[EI_CLASS]);
557 return false;
558 }
559 if (ET_DYN != header_->e_type) {
560 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
561 ET_DYN,
562 file_->GetPath().c_str(),
563 header_->e_type);
564 return false;
565 }
566 if (EV_CURRENT != header_->e_version) {
567 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
568 EV_CURRENT,
569 file_->GetPath().c_str(),
570 header_->e_version);
571 return false;
572 }
573 if (0 != header_->e_entry) {
574 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
575 0,
576 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700577 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800578 return false;
579 }
580 if (0 == header_->e_phoff) {
581 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
582 file_->GetPath().c_str());
583 return false;
584 }
585 if (0 == header_->e_shoff) {
586 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
587 file_->GetPath().c_str());
588 return false;
589 }
590 if (0 == header_->e_ehsize) {
591 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
592 file_->GetPath().c_str());
593 return false;
594 }
595 if (0 == header_->e_phentsize) {
596 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
597 file_->GetPath().c_str());
598 return false;
599 }
600 if (0 == header_->e_phnum) {
601 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
602 file_->GetPath().c_str());
603 return false;
604 }
605 if (0 == header_->e_shentsize) {
606 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
607 file_->GetPath().c_str());
608 return false;
609 }
610 if (0 == header_->e_shnum) {
611 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
612 file_->GetPath().c_str());
613 return false;
614 }
615 if (0 == header_->e_shstrndx) {
616 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
617 file_->GetPath().c_str());
618 return false;
619 }
620 if (header_->e_shstrndx >= header_->e_shnum) {
621 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
622 header_->e_shstrndx,
623 header_->e_shnum,
624 file_->GetPath().c_str());
625 return false;
626 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800627
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800628 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800629 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700630 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
631 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800632 Size(),
633 file_->GetPath().c_str());
634 return false;
635 }
636 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700637 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
638 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800639 Size(),
640 file_->GetPath().c_str());
641 return false;
642 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800643 }
644 return true;
645}
646
Tong Shen62d1ca32014-09-03 17:24:56 -0700647template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
648 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
649 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
650Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
651 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
652 ::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700653 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800654 return *header_;
655}
656
Tong Shen62d1ca32014-09-03 17:24:56 -0700657template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
658 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
659 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700660uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700661 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
662 ::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700663 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
664 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800665 return program_headers_start_;
666}
667
Tong Shen62d1ca32014-09-03 17:24:56 -0700668template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
669 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
670 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700671uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700672 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
673 ::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700674 CHECK(!program_header_only_); // Only used in "full" mode.
675 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800676 return section_headers_start_;
677}
678
Tong Shen62d1ca32014-09-03 17:24:56 -0700679template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
680 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
681 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
682Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
683 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
684 ::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700685 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800686 return *dynamic_program_header_;
687}
688
Tong Shen62d1ca32014-09-03 17:24:56 -0700689template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
690 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
691 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
692Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
693 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
694 ::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700695 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800696 return dynamic_section_start_;
697}
698
Tong Shen62d1ca32014-09-03 17:24:56 -0700699template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
700 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
701 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
702Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
703 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
704 ::GetSymbolSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800705 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800706 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000707 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700708 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800709 break;
710 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000711 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700712 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800713 break;
714 }
715 default: {
716 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700717 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800718 }
719 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800720}
721
Tong Shen62d1ca32014-09-03 17:24:56 -0700722template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
723 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
724 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
725const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
726 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
727 ::GetStringSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800728 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800729 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000730 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700731 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800732 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000733 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700734 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800735 }
736 default: {
737 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700738 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800739 }
740 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800741}
742
Tong Shen62d1ca32014-09-03 17:24:56 -0700743template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
744 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
745 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
746const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
747 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
748 ::GetString(Elf_Word section_type, Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800749 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
750 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700751 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800752 }
753 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700754 if (string_section_start == nullptr) {
755 return nullptr;
756 }
757 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758}
759
Andreas Gampedaab38c2014-09-12 18:38:24 -0700760// WARNING: The following methods do not check for an error condition (non-existent hash section).
761// It is the caller's job to do this.
762
Tong Shen62d1ca32014-09-03 17:24:56 -0700763template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
764 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
765 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
766Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
767 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
768 ::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800769 return hash_section_start_;
770}
771
Tong Shen62d1ca32014-09-03 17:24:56 -0700772template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
773 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
774 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
775Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
776 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
777 ::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800778 return GetHashSectionStart()[0];
779}
780
Tong Shen62d1ca32014-09-03 17:24:56 -0700781template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
782 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
783 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
784Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
785 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
786 ::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800787 return GetHashSectionStart()[1];
788}
789
Tong Shen62d1ca32014-09-03 17:24:56 -0700790template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
791 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
792 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
793Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
794 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
795 ::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700796 if (i >= GetHashBucketNum()) {
797 *ok = false;
798 return 0;
799 }
800 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800801 // 0 is nbucket, 1 is nchain
802 return GetHashSectionStart()[2 + i];
803}
804
Tong Shen62d1ca32014-09-03 17:24:56 -0700805template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
806 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
807 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
808Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
809 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
810 ::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600811 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700812 *ok = false;
813 return 0;
814 }
815 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800816 // 0 is nbucket, 1 is nchain, & chains are after buckets
817 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
818}
819
Tong Shen62d1ca32014-09-03 17:24:56 -0700820template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
821 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
822 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
823Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
824 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
825 ::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800826 return GetHeader().e_phnum;
827}
828
Tong Shen62d1ca32014-09-03 17:24:56 -0700829template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
830 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
831 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
832Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
833 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
834 ::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700835 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700836 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700837 if (program_header >= End()) {
838 return nullptr; // Failure condition.
839 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700840 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800841}
842
Tong Shen62d1ca32014-09-03 17:24:56 -0700843template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
844 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
845 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
846Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
847 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
848 ::FindProgamHeaderByType(Elf_Word type) const {
849 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
850 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700851 if (program_header->p_type == type) {
852 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800853 }
854 }
Alex Light3470ab42014-06-18 10:35:45 -0700855 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800856}
857
Tong Shen62d1ca32014-09-03 17:24:56 -0700858template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
859 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
860 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
861Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
862 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
863 ::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800864 return GetHeader().e_shnum;
865}
866
Tong Shen62d1ca32014-09-03 17:24:56 -0700867template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
868 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
869 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
870Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
871 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
872 ::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800873 // Can only access arbitrary sections when we have the whole file, not just program header.
874 // Even if we Load(), it doesn't bring in all the sections.
875 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700876 if (i >= GetSectionHeaderNum()) {
877 return nullptr; // Failure condition.
878 }
Ian Rogers13735952014-10-08 12:43:28 -0700879 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700880 if (section_header >= End()) {
881 return nullptr; // Failure condition.
882 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700883 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884}
885
Tong Shen62d1ca32014-09-03 17:24:56 -0700886template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
887 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
888 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
889Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
890 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
891 ::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800892 // Can only access arbitrary sections when we have the whole file, not just program header.
893 // We could change this to switch on known types if they were detected during loading.
894 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700895 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
896 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700897 if (section_header->sh_type == type) {
898 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800899 }
900 }
Alex Light3470ab42014-06-18 10:35:45 -0700901 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800902}
903
904// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800905static unsigned elfhash(const char *_name) {
906 const unsigned char *name = (const unsigned char *) _name;
907 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800908
Brian Carlstromdf629502013-07-17 22:39:56 -0700909 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 h = (h << 4) + *name++;
911 g = h & 0xf0000000;
912 h ^= g;
913 h ^= g >> 24;
914 }
915 return h;
916}
917
Tong Shen62d1ca32014-09-03 17:24:56 -0700918template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
919 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
920 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
921Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
922 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
923 ::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800924 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800925}
926
Tong Shen62d1ca32014-09-03 17:24:56 -0700927template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
928 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
929 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700930const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700931 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
932 ::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700933 // Check that we have a hash section.
934 if (GetHashSectionStart() == nullptr) {
935 return nullptr; // Failure condition.
936 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700937 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700938 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700939 // TODO: we need to change this to calculate base_address_ in ::Open,
940 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700941 return base_address_ + sym->st_value;
942 } else {
943 return nullptr;
944 }
945}
946
Andreas Gampedaab38c2014-09-12 18:38:24 -0700947// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
Tong Shen62d1ca32014-09-03 17:24:56 -0700948template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
949 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
950 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
951const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
952 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
953 ::FindDynamicSymbol(const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700954 if (GetHashBucketNum() == 0) {
955 // No dynamic symbols at all.
956 return nullptr;
957 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700958 Elf_Word hash = elfhash(symbol_name.c_str());
959 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700960 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700961 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700962 if (!ok) {
963 return nullptr;
964 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800965 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700966 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700967 if (symbol == nullptr) {
968 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700970 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
971 if (symbol_name == name) {
972 return symbol;
973 }
974 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
975 if (!ok) {
976 return nullptr;
977 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800978 }
Alex Light3470ab42014-06-18 10:35:45 -0700979 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800980}
981
Tong Shen62d1ca32014-09-03 17:24:56 -0700982template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
983 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
984 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
985bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
986 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
987 ::IsSymbolSectionType(Elf_Word section_type) {
988 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
989}
990
991template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
992 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
993 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
994Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
995 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
996 ::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800997 CHECK(IsSymbolSectionType(section_header.sh_type))
998 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800999 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1000 return section_header.sh_size / section_header.sh_entsize;
1001}
1002
Tong Shen62d1ca32014-09-03 17:24:56 -07001003template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1004 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1005 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1006Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1007 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1008 ::GetSymbol(Elf_Word section_type,
1009 Elf_Word i) const {
1010 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001011 if (sym_start == nullptr) {
1012 return nullptr;
1013 }
1014 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001015}
1016
Tong Shen62d1ca32014-09-03 17:24:56 -07001017template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1018 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1019 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1020typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1021 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1022 ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1023 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1024 ::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001025 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
1026 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001027 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001028 return &symtab_symbol_table_;
1029 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001030 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001031 return &dynsym_symbol_table_;
1032 }
1033 default: {
1034 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -07001035 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001036 }
1037 }
1038}
1039
Tong Shen62d1ca32014-09-03 17:24:56 -07001040template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1041 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1042 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1043Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1044 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1045 ::FindSymbolByName(Elf_Word section_type,
1046 const std::string& symbol_name,
1047 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001048 CHECK(!program_header_only_) << file_->GetPath();
1049 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001050
1051 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -07001052 if (*symbol_table != nullptr || build_map) {
1053 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001054 DCHECK(build_map);
1055 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -07001056 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001057 if (symbol_section == nullptr) {
1058 return nullptr; // Failure condition.
1059 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001060 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001061 if (string_section == nullptr) {
1062 return nullptr; // Failure condition.
1063 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001064 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001065 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001066 if (symbol == nullptr) {
1067 return nullptr; // Failure condition.
1068 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001069 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
1070 ? ELF64_ST_TYPE(symbol->st_info)
1071 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001072 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001073 continue;
1074 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001075 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001076 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001077 continue;
1078 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001079 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -07001080 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -08001081 if (!result.second) {
1082 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001083 if ((symbol->st_value != result.first->second->st_value) ||
1084 (symbol->st_size != result.first->second->st_size) ||
1085 (symbol->st_info != result.first->second->st_info) ||
1086 (symbol->st_other != result.first->second->st_other) ||
1087 (symbol->st_shndx != result.first->second->st_shndx)) {
1088 return nullptr; // Failure condition.
1089 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001090 }
1091 }
1092 }
Alex Light3470ab42014-06-18 10:35:45 -07001093 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001094 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001095 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -07001096 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001097 }
1098 return it->second;
1099 }
1100
1101 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -07001102 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001103 if (symbol_section == nullptr) {
1104 return nullptr;
1105 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001106 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001107 if (string_section == nullptr) {
1108 return nullptr;
1109 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001111 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001112 if (symbol == nullptr) {
1113 return nullptr; // Failure condition.
1114 }
1115 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001116 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001117 continue;
1118 }
1119 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001120 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001121 }
1122 }
Alex Light3470ab42014-06-18 10:35:45 -07001123 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001124}
1125
Tong Shen62d1ca32014-09-03 17:24:56 -07001126template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1127 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1128 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1129Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1130 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1131 ::FindSymbolAddress(Elf_Word section_type,
1132 const std::string& symbol_name,
1133 bool build_map) {
1134 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001135 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001136 return 0;
1137 }
1138 return symbol->st_value;
1139}
1140
Tong Shen62d1ca32014-09-03 17:24:56 -07001141template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1142 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1143 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1144const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1145 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1146 ::GetString(Elf_Shdr& string_section, Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001147 CHECK(!program_header_only_) << file_->GetPath();
1148 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001149 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001150 return nullptr; // Failure condition.
1151 }
1152 if (i >= string_section.sh_size) {
1153 return nullptr;
1154 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001155 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001156 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001157 }
Ian Rogers13735952014-10-08 12:43:28 -07001158 uint8_t* strings = Begin() + string_section.sh_offset;
1159 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001160 if (string >= End()) {
1161 return nullptr;
1162 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001163 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001164}
1165
Tong Shen62d1ca32014-09-03 17:24:56 -07001166template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1167 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1168 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1169Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1170 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1171 ::GetDynamicNum() const {
1172 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001173}
1174
Tong Shen62d1ca32014-09-03 17:24:56 -07001175template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1176 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1177 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1178Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1179 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1180 ::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001181 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1182 return *(GetDynamicSectionStart() + i);
1183}
1184
Tong Shen62d1ca32014-09-03 17:24:56 -07001185template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1186 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1187 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1188Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1189 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1190 ::FindDynamicByType(Elf_Sword type) const {
1191 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1192 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001193 if (dyn->d_tag == type) {
1194 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001195 }
1196 }
Alex Light53cb16b2014-06-12 11:26:29 -07001197 return NULL;
1198}
1199
Tong Shen62d1ca32014-09-03 17:24:56 -07001200template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1201 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1202 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1203Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1204 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1205 ::FindDynamicValueByType(Elf_Sword type) const {
1206 Elf_Dyn* dyn = FindDynamicByType(type);
Alex Light53cb16b2014-06-12 11:26:29 -07001207 if (dyn == NULL) {
1208 return 0;
1209 } else {
1210 return dyn->d_un.d_val;
1211 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001212}
1213
Tong Shen62d1ca32014-09-03 17:24:56 -07001214template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1215 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1216 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1217Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1218 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1219 ::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001220 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001221 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001222}
1223
Tong Shen62d1ca32014-09-03 17:24:56 -07001224template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1225 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1226 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1227Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1228 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1229 ::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001230 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001231 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1232 return section_header.sh_size / section_header.sh_entsize;
1233}
1234
Tong Shen62d1ca32014-09-03 17:24:56 -07001235template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1236 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1237 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1238Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1239 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1240 ::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001241 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001242 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1243 return *(GetRelSectionStart(section_header) + i);
1244}
1245
Tong Shen62d1ca32014-09-03 17:24:56 -07001246template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1247 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1248 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1249Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1250 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1251 ::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001252 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001253 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001254}
1255
Tong Shen62d1ca32014-09-03 17:24:56 -07001256template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1257 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1258 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1259Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1260 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1261 ::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001262 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001263 return section_header.sh_size / section_header.sh_entsize;
1264}
1265
Tong Shen62d1ca32014-09-03 17:24:56 -07001266template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1267 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1268 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1269Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1270 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1271 ::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001272 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001273 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1274 return *(GetRelaSectionStart(section_header) + i);
1275}
1276
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001277// Base on bionic phdr_table_get_load_size
Tong Shen62d1ca32014-09-03 17:24:56 -07001278template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1279 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1280 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1281size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1282 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1283 ::GetLoadedSize() const {
1284 Elf_Addr min_vaddr = 0xFFFFFFFFu;
1285 Elf_Addr max_vaddr = 0x00000000u;
1286 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1287 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001288 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001289 continue;
1290 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001291 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001292 if (begin_vaddr < min_vaddr) {
1293 min_vaddr = begin_vaddr;
1294 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001295 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001296 if (end_vaddr > max_vaddr) {
1297 max_vaddr = end_vaddr;
1298 }
1299 }
1300 min_vaddr = RoundDown(min_vaddr, kPageSize);
1301 max_vaddr = RoundUp(max_vaddr, kPageSize);
1302 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1303 size_t loaded_size = max_vaddr - min_vaddr;
1304 return loaded_size;
1305}
1306
Tong Shen62d1ca32014-09-03 17:24:56 -07001307template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1308 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1309 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1310bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1311 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1312 ::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001313 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001314
1315 if (executable) {
1316 InstructionSet elf_ISA = kNone;
1317 switch (GetHeader().e_machine) {
1318 case EM_ARM: {
1319 elf_ISA = kArm;
1320 break;
1321 }
1322 case EM_AARCH64: {
1323 elf_ISA = kArm64;
1324 break;
1325 }
1326 case EM_386: {
1327 elf_ISA = kX86;
1328 break;
1329 }
1330 case EM_X86_64: {
1331 elf_ISA = kX86_64;
1332 break;
1333 }
1334 case EM_MIPS: {
Andreas Gampec5a3ea72015-01-13 16:41:53 -08001335 if ((GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 ||
1336 (GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) {
1337 elf_ISA = kMips;
1338 }
Andreas Gampe91268c12014-04-03 17:50:24 -07001339 break;
1340 }
1341 }
1342
1343 if (elf_ISA != kRuntimeISA) {
1344 std::ostringstream oss;
1345 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1346 *error_msg = oss.str();
1347 return false;
1348 }
1349 }
1350
Jim_Guoa62a5882014-04-28 11:11:57 +08001351 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001352 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1353 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001354 if (program_header == nullptr) {
1355 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1356 i, file_->GetPath().c_str());
1357 return false;
1358 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001359
1360 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001361 if (program_header->p_type == PT_DYNAMIC) {
1362 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001363 continue;
1364 }
1365
1366 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001367 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001368 continue;
1369 }
1370
1371 // Found something to load.
1372
Jim_Guoa62a5882014-04-28 11:11:57 +08001373 // Before load the actual segments, reserve a contiguous chunk
1374 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001375 // permissions. We'll then carve that up with the proper
1376 // permissions as we load the actual segments. If p_vaddr is
1377 // non-zero, the segments require the specific address specified,
1378 // which either was specified in the file because we already set
1379 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001380 int64_t temp_file_length = file_->GetLength();
1381 if (temp_file_length < 0) {
1382 errno = -temp_file_length;
1383 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1384 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1385 return false;
1386 }
1387 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001388 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001389 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1390 uint8_t* reserve_base_override = reserve_base;
1391 // Override the base (e.g. when compiling with --compile-pic)
1392 if (requested_base_ != nullptr) {
1393 reserve_base_override = requested_base_;
1394 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001395 std::string reservation_name("ElfFile reservation for ");
1396 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001397 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001398 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001399 GetLoadedSize(), PROT_NONE, false,
1400 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001401 if (reserve.get() == nullptr) {
1402 *error_msg = StringPrintf("Failed to allocate %s: %s",
1403 reservation_name.c_str(), error_msg->c_str());
1404 return false;
1405 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001406 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001407
1408 // Base address is the difference of actual mapped location and the p_vaddr
1409 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1410 - reinterpret_cast<uintptr_t>(reserve_base));
1411 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1412 // dynamic memory address of where that object is actually mapped
1413 //
1414 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1415 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001416 segments_.push_back(reserve.release());
1417 }
1418 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001419 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001420 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001421 }
Ian Rogers13735952014-10-08 12:43:28 -07001422 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001423 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001424 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001425 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001426 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001427 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001428 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001429 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001430 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001431 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001432 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001433 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001434 if (writable_) {
1435 prot |= PROT_WRITE;
1436 flags |= MAP_SHARED;
1437 } else {
1438 flags |= MAP_PRIVATE;
1439 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001440 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001441 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001442 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1443 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001444 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001445 return false;
1446 }
Ian Rogers700a4022014-05-19 16:49:03 -07001447 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001448 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001449 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001450 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001451 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001452 file_->GetPath().c_str(),
1453 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001454 if (segment.get() == nullptr) {
1455 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1456 i, file_->GetPath().c_str(), error_msg->c_str());
1457 return false;
1458 }
1459 if (segment->Begin() != p_vaddr) {
1460 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1461 "instead mapped to %p",
1462 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1463 return false;
1464 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001465 segments_.push_back(segment.release());
1466 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001467
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001468 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001469 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001470 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1471 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1472 file_->GetPath().c_str());
1473 return false;
1474 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001475 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001476
Tong Shen62d1ca32014-09-03 17:24:56 -07001477 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1478 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001479 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001480 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001481 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001482 if (!ValidPointer(d_ptr)) {
1483 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1484 d_ptr, file_->GetPath().c_str());
1485 return false;
1486 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001487 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001488 break;
1489 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001490 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001491 if (!ValidPointer(d_ptr)) {
1492 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1493 d_ptr, file_->GetPath().c_str());
1494 return false;
1495 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001496 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1497 break;
1498 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001499 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001500 if (!ValidPointer(d_ptr)) {
1501 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1502 d_ptr, file_->GetPath().c_str());
1503 return false;
1504 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001505 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001506 break;
1507 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001508 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001509 if (GetDynamicNum() != i+1) {
1510 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1511 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1512 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1513 return false;
1514 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001515 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001516 }
1517 }
1518 }
1519
Andreas Gampedaab38c2014-09-12 18:38:24 -07001520 // Check for the existence of some sections.
1521 if (!CheckSectionsExist(error_msg)) {
1522 return false;
1523 }
1524
Mark Mendellae9fd932014-02-10 16:14:35 -08001525 // Use GDB JIT support to do stack backtrace, etc.
1526 if (executable) {
1527 GdbJITSupport();
1528 }
1529
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001530 return true;
1531}
1532
Tong Shen62d1ca32014-09-03 17:24:56 -07001533template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1534 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1535 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1536bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1537 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001538 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001539 for (size_t i = 0; i < segments_.size(); ++i) {
1540 const MemMap* segment = segments_[i];
1541 if (segment->Begin() <= start && start < segment->End()) {
1542 return true;
1543 }
1544 }
1545 return false;
1546}
1547
Alex Light3470ab42014-06-18 10:35:45 -07001548
Tong Shen62d1ca32014-09-03 17:24:56 -07001549template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1550 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1551 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1552Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1553 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1554 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001555 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001556 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001557 if (shstrtab_sec == nullptr) {
1558 return nullptr;
1559 }
Alex Light3470ab42014-06-18 10:35:45 -07001560 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001561 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001562 if (shdr == nullptr) {
1563 return nullptr;
1564 }
1565 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001566 if (sec_name == nullptr) {
1567 continue;
1568 }
1569 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001570 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001571 }
1572 }
1573 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001574}
1575
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001576struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001577 uint32_t raw_length_;
1578 uint32_t GetLength() {
1579 return raw_length_ + sizeof(raw_length_);
1580 }
1581 uint32_t CIE_pointer;
1582 uint32_t initial_location;
1583 uint32_t address_range;
1584 uint8_t instructions[0];
1585};
1586
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001587static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001588 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001589 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001590 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001591}
1592
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001593static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001594 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001595}
1596
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001597struct PACKED(1) FDE64 {
1598 uint32_t raw_length_;
1599 uint64_t extended_length_;
1600 uint64_t GetLength() {
1601 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1602 }
1603 uint64_t CIE_pointer;
1604 uint64_t initial_location;
1605 uint64_t address_range;
1606 uint8_t instructions[0];
1607};
1608
1609static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001610 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001611 fde_bytes += frame->GetLength();
1612 return reinterpret_cast<FDE64*>(fde_bytes);
1613}
1614
1615static bool IsFDE(FDE64* frame) {
1616 return frame->CIE_pointer != 0;
1617}
1618
1619static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001620 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001621 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1622 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1623 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1624 for (; frame < last_frame; frame = NextFDE(frame)) {
1625 if (!IsFDE(frame)) {
1626 return false;
1627 }
1628 frame->initial_location += base_address_delta;
1629 }
1630 return true;
1631 } else {
1632 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1633 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1634 for (; frame < last_frame; frame = NextFDE(frame)) {
1635 if (!IsFDE(frame)) {
1636 return false;
1637 }
1638 frame->initial_location += base_address_delta;
1639 }
1640 return true;
1641 }
1642}
1643
1644static uint8_t* NextLeb128(uint8_t* current) {
1645 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1646 return current;
1647}
1648
1649struct PACKED(1) DebugLineHeader {
1650 uint32_t unit_length_; // TODO 32-bit specific size
1651 uint16_t version_;
1652 uint32_t header_length_; // TODO 32-bit specific size
1653 uint8_t minimum_instruction_lenght_;
1654 uint8_t maximum_operations_per_instruction_;
1655 uint8_t default_is_stmt_;
1656 int8_t line_base_;
1657 uint8_t line_range_;
1658 uint8_t opcode_base_;
1659 uint8_t remaining_[0];
1660
1661 bool IsStandardOpcode(const uint8_t* op) const {
1662 return *op != 0 && *op < opcode_base_;
1663 }
1664
1665 bool IsExtendedOpcode(const uint8_t* op) const {
1666 return *op == 0;
1667 }
1668
1669 const uint8_t* GetStandardOpcodeLengths() const {
1670 return remaining_;
1671 }
1672
1673 uint8_t* GetNextOpcode(uint8_t* op) const {
1674 if (IsExtendedOpcode(op)) {
1675 uint8_t* length_field = op + 1;
1676 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1677 return length_field + length;
1678 } else if (!IsStandardOpcode(op)) {
1679 return op + 1;
1680 } else if (*op == DW_LNS_fixed_advance_pc) {
1681 return op + 1 + sizeof(uint16_t);
1682 } else {
1683 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1684 op += 1;
1685 for (int i = 0; i < num_args; i++) {
1686 op = NextLeb128(op);
1687 }
1688 return op;
1689 }
1690 }
1691
1692 uint8_t* GetDebugLineData() const {
1693 const uint8_t* hdr_start =
1694 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1695 return const_cast<uint8_t*>(hdr_start + header_length_);
1696 }
1697};
1698
Ian Rogersd4c4d952014-10-16 20:31:53 -07001699class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001700 public:
1701 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1702 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1703 new DebugLineInstructionIterator(header, section_size));
1704 if (line_iter.get() == nullptr) {
1705 return nullptr;
1706 } else {
1707 return line_iter.release();
1708 }
1709 }
1710
1711 ~DebugLineInstructionIterator() {}
1712
1713 bool Next() {
1714 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001715 return false;
1716 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001717 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1718 if (current_instruction_ >= last_instruction_) {
1719 current_instruction_ = nullptr;
1720 return false;
1721 } else {
1722 return true;
1723 }
1724 }
1725
Ian Rogersd4c4d952014-10-16 20:31:53 -07001726 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001727 return current_instruction_;
1728 }
1729
Ian Rogersd4c4d952014-10-16 20:31:53 -07001730 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001731 return header_->IsExtendedOpcode(current_instruction_);
1732 }
1733
1734 uint8_t GetOpcode() {
1735 if (!IsExtendedOpcode()) {
1736 return *current_instruction_;
1737 } else {
1738 uint8_t* len_ptr = current_instruction_ + 1;
1739 return *NextLeb128(len_ptr);
1740 }
1741 }
1742
1743 uint8_t* GetArguments() {
1744 if (!IsExtendedOpcode()) {
1745 return current_instruction_ + 1;
1746 } else {
1747 uint8_t* len_ptr = current_instruction_ + 1;
1748 return NextLeb128(len_ptr) + 1;
1749 }
1750 }
1751
1752 private:
1753 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1754 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1755 current_instruction_(header->GetDebugLineData()) {}
1756
Ian Rogersd4c4d952014-10-16 20:31:53 -07001757 DebugLineHeader* const header_;
1758 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001759 uint8_t* current_instruction_;
1760};
1761
1762static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1763 while (iter->Next()) {
1764 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1765 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1766 }
Alex Light3470ab42014-06-18 10:35:45 -07001767 }
1768 return true;
1769}
1770
1771struct PACKED(1) DebugInfoHeader {
1772 uint32_t unit_length; // TODO 32-bit specific size
1773 uint16_t version;
1774 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1775 uint8_t address_size;
1776};
1777
1778// Returns -1 if it is variable length, which we will just disallow for now.
1779static int32_t FormLength(uint32_t att) {
1780 switch (att) {
1781 case DW_FORM_data1:
1782 case DW_FORM_flag:
1783 case DW_FORM_flag_present:
1784 case DW_FORM_ref1:
1785 return 1;
1786
1787 case DW_FORM_data2:
1788 case DW_FORM_ref2:
1789 return 2;
1790
1791 case DW_FORM_addr: // TODO 32-bit only
1792 case DW_FORM_ref_addr: // TODO 32-bit only
1793 case DW_FORM_sec_offset: // TODO 32-bit only
1794 case DW_FORM_strp: // TODO 32-bit only
1795 case DW_FORM_data4:
1796 case DW_FORM_ref4:
1797 return 4;
1798
1799 case DW_FORM_data8:
1800 case DW_FORM_ref8:
1801 case DW_FORM_ref_sig8:
1802 return 8;
1803
1804 case DW_FORM_block:
1805 case DW_FORM_block1:
1806 case DW_FORM_block2:
1807 case DW_FORM_block4:
1808 case DW_FORM_exprloc:
1809 case DW_FORM_indirect:
1810 case DW_FORM_ref_udata:
1811 case DW_FORM_sdata:
1812 case DW_FORM_string:
1813 case DW_FORM_udata:
1814 default:
1815 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001816 }
1817}
1818
Ian Rogersd4c4d952014-10-16 20:31:53 -07001819class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001820 public:
Alex Light3470ab42014-06-18 10:35:45 -07001821 ~DebugTag() {}
1822 // Creates a new tag and moves data pointer up to the start of the next one.
1823 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001824 static DebugTag* Create(const uint8_t** data_pointer) {
1825 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001826 uint32_t index = DecodeUnsignedLeb128(&data);
1827 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1828 tag->size_ = static_cast<uint32_t>(
1829 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1830 // skip the abbrev
1831 tag->tag_ = DecodeUnsignedLeb128(&data);
1832 tag->has_child_ = (*data == 0);
1833 data++;
1834 while (true) {
1835 uint32_t attr = DecodeUnsignedLeb128(&data);
1836 uint32_t form = DecodeUnsignedLeb128(&data);
1837 if (attr == 0 && form == 0) {
1838 break;
1839 } else if (attr == 0 || form == 0) {
1840 // Bad abbrev.
1841 return nullptr;
1842 }
1843 int32_t size = FormLength(form);
1844 if (size == -1) {
1845 return nullptr;
1846 }
1847 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1848 }
1849 *data_pointer = data;
1850 return tag.release();
1851 }
1852
1853 uint32_t GetSize() const {
1854 return size_;
1855 }
1856
Ian Rogersd4c4d952014-10-16 20:31:53 -07001857 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001858 return has_child_;
1859 }
1860
Ian Rogersd4c4d952014-10-16 20:31:53 -07001861 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001862 return tag_;
1863 }
1864
Ian Rogersd4c4d952014-10-16 20:31:53 -07001865 uint32_t GetIndex() const {
1866 return index_;
1867 }
1868
Alex Light3470ab42014-06-18 10:35:45 -07001869 // Gets the offset of a particular attribute in this tag structure.
1870 // Interpretation of the data is left to the consumer. 0 is returned if the
1871 // tag does not contain the attribute.
1872 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1873 auto it = off_map_.find(dwarf_attribute);
1874 if (it == off_map_.end()) {
1875 return 0;
1876 } else {
1877 return it->second;
1878 }
1879 }
1880
1881 // Gets the size of attribute
1882 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1883 auto it = size_map_.find(dwarf_attribute);
1884 if (it == size_map_.end()) {
1885 return 0;
1886 } else {
1887 return it->second;
1888 }
1889 }
1890
1891 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001892 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001893 void AddAttribute(uint32_t type, uint32_t attr_size) {
1894 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1895 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1896 size_ += attr_size;
1897 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001898
1899 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001900 std::map<uint32_t, uint32_t> off_map_;
1901 std::map<uint32_t, uint32_t> size_map_;
1902 uint32_t size_;
1903 uint32_t tag_;
1904 bool has_child_;
1905};
1906
1907class DebugAbbrev {
1908 public:
1909 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001910 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001911 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1912 if (!abbrev->ReadAtOffset(0)) {
1913 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001914 }
1915 return abbrev.release();
1916 }
1917
Alex Lightd338ae02014-08-13 17:15:38 -07001918 bool ReadAtOffset(uint32_t abbrev_offset) {
1919 tags_.clear();
1920 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001921 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001922 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1923 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1924 if (tag.get() == nullptr) {
1925 return false;
1926 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001927 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001928 tag_list_.push_back(std::move(tag));
1929 }
1930 }
1931 return true;
1932 }
1933
Ian Rogers13735952014-10-08 12:43:28 -07001934 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001935 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1936 auto it = tags_.find(tag_num);
1937 if (it == tags_.end()) {
1938 return nullptr;
1939 } else {
1940 CHECK_GT(tag_list_.size(), it->second);
1941 return tag_list_.at(it->second).get();
1942 }
1943 }
1944
1945 private:
Ian Rogers13735952014-10-08 12:43:28 -07001946 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001947 const uint8_t* const begin_;
1948 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001949 std::map<uint32_t, uint32_t> tags_;
1950 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1951};
1952
1953class DebugInfoIterator {
1954 public:
1955 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1956 DebugAbbrev* abbrev) {
1957 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1958 if (iter->GetCurrentTag() == nullptr) {
1959 return nullptr;
1960 } else {
1961 return iter.release();
1962 }
1963 }
1964 ~DebugInfoIterator() {}
1965
1966 // Moves to the next DIE. Returns false if at last entry.
1967 // TODO Handle variable length attributes.
1968 bool next() {
1969 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1970 return false;
1971 }
Alex Lightd338ae02014-08-13 17:15:38 -07001972 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001973 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001974 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1975 current_cu_ = next_cu_;
1976 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001977 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001978 reread_abbrev = true;
1979 }
Alex Light3470ab42014-06-18 10:35:45 -07001980 if (current_entry_ >= last_entry_) {
1981 current_entry_ = nullptr;
1982 return false;
1983 }
Alex Lightd338ae02014-08-13 17:15:38 -07001984 if (reread_abbrev) {
1985 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1986 }
Alex Light3470ab42014-06-18 10:35:45 -07001987 current_tag_ = abbrev_->ReadTag(current_entry_);
1988 if (current_tag_ == nullptr) {
1989 current_entry_ = nullptr;
1990 return false;
1991 } else {
1992 return true;
1993 }
1994 }
1995
1996 const DebugTag* GetCurrentTag() {
1997 return const_cast<DebugTag*>(current_tag_);
1998 }
Ian Rogers13735952014-10-08 12:43:28 -07001999 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07002000 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
2001 return nullptr;
2002 }
2003 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
2004 if (off == 0) {
2005 // tag does not have that field.
2006 return nullptr;
2007 } else {
2008 DCHECK_LT(off, current_tag_->GetSize());
2009 return current_entry_ + off;
2010 }
2011 }
2012
2013 private:
Alex Lightd338ae02014-08-13 17:15:38 -07002014 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07002015 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07002016 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
2017 }
2018
Alex Light3470ab42014-06-18 10:35:45 -07002019 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
2020 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07002021 current_cu_(header),
2022 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07002023 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
2024 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07002025 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002026 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002027 DebugInfoHeader* current_cu_;
2028 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002029 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002030 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002031 DebugTag* current_tag_;
2032};
2033
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002034static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002035 do {
2036 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2037 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002038 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002039 return false;
2040 }
2041 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2042 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2043 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002044 *PC_low += base_address_delta;
2045 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002046 }
2047 } while (iter->next());
2048 return true;
2049}
2050
Tong Shen62d1ca32014-09-03 17:24:56 -07002051template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2052 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2053 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2054bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2055 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2056 ::FixupDebugSections(off_t base_address_delta) {
2057 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2058 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2059 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2060 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2061 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2062 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2063 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002064
2065 if (debug_info == nullptr || debug_abbrev == nullptr ||
2066 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2067 // Release version of ART does not generate debug info.
2068 return true;
2069 }
2070 if (base_address_delta == 0) {
2071 return true;
2072 }
2073 if (eh_frame != nullptr &&
2074 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2075 return false;
2076 }
2077
2078 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2079 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002080 if (abbrev.get() == nullptr) {
2081 return false;
2082 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002083 DebugInfoHeader* info_header =
2084 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2085 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2086 debug_info->sh_size,
2087 abbrev.get()));
2088 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002089 return false;
2090 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002091 if (debug_line != nullptr) {
2092 DebugLineHeader* line_header =
2093 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2094 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2095 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2096 if (line_iter.get() == nullptr) {
2097 return false;
2098 }
2099 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2100 return false;
2101 }
2102 }
2103 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002104}
Mark Mendellae9fd932014-02-10 16:14:35 -08002105
Tong Shen62d1ca32014-09-03 17:24:56 -07002106template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2107 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2108 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2109void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2110 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2111 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002112 // We only get here if we only are mapping the program header.
2113 DCHECK(program_header_only_);
2114
2115 // Well, we need the whole file to do this.
2116 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002117 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2118 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002119 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2120 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2121 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2122 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002123 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002124 return;
2125 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002126 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2127 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002128
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002129 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002130 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002131 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002132 return;
2133 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002134
2135 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002136 // We need to add in a strtab and symtab to the image.
2137 // all is MAP_PRIVATE so it can be written to freely.
2138 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002139 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002140 elf_hdr.e_entry = 0;
2141 elf_hdr.e_phoff = 0;
2142 elf_hdr.e_phnum = 0;
2143 elf_hdr.e_phentsize = 0;
2144 elf_hdr.e_type = ET_EXEC;
2145
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002146 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2147 // and the actual address stuff starts at in regular files this is good.
2148 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002149 LOG(ERROR) << "Failed to load GDB data";
2150 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002151 }
2152
Alex Light3470ab42014-06-18 10:35:45 -07002153 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2154 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002155}
2156
Tong Shen62d1ca32014-09-03 17:24:56 -07002157template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2158 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2159 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2160bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2161 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2162 ::Strip(std::string* error_msg) {
2163 // ELF files produced by MCLinker look roughly like this
2164 //
2165 // +------------+
2166 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2167 // +------------+
2168 // | Elf_Phdr | program headers
2169 // | Elf_Phdr |
2170 // | ... |
2171 // | Elf_Phdr |
2172 // +------------+
2173 // | section | mixture of needed and unneeded sections
2174 // +------------+
2175 // | section |
2176 // +------------+
2177 // | ... |
2178 // +------------+
2179 // | section |
2180 // +------------+
2181 // | Elf_Shdr | section headers
2182 // | Elf_Shdr |
2183 // | ... | contains offset to section start
2184 // | Elf_Shdr |
2185 // +------------+
2186 //
2187 // To strip:
2188 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2189 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2190 // - move the sections are keeping up to fill in gaps of sections we want to strip
2191 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2192 // - truncate rest of file
2193 //
2194
2195 std::vector<Elf_Shdr> section_headers;
2196 std::vector<Elf_Word> section_headers_original_indexes;
2197 section_headers.reserve(GetSectionHeaderNum());
2198
2199
2200 Elf_Shdr* string_section = GetSectionNameStringSection();
2201 CHECK(string_section != nullptr);
2202 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2203 Elf_Shdr* sh = GetSectionHeader(i);
2204 CHECK(sh != nullptr);
2205 const char* name = GetString(*string_section, sh->sh_name);
2206 if (name == nullptr) {
2207 CHECK_EQ(0U, i);
2208 section_headers.push_back(*sh);
2209 section_headers_original_indexes.push_back(0);
2210 continue;
2211 }
2212 if (StartsWith(name, ".debug")
2213 || (strcmp(name, ".strtab") == 0)
2214 || (strcmp(name, ".symtab") == 0)) {
2215 continue;
2216 }
2217 section_headers.push_back(*sh);
2218 section_headers_original_indexes.push_back(i);
2219 }
2220 CHECK_NE(0U, section_headers.size());
2221 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2222
2223 // section 0 is the NULL section, sections start at offset of first section
2224 CHECK(GetSectionHeader(1) != nullptr);
2225 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2226 for (size_t i = 1; i < section_headers.size(); i++) {
2227 Elf_Shdr& new_sh = section_headers[i];
2228 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2229 CHECK(old_sh != nullptr);
2230 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2231 if (old_sh->sh_addralign > 1) {
2232 offset = RoundUp(offset, old_sh->sh_addralign);
2233 }
2234 if (old_sh->sh_offset == offset) {
2235 // already in place
2236 offset += old_sh->sh_size;
2237 continue;
2238 }
2239 // shift section earlier
2240 memmove(Begin() + offset,
2241 Begin() + old_sh->sh_offset,
2242 old_sh->sh_size);
2243 new_sh.sh_offset = offset;
2244 offset += old_sh->sh_size;
2245 }
2246
2247 Elf_Off shoff = offset;
2248 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2249 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2250 offset += section_headers_size_in_bytes;
2251
2252 GetHeader().e_shnum = section_headers.size();
2253 GetHeader().e_shoff = shoff;
2254 int result = ftruncate(file_->Fd(), offset);
2255 if (result != 0) {
2256 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2257 file_->GetPath().c_str(), strerror(errno));
2258 return false;
2259 }
2260 return true;
2261}
2262
2263static const bool DEBUG_FIXUP = false;
2264
2265template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2266 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2267 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2268bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2269 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2270 ::Fixup(uintptr_t base_address) {
2271 if (!FixupDynamic(base_address)) {
2272 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2273 return false;
2274 }
2275 if (!FixupSectionHeaders(base_address)) {
2276 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2277 return false;
2278 }
2279 if (!FixupProgramHeaders(base_address)) {
2280 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2281 return false;
2282 }
2283 if (!FixupSymbols(base_address, true)) {
2284 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2285 return false;
2286 }
2287 if (!FixupSymbols(base_address, false)) {
2288 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2289 return false;
2290 }
2291 if (!FixupRelocations(base_address)) {
2292 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2293 return false;
2294 }
2295 if (!FixupDebugSections(base_address)) {
2296 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2297 return false;
2298 }
2299 return true;
2300}
2301
2302template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2303 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2304 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2305bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2306 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2307 ::FixupDynamic(uintptr_t base_address) {
2308 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2309 Elf_Dyn& elf_dyn = GetDynamic(i);
2310 Elf_Word d_tag = elf_dyn.d_tag;
2311 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2312 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2313 if (DEBUG_FIXUP) {
2314 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2315 GetFile().GetPath().c_str(), i,
2316 static_cast<uint64_t>(d_ptr),
2317 static_cast<uint64_t>(d_ptr + base_address));
2318 }
2319 d_ptr += base_address;
2320 elf_dyn.d_un.d_ptr = d_ptr;
2321 }
2322 }
2323 return true;
2324}
2325
2326template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2327 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2328 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2329bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2330 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2331 ::FixupSectionHeaders(uintptr_t base_address) {
2332 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2333 Elf_Shdr* sh = GetSectionHeader(i);
2334 CHECK(sh != nullptr);
2335 // 0 implies that the section will not exist in the memory of the process
2336 if (sh->sh_addr == 0) {
2337 continue;
2338 }
2339 if (DEBUG_FIXUP) {
2340 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2341 GetFile().GetPath().c_str(), i,
2342 static_cast<uint64_t>(sh->sh_addr),
2343 static_cast<uint64_t>(sh->sh_addr + base_address));
2344 }
2345 sh->sh_addr += base_address;
2346 }
2347 return true;
2348}
2349
2350template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2351 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2352 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2353bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2354 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2355 ::FixupProgramHeaders(uintptr_t base_address) {
2356 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2357 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2358 Elf_Phdr* ph = GetProgramHeader(i);
2359 CHECK(ph != nullptr);
2360 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2361 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2362 << GetFile().GetPath() << " i=" << i;
2363 if (DEBUG_FIXUP) {
2364 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2365 GetFile().GetPath().c_str(), i,
2366 static_cast<uint64_t>(ph->p_vaddr),
2367 static_cast<uint64_t>(ph->p_vaddr + base_address));
2368 }
2369 ph->p_vaddr += base_address;
2370 ph->p_paddr += base_address;
2371 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2372 << GetFile().GetPath() << " i=" << i;
2373 }
2374 return true;
2375}
2376
2377template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2378 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2379 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2380bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2381 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2382 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2383 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2384 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2385 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2386 if (symbol_section == nullptr) {
2387 // file is missing optional .symtab
2388 CHECK(!dynamic) << GetFile().GetPath();
2389 return true;
2390 }
2391 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2392 Elf_Sym* symbol = GetSymbol(section_type, i);
2393 CHECK(symbol != nullptr);
2394 if (symbol->st_value != 0) {
2395 if (DEBUG_FIXUP) {
2396 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2397 GetFile().GetPath().c_str(), i,
2398 static_cast<uint64_t>(symbol->st_value),
2399 static_cast<uint64_t>(symbol->st_value + base_address));
2400 }
2401 symbol->st_value += base_address;
2402 }
2403 }
2404 return true;
2405}
2406
2407template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2408 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2409 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2410bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2411 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2412 ::FixupRelocations(uintptr_t base_address) {
2413 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2414 Elf_Shdr* sh = GetSectionHeader(i);
2415 CHECK(sh != nullptr);
2416 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002417 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
2418 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002419 if (DEBUG_FIXUP) {
2420 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002421 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002422 static_cast<uint64_t>(rel.r_offset),
2423 static_cast<uint64_t>(rel.r_offset + base_address));
2424 }
2425 rel.r_offset += base_address;
2426 }
2427 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002428 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
2429 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002430 if (DEBUG_FIXUP) {
2431 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002432 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002433 static_cast<uint64_t>(rela.r_offset),
2434 static_cast<uint64_t>(rela.r_offset + base_address));
2435 }
2436 rela.r_offset += base_address;
2437 }
2438 }
2439 }
2440 return true;
2441}
2442
2443// Explicit instantiations
2444template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2445 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2446template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2447 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2448
Ian Rogersd4c4d952014-10-16 20:31:53 -07002449ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002450}
2451
Ian Rogersd4c4d952014-10-16 20:31:53 -07002452ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002453}
2454
2455ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002456 // Should never have 32 and 64-bit impls.
2457 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002458}
2459
Igor Murashkin46774762014-10-22 11:37:02 -07002460ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2461 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002462 if (file->GetLength() < EI_NIDENT) {
2463 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2464 file->GetPath().c_str());
2465 return nullptr;
2466 }
2467 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2468 file->GetPath().c_str(), error_msg));
2469 if (map == nullptr && map->Size() != EI_NIDENT) {
2470 return nullptr;
2471 }
Ian Rogers13735952014-10-08 12:43:28 -07002472 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002473 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002474 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2475 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002476 if (elf_file_impl == nullptr)
2477 return nullptr;
2478 return new ElfFile(elf_file_impl);
2479 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002480 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2481 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002482 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002483 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002484 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002485 return new ElfFile(elf_file_impl);
2486 } else {
2487 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2488 ELFCLASS32, ELFCLASS64,
2489 file->GetPath().c_str(),
2490 header[EI_CLASS]);
2491 return nullptr;
2492 }
2493}
2494
2495ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2496 if (file->GetLength() < EI_NIDENT) {
2497 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2498 file->GetPath().c_str());
2499 return nullptr;
2500 }
2501 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2502 file->GetPath().c_str(), error_msg));
2503 if (map == nullptr && map->Size() != EI_NIDENT) {
2504 return nullptr;
2505 }
Ian Rogers13735952014-10-08 12:43:28 -07002506 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002507 if (header[EI_CLASS] == ELFCLASS64) {
2508 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002509 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002510 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002511 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002512 return new ElfFile(elf_file_impl);
2513 } else if (header[EI_CLASS] == ELFCLASS32) {
2514 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002515 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002516 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002517 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002518 return new ElfFile(elf_file_impl);
2519 } else {
2520 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2521 ELFCLASS32, ELFCLASS64,
2522 file->GetPath().c_str(),
2523 header[EI_CLASS]);
2524 return nullptr;
2525 }
2526}
2527
2528#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002529 if (elf64_.get() != nullptr) { \
2530 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002531 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002532 DCHECK(elf32_.get() != nullptr); \
2533 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002534 }
2535
2536bool ElfFile::Load(bool executable, std::string* error_msg) {
2537 DELEGATE_TO_IMPL(Load, executable, error_msg);
2538}
2539
Ian Rogers13735952014-10-08 12:43:28 -07002540const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002541 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2542}
2543
2544size_t ElfFile::Size() const {
2545 DELEGATE_TO_IMPL(Size);
2546}
2547
Ian Rogers13735952014-10-08 12:43:28 -07002548uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002549 DELEGATE_TO_IMPL(Begin);
2550}
2551
Ian Rogers13735952014-10-08 12:43:28 -07002552uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002553 DELEGATE_TO_IMPL(End);
2554}
2555
2556const File& ElfFile::GetFile() const {
2557 DELEGATE_TO_IMPL(GetFile);
2558}
2559
2560bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002561 if (elf32_.get() == nullptr) {
2562 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002563
Ian Rogersd4c4d952014-10-16 20:31:53 -07002564 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2565 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002566 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002567 }
2568 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002569 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002570 }
2571 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002572 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002573 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002574 return true;
2575 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002576 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2577 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002578 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002579 }
2580 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002581 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002582 }
2583 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002584 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002585 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002586 return true;
2587 }
2588}
2589
2590uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2591 const std::string& symbol_name,
2592 bool build_map) {
2593 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2594}
2595
2596size_t ElfFile::GetLoadedSize() const {
2597 DELEGATE_TO_IMPL(GetLoadedSize);
2598}
2599
2600bool ElfFile::Strip(File* file, std::string* error_msg) {
2601 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2602 if (elf_file.get() == nullptr) {
2603 return false;
2604 }
2605
Ian Rogersd4c4d952014-10-16 20:31:53 -07002606 if (elf_file->elf64_.get() != nullptr)
2607 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002608 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002609 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002610}
2611
2612bool ElfFile::Fixup(uintptr_t base_address) {
2613 DELEGATE_TO_IMPL(Fixup, base_address);
2614}
2615
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002616} // namespace art