blob: 603695d453114c1f756c4d16259d3192ba44aba3 [file] [log] [blame]
Zonr Chang0f9cad92012-04-13 14:35:45 +08001/*
2 * Copyright 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 "ELFObjectLoaderImpl.h"
18
19#include <llvm/Support/ELF.h>
20
21// The following files are included from librsloader.
22#include "ELFObject.h"
23#include "ELFSectionSymTab.h"
24#include "ELFSymbol.h"
25#include "utils/serialize.h"
26
Zonr Changc72c4dd2012-04-12 15:38:53 +080027#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
Zonr Changef73a242012-04-12 16:44:01 +080028#include "bcc/Support/Log.h"
Zonr Chang0f9cad92012-04-13 14:35:45 +080029
30using namespace bcc;
31
32bool ELFObjectLoaderImpl::load(const void *pMem, size_t pMemSize) {
33 ArchiveReaderLE reader(reinterpret_cast<const unsigned char *>(pMem),
34 pMemSize);
35
36 mObject = ELFObject<32>::read(reader);
37 if (mObject == NULL) {
38 ALOGE("Unable to load the ELF object!");
39 return false;
40 }
41
42 // Retrive the pointer to the symbol table.
43 mSymTab = static_cast<ELFSectionSymTab<32> *>(
44 mObject->getSectionByName(".symtab"));
45 if (mSymTab == NULL) {
46 ALOGW("Object doesn't contain any symbol table.");
47 }
48
49 return true;
50}
51
52bool ELFObjectLoaderImpl::relocate(SymbolResolverInterface &pResolver) {
53 mObject->relocate(SymbolResolverInterface::LookupFunction, &pResolver);
54
55 if (mObject->getMissingSymbols()) {
56 ALOGE("Some symbols are found to be undefined during relocation!");
57 return false;
58 }
59
60 return true;
61}
62
63bool ELFObjectLoaderImpl::prepareDebugImage(void *pDebugImg,
64 size_t pDebugImgSize) {
65 // Update the value of sh_addr in pDebugImg to its corresponding section in
66 // the mObject.
67 llvm::ELF::Elf32_Ehdr *elf_header =
68 reinterpret_cast<llvm::ELF::Elf32_Ehdr *>(pDebugImg);
69
70 if (elf_header->e_shoff > pDebugImgSize) {
71 ALOGE("Invalid section header table offset found! (e_shoff = %d)",
72 elf_header->e_shoff);
73 return false;
74 }
75
76 if ((elf_header->e_shoff +
77 sizeof(llvm::ELF::Elf32_Shdr) * elf_header->e_shnum) > pDebugImgSize) {
78 ALOGE("Invalid image supplied (debug image doesn't contain all the section"
79 "header or corrupted image)! (e_shoff = %d, e_shnum = %d)",
80 elf_header->e_shoff, elf_header->e_shnum);
81 return false;
82 }
83
84 llvm::ELF::Elf32_Shdr *section_header_table =
85 reinterpret_cast<llvm::ELF::Elf32_Shdr *>(
86 reinterpret_cast<uint8_t*>(pDebugImg) + elf_header->e_shoff);
87
88 for (unsigned i = 0; i < elf_header->e_shnum; i++) {
89 if (section_header_table[i].sh_flags & llvm::ELF::SHF_ALLOC) {
90 ELFSectionBits<32> *section =
91 static_cast<ELFSectionBits<32> *>(mObject->getSectionByIndex(i));
92 if (section != NULL) {
Ian Rogersbe07e232014-01-29 15:48:23 -080093 uintptr_t address = reinterpret_cast<uintptr_t>(section->getBuffer());
94 LOG_FATAL_IF(address > 0xFFFFFFFFu, "Out of bound address for Elf32_Addr");
95 section_header_table[i].sh_addr = static_cast<llvm::ELF::Elf32_Addr>(address);
Zonr Chang0f9cad92012-04-13 14:35:45 +080096 }
97 }
98 }
99
100 return true;
101}
102
103void *ELFObjectLoaderImpl::getSymbolAddress(const char *pName) const {
104 if (mSymTab == NULL) {
105 return NULL;
106 }
107
Shih-wei Liao97957542012-07-22 15:42:53 -0700108 const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
Zonr Chang0f9cad92012-04-13 14:35:45 +0800109 if (symbol == NULL) {
110 ALOGV("Request symbol '%s' is not found in the object!", pName);
111 return NULL;
112 }
113
114 return symbol->getAddress(mObject->getHeader()->getMachine(),
115 /* autoAlloc */false);
116}
117
Shih-wei Liao97957542012-07-22 15:42:53 -0700118size_t ELFObjectLoaderImpl::getSymbolSize(const char *pName) const {
119 if (mSymTab == NULL) {
120 return 0;
121 }
122
123 const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
124
125 if (symbol == NULL) {
126 ALOGV("Request symbol '%s' is not found in the object!", pName);
127 return 0;
128 }
129
130 return static_cast<size_t>(symbol->getSize());
131
132}
133
134bool
135ELFObjectLoaderImpl::getSymbolNameList(android::Vector<const char *>& pNameList,
136 ObjectLoader::SymbolType pType) const {
137 if (mSymTab == NULL) {
138 return false;
139 }
140
141 unsigned elf_type;
142 switch (pType) {
143 case ObjectLoader::kFunctionType: {
144 elf_type = llvm::ELF::STT_FUNC;
145 break;
146 }
147 case ObjectLoader::kUnknownType: {
148 break;
149 }
150 default: {
151 assert(false && "Invalid symbol type given!");
152 return false;
153 }
154 }
155
156 for (size_t i = 0, e = mSymTab->size(); i != e; i++) {
157 ELFSymbol<32> *symbol = (*mSymTab)[i];
158 if (symbol == NULL) {
159 continue;
160 }
161
162 if ((pType == ObjectLoader::kUnknownType) ||
163 (symbol->getType() == elf_type)) {
164 const char *symbol_name = symbol->getName();
165 if (symbol_name != NULL) {
166 pNameList.push_back(symbol_name);
167 }
168 }
169 }
170
171 return true;
172}
173
Zonr Chang0f9cad92012-04-13 14:35:45 +0800174ELFObjectLoaderImpl::~ELFObjectLoaderImpl() {
175 delete mObject;
176 return;
177}