blob: 12c33dead87a5f2ccf82bc455ac725a6abbc4c6c [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"
David Srbecky50928112019-03-22 17:06:28 +000032#include "elf/elf_utils.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070033#include "elf_file_impl.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>
Vladimir Markoc09cd052018-08-23 16:36:36 +010040ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable, bool program_header_only)
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070041 : writable_(writable),
Brian Carlstromc1409452014-02-26 14:06:23 -080042 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -070043 header_(nullptr),
44 base_address_(nullptr),
45 program_headers_start_(nullptr),
46 section_headers_start_(nullptr),
47 dynamic_program_header_(nullptr),
48 dynamic_section_start_(nullptr),
49 symtab_section_start_(nullptr),
50 dynsym_section_start_(nullptr),
51 strtab_section_start_(nullptr),
52 dynstr_section_start_(nullptr),
53 hash_section_start_(nullptr),
54 symtab_symbol_table_(nullptr),
Vladimir Markoc09cd052018-08-23 16:36:36 +010055 dynsym_symbol_table_(nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -070056 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -080057}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058
David Srbecky533c2072015-04-22 12:20:22 +010059template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080060ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
61 bool writable,
62 bool program_header_only,
63 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +010064 std::string* error_msg) {
65 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(
66 new ElfFileImpl<ElfTypes>(file, writable, program_header_only));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067 int prot;
68 int flags;
Alex Light3470ab42014-06-18 10:35:45 -070069 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080070 prot = PROT_READ | PROT_WRITE;
71 flags = MAP_SHARED;
72 } else {
73 prot = PROT_READ;
74 flags = MAP_PRIVATE;
75 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070076 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070077 return nullptr;
78 }
79 return elf_file.release();
80}
81
David Srbecky533c2072015-04-22 12:20:22 +010082template <typename ElfTypes>
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080083ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
84 int prot,
85 int flags,
86 bool low_4gb,
87 std::string* error_msg) {
Vladimir Markoc09cd052018-08-23 16:36:36 +010088 std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(
Andreas Gampe98ea9d92018-10-19 14:06:15 -070089 new ElfFileImpl<ElfTypes>(file, (prot & PROT_WRITE) != 0, /* program_header_only= */ false));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070090 if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
Alex Light3470ab42014-06-18 10:35:45 -070091 return nullptr;
92 }
93 return elf_file.release();
94}
95
David Srbecky533c2072015-04-22 12:20:22 +010096template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070097bool ElfFileImpl<ElfTypes>::Setup(File* file,
98 int prot,
99 int flags,
100 bool low_4gb,
101 std::string* error_msg) {
102 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -0800103 if (temp_file_length < 0) {
104 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700106 file->GetPath().c_str(), file->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800107 return false;
108 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800109 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700110 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800111 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700112 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700113 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114 return false;
115 }
116
Brian Carlstromc1409452014-02-26 14:06:23 -0800117 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700119 size_t elf_header_size = sizeof(Elf_Ehdr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700120 if (!SetMap(file,
121 MemMap::MapFile(elf_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800122 prot,
123 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700124 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800125 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800126 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700127 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800128 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800129 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130 return false;
131 }
132 // then remap to cover program header
133 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700134 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800135 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 "header of %zd bytes: '%s'", file_length,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700137 sizeof(Elf_Ehdr), file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700138 return false;
139 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700140 if (!SetMap(file,
141 MemMap::MapFile(program_header_size,
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800142 prot,
143 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700144 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800145 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800146 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700147 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800148 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800149 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700150 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800151 return false;
152 }
153 } else {
154 // otherwise map entire file
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700155 if (!SetMap(file,
156 MemMap::MapFile(file->GetLength(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800157 prot,
158 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700159 file->Fd(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800160 0,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800161 low_4gb,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700162 file->GetPath().c_str(),
Mathieu Chartier42bddce2015-11-09 15:16:56 -0800163 error_msg),
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800164 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700165 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800166 return false;
167 }
168 }
169
Andreas Gampedaab38c2014-09-12 18:38:24 -0700170 if (program_header_only_) {
171 program_headers_start_ = Begin() + GetHeader().e_phoff;
172 } else {
173 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
174 return false;
175 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800176
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800177 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700178 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
179 return false;
180 }
181
182 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700183 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700184 if (shstrtab_section_header == nullptr) {
185 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700186 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700187 return false;
188 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189
190 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000191 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700192 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700194 file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800195 return false;
196 }
197
Andreas Gampedaab38c2014-09-12 18:38:24 -0700198 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700199 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700200 return false;
201 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202
203 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700204 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
205 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700206 if (section_header == nullptr) {
207 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700208 i, file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700209 return false;
210 }
211 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000212 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700213 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700214 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700215 return false;
216 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 break;
218 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000219 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700220 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700221 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700222 return false;
223 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800224 break;
225 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000226 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800227 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700228 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
229 // Check that this is named ".dynstr" and ignore otherwise.
230 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
231 if (strncmp(".dynstr", header_name, 8) == 0) {
232 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700233 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700234 return false;
235 }
236 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800237 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700238 // Check that this is named ".strtab" and ignore otherwise.
239 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
240 if (strncmp(".strtab", header_name, 8) == 0) {
241 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700242 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700243 return false;
244 }
245 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800246 }
247 break;
248 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000249 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700250 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700251 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800252 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700253 << file->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800254 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700255 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800256 return false;
257 }
258 break;
259 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000260 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700261 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700262 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700263 return false;
264 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800265 break;
266 }
267 }
268 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700269
270 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700271 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700272 return false;
273 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800274 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700275
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276 return true;
277}
278
David Srbecky533c2072015-04-22 12:20:22 +0100279template <typename ElfTypes>
280ElfFileImpl<ElfTypes>::~ElfFileImpl() {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800281 delete symtab_symbol_table_;
282 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800283}
284
David Srbecky533c2072015-04-22 12:20:22 +0100285template <typename ElfTypes>
286bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
287 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700288 if (Begin() + offset >= End()) {
289 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700290 file_path_.c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700291 return false;
292 }
293 *target = Begin() + offset;
294 return true;
295}
296
David Srbecky533c2072015-04-22 12:20:22 +0100297template <typename ElfTypes>
298bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
299 const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700300 // Only works in whole-program mode, as we need to iterate over the sections.
301 // Note that we normally can't search by type, as duplicates are allowed for most section types.
302 if (program_header_only_) {
303 return true;
304 }
305
Tong Shen62d1ca32014-09-03 17:24:56 -0700306 Elf_Shdr* source_section = nullptr;
307 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700308 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700309 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
310 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700311
312 if (Begin() + section_header->sh_offset == source) {
313 // Found the source.
314 source_section = section_header;
315 if (target_index) {
316 break;
317 }
318 } else if (Begin() + section_header->sh_offset == target) {
319 target_index = i;
320 target_found = true;
321 if (source_section != nullptr) {
322 break;
323 }
324 }
325 }
326
327 return target_found && source_section != nullptr && source_section->sh_link == target_index;
328}
329
David Srbecky533c2072015-04-22 12:20:22 +0100330template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700331 bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700332 if (!program_header_only_) {
333 // If in full mode, need section headers.
334 if (section_headers_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700335 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700336 return false;
337 }
338 }
339
340 // This is redundant, but defensive.
341 if (dynamic_program_header_ == nullptr) {
342 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700343 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700344 return false;
345 }
346
347 // Need a dynamic section. This is redundant, but defensive.
348 if (dynamic_section_start_ == nullptr) {
349 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700350 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700351 return false;
352 }
353
354 // Symtab validation. These is not really a hard failure, as we are currently not using the
355 // symtab internally, but it's nice to be defensive.
356 if (symtab_section_start_ != nullptr) {
357 // When there's a symtab, there should be a strtab.
358 if (strtab_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700359 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700360 return false;
361 }
362
363 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700364 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
365 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700366 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700367 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700368 return false;
369 }
370 }
371
372 // We always need a dynstr & dynsym.
373 if (dynstr_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700374 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700375 return false;
376 }
377 if (dynsym_section_start_ == nullptr) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700378 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700379 return false;
380 }
381
382 // Need a hash section for dynamic symbol lookup.
383 if (hash_section_start_ == nullptr) {
384 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700385 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700386 return false;
387 }
388
389 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700390 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
391 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700392 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700393 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -0700394 return false;
395 }
396
Andreas Gampea696c0a2014-12-10 20:51:45 -0800397 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
398 // us). This is usually the last in an oat file, and a good indicator of whether writing was
399 // successful (or the process crashed and left garbage).
400 if (program_header_only_) {
401 // It might not be mapped, but we can compare against the file size.
402 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
403 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700404 if (offset >= file->GetLength()) {
Andreas Gampea696c0a2014-12-10 20:51:45 -0800405 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700406 file->GetPath().c_str());
Andreas Gampea696c0a2014-12-10 20:51:45 -0800407 return false;
408 }
409 }
410
Andreas Gampedaab38c2014-09-12 18:38:24 -0700411 return true;
412}
413
David Srbecky533c2072015-04-22 12:20:22 +0100414template <typename ElfTypes>
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100415bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap&& map, std::string* error_msg) {
416 if (!map.IsValid()) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800417 // MemMap::Open should have already set an error.
418 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800419 return false;
420 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100421 map_ = std::move(map);
422 CHECK(map_.IsValid()) << file->GetPath();
423 CHECK(map_.Begin() != nullptr) << file->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800424
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100425 header_ = reinterpret_cast<Elf_Ehdr*>(map_.Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000426 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
427 || (ELFMAG1 != header_->e_ident[EI_MAG1])
428 || (ELFMAG2 != header_->e_ident[EI_MAG2])
429 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800430 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
431 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700432 file->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000433 header_->e_ident[EI_MAG0],
434 header_->e_ident[EI_MAG1],
435 header_->e_ident[EI_MAG2],
436 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800437 return false;
438 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700439 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
440 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800441 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700442 elf_class,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700443 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800444 header_->e_ident[EI_CLASS]);
445 return false;
446 }
447 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
448 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
449 ELFDATA2LSB,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700450 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800451 header_->e_ident[EI_CLASS]);
452 return false;
453 }
454 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
455 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
456 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700457 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800458 header_->e_ident[EI_CLASS]);
459 return false;
460 }
461 if (ET_DYN != header_->e_type) {
462 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
463 ET_DYN,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700464 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800465 header_->e_type);
466 return false;
467 }
468 if (EV_CURRENT != header_->e_version) {
469 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
470 EV_CURRENT,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700471 file->GetPath().c_str(),
Brian Carlstromc1409452014-02-26 14:06:23 -0800472 header_->e_version);
473 return false;
474 }
475 if (0 != header_->e_entry) {
476 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
477 0,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700478 file->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700479 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800480 return false;
481 }
482 if (0 == header_->e_phoff) {
483 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700484 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800485 return false;
486 }
487 if (0 == header_->e_shoff) {
488 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700489 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800490 return false;
491 }
492 if (0 == header_->e_ehsize) {
493 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700494 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800495 return false;
496 }
497 if (0 == header_->e_phentsize) {
498 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700499 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800500 return false;
501 }
502 if (0 == header_->e_phnum) {
503 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700504 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800505 return false;
506 }
507 if (0 == header_->e_shentsize) {
508 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700509 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800510 return false;
511 }
512 if (0 == header_->e_shnum) {
513 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700514 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800515 return false;
516 }
517 if (0 == header_->e_shstrndx) {
518 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700519 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800520 return false;
521 }
522 if (header_->e_shstrndx >= header_->e_shnum) {
523 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
524 header_->e_shstrndx,
525 header_->e_shnum,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700526 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800527 return false;
528 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800529
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800530 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800531 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700532 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
533 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800534 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700535 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800536 return false;
537 }
538 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700539 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
540 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800541 Size(),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700542 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -0800543 return false;
544 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800545 }
546 return true;
547}
548
David Srbecky533c2072015-04-22 12:20:22 +0100549template <typename ElfTypes>
550typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700551 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800552 return *header_;
553}
554
David Srbecky533c2072015-04-22 12:20:22 +0100555template <typename ElfTypes>
556uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700557 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
558 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800559 return program_headers_start_;
560}
561
David Srbecky533c2072015-04-22 12:20:22 +0100562template <typename ElfTypes>
563uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700564 CHECK(!program_header_only_); // Only used in "full" mode.
565 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800566 return section_headers_start_;
567}
568
David Srbecky533c2072015-04-22 12:20:22 +0100569template <typename ElfTypes>
570typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700571 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800572 return *dynamic_program_header_;
573}
574
David Srbecky533c2072015-04-22 12:20:22 +0100575template <typename ElfTypes>
576typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700577 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800578 return dynamic_section_start_;
579}
580
David Srbecky533c2072015-04-22 12:20:22 +0100581template <typename ElfTypes>
582typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
583 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700584 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800585 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000586 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700587 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800588 break;
589 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000590 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700591 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800592 break;
593 }
594 default: {
595 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700596 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800597 }
598 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800599}
600
David Srbecky533c2072015-04-22 12:20:22 +0100601template <typename ElfTypes>
602const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
603 Elf_Word section_type) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700604 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800605 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000606 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700607 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800608 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000609 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700610 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800611 }
612 default: {
613 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700614 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800615 }
616 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800617}
618
David Srbecky533c2072015-04-22 12:20:22 +0100619template <typename ElfTypes>
620const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
621 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700622 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800623 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700624 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800625 }
626 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700627 if (string_section_start == nullptr) {
628 return nullptr;
629 }
630 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800631}
632
Andreas Gampedaab38c2014-09-12 18:38:24 -0700633// WARNING: The following methods do not check for an error condition (non-existent hash section).
634// It is the caller's job to do this.
635
David Srbecky533c2072015-04-22 12:20:22 +0100636template <typename ElfTypes>
637typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800638 return hash_section_start_;
639}
640
David Srbecky533c2072015-04-22 12:20:22 +0100641template <typename ElfTypes>
642typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800643 return GetHashSectionStart()[0];
644}
645
David Srbecky533c2072015-04-22 12:20:22 +0100646template <typename ElfTypes>
647typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800648 return GetHashSectionStart()[1];
649}
650
David Srbecky533c2072015-04-22 12:20:22 +0100651template <typename ElfTypes>
652typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700653 if (i >= GetHashBucketNum()) {
654 *ok = false;
655 return 0;
656 }
657 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800658 // 0 is nbucket, 1 is nchain
659 return GetHashSectionStart()[2 + i];
660}
661
David Srbecky533c2072015-04-22 12:20:22 +0100662template <typename ElfTypes>
663typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600664 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700665 *ok = false;
666 return 0;
667 }
668 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800669 // 0 is nbucket, 1 is nchain, & chains are after buckets
670 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
671}
672
David Srbecky533c2072015-04-22 12:20:22 +0100673template <typename ElfTypes>
674typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800675 return GetHeader().e_phnum;
676}
677
David Srbecky533c2072015-04-22 12:20:22 +0100678template <typename ElfTypes>
679typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700680 CHECK_LT(i, GetProgramHeaderNum()) << file_path_; // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700681 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Vladimir Markoc09cd052018-08-23 16:36:36 +0100682 CHECK_LT(program_header, End());
Tong Shen62d1ca32014-09-03 17:24:56 -0700683 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800684}
685
David Srbecky533c2072015-04-22 12:20:22 +0100686template <typename ElfTypes>
687typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700688 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
689 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700690 if (program_header->p_type == type) {
691 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692 }
693 }
Alex Light3470ab42014-06-18 10:35:45 -0700694 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800695}
696
David Srbecky533c2072015-04-22 12:20:22 +0100697template <typename ElfTypes>
698typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699 return GetHeader().e_shnum;
700}
701
David Srbecky533c2072015-04-22 12:20:22 +0100702template <typename ElfTypes>
703typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800704 // Can only access arbitrary sections when we have the whole file, not just program header.
705 // Even if we Load(), it doesn't bring in all the sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700706 CHECK(!program_header_only_) << file_path_;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700707 if (i >= GetSectionHeaderNum()) {
708 return nullptr; // Failure condition.
709 }
Ian Rogers13735952014-10-08 12:43:28 -0700710 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700711 if (section_header >= End()) {
712 return nullptr; // Failure condition.
713 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700714 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800715}
716
David Srbecky533c2072015-04-22 12:20:22 +0100717template <typename ElfTypes>
718typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800719 // Can only access arbitrary sections when we have the whole file, not just program header.
720 // We could change this to switch on known types if they were detected during loading.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700721 CHECK(!program_header_only_) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700722 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
723 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700724 if (section_header->sh_type == type) {
725 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800726 }
727 }
Alex Light3470ab42014-06-18 10:35:45 -0700728 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800729}
730
731// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800732static unsigned elfhash(const char *_name) {
733 const unsigned char *name = (const unsigned char *) _name;
734 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800735
Brian Carlstromdf629502013-07-17 22:39:56 -0700736 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 h = (h << 4) + *name++;
738 g = h & 0xf0000000;
739 h ^= g;
740 h ^= g >> 24;
741 }
742 return h;
743}
744
David Srbecky533c2072015-04-22 12:20:22 +0100745template <typename ElfTypes>
746typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800747 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800748}
749
David Srbecky533c2072015-04-22 12:20:22 +0100750template <typename ElfTypes>
751const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
752 const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700753 // Check that we have a hash section.
754 if (GetHashSectionStart() == nullptr) {
755 return nullptr; // Failure condition.
756 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700757 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700758 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700759 // TODO: we need to change this to calculate base_address_ in ::Open,
760 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700761 return base_address_ + sym->st_value;
762 } else {
763 return nullptr;
764 }
765}
766
Andreas Gampedaab38c2014-09-12 18:38:24 -0700767// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
David Srbecky533c2072015-04-22 12:20:22 +0100768template <typename ElfTypes>
769const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
770 const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700771 if (GetHashBucketNum() == 0) {
772 // No dynamic symbols at all.
773 return nullptr;
774 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700775 Elf_Word hash = elfhash(symbol_name.c_str());
776 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700777 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700778 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700779 if (!ok) {
780 return nullptr;
781 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800782 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700783 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700784 if (symbol == nullptr) {
785 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800786 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700787 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
788 if (symbol_name == name) {
789 return symbol;
790 }
791 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
792 if (!ok) {
793 return nullptr;
794 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800795 }
Alex Light3470ab42014-06-18 10:35:45 -0700796 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800797}
798
David Srbecky533c2072015-04-22 12:20:22 +0100799template <typename ElfTypes>
800bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700801 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
802}
803
David Srbecky533c2072015-04-22 12:20:22 +0100804template <typename ElfTypes>
805typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800806 CHECK(IsSymbolSectionType(section_header.sh_type))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700807 << file_path_ << " " << section_header.sh_type;
808 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800809 return section_header.sh_size / section_header.sh_entsize;
810}
811
David Srbecky533c2072015-04-22 12:20:22 +0100812template <typename ElfTypes>
813typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700814 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700815 if (sym_start == nullptr) {
816 return nullptr;
817 }
818 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800819}
820
David Srbecky533c2072015-04-22 12:20:22 +0100821template <typename ElfTypes>
822typename ElfFileImpl<ElfTypes>::SymbolTable**
823ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700824 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800825 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000826 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800827 return &symtab_symbol_table_;
828 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000829 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800830 return &dynsym_symbol_table_;
831 }
832 default: {
833 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700834 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800835 }
836 }
837}
838
David Srbecky533c2072015-04-22 12:20:22 +0100839template <typename ElfTypes>
840typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
841 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700842 CHECK(!program_header_only_) << file_path_;
843 CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800844
845 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700846 if (*symbol_table != nullptr || build_map) {
847 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800848 DCHECK(build_map);
849 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -0700850 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700851 if (symbol_section == nullptr) {
852 return nullptr; // Failure condition.
853 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700854 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700855 if (string_section == nullptr) {
856 return nullptr; // Failure condition.
857 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800858 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700859 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700860 if (symbol == nullptr) {
861 return nullptr; // Failure condition.
862 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700863 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
864 ? ELF64_ST_TYPE(symbol->st_info)
865 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000866 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800867 continue;
868 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700869 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700870 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 continue;
872 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700873 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -0700874 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800875 if (!result.second) {
876 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700877 if ((symbol->st_value != result.first->second->st_value) ||
878 (symbol->st_size != result.first->second->st_size) ||
879 (symbol->st_info != result.first->second->st_info) ||
880 (symbol->st_other != result.first->second->st_other) ||
881 (symbol->st_shndx != result.first->second->st_shndx)) {
882 return nullptr; // Failure condition.
883 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800884 }
885 }
886 }
Alex Light3470ab42014-06-18 10:35:45 -0700887 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -0700888 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800889 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700890 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800891 }
892 return it->second;
893 }
894
895 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -0700896 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700897 if (symbol_section == nullptr) {
898 return nullptr;
899 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700900 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700901 if (string_section == nullptr) {
902 return nullptr;
903 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800904 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700905 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700906 if (symbol == nullptr) {
907 return nullptr; // Failure condition.
908 }
909 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700910 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800911 continue;
912 }
913 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700914 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800915 }
916 }
Alex Light3470ab42014-06-18 10:35:45 -0700917 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800918}
919
David Srbecky533c2072015-04-22 12:20:22 +0100920template <typename ElfTypes>
921typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
922 Elf_Word section_type, const std::string& symbol_name, bool build_map) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700923 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -0700924 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800925 return 0;
926 }
927 return symbol->st_value;
928}
929
David Srbecky533c2072015-04-22 12:20:22 +0100930template <typename ElfTypes>
931const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
932 Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700933 CHECK(!program_header_only_) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800934 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -0700935 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700936 return nullptr; // Failure condition.
937 }
938 if (i >= string_section.sh_size) {
939 return nullptr;
940 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800941 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700942 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800943 }
Ian Rogers13735952014-10-08 12:43:28 -0700944 uint8_t* strings = Begin() + string_section.sh_offset;
945 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700946 if (string >= End()) {
947 return nullptr;
948 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800949 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800950}
951
David Srbecky533c2072015-04-22 12:20:22 +0100952template <typename ElfTypes>
953typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700954 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800955}
956
David Srbecky533c2072015-04-22 12:20:22 +0100957template <typename ElfTypes>
958typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700959 CHECK_LT(i, GetDynamicNum()) << file_path_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800960 return *(GetDynamicSectionStart() + i);
961}
962
David Srbecky533c2072015-04-22 12:20:22 +0100963template <typename ElfTypes>
964typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700965 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
966 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -0700967 if (dyn->d_tag == type) {
968 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800969 }
970 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700971 return nullptr;
Alex Light53cb16b2014-06-12 11:26:29 -0700972}
973
David Srbecky533c2072015-04-22 12:20:22 +0100974template <typename ElfTypes>
975typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
Tong Shen62d1ca32014-09-03 17:24:56 -0700976 Elf_Dyn* dyn = FindDynamicByType(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700977 if (dyn == nullptr) {
Alex Light53cb16b2014-06-12 11:26:29 -0700978 return 0;
979 } else {
980 return dyn->d_un.d_val;
981 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800982}
983
David Srbecky533c2072015-04-22 12:20:22 +0100984template <typename ElfTypes>
985typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700986 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -0700987 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800988}
989
David Srbecky533c2072015-04-22 12:20:22 +0100990template <typename ElfTypes>
991typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700992 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
993 CHECK_NE(0U, section_header.sh_entsize) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800994 return section_header.sh_size / section_header.sh_entsize;
995}
996
David Srbecky533c2072015-04-22 12:20:22 +0100997template <typename ElfTypes>
998typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700999 CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1000 CHECK_LT(i, GetRelNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001001 return *(GetRelSectionStart(section_header) + i);
1002}
1003
David Srbecky533c2072015-04-22 12:20:22 +01001004template <typename ElfTypes>
1005typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001006 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001007 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001008}
1009
David Srbecky533c2072015-04-22 12:20:22 +01001010template <typename ElfTypes>
1011typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001012 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001013 return section_header.sh_size / section_header.sh_entsize;
1014}
1015
David Srbecky533c2072015-04-22 12:20:22 +01001016template <typename ElfTypes>
1017typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001018 CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1019 CHECK_LT(i, GetRelaNum(section_header)) << file_path_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001020 return *(GetRelaSectionStart(section_header) + i);
1021}
1022
David Srbecky533c2072015-04-22 12:20:22 +01001023template <typename ElfTypes>
Vladimir Marko3fc99032015-05-13 19:06:30 +01001024bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001025 uint8_t* vaddr_begin;
1026 return GetLoadedAddressRange(&vaddr_begin, size, error_msg);
1027}
1028
1029// Base on bionic phdr_table_get_load_size
1030template <typename ElfTypes>
1031bool ElfFileImpl<ElfTypes>::GetLoadedAddressRange(/*out*/uint8_t** vaddr_begin,
1032 /*out*/size_t* vaddr_size,
1033 /*out*/std::string* error_msg) const {
Vladimir Marko3fc99032015-05-13 19:06:30 +01001034 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();
Vladimir Markoc09cd052018-08-23 16:36:36 +01001052 *vaddr_begin = nullptr;
1053 *vaddr_size = static_cast<size_t>(-1);
Vladimir Marko3fc99032015-05-13 19:06:30 +01001054 return false;
1055 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001056 if (end_vaddr > max_vaddr) {
1057 max_vaddr = end_vaddr;
1058 }
1059 }
1060 min_vaddr = RoundDown(min_vaddr, kPageSize);
1061 max_vaddr = RoundUp(max_vaddr, kPageSize);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001062 CHECK_LT(min_vaddr, max_vaddr) << file_path_;
Vladimir Markoc09cd052018-08-23 16:36:36 +01001063 // Check that the range fits into the runtime address space.
1064 if (UNLIKELY(max_vaddr - 1u > std::numeric_limits<size_t>::max())) {
Vladimir Marko3fc99032015-05-13 19:06:30 +01001065 std::ostringstream oss;
Vladimir Markoc09cd052018-08-23 16:36:36 +01001066 oss << "Loaded range is 0x" << std::hex << min_vaddr << "-0x" << max_vaddr
1067 << " but maximum size_t is 0x" << std::numeric_limits<size_t>::max()
1068 << " for ELF file \"" << file_path_ << "\"";
Vladimir Marko3fc99032015-05-13 19:06:30 +01001069 *error_msg = oss.str();
Vladimir Markoc09cd052018-08-23 16:36:36 +01001070 *vaddr_begin = nullptr;
1071 *vaddr_size = static_cast<size_t>(-1);
Vladimir Marko3fc99032015-05-13 19:06:30 +01001072 return false;
1073 }
Vladimir Markoc09cd052018-08-23 16:36:36 +01001074 *vaddr_begin = reinterpret_cast<uint8_t*>(min_vaddr);
1075 *vaddr_size = dchecked_integral_cast<size_t>(max_vaddr - min_vaddr);
Vladimir Marko3fc99032015-05-13 19:06:30 +01001076 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001077}
1078
David Sehrc3e18952018-05-11 16:59:31 -07001079static InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags) {
1080 switch (e_machine) {
1081 case EM_ARM:
1082 return InstructionSet::kArm;
1083 case EM_AARCH64:
1084 return InstructionSet::kArm64;
1085 case EM_386:
1086 return InstructionSet::kX86;
1087 case EM_X86_64:
1088 return InstructionSet::kX86_64;
1089 case EM_MIPS: {
1090 if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 ||
1091 (e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) {
1092 return InstructionSet::kMips;
1093 } else if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6) {
1094 return InstructionSet::kMips64;
1095 }
1096 break;
1097 }
1098 }
1099 return InstructionSet::kNone;
1100}
1101
David Srbecky533c2072015-04-22 12:20:22 +01001102template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001103bool ElfFileImpl<ElfTypes>::Load(File* file,
1104 bool executable,
1105 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001106 /*inout*/MemMap* reservation,
1107 /*out*/std::string* error_msg) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001108 CHECK(program_header_only_) << file->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001109
1110 if (executable) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001111 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001112 if (elf_ISA != kRuntimeISA) {
1113 std::ostringstream oss;
1114 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1115 *error_msg = oss.str();
1116 return false;
1117 }
1118 }
1119
Jim_Guoa62a5882014-04-28 11:11:57 +08001120 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001121 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1122 Elf_Phdr* program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001123
1124 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001125 if (program_header->p_type == PT_DYNAMIC) {
1126 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001127 continue;
1128 }
1129
1130 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001131 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001132 continue;
1133 }
1134
1135 // Found something to load.
1136
Jim_Guoa62a5882014-04-28 11:11:57 +08001137 // Before load the actual segments, reserve a contiguous chunk
1138 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001139 // permissions. We'll then carve that up with the proper
1140 // permissions as we load the actual segments. If p_vaddr is
1141 // non-zero, the segments require the specific address specified,
1142 // which either was specified in the file because we already set
1143 // base_address_ after the first zero segment).
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001144 int64_t temp_file_length = file->GetLength();
Ian Rogerscdfcf372014-01-23 20:38:36 -08001145 if (temp_file_length < 0) {
1146 errno = -temp_file_length;
1147 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001148 file->GetPath().c_str(), file->Fd(), strerror(errno));
Ian Rogerscdfcf372014-01-23 20:38:36 -08001149 return false;
1150 }
1151 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001152 if (!reserved) {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001153 uint8_t* vaddr_begin;
1154 size_t vaddr_size;
1155 if (!GetLoadedAddressRange(&vaddr_begin, &vaddr_size, error_msg)) {
Vladimir Marko3fc99032015-05-13 19:06:30 +01001156 DCHECK(!error_msg->empty());
1157 return false;
1158 }
Vladimir Markoc09cd052018-08-23 16:36:36 +01001159 std::string reservation_name = "ElfFile reservation for " + file->GetPath();
1160 MemMap local_reservation = MemMap::MapAnonymous(
1161 reservation_name.c_str(),
1162 (reservation != nullptr) ? reservation->Begin() : nullptr,
1163 vaddr_size,
1164 PROT_NONE,
1165 low_4gb,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001166 /* reuse= */ false,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001167 reservation,
1168 error_msg);
1169 if (!local_reservation.IsValid()) {
Brian Carlstromc1409452014-02-26 14:06:23 -08001170 *error_msg = StringPrintf("Failed to allocate %s: %s",
Vladimir Markoc09cd052018-08-23 16:36:36 +01001171 reservation_name.c_str(),
1172 error_msg->c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001173 return false;
1174 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001175 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001176
Vladimir Markoc09cd052018-08-23 16:36:36 +01001177 // Base address is the difference of actual mapped location and the vaddr_begin.
1178 base_address_ = reinterpret_cast<uint8_t*>(
1179 static_cast<uintptr_t>(local_reservation.Begin() - vaddr_begin));
Igor Murashkin46774762014-10-22 11:37:02 -07001180 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1181 // dynamic memory address of where that object is actually mapped
1182 //
1183 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1184 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Vladimir Markoc09cd052018-08-23 16:36:36 +01001185 segments_.push_back(std::move(local_reservation));
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001186 }
1187 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001188 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001189 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001190 }
Ian Rogers13735952014-10-08 12:43:28 -07001191 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001192 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001193 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001194 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001195 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001196 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001197 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001198 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001199 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001200 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001201 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001202 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001203 if (writable_) {
1204 prot |= PROT_WRITE;
1205 flags |= MAP_SHARED;
1206 } else {
1207 flags |= MAP_PRIVATE;
1208 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001209 if (program_header->p_filesz > program_header->p_memsz) {
1210 *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1211 static_cast<uint64_t>(program_header->p_filesz),
1212 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001213 file->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001214 return false;
1215 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001216 if (program_header->p_filesz < program_header->p_memsz &&
1217 !IsAligned<kPageSize>(program_header->p_filesz)) {
1218 *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1219 " < %" PRIu64 "): %s",
1220 static_cast<uint64_t>(program_header->p_filesz),
1221 static_cast<uint64_t>(program_header->p_memsz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001222 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001223 return false;
1224 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001225 if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1226 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1227 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1228 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001229 file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001230 return false;
1231 }
Vladimir Marko5c42c292015-02-25 12:02:49 +00001232 if (program_header->p_filesz != 0u) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001233 MemMap segment =
Vladimir Marko5c42c292015-02-25 12:02:49 +00001234 MemMap::MapFileAtAddress(p_vaddr,
1235 program_header->p_filesz,
Mathieu Chartier42bddce2015-11-09 15:16:56 -08001236 prot,
1237 flags,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001238 file->Fd(),
Vladimir Marko5c42c292015-02-25 12:02:49 +00001239 program_header->p_offset,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001240 /* low_4gb= */ false,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001241 file->GetPath().c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001242 /* reuse= */ true, // implies MAP_FIXED
1243 /* reservation= */ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001244 error_msg);
1245 if (!segment.IsValid()) {
Vladimir Marko5c42c292015-02-25 12:02:49 +00001246 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001247 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001248 return false;
1249 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001250 if (segment.Begin() != p_vaddr) {
Vladimir Marko5c42c292015-02-25 12:02:49 +00001251 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1252 "instead mapped to %p",
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001253 i, file->GetPath().c_str(), p_vaddr, segment.Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001254 return false;
1255 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001256 segments_.push_back(std::move(segment));
Vladimir Marko5c42c292015-02-25 12:02:49 +00001257 }
1258 if (program_header->p_filesz < program_header->p_memsz) {
1259 std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001260 static_cast<uint64_t>(i), file->GetPath().c_str());
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001261 MemMap segment = MemMap::MapAnonymous(name.c_str(),
1262 p_vaddr + program_header->p_filesz,
1263 program_header->p_memsz - program_header->p_filesz,
1264 prot,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001265 /* low_4gb= */ false,
1266 /* reuse= */ true,
1267 /* reservation= */ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001268 error_msg);
1269 if (!segment.IsValid()) {
Vladimir Marko5c42c292015-02-25 12:02:49 +00001270 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001271 i, file->GetPath().c_str(), error_msg->c_str());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001272 return false;
1273 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001274 if (segment.Begin() != p_vaddr) {
Vladimir Marko5c42c292015-02-25 12:02:49 +00001275 *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1276 "at expected address %p, instead mapped to %p",
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001277 i, file->GetPath().c_str(), p_vaddr, segment.Begin());
Vladimir Marko5c42c292015-02-25 12:02:49 +00001278 return false;
1279 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001280 segments_.push_back(std::move(segment));
Vladimir Marko5c42c292015-02-25 12:02:49 +00001281 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001282 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001283
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001284 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001285 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001286 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1287 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001288 file->GetPath().c_str());
Andreas Gampedaab38c2014-09-12 18:38:24 -07001289 return false;
1290 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001291 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001292
Tong Shen62d1ca32014-09-03 17:24:56 -07001293 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1294 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001295 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001296 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001297 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001298 if (!ValidPointer(d_ptr)) {
1299 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001300 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001301 return false;
1302 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001303 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001304 break;
1305 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001306 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001307 if (!ValidPointer(d_ptr)) {
1308 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001309 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001310 return false;
1311 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001312 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1313 break;
1314 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001315 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001316 if (!ValidPointer(d_ptr)) {
1317 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001318 d_ptr, file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001319 return false;
1320 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001321 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001322 break;
1323 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001324 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001325 if (GetDynamicNum() != i+1) {
1326 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1327 "expected %d as implied by size of PT_DYNAMIC segment in %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001328 i + 1, GetDynamicNum(), file->GetPath().c_str());
Brian Carlstromc1409452014-02-26 14:06:23 -08001329 return false;
1330 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001331 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001332 }
1333 }
1334 }
1335
Andreas Gampedaab38c2014-09-12 18:38:24 -07001336 // Check for the existence of some sections.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001337 if (!CheckSectionsExist(file, error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001338 return false;
1339 }
1340
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001341 return true;
1342}
1343
David Srbecky533c2072015-04-22 12:20:22 +01001344template <typename ElfTypes>
1345bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001346 for (const MemMap& segment : segments_) {
1347 if (segment.Begin() <= start && start < segment.End()) {
Brian Carlstromc1409452014-02-26 14:06:23 -08001348 return true;
1349 }
1350 }
1351 return false;
1352}
1353
Alex Light3470ab42014-06-18 10:35:45 -07001354
David Srbecky533c2072015-04-22 12:20:22 +01001355template <typename ElfTypes>
1356typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1357 const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001358 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001359 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001360 if (shstrtab_sec == nullptr) {
1361 return nullptr;
1362 }
Alex Light3470ab42014-06-18 10:35:45 -07001363 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001364 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001365 if (shdr == nullptr) {
1366 return nullptr;
1367 }
1368 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001369 if (sec_name == nullptr) {
1370 continue;
1371 }
1372 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001373 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001374 }
1375 }
1376 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001377}
1378
David Srbecky533c2072015-04-22 12:20:22 +01001379template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001380bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001381 if (base_address_delta == 0) {
1382 return true;
1383 }
David Srbeckyf8980872015-05-22 17:04:47 +01001384 return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1385 ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1386 ApplyOatPatchesTo(".debug_line", base_address_delta);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001387}
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001388
David Srbecky533c2072015-04-22 12:20:22 +01001389template <typename ElfTypes>
1390bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
David Srbeckyf8980872015-05-22 17:04:47 +01001391 const char* target_section_name, Elf_Addr delta) {
1392 auto target_section = FindSectionByName(target_section_name);
1393 if (target_section == nullptr) {
1394 return true;
1395 }
1396 std::string patches_name = target_section_name + std::string(".oat_patches");
1397 auto patches_section = FindSectionByName(patches_name.c_str());
David Srbecky2f6cdb02015-04-11 00:17:53 +01001398 if (patches_section == nullptr) {
David Srbeckyf8980872015-05-22 17:04:47 +01001399 LOG(ERROR) << patches_name << " section not found.";
Alex Light3470ab42014-06-18 10:35:45 -07001400 return false;
1401 }
David Srbecky2f6cdb02015-04-11 00:17:53 +01001402 if (patches_section->sh_type != SHT_OAT_PATCH) {
David Srbeckyf8980872015-05-22 17:04:47 +01001403 LOG(ERROR) << "Unexpected type of " << patches_name;
Alex Light3470ab42014-06-18 10:35:45 -07001404 return false;
1405 }
David Srbeckyf8980872015-05-22 17:04:47 +01001406 ApplyOatPatches(
David Srbecky2f6cdb02015-04-11 00:17:53 +01001407 Begin() + patches_section->sh_offset,
1408 Begin() + patches_section->sh_offset + patches_section->sh_size,
David Srbeckyf8980872015-05-22 17:04:47 +01001409 delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +01001410 Begin() + target_section->sh_offset,
David Srbeckyf8980872015-05-22 17:04:47 +01001411 Begin() + target_section->sh_offset + target_section->sh_size);
David Srbecky2f6cdb02015-04-11 00:17:53 +01001412 return true;
1413}
1414
David Srbeckyf8980872015-05-22 17:04:47 +01001415// Apply LEB128 encoded patches to given section.
David Srbecky533c2072015-04-22 12:20:22 +01001416template <typename ElfTypes>
David Srbeckyf8980872015-05-22 17:04:47 +01001417void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1418 const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky533c2072015-04-22 12:20:22 +01001419 uint8_t* to_patch, const uint8_t* to_patch_end) {
Andreas Gampec55bb392018-09-21 00:02:02 +00001420 using UnalignedAddress __attribute__((__aligned__(1))) = Elf_Addr;
David Srbeckyf8980872015-05-22 17:04:47 +01001421 while (patches < patches_end) {
1422 to_patch += DecodeUnsignedLeb128(&patches);
1423 DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1424 DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1425 *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
David Srbecky2f6cdb02015-04-11 00:17:53 +01001426 }
Alex Light3470ab42014-06-18 10:35:45 -07001427}
Mark Mendellae9fd932014-02-10 16:14:35 -08001428
David Srbecky533c2072015-04-22 12:20:22 +01001429template <typename ElfTypes>
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001430bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001431 // ELF files produced by MCLinker look roughly like this
1432 //
1433 // +------------+
1434 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
1435 // +------------+
1436 // | Elf_Phdr | program headers
1437 // | Elf_Phdr |
1438 // | ... |
1439 // | Elf_Phdr |
1440 // +------------+
1441 // | section | mixture of needed and unneeded sections
1442 // +------------+
1443 // | section |
1444 // +------------+
1445 // | ... |
1446 // +------------+
1447 // | section |
1448 // +------------+
1449 // | Elf_Shdr | section headers
1450 // | Elf_Shdr |
1451 // | ... | contains offset to section start
1452 // | Elf_Shdr |
1453 // +------------+
1454 //
1455 // To strip:
1456 // - leave the Elf_Ehdr and Elf_Phdr values in place.
1457 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1458 // - move the sections are keeping up to fill in gaps of sections we want to strip
1459 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1460 // - truncate rest of file
1461 //
1462
1463 std::vector<Elf_Shdr> section_headers;
1464 std::vector<Elf_Word> section_headers_original_indexes;
1465 section_headers.reserve(GetSectionHeaderNum());
1466
1467
1468 Elf_Shdr* string_section = GetSectionNameStringSection();
1469 CHECK(string_section != nullptr);
1470 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1471 Elf_Shdr* sh = GetSectionHeader(i);
1472 CHECK(sh != nullptr);
1473 const char* name = GetString(*string_section, sh->sh_name);
1474 if (name == nullptr) {
1475 CHECK_EQ(0U, i);
1476 section_headers.push_back(*sh);
1477 section_headers_original_indexes.push_back(0);
1478 continue;
1479 }
Andreas Gampe9186ced2016-12-12 14:28:21 -08001480 if (android::base::StartsWith(name, ".debug")
Tong Shen62d1ca32014-09-03 17:24:56 -07001481 || (strcmp(name, ".strtab") == 0)
1482 || (strcmp(name, ".symtab") == 0)) {
1483 continue;
1484 }
1485 section_headers.push_back(*sh);
1486 section_headers_original_indexes.push_back(i);
1487 }
1488 CHECK_NE(0U, section_headers.size());
1489 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1490
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001491 // section 0 is the null section, sections start at offset of first section
Tong Shen62d1ca32014-09-03 17:24:56 -07001492 CHECK(GetSectionHeader(1) != nullptr);
1493 Elf_Off offset = GetSectionHeader(1)->sh_offset;
1494 for (size_t i = 1; i < section_headers.size(); i++) {
1495 Elf_Shdr& new_sh = section_headers[i];
1496 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1497 CHECK(old_sh != nullptr);
1498 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1499 if (old_sh->sh_addralign > 1) {
1500 offset = RoundUp(offset, old_sh->sh_addralign);
1501 }
1502 if (old_sh->sh_offset == offset) {
1503 // already in place
1504 offset += old_sh->sh_size;
1505 continue;
1506 }
1507 // shift section earlier
1508 memmove(Begin() + offset,
1509 Begin() + old_sh->sh_offset,
1510 old_sh->sh_size);
1511 new_sh.sh_offset = offset;
1512 offset += old_sh->sh_size;
1513 }
1514
1515 Elf_Off shoff = offset;
1516 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1517 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1518 offset += section_headers_size_in_bytes;
1519
1520 GetHeader().e_shnum = section_headers.size();
1521 GetHeader().e_shoff = shoff;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001522 int result = ftruncate(file->Fd(), offset);
Tong Shen62d1ca32014-09-03 17:24:56 -07001523 if (result != 0) {
1524 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001525 file->GetPath().c_str(), strerror(errno));
Tong Shen62d1ca32014-09-03 17:24:56 -07001526 return false;
1527 }
1528 return true;
1529}
1530
1531static const bool DEBUG_FIXUP = false;
1532
David Srbecky533c2072015-04-22 12:20:22 +01001533template <typename ElfTypes>
1534bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001535 if (!FixupDynamic(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001536 LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001537 return false;
1538 }
1539 if (!FixupSectionHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001540 LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001541 return false;
1542 }
1543 if (!FixupProgramHeaders(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001544 LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001545 return false;
1546 }
1547 if (!FixupSymbols(base_address, true)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001548 LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001549 return false;
1550 }
1551 if (!FixupSymbols(base_address, false)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001552 LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001553 return false;
1554 }
1555 if (!FixupRelocations(base_address)) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001556 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001557 return false;
1558 }
Andreas Gampe3c54b002015-04-07 16:09:30 -07001559 static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1560 if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001561 LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001562 return false;
1563 }
1564 return true;
1565}
1566
David Srbecky533c2072015-04-22 12:20:22 +01001567template <typename ElfTypes>
1568bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001569 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1570 Elf_Dyn& elf_dyn = GetDynamic(i);
1571 Elf_Word d_tag = elf_dyn.d_tag;
1572 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1573 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1574 if (DEBUG_FIXUP) {
1575 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%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>(d_ptr),
1578 static_cast<uint64_t>(d_ptr + base_address));
1579 }
1580 d_ptr += base_address;
1581 elf_dyn.d_un.d_ptr = d_ptr;
1582 }
1583 }
1584 return true;
1585}
1586
David Srbecky533c2072015-04-22 12:20:22 +01001587template <typename ElfTypes>
1588bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001589 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1590 Elf_Shdr* sh = GetSectionHeader(i);
1591 CHECK(sh != nullptr);
1592 // 0 implies that the section will not exist in the memory of the process
1593 if (sh->sh_addr == 0) {
1594 continue;
1595 }
1596 if (DEBUG_FIXUP) {
1597 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001598 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001599 static_cast<uint64_t>(sh->sh_addr),
1600 static_cast<uint64_t>(sh->sh_addr + base_address));
1601 }
1602 sh->sh_addr += base_address;
1603 }
1604 return true;
1605}
1606
David Srbecky533c2072015-04-22 12:20:22 +01001607template <typename ElfTypes>
1608bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001609 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1610 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1611 Elf_Phdr* ph = GetProgramHeader(i);
1612 CHECK(ph != nullptr);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001613 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001614 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001615 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001616 if (DEBUG_FIXUP) {
1617 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001618 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001619 static_cast<uint64_t>(ph->p_vaddr),
1620 static_cast<uint64_t>(ph->p_vaddr + base_address));
1621 }
1622 ph->p_vaddr += base_address;
1623 ph->p_paddr += base_address;
1624 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001625 << file_path_ << " i=" << i;
Tong Shen62d1ca32014-09-03 17:24:56 -07001626 }
1627 return true;
1628}
1629
David Srbecky533c2072015-04-22 12:20:22 +01001630template <typename ElfTypes>
1631bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001632 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1633 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1634 Elf_Shdr* symbol_section = FindSectionByType(section_type);
1635 if (symbol_section == nullptr) {
1636 // file is missing optional .symtab
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001637 CHECK(!dynamic) << file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -07001638 return true;
1639 }
1640 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1641 Elf_Sym* symbol = GetSymbol(section_type, i);
1642 CHECK(symbol != nullptr);
1643 if (symbol->st_value != 0) {
1644 if (DEBUG_FIXUP) {
1645 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001646 file_path_.c_str(), i,
Tong Shen62d1ca32014-09-03 17:24:56 -07001647 static_cast<uint64_t>(symbol->st_value),
1648 static_cast<uint64_t>(symbol->st_value + base_address));
1649 }
1650 symbol->st_value += base_address;
1651 }
1652 }
1653 return true;
1654}
1655
David Srbecky533c2072015-04-22 12:20:22 +01001656template <typename ElfTypes>
1657bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001658 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1659 Elf_Shdr* sh = GetSectionHeader(i);
1660 CHECK(sh != nullptr);
1661 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001662 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1663 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001664 if (DEBUG_FIXUP) {
1665 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001666 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001667 static_cast<uint64_t>(rel.r_offset),
1668 static_cast<uint64_t>(rel.r_offset + base_address));
1669 }
1670 rel.r_offset += base_address;
1671 }
1672 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001673 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1674 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07001675 if (DEBUG_FIXUP) {
1676 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001677 file_path_.c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07001678 static_cast<uint64_t>(rela.r_offset),
1679 static_cast<uint64_t>(rela.r_offset + base_address));
1680 }
1681 rela.r_offset += base_address;
1682 }
1683 }
1684 }
1685 return true;
1686}
1687
1688// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +01001689template class ElfFileImpl<ElfTypes32>;
1690template class ElfFileImpl<ElfTypes64>;
Tong Shen62d1ca32014-09-03 17:24:56 -07001691
Ian Rogersd4c4d952014-10-16 20:31:53 -07001692ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001693}
1694
Ian Rogersd4c4d952014-10-16 20:31:53 -07001695ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001696}
1697
1698ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001699 // Should never have 32 and 64-bit impls.
1700 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001701}
1702
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001703ElfFile* ElfFile::Open(File* file,
1704 bool writable,
1705 bool program_header_only,
1706 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001707 /*out*/std::string* error_msg) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001708 if (file->GetLength() < EI_NIDENT) {
1709 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1710 file->GetPath().c_str());
1711 return nullptr;
1712 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001713 MemMap map = MemMap::MapFile(EI_NIDENT,
1714 PROT_READ,
1715 MAP_PRIVATE,
1716 file->Fd(),
1717 0,
1718 low_4gb,
1719 file->GetPath().c_str(),
1720 error_msg);
1721 if (!map.IsValid() || map.Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001722 return nullptr;
1723 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001724 uint8_t* header = map.Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001725 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001726 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1727 writable,
1728 program_header_only,
1729 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001730 error_msg);
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001731 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001732 return nullptr;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001733 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001734 return new ElfFile(elf_file_impl);
1735 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001736 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1737 writable,
1738 program_header_only,
1739 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001740 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001741 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001742 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001743 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001744 return new ElfFile(elf_file_impl);
1745 } else {
1746 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1747 ELFCLASS32, ELFCLASS64,
1748 file->GetPath().c_str(),
1749 header[EI_CLASS]);
1750 return nullptr;
1751 }
1752}
1753
Vladimir Markoc09cd052018-08-23 16:36:36 +01001754ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, /*out*/std::string* error_msg) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001755 // low_4gb support not required for this path.
1756 constexpr bool low_4gb = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001757 if (file->GetLength() < EI_NIDENT) {
1758 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1759 file->GetPath().c_str());
1760 return nullptr;
1761 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001762 MemMap map = MemMap::MapFile(EI_NIDENT,
1763 PROT_READ,
1764 MAP_PRIVATE,
1765 file->Fd(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001766 /* start= */ 0,
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001767 low_4gb,
1768 file->GetPath().c_str(),
1769 error_msg);
1770 if (!map.IsValid() || map.Size() != EI_NIDENT) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001771 return nullptr;
1772 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001773 uint8_t* header = map.Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07001774 if (header[EI_CLASS] == ELFCLASS64) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001775 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1776 mmap_prot,
1777 mmap_flags,
1778 low_4gb,
1779 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001780 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001781 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001782 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001783 return new ElfFile(elf_file_impl);
1784 } else if (header[EI_CLASS] == ELFCLASS32) {
Mathieu Chartierbcb6a722016-03-08 16:49:58 -08001785 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1786 mmap_prot,
1787 mmap_flags,
1788 low_4gb,
1789 error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07001790 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001791 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001792 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001793 return new ElfFile(elf_file_impl);
1794 } else {
1795 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1796 ELFCLASS32, ELFCLASS64,
1797 file->GetPath().c_str(),
1798 header[EI_CLASS]);
1799 return nullptr;
1800 }
1801}
1802
1803#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001804 if (elf64_.get() != nullptr) { \
1805 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001806 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07001807 DCHECK(elf32_.get() != nullptr); \
1808 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07001809 }
1810
Vladimir Markoc09cd052018-08-23 16:36:36 +01001811bool ElfFile::Load(File* file,
1812 bool executable,
1813 bool low_4gb,
1814 /*inout*/MemMap* reservation,
1815 /*out*/std::string* error_msg) {
1816 DELEGATE_TO_IMPL(Load, file, executable, low_4gb, reservation, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001817}
1818
Ian Rogers13735952014-10-08 12:43:28 -07001819const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001820 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1821}
1822
1823size_t ElfFile::Size() const {
1824 DELEGATE_TO_IMPL(Size);
1825}
1826
Ian Rogers13735952014-10-08 12:43:28 -07001827uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001828 DELEGATE_TO_IMPL(Begin);
1829}
1830
Ian Rogers13735952014-10-08 12:43:28 -07001831uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07001832 DELEGATE_TO_IMPL(End);
1833}
1834
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001835const std::string& ElfFile::GetFilePath() const {
1836 DELEGATE_TO_IMPL(GetFilePath);
Tong Shen62d1ca32014-09-03 17:24:56 -07001837}
1838
Alex Light0eb76d22015-08-11 18:03:47 -07001839bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1840 uint64_t* size) const {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001841 if (elf32_.get() == nullptr) {
1842 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001843
Ian Rogersd4c4d952014-10-16 20:31:53 -07001844 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1845 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001846 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001847 }
1848 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001849 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001850 }
1851 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001852 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001853 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001854 return true;
1855 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001856 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1857 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001858 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001859 }
1860 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001861 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001862 }
1863 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001864 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07001865 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001866 return true;
1867 }
1868}
1869
Jeff Hao24ec0282016-03-17 21:32:45 -07001870bool ElfFile::HasSection(const std::string& name) const {
1871 if (elf64_.get() != nullptr) {
1872 return elf64_->FindSectionByName(name) != nullptr;
1873 } else {
1874 return elf32_->FindSectionByName(name) != nullptr;
1875 }
1876}
1877
Tong Shen62d1ca32014-09-03 17:24:56 -07001878uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1879 const std::string& symbol_name,
1880 bool build_map) {
1881 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1882}
1883
Vladimir Marko3fc99032015-05-13 19:06:30 +01001884bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1885 DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07001886}
1887
1888bool ElfFile::Strip(File* file, std::string* error_msg) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001889 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb=*/false, error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -07001890 if (elf_file.get() == nullptr) {
1891 return false;
1892 }
1893
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001894 if (elf_file->elf64_.get() != nullptr) {
1895 return elf_file->elf64_->Strip(file, error_msg);
1896 } else {
1897 return elf_file->elf32_->Strip(file, error_msg);
1898 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001899}
1900
Andreas Gampe3c54b002015-04-07 16:09:30 -07001901bool ElfFile::Fixup(uint64_t base_address) {
1902 if (elf64_.get() != nullptr) {
1903 return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1904 } else {
1905 DCHECK(elf32_.get() != nullptr);
1906 CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1907 return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1908 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001909 DELEGATE_TO_IMPL(Fixup, base_address);
1910}
1911
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001912} // namespace art