blob: f16db8bb05e5822a6eaeef0cb99116e003ec27ef [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "elf_file.h"
18
19#include "base/logging.h"
20#include "base/stl_util.h"
21#include "utils.h"
22
23namespace art {
24
Brian Carlstromc1409452014-02-26 14:06:23 -080025ElfFile::ElfFile(File* file, bool writable, bool program_header_only)
26 : file_(file),
27 writable_(writable),
28 program_header_only_(program_header_only),
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070029 header_(NULL),
30 base_address_(NULL),
31 program_headers_start_(NULL),
32 section_headers_start_(NULL),
33 dynamic_program_header_(NULL),
34 dynamic_section_start_(NULL),
35 symtab_section_start_(NULL),
36 dynsym_section_start_(NULL),
37 strtab_section_start_(NULL),
38 dynstr_section_start_(NULL),
39 hash_section_start_(NULL),
40 symtab_symbol_table_(NULL),
Brian Carlstromc1409452014-02-26 14:06:23 -080041 dynsym_symbol_table_(NULL) {
42 CHECK(file != NULL);
43}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044
Ian Rogers8d31bbd2013-10-13 10:44:14 -070045ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
46 std::string* error_msg) {
Brian Carlstromc1409452014-02-26 14:06:23 -080047 UniquePtr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only));
48 if (!elf_file->Setup(error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070049 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 }
51 return elf_file.release();
52}
53
Brian Carlstromc1409452014-02-26 14:06:23 -080054bool ElfFile::Setup(std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 int prot;
56 int flags;
57 if (writable_) {
58 prot = PROT_READ | PROT_WRITE;
59 flags = MAP_SHARED;
60 } else {
61 prot = PROT_READ;
62 flags = MAP_PRIVATE;
63 }
Ian Rogerscdfcf372014-01-23 20:38:36 -080064 int64_t temp_file_length = file_->GetLength();
65 if (temp_file_length < 0) {
66 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070067 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
68 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -080069 return false;
70 }
Ian Rogerscdfcf372014-01-23 20:38:36 -080071 size_t file_length = static_cast<size_t>(temp_file_length);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000072 if (file_length < sizeof(Elf32_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -080073 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000074 "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076 return false;
77 }
78
Brian Carlstromc1409452014-02-26 14:06:23 -080079 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080080 // first just map ELF header to get program header size information
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000081 size_t elf_header_size = sizeof(Elf32_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070082 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -080083 file_->GetPath().c_str(), error_msg),
84 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 return false;
86 }
87 // then remap to cover program header
88 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -070089 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -080090 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 "header of %zd bytes: '%s'", file_length,
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000092 sizeof(Elf32_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -070093 return false;
94 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -080096 file_->GetPath().c_str(), error_msg),
97 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080099 return false;
100 }
101 } else {
102 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800104 file_->GetPath().c_str(), error_msg),
105 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800107 return false;
108 }
109 }
110
111 // Either way, the program header is relative to the elf header
112 program_headers_start_ = Begin() + GetHeader().e_phoff;
113
Brian Carlstromc1409452014-02-26 14:06:23 -0800114 if (!program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 // Setup section headers.
116 section_headers_start_ = Begin() + GetHeader().e_shoff;
117
118 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000119 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120 if (dynamic_program_header_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700121 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
122 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123 return false;
124 }
125
126 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000127 = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128
129 // Find other sections from section headers
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000130 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
131 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800132 byte* section_addr = Begin() + section_header.sh_offset;
133 switch (section_header.sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000134 case SHT_SYMTAB: {
135 symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800136 break;
137 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000138 case SHT_DYNSYM: {
139 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800140 break;
141 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000142 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 // TODO: base these off of sh_link from .symtab and .dynsym above
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000144 if ((section_header.sh_flags & SHF_ALLOC) != 0) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145 dynstr_section_start_ = reinterpret_cast<char*>(section_addr);
146 } else {
147 strtab_section_start_ = reinterpret_cast<char*>(section_addr);
148 }
149 break;
150 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000151 case SHT_DYNAMIC: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800152 if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) {
153 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800154 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 << reinterpret_cast<void*>(dynamic_section_start_)
156 << " != " << reinterpret_cast<void*>(section_addr);
157 return false;
158 }
159 break;
160 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000161 case SHT_HASH: {
162 hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800163 break;
164 }
165 }
166 }
167 }
168 return true;
169}
170
171ElfFile::~ElfFile() {
172 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800173 delete symtab_symbol_table_;
174 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800175}
176
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800177bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 if (map == NULL) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800179 // MemMap::Open should have already set an error.
180 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 return false;
182 }
183 map_.reset(map);
184 CHECK(map_.get() != NULL) << file_->GetPath();
185 CHECK(map_->Begin() != NULL) << file_->GetPath();
186
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000187 header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin());
188 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
189 || (ELFMAG1 != header_->e_ident[EI_MAG1])
190 || (ELFMAG2 != header_->e_ident[EI_MAG2])
191 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800192 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
193 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800194 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000195 header_->e_ident[EI_MAG0],
196 header_->e_ident[EI_MAG1],
197 header_->e_ident[EI_MAG2],
198 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 return false;
200 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800201 if (ELFCLASS32 != header_->e_ident[EI_CLASS]) {
202 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
203 ELFCLASS32,
204 file_->GetPath().c_str(),
205 header_->e_ident[EI_CLASS]);
206 return false;
207 }
208 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
209 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
210 ELFDATA2LSB,
211 file_->GetPath().c_str(),
212 header_->e_ident[EI_CLASS]);
213 return false;
214 }
215 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
216 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
217 EV_CURRENT,
218 file_->GetPath().c_str(),
219 header_->e_ident[EI_CLASS]);
220 return false;
221 }
222 if (ET_DYN != header_->e_type) {
223 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
224 ET_DYN,
225 file_->GetPath().c_str(),
226 header_->e_type);
227 return false;
228 }
229 if (EV_CURRENT != header_->e_version) {
230 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
231 EV_CURRENT,
232 file_->GetPath().c_str(),
233 header_->e_version);
234 return false;
235 }
236 if (0 != header_->e_entry) {
237 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
238 0,
239 file_->GetPath().c_str(),
240 header_->e_entry);
241 return false;
242 }
243 if (0 == header_->e_phoff) {
244 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
245 file_->GetPath().c_str());
246 return false;
247 }
248 if (0 == header_->e_shoff) {
249 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
250 file_->GetPath().c_str());
251 return false;
252 }
253 if (0 == header_->e_ehsize) {
254 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
255 file_->GetPath().c_str());
256 return false;
257 }
258 if (0 == header_->e_phentsize) {
259 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
260 file_->GetPath().c_str());
261 return false;
262 }
263 if (0 == header_->e_phnum) {
264 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
265 file_->GetPath().c_str());
266 return false;
267 }
268 if (0 == header_->e_shentsize) {
269 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
270 file_->GetPath().c_str());
271 return false;
272 }
273 if (0 == header_->e_shnum) {
274 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
275 file_->GetPath().c_str());
276 return false;
277 }
278 if (0 == header_->e_shstrndx) {
279 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
280 file_->GetPath().c_str());
281 return false;
282 }
283 if (header_->e_shstrndx >= header_->e_shnum) {
284 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
285 header_->e_shstrndx,
286 header_->e_shnum,
287 file_->GetPath().c_str());
288 return false;
289 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800290
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800291 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800292 if (header_->e_phoff >= Size()) {
293 *error_msg = StringPrintf("Failed to find e_phoff value %d less than %d in %s",
294 header_->e_phoff,
295 Size(),
296 file_->GetPath().c_str());
297 return false;
298 }
299 if (header_->e_shoff >= Size()) {
300 *error_msg = StringPrintf("Failed to find e_shoff value %d less than %d in %s",
301 header_->e_shoff,
302 Size(),
303 file_->GetPath().c_str());
304 return false;
305 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800306 }
307 return true;
308}
309
310
Brian Carlstromc1409452014-02-26 14:06:23 -0800311Elf32_Ehdr& ElfFile::GetHeader() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800312 CHECK(header_ != NULL);
313 return *header_;
314}
315
Brian Carlstromc1409452014-02-26 14:06:23 -0800316byte* ElfFile::GetProgramHeadersStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800317 CHECK(program_headers_start_ != NULL);
318 return program_headers_start_;
319}
320
Brian Carlstromc1409452014-02-26 14:06:23 -0800321byte* ElfFile::GetSectionHeadersStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800322 CHECK(section_headers_start_ != NULL);
323 return section_headers_start_;
324}
325
Brian Carlstromc1409452014-02-26 14:06:23 -0800326Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800327 CHECK(dynamic_program_header_ != NULL);
328 return *dynamic_program_header_;
329}
330
Brian Carlstromc1409452014-02-26 14:06:23 -0800331Elf32_Dyn* ElfFile::GetDynamicSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800332 CHECK(dynamic_section_start_ != NULL);
333 return dynamic_section_start_;
334}
335
Brian Carlstromc1409452014-02-26 14:06:23 -0800336Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800337 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000338 Elf32_Sym* symbol_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000340 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800341 symbol_section_start = symtab_section_start_;
342 break;
343 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000344 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800345 symbol_section_start = dynsym_section_start_;
346 break;
347 }
348 default: {
349 LOG(FATAL) << section_type;
350 symbol_section_start = NULL;
351 }
352 }
353 CHECK(symbol_section_start != NULL);
354 return symbol_section_start;
355}
356
Brian Carlstromc1409452014-02-26 14:06:23 -0800357const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800358 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800359 const char* string_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800360 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000361 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800362 string_section_start = strtab_section_start_;
363 break;
364 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000365 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800366 string_section_start = dynstr_section_start_;
367 break;
368 }
369 default: {
370 LOG(FATAL) << section_type;
371 string_section_start = NULL;
372 }
373 }
374 CHECK(string_section_start != NULL);
375 return string_section_start;
376}
377
Brian Carlstromc1409452014-02-26 14:06:23 -0800378const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800379 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
380 if (i == 0) {
381 return NULL;
382 }
383 const char* string_section_start = GetStringSectionStart(section_type);
384 const char* string = string_section_start + i;
385 return string;
386}
387
Brian Carlstromc1409452014-02-26 14:06:23 -0800388Elf32_Word* ElfFile::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800389 CHECK(hash_section_start_ != NULL);
390 return hash_section_start_;
391}
392
Brian Carlstromc1409452014-02-26 14:06:23 -0800393Elf32_Word ElfFile::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800394 return GetHashSectionStart()[0];
395}
396
Brian Carlstromc1409452014-02-26 14:06:23 -0800397Elf32_Word ElfFile::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800398 return GetHashSectionStart()[1];
399}
400
Brian Carlstromc1409452014-02-26 14:06:23 -0800401Elf32_Word ElfFile::GetHashBucket(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800402 CHECK_LT(i, GetHashBucketNum());
403 // 0 is nbucket, 1 is nchain
404 return GetHashSectionStart()[2 + i];
405}
406
Brian Carlstromc1409452014-02-26 14:06:23 -0800407Elf32_Word ElfFile::GetHashChain(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800408 CHECK_LT(i, GetHashChainNum());
409 // 0 is nbucket, 1 is nchain, & chains are after buckets
410 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
411}
412
Brian Carlstromc1409452014-02-26 14:06:23 -0800413Elf32_Word ElfFile::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800414 return GetHeader().e_phnum;
415}
416
Brian Carlstromc1409452014-02-26 14:06:23 -0800417Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800418 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath();
419 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
420 CHECK_LT(program_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000421 return *reinterpret_cast<Elf32_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800422}
423
Brian Carlstromc1409452014-02-26 14:06:23 -0800424Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000425 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
426 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800427 if (program_header.p_type == type) {
428 return &program_header;
429 }
430 }
431 return NULL;
432}
433
Brian Carlstromc1409452014-02-26 14:06:23 -0800434Elf32_Word ElfFile::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800435 return GetHeader().e_shnum;
436}
437
Brian Carlstromc1409452014-02-26 14:06:23 -0800438Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800439 // Can only access arbitrary sections when we have the whole file, not just program header.
440 // Even if we Load(), it doesn't bring in all the sections.
441 CHECK(!program_header_only_) << file_->GetPath();
442 CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath();
443 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
444 CHECK_LT(section_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000445 return *reinterpret_cast<Elf32_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800446}
447
Brian Carlstromc1409452014-02-26 14:06:23 -0800448Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 // Can only access arbitrary sections when we have the whole file, not just program header.
450 // We could change this to switch on known types if they were detected during loading.
451 CHECK(!program_header_only_) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000452 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
453 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800454 if (section_header.sh_type == type) {
455 return &section_header;
456 }
457 }
458 return NULL;
459}
460
461// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800462static unsigned elfhash(const char *_name) {
463 const unsigned char *name = (const unsigned char *) _name;
464 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800465
Brian Carlstromdf629502013-07-17 22:39:56 -0700466 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800467 h = (h << 4) + *name++;
468 g = h & 0xf0000000;
469 h ^= g;
470 h ^= g >> 24;
471 }
472 return h;
473}
474
Brian Carlstromc1409452014-02-26 14:06:23 -0800475Elf32_Shdr& ElfFile::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800476 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800477}
478
Brian Carlstromc1409452014-02-26 14:06:23 -0800479const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000480 Elf32_Word hash = elfhash(symbol_name.c_str());
481 Elf32_Word bucket_index = hash % GetHashBucketNum();
482 Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800483 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000484 Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
485 const char* name = GetString(SHT_DYNSYM, symbol.st_name);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800486 if (symbol_name == name) {
487 return base_address_ + symbol.st_value;
488 }
489 symbol_and_chain_index = GetHashChain(symbol_and_chain_index);
490 }
491 return NULL;
492}
493
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000494bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) {
495 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800496}
497
Brian Carlstromc1409452014-02-26 14:06:23 -0800498Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const {
499 CHECK(IsSymbolSectionType(section_header.sh_type))
500 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800501 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
502 return section_header.sh_size / section_header.sh_entsize;
503}
504
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000505Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800506 Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800507 return *(GetSymbolSectionStart(section_type) + i);
508}
509
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000510ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800511 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
512 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000513 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800514 return &symtab_symbol_table_;
515 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000516 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800517 return &dynsym_symbol_table_;
518 }
519 default: {
520 LOG(FATAL) << section_type;
521 return NULL;
522 }
523 }
524}
525
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000526Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type,
527 const std::string& symbol_name,
528 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800529 CHECK(!program_header_only_) << file_->GetPath();
530 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800531
532 SymbolTable** symbol_table = GetSymbolTable(section_type);
533 if (*symbol_table != NULL || build_map) {
534 if (*symbol_table == NULL) {
535 DCHECK(build_map);
536 *symbol_table = new SymbolTable;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000537 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800538 CHECK(symbol_section != NULL) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000539 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800540 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000541 Elf32_Sym& symbol = GetSymbol(section_type, i);
542 unsigned char type = ELF32_ST_TYPE(symbol.st_info);
543 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800544 continue;
545 }
546 const char* name = GetString(string_section, symbol.st_name);
547 if (name == NULL) {
548 continue;
549 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800550 std::pair<SymbolTable::iterator, bool> result =
551 (*symbol_table)->insert(std::make_pair(name, &symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800552 if (!result.second) {
553 // If a duplicate, make sure it has the same logical value. Seen on x86.
554 CHECK_EQ(symbol.st_value, result.first->second->st_value);
555 CHECK_EQ(symbol.st_size, result.first->second->st_size);
556 CHECK_EQ(symbol.st_info, result.first->second->st_info);
557 CHECK_EQ(symbol.st_other, result.first->second->st_other);
558 CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx);
559 }
560 }
561 }
562 CHECK(*symbol_table != NULL);
563 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
564 if (it == (*symbol_table)->end()) {
565 return NULL;
566 }
567 return it->second;
568 }
569
570 // Fall back to linear search
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000571 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800572 CHECK(symbol_section != NULL) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000573 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800574 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000575 Elf32_Sym& symbol = GetSymbol(section_type, i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800576 const char* name = GetString(string_section, symbol.st_name);
577 if (name == NULL) {
578 continue;
579 }
580 if (symbol_name == name) {
581 return &symbol;
582 }
583 }
584 return NULL;
585}
586
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000587Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800588 const std::string& symbol_name,
589 bool build_map) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000590 Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800591 if (symbol == NULL) {
592 return 0;
593 }
594 return symbol->st_value;
595}
596
Brian Carlstromc1409452014-02-26 14:06:23 -0800597const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800598 CHECK(!program_header_only_) << file_->GetPath();
599 // TODO: remove this static_cast from enum when using -std=gnu++0x
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000600 CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800601 CHECK_LT(i, string_section.sh_size) << file_->GetPath();
602 if (i == 0) {
603 return NULL;
604 }
605 byte* strings = Begin() + string_section.sh_offset;
606 byte* string = strings + i;
607 CHECK_LT(string, End()) << file_->GetPath();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800608 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800609}
610
Brian Carlstromc1409452014-02-26 14:06:23 -0800611Elf32_Word ElfFile::GetDynamicNum() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000612 return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800613}
614
Brian Carlstromc1409452014-02-26 14:06:23 -0800615Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800616 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
617 return *(GetDynamicSectionStart() + i);
618}
619
Brian Carlstromc1409452014-02-26 14:06:23 -0800620Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000621 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
622 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800623 if (elf_dyn.d_tag == type) {
624 return elf_dyn.d_un.d_val;
625 }
626 }
627 return 0;
628}
629
Brian Carlstromc1409452014-02-26 14:06:23 -0800630Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000631 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
632 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800633}
634
Brian Carlstromc1409452014-02-26 14:06:23 -0800635Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000636 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800637 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
638 return section_header.sh_size / section_header.sh_entsize;
639}
640
Brian Carlstromc1409452014-02-26 14:06:23 -0800641Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000642 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800643 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
644 return *(GetRelSectionStart(section_header) + i);
645}
646
Brian Carlstromc1409452014-02-26 14:06:23 -0800647Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000648 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
649 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800650}
651
Brian Carlstromc1409452014-02-26 14:06:23 -0800652Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000653 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800654 return section_header.sh_size / section_header.sh_entsize;
655}
656
Brian Carlstromc1409452014-02-26 14:06:23 -0800657Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000658 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800659 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
660 return *(GetRelaSectionStart(section_header) + i);
661}
662
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800663// Base on bionic phdr_table_get_load_size
Brian Carlstromc1409452014-02-26 14:06:23 -0800664size_t ElfFile::GetLoadedSize() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000665 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
666 Elf32_Addr max_vaddr = 0x00000000u;
667 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
668 Elf32_Phdr& program_header = GetProgramHeader(i);
669 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800670 continue;
671 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000672 Elf32_Addr begin_vaddr = program_header.p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800673 if (begin_vaddr < min_vaddr) {
674 min_vaddr = begin_vaddr;
675 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000676 Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800677 if (end_vaddr > max_vaddr) {
678 max_vaddr = end_vaddr;
679 }
680 }
681 min_vaddr = RoundDown(min_vaddr, kPageSize);
682 max_vaddr = RoundUp(max_vaddr, kPageSize);
683 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
684 size_t loaded_size = max_vaddr - min_vaddr;
685 return loaded_size;
686}
687
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700688bool ElfFile::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800689 CHECK(program_header_only_) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000690 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
691 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692
693 // Record .dynamic header information for later use
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000694 if (program_header.p_type == PT_DYNAMIC) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800695 dynamic_program_header_ = &program_header;
696 continue;
697 }
698
699 // Not something to load, move on.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000700 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800701 continue;
702 }
703
704 // Found something to load.
705
706 // If p_vaddr is zero, it must be the first loadable segment,
707 // since they must be in order. Since it is zero, there isn't a
708 // specific address requested, so first request a contiguous chunk
709 // of required size for all segments, but with no
710 // permissions. We'll then carve that up with the proper
711 // permissions as we load the actual segments. If p_vaddr is
712 // non-zero, the segments require the specific address specified,
713 // which either was specified in the file because we already set
714 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -0800715 int64_t temp_file_length = file_->GetLength();
716 if (temp_file_length < 0) {
717 errno = -temp_file_length;
718 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
719 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
720 return false;
721 }
722 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800723 if (program_header.p_vaddr == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700724 std::string reservation_name("ElfFile reservation for ");
725 reservation_name += file_->GetPath();
726 UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800727 NULL, GetLoadedSize(), PROT_NONE, false,
Brian Carlstromc1409452014-02-26 14:06:23 -0800728 error_msg));
729 if (reserve.get() == nullptr) {
730 *error_msg = StringPrintf("Failed to allocate %s: %s",
731 reservation_name.c_str(), error_msg->c_str());
732 return false;
733 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700734 base_address_ = reserve->Begin();
735 segments_.push_back(reserve.release());
736 }
737 // empty segment, nothing to map
738 if (program_header.p_memsz == 0) {
739 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800740 }
741 byte* p_vaddr = base_address_ + program_header.p_vaddr;
742 int prot = 0;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000743 if (executable && ((program_header.p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700744 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800745 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000746 if ((program_header.p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700747 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800748 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000749 if ((program_header.p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700750 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800751 }
752 int flags = MAP_FIXED;
753 if (writable_) {
754 prot |= PROT_WRITE;
755 flags |= MAP_SHARED;
756 } else {
757 flags |= MAP_PRIVATE;
758 }
Brian Carlstrom3a223612013-10-10 17:18:24 -0700759 if (file_length < (program_header.p_offset + program_header.p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800760 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700761 "%d of %d bytes: '%s'", file_length, i,
762 program_header.p_offset + program_header.p_memsz,
763 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700764 return false;
765 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800766 UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
767 program_header.p_memsz,
768 prot, flags, file_->Fd(),
769 program_header.p_offset,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700770 true,
771 file_->GetPath().c_str(),
772 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -0800773 if (segment.get() == nullptr) {
774 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
775 i, file_->GetPath().c_str(), error_msg->c_str());
776 return false;
777 }
778 if (segment->Begin() != p_vaddr) {
779 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
780 "instead mapped to %p",
781 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
782 return false;
783 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800784 segments_.push_back(segment.release());
785 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800786
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800787 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
788 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000789 = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr);
790 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
791 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800792 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
793 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000794 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800795 if (!ValidPointer(d_ptr)) {
796 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
797 d_ptr, file_->GetPath().c_str());
798 return false;
799 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000800 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800801 break;
802 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000803 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800804 if (!ValidPointer(d_ptr)) {
805 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
806 d_ptr, file_->GetPath().c_str());
807 return false;
808 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800809 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
810 break;
811 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000812 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800813 if (!ValidPointer(d_ptr)) {
814 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
815 d_ptr, file_->GetPath().c_str());
816 return false;
817 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000818 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800819 break;
820 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000821 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800822 if (GetDynamicNum() != i+1) {
823 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
824 "expected %d as implied by size of PT_DYNAMIC segment in %s",
825 i + 1, GetDynamicNum(), file_->GetPath().c_str());
826 return false;
827 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800828 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800829 }
830 }
831 }
832
833 return true;
834}
835
Brian Carlstromc1409452014-02-26 14:06:23 -0800836bool ElfFile::ValidPointer(const byte* start) const {
837 for (size_t i = 0; i < segments_.size(); ++i) {
838 const MemMap* segment = segments_[i];
839 if (segment->Begin() <= start && start < segment->End()) {
840 return true;
841 }
842 }
843 return false;
844}
845
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800846} // namespace art