blob: fb532711573faa84f89167eac199e4853b5ef31e [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
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070024#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070026#include "base/unix_file/fd_file.h"
Alex Light3470ab42014-06-18 10:35:45 -070027#include "dwarf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070028#include "elf_file_impl.h"
29#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070030#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "utils.h"
Andreas Gampe91268c12014-04-03 17:50:24 -070032#include "instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033
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.
63 void __attribute__((noinline)) __jit_debug_register_code() {
64 __asm__("");
65 }
66
67 // GDB will inspect contents of this descriptor.
68 // Static initialization is necessary to prevent GDB from seeing
69 // uninitialized descriptor.
70 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
71}
72
73
Ian Rogers13735952014-10-08 12:43:28 -070074static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr,
Mark Mendellae9fd932014-02-10 16:14:35 -080075 uintptr_t symfile_size) {
76 JITCodeEntry* entry = new JITCodeEntry;
77 entry->symfile_addr_ = symfile_addr;
78 entry->symfile_size_ = symfile_size;
79 entry->prev_ = nullptr;
80
81 // TODO: Do we need a lock here?
82 entry->next_ = __jit_debug_descriptor.first_entry_;
83 if (entry->next_ != nullptr) {
84 entry->next_->prev_ = entry;
85 }
86 __jit_debug_descriptor.first_entry_ = entry;
87 __jit_debug_descriptor.relevant_entry_ = entry;
88
89 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
90 __jit_debug_register_code();
91 return entry;
92}
93
94
95static void UnregisterCodeEntry(JITCodeEntry* entry) {
96 // TODO: Do we need a lock here?
97 if (entry->prev_ != nullptr) {
98 entry->prev_->next_ = entry->next_;
99 } else {
100 __jit_debug_descriptor.first_entry_ = entry->next_;
101 }
102
103 if (entry->next_ != nullptr) {
104 entry->next_->prev_ = entry->prev_;
105 }
106
107 __jit_debug_descriptor.relevant_entry_ = entry;
108 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
109 __jit_debug_register_code();
110 delete entry;
111}
112
Tong Shen62d1ca32014-09-03 17:24:56 -0700113template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
114 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
115 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
116ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
117 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700118 ::ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base)
Brian Carlstromc1409452014-02-26 14:06:23 -0800119 : file_(file),
120 writable_(writable),
121 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700122 header_(nullptr),
123 base_address_(nullptr),
124 program_headers_start_(nullptr),
125 section_headers_start_(nullptr),
126 dynamic_program_header_(nullptr),
127 dynamic_section_start_(nullptr),
128 symtab_section_start_(nullptr),
129 dynsym_section_start_(nullptr),
130 strtab_section_start_(nullptr),
131 dynstr_section_start_(nullptr),
132 hash_section_start_(nullptr),
133 symtab_symbol_table_(nullptr),
134 dynsym_symbol_table_(nullptr),
135 jit_elf_image_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -0700136 jit_gdb_entry_(nullptr),
137 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -0700138 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800139}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800140
Tong Shen62d1ca32014-09-03 17:24:56 -0700141template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
142 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
143 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
144ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
145 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
146 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
147 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
148 ::Open(File* file, bool writable, bool program_header_only,
Igor Murashkin46774762014-10-22 11:37:02 -0700149 std::string* error_msg, uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700150 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
151 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
152 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
153 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700154 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 int prot;
156 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700157 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800158 prot = PROT_READ | PROT_WRITE;
159 flags = MAP_SHARED;
160 } else {
161 prot = PROT_READ;
162 flags = MAP_PRIVATE;
163 }
Alex Light3470ab42014-06-18 10:35:45 -0700164 if (!elf_file->Setup(prot, flags, error_msg)) {
165 return nullptr;
166 }
167 return elf_file.release();
168}
169
Tong Shen62d1ca32014-09-03 17:24:56 -0700170template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
171 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
172 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
173ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
174 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
175 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
176 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
177 ::Open(File* file, int prot, int flags, std::string* error_msg) {
178 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
179 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
180 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
181 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700182 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
183 /*requested_base*/nullptr));
Alex Light3470ab42014-06-18 10:35:45 -0700184 if (!elf_file->Setup(prot, flags, error_msg)) {
185 return nullptr;
186 }
187 return elf_file.release();
188}
189
Tong Shen62d1ca32014-09-03 17:24:56 -0700190template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
191 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
192 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
193bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
194 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
195 ::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800196 int64_t temp_file_length = file_->GetLength();
197 if (temp_file_length < 0) {
198 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700199 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
200 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800201 return false;
202 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800203 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700204 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800205 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700206 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208 return false;
209 }
210
Brian Carlstromc1409452014-02-26 14:06:23 -0800211 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800212 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700213 size_t elf_header_size = sizeof(Elf_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700214 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800215 file_->GetPath().c_str(), error_msg),
216 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 return false;
218 }
219 // then remap to cover program header
220 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700221 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800222 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223 "header of %zd bytes: '%s'", file_length,
Tong Shen62d1ca32014-09-03 17:24:56 -0700224 sizeof(Elf_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700225 return false;
226 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700227 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800228 file_->GetPath().c_str(), error_msg),
229 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800231 return false;
232 }
233 } else {
234 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800236 file_->GetPath().c_str(), error_msg),
237 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700238 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800239 return false;
240 }
241 }
242
Andreas Gampedaab38c2014-09-12 18:38:24 -0700243 if (program_header_only_) {
244 program_headers_start_ = Begin() + GetHeader().e_phoff;
245 } else {
246 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
247 return false;
248 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800249
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800250 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700251 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
252 return false;
253 }
254
255 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700256 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700257 if (shstrtab_section_header == nullptr) {
258 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
259 file_->GetPath().c_str());
260 return false;
261 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800262
263 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000264 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700265 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700266 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
267 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800268 return false;
269 }
270
Andreas Gampedaab38c2014-09-12 18:38:24 -0700271 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700272 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700273 return false;
274 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800275
276 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700277 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
278 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700279 if (section_header == nullptr) {
280 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
281 i, file_->GetPath().c_str());
282 return false;
283 }
284 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000285 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700286 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700287 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700288 return false;
289 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800290 break;
291 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000292 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700293 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700294 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700295 return false;
296 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800297 break;
298 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000299 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800300 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700301 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
302 // Check that this is named ".dynstr" and ignore otherwise.
303 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
304 if (strncmp(".dynstr", header_name, 8) == 0) {
305 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700306 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700307 return false;
308 }
309 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800310 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700311 // Check that this is named ".strtab" and ignore otherwise.
312 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
313 if (strncmp(".strtab", header_name, 8) == 0) {
314 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700315 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700316 return false;
317 }
318 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800319 }
320 break;
321 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000322 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700323 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700324 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800325 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800326 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800327 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700328 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800329 return false;
330 }
331 break;
332 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000333 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700334 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700335 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700336 return false;
337 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800338 break;
339 }
340 }
341 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700342
343 // Check for the existence of some sections.
344 if (!CheckSectionsExist(error_msg)) {
345 return false;
346 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800347 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700348
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800349 return true;
350}
351
Tong Shen62d1ca32014-09-03 17:24:56 -0700352template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
353 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
354 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
355ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
356 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
357 ::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800358 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800359 delete symtab_symbol_table_;
360 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800361 delete jit_elf_image_;
362 if (jit_gdb_entry_) {
363 UnregisterCodeEntry(jit_gdb_entry_);
364 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800365}
366
Tong Shen62d1ca32014-09-03 17:24:56 -0700367template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
368 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
369 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
370bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
371 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
372 ::CheckAndSet(Elf32_Off offset, const char* label,
Ian Rogers13735952014-10-08 12:43:28 -0700373 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700374 if (Begin() + offset >= End()) {
375 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
376 file_->GetPath().c_str());
377 return false;
378 }
379 *target = Begin() + offset;
380 return true;
381}
382
Tong Shen62d1ca32014-09-03 17:24:56 -0700383template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
384 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
385 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
386bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
387 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700388 ::CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700389 // Only works in whole-program mode, as we need to iterate over the sections.
390 // Note that we normally can't search by type, as duplicates are allowed for most section types.
391 if (program_header_only_) {
392 return true;
393 }
394
Tong Shen62d1ca32014-09-03 17:24:56 -0700395 Elf_Shdr* source_section = nullptr;
396 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700397 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700398 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
399 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700400
401 if (Begin() + section_header->sh_offset == source) {
402 // Found the source.
403 source_section = section_header;
404 if (target_index) {
405 break;
406 }
407 } else if (Begin() + section_header->sh_offset == target) {
408 target_index = i;
409 target_found = true;
410 if (source_section != nullptr) {
411 break;
412 }
413 }
414 }
415
416 return target_found && source_section != nullptr && source_section->sh_link == target_index;
417}
418
Tong Shen62d1ca32014-09-03 17:24:56 -0700419template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
420 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
421 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
422bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
423 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
424 ::CheckSectionsExist(std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700425 if (!program_header_only_) {
426 // If in full mode, need section headers.
427 if (section_headers_start_ == nullptr) {
428 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
429 return false;
430 }
431 }
432
433 // This is redundant, but defensive.
434 if (dynamic_program_header_ == nullptr) {
435 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
436 file_->GetPath().c_str());
437 return false;
438 }
439
440 // Need a dynamic section. This is redundant, but defensive.
441 if (dynamic_section_start_ == nullptr) {
442 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
443 file_->GetPath().c_str());
444 return false;
445 }
446
447 // Symtab validation. These is not really a hard failure, as we are currently not using the
448 // symtab internally, but it's nice to be defensive.
449 if (symtab_section_start_ != nullptr) {
450 // When there's a symtab, there should be a strtab.
451 if (strtab_section_start_ == nullptr) {
452 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
453 return false;
454 }
455
456 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700457 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
458 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700459 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
460 file_->GetPath().c_str());
461 return false;
462 }
463 }
464
465 // We always need a dynstr & dynsym.
466 if (dynstr_section_start_ == nullptr) {
467 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
468 return false;
469 }
470 if (dynsym_section_start_ == nullptr) {
471 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
472 return false;
473 }
474
475 // Need a hash section for dynamic symbol lookup.
476 if (hash_section_start_ == nullptr) {
477 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
478 file_->GetPath().c_str());
479 return false;
480 }
481
482 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700483 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
484 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700485 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
486 file_->GetPath().c_str());
487 return false;
488 }
489
490 return true;
491}
492
Tong Shen62d1ca32014-09-03 17:24:56 -0700493template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
494 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
495 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
496bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
497 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
498 ::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700499 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800500 // MemMap::Open should have already set an error.
501 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800502 return false;
503 }
504 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700505 CHECK(map_.get() != nullptr) << file_->GetPath();
506 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800507
Tong Shen62d1ca32014-09-03 17:24:56 -0700508 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000509 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
510 || (ELFMAG1 != header_->e_ident[EI_MAG1])
511 || (ELFMAG2 != header_->e_ident[EI_MAG2])
512 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800513 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
514 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800515 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000516 header_->e_ident[EI_MAG0],
517 header_->e_ident[EI_MAG1],
518 header_->e_ident[EI_MAG2],
519 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800520 return false;
521 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700522 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
523 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800524 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700525 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800526 file_->GetPath().c_str(),
527 header_->e_ident[EI_CLASS]);
528 return false;
529 }
530 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
531 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
532 ELFDATA2LSB,
533 file_->GetPath().c_str(),
534 header_->e_ident[EI_CLASS]);
535 return false;
536 }
537 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
538 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
539 EV_CURRENT,
540 file_->GetPath().c_str(),
541 header_->e_ident[EI_CLASS]);
542 return false;
543 }
544 if (ET_DYN != header_->e_type) {
545 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
546 ET_DYN,
547 file_->GetPath().c_str(),
548 header_->e_type);
549 return false;
550 }
551 if (EV_CURRENT != header_->e_version) {
552 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
553 EV_CURRENT,
554 file_->GetPath().c_str(),
555 header_->e_version);
556 return false;
557 }
558 if (0 != header_->e_entry) {
559 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
560 0,
561 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700562 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800563 return false;
564 }
565 if (0 == header_->e_phoff) {
566 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
567 file_->GetPath().c_str());
568 return false;
569 }
570 if (0 == header_->e_shoff) {
571 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
572 file_->GetPath().c_str());
573 return false;
574 }
575 if (0 == header_->e_ehsize) {
576 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
577 file_->GetPath().c_str());
578 return false;
579 }
580 if (0 == header_->e_phentsize) {
581 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
582 file_->GetPath().c_str());
583 return false;
584 }
585 if (0 == header_->e_phnum) {
586 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
587 file_->GetPath().c_str());
588 return false;
589 }
590 if (0 == header_->e_shentsize) {
591 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
592 file_->GetPath().c_str());
593 return false;
594 }
595 if (0 == header_->e_shnum) {
596 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
597 file_->GetPath().c_str());
598 return false;
599 }
600 if (0 == header_->e_shstrndx) {
601 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
602 file_->GetPath().c_str());
603 return false;
604 }
605 if (header_->e_shstrndx >= header_->e_shnum) {
606 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
607 header_->e_shstrndx,
608 header_->e_shnum,
609 file_->GetPath().c_str());
610 return false;
611 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800612
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800613 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800614 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700615 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
616 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800617 Size(),
618 file_->GetPath().c_str());
619 return false;
620 }
621 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700622 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
623 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800624 Size(),
625 file_->GetPath().c_str());
626 return false;
627 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800628 }
629 return true;
630}
631
Tong Shen62d1ca32014-09-03 17:24:56 -0700632template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
633 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
634 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
635Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
636 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
637 ::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700638 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800639 return *header_;
640}
641
Tong Shen62d1ca32014-09-03 17:24:56 -0700642template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
643 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
644 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700645uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700646 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
647 ::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700648 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
649 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800650 return program_headers_start_;
651}
652
Tong Shen62d1ca32014-09-03 17:24:56 -0700653template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
654 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
655 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700656uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700657 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
658 ::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700659 CHECK(!program_header_only_); // Only used in "full" mode.
660 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800661 return section_headers_start_;
662}
663
Tong Shen62d1ca32014-09-03 17:24:56 -0700664template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
665 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
666 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
667Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
668 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
669 ::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700670 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800671 return *dynamic_program_header_;
672}
673
Tong Shen62d1ca32014-09-03 17:24:56 -0700674template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
675 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
676 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
677Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
678 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
679 ::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700680 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800681 return dynamic_section_start_;
682}
683
Tong Shen62d1ca32014-09-03 17:24:56 -0700684template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
685 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
686 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
687Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
688 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
689 ::GetSymbolSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800690 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800691 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000692 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700693 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800694 break;
695 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000696 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700697 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800698 break;
699 }
700 default: {
701 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700702 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800703 }
704 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800705}
706
Tong Shen62d1ca32014-09-03 17:24:56 -0700707template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
708 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
709 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
710const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
711 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
712 ::GetStringSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800713 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800714 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000715 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700716 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800717 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000718 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700719 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800720 }
721 default: {
722 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700723 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800724 }
725 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800726}
727
Tong Shen62d1ca32014-09-03 17:24:56 -0700728template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
729 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
730 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
731const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
732 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
733 ::GetString(Elf_Word section_type, Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800734 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
735 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700736 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 }
738 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700739 if (string_section_start == nullptr) {
740 return nullptr;
741 }
742 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800743}
744
Andreas Gampedaab38c2014-09-12 18:38:24 -0700745// WARNING: The following methods do not check for an error condition (non-existent hash section).
746// It is the caller's job to do this.
747
Tong Shen62d1ca32014-09-03 17:24:56 -0700748template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
749 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
750 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
751Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
752 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
753 ::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800754 return hash_section_start_;
755}
756
Tong Shen62d1ca32014-09-03 17:24:56 -0700757template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
758 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
759 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
760Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
761 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
762 ::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800763 return GetHashSectionStart()[0];
764}
765
Tong Shen62d1ca32014-09-03 17:24:56 -0700766template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
767 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
768 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
769Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
770 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
771 ::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800772 return GetHashSectionStart()[1];
773}
774
Tong Shen62d1ca32014-09-03 17:24:56 -0700775template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
776 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
777 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
778Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
779 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
780 ::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700781 if (i >= GetHashBucketNum()) {
782 *ok = false;
783 return 0;
784 }
785 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800786 // 0 is nbucket, 1 is nchain
787 return GetHashSectionStart()[2 + i];
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 ::GetHashChain(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, & chains are after buckets
802 return GetHashSectionStart()[2 + GetHashBucketNum() + 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 ::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800811 return GetHeader().e_phnum;
812}
813
Tong Shen62d1ca32014-09-03 17:24:56 -0700814template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
815 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
816 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
817Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
818 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
819 ::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700820 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700821 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700822 if (program_header >= End()) {
823 return nullptr; // Failure condition.
824 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700825 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800826}
827
Tong Shen62d1ca32014-09-03 17:24:56 -0700828template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
829 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
830 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
831Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
832 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
833 ::FindProgamHeaderByType(Elf_Word type) const {
834 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
835 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700836 if (program_header->p_type == type) {
837 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800838 }
839 }
Alex Light3470ab42014-06-18 10:35:45 -0700840 return nullptr;
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_Word 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 ::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800849 return GetHeader().e_shnum;
850}
851
Tong Shen62d1ca32014-09-03 17:24:56 -0700852template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
853 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
854 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
855Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
856 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
857 ::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800858 // Can only access arbitrary sections when we have the whole file, not just program header.
859 // Even if we Load(), it doesn't bring in all the sections.
860 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700861 if (i >= GetSectionHeaderNum()) {
862 return nullptr; // Failure condition.
863 }
Ian Rogers13735952014-10-08 12:43:28 -0700864 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700865 if (section_header >= End()) {
866 return nullptr; // Failure condition.
867 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700868 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800869}
870
Tong Shen62d1ca32014-09-03 17:24:56 -0700871template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
872 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
873 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
874Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
875 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
876 ::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800877 // Can only access arbitrary sections when we have the whole file, not just program header.
878 // We could change this to switch on known types if they were detected during loading.
879 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700880 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
881 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700882 if (section_header->sh_type == type) {
883 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884 }
885 }
Alex Light3470ab42014-06-18 10:35:45 -0700886 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800887}
888
889// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800890static unsigned elfhash(const char *_name) {
891 const unsigned char *name = (const unsigned char *) _name;
892 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800893
Brian Carlstromdf629502013-07-17 22:39:56 -0700894 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800895 h = (h << 4) + *name++;
896 g = h & 0xf0000000;
897 h ^= g;
898 h ^= g >> 24;
899 }
900 return h;
901}
902
Tong Shen62d1ca32014-09-03 17:24:56 -0700903template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
904 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
905 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
906Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
907 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
908 ::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800909 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800910}
911
Tong Shen62d1ca32014-09-03 17:24:56 -0700912template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
913 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
914 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700915const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700916 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
917 ::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700918 // Check that we have a hash section.
919 if (GetHashSectionStart() == nullptr) {
920 return nullptr; // Failure condition.
921 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700922 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700923 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700924 // TODO: we need to change this to calculate base_address_ in ::Open,
925 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700926 return base_address_ + sym->st_value;
927 } else {
928 return nullptr;
929 }
930}
931
Andreas Gampedaab38c2014-09-12 18:38:24 -0700932// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
Tong Shen62d1ca32014-09-03 17:24:56 -0700933template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
934 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
935 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
936const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
937 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
938 ::FindDynamicSymbol(const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700939 if (GetHashBucketNum() == 0) {
940 // No dynamic symbols at all.
941 return nullptr;
942 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700943 Elf_Word hash = elfhash(symbol_name.c_str());
944 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700945 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700946 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700947 if (!ok) {
948 return nullptr;
949 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800950 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700951 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700952 if (symbol == nullptr) {
953 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800954 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700955 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
956 if (symbol_name == name) {
957 return symbol;
958 }
959 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
960 if (!ok) {
961 return nullptr;
962 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800963 }
Alex Light3470ab42014-06-18 10:35:45 -0700964 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800965}
966
Tong Shen62d1ca32014-09-03 17:24:56 -0700967template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
968 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
969 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
970bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
971 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
972 ::IsSymbolSectionType(Elf_Word section_type) {
973 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
974}
975
976template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
977 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
978 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
979Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
980 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
981 ::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800982 CHECK(IsSymbolSectionType(section_header.sh_type))
983 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800984 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
985 return section_header.sh_size / section_header.sh_entsize;
986}
987
Tong Shen62d1ca32014-09-03 17:24:56 -0700988template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
989 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
990 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
991Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
992 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
993 ::GetSymbol(Elf_Word section_type,
994 Elf_Word i) const {
995 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700996 if (sym_start == nullptr) {
997 return nullptr;
998 }
999 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001000}
1001
Tong Shen62d1ca32014-09-03 17:24:56 -07001002template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1003 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1004 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1005typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1006 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1007 ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1008 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1009 ::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001010 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
1011 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001012 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001013 return &symtab_symbol_table_;
1014 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001015 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001016 return &dynsym_symbol_table_;
1017 }
1018 default: {
1019 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -07001020 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001021 }
1022 }
1023}
1024
Tong Shen62d1ca32014-09-03 17:24:56 -07001025template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1026 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1027 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1028Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1029 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1030 ::FindSymbolByName(Elf_Word section_type,
1031 const std::string& symbol_name,
1032 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001033 CHECK(!program_header_only_) << file_->GetPath();
1034 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001035
1036 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -07001037 if (*symbol_table != nullptr || build_map) {
1038 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001039 DCHECK(build_map);
1040 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -07001041 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001042 if (symbol_section == nullptr) {
1043 return nullptr; // Failure condition.
1044 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001045 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001046 if (string_section == nullptr) {
1047 return nullptr; // Failure condition.
1048 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001049 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001050 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001051 if (symbol == nullptr) {
1052 return nullptr; // Failure condition.
1053 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001054 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
1055 ? ELF64_ST_TYPE(symbol->st_info)
1056 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001057 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001058 continue;
1059 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001060 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001061 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001062 continue;
1063 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001064 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -07001065 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -08001066 if (!result.second) {
1067 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001068 if ((symbol->st_value != result.first->second->st_value) ||
1069 (symbol->st_size != result.first->second->st_size) ||
1070 (symbol->st_info != result.first->second->st_info) ||
1071 (symbol->st_other != result.first->second->st_other) ||
1072 (symbol->st_shndx != result.first->second->st_shndx)) {
1073 return nullptr; // Failure condition.
1074 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001075 }
1076 }
1077 }
Alex Light3470ab42014-06-18 10:35:45 -07001078 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001079 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001080 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -07001081 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001082 }
1083 return it->second;
1084 }
1085
1086 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -07001087 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001088 if (symbol_section == nullptr) {
1089 return nullptr;
1090 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001091 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001092 if (string_section == nullptr) {
1093 return nullptr;
1094 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001095 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001096 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001097 if (symbol == nullptr) {
1098 return nullptr; // Failure condition.
1099 }
1100 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001101 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001102 continue;
1103 }
1104 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001105 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001106 }
1107 }
Alex Light3470ab42014-06-18 10:35:45 -07001108 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001109}
1110
Tong Shen62d1ca32014-09-03 17:24:56 -07001111template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1112 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1113 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1114Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1115 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1116 ::FindSymbolAddress(Elf_Word section_type,
1117 const std::string& symbol_name,
1118 bool build_map) {
1119 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001120 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001121 return 0;
1122 }
1123 return symbol->st_value;
1124}
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>
1129const char* 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 ::GetString(Elf_Shdr& string_section, Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001132 CHECK(!program_header_only_) << file_->GetPath();
1133 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001134 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001135 return nullptr; // Failure condition.
1136 }
1137 if (i >= string_section.sh_size) {
1138 return nullptr;
1139 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001140 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001141 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001142 }
Ian Rogers13735952014-10-08 12:43:28 -07001143 uint8_t* strings = Begin() + string_section.sh_offset;
1144 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001145 if (string >= End()) {
1146 return nullptr;
1147 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001148 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001149}
1150
Tong Shen62d1ca32014-09-03 17:24:56 -07001151template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1152 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1153 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1154Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1155 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1156 ::GetDynamicNum() const {
1157 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001158}
1159
Tong Shen62d1ca32014-09-03 17:24:56 -07001160template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1161 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1162 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1163Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1164 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1165 ::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001166 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1167 return *(GetDynamicSectionStart() + i);
1168}
1169
Tong Shen62d1ca32014-09-03 17:24:56 -07001170template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1171 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1172 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1173Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1174 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1175 ::FindDynamicByType(Elf_Sword type) const {
1176 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1177 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001178 if (dyn->d_tag == type) {
1179 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001180 }
1181 }
Alex Light53cb16b2014-06-12 11:26:29 -07001182 return NULL;
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_Word 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 ::FindDynamicValueByType(Elf_Sword type) const {
1191 Elf_Dyn* dyn = FindDynamicByType(type);
Alex Light53cb16b2014-06-12 11:26:29 -07001192 if (dyn == NULL) {
1193 return 0;
1194 } else {
1195 return dyn->d_un.d_val;
1196 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001197}
1198
Tong Shen62d1ca32014-09-03 17:24:56 -07001199template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1200 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1201 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1202Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1203 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1204 ::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001205 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001206 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001207}
1208
Tong Shen62d1ca32014-09-03 17:24:56 -07001209template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1210 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1211 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1212Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1213 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1214 ::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001215 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001216 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1217 return section_header.sh_size / section_header.sh_entsize;
1218}
1219
Tong Shen62d1ca32014-09-03 17:24:56 -07001220template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1221 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1222 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1223Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1224 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1225 ::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001226 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001227 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1228 return *(GetRelSectionStart(section_header) + i);
1229}
1230
Tong Shen62d1ca32014-09-03 17:24:56 -07001231template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1232 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1233 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1234Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1235 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1236 ::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001237 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001238 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001239}
1240
Tong Shen62d1ca32014-09-03 17:24:56 -07001241template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1242 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1243 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1244Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1245 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1246 ::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001247 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001248 return section_header.sh_size / section_header.sh_entsize;
1249}
1250
Tong Shen62d1ca32014-09-03 17:24:56 -07001251template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1252 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1253 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1254Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1255 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1256 ::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001257 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001258 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1259 return *(GetRelaSectionStart(section_header) + i);
1260}
1261
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001262// Base on bionic phdr_table_get_load_size
Tong Shen62d1ca32014-09-03 17:24:56 -07001263template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1264 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1265 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1266size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1267 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1268 ::GetLoadedSize() const {
1269 Elf_Addr min_vaddr = 0xFFFFFFFFu;
1270 Elf_Addr max_vaddr = 0x00000000u;
1271 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1272 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001273 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001274 continue;
1275 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001276 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001277 if (begin_vaddr < min_vaddr) {
1278 min_vaddr = begin_vaddr;
1279 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001280 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001281 if (end_vaddr > max_vaddr) {
1282 max_vaddr = end_vaddr;
1283 }
1284 }
1285 min_vaddr = RoundDown(min_vaddr, kPageSize);
1286 max_vaddr = RoundUp(max_vaddr, kPageSize);
1287 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1288 size_t loaded_size = max_vaddr - min_vaddr;
1289 return loaded_size;
1290}
1291
Tong Shen62d1ca32014-09-03 17:24:56 -07001292template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1293 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1294 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1295bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1296 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1297 ::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001298 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001299
1300 if (executable) {
1301 InstructionSet elf_ISA = kNone;
1302 switch (GetHeader().e_machine) {
1303 case EM_ARM: {
1304 elf_ISA = kArm;
1305 break;
1306 }
1307 case EM_AARCH64: {
1308 elf_ISA = kArm64;
1309 break;
1310 }
1311 case EM_386: {
1312 elf_ISA = kX86;
1313 break;
1314 }
1315 case EM_X86_64: {
1316 elf_ISA = kX86_64;
1317 break;
1318 }
1319 case EM_MIPS: {
1320 elf_ISA = kMips;
1321 break;
1322 }
1323 }
1324
1325 if (elf_ISA != kRuntimeISA) {
1326 std::ostringstream oss;
1327 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1328 *error_msg = oss.str();
1329 return false;
1330 }
1331 }
1332
Jim_Guoa62a5882014-04-28 11:11:57 +08001333 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001334 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1335 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001336 if (program_header == nullptr) {
1337 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1338 i, file_->GetPath().c_str());
1339 return false;
1340 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001341
1342 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001343 if (program_header->p_type == PT_DYNAMIC) {
1344 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001345 continue;
1346 }
1347
1348 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001349 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001350 continue;
1351 }
1352
1353 // Found something to load.
1354
Jim_Guoa62a5882014-04-28 11:11:57 +08001355 // Before load the actual segments, reserve a contiguous chunk
1356 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001357 // permissions. We'll then carve that up with the proper
1358 // permissions as we load the actual segments. If p_vaddr is
1359 // non-zero, the segments require the specific address specified,
1360 // which either was specified in the file because we already set
1361 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001362 int64_t temp_file_length = file_->GetLength();
1363 if (temp_file_length < 0) {
1364 errno = -temp_file_length;
1365 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1366 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1367 return false;
1368 }
1369 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001370 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001371 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1372 uint8_t* reserve_base_override = reserve_base;
1373 // Override the base (e.g. when compiling with --compile-pic)
1374 if (requested_base_ != nullptr) {
1375 reserve_base_override = requested_base_;
1376 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001377 std::string reservation_name("ElfFile reservation for ");
1378 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001379 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001380 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001381 GetLoadedSize(), PROT_NONE, false,
1382 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001383 if (reserve.get() == nullptr) {
1384 *error_msg = StringPrintf("Failed to allocate %s: %s",
1385 reservation_name.c_str(), error_msg->c_str());
1386 return false;
1387 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001388 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001389
1390 // Base address is the difference of actual mapped location and the p_vaddr
1391 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1392 - reinterpret_cast<uintptr_t>(reserve_base));
1393 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1394 // dynamic memory address of where that object is actually mapped
1395 //
1396 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1397 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001398 segments_.push_back(reserve.release());
1399 }
1400 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001401 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001402 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001403 }
Ian Rogers13735952014-10-08 12:43:28 -07001404 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001405 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001406 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001407 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001408 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001409 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001410 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001411 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001412 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001413 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001414 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001415 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001416 if (writable_) {
1417 prot |= PROT_WRITE;
1418 flags |= MAP_SHARED;
1419 } else {
1420 flags |= MAP_PRIVATE;
1421 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001422 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001423 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001424 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1425 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001426 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001427 return false;
1428 }
Ian Rogers700a4022014-05-19 16:49:03 -07001429 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001430 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001431 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001432 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001433 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001434 file_->GetPath().c_str(),
1435 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001436 if (segment.get() == nullptr) {
1437 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1438 i, file_->GetPath().c_str(), error_msg->c_str());
1439 return false;
1440 }
1441 if (segment->Begin() != p_vaddr) {
1442 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1443 "instead mapped to %p",
1444 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1445 return false;
1446 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001447 segments_.push_back(segment.release());
1448 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001449
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001450 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001451 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001452 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1453 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1454 file_->GetPath().c_str());
1455 return false;
1456 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001457 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001458
Tong Shen62d1ca32014-09-03 17:24:56 -07001459 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1460 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001461 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001462 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001463 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001464 if (!ValidPointer(d_ptr)) {
1465 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1466 d_ptr, file_->GetPath().c_str());
1467 return false;
1468 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001469 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001470 break;
1471 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001472 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001473 if (!ValidPointer(d_ptr)) {
1474 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1475 d_ptr, file_->GetPath().c_str());
1476 return false;
1477 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001478 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1479 break;
1480 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001481 case DT_SYMTAB: {
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 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001488 break;
1489 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001490 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001491 if (GetDynamicNum() != i+1) {
1492 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1493 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1494 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1495 return false;
1496 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001497 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001498 }
1499 }
1500 }
1501
Andreas Gampedaab38c2014-09-12 18:38:24 -07001502 // Check for the existence of some sections.
1503 if (!CheckSectionsExist(error_msg)) {
1504 return false;
1505 }
1506
Mark Mendellae9fd932014-02-10 16:14:35 -08001507 // Use GDB JIT support to do stack backtrace, etc.
1508 if (executable) {
1509 GdbJITSupport();
1510 }
1511
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001512 return true;
1513}
1514
Tong Shen62d1ca32014-09-03 17:24:56 -07001515template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1516 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1517 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1518bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1519 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001520 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001521 for (size_t i = 0; i < segments_.size(); ++i) {
1522 const MemMap* segment = segments_[i];
1523 if (segment->Begin() <= start && start < segment->End()) {
1524 return true;
1525 }
1526 }
1527 return false;
1528}
1529
Alex Light3470ab42014-06-18 10:35:45 -07001530
Tong Shen62d1ca32014-09-03 17:24:56 -07001531template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1532 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1533 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1534Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1535 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1536 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001537 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001538 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001539 if (shstrtab_sec == nullptr) {
1540 return nullptr;
1541 }
Alex Light3470ab42014-06-18 10:35:45 -07001542 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001543 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001544 if (shdr == nullptr) {
1545 return nullptr;
1546 }
1547 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001548 if (sec_name == nullptr) {
1549 continue;
1550 }
1551 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001552 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001553 }
1554 }
1555 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001556}
1557
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001558struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001559 uint32_t raw_length_;
1560 uint32_t GetLength() {
1561 return raw_length_ + sizeof(raw_length_);
1562 }
1563 uint32_t CIE_pointer;
1564 uint32_t initial_location;
1565 uint32_t address_range;
1566 uint8_t instructions[0];
1567};
1568
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001569static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001570 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001571 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001572 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001573}
1574
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001575static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001576 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001577}
1578
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001579struct PACKED(1) FDE64 {
1580 uint32_t raw_length_;
1581 uint64_t extended_length_;
1582 uint64_t GetLength() {
1583 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1584 }
1585 uint64_t CIE_pointer;
1586 uint64_t initial_location;
1587 uint64_t address_range;
1588 uint8_t instructions[0];
1589};
1590
1591static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001592 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001593 fde_bytes += frame->GetLength();
1594 return reinterpret_cast<FDE64*>(fde_bytes);
1595}
1596
1597static bool IsFDE(FDE64* frame) {
1598 return frame->CIE_pointer != 0;
1599}
1600
1601static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001602 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001603 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1604 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1605 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1606 for (; frame < last_frame; frame = NextFDE(frame)) {
1607 if (!IsFDE(frame)) {
1608 return false;
1609 }
1610 frame->initial_location += base_address_delta;
1611 }
1612 return true;
1613 } else {
1614 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1615 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1616 for (; frame < last_frame; frame = NextFDE(frame)) {
1617 if (!IsFDE(frame)) {
1618 return false;
1619 }
1620 frame->initial_location += base_address_delta;
1621 }
1622 return true;
1623 }
1624}
1625
1626static uint8_t* NextLeb128(uint8_t* current) {
1627 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1628 return current;
1629}
1630
1631struct PACKED(1) DebugLineHeader {
1632 uint32_t unit_length_; // TODO 32-bit specific size
1633 uint16_t version_;
1634 uint32_t header_length_; // TODO 32-bit specific size
1635 uint8_t minimum_instruction_lenght_;
1636 uint8_t maximum_operations_per_instruction_;
1637 uint8_t default_is_stmt_;
1638 int8_t line_base_;
1639 uint8_t line_range_;
1640 uint8_t opcode_base_;
1641 uint8_t remaining_[0];
1642
1643 bool IsStandardOpcode(const uint8_t* op) const {
1644 return *op != 0 && *op < opcode_base_;
1645 }
1646
1647 bool IsExtendedOpcode(const uint8_t* op) const {
1648 return *op == 0;
1649 }
1650
1651 const uint8_t* GetStandardOpcodeLengths() const {
1652 return remaining_;
1653 }
1654
1655 uint8_t* GetNextOpcode(uint8_t* op) const {
1656 if (IsExtendedOpcode(op)) {
1657 uint8_t* length_field = op + 1;
1658 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1659 return length_field + length;
1660 } else if (!IsStandardOpcode(op)) {
1661 return op + 1;
1662 } else if (*op == DW_LNS_fixed_advance_pc) {
1663 return op + 1 + sizeof(uint16_t);
1664 } else {
1665 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1666 op += 1;
1667 for (int i = 0; i < num_args; i++) {
1668 op = NextLeb128(op);
1669 }
1670 return op;
1671 }
1672 }
1673
1674 uint8_t* GetDebugLineData() const {
1675 const uint8_t* hdr_start =
1676 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1677 return const_cast<uint8_t*>(hdr_start + header_length_);
1678 }
1679};
1680
Ian Rogersd4c4d952014-10-16 20:31:53 -07001681class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001682 public:
1683 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1684 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1685 new DebugLineInstructionIterator(header, section_size));
1686 if (line_iter.get() == nullptr) {
1687 return nullptr;
1688 } else {
1689 return line_iter.release();
1690 }
1691 }
1692
1693 ~DebugLineInstructionIterator() {}
1694
1695 bool Next() {
1696 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001697 return false;
1698 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001699 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1700 if (current_instruction_ >= last_instruction_) {
1701 current_instruction_ = nullptr;
1702 return false;
1703 } else {
1704 return true;
1705 }
1706 }
1707
Ian Rogersd4c4d952014-10-16 20:31:53 -07001708 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001709 return current_instruction_;
1710 }
1711
Ian Rogersd4c4d952014-10-16 20:31:53 -07001712 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001713 return header_->IsExtendedOpcode(current_instruction_);
1714 }
1715
1716 uint8_t GetOpcode() {
1717 if (!IsExtendedOpcode()) {
1718 return *current_instruction_;
1719 } else {
1720 uint8_t* len_ptr = current_instruction_ + 1;
1721 return *NextLeb128(len_ptr);
1722 }
1723 }
1724
1725 uint8_t* GetArguments() {
1726 if (!IsExtendedOpcode()) {
1727 return current_instruction_ + 1;
1728 } else {
1729 uint8_t* len_ptr = current_instruction_ + 1;
1730 return NextLeb128(len_ptr) + 1;
1731 }
1732 }
1733
1734 private:
1735 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1736 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1737 current_instruction_(header->GetDebugLineData()) {}
1738
Ian Rogersd4c4d952014-10-16 20:31:53 -07001739 DebugLineHeader* const header_;
1740 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001741 uint8_t* current_instruction_;
1742};
1743
1744static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1745 while (iter->Next()) {
1746 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1747 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1748 }
Alex Light3470ab42014-06-18 10:35:45 -07001749 }
1750 return true;
1751}
1752
1753struct PACKED(1) DebugInfoHeader {
1754 uint32_t unit_length; // TODO 32-bit specific size
1755 uint16_t version;
1756 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1757 uint8_t address_size;
1758};
1759
1760// Returns -1 if it is variable length, which we will just disallow for now.
1761static int32_t FormLength(uint32_t att) {
1762 switch (att) {
1763 case DW_FORM_data1:
1764 case DW_FORM_flag:
1765 case DW_FORM_flag_present:
1766 case DW_FORM_ref1:
1767 return 1;
1768
1769 case DW_FORM_data2:
1770 case DW_FORM_ref2:
1771 return 2;
1772
1773 case DW_FORM_addr: // TODO 32-bit only
1774 case DW_FORM_ref_addr: // TODO 32-bit only
1775 case DW_FORM_sec_offset: // TODO 32-bit only
1776 case DW_FORM_strp: // TODO 32-bit only
1777 case DW_FORM_data4:
1778 case DW_FORM_ref4:
1779 return 4;
1780
1781 case DW_FORM_data8:
1782 case DW_FORM_ref8:
1783 case DW_FORM_ref_sig8:
1784 return 8;
1785
1786 case DW_FORM_block:
1787 case DW_FORM_block1:
1788 case DW_FORM_block2:
1789 case DW_FORM_block4:
1790 case DW_FORM_exprloc:
1791 case DW_FORM_indirect:
1792 case DW_FORM_ref_udata:
1793 case DW_FORM_sdata:
1794 case DW_FORM_string:
1795 case DW_FORM_udata:
1796 default:
1797 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001798 }
1799}
1800
Ian Rogersd4c4d952014-10-16 20:31:53 -07001801class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001802 public:
Alex Light3470ab42014-06-18 10:35:45 -07001803 ~DebugTag() {}
1804 // Creates a new tag and moves data pointer up to the start of the next one.
1805 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001806 static DebugTag* Create(const uint8_t** data_pointer) {
1807 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001808 uint32_t index = DecodeUnsignedLeb128(&data);
1809 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1810 tag->size_ = static_cast<uint32_t>(
1811 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1812 // skip the abbrev
1813 tag->tag_ = DecodeUnsignedLeb128(&data);
1814 tag->has_child_ = (*data == 0);
1815 data++;
1816 while (true) {
1817 uint32_t attr = DecodeUnsignedLeb128(&data);
1818 uint32_t form = DecodeUnsignedLeb128(&data);
1819 if (attr == 0 && form == 0) {
1820 break;
1821 } else if (attr == 0 || form == 0) {
1822 // Bad abbrev.
1823 return nullptr;
1824 }
1825 int32_t size = FormLength(form);
1826 if (size == -1) {
1827 return nullptr;
1828 }
1829 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1830 }
1831 *data_pointer = data;
1832 return tag.release();
1833 }
1834
1835 uint32_t GetSize() const {
1836 return size_;
1837 }
1838
Ian Rogersd4c4d952014-10-16 20:31:53 -07001839 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001840 return has_child_;
1841 }
1842
Ian Rogersd4c4d952014-10-16 20:31:53 -07001843 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001844 return tag_;
1845 }
1846
Ian Rogersd4c4d952014-10-16 20:31:53 -07001847 uint32_t GetIndex() const {
1848 return index_;
1849 }
1850
Alex Light3470ab42014-06-18 10:35:45 -07001851 // Gets the offset of a particular attribute in this tag structure.
1852 // Interpretation of the data is left to the consumer. 0 is returned if the
1853 // tag does not contain the attribute.
1854 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1855 auto it = off_map_.find(dwarf_attribute);
1856 if (it == off_map_.end()) {
1857 return 0;
1858 } else {
1859 return it->second;
1860 }
1861 }
1862
1863 // Gets the size of attribute
1864 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1865 auto it = size_map_.find(dwarf_attribute);
1866 if (it == size_map_.end()) {
1867 return 0;
1868 } else {
1869 return it->second;
1870 }
1871 }
1872
1873 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001874 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001875 void AddAttribute(uint32_t type, uint32_t attr_size) {
1876 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1877 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1878 size_ += attr_size;
1879 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001880
1881 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001882 std::map<uint32_t, uint32_t> off_map_;
1883 std::map<uint32_t, uint32_t> size_map_;
1884 uint32_t size_;
1885 uint32_t tag_;
1886 bool has_child_;
1887};
1888
1889class DebugAbbrev {
1890 public:
1891 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001892 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001893 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1894 if (!abbrev->ReadAtOffset(0)) {
1895 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001896 }
1897 return abbrev.release();
1898 }
1899
Alex Lightd338ae02014-08-13 17:15:38 -07001900 bool ReadAtOffset(uint32_t abbrev_offset) {
1901 tags_.clear();
1902 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001903 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001904 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1905 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1906 if (tag.get() == nullptr) {
1907 return false;
1908 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001909 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001910 tag_list_.push_back(std::move(tag));
1911 }
1912 }
1913 return true;
1914 }
1915
Ian Rogers13735952014-10-08 12:43:28 -07001916 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001917 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1918 auto it = tags_.find(tag_num);
1919 if (it == tags_.end()) {
1920 return nullptr;
1921 } else {
1922 CHECK_GT(tag_list_.size(), it->second);
1923 return tag_list_.at(it->second).get();
1924 }
1925 }
1926
1927 private:
Ian Rogers13735952014-10-08 12:43:28 -07001928 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001929 const uint8_t* const begin_;
1930 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001931 std::map<uint32_t, uint32_t> tags_;
1932 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1933};
1934
1935class DebugInfoIterator {
1936 public:
1937 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1938 DebugAbbrev* abbrev) {
1939 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1940 if (iter->GetCurrentTag() == nullptr) {
1941 return nullptr;
1942 } else {
1943 return iter.release();
1944 }
1945 }
1946 ~DebugInfoIterator() {}
1947
1948 // Moves to the next DIE. Returns false if at last entry.
1949 // TODO Handle variable length attributes.
1950 bool next() {
1951 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1952 return false;
1953 }
Alex Lightd338ae02014-08-13 17:15:38 -07001954 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001955 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001956 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1957 current_cu_ = next_cu_;
1958 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001959 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001960 reread_abbrev = true;
1961 }
Alex Light3470ab42014-06-18 10:35:45 -07001962 if (current_entry_ >= last_entry_) {
1963 current_entry_ = nullptr;
1964 return false;
1965 }
Alex Lightd338ae02014-08-13 17:15:38 -07001966 if (reread_abbrev) {
1967 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1968 }
Alex Light3470ab42014-06-18 10:35:45 -07001969 current_tag_ = abbrev_->ReadTag(current_entry_);
1970 if (current_tag_ == nullptr) {
1971 current_entry_ = nullptr;
1972 return false;
1973 } else {
1974 return true;
1975 }
1976 }
1977
1978 const DebugTag* GetCurrentTag() {
1979 return const_cast<DebugTag*>(current_tag_);
1980 }
Ian Rogers13735952014-10-08 12:43:28 -07001981 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07001982 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1983 return nullptr;
1984 }
1985 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1986 if (off == 0) {
1987 // tag does not have that field.
1988 return nullptr;
1989 } else {
1990 DCHECK_LT(off, current_tag_->GetSize());
1991 return current_entry_ + off;
1992 }
1993 }
1994
1995 private:
Alex Lightd338ae02014-08-13 17:15:38 -07001996 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07001997 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07001998 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
1999 }
2000
Alex Light3470ab42014-06-18 10:35:45 -07002001 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
2002 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07002003 current_cu_(header),
2004 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07002005 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
2006 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07002007 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002008 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002009 DebugInfoHeader* current_cu_;
2010 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002011 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002012 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002013 DebugTag* current_tag_;
2014};
2015
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002016static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002017 do {
2018 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2019 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002020 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002021 return false;
2022 }
2023 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2024 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2025 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002026 *PC_low += base_address_delta;
2027 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002028 }
2029 } while (iter->next());
2030 return true;
2031}
2032
Tong Shen62d1ca32014-09-03 17:24:56 -07002033template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2034 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2035 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2036bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2037 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2038 ::FixupDebugSections(off_t base_address_delta) {
2039 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2040 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2041 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2042 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2043 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2044 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2045 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002046
2047 if (debug_info == nullptr || debug_abbrev == nullptr ||
2048 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2049 // Release version of ART does not generate debug info.
2050 return true;
2051 }
2052 if (base_address_delta == 0) {
2053 return true;
2054 }
2055 if (eh_frame != nullptr &&
2056 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2057 return false;
2058 }
2059
2060 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2061 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002062 if (abbrev.get() == nullptr) {
2063 return false;
2064 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002065 DebugInfoHeader* info_header =
2066 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2067 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2068 debug_info->sh_size,
2069 abbrev.get()));
2070 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002071 return false;
2072 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002073 if (debug_line != nullptr) {
2074 DebugLineHeader* line_header =
2075 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2076 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2077 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2078 if (line_iter.get() == nullptr) {
2079 return false;
2080 }
2081 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2082 return false;
2083 }
2084 }
2085 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002086}
Mark Mendellae9fd932014-02-10 16:14:35 -08002087
Tong Shen62d1ca32014-09-03 17:24:56 -07002088template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2089 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2090 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2091void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2092 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2093 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002094 // We only get here if we only are mapping the program header.
2095 DCHECK(program_header_only_);
2096
2097 // Well, we need the whole file to do this.
2098 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002099 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2100 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002101 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2102 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2103 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2104 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002105 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002106 return;
2107 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002108 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2109 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002110
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002111 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002112 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002113 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002114 return;
2115 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002116
2117 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002118 // We need to add in a strtab and symtab to the image.
2119 // all is MAP_PRIVATE so it can be written to freely.
2120 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002121 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002122 elf_hdr.e_entry = 0;
2123 elf_hdr.e_phoff = 0;
2124 elf_hdr.e_phnum = 0;
2125 elf_hdr.e_phentsize = 0;
2126 elf_hdr.e_type = ET_EXEC;
2127
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002128 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2129 // and the actual address stuff starts at in regular files this is good.
2130 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002131 LOG(ERROR) << "Failed to load GDB data";
2132 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002133 }
2134
Alex Light3470ab42014-06-18 10:35:45 -07002135 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2136 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002137}
2138
Tong Shen62d1ca32014-09-03 17:24:56 -07002139template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2140 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2141 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2142bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2143 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2144 ::Strip(std::string* error_msg) {
2145 // ELF files produced by MCLinker look roughly like this
2146 //
2147 // +------------+
2148 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2149 // +------------+
2150 // | Elf_Phdr | program headers
2151 // | Elf_Phdr |
2152 // | ... |
2153 // | Elf_Phdr |
2154 // +------------+
2155 // | section | mixture of needed and unneeded sections
2156 // +------------+
2157 // | section |
2158 // +------------+
2159 // | ... |
2160 // +------------+
2161 // | section |
2162 // +------------+
2163 // | Elf_Shdr | section headers
2164 // | Elf_Shdr |
2165 // | ... | contains offset to section start
2166 // | Elf_Shdr |
2167 // +------------+
2168 //
2169 // To strip:
2170 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2171 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2172 // - move the sections are keeping up to fill in gaps of sections we want to strip
2173 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2174 // - truncate rest of file
2175 //
2176
2177 std::vector<Elf_Shdr> section_headers;
2178 std::vector<Elf_Word> section_headers_original_indexes;
2179 section_headers.reserve(GetSectionHeaderNum());
2180
2181
2182 Elf_Shdr* string_section = GetSectionNameStringSection();
2183 CHECK(string_section != nullptr);
2184 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2185 Elf_Shdr* sh = GetSectionHeader(i);
2186 CHECK(sh != nullptr);
2187 const char* name = GetString(*string_section, sh->sh_name);
2188 if (name == nullptr) {
2189 CHECK_EQ(0U, i);
2190 section_headers.push_back(*sh);
2191 section_headers_original_indexes.push_back(0);
2192 continue;
2193 }
2194 if (StartsWith(name, ".debug")
2195 || (strcmp(name, ".strtab") == 0)
2196 || (strcmp(name, ".symtab") == 0)) {
2197 continue;
2198 }
2199 section_headers.push_back(*sh);
2200 section_headers_original_indexes.push_back(i);
2201 }
2202 CHECK_NE(0U, section_headers.size());
2203 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2204
2205 // section 0 is the NULL section, sections start at offset of first section
2206 CHECK(GetSectionHeader(1) != nullptr);
2207 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2208 for (size_t i = 1; i < section_headers.size(); i++) {
2209 Elf_Shdr& new_sh = section_headers[i];
2210 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2211 CHECK(old_sh != nullptr);
2212 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2213 if (old_sh->sh_addralign > 1) {
2214 offset = RoundUp(offset, old_sh->sh_addralign);
2215 }
2216 if (old_sh->sh_offset == offset) {
2217 // already in place
2218 offset += old_sh->sh_size;
2219 continue;
2220 }
2221 // shift section earlier
2222 memmove(Begin() + offset,
2223 Begin() + old_sh->sh_offset,
2224 old_sh->sh_size);
2225 new_sh.sh_offset = offset;
2226 offset += old_sh->sh_size;
2227 }
2228
2229 Elf_Off shoff = offset;
2230 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2231 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2232 offset += section_headers_size_in_bytes;
2233
2234 GetHeader().e_shnum = section_headers.size();
2235 GetHeader().e_shoff = shoff;
2236 int result = ftruncate(file_->Fd(), offset);
2237 if (result != 0) {
2238 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2239 file_->GetPath().c_str(), strerror(errno));
2240 return false;
2241 }
2242 return true;
2243}
2244
2245static const bool DEBUG_FIXUP = false;
2246
2247template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2248 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2249 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2250bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2251 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2252 ::Fixup(uintptr_t base_address) {
2253 if (!FixupDynamic(base_address)) {
2254 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2255 return false;
2256 }
2257 if (!FixupSectionHeaders(base_address)) {
2258 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2259 return false;
2260 }
2261 if (!FixupProgramHeaders(base_address)) {
2262 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2263 return false;
2264 }
2265 if (!FixupSymbols(base_address, true)) {
2266 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2267 return false;
2268 }
2269 if (!FixupSymbols(base_address, false)) {
2270 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2271 return false;
2272 }
2273 if (!FixupRelocations(base_address)) {
2274 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2275 return false;
2276 }
2277 if (!FixupDebugSections(base_address)) {
2278 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2279 return false;
2280 }
2281 return true;
2282}
2283
2284template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2285 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2286 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2287bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2288 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2289 ::FixupDynamic(uintptr_t base_address) {
2290 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2291 Elf_Dyn& elf_dyn = GetDynamic(i);
2292 Elf_Word d_tag = elf_dyn.d_tag;
2293 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2294 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2295 if (DEBUG_FIXUP) {
2296 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2297 GetFile().GetPath().c_str(), i,
2298 static_cast<uint64_t>(d_ptr),
2299 static_cast<uint64_t>(d_ptr + base_address));
2300 }
2301 d_ptr += base_address;
2302 elf_dyn.d_un.d_ptr = d_ptr;
2303 }
2304 }
2305 return true;
2306}
2307
2308template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2309 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2310 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2311bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2312 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2313 ::FixupSectionHeaders(uintptr_t base_address) {
2314 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2315 Elf_Shdr* sh = GetSectionHeader(i);
2316 CHECK(sh != nullptr);
2317 // 0 implies that the section will not exist in the memory of the process
2318 if (sh->sh_addr == 0) {
2319 continue;
2320 }
2321 if (DEBUG_FIXUP) {
2322 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2323 GetFile().GetPath().c_str(), i,
2324 static_cast<uint64_t>(sh->sh_addr),
2325 static_cast<uint64_t>(sh->sh_addr + base_address));
2326 }
2327 sh->sh_addr += base_address;
2328 }
2329 return true;
2330}
2331
2332template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2333 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2334 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2335bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2336 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2337 ::FixupProgramHeaders(uintptr_t base_address) {
2338 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2339 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2340 Elf_Phdr* ph = GetProgramHeader(i);
2341 CHECK(ph != nullptr);
2342 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2343 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2344 << GetFile().GetPath() << " i=" << i;
2345 if (DEBUG_FIXUP) {
2346 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2347 GetFile().GetPath().c_str(), i,
2348 static_cast<uint64_t>(ph->p_vaddr),
2349 static_cast<uint64_t>(ph->p_vaddr + base_address));
2350 }
2351 ph->p_vaddr += base_address;
2352 ph->p_paddr += base_address;
2353 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2354 << GetFile().GetPath() << " i=" << i;
2355 }
2356 return true;
2357}
2358
2359template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2360 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2361 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2362bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2363 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2364 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2365 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2366 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2367 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2368 if (symbol_section == nullptr) {
2369 // file is missing optional .symtab
2370 CHECK(!dynamic) << GetFile().GetPath();
2371 return true;
2372 }
2373 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2374 Elf_Sym* symbol = GetSymbol(section_type, i);
2375 CHECK(symbol != nullptr);
2376 if (symbol->st_value != 0) {
2377 if (DEBUG_FIXUP) {
2378 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2379 GetFile().GetPath().c_str(), i,
2380 static_cast<uint64_t>(symbol->st_value),
2381 static_cast<uint64_t>(symbol->st_value + base_address));
2382 }
2383 symbol->st_value += base_address;
2384 }
2385 }
2386 return true;
2387}
2388
2389template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2390 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2391 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2392bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2393 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2394 ::FixupRelocations(uintptr_t base_address) {
2395 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2396 Elf_Shdr* sh = GetSectionHeader(i);
2397 CHECK(sh != nullptr);
2398 if (sh->sh_type == SHT_REL) {
2399 for (uint32_t i = 0; i < GetRelNum(*sh); i++) {
2400 Elf_Rel& rel = GetRel(*sh, i);
2401 if (DEBUG_FIXUP) {
2402 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2403 GetFile().GetPath().c_str(), i,
2404 static_cast<uint64_t>(rel.r_offset),
2405 static_cast<uint64_t>(rel.r_offset + base_address));
2406 }
2407 rel.r_offset += base_address;
2408 }
2409 } else if (sh->sh_type == SHT_RELA) {
2410 for (uint32_t i = 0; i < GetRelaNum(*sh); i++) {
2411 Elf_Rela& rela = GetRela(*sh, i);
2412 if (DEBUG_FIXUP) {
2413 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2414 GetFile().GetPath().c_str(), i,
2415 static_cast<uint64_t>(rela.r_offset),
2416 static_cast<uint64_t>(rela.r_offset + base_address));
2417 }
2418 rela.r_offset += base_address;
2419 }
2420 }
2421 }
2422 return true;
2423}
2424
2425// Explicit instantiations
2426template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2427 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2428template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2429 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2430
Ian Rogersd4c4d952014-10-16 20:31:53 -07002431ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002432}
2433
Ian Rogersd4c4d952014-10-16 20:31:53 -07002434ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002435}
2436
2437ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002438 // Should never have 32 and 64-bit impls.
2439 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002440}
2441
Igor Murashkin46774762014-10-22 11:37:02 -07002442ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2443 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002444 if (file->GetLength() < EI_NIDENT) {
2445 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2446 file->GetPath().c_str());
2447 return nullptr;
2448 }
2449 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2450 file->GetPath().c_str(), error_msg));
2451 if (map == nullptr && map->Size() != EI_NIDENT) {
2452 return nullptr;
2453 }
Ian Rogers13735952014-10-08 12:43:28 -07002454 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002455 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002456 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2457 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002458 if (elf_file_impl == nullptr)
2459 return nullptr;
2460 return new ElfFile(elf_file_impl);
2461 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002462 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2463 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002464 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002465 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002466 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002467 return new ElfFile(elf_file_impl);
2468 } else {
2469 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2470 ELFCLASS32, ELFCLASS64,
2471 file->GetPath().c_str(),
2472 header[EI_CLASS]);
2473 return nullptr;
2474 }
2475}
2476
2477ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2478 if (file->GetLength() < EI_NIDENT) {
2479 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2480 file->GetPath().c_str());
2481 return nullptr;
2482 }
2483 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2484 file->GetPath().c_str(), error_msg));
2485 if (map == nullptr && map->Size() != EI_NIDENT) {
2486 return nullptr;
2487 }
Ian Rogers13735952014-10-08 12:43:28 -07002488 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002489 if (header[EI_CLASS] == ELFCLASS64) {
2490 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002491 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002492 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002493 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002494 return new ElfFile(elf_file_impl);
2495 } else if (header[EI_CLASS] == ELFCLASS32) {
2496 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002497 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002498 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002499 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002500 return new ElfFile(elf_file_impl);
2501 } else {
2502 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2503 ELFCLASS32, ELFCLASS64,
2504 file->GetPath().c_str(),
2505 header[EI_CLASS]);
2506 return nullptr;
2507 }
2508}
2509
2510#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002511 if (elf64_.get() != nullptr) { \
2512 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002513 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002514 DCHECK(elf32_.get() != nullptr); \
2515 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002516 }
2517
2518bool ElfFile::Load(bool executable, std::string* error_msg) {
2519 DELEGATE_TO_IMPL(Load, executable, error_msg);
2520}
2521
Ian Rogers13735952014-10-08 12:43:28 -07002522const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002523 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2524}
2525
2526size_t ElfFile::Size() const {
2527 DELEGATE_TO_IMPL(Size);
2528}
2529
Ian Rogers13735952014-10-08 12:43:28 -07002530uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002531 DELEGATE_TO_IMPL(Begin);
2532}
2533
Ian Rogers13735952014-10-08 12:43:28 -07002534uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002535 DELEGATE_TO_IMPL(End);
2536}
2537
2538const File& ElfFile::GetFile() const {
2539 DELEGATE_TO_IMPL(GetFile);
2540}
2541
2542bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002543 if (elf32_.get() == nullptr) {
2544 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002545
Ian Rogersd4c4d952014-10-16 20:31:53 -07002546 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2547 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002548 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002549 }
2550 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002551 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002552 }
2553 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002554 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002555 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002556 return true;
2557 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002558 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2559 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002560 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002561 }
2562 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002563 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002564 }
2565 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002566 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002567 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002568 return true;
2569 }
2570}
2571
2572uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2573 const std::string& symbol_name,
2574 bool build_map) {
2575 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2576}
2577
2578size_t ElfFile::GetLoadedSize() const {
2579 DELEGATE_TO_IMPL(GetLoadedSize);
2580}
2581
2582bool ElfFile::Strip(File* file, std::string* error_msg) {
2583 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2584 if (elf_file.get() == nullptr) {
2585 return false;
2586 }
2587
Ian Rogersd4c4d952014-10-16 20:31:53 -07002588 if (elf_file->elf64_.get() != nullptr)
2589 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002590 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002591 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002592}
2593
2594bool ElfFile::Fixup(uintptr_t base_address) {
2595 DELEGATE_TO_IMPL(Fixup, base_address);
2596}
2597
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002598} // namespace art