blob: afe4eeb05923c5f90a8f822ffa77435b17d6b6bc [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>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070020#include <sys/mman.h> // For the PROT_* and MAP_* constants.
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000021#include <sys/types.h>
22#include <unistd.h>
23
Andreas Gampe46ee31b2016-12-14 10:11:49 -080024#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080025#include "android-base/strings.h"
26
Ian Rogersd582fa42014-11-05 23:46:43 -080027#include "arch/instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "base/logging.h"
29#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070030#include "base/unix_file/fd_file.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070031#include "elf_file_impl.h"
32#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070033#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034#include "utils.h"
35
36namespace art {
37
Andreas Gampe46ee31b2016-12-14 10:11:49 -080038using android::base::StringPrintf;
39
David Srbecky533c2072015-04-22 12:20:22 +010040template <typename ElfTypes>
41ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable,
42 bool program_header_only,
43 uint8_t* requested_base)
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070044 : writable_(writable),
Brian Carlstromc1409452014-02-26 14:06:23 -080045 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -070046 header_(nullptr),
47 base_address_(nullptr),
48 program_headers_start_(nullptr),
49 section_headers_start_(nullptr),
50 dynamic_program_header_(nullptr),
51 dynamic_section_start_(nullptr),
52 symtab_section_start_(nullptr),
53 dynsym_section_start_(nullptr),
54 strtab_section_start_(nullptr),
55 dynstr_section_start_(nullptr),
56 hash_section_start_(nullptr),
57 symtab_symbol_table_(nullptr),
58 dynsym_symbol_table_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070059 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -070060 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -080061}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080062
David Srbecky533c2072015-04-22 12:20:22 +010063template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080064ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
65 bool writable,
66 bool program_header_only,
67 bool low_4gb,
68 std::string* error_msg,
69 uint8_t* requested_base) {
David Srbecky533c2072015-04-22 12:20:22 +010070 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
71 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072 int prot;
73 int flags;
Alex Light3470ab42014-06-18 10:35:45 -070074 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 prot = PROT_READ | PROT_WRITE;
76 flags = MAP_SHARED;
77 } else {
78 prot = PROT_READ;
79 flags = MAP_PRIVATE;
80 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070081 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070082 return nullptr;
83 }
84 return elf_file.release();
85}
86
David Srbecky533c2072015-04-22 12:20:22 +010087template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080088ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
89 int prot,
90 int flags,
91 bool low_4gb,
92 std::string* error_msg) {
David Srbecky533c2072015-04-22 12:20:22 +010093 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
94 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
95 /*requested_base*/nullptr));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070096 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070097 return nullptr;
98 }
99 return elf_file.release();
100}
101
David Srbecky533c2072015-04-22 12:20:22 +0100102template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700103bool ElfFileImpl<ElfTypes>::Setup(File* file,
104 int prot,
105 int flags,
106 bool low_4gb,
107 std::string* error_msg) {
108 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -0800109 if (temp_file_length < 0) {
110 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700112 file->GetPath().c_str(), file->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800113 return false;
114 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800115 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700116 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800117 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700118 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700119 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120 return false;
121 }
122
Brian Carlstromc1409452014-02-26 14:06:23 -0800123 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700125 size_t elf_header_size = sizeof(Elf_Ehdr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700126 if (!SetMap(file,
127 MemMap::MapFile(elf_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800128 prot,
129 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700130 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800131 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800132 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700133 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800134 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800135 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800136 return false;
137 }
138 // then remap to cover program header
139 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700140 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800141 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700142 "header of %zd bytes: '%s'", file_length,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700143 sizeof(Elf_Ehdr), file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700144 return false;
145 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700146 if (!SetMap(file,
147 MemMap::MapFile(program_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800148 prot,
149 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700150 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800151 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800152 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700153 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800154 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800155 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700156 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800157 return false;
158 }
159 } else {
160 // otherwise map entire file
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700161 if (!SetMap(file,
162 MemMap::MapFile(file->GetLength(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800163 prot,
164 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700165 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800166 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800167 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700168 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800169 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800170 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700171 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800172 return false;
173 }
174 }
175
Andreas Gampedaab38c2014-09-12 18:38:24 -0700176 if (program_header_only_) {
177 program_headers_start_ = Begin() + GetHeader().e_phoff;
178 } else {
179 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
180 return false;
181 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800183 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700184 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
185 return false;
186 }
187
188 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700189 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700190 if (shstrtab_section_header == nullptr) {
191 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700192 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700193 return false;
194 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195
196 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000197 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700198 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700199 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700200 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203
Andreas Gampedaab38c2014-09-12 18:38:24 -0700204 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700205 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700206 return false;
207 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208
209 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700210 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
211 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700212 if (section_header == nullptr) {
213 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700214 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700215 return false;
216 }
217 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000218 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700219 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700220 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700221 return false;
222 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800223 break;
224 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000225 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700226 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700227 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700228 return false;
229 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 break;
231 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000232 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700234 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
235 // Check that this is named ".dynstr" and ignore otherwise.
236 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
237 if (strncmp(".dynstr", header_name, 8) == 0) {
238 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700239 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700240 return false;
241 }
242 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800243 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700244 // Check that this is named ".strtab" and ignore otherwise.
245 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
246 if (strncmp(".strtab", header_name, 8) == 0) {
247 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700248 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700249 return false;
250 }
251 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800252 }
253 break;
254 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000255 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700256 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700257 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800258 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700259 << file->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800260 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700261 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800262 return false;
263 }
264 break;
265 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000266 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700267 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700268 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700269 return false;
270 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800271 break;
272 }
273 }
274 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700275
276 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700277 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700278 return false;
279 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800280 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700281
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800282 return true;
283}
284
David Srbecky533c2072015-04-22 12:20:22 +0100285template <typename ElfTypes>
286ElfFileImpl<ElfTypes>::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800287 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800288 delete symtab_symbol_table_;
289 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800290}
291
David Srbecky533c2072015-04-22 12:20:22 +0100292template <typename ElfTypes>
293bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
294 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700295 if (Begin() + offset >= End()) {
296 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700297 file_path_.c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700298 return false;
299 }
300 *target = Begin() + offset;
301 return true;
302}
303
David Srbecky533c2072015-04-22 12:20:22 +0100304template <typename ElfTypes>
305bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
306 const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700307 // Only works in whole-program mode, as we need to iterate over the sections.
308 // Note that we normally can't search by type, as duplicates are allowed for most section types.
309 if (program_header_only_) {
310 return true;
311 }
312
Tong Shen62d1ca32014-09-03 17:24:56 -0700313 Elf_Shdr* source_section = nullptr;
314 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700315 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700316 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
317 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700318
319 if (Begin() + section_header->sh_offset == source) {
320 // Found the source.
321 source_section = section_header;
322 if (target_index) {
323 break;
324 }
325 } else if (Begin() + section_header->sh_offset == target) {
326 target_index = i;
327 target_found = true;
328 if (source_section != nullptr) {
329 break;
330 }
331 }
332 }
333
334 return target_found && source_section != nullptr && source_section->sh_link == target_index;
335}
336
David Srbecky533c2072015-04-22 12:20:22 +0100337template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700338 bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700339 if (!program_header_only_) {
340 // If in full mode, need section headers.
341 if (section_headers_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700342 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700343 return false;
344 }
345 }
346
347 // This is redundant, but defensive.
348 if (dynamic_program_header_ == nullptr) {
349 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700350 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700351 return false;
352 }
353
354 // Need a dynamic section. This is redundant, but defensive.
355 if (dynamic_section_start_ == nullptr) {
356 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700357 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700358 return false;
359 }
360
361 // Symtab validation. These is not really a hard failure, as we are currently not using the
362 // symtab internally, but it's nice to be defensive.
363 if (symtab_section_start_ != nullptr) {
364 // When there's a symtab, there should be a strtab.
365 if (strtab_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700366 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700367 return false;
368 }
369
370 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700371 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
372 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700373 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700374 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700375 return false;
376 }
377 }
378
379 // We always need a dynstr & dynsym.
380 if (dynstr_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700381 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700382 return false;
383 }
384 if (dynsym_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700385 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700386 return false;
387 }
388
389 // Need a hash section for dynamic symbol lookup.
390 if (hash_section_start_ == nullptr) {
391 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700392 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700393 return false;
394 }
395
396 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700397 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
398 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700399 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700400 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700401 return false;
402 }
403
Andreas Gampea696c0a2014-12-10 20:51:45 -0800404 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
405 // us). This is usually the last in an oat file, and a good indicator of whether writing was
406 // successful (or the process crashed and left garbage).
407 if (program_header_only_) {
408 // It might not be mapped, but we can compare against the file size.
409 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
410 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700411 if (offset >= file->GetLength()) {
Andreas Gampea696c0a2014-12-10 20:51:45 -0800412 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700413 file->GetPath().c_str());
Andreas Gampea696c0a2014-12-10 20:51:45 -0800414 return false;
415 }
416 }
417
Andreas Gampedaab38c2014-09-12 18:38:24 -0700418 return true;
419}
420
David Srbecky533c2072015-04-22 12:20:22 +0100421template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700422bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700423 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800424 // MemMap::Open should have already set an error.
425 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426 return false;
427 }
428 map_.reset(map);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700429 CHECK(map_.get() != nullptr) << file->GetPath();
430 CHECK(map_->Begin() != nullptr) << file->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800431
Tong Shen62d1ca32014-09-03 17:24:56 -0700432 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000433 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
434 || (ELFMAG1 != header_->e_ident[EI_MAG1])
435 || (ELFMAG2 != header_->e_ident[EI_MAG2])
436 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800437 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
438 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700439 file->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000440 header_->e_ident[EI_MAG0],
441 header_->e_ident[EI_MAG1],
442 header_->e_ident[EI_MAG2],
443 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800444 return false;
445 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700446 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
447 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800448 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700449 elf_class,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700450 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800451 header_->e_ident[EI_CLASS]);
452 return false;
453 }
454 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
455 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
456 ELFDATA2LSB,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700457 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800458 header_->e_ident[EI_CLASS]);
459 return false;
460 }
461 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
462 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
463 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700464 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800465 header_->e_ident[EI_CLASS]);
466 return false;
467 }
468 if (ET_DYN != header_->e_type) {
469 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
470 ET_DYN,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700471 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800472 header_->e_type);
473 return false;
474 }
475 if (EV_CURRENT != header_->e_version) {
476 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
477 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700478 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800479 header_->e_version);
480 return false;
481 }
482 if (0 != header_->e_entry) {
483 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
484 0,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700485 file->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700486 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800487 return false;
488 }
489 if (0 == header_->e_phoff) {
490 *error_msg = StringPrintf("Failed to find non-zero e_phoff 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_shoff) {
495 *error_msg = StringPrintf("Failed to find non-zero e_shoff 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_ehsize) {
500 *error_msg = StringPrintf("Failed to find non-zero e_ehsize 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_phentsize) {
505 *error_msg = StringPrintf("Failed to find non-zero e_phentsize 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_phnum) {
510 *error_msg = StringPrintf("Failed to find non-zero e_phnum 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_shentsize) {
515 *error_msg = StringPrintf("Failed to find non-zero e_shentsize 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_shnum) {
520 *error_msg = StringPrintf("Failed to find non-zero e_shnum 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 (0 == header_->e_shstrndx) {
525 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700526 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800527 return false;
528 }
529 if (header_->e_shstrndx >= header_->e_shnum) {
530 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
531 header_->e_shstrndx,
532 header_->e_shnum,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700533 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800534 return false;
535 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800536
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800537 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800538 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700539 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
540 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800541 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700542 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800543 return false;
544 }
545 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700546 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
547 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800548 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700549 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800550 return false;
551 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800552 }
553 return true;
554}
555
David Srbecky533c2072015-04-22 12:20:22 +0100556template <typename ElfTypes>
557typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700558 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800559 return *header_;
560}
561
David Srbecky533c2072015-04-22 12:20:22 +0100562template <typename ElfTypes>
563uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700564 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
565 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800566 return program_headers_start_;
567}
568
David Srbecky533c2072015-04-22 12:20:22 +0100569template <typename ElfTypes>
570uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700571 CHECK(!program_header_only_); // Only used in "full" mode.
572 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800573 return section_headers_start_;
574}
575
David Srbecky533c2072015-04-22 12:20:22 +0100576template <typename ElfTypes>
577typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700578 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800579 return *dynamic_program_header_;
580}
581
David Srbecky533c2072015-04-22 12:20:22 +0100582template <typename ElfTypes>
583typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700584 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800585 return dynamic_section_start_;
586}
587
David Srbecky533c2072015-04-22 12:20:22 +0100588template <typename ElfTypes>
589typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
590 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700591 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800592 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000593 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700594 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800595 break;
596 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000597 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700598 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800599 break;
600 }
601 default: {
602 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700603 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800604 }
605 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800606}
607
David Srbecky533c2072015-04-22 12:20:22 +0100608template <typename ElfTypes>
609const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
610 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700611 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800612 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000613 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700614 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800615 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000616 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700617 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800618 }
619 default: {
620 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700621 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800622 }
623 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800624}
625
David Srbecky533c2072015-04-22 12:20:22 +0100626template <typename ElfTypes>
627const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
628 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700629 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800630 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700631 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800632 }
633 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700634 if (string_section_start == nullptr) {
635 return nullptr;
636 }
637 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800638}
639
Andreas Gampedaab38c2014-09-12 18:38:24 -0700640// WARNING: The following methods do not check for an error condition (non-existent hash section).
641// It is the caller's job to do this.
642
David Srbecky533c2072015-04-22 12:20:22 +0100643template <typename ElfTypes>
644typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800645 return hash_section_start_;
646}
647
David Srbecky533c2072015-04-22 12:20:22 +0100648template <typename ElfTypes>
649typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800650 return GetHashSectionStart()[0];
651}
652
David Srbecky533c2072015-04-22 12:20:22 +0100653template <typename ElfTypes>
654typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800655 return GetHashSectionStart()[1];
656}
657
David Srbecky533c2072015-04-22 12:20:22 +0100658template <typename ElfTypes>
659typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700660 if (i >= GetHashBucketNum()) {
661 *ok = false;
662 return 0;
663 }
664 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800665 // 0 is nbucket, 1 is nchain
666 return GetHashSectionStart()[2 + i];
667}
668
David Srbecky533c2072015-04-22 12:20:22 +0100669template <typename ElfTypes>
670typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600671 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700672 *ok = false;
673 return 0;
674 }
675 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800676 // 0 is nbucket, 1 is nchain, & chains are after buckets
677 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
678}
679
David Srbecky533c2072015-04-22 12:20:22 +0100680template <typename ElfTypes>
681typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800682 return GetHeader().e_phnum;
683}
684
David Srbecky533c2072015-04-22 12:20:22 +0100685template <typename ElfTypes>
686typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700687 CHECK_LT(i, GetProgramHeaderNum()) << file_path_; // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700688 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700689 if (program_header >= End()) {
690 return nullptr; // Failure condition.
691 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700692 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800693}
694
David Srbecky533c2072015-04-22 12:20:22 +0100695template <typename ElfTypes>
696typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700697 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
698 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700699 if (program_header->p_type == type) {
700 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800701 }
702 }
Alex Light3470ab42014-06-18 10:35:45 -0700703 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800704}
705
David Srbecky533c2072015-04-22 12:20:22 +0100706template <typename ElfTypes>
707typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800708 return GetHeader().e_shnum;
709}
710
David Srbecky533c2072015-04-22 12:20:22 +0100711template <typename ElfTypes>
712typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800713 // Can only access arbitrary sections when we have the whole file, not just program header.
714 // Even if we Load(), it doesn't bring in all the sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700715 CHECK(!program_header_only_) << file_path_;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700716 if (i >= GetSectionHeaderNum()) {
717 return nullptr; // Failure condition.
718 }
Ian Rogers13735952014-10-08 12:43:28 -0700719 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700720 if (section_header >= End()) {
721 return nullptr; // Failure condition.
722 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700723 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800724}
725
David Srbecky533c2072015-04-22 12:20:22 +0100726template <typename ElfTypes>
727typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800728 // Can only access arbitrary sections when we have the whole file, not just program header.
729 // We could change this to switch on known types if they were detected during loading.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700730 CHECK(!program_header_only_) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700731 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
732 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700733 if (section_header->sh_type == type) {
734 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800735 }
736 }
Alex Light3470ab42014-06-18 10:35:45 -0700737 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800738}
739
740// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800741static unsigned elfhash(const char *_name) {
742 const unsigned char *name = (const unsigned char *) _name;
743 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800744
Brian Carlstromdf629502013-07-17 22:39:56 -0700745 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800746 h = (h << 4) + *name++;
747 g = h & 0xf0000000;
748 h ^= g;
749 h ^= g >> 24;
750 }
751 return h;
752}
753
David Srbecky533c2072015-04-22 12:20:22 +0100754template <typename ElfTypes>
755typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800756 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800757}
758
David Srbecky533c2072015-04-22 12:20:22 +0100759template <typename ElfTypes>
760const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
761 const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700762 // Check that we have a hash section.
763 if (GetHashSectionStart() == nullptr) {
764 return nullptr; // Failure condition.
765 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700766 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700767 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700768 // TODO: we need to change this to calculate base_address_ in ::Open,
769 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700770 return base_address_ + sym->st_value;
771 } else {
772 return nullptr;
773 }
774}
775
Andreas Gampedaab38c2014-09-12 18:38:24 -0700776// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
David Srbecky533c2072015-04-22 12:20:22 +0100777template <typename ElfTypes>
778const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
779 const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700780 if (GetHashBucketNum() == 0) {
781 // No dynamic symbols at all.
782 return nullptr;
783 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700784 Elf_Word hash = elfhash(symbol_name.c_str());
785 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700786 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700787 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700788 if (!ok) {
789 return nullptr;
790 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800791 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700792 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700793 if (symbol == nullptr) {
794 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800795 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700796 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
797 if (symbol_name == name) {
798 return symbol;
799 }
800 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
801 if (!ok) {
802 return nullptr;
803 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800804 }
Alex Light3470ab42014-06-18 10:35:45 -0700805 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800806}
807
David Srbecky533c2072015-04-22 12:20:22 +0100808template <typename ElfTypes>
809bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700810 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
811}
812
David Srbecky533c2072015-04-22 12:20:22 +0100813template <typename ElfTypes>
814typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800815 CHECK(IsSymbolSectionType(section_header.sh_type))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700816 << file_path_ << " " << section_header.sh_type;
817 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800818 return section_header.sh_size / section_header.sh_entsize;
819}
820
David Srbecky533c2072015-04-22 12:20:22 +0100821template <typename ElfTypes>
822typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700823 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700824 if (sym_start == nullptr) {
825 return nullptr;
826 }
827 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800828}
829
David Srbecky533c2072015-04-22 12:20:22 +0100830template <typename ElfTypes>
831typename ElfFileImpl<ElfTypes>::SymbolTable**
832ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700833 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800834 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000835 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800836 return &symtab_symbol_table_;
837 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000838 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800839 return &dynsym_symbol_table_;
840 }
841 default: {
842 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700843 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800844 }
845 }
846}
847
David Srbecky533c2072015-04-22 12:20:22 +0100848template <typename ElfTypes>
849typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
850 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700851 CHECK(!program_header_only_) << file_path_;
852 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800853
854 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700855 if (*symbol_table != nullptr || build_map) {
856 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800857 DCHECK(build_map);
858 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -0700859 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700860 if (symbol_section == nullptr) {
861 return nullptr; // Failure condition.
862 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700863 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700864 if (string_section == nullptr) {
865 return nullptr; // Failure condition.
866 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800867 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700868 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700869 if (symbol == nullptr) {
870 return nullptr; // Failure condition.
871 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700872 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
873 ? ELF64_ST_TYPE(symbol->st_info)
874 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000875 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800876 continue;
877 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700878 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700879 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800880 continue;
881 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700882 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -0700883 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800884 if (!result.second) {
885 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700886 if ((symbol->st_value != result.first->second->st_value) ||
887 (symbol->st_size != result.first->second->st_size) ||
888 (symbol->st_info != result.first->second->st_info) ||
889 (symbol->st_other != result.first->second->st_other) ||
890 (symbol->st_shndx != result.first->second->st_shndx)) {
891 return nullptr; // Failure condition.
892 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800893 }
894 }
895 }
Alex Light3470ab42014-06-18 10:35:45 -0700896 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -0700897 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800898 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700899 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800900 }
901 return it->second;
902 }
903
904 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -0700905 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700906 if (symbol_section == nullptr) {
907 return nullptr;
908 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700909 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700910 if (string_section == nullptr) {
911 return nullptr;
912 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800913 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700914 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700915 if (symbol == nullptr) {
916 return nullptr; // Failure condition.
917 }
918 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700919 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800920 continue;
921 }
922 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700923 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800924 }
925 }
Alex Light3470ab42014-06-18 10:35:45 -0700926 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800927}
928
David Srbecky533c2072015-04-22 12:20:22 +0100929template <typename ElfTypes>
930typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
931 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700932 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -0700933 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800934 return 0;
935 }
936 return symbol->st_value;
937}
938
David Srbecky533c2072015-04-22 12:20:22 +0100939template <typename ElfTypes>
940const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
941 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700942 CHECK(!program_header_only_) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800943 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -0700944 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700945 return nullptr; // Failure condition.
946 }
947 if (i >= string_section.sh_size) {
948 return nullptr;
949 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800950 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700951 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800952 }
Ian Rogers13735952014-10-08 12:43:28 -0700953 uint8_t* strings = Begin() + string_section.sh_offset;
954 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700955 if (string >= End()) {
956 return nullptr;
957 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800958 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800959}
960
David Srbecky533c2072015-04-22 12:20:22 +0100961template <typename ElfTypes>
962typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700963 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800964}
965
David Srbecky533c2072015-04-22 12:20:22 +0100966template <typename ElfTypes>
967typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700968 CHECK_LT(i, GetDynamicNum()) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 return *(GetDynamicSectionStart() + i);
970}
971
David Srbecky533c2072015-04-22 12:20:22 +0100972template <typename ElfTypes>
973typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700974 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
975 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -0700976 if (dyn->d_tag == type) {
977 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800978 }
979 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700980 return nullptr;
Alex Light53cb16b2014-06-12 11:26:29 -0700981}
982
David Srbecky533c2072015-04-22 12:20:22 +0100983template <typename ElfTypes>
984typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700985 Elf_Dyn* dyn = FindDynamicByType(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700986 if (dyn == nullptr) {
Alex Light53cb16b2014-06-12 11:26:29 -0700987 return 0;
988 } else {
989 return dyn->d_un.d_val;
990 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800991}
992
David Srbecky533c2072015-04-22 12:20:22 +0100993template <typename ElfTypes>
994typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700995 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -0700996 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800997}
998
David Srbecky533c2072015-04-22 12:20:22 +0100999template <typename ElfTypes>
1000typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001001 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1002 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001003 return section_header.sh_size / section_header.sh_entsize;
1004}
1005
David Srbecky533c2072015-04-22 12:20:22 +01001006template <typename ElfTypes>
1007typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001008 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1009 CHECK_LT(i, GetRelNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001010 return *(GetRelSectionStart(section_header) + i);
1011}
1012
David Srbecky533c2072015-04-22 12:20:22 +01001013template <typename ElfTypes>
1014typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001015 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001016 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001017}
1018
David Srbecky533c2072015-04-22 12:20:22 +01001019template <typename ElfTypes>
1020typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001021 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001022 return section_header.sh_size / section_header.sh_entsize;
1023}
1024
David Srbecky533c2072015-04-22 12:20:22 +01001025template <typename ElfTypes>
1026typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001027 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1028 CHECK_LT(i, GetRelaNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001029 return *(GetRelaSectionStart(section_header) + i);
1030}
1031
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001032// Base on bionic phdr_table_get_load_size
David Srbecky533c2072015-04-22 12:20:22 +01001033template <typename ElfTypes>
Vladimir Marko3fc99032015-05-13 19:06:30 +01001034bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
1035 Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1);
1036 Elf_Addr max_vaddr = 0u;
Tong Shen62d1ca32014-09-03 17:24:56 -07001037 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1038 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001039 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001040 continue;
1041 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001042 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001043 if (begin_vaddr < min_vaddr) {
1044 min_vaddr = begin_vaddr;
1045 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001046 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001047 if (UNLIKELY(begin_vaddr > end_vaddr)) {
1048 std::ostringstream oss;
1049 oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex
1050 << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001051 << " in ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001052 *error_msg = oss.str();
1053 *size = static_cast<size_t>(-1);
1054 return false;
1055 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001056 if (end_vaddr > max_vaddr) {
1057 max_vaddr = end_vaddr;
1058 }
1059 }
1060 min_vaddr = RoundDown(min_vaddr, kPageSize);
1061 max_vaddr = RoundUp(max_vaddr, kPageSize);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001062 CHECK_LT(min_vaddr, max_vaddr) << file_path_;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001063 Elf_Addr loaded_size = max_vaddr - min_vaddr;
1064 // Check that the loaded_size fits in size_t.
1065 if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) {
1066 std::ostringstream oss;
1067 oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x"
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001068 << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001069 *error_msg = oss.str();
1070 *size = static_cast<size_t>(-1);
1071 return false;
1072 }
1073 *size = loaded_size;
1074 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001075}
1076
David Srbecky533c2072015-04-22 12:20:22 +01001077template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001078bool ElfFileImpl<ElfTypes>::Load(File* file,
1079 bool executable,
1080 bool low_4gb,
1081 std::string* error_msg) {
1082 CHECK(program_header_only_) << file->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001083
1084 if (executable) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001085 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001086 if (elf_ISA != kRuntimeISA) {
1087 std::ostringstream oss;
1088 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1089 *error_msg = oss.str();
1090 return false;
1091 }
1092 }
1093
Jim_Guoa62a5882014-04-28 11:11:57 +08001094 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001095 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1096 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001097 if (program_header == nullptr) {
1098 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001099 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001100 return false;
1101 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001102
1103 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001104 if (program_header->p_type == PT_DYNAMIC) {
1105 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001106 continue;
1107 }
1108
1109 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001110 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001111 continue;
1112 }
1113
1114 // Found something to load.
1115
Jim_Guoa62a5882014-04-28 11:11:57 +08001116 // Before load the actual segments, reserve a contiguous chunk
1117 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001118 // permissions. We'll then carve that up with the proper
1119 // permissions as we load the actual segments. If p_vaddr is
1120 // non-zero, the segments require the specific address specified,
1121 // which either was specified in the file because we already set
1122 // base_address_ after the first zero segment).
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001123 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -08001124 if (temp_file_length < 0) {
1125 errno = -temp_file_length;
1126 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001127 file->GetPath().c_str(), file->Fd(), strerror(errno));
Ian Rogerscdfcf372014-01-23 20:38:36 -08001128 return false;
1129 }
1130 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001131 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001132 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1133 uint8_t* reserve_base_override = reserve_base;
1134 // Override the base (e.g. when compiling with --compile-pic)
1135 if (requested_base_ != nullptr) {
1136 reserve_base_override = requested_base_;
1137 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001138 std::string reservation_name("ElfFile reservation for ");
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001139 reservation_name += file->GetPath();
Vladimir Marko3fc99032015-05-13 19:06:30 +01001140 size_t loaded_size;
1141 if (!GetLoadedSize(&loaded_size, error_msg)) {
1142 DCHECK(!error_msg->empty());
1143 return false;
1144 }
Ian Rogers700a4022014-05-19 16:49:03 -07001145 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001146 reserve_base_override,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001147 loaded_size,
1148 PROT_NONE,
1149 low_4gb,
1150 false,
Jim_Guoa62a5882014-04-28 11:11:57 +08001151 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001152 if (reserve.get() == nullptr) {
1153 *error_msg = StringPrintf("Failed to allocate %s: %s",
1154 reservation_name.c_str(), error_msg->c_str());
1155 return false;
1156 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001157 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001158
1159 // Base address is the difference of actual mapped location and the p_vaddr
1160 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1161 - reinterpret_cast<uintptr_t>(reserve_base));
1162 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1163 // dynamic memory address of where that object is actually mapped
1164 //
1165 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1166 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001167 segments_.push_back(reserve.release());
1168 }
1169 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001170 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001171 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001172 }
Ian Rogers13735952014-10-08 12:43:28 -07001173 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001174 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001175 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001176 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001177 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001178 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001179 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001180 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001181 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001182 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001183 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001184 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001185 if (writable_) {
1186 prot |= PROT_WRITE;
1187 flags |= MAP_SHARED;
1188 } else {
1189 flags |= MAP_PRIVATE;
1190 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001191 if (program_header->p_filesz > program_header->p_memsz) {
1192 *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1193 static_cast<uint64_t>(program_header->p_filesz),
1194 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001195 file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001196 return false;
1197 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001198 if (program_header->p_filesz < program_header->p_memsz &&
1199 !IsAligned<kPageSize>(program_header->p_filesz)) {
1200 *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1201 " < %" PRIu64 "): %s",
1202 static_cast<uint64_t>(program_header->p_filesz),
1203 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001204 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001205 return false;
1206 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001207 if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1208 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1209 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1210 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001211 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001212 return false;
1213 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001214 if (program_header->p_filesz != 0u) {
1215 std::unique_ptr<MemMap> segment(
1216 MemMap::MapFileAtAddress(p_vaddr,
1217 program_header->p_filesz,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001218 prot,
1219 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001220 file->Fd(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001221 program_header->p_offset,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001222 /*low4_gb*/false,
1223 /*reuse*/true, // implies MAP_FIXED
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001224 file->GetPath().c_str(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001225 error_msg));
1226 if (segment.get() == nullptr) {
1227 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001228 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001229 return false;
1230 }
1231 if (segment->Begin() != p_vaddr) {
1232 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1233 "instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001234 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001235 return false;
1236 }
1237 segments_.push_back(segment.release());
1238 }
1239 if (program_header->p_filesz < program_header->p_memsz) {
1240 std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001241 static_cast<uint64_t>(i), file->GetPath().c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001242 std::unique_ptr<MemMap> segment(
1243 MemMap::MapAnonymous(name.c_str(),
1244 p_vaddr + program_header->p_filesz,
1245 program_header->p_memsz - program_header->p_filesz,
1246 prot, false, true /* reuse */, error_msg));
1247 if (segment == nullptr) {
1248 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001249 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001250 return false;
1251 }
1252 if (segment->Begin() != p_vaddr) {
1253 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1254 "at expected address %p, instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001255 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001256 return false;
1257 }
1258 segments_.push_back(segment.release());
1259 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001260 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001261
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001262 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001263 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001264 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1265 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001266 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001267 return false;
1268 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001269 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001270
Tong Shen62d1ca32014-09-03 17:24:56 -07001271 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1272 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001273 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001274 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001275 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001276 if (!ValidPointer(d_ptr)) {
1277 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001278 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001279 return false;
1280 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001281 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001282 break;
1283 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001284 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001285 if (!ValidPointer(d_ptr)) {
1286 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001287 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001288 return false;
1289 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001290 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1291 break;
1292 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001293 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001294 if (!ValidPointer(d_ptr)) {
1295 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001296 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001297 return false;
1298 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001299 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001300 break;
1301 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001302 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001303 if (GetDynamicNum() != i+1) {
1304 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1305 "expected %d as implied by size of PT_DYNAMIC segment in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001306 i + 1, GetDynamicNum(), file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001307 return false;
1308 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001309 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001310 }
1311 }
1312 }
1313
Andreas Gampedaab38c2014-09-12 18:38:24 -07001314 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001315 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001316 return false;
1317 }
1318
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001319 return true;
1320}
1321
David Srbecky533c2072015-04-22 12:20:22 +01001322template <typename ElfTypes>
1323bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001324 for (size_t i = 0; i < segments_.size(); ++i) {
1325 const MemMap* segment = segments_[i];
1326 if (segment->Begin() <= start && start < segment->End()) {
1327 return true;
1328 }
1329 }
1330 return false;
1331}
1332
Alex Light3470ab42014-06-18 10:35:45 -07001333
David Srbecky533c2072015-04-22 12:20:22 +01001334template <typename ElfTypes>
1335typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1336 const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001337 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001338 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001339 if (shstrtab_sec == nullptr) {
1340 return nullptr;
1341 }
Alex Light3470ab42014-06-18 10:35:45 -07001342 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001343 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001344 if (shdr == nullptr) {
1345 return nullptr;
1346 }
1347 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001348 if (sec_name == nullptr) {
1349 continue;
1350 }
1351 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001352 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001353 }
1354 }
1355 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001356}
1357
David Srbecky533c2072015-04-22 12:20:22 +01001358template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001359bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001360 if (base_address_delta == 0) {
1361 return true;
1362 }
David Srbeckyf8980872015-05-22 17:04:47 +01001363 return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1364 ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1365 ApplyOatPatchesTo(".debug_line", base_address_delta);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001366}
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001367
David Srbecky533c2072015-04-22 12:20:22 +01001368template <typename ElfTypes>
1369bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
David Srbeckyf8980872015-05-22 17:04:47 +01001370 const char* target_section_name, Elf_Addr delta) {
1371 auto target_section = FindSectionByName(target_section_name);
1372 if (target_section == nullptr) {
1373 return true;
1374 }
1375 std::string patches_name = target_section_name + std::string(".oat_patches");
1376 auto patches_section = FindSectionByName(patches_name.c_str());
David Srbecky2f6cdb02015-04-11 00:17:53 +01001377 if (patches_section == nullptr) {
David Srbeckyf8980872015-05-22 17:04:47 +01001378 LOG(ERROR) << patches_name << " section not found.";
Alex Light3470ab42014-06-18 10:35:45 -07001379 return false;
1380 }
David Srbecky2f6cdb02015-04-11 00:17:53 +01001381 if (patches_section->sh_type != SHT_OAT_PATCH) {
David Srbeckyf8980872015-05-22 17:04:47 +01001382 LOG(ERROR) << "Unexpected type of " << patches_name;
Alex Light3470ab42014-06-18 10:35:45 -07001383 return false;
1384 }
David Srbeckyf8980872015-05-22 17:04:47 +01001385 ApplyOatPatches(
David Srbecky2f6cdb02015-04-11 00:17:53 +01001386 Begin() + patches_section->sh_offset,
1387 Begin() + patches_section->sh_offset + patches_section->sh_size,
David Srbeckyf8980872015-05-22 17:04:47 +01001388 delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +01001389 Begin() + target_section->sh_offset,
David Srbeckyf8980872015-05-22 17:04:47 +01001390 Begin() + target_section->sh_offset + target_section->sh_size);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001391 return true;
1392}
1393
David Srbeckyf8980872015-05-22 17:04:47 +01001394// Apply LEB128 encoded patches to given section.
David Srbecky533c2072015-04-22 12:20:22 +01001395template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001396void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1397 const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky533c2072015-04-22 12:20:22 +01001398 uint8_t* to_patch, const uint8_t* to_patch_end) {
David Srbeckyf8980872015-05-22 17:04:47 +01001399 typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress;
1400 while (patches < patches_end) {
1401 to_patch += DecodeUnsignedLeb128(&patches);
1402 DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1403 DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1404 *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
David Srbecky2f6cdb02015-04-11 00:17:53 +01001405 }
Alex Light3470ab42014-06-18 10:35:45 -07001406}
Mark Mendellae9fd932014-02-10 16:14:35 -08001407
David Srbecky533c2072015-04-22 12:20:22 +01001408template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001409bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001410 // ELF files produced by MCLinker look roughly like this
1411 //
1412 // +------------+
1413 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
1414 // +------------+
1415 // | Elf_Phdr | program headers
1416 // | Elf_Phdr |
1417 // | ... |
1418 // | Elf_Phdr |
1419 // +------------+
1420 // | section | mixture of needed and unneeded sections
1421 // +------------+
1422 // | section |
1423 // +------------+
1424 // | ... |
1425 // +------------+
1426 // | section |
1427 // +------------+
1428 // | Elf_Shdr | section headers
1429 // | Elf_Shdr |
1430 // | ... | contains offset to section start
1431 // | Elf_Shdr |
1432 // +------------+
1433 //
1434 // To strip:
1435 // - leave the Elf_Ehdr and Elf_Phdr values in place.
1436 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1437 // - move the sections are keeping up to fill in gaps of sections we want to strip
1438 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1439 // - truncate rest of file
1440 //
1441
1442 std::vector<Elf_Shdr> section_headers;
1443 std::vector<Elf_Word> section_headers_original_indexes;
1444 section_headers.reserve(GetSectionHeaderNum());
1445
1446
1447 Elf_Shdr* string_section = GetSectionNameStringSection();
1448 CHECK(string_section != nullptr);
1449 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1450 Elf_Shdr* sh = GetSectionHeader(i);
1451 CHECK(sh != nullptr);
1452 const char* name = GetString(*string_section, sh->sh_name);
1453 if (name == nullptr) {
1454 CHECK_EQ(0U, i);
1455 section_headers.push_back(*sh);
1456 section_headers_original_indexes.push_back(0);
1457 continue;
1458 }
Andreas Gampe9186ced2016-12-12 14:28:21 -08001459 if (android::base::StartsWith(name, ".debug")
Tong Shen62d1ca32014-09-03 17:24:56 -07001460 || (strcmp(name, ".strtab") == 0)
1461 || (strcmp(name, ".symtab") == 0)) {
1462 continue;
1463 }
1464 section_headers.push_back(*sh);
1465 section_headers_original_indexes.push_back(i);
1466 }
1467 CHECK_NE(0U, section_headers.size());
1468 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1469
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001470 // section 0 is the null section, sections start at offset of first section
Tong Shen62d1ca32014-09-03 17:24:56 -07001471 CHECK(GetSectionHeader(1) != nullptr);
1472 Elf_Off offset = GetSectionHeader(1)->sh_offset;
1473 for (size_t i = 1; i < section_headers.size(); i++) {
1474 Elf_Shdr& new_sh = section_headers[i];
1475 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1476 CHECK(old_sh != nullptr);
1477 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1478 if (old_sh->sh_addralign > 1) {
1479 offset = RoundUp(offset, old_sh->sh_addralign);
1480 }
1481 if (old_sh->sh_offset == offset) {
1482 // already in place
1483 offset += old_sh->sh_size;
1484 continue;
1485 }
1486 // shift section earlier
1487 memmove(Begin() + offset,
1488 Begin() + old_sh->sh_offset,
1489 old_sh->sh_size);
1490 new_sh.sh_offset = offset;
1491 offset += old_sh->sh_size;
1492 }
1493
1494 Elf_Off shoff = offset;
1495 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1496 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1497 offset += section_headers_size_in_bytes;
1498
1499 GetHeader().e_shnum = section_headers.size();
1500 GetHeader().e_shoff = shoff;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001501 int result = ftruncate(file->Fd(), offset);
Tong Shen62d1ca32014-09-03 17:24:56 -07001502 if (result != 0) {
1503 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001504 file->GetPath().c_str(), strerror(errno));
Tong Shen62d1ca32014-09-03 17:24:56 -07001505 return false;
1506 }
1507 return true;
1508}
1509
1510static const bool DEBUG_FIXUP = false;
1511
David Srbecky533c2072015-04-22 12:20:22 +01001512template <typename ElfTypes>
1513bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001514 if (!FixupDynamic(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001515 LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001516 return false;
1517 }
1518 if (!FixupSectionHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001519 LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001520 return false;
1521 }
1522 if (!FixupProgramHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001523 LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001524 return false;
1525 }
1526 if (!FixupSymbols(base_address, true)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001527 LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001528 return false;
1529 }
1530 if (!FixupSymbols(base_address, false)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001531 LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001532 return false;
1533 }
1534 if (!FixupRelocations(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001535 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001536 return false;
1537 }
Andreas Gampe3c54b002015-04-07 16:09:30 -07001538 static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1539 if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001540 LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001541 return false;
1542 }
1543 return true;
1544}
1545
David Srbecky533c2072015-04-22 12:20:22 +01001546template <typename ElfTypes>
1547bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001548 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1549 Elf_Dyn& elf_dyn = GetDynamic(i);
1550 Elf_Word d_tag = elf_dyn.d_tag;
1551 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1552 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1553 if (DEBUG_FIXUP) {
1554 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001555 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001556 static_cast<uint64_t>(d_ptr),
1557 static_cast<uint64_t>(d_ptr + base_address));
1558 }
1559 d_ptr += base_address;
1560 elf_dyn.d_un.d_ptr = d_ptr;
1561 }
1562 }
1563 return true;
1564}
1565
David Srbecky533c2072015-04-22 12:20:22 +01001566template <typename ElfTypes>
1567bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001568 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1569 Elf_Shdr* sh = GetSectionHeader(i);
1570 CHECK(sh != nullptr);
1571 // 0 implies that the section will not exist in the memory of the process
1572 if (sh->sh_addr == 0) {
1573 continue;
1574 }
1575 if (DEBUG_FIXUP) {
1576 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001577 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001578 static_cast<uint64_t>(sh->sh_addr),
1579 static_cast<uint64_t>(sh->sh_addr + base_address));
1580 }
1581 sh->sh_addr += base_address;
1582 }
1583 return true;
1584}
1585
David Srbecky533c2072015-04-22 12:20:22 +01001586template <typename ElfTypes>
1587bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001588 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1589 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1590 Elf_Phdr* ph = GetProgramHeader(i);
1591 CHECK(ph != nullptr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001592 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001593 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001594 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001595 if (DEBUG_FIXUP) {
1596 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001597 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001598 static_cast<uint64_t>(ph->p_vaddr),
1599 static_cast<uint64_t>(ph->p_vaddr + base_address));
1600 }
1601 ph->p_vaddr += base_address;
1602 ph->p_paddr += base_address;
1603 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001604 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001605 }
1606 return true;
1607}
1608
David Srbecky533c2072015-04-22 12:20:22 +01001609template <typename ElfTypes>
1610bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001611 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1612 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1613 Elf_Shdr* symbol_section = FindSectionByType(section_type);
1614 if (symbol_section == nullptr) {
1615 // file is missing optional .symtab
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001616 CHECK(!dynamic) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001617 return true;
1618 }
1619 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1620 Elf_Sym* symbol = GetSymbol(section_type, i);
1621 CHECK(symbol != nullptr);
1622 if (symbol->st_value != 0) {
1623 if (DEBUG_FIXUP) {
1624 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001625 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001626 static_cast<uint64_t>(symbol->st_value),
1627 static_cast<uint64_t>(symbol->st_value + base_address));
1628 }
1629 symbol->st_value += base_address;
1630 }
1631 }
1632 return true;
1633}
1634
David Srbecky533c2072015-04-22 12:20:22 +01001635template <typename ElfTypes>
1636bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001637 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1638 Elf_Shdr* sh = GetSectionHeader(i);
1639 CHECK(sh != nullptr);
1640 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001641 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1642 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001643 if (DEBUG_FIXUP) {
1644 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001645 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001646 static_cast<uint64_t>(rel.r_offset),
1647 static_cast<uint64_t>(rel.r_offset + base_address));
1648 }
1649 rel.r_offset += base_address;
1650 }
1651 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001652 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1653 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001654 if (DEBUG_FIXUP) {
1655 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001656 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001657 static_cast<uint64_t>(rela.r_offset),
1658 static_cast<uint64_t>(rela.r_offset + base_address));
1659 }
1660 rela.r_offset += base_address;
1661 }
1662 }
1663 }
1664 return true;
1665}
1666
1667// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +01001668template class ElfFileImpl<ElfTypes32>;
1669template class ElfFileImpl<ElfTypes64>;
Tong Shen62d1ca32014-09-03 17:24:56 -07001670
Ian Rogersd4c4d952014-10-16 20:31:53 -07001671ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001672}
1673
Ian Rogersd4c4d952014-10-16 20:31:53 -07001674ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001675}
1676
1677ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001678 // Should never have 32 and 64-bit impls.
1679 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001680}
1681
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001682ElfFile* ElfFile::Open(File* file,
1683 bool writable,
1684 bool program_header_only,
1685 bool low_4gb,
1686 std::string* error_msg,
Igor Murashkin46774762014-10-22 11:37:02 -07001687 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001688 if (file->GetLength() < EI_NIDENT) {
1689 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1690 file->GetPath().c_str());
1691 return nullptr;
1692 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001693 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1694 PROT_READ,
1695 MAP_PRIVATE,
1696 file->Fd(),
1697 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001698 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001699 file->GetPath().c_str(),
1700 error_msg));
Steven Morelande16c3bf2017-05-24 09:12:32 -07001701 if (map == nullptr || map->Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001702 return nullptr;
1703 }
Ian Rogers13735952014-10-08 12:43:28 -07001704 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001705 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001706 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1707 writable,
1708 program_header_only,
1709 low_4gb,
1710 error_msg,
1711 requested_base);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001712 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001713 return nullptr;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001714 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001715 return new ElfFile(elf_file_impl);
1716 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001717 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1718 writable,
1719 program_header_only,
1720 low_4gb,
1721 error_msg,
1722 requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001723 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001724 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001725 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001726 return new ElfFile(elf_file_impl);
1727 } else {
1728 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1729 ELFCLASS32, ELFCLASS64,
1730 file->GetPath().c_str(),
1731 header[EI_CLASS]);
1732 return nullptr;
1733 }
1734}
1735
1736ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001737 // low_4gb support not required for this path.
1738 constexpr bool low_4gb = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001739 if (file->GetLength() < EI_NIDENT) {
1740 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1741 file->GetPath().c_str());
1742 return nullptr;
1743 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001744 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1745 PROT_READ,
1746 MAP_PRIVATE,
1747 file->Fd(),
1748 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001749 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001750 file->GetPath().c_str(),
1751 error_msg));
Steven Morelande16c3bf2017-05-24 09:12:32 -07001752 if (map == nullptr || map->Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001753 return nullptr;
1754 }
Ian Rogers13735952014-10-08 12:43:28 -07001755 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001756 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001757 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1758 mmap_prot,
1759 mmap_flags,
1760 low_4gb,
1761 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001762 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001763 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001764 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001765 return new ElfFile(elf_file_impl);
1766 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001767 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1768 mmap_prot,
1769 mmap_flags,
1770 low_4gb,
1771 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001772 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001773 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001774 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001775 return new ElfFile(elf_file_impl);
1776 } else {
1777 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1778 ELFCLASS32, ELFCLASS64,
1779 file->GetPath().c_str(),
1780 header[EI_CLASS]);
1781 return nullptr;
1782 }
1783}
1784
1785#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001786 if (elf64_.get() != nullptr) { \
1787 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001788 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001789 DCHECK(elf32_.get() != nullptr); \
1790 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001791 }
1792
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001793bool ElfFile::Load(File* file, bool executable, bool low_4gb, std::string* error_msg) {
1794 DELEGATE_TO_IMPL(Load, file, executable, low_4gb, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001795}
1796
Ian Rogers13735952014-10-08 12:43:28 -07001797const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001798 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1799}
1800
1801size_t ElfFile::Size() const {
1802 DELEGATE_TO_IMPL(Size);
1803}
1804
Ian Rogers13735952014-10-08 12:43:28 -07001805uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001806 DELEGATE_TO_IMPL(Begin);
1807}
1808
Ian Rogers13735952014-10-08 12:43:28 -07001809uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001810 DELEGATE_TO_IMPL(End);
1811}
1812
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001813const std::string& ElfFile::GetFilePath() const {
1814 DELEGATE_TO_IMPL(GetFilePath);
Tong Shen62d1ca32014-09-03 17:24:56 -07001815}
1816
Alex Light0eb76d22015-08-11 18:03:47 -07001817bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1818 uint64_t* size) const {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001819 if (elf32_.get() == nullptr) {
1820 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001821
Ian Rogersd4c4d952014-10-16 20:31:53 -07001822 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1823 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001824 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001825 }
1826 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001827 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001828 }
1829 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001830 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001831 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001832 return true;
1833 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001834 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1835 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001836 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001837 }
1838 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001839 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001840 }
1841 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001842 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001843 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001844 return true;
1845 }
1846}
1847
Jeff Hao24ec0282016-03-17 21:32:45 -07001848bool ElfFile::HasSection(const std::string& name) const {
1849 if (elf64_.get() != nullptr) {
1850 return elf64_->FindSectionByName(name) != nullptr;
1851 } else {
1852 return elf32_->FindSectionByName(name) != nullptr;
1853 }
1854}
1855
Tong Shen62d1ca32014-09-03 17:24:56 -07001856uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1857 const std::string& symbol_name,
1858 bool build_map) {
1859 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1860}
1861
Vladimir Marko3fc99032015-05-13 19:06:30 +01001862bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1863 DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001864}
1865
1866bool ElfFile::Strip(File* file, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001867 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001868 if (elf_file.get() == nullptr) {
1869 return false;
1870 }
1871
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001872 if (elf_file->elf64_.get() != nullptr) {
1873 return elf_file->elf64_->Strip(file, error_msg);
1874 } else {
1875 return elf_file->elf32_->Strip(file, error_msg);
1876 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001877}
1878
Andreas Gampe3c54b002015-04-07 16:09:30 -07001879bool ElfFile::Fixup(uint64_t base_address) {
1880 if (elf64_.get() != nullptr) {
1881 return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1882 } else {
1883 DCHECK(elf32_.get() != nullptr);
1884 CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1885 return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1886 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001887 DELEGATE_TO_IMPL(Fixup, base_address);
1888}
1889
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001890} // namespace art