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