blob: 2ea7bb6778c7ea3ce75677b653defa9e711848fb [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "elf_file.h"
18
Tong Shen62d1ca32014-09-03 17:24:56 -070019#include <inttypes.h>
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000020#include <sys/types.h>
21#include <unistd.h>
22
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070027#include "base/unix_file/fd_file.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070028#include "elf_file_impl.h"
29#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070030#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "utils.h"
32
33namespace art {
34
David Srbecky533c2072015-04-22 12:20:22 +010035template <typename ElfTypes>
36ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable,
37 bool program_header_only,
38 uint8_t* requested_base)
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070039 : writable_(writable),
Brian Carlstromc1409452014-02-26 14:06:23 -080040 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -070041 header_(nullptr),
42 base_address_(nullptr),
43 program_headers_start_(nullptr),
44 section_headers_start_(nullptr),
45 dynamic_program_header_(nullptr),
46 dynamic_section_start_(nullptr),
47 symtab_section_start_(nullptr),
48 dynsym_section_start_(nullptr),
49 strtab_section_start_(nullptr),
50 dynstr_section_start_(nullptr),
51 hash_section_start_(nullptr),
52 symtab_symbol_table_(nullptr),
53 dynsym_symbol_table_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070054 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -070055 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -080056}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057
David Srbecky533c2072015-04-22 12:20:22 +010058template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080059ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
60 bool writable,
61 bool program_header_only,
62 bool low_4gb,
63 std::string* error_msg,
64 uint8_t* requested_base) {
David Srbecky533c2072015-04-22 12:20:22 +010065 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
66 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067 int prot;
68 int flags;
Alex Light3470ab42014-06-18 10:35:45 -070069 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080070 prot = PROT_READ | PROT_WRITE;
71 flags = MAP_SHARED;
72 } else {
73 prot = PROT_READ;
74 flags = MAP_PRIVATE;
75 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070076 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070077 return nullptr;
78 }
79 return elf_file.release();
80}
81
David Srbecky533c2072015-04-22 12:20:22 +010082template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080083ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
84 int prot,
85 int flags,
86 bool low_4gb,
87 std::string* error_msg) {
David Srbecky533c2072015-04-22 12:20:22 +010088 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
89 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
90 /*requested_base*/nullptr));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070091 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070092 return nullptr;
93 }
94 return elf_file.release();
95}
96
David Srbecky533c2072015-04-22 12:20:22 +010097template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070098bool ElfFileImpl<ElfTypes>::Setup(File* file,
99 int prot,
100 int flags,
101 bool low_4gb,
102 std::string* error_msg) {
103 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -0800104 if (temp_file_length < 0) {
105 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700107 file->GetPath().c_str(), file->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800108 return false;
109 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800110 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700111 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800112 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700113 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700114 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 return false;
116 }
117
Brian Carlstromc1409452014-02-26 14:06:23 -0800118 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700120 size_t elf_header_size = sizeof(Elf_Ehdr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700121 if (!SetMap(file,
122 MemMap::MapFile(elf_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800123 prot,
124 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700125 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800126 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800127 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700128 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800129 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800130 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800131 return false;
132 }
133 // then remap to cover program header
134 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700135 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800136 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700137 "header of %zd bytes: '%s'", file_length,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700138 sizeof(Elf_Ehdr), file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700139 return false;
140 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700141 if (!SetMap(file,
142 MemMap::MapFile(program_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800143 prot,
144 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700145 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800146 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800147 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700148 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800149 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800150 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700151 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800152 return false;
153 }
154 } else {
155 // otherwise map entire file
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700156 if (!SetMap(file,
157 MemMap::MapFile(file->GetLength(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800158 prot,
159 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700160 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800161 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800162 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700163 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800164 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800165 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700166 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800167 return false;
168 }
169 }
170
Andreas Gampedaab38c2014-09-12 18:38:24 -0700171 if (program_header_only_) {
172 program_headers_start_ = Begin() + GetHeader().e_phoff;
173 } else {
174 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
175 return false;
176 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800177
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700179 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
180 return false;
181 }
182
183 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700184 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700185 if (shstrtab_section_header == nullptr) {
186 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700187 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700188 return false;
189 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800190
191 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000192 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700193 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700194 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700195 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800196 return false;
197 }
198
Andreas Gampedaab38c2014-09-12 18:38:24 -0700199 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700200 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700201 return false;
202 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800203
204 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700205 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
206 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700207 if (section_header == nullptr) {
208 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700209 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700210 return false;
211 }
212 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000213 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700214 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700215 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700216 return false;
217 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 break;
219 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000220 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700221 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700222 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700223 return false;
224 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800225 break;
226 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000227 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700229 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
230 // Check that this is named ".dynstr" and ignore otherwise.
231 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
232 if (strncmp(".dynstr", header_name, 8) == 0) {
233 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700234 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700235 return false;
236 }
237 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800238 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700239 // Check that this is named ".strtab" and ignore otherwise.
240 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
241 if (strncmp(".strtab", header_name, 8) == 0) {
242 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700243 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700244 return false;
245 }
246 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800247 }
248 break;
249 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000250 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700251 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700252 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800253 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700254 << file->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800255 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700256 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 return false;
258 }
259 break;
260 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000261 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700262 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700263 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700264 return false;
265 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800266 break;
267 }
268 }
269 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700270
271 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700272 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700273 return false;
274 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800275 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700276
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800277 return true;
278}
279
David Srbecky533c2072015-04-22 12:20:22 +0100280template <typename ElfTypes>
281ElfFileImpl<ElfTypes>::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800282 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800283 delete symtab_symbol_table_;
284 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800285}
286
David Srbecky533c2072015-04-22 12:20:22 +0100287template <typename ElfTypes>
288bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
289 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700290 if (Begin() + offset >= End()) {
291 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700292 file_path_.c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700293 return false;
294 }
295 *target = Begin() + offset;
296 return true;
297}
298
David Srbecky533c2072015-04-22 12:20:22 +0100299template <typename ElfTypes>
300bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
301 const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700302 // Only works in whole-program mode, as we need to iterate over the sections.
303 // Note that we normally can't search by type, as duplicates are allowed for most section types.
304 if (program_header_only_) {
305 return true;
306 }
307
Tong Shen62d1ca32014-09-03 17:24:56 -0700308 Elf_Shdr* source_section = nullptr;
309 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700310 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700311 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
312 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700313
314 if (Begin() + section_header->sh_offset == source) {
315 // Found the source.
316 source_section = section_header;
317 if (target_index) {
318 break;
319 }
320 } else if (Begin() + section_header->sh_offset == target) {
321 target_index = i;
322 target_found = true;
323 if (source_section != nullptr) {
324 break;
325 }
326 }
327 }
328
329 return target_found && source_section != nullptr && source_section->sh_link == target_index;
330}
331
David Srbecky533c2072015-04-22 12:20:22 +0100332template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700333 bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700334 if (!program_header_only_) {
335 // If in full mode, need section headers.
336 if (section_headers_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700337 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700338 return false;
339 }
340 }
341
342 // This is redundant, but defensive.
343 if (dynamic_program_header_ == nullptr) {
344 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700345 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700346 return false;
347 }
348
349 // Need a dynamic section. This is redundant, but defensive.
350 if (dynamic_section_start_ == nullptr) {
351 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700352 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700353 return false;
354 }
355
356 // Symtab validation. These is not really a hard failure, as we are currently not using the
357 // symtab internally, but it's nice to be defensive.
358 if (symtab_section_start_ != nullptr) {
359 // When there's a symtab, there should be a strtab.
360 if (strtab_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700361 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700362 return false;
363 }
364
365 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700366 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
367 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700368 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700369 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700370 return false;
371 }
372 }
373
374 // We always need a dynstr & dynsym.
375 if (dynstr_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700376 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700377 return false;
378 }
379 if (dynsym_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700380 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700381 return false;
382 }
383
384 // Need a hash section for dynamic symbol lookup.
385 if (hash_section_start_ == nullptr) {
386 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700387 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700388 return false;
389 }
390
391 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700392 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
393 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700394 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700395 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700396 return false;
397 }
398
Andreas Gampea696c0a2014-12-10 20:51:45 -0800399 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
400 // us). This is usually the last in an oat file, and a good indicator of whether writing was
401 // successful (or the process crashed and left garbage).
402 if (program_header_only_) {
403 // It might not be mapped, but we can compare against the file size.
404 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
405 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700406 if (offset >= file->GetLength()) {
Andreas Gampea696c0a2014-12-10 20:51:45 -0800407 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700408 file->GetPath().c_str());
Andreas Gampea696c0a2014-12-10 20:51:45 -0800409 return false;
410 }
411 }
412
Andreas Gampedaab38c2014-09-12 18:38:24 -0700413 return true;
414}
415
David Srbecky533c2072015-04-22 12:20:22 +0100416template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700417bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700418 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800419 // MemMap::Open should have already set an error.
420 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800421 return false;
422 }
423 map_.reset(map);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700424 CHECK(map_.get() != nullptr) << file->GetPath();
425 CHECK(map_->Begin() != nullptr) << file->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426
Tong Shen62d1ca32014-09-03 17:24:56 -0700427 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000428 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
429 || (ELFMAG1 != header_->e_ident[EI_MAG1])
430 || (ELFMAG2 != header_->e_ident[EI_MAG2])
431 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800432 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
433 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700434 file->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000435 header_->e_ident[EI_MAG0],
436 header_->e_ident[EI_MAG1],
437 header_->e_ident[EI_MAG2],
438 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800439 return false;
440 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700441 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
442 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800443 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700444 elf_class,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700445 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800446 header_->e_ident[EI_CLASS]);
447 return false;
448 }
449 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
450 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
451 ELFDATA2LSB,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700452 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800453 header_->e_ident[EI_CLASS]);
454 return false;
455 }
456 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
457 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
458 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700459 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800460 header_->e_ident[EI_CLASS]);
461 return false;
462 }
463 if (ET_DYN != header_->e_type) {
464 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
465 ET_DYN,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700466 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800467 header_->e_type);
468 return false;
469 }
470 if (EV_CURRENT != header_->e_version) {
471 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
472 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700473 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800474 header_->e_version);
475 return false;
476 }
477 if (0 != header_->e_entry) {
478 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
479 0,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700480 file->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700481 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800482 return false;
483 }
484 if (0 == header_->e_phoff) {
485 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700486 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800487 return false;
488 }
489 if (0 == header_->e_shoff) {
490 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700491 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800492 return false;
493 }
494 if (0 == header_->e_ehsize) {
495 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700496 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800497 return false;
498 }
499 if (0 == header_->e_phentsize) {
500 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700501 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800502 return false;
503 }
504 if (0 == header_->e_phnum) {
505 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700506 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800507 return false;
508 }
509 if (0 == header_->e_shentsize) {
510 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700511 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800512 return false;
513 }
514 if (0 == header_->e_shnum) {
515 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700516 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800517 return false;
518 }
519 if (0 == header_->e_shstrndx) {
520 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700521 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800522 return false;
523 }
524 if (header_->e_shstrndx >= header_->e_shnum) {
525 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
526 header_->e_shstrndx,
527 header_->e_shnum,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700528 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800529 return false;
530 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800531
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800532 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800533 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700534 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
535 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800536 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700537 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800538 return false;
539 }
540 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700541 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
542 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800543 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700544 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800545 return false;
546 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800547 }
548 return true;
549}
550
David Srbecky533c2072015-04-22 12:20:22 +0100551template <typename ElfTypes>
552typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700553 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800554 return *header_;
555}
556
David Srbecky533c2072015-04-22 12:20:22 +0100557template <typename ElfTypes>
558uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700559 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
560 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800561 return program_headers_start_;
562}
563
David Srbecky533c2072015-04-22 12:20:22 +0100564template <typename ElfTypes>
565uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700566 CHECK(!program_header_only_); // Only used in "full" mode.
567 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800568 return section_headers_start_;
569}
570
David Srbecky533c2072015-04-22 12:20:22 +0100571template <typename ElfTypes>
572typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700573 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800574 return *dynamic_program_header_;
575}
576
David Srbecky533c2072015-04-22 12:20:22 +0100577template <typename ElfTypes>
578typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700579 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800580 return dynamic_section_start_;
581}
582
David Srbecky533c2072015-04-22 12:20:22 +0100583template <typename ElfTypes>
584typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
585 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700586 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800587 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000588 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700589 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800590 break;
591 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000592 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700593 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800594 break;
595 }
596 default: {
597 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700598 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800599 }
600 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800601}
602
David Srbecky533c2072015-04-22 12:20:22 +0100603template <typename ElfTypes>
604const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
605 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700606 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800607 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000608 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700609 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800610 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000611 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700612 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800613 }
614 default: {
615 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700616 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800617 }
618 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800619}
620
David Srbecky533c2072015-04-22 12:20:22 +0100621template <typename ElfTypes>
622const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
623 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700624 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800625 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700626 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800627 }
628 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700629 if (string_section_start == nullptr) {
630 return nullptr;
631 }
632 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800633}
634
Andreas Gampedaab38c2014-09-12 18:38:24 -0700635// WARNING: The following methods do not check for an error condition (non-existent hash section).
636// It is the caller's job to do this.
637
David Srbecky533c2072015-04-22 12:20:22 +0100638template <typename ElfTypes>
639typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800640 return hash_section_start_;
641}
642
David Srbecky533c2072015-04-22 12:20:22 +0100643template <typename ElfTypes>
644typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800645 return GetHashSectionStart()[0];
646}
647
David Srbecky533c2072015-04-22 12:20:22 +0100648template <typename ElfTypes>
649typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800650 return GetHashSectionStart()[1];
651}
652
David Srbecky533c2072015-04-22 12:20:22 +0100653template <typename ElfTypes>
654typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700655 if (i >= GetHashBucketNum()) {
656 *ok = false;
657 return 0;
658 }
659 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800660 // 0 is nbucket, 1 is nchain
661 return GetHashSectionStart()[2 + i];
662}
663
David Srbecky533c2072015-04-22 12:20:22 +0100664template <typename ElfTypes>
665typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600666 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700667 *ok = false;
668 return 0;
669 }
670 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800671 // 0 is nbucket, 1 is nchain, & chains are after buckets
672 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
673}
674
David Srbecky533c2072015-04-22 12:20:22 +0100675template <typename ElfTypes>
676typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800677 return GetHeader().e_phnum;
678}
679
David Srbecky533c2072015-04-22 12:20:22 +0100680template <typename ElfTypes>
681typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700682 CHECK_LT(i, GetProgramHeaderNum()) << file_path_; // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700683 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700684 if (program_header >= End()) {
685 return nullptr; // Failure condition.
686 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700687 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800688}
689
David Srbecky533c2072015-04-22 12:20:22 +0100690template <typename ElfTypes>
691typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700692 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
693 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700694 if (program_header->p_type == type) {
695 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800696 }
697 }
Alex Light3470ab42014-06-18 10:35:45 -0700698 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699}
700
David Srbecky533c2072015-04-22 12:20:22 +0100701template <typename ElfTypes>
702typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800703 return GetHeader().e_shnum;
704}
705
David Srbecky533c2072015-04-22 12:20:22 +0100706template <typename ElfTypes>
707typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800708 // Can only access arbitrary sections when we have the whole file, not just program header.
709 // Even if we Load(), it doesn't bring in all the sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700710 CHECK(!program_header_only_) << file_path_;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700711 if (i >= GetSectionHeaderNum()) {
712 return nullptr; // Failure condition.
713 }
Ian Rogers13735952014-10-08 12:43:28 -0700714 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700715 if (section_header >= End()) {
716 return nullptr; // Failure condition.
717 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700718 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800719}
720
David Srbecky533c2072015-04-22 12:20:22 +0100721template <typename ElfTypes>
722typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800723 // Can only access arbitrary sections when we have the whole file, not just program header.
724 // We could change this to switch on known types if they were detected during loading.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700725 CHECK(!program_header_only_) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700726 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
727 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700728 if (section_header->sh_type == type) {
729 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800730 }
731 }
Alex Light3470ab42014-06-18 10:35:45 -0700732 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800733}
734
735// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800736static unsigned elfhash(const char *_name) {
737 const unsigned char *name = (const unsigned char *) _name;
738 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800739
Brian Carlstromdf629502013-07-17 22:39:56 -0700740 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800741 h = (h << 4) + *name++;
742 g = h & 0xf0000000;
743 h ^= g;
744 h ^= g >> 24;
745 }
746 return h;
747}
748
David Srbecky533c2072015-04-22 12:20:22 +0100749template <typename ElfTypes>
750typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800751 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800752}
753
David Srbecky533c2072015-04-22 12:20:22 +0100754template <typename ElfTypes>
755const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
756 const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700757 // Check that we have a hash section.
758 if (GetHashSectionStart() == nullptr) {
759 return nullptr; // Failure condition.
760 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700761 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700762 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700763 // TODO: we need to change this to calculate base_address_ in ::Open,
764 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700765 return base_address_ + sym->st_value;
766 } else {
767 return nullptr;
768 }
769}
770
Andreas Gampedaab38c2014-09-12 18:38:24 -0700771// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
David Srbecky533c2072015-04-22 12:20:22 +0100772template <typename ElfTypes>
773const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
774 const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700775 if (GetHashBucketNum() == 0) {
776 // No dynamic symbols at all.
777 return nullptr;
778 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700779 Elf_Word hash = elfhash(symbol_name.c_str());
780 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700781 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700782 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700783 if (!ok) {
784 return nullptr;
785 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800786 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700787 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700788 if (symbol == nullptr) {
789 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800790 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700791 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
792 if (symbol_name == name) {
793 return symbol;
794 }
795 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
796 if (!ok) {
797 return nullptr;
798 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800799 }
Alex Light3470ab42014-06-18 10:35:45 -0700800 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800801}
802
David Srbecky533c2072015-04-22 12:20:22 +0100803template <typename ElfTypes>
804bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700805 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
806}
807
David Srbecky533c2072015-04-22 12:20:22 +0100808template <typename ElfTypes>
809typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800810 CHECK(IsSymbolSectionType(section_header.sh_type))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700811 << file_path_ << " " << section_header.sh_type;
812 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800813 return section_header.sh_size / section_header.sh_entsize;
814}
815
David Srbecky533c2072015-04-22 12:20:22 +0100816template <typename ElfTypes>
817typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700818 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700819 if (sym_start == nullptr) {
820 return nullptr;
821 }
822 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800823}
824
David Srbecky533c2072015-04-22 12:20:22 +0100825template <typename ElfTypes>
826typename ElfFileImpl<ElfTypes>::SymbolTable**
827ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700828 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800829 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000830 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800831 return &symtab_symbol_table_;
832 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000833 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800834 return &dynsym_symbol_table_;
835 }
836 default: {
837 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700838 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800839 }
840 }
841}
842
David Srbecky533c2072015-04-22 12:20:22 +0100843template <typename ElfTypes>
844typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
845 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700846 CHECK(!program_header_only_) << file_path_;
847 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800848
849 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700850 if (*symbol_table != nullptr || build_map) {
851 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800852 DCHECK(build_map);
853 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -0700854 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700855 if (symbol_section == nullptr) {
856 return nullptr; // Failure condition.
857 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700858 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700859 if (string_section == nullptr) {
860 return nullptr; // Failure condition.
861 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800862 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700863 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700864 if (symbol == nullptr) {
865 return nullptr; // Failure condition.
866 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700867 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
868 ? ELF64_ST_TYPE(symbol->st_info)
869 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000870 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 continue;
872 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700873 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700874 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800875 continue;
876 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700877 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -0700878 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800879 if (!result.second) {
880 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700881 if ((symbol->st_value != result.first->second->st_value) ||
882 (symbol->st_size != result.first->second->st_size) ||
883 (symbol->st_info != result.first->second->st_info) ||
884 (symbol->st_other != result.first->second->st_other) ||
885 (symbol->st_shndx != result.first->second->st_shndx)) {
886 return nullptr; // Failure condition.
887 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800888 }
889 }
890 }
Alex Light3470ab42014-06-18 10:35:45 -0700891 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -0700892 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800893 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700894 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800895 }
896 return it->second;
897 }
898
899 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -0700900 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700901 if (symbol_section == nullptr) {
902 return nullptr;
903 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700904 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700905 if (string_section == nullptr) {
906 return nullptr;
907 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800908 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700909 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700910 if (symbol == nullptr) {
911 return nullptr; // Failure condition.
912 }
913 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700914 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800915 continue;
916 }
917 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700918 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800919 }
920 }
Alex Light3470ab42014-06-18 10:35:45 -0700921 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800922}
923
David Srbecky533c2072015-04-22 12:20:22 +0100924template <typename ElfTypes>
925typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
926 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700927 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -0700928 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800929 return 0;
930 }
931 return symbol->st_value;
932}
933
David Srbecky533c2072015-04-22 12:20:22 +0100934template <typename ElfTypes>
935const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
936 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700937 CHECK(!program_header_only_) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800938 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -0700939 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700940 return nullptr; // Failure condition.
941 }
942 if (i >= string_section.sh_size) {
943 return nullptr;
944 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800945 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700946 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800947 }
Ian Rogers13735952014-10-08 12:43:28 -0700948 uint8_t* strings = Begin() + string_section.sh_offset;
949 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700950 if (string >= End()) {
951 return nullptr;
952 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800953 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800954}
955
David Srbecky533c2072015-04-22 12:20:22 +0100956template <typename ElfTypes>
957typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700958 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800959}
960
David Srbecky533c2072015-04-22 12:20:22 +0100961template <typename ElfTypes>
962typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700963 CHECK_LT(i, GetDynamicNum()) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800964 return *(GetDynamicSectionStart() + i);
965}
966
David Srbecky533c2072015-04-22 12:20:22 +0100967template <typename ElfTypes>
968typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700969 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
970 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -0700971 if (dyn->d_tag == type) {
972 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800973 }
974 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700975 return nullptr;
Alex Light53cb16b2014-06-12 11:26:29 -0700976}
977
David Srbecky533c2072015-04-22 12:20:22 +0100978template <typename ElfTypes>
979typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700980 Elf_Dyn* dyn = FindDynamicByType(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700981 if (dyn == nullptr) {
Alex Light53cb16b2014-06-12 11:26:29 -0700982 return 0;
983 } else {
984 return dyn->d_un.d_val;
985 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800986}
987
David Srbecky533c2072015-04-22 12:20:22 +0100988template <typename ElfTypes>
989typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700990 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -0700991 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800992}
993
David Srbecky533c2072015-04-22 12:20:22 +0100994template <typename ElfTypes>
995typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700996 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
997 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800998 return section_header.sh_size / section_header.sh_entsize;
999}
1000
David Srbecky533c2072015-04-22 12:20:22 +01001001template <typename ElfTypes>
1002typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001003 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1004 CHECK_LT(i, GetRelNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001005 return *(GetRelSectionStart(section_header) + i);
1006}
1007
David Srbecky533c2072015-04-22 12:20:22 +01001008template <typename ElfTypes>
1009typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001010 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001011 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001012}
1013
David Srbecky533c2072015-04-22 12:20:22 +01001014template <typename ElfTypes>
1015typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001016 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001017 return section_header.sh_size / section_header.sh_entsize;
1018}
1019
David Srbecky533c2072015-04-22 12:20:22 +01001020template <typename ElfTypes>
1021typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001022 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1023 CHECK_LT(i, GetRelaNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001024 return *(GetRelaSectionStart(section_header) + i);
1025}
1026
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001027// Base on bionic phdr_table_get_load_size
David Srbecky533c2072015-04-22 12:20:22 +01001028template <typename ElfTypes>
Vladimir Marko3fc99032015-05-13 19:06:30 +01001029bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
1030 Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1);
1031 Elf_Addr max_vaddr = 0u;
Tong Shen62d1ca32014-09-03 17:24:56 -07001032 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1033 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001034 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001035 continue;
1036 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001037 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001038 if (begin_vaddr < min_vaddr) {
1039 min_vaddr = begin_vaddr;
1040 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001041 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001042 if (UNLIKELY(begin_vaddr > end_vaddr)) {
1043 std::ostringstream oss;
1044 oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex
1045 << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001046 << " in ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001047 *error_msg = oss.str();
1048 *size = static_cast<size_t>(-1);
1049 return false;
1050 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001051 if (end_vaddr > max_vaddr) {
1052 max_vaddr = end_vaddr;
1053 }
1054 }
1055 min_vaddr = RoundDown(min_vaddr, kPageSize);
1056 max_vaddr = RoundUp(max_vaddr, kPageSize);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001057 CHECK_LT(min_vaddr, max_vaddr) << file_path_;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001058 Elf_Addr loaded_size = max_vaddr - min_vaddr;
1059 // Check that the loaded_size fits in size_t.
1060 if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) {
1061 std::ostringstream oss;
1062 oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x"
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001063 << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001064 *error_msg = oss.str();
1065 *size = static_cast<size_t>(-1);
1066 return false;
1067 }
1068 *size = loaded_size;
1069 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001070}
1071
David Srbecky533c2072015-04-22 12:20:22 +01001072template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001073bool ElfFileImpl<ElfTypes>::Load(File* file,
1074 bool executable,
1075 bool low_4gb,
1076 std::string* error_msg) {
1077 CHECK(program_header_only_) << file->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001078
1079 if (executable) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001080 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001081 if (elf_ISA != kRuntimeISA) {
1082 std::ostringstream oss;
1083 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1084 *error_msg = oss.str();
1085 return false;
1086 }
1087 }
1088
Jim_Guoa62a5882014-04-28 11:11:57 +08001089 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001090 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1091 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001092 if (program_header == nullptr) {
1093 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001094 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001095 return false;
1096 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001097
1098 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001099 if (program_header->p_type == PT_DYNAMIC) {
1100 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001101 continue;
1102 }
1103
1104 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001105 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001106 continue;
1107 }
1108
1109 // Found something to load.
1110
Jim_Guoa62a5882014-04-28 11:11:57 +08001111 // Before load the actual segments, reserve a contiguous chunk
1112 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001113 // permissions. We'll then carve that up with the proper
1114 // permissions as we load the actual segments. If p_vaddr is
1115 // non-zero, the segments require the specific address specified,
1116 // which either was specified in the file because we already set
1117 // base_address_ after the first zero segment).
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001118 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -08001119 if (temp_file_length < 0) {
1120 errno = -temp_file_length;
1121 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001122 file->GetPath().c_str(), file->Fd(), strerror(errno));
Ian Rogerscdfcf372014-01-23 20:38:36 -08001123 return false;
1124 }
1125 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001126 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001127 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1128 uint8_t* reserve_base_override = reserve_base;
1129 // Override the base (e.g. when compiling with --compile-pic)
1130 if (requested_base_ != nullptr) {
1131 reserve_base_override = requested_base_;
1132 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001133 std::string reservation_name("ElfFile reservation for ");
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001134 reservation_name += file->GetPath();
Vladimir Marko3fc99032015-05-13 19:06:30 +01001135 size_t loaded_size;
1136 if (!GetLoadedSize(&loaded_size, error_msg)) {
1137 DCHECK(!error_msg->empty());
1138 return false;
1139 }
Ian Rogers700a4022014-05-19 16:49:03 -07001140 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001141 reserve_base_override,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001142 loaded_size,
1143 PROT_NONE,
1144 low_4gb,
1145 false,
Jim_Guoa62a5882014-04-28 11:11:57 +08001146 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001147 if (reserve.get() == nullptr) {
1148 *error_msg = StringPrintf("Failed to allocate %s: %s",
1149 reservation_name.c_str(), error_msg->c_str());
1150 return false;
1151 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001152 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001153
1154 // Base address is the difference of actual mapped location and the p_vaddr
1155 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1156 - reinterpret_cast<uintptr_t>(reserve_base));
1157 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1158 // dynamic memory address of where that object is actually mapped
1159 //
1160 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1161 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001162 segments_.push_back(reserve.release());
1163 }
1164 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001165 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001166 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001167 }
Ian Rogers13735952014-10-08 12:43:28 -07001168 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001169 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001170 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001171 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001172 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001173 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001174 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001175 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001176 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001177 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001178 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001179 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001180 if (writable_) {
1181 prot |= PROT_WRITE;
1182 flags |= MAP_SHARED;
1183 } else {
1184 flags |= MAP_PRIVATE;
1185 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001186 if (program_header->p_filesz > program_header->p_memsz) {
1187 *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1188 static_cast<uint64_t>(program_header->p_filesz),
1189 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001190 file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001191 return false;
1192 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001193 if (program_header->p_filesz < program_header->p_memsz &&
1194 !IsAligned<kPageSize>(program_header->p_filesz)) {
1195 *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1196 " < %" PRIu64 "): %s",
1197 static_cast<uint64_t>(program_header->p_filesz),
1198 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001199 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001200 return false;
1201 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001202 if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1203 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1204 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1205 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001206 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001207 return false;
1208 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001209 if (program_header->p_filesz != 0u) {
1210 std::unique_ptr<MemMap> segment(
1211 MemMap::MapFileAtAddress(p_vaddr,
1212 program_header->p_filesz,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001213 prot,
1214 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001215 file->Fd(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001216 program_header->p_offset,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001217 /*low4_gb*/false,
1218 /*reuse*/true, // implies MAP_FIXED
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001219 file->GetPath().c_str(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001220 error_msg));
1221 if (segment.get() == nullptr) {
1222 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001223 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001224 return false;
1225 }
1226 if (segment->Begin() != p_vaddr) {
1227 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1228 "instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001229 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001230 return false;
1231 }
1232 segments_.push_back(segment.release());
1233 }
1234 if (program_header->p_filesz < program_header->p_memsz) {
1235 std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001236 static_cast<uint64_t>(i), file->GetPath().c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001237 std::unique_ptr<MemMap> segment(
1238 MemMap::MapAnonymous(name.c_str(),
1239 p_vaddr + program_header->p_filesz,
1240 program_header->p_memsz - program_header->p_filesz,
1241 prot, false, true /* reuse */, error_msg));
1242 if (segment == nullptr) {
1243 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001244 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001245 return false;
1246 }
1247 if (segment->Begin() != p_vaddr) {
1248 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1249 "at expected address %p, instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001250 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001251 return false;
1252 }
1253 segments_.push_back(segment.release());
1254 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001255 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001256
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001257 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001258 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001259 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1260 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001261 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001262 return false;
1263 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001264 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001265
Tong Shen62d1ca32014-09-03 17:24:56 -07001266 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1267 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001268 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001269 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001270 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001271 if (!ValidPointer(d_ptr)) {
1272 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001273 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001274 return false;
1275 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001276 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001277 break;
1278 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001279 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001280 if (!ValidPointer(d_ptr)) {
1281 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001282 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001283 return false;
1284 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001285 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1286 break;
1287 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001288 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001289 if (!ValidPointer(d_ptr)) {
1290 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001291 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001292 return false;
1293 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001294 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001295 break;
1296 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001297 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001298 if (GetDynamicNum() != i+1) {
1299 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1300 "expected %d as implied by size of PT_DYNAMIC segment in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001301 i + 1, GetDynamicNum(), file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001302 return false;
1303 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001304 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001305 }
1306 }
1307 }
1308
Andreas Gampedaab38c2014-09-12 18:38:24 -07001309 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001310 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001311 return false;
1312 }
1313
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001314 return true;
1315}
1316
David Srbecky533c2072015-04-22 12:20:22 +01001317template <typename ElfTypes>
1318bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001319 for (size_t i = 0; i < segments_.size(); ++i) {
1320 const MemMap* segment = segments_[i];
1321 if (segment->Begin() <= start && start < segment->End()) {
1322 return true;
1323 }
1324 }
1325 return false;
1326}
1327
Alex Light3470ab42014-06-18 10:35:45 -07001328
David Srbecky533c2072015-04-22 12:20:22 +01001329template <typename ElfTypes>
1330typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1331 const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001332 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001333 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001334 if (shstrtab_sec == nullptr) {
1335 return nullptr;
1336 }
Alex Light3470ab42014-06-18 10:35:45 -07001337 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001338 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001339 if (shdr == nullptr) {
1340 return nullptr;
1341 }
1342 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001343 if (sec_name == nullptr) {
1344 continue;
1345 }
1346 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001347 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001348 }
1349 }
1350 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001351}
1352
David Srbecky533c2072015-04-22 12:20:22 +01001353template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001354bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001355 if (base_address_delta == 0) {
1356 return true;
1357 }
David Srbeckyf8980872015-05-22 17:04:47 +01001358 return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1359 ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1360 ApplyOatPatchesTo(".debug_line", base_address_delta);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001361}
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001362
David Srbecky533c2072015-04-22 12:20:22 +01001363template <typename ElfTypes>
1364bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
David Srbeckyf8980872015-05-22 17:04:47 +01001365 const char* target_section_name, Elf_Addr delta) {
1366 auto target_section = FindSectionByName(target_section_name);
1367 if (target_section == nullptr) {
1368 return true;
1369 }
1370 std::string patches_name = target_section_name + std::string(".oat_patches");
1371 auto patches_section = FindSectionByName(patches_name.c_str());
David Srbecky2f6cdb02015-04-11 00:17:53 +01001372 if (patches_section == nullptr) {
David Srbeckyf8980872015-05-22 17:04:47 +01001373 LOG(ERROR) << patches_name << " section not found.";
Alex Light3470ab42014-06-18 10:35:45 -07001374 return false;
1375 }
David Srbecky2f6cdb02015-04-11 00:17:53 +01001376 if (patches_section->sh_type != SHT_OAT_PATCH) {
David Srbeckyf8980872015-05-22 17:04:47 +01001377 LOG(ERROR) << "Unexpected type of " << patches_name;
Alex Light3470ab42014-06-18 10:35:45 -07001378 return false;
1379 }
David Srbeckyf8980872015-05-22 17:04:47 +01001380 ApplyOatPatches(
David Srbecky2f6cdb02015-04-11 00:17:53 +01001381 Begin() + patches_section->sh_offset,
1382 Begin() + patches_section->sh_offset + patches_section->sh_size,
David Srbeckyf8980872015-05-22 17:04:47 +01001383 delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +01001384 Begin() + target_section->sh_offset,
David Srbeckyf8980872015-05-22 17:04:47 +01001385 Begin() + target_section->sh_offset + target_section->sh_size);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001386 return true;
1387}
1388
David Srbeckyf8980872015-05-22 17:04:47 +01001389// Apply LEB128 encoded patches to given section.
David Srbecky533c2072015-04-22 12:20:22 +01001390template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001391void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1392 const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky533c2072015-04-22 12:20:22 +01001393 uint8_t* to_patch, const uint8_t* to_patch_end) {
David Srbeckyf8980872015-05-22 17:04:47 +01001394 typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress;
1395 while (patches < patches_end) {
1396 to_patch += DecodeUnsignedLeb128(&patches);
1397 DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1398 DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1399 *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
David Srbecky2f6cdb02015-04-11 00:17:53 +01001400 }
Alex Light3470ab42014-06-18 10:35:45 -07001401}
Mark Mendellae9fd932014-02-10 16:14:35 -08001402
David Srbecky533c2072015-04-22 12:20:22 +01001403template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001404bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001405 // ELF files produced by MCLinker look roughly like this
1406 //
1407 // +------------+
1408 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
1409 // +------------+
1410 // | Elf_Phdr | program headers
1411 // | Elf_Phdr |
1412 // | ... |
1413 // | Elf_Phdr |
1414 // +------------+
1415 // | section | mixture of needed and unneeded sections
1416 // +------------+
1417 // | section |
1418 // +------------+
1419 // | ... |
1420 // +------------+
1421 // | section |
1422 // +------------+
1423 // | Elf_Shdr | section headers
1424 // | Elf_Shdr |
1425 // | ... | contains offset to section start
1426 // | Elf_Shdr |
1427 // +------------+
1428 //
1429 // To strip:
1430 // - leave the Elf_Ehdr and Elf_Phdr values in place.
1431 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1432 // - move the sections are keeping up to fill in gaps of sections we want to strip
1433 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1434 // - truncate rest of file
1435 //
1436
1437 std::vector<Elf_Shdr> section_headers;
1438 std::vector<Elf_Word> section_headers_original_indexes;
1439 section_headers.reserve(GetSectionHeaderNum());
1440
1441
1442 Elf_Shdr* string_section = GetSectionNameStringSection();
1443 CHECK(string_section != nullptr);
1444 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1445 Elf_Shdr* sh = GetSectionHeader(i);
1446 CHECK(sh != nullptr);
1447 const char* name = GetString(*string_section, sh->sh_name);
1448 if (name == nullptr) {
1449 CHECK_EQ(0U, i);
1450 section_headers.push_back(*sh);
1451 section_headers_original_indexes.push_back(0);
1452 continue;
1453 }
1454 if (StartsWith(name, ".debug")
1455 || (strcmp(name, ".strtab") == 0)
1456 || (strcmp(name, ".symtab") == 0)) {
1457 continue;
1458 }
1459 section_headers.push_back(*sh);
1460 section_headers_original_indexes.push_back(i);
1461 }
1462 CHECK_NE(0U, section_headers.size());
1463 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1464
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001465 // section 0 is the null section, sections start at offset of first section
Tong Shen62d1ca32014-09-03 17:24:56 -07001466 CHECK(GetSectionHeader(1) != nullptr);
1467 Elf_Off offset = GetSectionHeader(1)->sh_offset;
1468 for (size_t i = 1; i < section_headers.size(); i++) {
1469 Elf_Shdr& new_sh = section_headers[i];
1470 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1471 CHECK(old_sh != nullptr);
1472 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1473 if (old_sh->sh_addralign > 1) {
1474 offset = RoundUp(offset, old_sh->sh_addralign);
1475 }
1476 if (old_sh->sh_offset == offset) {
1477 // already in place
1478 offset += old_sh->sh_size;
1479 continue;
1480 }
1481 // shift section earlier
1482 memmove(Begin() + offset,
1483 Begin() + old_sh->sh_offset,
1484 old_sh->sh_size);
1485 new_sh.sh_offset = offset;
1486 offset += old_sh->sh_size;
1487 }
1488
1489 Elf_Off shoff = offset;
1490 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1491 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1492 offset += section_headers_size_in_bytes;
1493
1494 GetHeader().e_shnum = section_headers.size();
1495 GetHeader().e_shoff = shoff;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001496 int result = ftruncate(file->Fd(), offset);
Tong Shen62d1ca32014-09-03 17:24:56 -07001497 if (result != 0) {
1498 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001499 file->GetPath().c_str(), strerror(errno));
Tong Shen62d1ca32014-09-03 17:24:56 -07001500 return false;
1501 }
1502 return true;
1503}
1504
1505static const bool DEBUG_FIXUP = false;
1506
David Srbecky533c2072015-04-22 12:20:22 +01001507template <typename ElfTypes>
1508bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001509 if (!FixupDynamic(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001510 LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001511 return false;
1512 }
1513 if (!FixupSectionHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001514 LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001515 return false;
1516 }
1517 if (!FixupProgramHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001518 LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001519 return false;
1520 }
1521 if (!FixupSymbols(base_address, true)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001522 LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001523 return false;
1524 }
1525 if (!FixupSymbols(base_address, false)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001526 LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001527 return false;
1528 }
1529 if (!FixupRelocations(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001530 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001531 return false;
1532 }
Andreas Gampe3c54b002015-04-07 16:09:30 -07001533 static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1534 if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001535 LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001536 return false;
1537 }
1538 return true;
1539}
1540
David Srbecky533c2072015-04-22 12:20:22 +01001541template <typename ElfTypes>
1542bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001543 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1544 Elf_Dyn& elf_dyn = GetDynamic(i);
1545 Elf_Word d_tag = elf_dyn.d_tag;
1546 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1547 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1548 if (DEBUG_FIXUP) {
1549 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001550 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001551 static_cast<uint64_t>(d_ptr),
1552 static_cast<uint64_t>(d_ptr + base_address));
1553 }
1554 d_ptr += base_address;
1555 elf_dyn.d_un.d_ptr = d_ptr;
1556 }
1557 }
1558 return true;
1559}
1560
David Srbecky533c2072015-04-22 12:20:22 +01001561template <typename ElfTypes>
1562bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001563 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1564 Elf_Shdr* sh = GetSectionHeader(i);
1565 CHECK(sh != nullptr);
1566 // 0 implies that the section will not exist in the memory of the process
1567 if (sh->sh_addr == 0) {
1568 continue;
1569 }
1570 if (DEBUG_FIXUP) {
1571 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001572 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001573 static_cast<uint64_t>(sh->sh_addr),
1574 static_cast<uint64_t>(sh->sh_addr + base_address));
1575 }
1576 sh->sh_addr += base_address;
1577 }
1578 return true;
1579}
1580
David Srbecky533c2072015-04-22 12:20:22 +01001581template <typename ElfTypes>
1582bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001583 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1584 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1585 Elf_Phdr* ph = GetProgramHeader(i);
1586 CHECK(ph != nullptr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001587 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001588 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001589 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001590 if (DEBUG_FIXUP) {
1591 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001592 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001593 static_cast<uint64_t>(ph->p_vaddr),
1594 static_cast<uint64_t>(ph->p_vaddr + base_address));
1595 }
1596 ph->p_vaddr += base_address;
1597 ph->p_paddr += base_address;
1598 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001599 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001600 }
1601 return true;
1602}
1603
David Srbecky533c2072015-04-22 12:20:22 +01001604template <typename ElfTypes>
1605bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001606 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1607 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1608 Elf_Shdr* symbol_section = FindSectionByType(section_type);
1609 if (symbol_section == nullptr) {
1610 // file is missing optional .symtab
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001611 CHECK(!dynamic) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001612 return true;
1613 }
1614 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1615 Elf_Sym* symbol = GetSymbol(section_type, i);
1616 CHECK(symbol != nullptr);
1617 if (symbol->st_value != 0) {
1618 if (DEBUG_FIXUP) {
1619 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001620 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001621 static_cast<uint64_t>(symbol->st_value),
1622 static_cast<uint64_t>(symbol->st_value + base_address));
1623 }
1624 symbol->st_value += base_address;
1625 }
1626 }
1627 return true;
1628}
1629
David Srbecky533c2072015-04-22 12:20:22 +01001630template <typename ElfTypes>
1631bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001632 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1633 Elf_Shdr* sh = GetSectionHeader(i);
1634 CHECK(sh != nullptr);
1635 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001636 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1637 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001638 if (DEBUG_FIXUP) {
1639 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001640 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001641 static_cast<uint64_t>(rel.r_offset),
1642 static_cast<uint64_t>(rel.r_offset + base_address));
1643 }
1644 rel.r_offset += base_address;
1645 }
1646 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001647 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1648 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001649 if (DEBUG_FIXUP) {
1650 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001651 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001652 static_cast<uint64_t>(rela.r_offset),
1653 static_cast<uint64_t>(rela.r_offset + base_address));
1654 }
1655 rela.r_offset += base_address;
1656 }
1657 }
1658 }
1659 return true;
1660}
1661
1662// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +01001663template class ElfFileImpl<ElfTypes32>;
1664template class ElfFileImpl<ElfTypes64>;
Tong Shen62d1ca32014-09-03 17:24:56 -07001665
Ian Rogersd4c4d952014-10-16 20:31:53 -07001666ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001667}
1668
Ian Rogersd4c4d952014-10-16 20:31:53 -07001669ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001670}
1671
1672ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001673 // Should never have 32 and 64-bit impls.
1674 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001675}
1676
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001677ElfFile* ElfFile::Open(File* file,
1678 bool writable,
1679 bool program_header_only,
1680 bool low_4gb,
1681 std::string* error_msg,
Igor Murashkin46774762014-10-22 11:37:02 -07001682 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001683 if (file->GetLength() < EI_NIDENT) {
1684 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1685 file->GetPath().c_str());
1686 return nullptr;
1687 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001688 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1689 PROT_READ,
1690 MAP_PRIVATE,
1691 file->Fd(),
1692 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001693 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001694 file->GetPath().c_str(),
1695 error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001696 if (map == nullptr && map->Size() != EI_NIDENT) {
1697 return nullptr;
1698 }
Ian Rogers13735952014-10-08 12:43:28 -07001699 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001700 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001701 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1702 writable,
1703 program_header_only,
1704 low_4gb,
1705 error_msg,
1706 requested_base);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001707 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001708 return nullptr;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001709 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001710 return new ElfFile(elf_file_impl);
1711 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001712 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1713 writable,
1714 program_header_only,
1715 low_4gb,
1716 error_msg,
1717 requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001718 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001719 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001720 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001721 return new ElfFile(elf_file_impl);
1722 } else {
1723 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1724 ELFCLASS32, ELFCLASS64,
1725 file->GetPath().c_str(),
1726 header[EI_CLASS]);
1727 return nullptr;
1728 }
1729}
1730
1731ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001732 // low_4gb support not required for this path.
1733 constexpr bool low_4gb = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001734 if (file->GetLength() < EI_NIDENT) {
1735 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1736 file->GetPath().c_str());
1737 return nullptr;
1738 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001739 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1740 PROT_READ,
1741 MAP_PRIVATE,
1742 file->Fd(),
1743 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001744 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001745 file->GetPath().c_str(),
1746 error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001747 if (map == nullptr && map->Size() != EI_NIDENT) {
1748 return nullptr;
1749 }
Ian Rogers13735952014-10-08 12:43:28 -07001750 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001751 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001752 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1753 mmap_prot,
1754 mmap_flags,
1755 low_4gb,
1756 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001757 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001758 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001759 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001760 return new ElfFile(elf_file_impl);
1761 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001762 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1763 mmap_prot,
1764 mmap_flags,
1765 low_4gb,
1766 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001767 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001768 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001769 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001770 return new ElfFile(elf_file_impl);
1771 } else {
1772 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1773 ELFCLASS32, ELFCLASS64,
1774 file->GetPath().c_str(),
1775 header[EI_CLASS]);
1776 return nullptr;
1777 }
1778}
1779
1780#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001781 if (elf64_.get() != nullptr) { \
1782 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001783 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001784 DCHECK(elf32_.get() != nullptr); \
1785 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001786 }
1787
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001788bool ElfFile::Load(File* file, bool executable, bool low_4gb, std::string* error_msg) {
1789 DELEGATE_TO_IMPL(Load, file, executable, low_4gb, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001790}
1791
Ian Rogers13735952014-10-08 12:43:28 -07001792const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001793 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1794}
1795
1796size_t ElfFile::Size() const {
1797 DELEGATE_TO_IMPL(Size);
1798}
1799
Ian Rogers13735952014-10-08 12:43:28 -07001800uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001801 DELEGATE_TO_IMPL(Begin);
1802}
1803
Ian Rogers13735952014-10-08 12:43:28 -07001804uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001805 DELEGATE_TO_IMPL(End);
1806}
1807
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001808const std::string& ElfFile::GetFilePath() const {
1809 DELEGATE_TO_IMPL(GetFilePath);
Tong Shen62d1ca32014-09-03 17:24:56 -07001810}
1811
Alex Light0eb76d22015-08-11 18:03:47 -07001812bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1813 uint64_t* size) const {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001814 if (elf32_.get() == nullptr) {
1815 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001816
Ian Rogersd4c4d952014-10-16 20:31:53 -07001817 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1818 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001819 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001820 }
1821 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001822 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001823 }
1824 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001825 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001826 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001827 return true;
1828 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001829 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1830 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001831 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001832 }
1833 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001834 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001835 }
1836 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001837 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001838 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001839 return true;
1840 }
1841}
1842
Jeff Hao24ec0282016-03-17 21:32:45 -07001843bool ElfFile::HasSection(const std::string& name) const {
1844 if (elf64_.get() != nullptr) {
1845 return elf64_->FindSectionByName(name) != nullptr;
1846 } else {
1847 return elf32_->FindSectionByName(name) != nullptr;
1848 }
1849}
1850
Tong Shen62d1ca32014-09-03 17:24:56 -07001851uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1852 const std::string& symbol_name,
1853 bool build_map) {
1854 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1855}
1856
Vladimir Marko3fc99032015-05-13 19:06:30 +01001857bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1858 DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001859}
1860
1861bool ElfFile::Strip(File* file, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001862 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001863 if (elf_file.get() == nullptr) {
1864 return false;
1865 }
1866
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001867 if (elf_file->elf64_.get() != nullptr) {
1868 return elf_file->elf64_->Strip(file, error_msg);
1869 } else {
1870 return elf_file->elf32_->Strip(file, error_msg);
1871 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001872}
1873
Andreas Gampe3c54b002015-04-07 16:09:30 -07001874bool ElfFile::Fixup(uint64_t base_address) {
1875 if (elf64_.get() != nullptr) {
1876 return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1877 } else {
1878 DCHECK(elf32_.get() != nullptr);
1879 CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1880 return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1881 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001882 DELEGATE_TO_IMPL(Fixup, base_address);
1883}
1884
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001885} // namespace art