blob: 719b4af293919c650a5a89b8419fbd773785e48b [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"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070030#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/utils.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070032#include "elf_file_impl.h"
33#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034
35namespace art {
36
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037using android::base::StringPrintf;
38
David Srbecky533c2072015-04-22 12:20:22 +010039template <typename ElfTypes>
40ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable,
41 bool program_header_only,
42 uint8_t* requested_base)
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070043 : writable_(writable),
Brian Carlstromc1409452014-02-26 14:06:23 -080044 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -070045 header_(nullptr),
46 base_address_(nullptr),
47 program_headers_start_(nullptr),
48 section_headers_start_(nullptr),
49 dynamic_program_header_(nullptr),
50 dynamic_section_start_(nullptr),
51 symtab_section_start_(nullptr),
52 dynsym_section_start_(nullptr),
53 strtab_section_start_(nullptr),
54 dynstr_section_start_(nullptr),
55 hash_section_start_(nullptr),
56 symtab_symbol_table_(nullptr),
57 dynsym_symbol_table_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -070058 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -070059 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -080060}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061
David Srbecky533c2072015-04-22 12:20:22 +010062template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080063ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
64 bool writable,
65 bool program_header_only,
66 bool low_4gb,
67 std::string* error_msg,
68 uint8_t* requested_base) {
David Srbecky533c2072015-04-22 12:20:22 +010069 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
70 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071 int prot;
72 int flags;
Alex Light3470ab42014-06-18 10:35:45 -070073 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080074 prot = PROT_READ | PROT_WRITE;
75 flags = MAP_SHARED;
76 } else {
77 prot = PROT_READ;
78 flags = MAP_PRIVATE;
79 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070080 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070081 return nullptr;
82 }
83 return elf_file.release();
84}
85
David Srbecky533c2072015-04-22 12:20:22 +010086template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080087ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
88 int prot,
89 int flags,
90 bool low_4gb,
91 std::string* error_msg) {
David Srbecky533c2072015-04-22 12:20:22 +010092 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
93 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
94 /*requested_base*/nullptr));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070095 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070096 return nullptr;
97 }
98 return elf_file.release();
99}
100
David Srbecky533c2072015-04-22 12:20:22 +0100101template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700102bool ElfFileImpl<ElfTypes>::Setup(File* file,
103 int prot,
104 int flags,
105 bool low_4gb,
106 std::string* error_msg) {
107 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -0800108 if (temp_file_length < 0) {
109 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700110 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700111 file->GetPath().c_str(), file->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800112 return false;
113 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800114 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700115 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800116 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700117 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700118 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800119 return false;
120 }
121
Brian Carlstromc1409452014-02-26 14:06:23 -0800122 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700124 size_t elf_header_size = sizeof(Elf_Ehdr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700125 if (!SetMap(file,
126 MemMap::MapFile(elf_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800127 prot,
128 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700129 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800130 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800131 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700132 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800133 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800134 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800135 return false;
136 }
137 // then remap to cover program header
138 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700139 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800140 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 "header of %zd bytes: '%s'", file_length,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700142 sizeof(Elf_Ehdr), file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700143 return false;
144 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700145 if (!SetMap(file,
146 MemMap::MapFile(program_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800147 prot,
148 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700149 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800150 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800151 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700152 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800153 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800154 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700155 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 return false;
157 }
158 } else {
159 // otherwise map entire file
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700160 if (!SetMap(file,
161 MemMap::MapFile(file->GetLength(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800162 prot,
163 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700164 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800165 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800166 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700167 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800168 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800169 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700170 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171 return false;
172 }
173 }
174
Andreas Gampedaab38c2014-09-12 18:38:24 -0700175 if (program_header_only_) {
176 program_headers_start_ = Begin() + GetHeader().e_phoff;
177 } else {
178 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
179 return false;
180 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700183 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
184 return false;
185 }
186
187 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700188 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700189 if (shstrtab_section_header == nullptr) {
190 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700191 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700192 return false;
193 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194
195 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000196 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700197 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700198 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700199 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800200 return false;
201 }
202
Andreas Gampedaab38c2014-09-12 18:38:24 -0700203 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700204 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700205 return false;
206 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207
208 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700209 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
210 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700211 if (section_header == nullptr) {
212 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700213 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700214 return false;
215 }
216 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000217 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700218 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700219 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700220 return false;
221 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800222 break;
223 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000224 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700225 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700226 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700227 return false;
228 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800229 break;
230 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000231 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700233 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
234 // Check that this is named ".dynstr" and ignore otherwise.
235 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
236 if (strncmp(".dynstr", header_name, 8) == 0) {
237 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700238 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700239 return false;
240 }
241 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700243 // Check that this is named ".strtab" and ignore otherwise.
244 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
245 if (strncmp(".strtab", header_name, 8) == 0) {
246 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700247 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700248 return false;
249 }
250 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 }
252 break;
253 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000254 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700255 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700256 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700258 << file->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800259 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700260 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800261 return false;
262 }
263 break;
264 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000265 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700266 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700267 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700268 return false;
269 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800270 break;
271 }
272 }
273 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700274
275 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700276 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700277 return false;
278 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800279 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700280
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800281 return true;
282}
283
David Srbecky533c2072015-04-22 12:20:22 +0100284template <typename ElfTypes>
285ElfFileImpl<ElfTypes>::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800286 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800287 delete symtab_symbol_table_;
288 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800289}
290
David Srbecky533c2072015-04-22 12:20:22 +0100291template <typename ElfTypes>
292bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
293 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700294 if (Begin() + offset >= End()) {
295 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700296 file_path_.c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700297 return false;
298 }
299 *target = Begin() + offset;
300 return true;
301}
302
David Srbecky533c2072015-04-22 12:20:22 +0100303template <typename ElfTypes>
304bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
305 const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700306 // Only works in whole-program mode, as we need to iterate over the sections.
307 // Note that we normally can't search by type, as duplicates are allowed for most section types.
308 if (program_header_only_) {
309 return true;
310 }
311
Tong Shen62d1ca32014-09-03 17:24:56 -0700312 Elf_Shdr* source_section = nullptr;
313 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700314 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700315 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
316 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700317
318 if (Begin() + section_header->sh_offset == source) {
319 // Found the source.
320 source_section = section_header;
321 if (target_index) {
322 break;
323 }
324 } else if (Begin() + section_header->sh_offset == target) {
325 target_index = i;
326 target_found = true;
327 if (source_section != nullptr) {
328 break;
329 }
330 }
331 }
332
333 return target_found && source_section != nullptr && source_section->sh_link == target_index;
334}
335
David Srbecky533c2072015-04-22 12:20:22 +0100336template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700337 bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700338 if (!program_header_only_) {
339 // If in full mode, need section headers.
340 if (section_headers_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700341 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700342 return false;
343 }
344 }
345
346 // This is redundant, but defensive.
347 if (dynamic_program_header_ == nullptr) {
348 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700349 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700350 return false;
351 }
352
353 // Need a dynamic section. This is redundant, but defensive.
354 if (dynamic_section_start_ == nullptr) {
355 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700356 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700357 return false;
358 }
359
360 // Symtab validation. These is not really a hard failure, as we are currently not using the
361 // symtab internally, but it's nice to be defensive.
362 if (symtab_section_start_ != nullptr) {
363 // When there's a symtab, there should be a strtab.
364 if (strtab_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700365 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700366 return false;
367 }
368
369 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700370 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
371 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700372 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700373 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700374 return false;
375 }
376 }
377
378 // We always need a dynstr & dynsym.
379 if (dynstr_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700380 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700381 return false;
382 }
383 if (dynsym_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700384 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700385 return false;
386 }
387
388 // Need a hash section for dynamic symbol lookup.
389 if (hash_section_start_ == nullptr) {
390 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700391 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700392 return false;
393 }
394
395 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700396 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
397 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700398 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700399 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700400 return false;
401 }
402
Andreas Gampea696c0a2014-12-10 20:51:45 -0800403 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
404 // us). This is usually the last in an oat file, and a good indicator of whether writing was
405 // successful (or the process crashed and left garbage).
406 if (program_header_only_) {
407 // It might not be mapped, but we can compare against the file size.
408 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
409 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700410 if (offset >= file->GetLength()) {
Andreas Gampea696c0a2014-12-10 20:51:45 -0800411 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700412 file->GetPath().c_str());
Andreas Gampea696c0a2014-12-10 20:51:45 -0800413 return false;
414 }
415 }
416
Andreas Gampedaab38c2014-09-12 18:38:24 -0700417 return true;
418}
419
David Srbecky533c2072015-04-22 12:20:22 +0100420template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700421bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700422 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800423 // MemMap::Open should have already set an error.
424 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800425 return false;
426 }
427 map_.reset(map);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700428 CHECK(map_.get() != nullptr) << file->GetPath();
429 CHECK(map_->Begin() != nullptr) << file->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800430
Tong Shen62d1ca32014-09-03 17:24:56 -0700431 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000432 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
433 || (ELFMAG1 != header_->e_ident[EI_MAG1])
434 || (ELFMAG2 != header_->e_ident[EI_MAG2])
435 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800436 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
437 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700438 file->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000439 header_->e_ident[EI_MAG0],
440 header_->e_ident[EI_MAG1],
441 header_->e_ident[EI_MAG2],
442 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800443 return false;
444 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700445 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
446 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800447 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700448 elf_class,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700449 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800450 header_->e_ident[EI_CLASS]);
451 return false;
452 }
453 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
454 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
455 ELFDATA2LSB,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700456 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800457 header_->e_ident[EI_CLASS]);
458 return false;
459 }
460 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
461 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
462 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700463 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800464 header_->e_ident[EI_CLASS]);
465 return false;
466 }
467 if (ET_DYN != header_->e_type) {
468 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
469 ET_DYN,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700470 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800471 header_->e_type);
472 return false;
473 }
474 if (EV_CURRENT != header_->e_version) {
475 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
476 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700477 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800478 header_->e_version);
479 return false;
480 }
481 if (0 != header_->e_entry) {
482 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
483 0,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700484 file->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700485 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800486 return false;
487 }
488 if (0 == header_->e_phoff) {
489 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700490 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800491 return false;
492 }
493 if (0 == header_->e_shoff) {
494 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700495 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800496 return false;
497 }
498 if (0 == header_->e_ehsize) {
499 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700500 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800501 return false;
502 }
503 if (0 == header_->e_phentsize) {
504 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700505 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800506 return false;
507 }
508 if (0 == header_->e_phnum) {
509 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700510 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800511 return false;
512 }
513 if (0 == header_->e_shentsize) {
514 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700515 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800516 return false;
517 }
518 if (0 == header_->e_shnum) {
519 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700520 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800521 return false;
522 }
523 if (0 == header_->e_shstrndx) {
524 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700525 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800526 return false;
527 }
528 if (header_->e_shstrndx >= header_->e_shnum) {
529 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
530 header_->e_shstrndx,
531 header_->e_shnum,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700532 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800533 return false;
534 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800535
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800536 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800537 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700538 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
539 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800540 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700541 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800542 return false;
543 }
544 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700545 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
546 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800547 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700548 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800549 return false;
550 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800551 }
552 return true;
553}
554
David Srbecky533c2072015-04-22 12:20:22 +0100555template <typename ElfTypes>
556typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700557 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800558 return *header_;
559}
560
David Srbecky533c2072015-04-22 12:20:22 +0100561template <typename ElfTypes>
562uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700563 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
564 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800565 return program_headers_start_;
566}
567
David Srbecky533c2072015-04-22 12:20:22 +0100568template <typename ElfTypes>
569uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700570 CHECK(!program_header_only_); // Only used in "full" mode.
571 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800572 return section_headers_start_;
573}
574
David Srbecky533c2072015-04-22 12:20:22 +0100575template <typename ElfTypes>
576typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700577 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800578 return *dynamic_program_header_;
579}
580
David Srbecky533c2072015-04-22 12:20:22 +0100581template <typename ElfTypes>
582typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700583 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800584 return dynamic_section_start_;
585}
586
David Srbecky533c2072015-04-22 12:20:22 +0100587template <typename ElfTypes>
588typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
589 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700590 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800591 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000592 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700593 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800594 break;
595 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000596 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700597 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800598 break;
599 }
600 default: {
601 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700602 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800603 }
604 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800605}
606
David Srbecky533c2072015-04-22 12:20:22 +0100607template <typename ElfTypes>
608const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
609 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700610 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800611 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000612 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700613 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800614 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000615 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700616 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800617 }
618 default: {
619 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700620 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800621 }
622 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800623}
624
David Srbecky533c2072015-04-22 12:20:22 +0100625template <typename ElfTypes>
626const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
627 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700628 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800629 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700630 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800631 }
632 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700633 if (string_section_start == nullptr) {
634 return nullptr;
635 }
636 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800637}
638
Andreas Gampedaab38c2014-09-12 18:38:24 -0700639// WARNING: The following methods do not check for an error condition (non-existent hash section).
640// It is the caller's job to do this.
641
David Srbecky533c2072015-04-22 12:20:22 +0100642template <typename ElfTypes>
643typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800644 return hash_section_start_;
645}
646
David Srbecky533c2072015-04-22 12:20:22 +0100647template <typename ElfTypes>
648typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800649 return GetHashSectionStart()[0];
650}
651
David Srbecky533c2072015-04-22 12:20:22 +0100652template <typename ElfTypes>
653typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800654 return GetHashSectionStart()[1];
655}
656
David Srbecky533c2072015-04-22 12:20:22 +0100657template <typename ElfTypes>
658typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700659 if (i >= GetHashBucketNum()) {
660 *ok = false;
661 return 0;
662 }
663 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800664 // 0 is nbucket, 1 is nchain
665 return GetHashSectionStart()[2 + i];
666}
667
David Srbecky533c2072015-04-22 12:20:22 +0100668template <typename ElfTypes>
669typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600670 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700671 *ok = false;
672 return 0;
673 }
674 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800675 // 0 is nbucket, 1 is nchain, & chains are after buckets
676 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
677}
678
David Srbecky533c2072015-04-22 12:20:22 +0100679template <typename ElfTypes>
680typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800681 return GetHeader().e_phnum;
682}
683
David Srbecky533c2072015-04-22 12:20:22 +0100684template <typename ElfTypes>
685typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700686 CHECK_LT(i, GetProgramHeaderNum()) << file_path_; // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700687 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700688 if (program_header >= End()) {
689 return nullptr; // Failure condition.
690 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700691 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692}
693
David Srbecky533c2072015-04-22 12:20:22 +0100694template <typename ElfTypes>
695typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700696 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
697 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700698 if (program_header->p_type == type) {
699 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800700 }
701 }
Alex Light3470ab42014-06-18 10:35:45 -0700702 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800703}
704
David Srbecky533c2072015-04-22 12:20:22 +0100705template <typename ElfTypes>
706typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800707 return GetHeader().e_shnum;
708}
709
David Srbecky533c2072015-04-22 12:20:22 +0100710template <typename ElfTypes>
711typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800712 // Can only access arbitrary sections when we have the whole file, not just program header.
713 // Even if we Load(), it doesn't bring in all the sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700714 CHECK(!program_header_only_) << file_path_;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700715 if (i >= GetSectionHeaderNum()) {
716 return nullptr; // Failure condition.
717 }
Ian Rogers13735952014-10-08 12:43:28 -0700718 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700719 if (section_header >= End()) {
720 return nullptr; // Failure condition.
721 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700722 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800723}
724
David Srbecky533c2072015-04-22 12:20:22 +0100725template <typename ElfTypes>
726typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800727 // Can only access arbitrary sections when we have the whole file, not just program header.
728 // We could change this to switch on known types if they were detected during loading.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700729 CHECK(!program_header_only_) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700730 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
731 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700732 if (section_header->sh_type == type) {
733 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800734 }
735 }
Alex Light3470ab42014-06-18 10:35:45 -0700736 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800737}
738
739// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800740static unsigned elfhash(const char *_name) {
741 const unsigned char *name = (const unsigned char *) _name;
742 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800743
Brian Carlstromdf629502013-07-17 22:39:56 -0700744 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800745 h = (h << 4) + *name++;
746 g = h & 0xf0000000;
747 h ^= g;
748 h ^= g >> 24;
749 }
750 return h;
751}
752
David Srbecky533c2072015-04-22 12:20:22 +0100753template <typename ElfTypes>
754typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800755 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800756}
757
David Srbecky533c2072015-04-22 12:20:22 +0100758template <typename ElfTypes>
759const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
760 const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700761 // Check that we have a hash section.
762 if (GetHashSectionStart() == nullptr) {
763 return nullptr; // Failure condition.
764 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700765 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700766 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700767 // TODO: we need to change this to calculate base_address_ in ::Open,
768 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700769 return base_address_ + sym->st_value;
770 } else {
771 return nullptr;
772 }
773}
774
Andreas Gampedaab38c2014-09-12 18:38:24 -0700775// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
David Srbecky533c2072015-04-22 12:20:22 +0100776template <typename ElfTypes>
777const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
778 const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700779 if (GetHashBucketNum() == 0) {
780 // No dynamic symbols at all.
781 return nullptr;
782 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700783 Elf_Word hash = elfhash(symbol_name.c_str());
784 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700785 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700786 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700787 if (!ok) {
788 return nullptr;
789 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800790 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700791 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700792 if (symbol == nullptr) {
793 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800794 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700795 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
796 if (symbol_name == name) {
797 return symbol;
798 }
799 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
800 if (!ok) {
801 return nullptr;
802 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800803 }
Alex Light3470ab42014-06-18 10:35:45 -0700804 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800805}
806
David Srbecky533c2072015-04-22 12:20:22 +0100807template <typename ElfTypes>
808bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700809 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
810}
811
David Srbecky533c2072015-04-22 12:20:22 +0100812template <typename ElfTypes>
813typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800814 CHECK(IsSymbolSectionType(section_header.sh_type))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700815 << file_path_ << " " << section_header.sh_type;
816 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800817 return section_header.sh_size / section_header.sh_entsize;
818}
819
David Srbecky533c2072015-04-22 12:20:22 +0100820template <typename ElfTypes>
821typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700822 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700823 if (sym_start == nullptr) {
824 return nullptr;
825 }
826 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800827}
828
David Srbecky533c2072015-04-22 12:20:22 +0100829template <typename ElfTypes>
830typename ElfFileImpl<ElfTypes>::SymbolTable**
831ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700832 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800833 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000834 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800835 return &symtab_symbol_table_;
836 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000837 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800838 return &dynsym_symbol_table_;
839 }
840 default: {
841 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700842 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800843 }
844 }
845}
846
David Srbecky533c2072015-04-22 12:20:22 +0100847template <typename ElfTypes>
848typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
849 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700850 CHECK(!program_header_only_) << file_path_;
851 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800852
853 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700854 if (*symbol_table != nullptr || build_map) {
855 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800856 DCHECK(build_map);
857 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -0700858 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700859 if (symbol_section == nullptr) {
860 return nullptr; // Failure condition.
861 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700862 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700863 if (string_section == nullptr) {
864 return nullptr; // Failure condition.
865 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800866 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700867 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700868 if (symbol == nullptr) {
869 return nullptr; // Failure condition.
870 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700871 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
872 ? ELF64_ST_TYPE(symbol->st_info)
873 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000874 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800875 continue;
876 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700877 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700878 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800879 continue;
880 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700881 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -0700882 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800883 if (!result.second) {
884 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700885 if ((symbol->st_value != result.first->second->st_value) ||
886 (symbol->st_size != result.first->second->st_size) ||
887 (symbol->st_info != result.first->second->st_info) ||
888 (symbol->st_other != result.first->second->st_other) ||
889 (symbol->st_shndx != result.first->second->st_shndx)) {
890 return nullptr; // Failure condition.
891 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800892 }
893 }
894 }
Alex Light3470ab42014-06-18 10:35:45 -0700895 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -0700896 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800897 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700898 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800899 }
900 return it->second;
901 }
902
903 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -0700904 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700905 if (symbol_section == nullptr) {
906 return nullptr;
907 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700908 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700909 if (string_section == nullptr) {
910 return nullptr;
911 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800912 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700913 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700914 if (symbol == nullptr) {
915 return nullptr; // Failure condition.
916 }
917 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700918 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800919 continue;
920 }
921 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700922 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800923 }
924 }
Alex Light3470ab42014-06-18 10:35:45 -0700925 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800926}
927
David Srbecky533c2072015-04-22 12:20:22 +0100928template <typename ElfTypes>
929typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
930 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700931 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -0700932 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800933 return 0;
934 }
935 return symbol->st_value;
936}
937
David Srbecky533c2072015-04-22 12:20:22 +0100938template <typename ElfTypes>
939const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
940 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700941 CHECK(!program_header_only_) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800942 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -0700943 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700944 return nullptr; // Failure condition.
945 }
946 if (i >= string_section.sh_size) {
947 return nullptr;
948 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800949 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700950 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800951 }
Ian Rogers13735952014-10-08 12:43:28 -0700952 uint8_t* strings = Begin() + string_section.sh_offset;
953 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700954 if (string >= End()) {
955 return nullptr;
956 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800957 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800958}
959
David Srbecky533c2072015-04-22 12:20:22 +0100960template <typename ElfTypes>
961typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700962 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800963}
964
David Srbecky533c2072015-04-22 12:20:22 +0100965template <typename ElfTypes>
966typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700967 CHECK_LT(i, GetDynamicNum()) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800968 return *(GetDynamicSectionStart() + i);
969}
970
David Srbecky533c2072015-04-22 12:20:22 +0100971template <typename ElfTypes>
972typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700973 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
974 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -0700975 if (dyn->d_tag == type) {
976 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800977 }
978 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700979 return nullptr;
Alex Light53cb16b2014-06-12 11:26:29 -0700980}
981
David Srbecky533c2072015-04-22 12:20:22 +0100982template <typename ElfTypes>
983typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700984 Elf_Dyn* dyn = FindDynamicByType(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700985 if (dyn == nullptr) {
Alex Light53cb16b2014-06-12 11:26:29 -0700986 return 0;
987 } else {
988 return dyn->d_un.d_val;
989 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800990}
991
David Srbecky533c2072015-04-22 12:20:22 +0100992template <typename ElfTypes>
993typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700994 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -0700995 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800996}
997
David Srbecky533c2072015-04-22 12:20:22 +0100998template <typename ElfTypes>
999typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001000 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1001 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001002 return section_header.sh_size / section_header.sh_entsize;
1003}
1004
David Srbecky533c2072015-04-22 12:20:22 +01001005template <typename ElfTypes>
1006typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001007 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1008 CHECK_LT(i, GetRelNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001009 return *(GetRelSectionStart(section_header) + i);
1010}
1011
David Srbecky533c2072015-04-22 12:20:22 +01001012template <typename ElfTypes>
1013typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001014 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001015 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001016}
1017
David Srbecky533c2072015-04-22 12:20:22 +01001018template <typename ElfTypes>
1019typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001020 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001021 return section_header.sh_size / section_header.sh_entsize;
1022}
1023
David Srbecky533c2072015-04-22 12:20:22 +01001024template <typename ElfTypes>
1025typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001026 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1027 CHECK_LT(i, GetRelaNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001028 return *(GetRelaSectionStart(section_header) + i);
1029}
1030
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001031// Base on bionic phdr_table_get_load_size
David Srbecky533c2072015-04-22 12:20:22 +01001032template <typename ElfTypes>
Vladimir Marko3fc99032015-05-13 19:06:30 +01001033bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
1034 Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1);
1035 Elf_Addr max_vaddr = 0u;
Tong Shen62d1ca32014-09-03 17:24:56 -07001036 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1037 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001038 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001039 continue;
1040 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001041 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001042 if (begin_vaddr < min_vaddr) {
1043 min_vaddr = begin_vaddr;
1044 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001045 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001046 if (UNLIKELY(begin_vaddr > end_vaddr)) {
1047 std::ostringstream oss;
1048 oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex
1049 << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001050 << " in ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001051 *error_msg = oss.str();
1052 *size = static_cast<size_t>(-1);
1053 return false;
1054 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001055 if (end_vaddr > max_vaddr) {
1056 max_vaddr = end_vaddr;
1057 }
1058 }
1059 min_vaddr = RoundDown(min_vaddr, kPageSize);
1060 max_vaddr = RoundUp(max_vaddr, kPageSize);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001061 CHECK_LT(min_vaddr, max_vaddr) << file_path_;
Vladimir Marko3fc99032015-05-13 19:06:30 +01001062 Elf_Addr loaded_size = max_vaddr - min_vaddr;
1063 // Check that the loaded_size fits in size_t.
1064 if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) {
1065 std::ostringstream oss;
1066 oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x"
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001067 << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001068 *error_msg = oss.str();
1069 *size = static_cast<size_t>(-1);
1070 return false;
1071 }
1072 *size = loaded_size;
1073 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001074}
1075
David Srbecky533c2072015-04-22 12:20:22 +01001076template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001077bool ElfFileImpl<ElfTypes>::Load(File* file,
1078 bool executable,
1079 bool low_4gb,
1080 std::string* error_msg) {
1081 CHECK(program_header_only_) << file->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001082
1083 if (executable) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001084 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001085 if (elf_ISA != kRuntimeISA) {
1086 std::ostringstream oss;
1087 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1088 *error_msg = oss.str();
1089 return false;
1090 }
1091 }
1092
Jim_Guoa62a5882014-04-28 11:11:57 +08001093 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001094 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1095 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001096 if (program_header == nullptr) {
1097 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001098 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001099 return false;
1100 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001101
1102 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001103 if (program_header->p_type == PT_DYNAMIC) {
1104 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001105 continue;
1106 }
1107
1108 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001109 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110 continue;
1111 }
1112
1113 // Found something to load.
1114
Jim_Guoa62a5882014-04-28 11:11:57 +08001115 // Before load the actual segments, reserve a contiguous chunk
1116 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001117 // permissions. We'll then carve that up with the proper
1118 // permissions as we load the actual segments. If p_vaddr is
1119 // non-zero, the segments require the specific address specified,
1120 // which either was specified in the file because we already set
1121 // base_address_ after the first zero segment).
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001122 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -08001123 if (temp_file_length < 0) {
1124 errno = -temp_file_length;
1125 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001126 file->GetPath().c_str(), file->Fd(), strerror(errno));
Ian Rogerscdfcf372014-01-23 20:38:36 -08001127 return false;
1128 }
1129 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001130 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001131 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1132 uint8_t* reserve_base_override = reserve_base;
1133 // Override the base (e.g. when compiling with --compile-pic)
1134 if (requested_base_ != nullptr) {
1135 reserve_base_override = requested_base_;
1136 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001137 std::string reservation_name("ElfFile reservation for ");
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001138 reservation_name += file->GetPath();
Vladimir Marko3fc99032015-05-13 19:06:30 +01001139 size_t loaded_size;
1140 if (!GetLoadedSize(&loaded_size, error_msg)) {
1141 DCHECK(!error_msg->empty());
1142 return false;
1143 }
Ian Rogers700a4022014-05-19 16:49:03 -07001144 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001145 reserve_base_override,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001146 loaded_size,
1147 PROT_NONE,
1148 low_4gb,
1149 false,
Jim_Guoa62a5882014-04-28 11:11:57 +08001150 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001151 if (reserve.get() == nullptr) {
1152 *error_msg = StringPrintf("Failed to allocate %s: %s",
1153 reservation_name.c_str(), error_msg->c_str());
1154 return false;
1155 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001156 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001157
1158 // Base address is the difference of actual mapped location and the p_vaddr
1159 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1160 - reinterpret_cast<uintptr_t>(reserve_base));
1161 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1162 // dynamic memory address of where that object is actually mapped
1163 //
1164 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1165 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001166 segments_.push_back(reserve.release());
1167 }
1168 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001169 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001170 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001171 }
Ian Rogers13735952014-10-08 12:43:28 -07001172 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001173 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001174 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001175 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001176 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001177 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001178 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001179 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001180 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001181 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001182 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001183 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001184 if (writable_) {
1185 prot |= PROT_WRITE;
1186 flags |= MAP_SHARED;
1187 } else {
1188 flags |= MAP_PRIVATE;
1189 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001190 if (program_header->p_filesz > program_header->p_memsz) {
1191 *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1192 static_cast<uint64_t>(program_header->p_filesz),
1193 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001194 file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001195 return false;
1196 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001197 if (program_header->p_filesz < program_header->p_memsz &&
1198 !IsAligned<kPageSize>(program_header->p_filesz)) {
1199 *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1200 " < %" PRIu64 "): %s",
1201 static_cast<uint64_t>(program_header->p_filesz),
1202 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001203 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001204 return false;
1205 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001206 if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1207 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1208 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1209 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001210 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001211 return false;
1212 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001213 if (program_header->p_filesz != 0u) {
1214 std::unique_ptr<MemMap> segment(
1215 MemMap::MapFileAtAddress(p_vaddr,
1216 program_header->p_filesz,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001217 prot,
1218 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001219 file->Fd(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001220 program_header->p_offset,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001221 /*low4_gb*/false,
1222 /*reuse*/true, // implies MAP_FIXED
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001223 file->GetPath().c_str(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001224 error_msg));
1225 if (segment.get() == nullptr) {
1226 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001227 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001228 return false;
1229 }
1230 if (segment->Begin() != p_vaddr) {
1231 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1232 "instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001233 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001234 return false;
1235 }
1236 segments_.push_back(segment.release());
1237 }
1238 if (program_header->p_filesz < program_header->p_memsz) {
1239 std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001240 static_cast<uint64_t>(i), file->GetPath().c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001241 std::unique_ptr<MemMap> segment(
1242 MemMap::MapAnonymous(name.c_str(),
1243 p_vaddr + program_header->p_filesz,
1244 program_header->p_memsz - program_header->p_filesz,
1245 prot, false, true /* reuse */, error_msg));
1246 if (segment == nullptr) {
1247 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001248 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001249 return false;
1250 }
1251 if (segment->Begin() != p_vaddr) {
1252 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1253 "at expected address %p, instead mapped to %p",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001254 i, file->GetPath().c_str(), p_vaddr, segment->Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001255 return false;
1256 }
1257 segments_.push_back(segment.release());
1258 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001259 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001260
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001261 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001262 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001263 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1264 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001265 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001266 return false;
1267 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001268 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001269
Tong Shen62d1ca32014-09-03 17:24:56 -07001270 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1271 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001272 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001273 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001274 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001275 if (!ValidPointer(d_ptr)) {
1276 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001277 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001278 return false;
1279 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001280 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001281 break;
1282 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001283 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001284 if (!ValidPointer(d_ptr)) {
1285 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001286 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001287 return false;
1288 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001289 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1290 break;
1291 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001292 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001293 if (!ValidPointer(d_ptr)) {
1294 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001295 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001296 return false;
1297 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001298 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001299 break;
1300 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001301 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001302 if (GetDynamicNum() != i+1) {
1303 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1304 "expected %d as implied by size of PT_DYNAMIC segment in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001305 i + 1, GetDynamicNum(), file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001306 return false;
1307 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001308 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001309 }
1310 }
1311 }
1312
Andreas Gampedaab38c2014-09-12 18:38:24 -07001313 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001314 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001315 return false;
1316 }
1317
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001318 return true;
1319}
1320
David Srbecky533c2072015-04-22 12:20:22 +01001321template <typename ElfTypes>
1322bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001323 for (size_t i = 0; i < segments_.size(); ++i) {
1324 const MemMap* segment = segments_[i];
1325 if (segment->Begin() <= start && start < segment->End()) {
1326 return true;
1327 }
1328 }
1329 return false;
1330}
1331
Alex Light3470ab42014-06-18 10:35:45 -07001332
David Srbecky533c2072015-04-22 12:20:22 +01001333template <typename ElfTypes>
1334typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1335 const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001336 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001337 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001338 if (shstrtab_sec == nullptr) {
1339 return nullptr;
1340 }
Alex Light3470ab42014-06-18 10:35:45 -07001341 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001342 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001343 if (shdr == nullptr) {
1344 return nullptr;
1345 }
1346 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001347 if (sec_name == nullptr) {
1348 continue;
1349 }
1350 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001351 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001352 }
1353 }
1354 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001355}
1356
David Srbecky533c2072015-04-22 12:20:22 +01001357template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001358bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001359 if (base_address_delta == 0) {
1360 return true;
1361 }
David Srbeckyf8980872015-05-22 17:04:47 +01001362 return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1363 ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1364 ApplyOatPatchesTo(".debug_line", base_address_delta);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001365}
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001366
David Srbecky533c2072015-04-22 12:20:22 +01001367template <typename ElfTypes>
1368bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
David Srbeckyf8980872015-05-22 17:04:47 +01001369 const char* target_section_name, Elf_Addr delta) {
1370 auto target_section = FindSectionByName(target_section_name);
1371 if (target_section == nullptr) {
1372 return true;
1373 }
1374 std::string patches_name = target_section_name + std::string(".oat_patches");
1375 auto patches_section = FindSectionByName(patches_name.c_str());
David Srbecky2f6cdb02015-04-11 00:17:53 +01001376 if (patches_section == nullptr) {
David Srbeckyf8980872015-05-22 17:04:47 +01001377 LOG(ERROR) << patches_name << " section not found.";
Alex Light3470ab42014-06-18 10:35:45 -07001378 return false;
1379 }
David Srbecky2f6cdb02015-04-11 00:17:53 +01001380 if (patches_section->sh_type != SHT_OAT_PATCH) {
David Srbeckyf8980872015-05-22 17:04:47 +01001381 LOG(ERROR) << "Unexpected type of " << patches_name;
Alex Light3470ab42014-06-18 10:35:45 -07001382 return false;
1383 }
David Srbeckyf8980872015-05-22 17:04:47 +01001384 ApplyOatPatches(
David Srbecky2f6cdb02015-04-11 00:17:53 +01001385 Begin() + patches_section->sh_offset,
1386 Begin() + patches_section->sh_offset + patches_section->sh_size,
David Srbeckyf8980872015-05-22 17:04:47 +01001387 delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +01001388 Begin() + target_section->sh_offset,
David Srbeckyf8980872015-05-22 17:04:47 +01001389 Begin() + target_section->sh_offset + target_section->sh_size);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001390 return true;
1391}
1392
David Srbeckyf8980872015-05-22 17:04:47 +01001393// Apply LEB128 encoded patches to given section.
David Srbecky533c2072015-04-22 12:20:22 +01001394template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001395void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1396 const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky533c2072015-04-22 12:20:22 +01001397 uint8_t* to_patch, const uint8_t* to_patch_end) {
David Srbeckyf8980872015-05-22 17:04:47 +01001398 typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress;
1399 while (patches < patches_end) {
1400 to_patch += DecodeUnsignedLeb128(&patches);
1401 DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1402 DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1403 *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
David Srbecky2f6cdb02015-04-11 00:17:53 +01001404 }
Alex Light3470ab42014-06-18 10:35:45 -07001405}
Mark Mendellae9fd932014-02-10 16:14:35 -08001406
David Srbecky533c2072015-04-22 12:20:22 +01001407template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001408bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001409 // ELF files produced by MCLinker look roughly like this
1410 //
1411 // +------------+
1412 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
1413 // +------------+
1414 // | Elf_Phdr | program headers
1415 // | Elf_Phdr |
1416 // | ... |
1417 // | Elf_Phdr |
1418 // +------------+
1419 // | section | mixture of needed and unneeded sections
1420 // +------------+
1421 // | section |
1422 // +------------+
1423 // | ... |
1424 // +------------+
1425 // | section |
1426 // +------------+
1427 // | Elf_Shdr | section headers
1428 // | Elf_Shdr |
1429 // | ... | contains offset to section start
1430 // | Elf_Shdr |
1431 // +------------+
1432 //
1433 // To strip:
1434 // - leave the Elf_Ehdr and Elf_Phdr values in place.
1435 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1436 // - move the sections are keeping up to fill in gaps of sections we want to strip
1437 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1438 // - truncate rest of file
1439 //
1440
1441 std::vector<Elf_Shdr> section_headers;
1442 std::vector<Elf_Word> section_headers_original_indexes;
1443 section_headers.reserve(GetSectionHeaderNum());
1444
1445
1446 Elf_Shdr* string_section = GetSectionNameStringSection();
1447 CHECK(string_section != nullptr);
1448 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1449 Elf_Shdr* sh = GetSectionHeader(i);
1450 CHECK(sh != nullptr);
1451 const char* name = GetString(*string_section, sh->sh_name);
1452 if (name == nullptr) {
1453 CHECK_EQ(0U, i);
1454 section_headers.push_back(*sh);
1455 section_headers_original_indexes.push_back(0);
1456 continue;
1457 }
Andreas Gampe9186ced2016-12-12 14:28:21 -08001458 if (android::base::StartsWith(name, ".debug")
Tong Shen62d1ca32014-09-03 17:24:56 -07001459 || (strcmp(name, ".strtab") == 0)
1460 || (strcmp(name, ".symtab") == 0)) {
1461 continue;
1462 }
1463 section_headers.push_back(*sh);
1464 section_headers_original_indexes.push_back(i);
1465 }
1466 CHECK_NE(0U, section_headers.size());
1467 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1468
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001469 // section 0 is the null section, sections start at offset of first section
Tong Shen62d1ca32014-09-03 17:24:56 -07001470 CHECK(GetSectionHeader(1) != nullptr);
1471 Elf_Off offset = GetSectionHeader(1)->sh_offset;
1472 for (size_t i = 1; i < section_headers.size(); i++) {
1473 Elf_Shdr& new_sh = section_headers[i];
1474 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1475 CHECK(old_sh != nullptr);
1476 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1477 if (old_sh->sh_addralign > 1) {
1478 offset = RoundUp(offset, old_sh->sh_addralign);
1479 }
1480 if (old_sh->sh_offset == offset) {
1481 // already in place
1482 offset += old_sh->sh_size;
1483 continue;
1484 }
1485 // shift section earlier
1486 memmove(Begin() + offset,
1487 Begin() + old_sh->sh_offset,
1488 old_sh->sh_size);
1489 new_sh.sh_offset = offset;
1490 offset += old_sh->sh_size;
1491 }
1492
1493 Elf_Off shoff = offset;
1494 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1495 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1496 offset += section_headers_size_in_bytes;
1497
1498 GetHeader().e_shnum = section_headers.size();
1499 GetHeader().e_shoff = shoff;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001500 int result = ftruncate(file->Fd(), offset);
Tong Shen62d1ca32014-09-03 17:24:56 -07001501 if (result != 0) {
1502 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001503 file->GetPath().c_str(), strerror(errno));
Tong Shen62d1ca32014-09-03 17:24:56 -07001504 return false;
1505 }
1506 return true;
1507}
1508
1509static const bool DEBUG_FIXUP = false;
1510
David Srbecky533c2072015-04-22 12:20:22 +01001511template <typename ElfTypes>
1512bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001513 if (!FixupDynamic(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001514 LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001515 return false;
1516 }
1517 if (!FixupSectionHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001518 LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001519 return false;
1520 }
1521 if (!FixupProgramHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001522 LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001523 return false;
1524 }
1525 if (!FixupSymbols(base_address, true)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001526 LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001527 return false;
1528 }
1529 if (!FixupSymbols(base_address, false)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001530 LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001531 return false;
1532 }
1533 if (!FixupRelocations(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001534 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001535 return false;
1536 }
Andreas Gampe3c54b002015-04-07 16:09:30 -07001537 static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1538 if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001539 LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001540 return false;
1541 }
1542 return true;
1543}
1544
David Srbecky533c2072015-04-22 12:20:22 +01001545template <typename ElfTypes>
1546bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001547 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1548 Elf_Dyn& elf_dyn = GetDynamic(i);
1549 Elf_Word d_tag = elf_dyn.d_tag;
1550 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1551 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1552 if (DEBUG_FIXUP) {
1553 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001554 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001555 static_cast<uint64_t>(d_ptr),
1556 static_cast<uint64_t>(d_ptr + base_address));
1557 }
1558 d_ptr += base_address;
1559 elf_dyn.d_un.d_ptr = d_ptr;
1560 }
1561 }
1562 return true;
1563}
1564
David Srbecky533c2072015-04-22 12:20:22 +01001565template <typename ElfTypes>
1566bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001567 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1568 Elf_Shdr* sh = GetSectionHeader(i);
1569 CHECK(sh != nullptr);
1570 // 0 implies that the section will not exist in the memory of the process
1571 if (sh->sh_addr == 0) {
1572 continue;
1573 }
1574 if (DEBUG_FIXUP) {
1575 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001576 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001577 static_cast<uint64_t>(sh->sh_addr),
1578 static_cast<uint64_t>(sh->sh_addr + base_address));
1579 }
1580 sh->sh_addr += base_address;
1581 }
1582 return true;
1583}
1584
David Srbecky533c2072015-04-22 12:20:22 +01001585template <typename ElfTypes>
1586bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001587 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1588 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1589 Elf_Phdr* ph = GetProgramHeader(i);
1590 CHECK(ph != nullptr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001591 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001592 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001593 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001594 if (DEBUG_FIXUP) {
1595 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001596 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001597 static_cast<uint64_t>(ph->p_vaddr),
1598 static_cast<uint64_t>(ph->p_vaddr + base_address));
1599 }
1600 ph->p_vaddr += base_address;
1601 ph->p_paddr += base_address;
1602 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001603 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001604 }
1605 return true;
1606}
1607
David Srbecky533c2072015-04-22 12:20:22 +01001608template <typename ElfTypes>
1609bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001610 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1611 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1612 Elf_Shdr* symbol_section = FindSectionByType(section_type);
1613 if (symbol_section == nullptr) {
1614 // file is missing optional .symtab
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001615 CHECK(!dynamic) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001616 return true;
1617 }
1618 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1619 Elf_Sym* symbol = GetSymbol(section_type, i);
1620 CHECK(symbol != nullptr);
1621 if (symbol->st_value != 0) {
1622 if (DEBUG_FIXUP) {
1623 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001624 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001625 static_cast<uint64_t>(symbol->st_value),
1626 static_cast<uint64_t>(symbol->st_value + base_address));
1627 }
1628 symbol->st_value += base_address;
1629 }
1630 }
1631 return true;
1632}
1633
David Srbecky533c2072015-04-22 12:20:22 +01001634template <typename ElfTypes>
1635bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001636 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1637 Elf_Shdr* sh = GetSectionHeader(i);
1638 CHECK(sh != nullptr);
1639 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001640 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1641 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001642 if (DEBUG_FIXUP) {
1643 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001644 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001645 static_cast<uint64_t>(rel.r_offset),
1646 static_cast<uint64_t>(rel.r_offset + base_address));
1647 }
1648 rel.r_offset += base_address;
1649 }
1650 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001651 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1652 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001653 if (DEBUG_FIXUP) {
1654 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001655 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001656 static_cast<uint64_t>(rela.r_offset),
1657 static_cast<uint64_t>(rela.r_offset + base_address));
1658 }
1659 rela.r_offset += base_address;
1660 }
1661 }
1662 }
1663 return true;
1664}
1665
1666// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +01001667template class ElfFileImpl<ElfTypes32>;
1668template class ElfFileImpl<ElfTypes64>;
Tong Shen62d1ca32014-09-03 17:24:56 -07001669
Ian Rogersd4c4d952014-10-16 20:31:53 -07001670ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001671}
1672
Ian Rogersd4c4d952014-10-16 20:31:53 -07001673ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001674}
1675
1676ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001677 // Should never have 32 and 64-bit impls.
1678 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001679}
1680
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001681ElfFile* ElfFile::Open(File* file,
1682 bool writable,
1683 bool program_header_only,
1684 bool low_4gb,
1685 std::string* error_msg,
Igor Murashkin46774762014-10-22 11:37:02 -07001686 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001687 if (file->GetLength() < EI_NIDENT) {
1688 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1689 file->GetPath().c_str());
1690 return nullptr;
1691 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001692 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1693 PROT_READ,
1694 MAP_PRIVATE,
1695 file->Fd(),
1696 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001697 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001698 file->GetPath().c_str(),
1699 error_msg));
Steven Morelande16c3bf2017-05-24 09:12:32 -07001700 if (map == nullptr || map->Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001701 return nullptr;
1702 }
Ian Rogers13735952014-10-08 12:43:28 -07001703 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001704 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001705 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1706 writable,
1707 program_header_only,
1708 low_4gb,
1709 error_msg,
1710 requested_base);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001711 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001712 return nullptr;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001713 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001714 return new ElfFile(elf_file_impl);
1715 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001716 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1717 writable,
1718 program_header_only,
1719 low_4gb,
1720 error_msg,
1721 requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001722 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001723 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001724 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001725 return new ElfFile(elf_file_impl);
1726 } else {
1727 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1728 ELFCLASS32, ELFCLASS64,
1729 file->GetPath().c_str(),
1730 header[EI_CLASS]);
1731 return nullptr;
1732 }
1733}
1734
1735ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001736 // low_4gb support not required for this path.
1737 constexpr bool low_4gb = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001738 if (file->GetLength() < EI_NIDENT) {
1739 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1740 file->GetPath().c_str());
1741 return nullptr;
1742 }
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001743 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1744 PROT_READ,
1745 MAP_PRIVATE,
1746 file->Fd(),
1747 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001748 low_4gb,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001749 file->GetPath().c_str(),
1750 error_msg));
Steven Morelande16c3bf2017-05-24 09:12:32 -07001751 if (map == nullptr || map->Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001752 return nullptr;
1753 }
Ian Rogers13735952014-10-08 12:43:28 -07001754 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001755 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001756 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1757 mmap_prot,
1758 mmap_flags,
1759 low_4gb,
1760 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001761 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001762 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001763 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001764 return new ElfFile(elf_file_impl);
1765 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001766 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1767 mmap_prot,
1768 mmap_flags,
1769 low_4gb,
1770 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001771 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001772 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001773 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001774 return new ElfFile(elf_file_impl);
1775 } else {
1776 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1777 ELFCLASS32, ELFCLASS64,
1778 file->GetPath().c_str(),
1779 header[EI_CLASS]);
1780 return nullptr;
1781 }
1782}
1783
1784#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001785 if (elf64_.get() != nullptr) { \
1786 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001787 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001788 DCHECK(elf32_.get() != nullptr); \
1789 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001790 }
1791
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001792bool ElfFile::Load(File* file, bool executable, bool low_4gb, std::string* error_msg) {
1793 DELEGATE_TO_IMPL(Load, file, executable, low_4gb, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001794}
1795
Ian Rogers13735952014-10-08 12:43:28 -07001796const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001797 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1798}
1799
1800size_t ElfFile::Size() const {
1801 DELEGATE_TO_IMPL(Size);
1802}
1803
Ian Rogers13735952014-10-08 12:43:28 -07001804uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001805 DELEGATE_TO_IMPL(Begin);
1806}
1807
Ian Rogers13735952014-10-08 12:43:28 -07001808uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001809 DELEGATE_TO_IMPL(End);
1810}
1811
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001812const std::string& ElfFile::GetFilePath() const {
1813 DELEGATE_TO_IMPL(GetFilePath);
Tong Shen62d1ca32014-09-03 17:24:56 -07001814}
1815
Alex Light0eb76d22015-08-11 18:03:47 -07001816bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1817 uint64_t* size) const {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001818 if (elf32_.get() == nullptr) {
1819 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001820
Ian Rogersd4c4d952014-10-16 20:31:53 -07001821 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1822 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001823 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001824 }
1825 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001826 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001827 }
1828 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001829 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001830 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001831 return true;
1832 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001833 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1834 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001835 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001836 }
1837 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001838 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001839 }
1840 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001841 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001842 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001843 return true;
1844 }
1845}
1846
Jeff Hao24ec0282016-03-17 21:32:45 -07001847bool ElfFile::HasSection(const std::string& name) const {
1848 if (elf64_.get() != nullptr) {
1849 return elf64_->FindSectionByName(name) != nullptr;
1850 } else {
1851 return elf32_->FindSectionByName(name) != nullptr;
1852 }
1853}
1854
Tong Shen62d1ca32014-09-03 17:24:56 -07001855uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1856 const std::string& symbol_name,
1857 bool build_map) {
1858 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1859}
1860
Vladimir Marko3fc99032015-05-13 19:06:30 +01001861bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1862 DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001863}
1864
1865bool ElfFile::Strip(File* file, std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001866 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001867 if (elf_file.get() == nullptr) {
1868 return false;
1869 }
1870
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001871 if (elf_file->elf64_.get() != nullptr) {
1872 return elf_file->elf64_->Strip(file, error_msg);
1873 } else {
1874 return elf_file->elf32_->Strip(file, error_msg);
1875 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001876}
1877
Andreas Gampe3c54b002015-04-07 16:09:30 -07001878bool ElfFile::Fixup(uint64_t base_address) {
1879 if (elf64_.get() != nullptr) {
1880 return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1881 } else {
1882 DCHECK(elf32_.get() != nullptr);
1883 CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1884 return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1885 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001886 DELEGATE_TO_IMPL(Fixup, base_address);
1887}
1888
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001889} // namespace art