blob: 2f7c38a71fc7adbaa29c96c29477a626ba9e84f0 [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 Carlstrom02c8cc62013-07-18 15:54:44 -070025ElfFile::ElfFile()
26 : file_(NULL),
27 writable_(false),
28 program_header_only_(false),
29 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),
41 dynsym_symbol_table_(NULL) {}
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042
Ian Rogers8d31bbd2013-10-13 10:44:14 -070043ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
44 std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045 UniquePtr<ElfFile> elf_file(new ElfFile());
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 if (!elf_file->Setup(file, writable, program_header_only, error_msg)) {
47 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048 }
49 return elf_file.release();
50}
51
Ian Rogers8d31bbd2013-10-13 10:44:14 -070052bool ElfFile::Setup(File* file, bool writable, bool program_header_only, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 CHECK(file != NULL);
54 file_ = file;
55 writable_ = writable;
56 program_header_only_ = program_header_only;
57
58 int prot;
59 int flags;
60 if (writable_) {
61 prot = PROT_READ | PROT_WRITE;
62 flags = MAP_SHARED;
63 } else {
64 prot = PROT_READ;
65 flags = MAP_PRIVATE;
66 }
Ian Rogerscdfcf372014-01-23 20:38:36 -080067 int64_t temp_file_length = file_->GetLength();
68 if (temp_file_length < 0) {
69 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070070 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
71 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -080072 return false;
73 }
Ian Rogerscdfcf372014-01-23 20:38:36 -080074 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom265091e2013-01-30 14:08:26 -080075 if (file_length < sizeof(llvm::ELF::Elf32_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -080076 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Ian Rogers8d31bbd2013-10-13 10:44:14 -070077 "%zd bytes: '%s'", file_length, sizeof(llvm::ELF::Elf32_Ehdr),
78 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079 return false;
80 }
81
82 if (program_header_only) {
83 // first just map ELF header to get program header size information
84 size_t elf_header_size = sizeof(llvm::ELF::Elf32_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -080086 file_->GetPath().c_str(), error_msg),
87 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088 return false;
89 }
90 // then remap to cover program header
91 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -070092 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -080093 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -070094 "header of %zd bytes: '%s'", file_length,
95 sizeof(llvm::ELF::Elf32_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -070096 return false;
97 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -080099 file_->GetPath().c_str(), error_msg),
100 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 return false;
103 }
104 } else {
105 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800107 file_->GetPath().c_str(), error_msg),
108 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700109 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 return false;
111 }
112 }
113
114 // Either way, the program header is relative to the elf header
115 program_headers_start_ = Begin() + GetHeader().e_phoff;
116
117 if (!program_header_only) {
118 // Setup section headers.
119 section_headers_start_ = Begin() + GetHeader().e_shoff;
120
121 // Find .dynamic section info from program header
122 dynamic_program_header_ = FindProgamHeaderByType(llvm::ELF::PT_DYNAMIC);
123 if (dynamic_program_header_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
125 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800126 return false;
127 }
128
129 dynamic_section_start_
130 = reinterpret_cast<llvm::ELF::Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset);
131
132 // Find other sections from section headers
133 for (llvm::ELF::Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
134 llvm::ELF::Elf32_Shdr& section_header = GetSectionHeader(i);
135 byte* section_addr = Begin() + section_header.sh_offset;
136 switch (section_header.sh_type) {
137 case llvm::ELF::SHT_SYMTAB: {
138 symtab_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(section_addr);
139 break;
140 }
141 case llvm::ELF::SHT_DYNSYM: {
142 dynsym_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(section_addr);
143 break;
144 }
145 case llvm::ELF::SHT_STRTAB: {
146 // TODO: base these off of sh_link from .symtab and .dynsym above
147 if ((section_header.sh_flags & llvm::ELF::SHF_ALLOC) != 0) {
148 dynstr_section_start_ = reinterpret_cast<char*>(section_addr);
149 } else {
150 strtab_section_start_ = reinterpret_cast<char*>(section_addr);
151 }
152 break;
153 }
154 case llvm::ELF::SHT_DYNAMIC: {
155 if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) {
156 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800157 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800158 << reinterpret_cast<void*>(dynamic_section_start_)
159 << " != " << reinterpret_cast<void*>(section_addr);
160 return false;
161 }
162 break;
163 }
164 case llvm::ELF::SHT_HASH: {
165 hash_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Word*>(section_addr);
166 break;
167 }
168 }
169 }
170 }
171 return true;
172}
173
174ElfFile::~ElfFile() {
175 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800176 delete symtab_symbol_table_;
177 delete dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178}
179
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800180bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 if (map == NULL) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800182 // MemMap::Open should have already set an error.
183 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184 return false;
185 }
186 map_.reset(map);
187 CHECK(map_.get() != NULL) << file_->GetPath();
188 CHECK(map_->Begin() != NULL) << file_->GetPath();
189
190 header_ = reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(map_->Begin());
191 if ((llvm::ELF::ElfMagic[0] != header_->e_ident[llvm::ELF::EI_MAG0])
192 || (llvm::ELF::ElfMagic[1] != header_->e_ident[llvm::ELF::EI_MAG1])
193 || (llvm::ELF::ElfMagic[2] != header_->e_ident[llvm::ELF::EI_MAG2])
194 || (llvm::ELF::ElfMagic[3] != header_->e_ident[llvm::ELF::EI_MAG3])) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800195 *error_msg = StringPrintf("Failed to find ELF magic in %s: %c%c%c%c",
196 file_->GetPath().c_str(),
197 header_->e_ident[llvm::ELF::EI_MAG0],
198 header_->e_ident[llvm::ELF::EI_MAG1],
199 header_->e_ident[llvm::ELF::EI_MAG2],
200 header_->e_ident[llvm::ELF::EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203
204
205 // TODO: remove these static_casts from enum when using -std=gnu++0x
206 CHECK_EQ(static_cast<unsigned char>(llvm::ELF::ELFCLASS32), header_->e_ident[llvm::ELF::EI_CLASS]) << file_->GetPath();
207 CHECK_EQ(static_cast<unsigned char>(llvm::ELF::ELFDATA2LSB), header_->e_ident[llvm::ELF::EI_DATA]) << file_->GetPath();
208 CHECK_EQ(static_cast<unsigned char>(llvm::ELF::EV_CURRENT), header_->e_ident[llvm::ELF::EI_VERSION]) << file_->GetPath();
209
210 // TODO: remove these static_casts from enum when using -std=gnu++0x
211 CHECK_EQ(static_cast<llvm::ELF::Elf32_Half>(llvm::ELF::ET_DYN), header_->e_type) << file_->GetPath();
212 CHECK_EQ(static_cast<llvm::ELF::Elf32_Word>(llvm::ELF::EV_CURRENT), header_->e_version) << file_->GetPath();
213 CHECK_EQ(0U, header_->e_entry) << file_->GetPath();
214
215 CHECK_NE(0U, header_->e_phoff) << file_->GetPath();
216 CHECK_NE(0U, header_->e_shoff) << file_->GetPath();
217 CHECK_NE(0U, header_->e_ehsize) << file_->GetPath();
218 CHECK_NE(0U, header_->e_phentsize) << file_->GetPath();
219 CHECK_NE(0U, header_->e_phnum) << file_->GetPath();
220 CHECK_NE(0U, header_->e_shentsize) << file_->GetPath();
221 CHECK_NE(0U, header_->e_shnum) << file_->GetPath();
222 CHECK_NE(0U, header_->e_shstrndx) << file_->GetPath();
223 CHECK_GE(header_->e_shnum, header_->e_shstrndx) << file_->GetPath();
224 if (!program_header_only_) {
225 CHECK_GT(Size(), header_->e_phoff) << file_->GetPath();
226 CHECK_GT(Size(), header_->e_shoff) << file_->GetPath();
227 }
228 return true;
229}
230
231
232llvm::ELF::Elf32_Ehdr& ElfFile::GetHeader() {
233 CHECK(header_ != NULL);
234 return *header_;
235}
236
237byte* ElfFile::GetProgramHeadersStart() {
238 CHECK(program_headers_start_ != NULL);
239 return program_headers_start_;
240}
241
242byte* ElfFile::GetSectionHeadersStart() {
243 CHECK(section_headers_start_ != NULL);
244 return section_headers_start_;
245}
246
247llvm::ELF::Elf32_Phdr& ElfFile::GetDynamicProgramHeader() {
248 CHECK(dynamic_program_header_ != NULL);
249 return *dynamic_program_header_;
250}
251
252llvm::ELF::Elf32_Dyn* ElfFile::GetDynamicSectionStart() {
253 CHECK(dynamic_section_start_ != NULL);
254 return dynamic_section_start_;
255}
256
257llvm::ELF::Elf32_Sym* ElfFile::GetSymbolSectionStart(llvm::ELF::Elf32_Word section_type) {
258 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
259 llvm::ELF::Elf32_Sym* symbol_section_start;
260 switch (section_type) {
261 case llvm::ELF::SHT_SYMTAB: {
262 symbol_section_start = symtab_section_start_;
263 break;
264 }
265 case llvm::ELF::SHT_DYNSYM: {
266 symbol_section_start = dynsym_section_start_;
267 break;
268 }
269 default: {
270 LOG(FATAL) << section_type;
271 symbol_section_start = NULL;
272 }
273 }
274 CHECK(symbol_section_start != NULL);
275 return symbol_section_start;
276}
277
Brian Carlstrom265091e2013-01-30 14:08:26 -0800278const char* ElfFile::GetStringSectionStart(llvm::ELF::Elf32_Word section_type) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800279 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800280 const char* string_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800281 switch (section_type) {
282 case llvm::ELF::SHT_SYMTAB: {
283 string_section_start = strtab_section_start_;
284 break;
285 }
286 case llvm::ELF::SHT_DYNSYM: {
287 string_section_start = dynstr_section_start_;
288 break;
289 }
290 default: {
291 LOG(FATAL) << section_type;
292 string_section_start = NULL;
293 }
294 }
295 CHECK(string_section_start != NULL);
296 return string_section_start;
297}
298
Brian Carlstrom265091e2013-01-30 14:08:26 -0800299const char* ElfFile::GetString(llvm::ELF::Elf32_Word section_type, llvm::ELF::Elf32_Word i) {
300 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
301 if (i == 0) {
302 return NULL;
303 }
304 const char* string_section_start = GetStringSectionStart(section_type);
305 const char* string = string_section_start + i;
306 return string;
307}
308
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800309llvm::ELF::Elf32_Word* ElfFile::GetHashSectionStart() {
310 CHECK(hash_section_start_ != NULL);
311 return hash_section_start_;
312}
313
314llvm::ELF::Elf32_Word ElfFile::GetHashBucketNum() {
315 return GetHashSectionStart()[0];
316}
317
318llvm::ELF::Elf32_Word ElfFile::GetHashChainNum() {
319 return GetHashSectionStart()[1];
320}
321
322llvm::ELF::Elf32_Word ElfFile::GetHashBucket(size_t i) {
323 CHECK_LT(i, GetHashBucketNum());
324 // 0 is nbucket, 1 is nchain
325 return GetHashSectionStart()[2 + i];
326}
327
328llvm::ELF::Elf32_Word ElfFile::GetHashChain(size_t i) {
329 CHECK_LT(i, GetHashChainNum());
330 // 0 is nbucket, 1 is nchain, & chains are after buckets
331 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
332}
333
334llvm::ELF::Elf32_Word ElfFile::GetProgramHeaderNum() {
335 return GetHeader().e_phnum;
336}
337
338llvm::ELF::Elf32_Phdr& ElfFile::GetProgramHeader(llvm::ELF::Elf32_Word i) {
339 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath();
340 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
341 CHECK_LT(program_header, End()) << file_->GetPath();
342 return *reinterpret_cast<llvm::ELF::Elf32_Phdr*>(program_header);
343}
344
345llvm::ELF::Elf32_Phdr* ElfFile::FindProgamHeaderByType(llvm::ELF::Elf32_Word type) {
346 for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
347 llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i);
348 if (program_header.p_type == type) {
349 return &program_header;
350 }
351 }
352 return NULL;
353}
354
355llvm::ELF::Elf32_Word ElfFile::GetSectionHeaderNum() {
356 return GetHeader().e_shnum;
357}
358
359llvm::ELF::Elf32_Shdr& ElfFile::GetSectionHeader(llvm::ELF::Elf32_Word i) {
360 // Can only access arbitrary sections when we have the whole file, not just program header.
361 // Even if we Load(), it doesn't bring in all the sections.
362 CHECK(!program_header_only_) << file_->GetPath();
363 CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath();
364 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
365 CHECK_LT(section_header, End()) << file_->GetPath();
366 return *reinterpret_cast<llvm::ELF::Elf32_Shdr*>(section_header);
367}
368
369llvm::ELF::Elf32_Shdr* ElfFile::FindSectionByType(llvm::ELF::Elf32_Word type) {
370 // Can only access arbitrary sections when we have the whole file, not just program header.
371 // We could change this to switch on known types if they were detected during loading.
372 CHECK(!program_header_only_) << file_->GetPath();
373 for (llvm::ELF::Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
374 llvm::ELF::Elf32_Shdr& section_header = GetSectionHeader(i);
375 if (section_header.sh_type == type) {
376 return &section_header;
377 }
378 }
379 return NULL;
380}
381
382// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800383static unsigned elfhash(const char *_name) {
384 const unsigned char *name = (const unsigned char *) _name;
385 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800386
Brian Carlstromdf629502013-07-17 22:39:56 -0700387 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800388 h = (h << 4) + *name++;
389 g = h & 0xf0000000;
390 h ^= g;
391 h ^= g >> 24;
392 }
393 return h;
394}
395
396llvm::ELF::Elf32_Shdr& ElfFile::GetSectionNameStringSection() {
397 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800398}
399
400byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) {
401 llvm::ELF::Elf32_Word hash = elfhash(symbol_name.c_str());
402 llvm::ELF::Elf32_Word bucket_index = hash % GetHashBucketNum();
403 llvm::ELF::Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800404 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
405 llvm::ELF::Elf32_Sym& symbol = GetSymbol(llvm::ELF::SHT_DYNSYM, symbol_and_chain_index);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800406 const char* name = GetString(llvm::ELF::SHT_DYNSYM, symbol.st_name);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800407 if (symbol_name == name) {
408 return base_address_ + symbol.st_value;
409 }
410 symbol_and_chain_index = GetHashChain(symbol_and_chain_index);
411 }
412 return NULL;
413}
414
415bool ElfFile::IsSymbolSectionType(llvm::ELF::Elf32_Word section_type) {
416 return ((section_type == llvm::ELF::SHT_SYMTAB) || (section_type == llvm::ELF::SHT_DYNSYM));
417}
418
419llvm::ELF::Elf32_Word ElfFile::GetSymbolNum(llvm::ELF::Elf32_Shdr& section_header) {
420 CHECK(IsSymbolSectionType(section_header.sh_type)) << file_->GetPath() << " " << section_header.sh_type;
421 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
422 return section_header.sh_size / section_header.sh_entsize;
423}
424
425llvm::ELF::Elf32_Sym& ElfFile::GetSymbol(llvm::ELF::Elf32_Word section_type,
426 llvm::ELF::Elf32_Word i) {
427 return *(GetSymbolSectionStart(section_type) + i);
428}
429
Brian Carlstrom265091e2013-01-30 14:08:26 -0800430ElfFile::SymbolTable** ElfFile::GetSymbolTable(llvm::ELF::Elf32_Word section_type) {
431 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
432 switch (section_type) {
433 case llvm::ELF::SHT_SYMTAB: {
434 return &symtab_symbol_table_;
435 }
436 case llvm::ELF::SHT_DYNSYM: {
437 return &dynsym_symbol_table_;
438 }
439 default: {
440 LOG(FATAL) << section_type;
441 return NULL;
442 }
443 }
444}
445
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800446llvm::ELF::Elf32_Sym* ElfFile::FindSymbolByName(llvm::ELF::Elf32_Word section_type,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800447 const std::string& symbol_name,
448 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 CHECK(!program_header_only_) << file_->GetPath();
450 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800451
452 SymbolTable** symbol_table = GetSymbolTable(section_type);
453 if (*symbol_table != NULL || build_map) {
454 if (*symbol_table == NULL) {
455 DCHECK(build_map);
456 *symbol_table = new SymbolTable;
457 llvm::ELF::Elf32_Shdr* symbol_section = FindSectionByType(section_type);
458 CHECK(symbol_section != NULL) << file_->GetPath();
459 llvm::ELF::Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
460 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
461 llvm::ELF::Elf32_Sym& symbol = GetSymbol(section_type, i);
462 unsigned char type = symbol.getType();
463 if (type == llvm::ELF::STT_NOTYPE) {
464 continue;
465 }
466 const char* name = GetString(string_section, symbol.st_name);
467 if (name == NULL) {
468 continue;
469 }
470 std::pair<SymbolTable::iterator, bool> result = (*symbol_table)->insert(std::make_pair(name, &symbol));
471 if (!result.second) {
472 // If a duplicate, make sure it has the same logical value. Seen on x86.
473 CHECK_EQ(symbol.st_value, result.first->second->st_value);
474 CHECK_EQ(symbol.st_size, result.first->second->st_size);
475 CHECK_EQ(symbol.st_info, result.first->second->st_info);
476 CHECK_EQ(symbol.st_other, result.first->second->st_other);
477 CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx);
478 }
479 }
480 }
481 CHECK(*symbol_table != NULL);
482 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
483 if (it == (*symbol_table)->end()) {
484 return NULL;
485 }
486 return it->second;
487 }
488
489 // Fall back to linear search
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800490 llvm::ELF::Elf32_Shdr* symbol_section = FindSectionByType(section_type);
491 CHECK(symbol_section != NULL) << file_->GetPath();
492 llvm::ELF::Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
493 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
494 llvm::ELF::Elf32_Sym& symbol = GetSymbol(section_type, i);
495 const char* name = GetString(string_section, symbol.st_name);
496 if (name == NULL) {
497 continue;
498 }
499 if (symbol_name == name) {
500 return &symbol;
501 }
502 }
503 return NULL;
504}
505
506llvm::ELF::Elf32_Addr ElfFile::FindSymbolAddress(llvm::ELF::Elf32_Word section_type,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800507 const std::string& symbol_name,
508 bool build_map) {
509 llvm::ELF::Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800510 if (symbol == NULL) {
511 return 0;
512 }
513 return symbol->st_value;
514}
515
Brian Carlstrom265091e2013-01-30 14:08:26 -0800516const char* ElfFile::GetString(llvm::ELF::Elf32_Shdr& string_section, llvm::ELF::Elf32_Word i) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800517 CHECK(!program_header_only_) << file_->GetPath();
518 // TODO: remove this static_cast from enum when using -std=gnu++0x
519 CHECK_EQ(static_cast<llvm::ELF::Elf32_Word>(llvm::ELF::SHT_STRTAB), string_section.sh_type) << file_->GetPath();
520 CHECK_LT(i, string_section.sh_size) << file_->GetPath();
521 if (i == 0) {
522 return NULL;
523 }
524 byte* strings = Begin() + string_section.sh_offset;
525 byte* string = strings + i;
526 CHECK_LT(string, End()) << file_->GetPath();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800527 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800528}
529
530llvm::ELF::Elf32_Word ElfFile::GetDynamicNum() {
531 return GetDynamicProgramHeader().p_filesz / sizeof(llvm::ELF::Elf32_Dyn);
532}
533
534llvm::ELF::Elf32_Dyn& ElfFile::GetDynamic(llvm::ELF::Elf32_Word i) {
535 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
536 return *(GetDynamicSectionStart() + i);
537}
538
Brian Carlstrom265091e2013-01-30 14:08:26 -0800539llvm::ELF::Elf32_Word ElfFile::FindDynamicValueByType(llvm::ELF::Elf32_Sword type) {
540 for (llvm::ELF::Elf32_Word i = 0; i < GetDynamicNum(); i++) {
541 llvm::ELF::Elf32_Dyn& elf_dyn = GetDynamic(i);
542 if (elf_dyn.d_tag == type) {
543 return elf_dyn.d_un.d_val;
544 }
545 }
546 return 0;
547}
548
549llvm::ELF::Elf32_Rel* ElfFile::GetRelSectionStart(llvm::ELF::Elf32_Shdr& section_header) {
550 CHECK(llvm::ELF::SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
551 return reinterpret_cast<llvm::ELF::Elf32_Rel*>(Begin() + section_header.sh_offset);
552}
553
554llvm::ELF::Elf32_Word ElfFile::GetRelNum(llvm::ELF::Elf32_Shdr& section_header) {
555 CHECK(llvm::ELF::SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
556 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
557 return section_header.sh_size / section_header.sh_entsize;
558}
559
560llvm::ELF::Elf32_Rel& ElfFile::GetRel(llvm::ELF::Elf32_Shdr& section_header, llvm::ELF::Elf32_Word i) {
561 CHECK(llvm::ELF::SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
562 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
563 return *(GetRelSectionStart(section_header) + i);
564}
565
566llvm::ELF::Elf32_Rela* ElfFile::GetRelaSectionStart(llvm::ELF::Elf32_Shdr& section_header) {
567 CHECK(llvm::ELF::SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
568 return reinterpret_cast<llvm::ELF::Elf32_Rela*>(Begin() + section_header.sh_offset);
569}
570
571llvm::ELF::Elf32_Word ElfFile::GetRelaNum(llvm::ELF::Elf32_Shdr& section_header) {
572 CHECK(llvm::ELF::SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
573 return section_header.sh_size / section_header.sh_entsize;
574}
575
576llvm::ELF::Elf32_Rela& ElfFile::GetRela(llvm::ELF::Elf32_Shdr& section_header,
577 llvm::ELF::Elf32_Word i) {
578 CHECK(llvm::ELF::SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
579 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
580 return *(GetRelaSectionStart(section_header) + i);
581}
582
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800583// Base on bionic phdr_table_get_load_size
584size_t ElfFile::GetLoadedSize() {
585 llvm::ELF::Elf32_Addr min_vaddr = 0xFFFFFFFFu;
586 llvm::ELF::Elf32_Addr max_vaddr = 0x00000000u;
587 for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
588 llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i);
589 if (program_header.p_type != llvm::ELF::PT_LOAD) {
590 continue;
591 }
592 llvm::ELF::Elf32_Addr begin_vaddr = program_header.p_vaddr;
593 if (begin_vaddr < min_vaddr) {
594 min_vaddr = begin_vaddr;
595 }
596 llvm::ELF::Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz;
597 if (end_vaddr > max_vaddr) {
598 max_vaddr = end_vaddr;
599 }
600 }
601 min_vaddr = RoundDown(min_vaddr, kPageSize);
602 max_vaddr = RoundUp(max_vaddr, kPageSize);
603 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
604 size_t loaded_size = max_vaddr - min_vaddr;
605 return loaded_size;
606}
607
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700608bool ElfFile::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800609 // TODO: actually return false error
610 CHECK(program_header_only_) << file_->GetPath();
611 for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
612 llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i);
613
614 // Record .dynamic header information for later use
615 if (program_header.p_type == llvm::ELF::PT_DYNAMIC) {
616 dynamic_program_header_ = &program_header;
617 continue;
618 }
619
620 // Not something to load, move on.
621 if (program_header.p_type != llvm::ELF::PT_LOAD) {
622 continue;
623 }
624
625 // Found something to load.
626
627 // If p_vaddr is zero, it must be the first loadable segment,
628 // since they must be in order. Since it is zero, there isn't a
629 // specific address requested, so first request a contiguous chunk
630 // of required size for all segments, but with no
631 // permissions. We'll then carve that up with the proper
632 // permissions as we load the actual segments. If p_vaddr is
633 // non-zero, the segments require the specific address specified,
634 // which either was specified in the file because we already set
635 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -0800636 int64_t temp_file_length = file_->GetLength();
637 if (temp_file_length < 0) {
638 errno = -temp_file_length;
639 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
640 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
641 return false;
642 }
643 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800644 if (program_header.p_vaddr == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700645 std::string reservation_name("ElfFile reservation for ");
646 reservation_name += file_->GetPath();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700647 std::string error_msg;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700648 UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700649 NULL, GetLoadedSize(), PROT_NONE, &error_msg));
650 CHECK(reserve.get() != NULL) << file_->GetPath() << ": " << error_msg;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700651 base_address_ = reserve->Begin();
652 segments_.push_back(reserve.release());
653 }
654 // empty segment, nothing to map
655 if (program_header.p_memsz == 0) {
656 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800657 }
658 byte* p_vaddr = base_address_ + program_header.p_vaddr;
659 int prot = 0;
Brian Carlstromf1d34552013-07-12 20:22:23 -0700660 if (executable && ((program_header.p_flags & llvm::ELF::PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700661 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800662 }
663 if ((program_header.p_flags & llvm::ELF::PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700664 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800665 }
666 if ((program_header.p_flags & llvm::ELF::PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700667 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800668 }
669 int flags = MAP_FIXED;
670 if (writable_) {
671 prot |= PROT_WRITE;
672 flags |= MAP_SHARED;
673 } else {
674 flags |= MAP_PRIVATE;
675 }
Brian Carlstrom3a223612013-10-10 17:18:24 -0700676 if (file_length < (program_header.p_offset + program_header.p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800677 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700678 "%d of %d bytes: '%s'", file_length, i,
679 program_header.p_offset + program_header.p_memsz,
680 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700681 return false;
682 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800683 UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
684 program_header.p_memsz,
685 prot, flags, file_->Fd(),
686 program_header.p_offset,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700687 true,
688 file_->GetPath().c_str(),
689 error_msg));
690 CHECK(segment.get() != nullptr) << *error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800691 CHECK_EQ(segment->Begin(), p_vaddr) << file_->GetPath();
692 segments_.push_back(segment.release());
693 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800694
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800695 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
696 dynamic_section_start_
697 = reinterpret_cast<llvm::ELF::Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr);
698 for (llvm::ELF::Elf32_Word i = 0; i < GetDynamicNum(); i++) {
699 llvm::ELF::Elf32_Dyn& elf_dyn = GetDynamic(i);
700 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
701 switch (elf_dyn.d_tag) {
702 case llvm::ELF::DT_HASH: {
703 hash_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Word*>(d_ptr);
704 break;
705 }
706 case llvm::ELF::DT_STRTAB: {
707 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
708 break;
709 }
710 case llvm::ELF::DT_SYMTAB: {
711 dynsym_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(d_ptr);
712 break;
713 }
714 case llvm::ELF::DT_NULL: {
715 CHECK_EQ(GetDynamicNum(), i+1);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800716 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800717 }
718 }
719 }
720
721 return true;
722}
723
724} // namespace art