Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
Nicolas Geoffray | a7f198c | 2014-03-10 11:12:54 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 25 | #include "android-base/strings.h" |
| 26 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 27 | #include "arch/instruction_set.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 28 | #include "base/leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 31 | #include "base/utils.h" |
David Srbecky | 5092811 | 2019-03-22 17:06:28 +0000 | [diff] [blame] | 32 | #include "elf/elf_utils.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 33 | #include "elf_file_impl.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 39 | template <typename ElfTypes> |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 40 | ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable, bool program_header_only) |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 41 | : writable_(writable), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 42 | program_header_only_(program_header_only), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 43 | 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 Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 55 | dynsym_symbol_table_(nullptr) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 56 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 57 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 58 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 59 | template <typename ElfTypes> |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 60 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file, |
| 61 | bool writable, |
| 62 | bool program_header_only, |
| 63 | bool low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 64 | std::string* error_msg) { |
| 65 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file( |
| 66 | new ElfFileImpl<ElfTypes>(file, writable, program_header_only)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 67 | int prot; |
| 68 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 69 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 70 | prot = PROT_READ | PROT_WRITE; |
| 71 | flags = MAP_SHARED; |
| 72 | } else { |
| 73 | prot = PROT_READ; |
| 74 | flags = MAP_PRIVATE; |
| 75 | } |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 76 | if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | return elf_file.release(); |
| 80 | } |
| 81 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 82 | template <typename ElfTypes> |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 83 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file, |
| 84 | int prot, |
| 85 | int flags, |
| 86 | bool low_4gb, |
| 87 | std::string* error_msg) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 88 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 89 | new ElfFileImpl<ElfTypes>(file, (prot & PROT_WRITE) != 0, /* program_header_only= */ false)); |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 90 | if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 91 | return nullptr; |
| 92 | } |
| 93 | return elf_file.release(); |
| 94 | } |
| 95 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 96 | template <typename ElfTypes> |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 97 | bool 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 Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 103 | if (temp_file_length < 0) { |
| 104 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 105 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 106 | file->GetPath().c_str(), file->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 107 | return false; |
| 108 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 109 | size_t file_length = static_cast<size_t>(temp_file_length); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 110 | if (file_length < sizeof(Elf_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 111 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of " |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 112 | "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 113 | file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 117 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 118 | // first just map ELF header to get program header size information |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 119 | size_t elf_header_size = sizeof(Elf_Ehdr); |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 120 | if (!SetMap(file, |
| 121 | MemMap::MapFile(elf_header_size, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 122 | prot, |
| 123 | flags, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 124 | file->Fd(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 125 | 0, |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 126 | low_4gb, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 127 | file->GetPath().c_str(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 128 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 129 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 130 | 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 Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 134 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 135 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 136 | "header of %zd bytes: '%s'", file_length, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 137 | sizeof(Elf_Ehdr), file->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 140 | if (!SetMap(file, |
| 141 | MemMap::MapFile(program_header_size, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 142 | prot, |
| 143 | flags, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 144 | file->Fd(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 145 | 0, |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 146 | low_4gb, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 147 | file->GetPath().c_str(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 148 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 149 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 150 | *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | } else { |
| 154 | // otherwise map entire file |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 155 | if (!SetMap(file, |
| 156 | MemMap::MapFile(file->GetLength(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 157 | prot, |
| 158 | flags, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 159 | file->Fd(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 160 | 0, |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 161 | low_4gb, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 162 | file->GetPath().c_str(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 163 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 164 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 165 | *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 170 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 176 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 177 | // Setup section headers. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 178 | if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Find shstrtab. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 183 | Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 184 | if (shstrtab_section_header == nullptr) { |
| 185 | *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 186 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 189 | |
| 190 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 191 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 192 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 193 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 194 | file->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 198 | if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 199 | reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 202 | |
| 203 | // Find other sections from section headers |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 204 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 205 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 206 | if (section_header == nullptr) { |
| 207 | *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 208 | i, file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | switch (section_header->sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 212 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 213 | if (!CheckAndSet(section_header->sh_offset, "symtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 214 | reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 217 | break; |
| 218 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 219 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 220 | if (!CheckAndSet(section_header->sh_offset, "dynsym", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 221 | reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 224 | break; |
| 225 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 226 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 227 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 228 | 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 Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 233 | reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 237 | } else { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 238 | // 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 Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 242 | reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 249 | case SHT_DYNAMIC: { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 250 | if (reinterpret_cast<uint8_t*>(dynamic_section_start_) != |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 251 | Begin() + section_header->sh_offset) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 252 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 253 | << file->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 254 | << reinterpret_cast<void*>(dynamic_section_start_) |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 255 | << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | break; |
| 259 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 260 | case SHT_HASH: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 261 | if (!CheckAndSet(section_header->sh_offset, "hash section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 262 | reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | } |
| 268 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 269 | |
| 270 | // Check for the existence of some sections. |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 271 | if (!CheckSectionsExist(file, error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 274 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 275 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 279 | template <typename ElfTypes> |
| 280 | ElfFileImpl<ElfTypes>::~ElfFileImpl() { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 281 | delete symtab_symbol_table_; |
| 282 | delete dynsym_symbol_table_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 283 | } |
| 284 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 285 | template <typename ElfTypes> |
| 286 | bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label, |
| 287 | uint8_t** target, std::string* error_msg) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 288 | if (Begin() + offset >= End()) { |
| 289 | *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 290 | file_path_.c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | *target = Begin() + offset; |
| 294 | return true; |
| 295 | } |
| 296 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 297 | template <typename ElfTypes> |
| 298 | bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source, |
| 299 | const uint8_t* target) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 300 | // 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 Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 306 | Elf_Shdr* source_section = nullptr; |
| 307 | Elf_Word target_index = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 308 | bool target_found = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 309 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 310 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 311 | |
| 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 Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 330 | template <typename ElfTypes> |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 331 | bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 332 | if (!program_header_only_) { |
| 333 | // If in full mode, need section headers. |
| 334 | if (section_headers_start_ == nullptr) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 335 | *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 336 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 343 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 344 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 350 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 351 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 359 | *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
| 363 | // The symtab should link to the strtab. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 364 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_), |
| 365 | reinterpret_cast<const uint8_t*>(strtab_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 366 | *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 367 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // We always need a dynstr & dynsym. |
| 373 | if (dynstr_section_start_ == nullptr) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 374 | *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | if (dynsym_section_start_ == nullptr) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 378 | *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 379 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 385 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // And the hash section should be linking to the dynsym. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 390 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_), |
| 391 | reinterpret_cast<const uint8_t*>(dynsym_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 392 | *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 393 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 397 | // 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 404 | if (offset >= file->GetLength()) { |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 405 | *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 406 | file->GetPath().c_str()); |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 414 | template <typename ElfTypes> |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 415 | bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap&& map, std::string* error_msg) { |
| 416 | if (!map.IsValid()) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 417 | // MemMap::Open should have already set an error. |
| 418 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 419 | return false; |
| 420 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 421 | map_ = std::move(map); |
| 422 | CHECK(map_.IsValid()) << file->GetPath(); |
| 423 | CHECK(map_.Begin() != nullptr) << file->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 424 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 425 | header_ = reinterpret_cast<Elf_Ehdr*>(map_.Begin()); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 426 | 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 Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 430 | *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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 432 | file->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 433 | header_->e_ident[EI_MAG0], |
| 434 | header_->e_ident[EI_MAG1], |
| 435 | header_->e_ident[EI_MAG2], |
| 436 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 437 | return false; |
| 438 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 439 | uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32; |
| 440 | if (elf_class != header_->e_ident[EI_CLASS]) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 441 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 442 | elf_class, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 443 | file->GetPath().c_str(), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 444 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 450 | file->GetPath().c_str(), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 451 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 457 | file->GetPath().c_str(), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 458 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 464 | file->GetPath().c_str(), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 465 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 471 | file->GetPath().c_str(), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 472 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 478 | file->GetPath().c_str(), |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 479 | static_cast<int32_t>(header_->e_entry)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | if (0 == header_->e_phoff) { |
| 483 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 484 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | if (0 == header_->e_shoff) { |
| 488 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 489 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | if (0 == header_->e_ehsize) { |
| 493 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 494 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 495 | return false; |
| 496 | } |
| 497 | if (0 == header_->e_phentsize) { |
| 498 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 499 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | if (0 == header_->e_phnum) { |
| 503 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 504 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | if (0 == header_->e_shentsize) { |
| 508 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 509 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | if (0 == header_->e_shnum) { |
| 513 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 514 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | if (0 == header_->e_shstrndx) { |
| 518 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 519 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 520 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 526 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 527 | return false; |
| 528 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 529 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 530 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 531 | if (header_->e_phoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 532 | *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s", |
| 533 | static_cast<uint64_t>(header_->e_phoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 534 | Size(), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 535 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | if (header_->e_shoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 539 | *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s", |
| 540 | static_cast<uint64_t>(header_->e_shoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 541 | Size(), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 542 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 543 | return false; |
| 544 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 545 | } |
| 546 | return true; |
| 547 | } |
| 548 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 549 | template <typename ElfTypes> |
| 550 | typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 551 | CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 552 | return *header_; |
| 553 | } |
| 554 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 555 | template <typename ElfTypes> |
| 556 | uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 557 | CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity |
| 558 | // check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 559 | return program_headers_start_; |
| 560 | } |
| 561 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 562 | template <typename ElfTypes> |
| 563 | uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 564 | CHECK(!program_header_only_); // Only used in "full" mode. |
| 565 | CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 566 | return section_headers_start_; |
| 567 | } |
| 568 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 569 | template <typename ElfTypes> |
| 570 | typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 571 | CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 572 | return *dynamic_program_header_; |
| 573 | } |
| 574 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 575 | template <typename ElfTypes> |
| 576 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 577 | CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 578 | return dynamic_section_start_; |
| 579 | } |
| 580 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 581 | template <typename ElfTypes> |
| 582 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart( |
| 583 | Elf_Word section_type) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 584 | CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 585 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 586 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 587 | return symtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 588 | break; |
| 589 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 590 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 591 | return dynsym_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | default: { |
| 595 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 596 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 599 | } |
| 600 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 601 | template <typename ElfTypes> |
| 602 | const char* ElfFileImpl<ElfTypes>::GetStringSectionStart( |
| 603 | Elf_Word section_type) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 604 | CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 605 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 606 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 607 | return strtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 608 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 609 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 610 | return dynstr_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 611 | } |
| 612 | default: { |
| 613 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 614 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 615 | } |
| 616 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 617 | } |
| 618 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 619 | template <typename ElfTypes> |
| 620 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type, |
| 621 | Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 622 | CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 623 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 624 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 625 | } |
| 626 | const char* string_section_start = GetStringSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 627 | if (string_section_start == nullptr) { |
| 628 | return nullptr; |
| 629 | } |
| 630 | return string_section_start + i; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 633 | // 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 Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 636 | template <typename ElfTypes> |
| 637 | typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 638 | return hash_section_start_; |
| 639 | } |
| 640 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 641 | template <typename ElfTypes> |
| 642 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 643 | return GetHashSectionStart()[0]; |
| 644 | } |
| 645 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 646 | template <typename ElfTypes> |
| 647 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 648 | return GetHashSectionStart()[1]; |
| 649 | } |
| 650 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 651 | template <typename ElfTypes> |
| 652 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 653 | if (i >= GetHashBucketNum()) { |
| 654 | *ok = false; |
| 655 | return 0; |
| 656 | } |
| 657 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 658 | // 0 is nbucket, 1 is nchain |
| 659 | return GetHashSectionStart()[2 + i]; |
| 660 | } |
| 661 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 662 | template <typename ElfTypes> |
| 663 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const { |
Yevgeny Rouban | acb0138 | 2014-11-24 13:40:56 +0600 | [diff] [blame] | 664 | if (i >= GetHashChainNum()) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 665 | *ok = false; |
| 666 | return 0; |
| 667 | } |
| 668 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 669 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 670 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 671 | } |
| 672 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 673 | template <typename ElfTypes> |
| 674 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 675 | return GetHeader().e_phnum; |
| 676 | } |
| 677 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 678 | template <typename ElfTypes> |
| 679 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 680 | CHECK_LT(i, GetProgramHeaderNum()) << file_path_; // Sanity check for caller. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 681 | uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 682 | CHECK_LT(program_header, End()); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 683 | return reinterpret_cast<Elf_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 684 | } |
| 685 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 686 | template <typename ElfTypes> |
| 687 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 688 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 689 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 690 | if (program_header->p_type == type) { |
| 691 | return program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 694 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 695 | } |
| 696 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 697 | template <typename ElfTypes> |
| 698 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 699 | return GetHeader().e_shnum; |
| 700 | } |
| 701 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 702 | template <typename ElfTypes> |
| 703 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 704 | // 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 706 | CHECK(!program_header_only_) << file_path_; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 707 | if (i >= GetSectionHeaderNum()) { |
| 708 | return nullptr; // Failure condition. |
| 709 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 710 | uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 711 | if (section_header >= End()) { |
| 712 | return nullptr; // Failure condition. |
| 713 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 714 | return reinterpret_cast<Elf_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 715 | } |
| 716 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 717 | template <typename ElfTypes> |
| 718 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 719 | // 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 721 | CHECK(!program_header_only_) << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 722 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 723 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 724 | if (section_header->sh_type == type) { |
| 725 | return section_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 726 | } |
| 727 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 728 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 732 | static unsigned elfhash(const char *_name) { |
| 733 | const unsigned char *name = (const unsigned char *) _name; |
| 734 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 735 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 736 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 737 | h = (h << 4) + *name++; |
| 738 | g = h & 0xf0000000; |
| 739 | h ^= g; |
| 740 | h ^= g >> 24; |
| 741 | } |
| 742 | return h; |
| 743 | } |
| 744 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 745 | template <typename ElfTypes> |
| 746 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 747 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 748 | } |
| 749 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 750 | template <typename ElfTypes> |
| 751 | const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress( |
| 752 | const std::string& symbol_name) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 753 | // Check that we have a hash section. |
| 754 | if (GetHashSectionStart() == nullptr) { |
| 755 | return nullptr; // Failure condition. |
| 756 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 757 | const Elf_Sym* sym = FindDynamicSymbol(symbol_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 758 | if (sym != nullptr) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 759 | // 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 Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 761 | return base_address_ + sym->st_value; |
| 762 | } else { |
| 763 | return nullptr; |
| 764 | } |
| 765 | } |
| 766 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 767 | // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 768 | template <typename ElfTypes> |
| 769 | const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol( |
| 770 | const std::string& symbol_name) const { |
Andreas Gampe | c48b206 | 2014-09-08 23:39:45 -0700 | [diff] [blame] | 771 | if (GetHashBucketNum() == 0) { |
| 772 | // No dynamic symbols at all. |
| 773 | return nullptr; |
| 774 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 775 | Elf_Word hash = elfhash(symbol_name.c_str()); |
| 776 | Elf_Word bucket_index = hash % GetHashBucketNum(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 777 | bool ok; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 778 | Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 779 | if (!ok) { |
| 780 | return nullptr; |
| 781 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 782 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 783 | Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 784 | if (symbol == nullptr) { |
| 785 | return nullptr; // Failure condition. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 786 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 787 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 795 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 796 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 797 | } |
| 798 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 799 | template <typename ElfTypes> |
| 800 | bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 801 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
| 802 | } |
| 803 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 804 | template <typename ElfTypes> |
| 805 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 806 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 807 | << file_path_ << " " << section_header.sh_type; |
| 808 | CHECK_NE(0U, section_header.sh_entsize) << file_path_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 809 | return section_header.sh_size / section_header.sh_entsize; |
| 810 | } |
| 811 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 812 | template <typename ElfTypes> |
| 813 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 814 | Elf_Sym* sym_start = GetSymbolSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 815 | if (sym_start == nullptr) { |
| 816 | return nullptr; |
| 817 | } |
| 818 | return sym_start + i; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 819 | } |
| 820 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 821 | template <typename ElfTypes> |
| 822 | typename ElfFileImpl<ElfTypes>::SymbolTable** |
| 823 | ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 824 | CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 825 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 826 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 827 | return &symtab_symbol_table_; |
| 828 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 829 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 830 | return &dynsym_symbol_table_; |
| 831 | } |
| 832 | default: { |
| 833 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 834 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 839 | template <typename ElfTypes> |
| 840 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName( |
| 841 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 842 | CHECK(!program_header_only_) << file_path_; |
| 843 | CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 844 | |
| 845 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 846 | if (*symbol_table != nullptr || build_map) { |
| 847 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 848 | DCHECK(build_map); |
| 849 | *symbol_table = new SymbolTable; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 850 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 851 | if (symbol_section == nullptr) { |
| 852 | return nullptr; // Failure condition. |
| 853 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 854 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 855 | if (string_section == nullptr) { |
| 856 | return nullptr; // Failure condition. |
| 857 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 858 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 859 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 860 | if (symbol == nullptr) { |
| 861 | return nullptr; // Failure condition. |
| 862 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 863 | unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) |
| 864 | ? ELF64_ST_TYPE(symbol->st_info) |
| 865 | : ELF32_ST_TYPE(symbol->st_info); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 866 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 867 | continue; |
| 868 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 869 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 870 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 871 | continue; |
| 872 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 873 | std::pair<typename SymbolTable::iterator, bool> result = |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 874 | (*symbol_table)->insert(std::make_pair(name, symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 875 | if (!result.second) { |
| 876 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 877 | 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 Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 887 | CHECK(*symbol_table != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 888 | typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 889 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 890 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 891 | } |
| 892 | return it->second; |
| 893 | } |
| 894 | |
| 895 | // Fall back to linear search |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 896 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 897 | if (symbol_section == nullptr) { |
| 898 | return nullptr; |
| 899 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 900 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 901 | if (string_section == nullptr) { |
| 902 | return nullptr; |
| 903 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 904 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 905 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 906 | if (symbol == nullptr) { |
| 907 | return nullptr; // Failure condition. |
| 908 | } |
| 909 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 910 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 911 | continue; |
| 912 | } |
| 913 | if (symbol_name == name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 914 | return symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 915 | } |
| 916 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 917 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 918 | } |
| 919 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 920 | template <typename ElfTypes> |
| 921 | typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress( |
| 922 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 923 | Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 924 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 925 | return 0; |
| 926 | } |
| 927 | return symbol->st_value; |
| 928 | } |
| 929 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 930 | template <typename ElfTypes> |
| 931 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section, |
| 932 | Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 933 | CHECK(!program_header_only_) << file_path_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 934 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 935 | if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 936 | return nullptr; // Failure condition. |
| 937 | } |
| 938 | if (i >= string_section.sh_size) { |
| 939 | return nullptr; |
| 940 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 941 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 942 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 943 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 944 | uint8_t* strings = Begin() + string_section.sh_offset; |
| 945 | uint8_t* string = strings + i; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 946 | if (string >= End()) { |
| 947 | return nullptr; |
| 948 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 949 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 950 | } |
| 951 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 952 | template <typename ElfTypes> |
| 953 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 954 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 955 | } |
| 956 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 957 | template <typename ElfTypes> |
| 958 | typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 959 | CHECK_LT(i, GetDynamicNum()) << file_path_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 960 | return *(GetDynamicSectionStart() + i); |
| 961 | } |
| 962 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 963 | template <typename ElfTypes> |
| 964 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 965 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 966 | Elf_Dyn* dyn = &GetDynamic(i); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 967 | if (dyn->d_tag == type) { |
| 968 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 969 | } |
| 970 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 971 | return nullptr; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 972 | } |
| 973 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 974 | template <typename ElfTypes> |
| 975 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 976 | Elf_Dyn* dyn = FindDynamicByType(type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 977 | if (dyn == nullptr) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } else { |
| 980 | return dyn->d_un.d_val; |
| 981 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 982 | } |
| 983 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 984 | template <typename ElfTypes> |
| 985 | typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 986 | CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 987 | return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 988 | } |
| 989 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 990 | template <typename ElfTypes> |
| 991 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 992 | CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
| 993 | CHECK_NE(0U, section_header.sh_entsize) << file_path_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 994 | return section_header.sh_size / section_header.sh_entsize; |
| 995 | } |
| 996 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 997 | template <typename ElfTypes> |
| 998 | typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 999 | CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
| 1000 | CHECK_LT(i, GetRelNum(section_header)) << file_path_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1001 | return *(GetRelSectionStart(section_header) + i); |
| 1002 | } |
| 1003 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1004 | template <typename ElfTypes> |
| 1005 | typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1006 | CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1007 | return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1010 | template <typename ElfTypes> |
| 1011 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1012 | CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1013 | return section_header.sh_size / section_header.sh_entsize; |
| 1014 | } |
| 1015 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1016 | template <typename ElfTypes> |
| 1017 | typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1018 | CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type; |
| 1019 | CHECK_LT(i, GetRelaNum(section_header)) << file_path_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1020 | return *(GetRelaSectionStart(section_header) + i); |
| 1021 | } |
| 1022 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1023 | template <typename ElfTypes> |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1024 | bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1025 | uint8_t* vaddr_begin; |
| 1026 | return GetLoadedAddressRange(&vaddr_begin, size, error_msg); |
| 1027 | } |
| 1028 | |
| 1029 | // Base on bionic phdr_table_get_load_size |
| 1030 | template <typename ElfTypes> |
| 1031 | bool ElfFileImpl<ElfTypes>::GetLoadedAddressRange(/*out*/uint8_t** vaddr_begin, |
| 1032 | /*out*/size_t* vaddr_size, |
| 1033 | /*out*/std::string* error_msg) const { |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1034 | Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1); |
| 1035 | Elf_Addr max_vaddr = 0u; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1036 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1037 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1038 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1039 | continue; |
| 1040 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1041 | Elf_Addr begin_vaddr = program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1042 | if (begin_vaddr < min_vaddr) { |
| 1043 | min_vaddr = begin_vaddr; |
| 1044 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1045 | Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz; |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1046 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1050 | << " in ELF file \"" << file_path_ << "\""; |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1051 | *error_msg = oss.str(); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1052 | *vaddr_begin = nullptr; |
| 1053 | *vaddr_size = static_cast<size_t>(-1); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1056 | 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 Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1062 | CHECK_LT(min_vaddr, max_vaddr) << file_path_; |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1063 | // Check that the range fits into the runtime address space. |
| 1064 | if (UNLIKELY(max_vaddr - 1u > std::numeric_limits<size_t>::max())) { |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1065 | std::ostringstream oss; |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1066 | 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 Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1069 | *error_msg = oss.str(); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1070 | *vaddr_begin = nullptr; |
| 1071 | *vaddr_size = static_cast<size_t>(-1); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1072 | return false; |
| 1073 | } |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1074 | *vaddr_begin = reinterpret_cast<uint8_t*>(min_vaddr); |
| 1075 | *vaddr_size = dchecked_integral_cast<size_t>(max_vaddr - min_vaddr); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1076 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Vladimir Marko | 41b605c | 2020-02-12 10:52:22 +0000 | [diff] [blame^] | 1079 | static InstructionSet GetInstructionSetFromELF(uint16_t e_machine, |
| 1080 | uint32_t e_flags ATTRIBUTE_UNUSED) { |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 1081 | switch (e_machine) { |
| 1082 | case EM_ARM: |
| 1083 | return InstructionSet::kArm; |
| 1084 | case EM_AARCH64: |
| 1085 | return InstructionSet::kArm64; |
| 1086 | case EM_386: |
| 1087 | return InstructionSet::kX86; |
| 1088 | case EM_X86_64: |
| 1089 | return InstructionSet::kX86_64; |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 1090 | } |
| 1091 | return InstructionSet::kNone; |
| 1092 | } |
| 1093 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1094 | template <typename ElfTypes> |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1095 | bool ElfFileImpl<ElfTypes>::Load(File* file, |
| 1096 | bool executable, |
| 1097 | bool low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1098 | /*inout*/MemMap* reservation, |
| 1099 | /*out*/std::string* error_msg) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1100 | CHECK(program_header_only_) << file->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1101 | |
| 1102 | if (executable) { |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 1103 | InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1104 | if (elf_ISA != kRuntimeISA) { |
| 1105 | std::ostringstream oss; |
| 1106 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 1107 | *error_msg = oss.str(); |
| 1108 | return false; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1112 | bool reserved = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1113 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1114 | Elf_Phdr* program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1115 | |
| 1116 | // Record .dynamic header information for later use |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1117 | if (program_header->p_type == PT_DYNAMIC) { |
| 1118 | dynamic_program_header_ = program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
| 1121 | |
| 1122 | // Not something to load, move on. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1123 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1124 | continue; |
| 1125 | } |
| 1126 | |
| 1127 | // Found something to load. |
| 1128 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1129 | // Before load the actual segments, reserve a contiguous chunk |
| 1130 | // of required size and address for all segments, but with no |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1131 | // permissions. We'll then carve that up with the proper |
| 1132 | // permissions as we load the actual segments. If p_vaddr is |
| 1133 | // non-zero, the segments require the specific address specified, |
| 1134 | // which either was specified in the file because we already set |
| 1135 | // base_address_ after the first zero segment). |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1136 | int64_t temp_file_length = file->GetLength(); |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1137 | if (temp_file_length < 0) { |
| 1138 | errno = -temp_file_length; |
| 1139 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1140 | file->GetPath().c_str(), file->Fd(), strerror(errno)); |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
| 1143 | size_t file_length = static_cast<size_t>(temp_file_length); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1144 | if (!reserved) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1145 | uint8_t* vaddr_begin; |
| 1146 | size_t vaddr_size; |
| 1147 | if (!GetLoadedAddressRange(&vaddr_begin, &vaddr_size, error_msg)) { |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1148 | DCHECK(!error_msg->empty()); |
| 1149 | return false; |
| 1150 | } |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1151 | std::string reservation_name = "ElfFile reservation for " + file->GetPath(); |
| 1152 | MemMap local_reservation = MemMap::MapAnonymous( |
| 1153 | reservation_name.c_str(), |
| 1154 | (reservation != nullptr) ? reservation->Begin() : nullptr, |
| 1155 | vaddr_size, |
| 1156 | PROT_NONE, |
| 1157 | low_4gb, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1158 | /* reuse= */ false, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1159 | reservation, |
| 1160 | error_msg); |
| 1161 | if (!local_reservation.IsValid()) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1162 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1163 | reservation_name.c_str(), |
| 1164 | error_msg->c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1167 | reserved = true; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1168 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1169 | // Base address is the difference of actual mapped location and the vaddr_begin. |
| 1170 | base_address_ = reinterpret_cast<uint8_t*>( |
| 1171 | static_cast<uintptr_t>(local_reservation.Begin() - vaddr_begin)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1172 | // By adding the p_vaddr of a section/symbol to base_address_ we will always get the |
| 1173 | // dynamic memory address of where that object is actually mapped |
| 1174 | // |
| 1175 | // TODO: base_address_ needs to be calculated in ::Open, otherwise |
| 1176 | // FindDynamicSymbolAddress returns the wrong values until Load is called. |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1177 | segments_.push_back(std::move(local_reservation)); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1178 | } |
| 1179 | // empty segment, nothing to map |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1180 | if (program_header->p_memsz == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1181 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1182 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1183 | uint8_t* p_vaddr = base_address_ + program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1184 | int prot = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1185 | if (executable && ((program_header->p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1186 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1187 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1188 | if ((program_header->p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1189 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1190 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1191 | if ((program_header->p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1192 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1193 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1194 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1195 | if (writable_) { |
| 1196 | prot |= PROT_WRITE; |
| 1197 | flags |= MAP_SHARED; |
| 1198 | } else { |
| 1199 | flags |= MAP_PRIVATE; |
| 1200 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1201 | if (program_header->p_filesz > program_header->p_memsz) { |
| 1202 | *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s", |
| 1203 | static_cast<uint64_t>(program_header->p_filesz), |
| 1204 | static_cast<uint64_t>(program_header->p_memsz), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1205 | file->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 1206 | return false; |
| 1207 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1208 | if (program_header->p_filesz < program_header->p_memsz && |
| 1209 | !IsAligned<kPageSize>(program_header->p_filesz)) { |
| 1210 | *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64 |
| 1211 | " < %" PRIu64 "): %s", |
| 1212 | static_cast<uint64_t>(program_header->p_filesz), |
| 1213 | static_cast<uint64_t>(program_header->p_memsz), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1214 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1215 | return false; |
| 1216 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1217 | if (file_length < (program_header->p_offset + program_header->p_filesz)) { |
| 1218 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
| 1219 | "%d of %" PRIu64 " bytes: '%s'", file_length, i, |
| 1220 | static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz), |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1221 | file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1224 | if (program_header->p_filesz != 0u) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1225 | MemMap segment = |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1226 | MemMap::MapFileAtAddress(p_vaddr, |
| 1227 | program_header->p_filesz, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1228 | prot, |
| 1229 | flags, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1230 | file->Fd(), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1231 | program_header->p_offset, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1232 | /* low_4gb= */ false, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1233 | file->GetPath().c_str(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1234 | /* reuse= */ true, // implies MAP_FIXED |
| 1235 | /* reservation= */ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1236 | error_msg); |
| 1237 | if (!segment.IsValid()) { |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1238 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1239 | i, file->GetPath().c_str(), error_msg->c_str()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1242 | if (segment.Begin() != p_vaddr) { |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1243 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 1244 | "instead mapped to %p", |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1245 | i, file->GetPath().c_str(), p_vaddr, segment.Begin()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1246 | return false; |
| 1247 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1248 | segments_.push_back(std::move(segment)); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1249 | } |
| 1250 | if (program_header->p_filesz < program_header->p_memsz) { |
| 1251 | std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1252 | static_cast<uint64_t>(i), file->GetPath().c_str()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1253 | MemMap segment = MemMap::MapAnonymous(name.c_str(), |
| 1254 | p_vaddr + program_header->p_filesz, |
| 1255 | program_header->p_memsz - program_header->p_filesz, |
| 1256 | prot, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1257 | /* low_4gb= */ false, |
| 1258 | /* reuse= */ true, |
| 1259 | /* reservation= */ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1260 | error_msg); |
| 1261 | if (!segment.IsValid()) { |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1262 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1263 | i, file->GetPath().c_str(), error_msg->c_str()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1266 | if (segment.Begin() != p_vaddr) { |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1267 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s " |
| 1268 | "at expected address %p, instead mapped to %p", |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1269 | i, file->GetPath().c_str(), p_vaddr, segment.Begin()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1270 | return false; |
| 1271 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1272 | segments_.push_back(std::move(segment)); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1273 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1274 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1275 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1276 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1277 | uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1278 | if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) { |
| 1279 | *error_msg = StringPrintf("dynamic section address invalid in ELF file %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1280 | file->GetPath().c_str()); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1281 | return false; |
| 1282 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1283 | dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1284 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1285 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1286 | Elf_Dyn& elf_dyn = GetDynamic(i); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1287 | uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1288 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1289 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1290 | if (!ValidPointer(d_ptr)) { |
| 1291 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1292 | d_ptr, file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1295 | hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1296 | break; |
| 1297 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1298 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1299 | if (!ValidPointer(d_ptr)) { |
| 1300 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1301 | d_ptr, file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1304 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 1305 | break; |
| 1306 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1307 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1308 | if (!ValidPointer(d_ptr)) { |
| 1309 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1310 | d_ptr, file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1313 | dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1314 | break; |
| 1315 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1316 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1317 | if (GetDynamicNum() != i+1) { |
| 1318 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 1319 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1320 | i + 1, GetDynamicNum(), file->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1323 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1328 | // Check for the existence of some sections. |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1329 | if (!CheckSectionsExist(file, error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1330 | return false; |
| 1331 | } |
| 1332 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1333 | return true; |
| 1334 | } |
| 1335 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1336 | template <typename ElfTypes> |
| 1337 | bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1338 | for (const MemMap& segment : segments_) { |
| 1339 | if (segment.Begin() <= start && start < segment.End()) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1340 | return true; |
| 1341 | } |
| 1342 | } |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1346 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1347 | template <typename ElfTypes> |
| 1348 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName( |
| 1349 | const std::string& name) const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1350 | CHECK(!program_header_only_); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1351 | Elf_Shdr* shstrtab_sec = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1352 | if (shstrtab_sec == nullptr) { |
| 1353 | return nullptr; |
| 1354 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1355 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1356 | Elf_Shdr* shdr = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1357 | if (shdr == nullptr) { |
| 1358 | return nullptr; |
| 1359 | } |
| 1360 | const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1361 | if (sec_name == nullptr) { |
| 1362 | continue; |
| 1363 | } |
| 1364 | if (name == sec_name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1365 | return shdr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1371 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1372 | bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1373 | if (base_address_delta == 0) { |
| 1374 | return true; |
| 1375 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1376 | return ApplyOatPatchesTo(".debug_frame", base_address_delta) && |
| 1377 | ApplyOatPatchesTo(".debug_info", base_address_delta) && |
| 1378 | ApplyOatPatchesTo(".debug_line", base_address_delta); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1379 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1380 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1381 | template <typename ElfTypes> |
| 1382 | bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo( |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1383 | const char* target_section_name, Elf_Addr delta) { |
| 1384 | auto target_section = FindSectionByName(target_section_name); |
| 1385 | if (target_section == nullptr) { |
| 1386 | return true; |
| 1387 | } |
| 1388 | std::string patches_name = target_section_name + std::string(".oat_patches"); |
| 1389 | auto patches_section = FindSectionByName(patches_name.c_str()); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1390 | if (patches_section == nullptr) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1391 | LOG(ERROR) << patches_name << " section not found."; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1392 | return false; |
| 1393 | } |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1394 | if (patches_section->sh_type != SHT_OAT_PATCH) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1395 | LOG(ERROR) << "Unexpected type of " << patches_name; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1396 | return false; |
| 1397 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1398 | ApplyOatPatches( |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1399 | Begin() + patches_section->sh_offset, |
| 1400 | Begin() + patches_section->sh_offset + patches_section->sh_size, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1401 | delta, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1402 | Begin() + target_section->sh_offset, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1403 | Begin() + target_section->sh_offset + target_section->sh_size); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1404 | return true; |
| 1405 | } |
| 1406 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1407 | // Apply LEB128 encoded patches to given section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1408 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1409 | void ElfFileImpl<ElfTypes>::ApplyOatPatches( |
| 1410 | const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta, |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1411 | uint8_t* to_patch, const uint8_t* to_patch_end) { |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 1412 | using UnalignedAddress __attribute__((__aligned__(1))) = Elf_Addr; |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1413 | while (patches < patches_end) { |
| 1414 | to_patch += DecodeUnsignedLeb128(&patches); |
| 1415 | DCHECK_LE(patches, patches_end) << "Unexpected end of patch list."; |
| 1416 | DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section."; |
| 1417 | *reinterpret_cast<UnalignedAddress*>(to_patch) += delta; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1418 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1419 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1420 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1421 | template <typename ElfTypes> |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1422 | bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1423 | // ELF files produced by MCLinker look roughly like this |
| 1424 | // |
| 1425 | // +------------+ |
| 1426 | // | Elf_Ehdr | contains number of Elf_Shdr and offset to first |
| 1427 | // +------------+ |
| 1428 | // | Elf_Phdr | program headers |
| 1429 | // | Elf_Phdr | |
| 1430 | // | ... | |
| 1431 | // | Elf_Phdr | |
| 1432 | // +------------+ |
| 1433 | // | section | mixture of needed and unneeded sections |
| 1434 | // +------------+ |
| 1435 | // | section | |
| 1436 | // +------------+ |
| 1437 | // | ... | |
| 1438 | // +------------+ |
| 1439 | // | section | |
| 1440 | // +------------+ |
| 1441 | // | Elf_Shdr | section headers |
| 1442 | // | Elf_Shdr | |
| 1443 | // | ... | contains offset to section start |
| 1444 | // | Elf_Shdr | |
| 1445 | // +------------+ |
| 1446 | // |
| 1447 | // To strip: |
| 1448 | // - leave the Elf_Ehdr and Elf_Phdr values in place. |
| 1449 | // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep |
| 1450 | // - move the sections are keeping up to fill in gaps of sections we want to strip |
| 1451 | // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr |
| 1452 | // - truncate rest of file |
| 1453 | // |
| 1454 | |
| 1455 | std::vector<Elf_Shdr> section_headers; |
| 1456 | std::vector<Elf_Word> section_headers_original_indexes; |
| 1457 | section_headers.reserve(GetSectionHeaderNum()); |
| 1458 | |
| 1459 | |
| 1460 | Elf_Shdr* string_section = GetSectionNameStringSection(); |
| 1461 | CHECK(string_section != nullptr); |
| 1462 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1463 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1464 | CHECK(sh != nullptr); |
| 1465 | const char* name = GetString(*string_section, sh->sh_name); |
| 1466 | if (name == nullptr) { |
| 1467 | CHECK_EQ(0U, i); |
| 1468 | section_headers.push_back(*sh); |
| 1469 | section_headers_original_indexes.push_back(0); |
| 1470 | continue; |
| 1471 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1472 | if (android::base::StartsWith(name, ".debug") |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1473 | || (strcmp(name, ".strtab") == 0) |
| 1474 | || (strcmp(name, ".symtab") == 0)) { |
| 1475 | continue; |
| 1476 | } |
| 1477 | section_headers.push_back(*sh); |
| 1478 | section_headers_original_indexes.push_back(i); |
| 1479 | } |
| 1480 | CHECK_NE(0U, section_headers.size()); |
| 1481 | CHECK_EQ(section_headers.size(), section_headers_original_indexes.size()); |
| 1482 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1483 | // section 0 is the null section, sections start at offset of first section |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1484 | CHECK(GetSectionHeader(1) != nullptr); |
| 1485 | Elf_Off offset = GetSectionHeader(1)->sh_offset; |
| 1486 | for (size_t i = 1; i < section_headers.size(); i++) { |
| 1487 | Elf_Shdr& new_sh = section_headers[i]; |
| 1488 | Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]); |
| 1489 | CHECK(old_sh != nullptr); |
| 1490 | CHECK_EQ(new_sh.sh_name, old_sh->sh_name); |
| 1491 | if (old_sh->sh_addralign > 1) { |
| 1492 | offset = RoundUp(offset, old_sh->sh_addralign); |
| 1493 | } |
| 1494 | if (old_sh->sh_offset == offset) { |
| 1495 | // already in place |
| 1496 | offset += old_sh->sh_size; |
| 1497 | continue; |
| 1498 | } |
| 1499 | // shift section earlier |
| 1500 | memmove(Begin() + offset, |
| 1501 | Begin() + old_sh->sh_offset, |
| 1502 | old_sh->sh_size); |
| 1503 | new_sh.sh_offset = offset; |
| 1504 | offset += old_sh->sh_size; |
| 1505 | } |
| 1506 | |
| 1507 | Elf_Off shoff = offset; |
| 1508 | size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr); |
| 1509 | memcpy(Begin() + offset, §ion_headers[0], section_headers_size_in_bytes); |
| 1510 | offset += section_headers_size_in_bytes; |
| 1511 | |
| 1512 | GetHeader().e_shnum = section_headers.size(); |
| 1513 | GetHeader().e_shoff = shoff; |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1514 | int result = ftruncate(file->Fd(), offset); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1515 | if (result != 0) { |
| 1516 | *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s", |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1517 | file->GetPath().c_str(), strerror(errno)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1518 | return false; |
| 1519 | } |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
| 1523 | static const bool DEBUG_FIXUP = false; |
| 1524 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1525 | template <typename ElfTypes> |
| 1526 | bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1527 | if (!FixupDynamic(base_address)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1528 | LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1529 | return false; |
| 1530 | } |
| 1531 | if (!FixupSectionHeaders(base_address)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1532 | LOG(WARNING) << "Failed to fixup section headers in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
| 1535 | if (!FixupProgramHeaders(base_address)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1536 | LOG(WARNING) << "Failed to fixup program headers in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1537 | return false; |
| 1538 | } |
| 1539 | if (!FixupSymbols(base_address, true)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1540 | LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1541 | return false; |
| 1542 | } |
| 1543 | if (!FixupSymbols(base_address, false)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1544 | LOG(WARNING) << "Failed to fixup .symtab in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1545 | return false; |
| 1546 | } |
| 1547 | if (!FixupRelocations(base_address)) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1548 | LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1551 | static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision."); |
| 1552 | if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) { |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1553 | LOG(WARNING) << "Failed to fixup debug sections in " << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1559 | template <typename ElfTypes> |
| 1560 | bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1561 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1562 | Elf_Dyn& elf_dyn = GetDynamic(i); |
| 1563 | Elf_Word d_tag = elf_dyn.d_tag; |
| 1564 | if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) { |
| 1565 | Elf_Addr d_ptr = elf_dyn.d_un.d_ptr; |
| 1566 | if (DEBUG_FIXUP) { |
| 1567 | LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1568 | file_path_.c_str(), i, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1569 | static_cast<uint64_t>(d_ptr), |
| 1570 | static_cast<uint64_t>(d_ptr + base_address)); |
| 1571 | } |
| 1572 | d_ptr += base_address; |
| 1573 | elf_dyn.d_un.d_ptr = d_ptr; |
| 1574 | } |
| 1575 | } |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1579 | template <typename ElfTypes> |
| 1580 | bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1581 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1582 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1583 | CHECK(sh != nullptr); |
| 1584 | // 0 implies that the section will not exist in the memory of the process |
| 1585 | if (sh->sh_addr == 0) { |
| 1586 | continue; |
| 1587 | } |
| 1588 | if (DEBUG_FIXUP) { |
| 1589 | LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1590 | file_path_.c_str(), i, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1591 | static_cast<uint64_t>(sh->sh_addr), |
| 1592 | static_cast<uint64_t>(sh->sh_addr + base_address)); |
| 1593 | } |
| 1594 | sh->sh_addr += base_address; |
| 1595 | } |
| 1596 | return true; |
| 1597 | } |
| 1598 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1599 | template <typename ElfTypes> |
| 1600 | bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1601 | // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now. |
| 1602 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1603 | Elf_Phdr* ph = GetProgramHeader(i); |
| 1604 | CHECK(ph != nullptr); |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1605 | CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1606 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1607 | << file_path_ << " i=" << i; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1608 | if (DEBUG_FIXUP) { |
| 1609 | LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1610 | file_path_.c_str(), i, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1611 | static_cast<uint64_t>(ph->p_vaddr), |
| 1612 | static_cast<uint64_t>(ph->p_vaddr + base_address)); |
| 1613 | } |
| 1614 | ph->p_vaddr += base_address; |
| 1615 | ph->p_paddr += base_address; |
| 1616 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1617 | << file_path_ << " i=" << i; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1618 | } |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1622 | template <typename ElfTypes> |
| 1623 | bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1624 | Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB; |
| 1625 | // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile |
| 1626 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
| 1627 | if (symbol_section == nullptr) { |
| 1628 | // file is missing optional .symtab |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1629 | CHECK(!dynamic) << file_path_; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
| 1633 | Elf_Sym* symbol = GetSymbol(section_type, i); |
| 1634 | CHECK(symbol != nullptr); |
| 1635 | if (symbol->st_value != 0) { |
| 1636 | if (DEBUG_FIXUP) { |
| 1637 | LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1638 | file_path_.c_str(), i, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1639 | static_cast<uint64_t>(symbol->st_value), |
| 1640 | static_cast<uint64_t>(symbol->st_value + base_address)); |
| 1641 | } |
| 1642 | symbol->st_value += base_address; |
| 1643 | } |
| 1644 | } |
| 1645 | return true; |
| 1646 | } |
| 1647 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1648 | template <typename ElfTypes> |
| 1649 | bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1650 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1651 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1652 | CHECK(sh != nullptr); |
| 1653 | if (sh->sh_type == SHT_REL) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1654 | for (uint32_t j = 0; j < GetRelNum(*sh); j++) { |
| 1655 | Elf_Rel& rel = GetRel(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1656 | if (DEBUG_FIXUP) { |
| 1657 | LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1658 | file_path_.c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1659 | static_cast<uint64_t>(rel.r_offset), |
| 1660 | static_cast<uint64_t>(rel.r_offset + base_address)); |
| 1661 | } |
| 1662 | rel.r_offset += base_address; |
| 1663 | } |
| 1664 | } else if (sh->sh_type == SHT_RELA) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1665 | for (uint32_t j = 0; j < GetRelaNum(*sh); j++) { |
| 1666 | Elf_Rela& rela = GetRela(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1667 | if (DEBUG_FIXUP) { |
| 1668 | LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1669 | file_path_.c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1670 | static_cast<uint64_t>(rela.r_offset), |
| 1671 | static_cast<uint64_t>(rela.r_offset + base_address)); |
| 1672 | } |
| 1673 | rela.r_offset += base_address; |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | return true; |
| 1678 | } |
| 1679 | |
| 1680 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1681 | template class ElfFileImpl<ElfTypes32>; |
| 1682 | template class ElfFileImpl<ElfTypes64>; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1683 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1684 | ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1687 | ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | ElfFile::~ElfFile() { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1691 | // Should never have 32 and 64-bit impls. |
| 1692 | CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1695 | ElfFile* ElfFile::Open(File* file, |
| 1696 | bool writable, |
| 1697 | bool program_header_only, |
| 1698 | bool low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1699 | /*out*/std::string* error_msg) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1700 | if (file->GetLength() < EI_NIDENT) { |
| 1701 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1702 | file->GetPath().c_str()); |
| 1703 | return nullptr; |
| 1704 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1705 | MemMap map = MemMap::MapFile(EI_NIDENT, |
| 1706 | PROT_READ, |
| 1707 | MAP_PRIVATE, |
| 1708 | file->Fd(), |
| 1709 | 0, |
| 1710 | low_4gb, |
| 1711 | file->GetPath().c_str(), |
| 1712 | error_msg); |
| 1713 | if (!map.IsValid() || map.Size() != EI_NIDENT) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1714 | return nullptr; |
| 1715 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1716 | uint8_t* header = map.Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1717 | if (header[EI_CLASS] == ELFCLASS64) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1718 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, |
| 1719 | writable, |
| 1720 | program_header_only, |
| 1721 | low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1722 | error_msg); |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1723 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1724 | return nullptr; |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1725 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1726 | return new ElfFile(elf_file_impl); |
| 1727 | } else if (header[EI_CLASS] == ELFCLASS32) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1728 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, |
| 1729 | writable, |
| 1730 | program_header_only, |
| 1731 | low_4gb, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1732 | error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1733 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1734 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1735 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1736 | return new ElfFile(elf_file_impl); |
| 1737 | } else { |
| 1738 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1739 | ELFCLASS32, ELFCLASS64, |
| 1740 | file->GetPath().c_str(), |
| 1741 | header[EI_CLASS]); |
| 1742 | return nullptr; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1746 | ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, /*out*/std::string* error_msg) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1747 | // low_4gb support not required for this path. |
| 1748 | constexpr bool low_4gb = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1749 | if (file->GetLength() < EI_NIDENT) { |
| 1750 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1751 | file->GetPath().c_str()); |
| 1752 | return nullptr; |
| 1753 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1754 | MemMap map = MemMap::MapFile(EI_NIDENT, |
| 1755 | PROT_READ, |
| 1756 | MAP_PRIVATE, |
| 1757 | file->Fd(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1758 | /* start= */ 0, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1759 | low_4gb, |
| 1760 | file->GetPath().c_str(), |
| 1761 | error_msg); |
| 1762 | if (!map.IsValid() || map.Size() != EI_NIDENT) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1763 | return nullptr; |
| 1764 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1765 | uint8_t* header = map.Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1766 | if (header[EI_CLASS] == ELFCLASS64) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1767 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, |
| 1768 | mmap_prot, |
| 1769 | mmap_flags, |
| 1770 | low_4gb, |
| 1771 | error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1772 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1773 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1774 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1775 | return new ElfFile(elf_file_impl); |
| 1776 | } else if (header[EI_CLASS] == ELFCLASS32) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1777 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, |
| 1778 | mmap_prot, |
| 1779 | mmap_flags, |
| 1780 | low_4gb, |
| 1781 | error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1782 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1783 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1784 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1785 | return new ElfFile(elf_file_impl); |
| 1786 | } else { |
| 1787 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1788 | ELFCLASS32, ELFCLASS64, |
| 1789 | file->GetPath().c_str(), |
| 1790 | header[EI_CLASS]); |
| 1791 | return nullptr; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | #define DELEGATE_TO_IMPL(func, ...) \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1796 | if (elf64_.get() != nullptr) { \ |
| 1797 | return elf64_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1798 | } else { \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1799 | DCHECK(elf32_.get() != nullptr); \ |
| 1800 | return elf32_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1803 | bool ElfFile::Load(File* file, |
| 1804 | bool executable, |
| 1805 | bool low_4gb, |
| 1806 | /*inout*/MemMap* reservation, |
| 1807 | /*out*/std::string* error_msg) { |
| 1808 | DELEGATE_TO_IMPL(Load, file, executable, low_4gb, reservation, error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1811 | const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1812 | DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name); |
| 1813 | } |
| 1814 | |
| 1815 | size_t ElfFile::Size() const { |
| 1816 | DELEGATE_TO_IMPL(Size); |
| 1817 | } |
| 1818 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1819 | uint8_t* ElfFile::Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1820 | DELEGATE_TO_IMPL(Begin); |
| 1821 | } |
| 1822 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1823 | uint8_t* ElfFile::End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1824 | DELEGATE_TO_IMPL(End); |
| 1825 | } |
| 1826 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1827 | const std::string& ElfFile::GetFilePath() const { |
| 1828 | DELEGATE_TO_IMPL(GetFilePath); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1831 | bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, |
| 1832 | uint64_t* size) const { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1833 | if (elf32_.get() == nullptr) { |
| 1834 | CHECK(elf64_.get() != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1835 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1836 | Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name); |
| 1837 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1838 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1839 | } |
| 1840 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1841 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1842 | } |
| 1843 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1844 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1845 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1846 | return true; |
| 1847 | } else { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1848 | Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name); |
| 1849 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1850 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1851 | } |
| 1852 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1853 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1854 | } |
| 1855 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1856 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1857 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1858 | return true; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Jeff Hao | 24ec028 | 2016-03-17 21:32:45 -0700 | [diff] [blame] | 1862 | bool ElfFile::HasSection(const std::string& name) const { |
| 1863 | if (elf64_.get() != nullptr) { |
| 1864 | return elf64_->FindSectionByName(name) != nullptr; |
| 1865 | } else { |
| 1866 | return elf32_->FindSectionByName(name) != nullptr; |
| 1867 | } |
| 1868 | } |
| 1869 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1870 | uint64_t ElfFile::FindSymbolAddress(unsigned section_type, |
| 1871 | const std::string& symbol_name, |
| 1872 | bool build_map) { |
| 1873 | DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map); |
| 1874 | } |
| 1875 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1876 | bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const { |
| 1877 | DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | bool ElfFile::Strip(File* file, std::string* error_msg) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1881 | std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb=*/false, error_msg)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1882 | if (elf_file.get() == nullptr) { |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
Brian Carlstrom | f5b0f2c | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 1886 | if (elf_file->elf64_.get() != nullptr) { |
| 1887 | return elf_file->elf64_->Strip(file, error_msg); |
| 1888 | } else { |
| 1889 | return elf_file->elf32_->Strip(file, error_msg); |
| 1890 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1893 | bool ElfFile::Fixup(uint64_t base_address) { |
| 1894 | if (elf64_.get() != nullptr) { |
| 1895 | return elf64_->Fixup(static_cast<Elf64_Addr>(base_address)); |
| 1896 | } else { |
| 1897 | DCHECK(elf32_.get() != nullptr); |
| 1898 | CHECK(IsUint<32>(base_address)) << std::hex << base_address; |
| 1899 | return elf32_->Fixup(static_cast<Elf32_Addr>(base_address)); |
| 1900 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1901 | DELEGATE_TO_IMPL(Fixup, base_address); |
| 1902 | } |
| 1903 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1904 | } // namespace art |