summaryrefslogtreecommitdiffstats
path: root/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
blob: 6aeda0067a3ce628535b92fd75b8da896420256d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright 2012, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ELFObjectLoaderImpl.h"

#include <llvm/Support/ELF.h>

// The following files are included from librsloader.
#include "ELFObject.h"
#include "ELFSectionSymTab.h"
#include "ELFSymbol.h"
#include "utils/serialize.h"

#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
#include "bcc/Support/Log.h"

using namespace bcc;

bool ELFObjectLoaderImpl::load(const void *pMem, size_t pMemSize) {
  ArchiveReaderLE reader(reinterpret_cast<const unsigned char *>(pMem),
                         pMemSize);

#ifdef __LP64__
  mObject = ELFObject<64>::read(reader);
#else
  mObject = ELFObject<32>::read(reader);
#endif
  if (mObject == NULL) {
    ALOGE("Unable to load the ELF object!");
    return false;
  }

  // Retrive the pointer to the symbol table.
#ifdef __LP64__
  mSymTab = static_cast<ELFSectionSymTab<64> *>(
                 mObject->getSectionByName(".symtab"));
#else
  mSymTab = static_cast<ELFSectionSymTab<32> *>(
                 mObject->getSectionByName(".symtab"));
#endif
  if (mSymTab == NULL) {
    ALOGW("Object doesn't contain any symbol table.");
  }

  return true;
}

bool ELFObjectLoaderImpl::relocate(SymbolResolverInterface &pResolver) {
  mObject->relocate(SymbolResolverInterface::LookupFunction, &pResolver);

  if (mObject->getMissingSymbols()) {
    ALOGE("Some symbols are found to be undefined during relocation!");
    return false;
  }

  return true;
}

bool ELFObjectLoaderImpl::prepareDebugImage(void *pDebugImg,
                                            size_t pDebugImgSize) {
  // Update the value of sh_addr in pDebugImg to its corresponding section in
  // the mObject.
#ifdef __LP64__
  llvm::ELF::Elf64_Ehdr *elf_header =
      reinterpret_cast<llvm::ELF::Elf64_Ehdr *>(pDebugImg);
#else
  llvm::ELF::Elf32_Ehdr *elf_header =
       reinterpret_cast<llvm::ELF::Elf32_Ehdr *>(pDebugImg);
#endif

  if (elf_header->e_shoff > pDebugImgSize) {
#ifdef __LP64__
    ALOGE("Invalid section header table offset found! (e_shoff = %ld)",
	  elf_header->e_shoff);
#else
    ALOGE("Invalid section header table offset found! (e_shoff = %d)",
          elf_header->e_shoff);
#endif
    return false;
  }

  if ((elf_header->e_shoff +
       sizeof(llvm::ELF::Elf32_Shdr) * elf_header->e_shnum) > pDebugImgSize) {
#ifdef __LP64__
    ALOGE("Invalid image supplied (debug image doesn't contain all the section"
	  "header or corrupted image)! (e_shoff = %ld, e_shnum = %d)",
	  elf_header->e_shoff, elf_header->e_shnum);
#else
    ALOGE("Invalid image supplied (debug image doesn't contain all the section"
          "header or corrupted image)! (e_shoff = %d, e_shnum = %d)",
          elf_header->e_shoff, elf_header->e_shnum);
#endif
    return false;
  }

#ifdef __LP64__
  llvm::ELF::Elf64_Shdr *section_header_table =
      reinterpret_cast<llvm::ELF::Elf64_Shdr *>(
          reinterpret_cast<uint8_t*>(pDebugImg) + elf_header->e_shoff);
#else
  llvm::ELF::Elf32_Shdr *section_header_table =
      reinterpret_cast<llvm::ELF::Elf32_Shdr *>(
          reinterpret_cast<uint8_t*>(pDebugImg) + elf_header->e_shoff);
#endif

  for (unsigned i = 0; i < elf_header->e_shnum; i++) {
    if (section_header_table[i].sh_flags & llvm::ELF::SHF_ALLOC) {
#ifdef __LP64__
      ELFSectionBits<64> *section =
          static_cast<ELFSectionBits<64> *>(mObject->getSectionByIndex(i));
#else
      ELFSectionBits<32> *section =
          static_cast<ELFSectionBits<32> *>(mObject->getSectionByIndex(i));
#endif
      if (section != NULL) {
        uintptr_t address = reinterpret_cast<uintptr_t>(section->getBuffer());
#ifdef __LP64__
        LOG_FATAL_IF(address > 0xFFFFFFFFFFFFFFFFu, "Out of bound address for Elf64_Addr");
        section_header_table[i].sh_addr = static_cast<llvm::ELF::Elf64_Addr>(address);
#else
        LOG_FATAL_IF(address > 0xFFFFFFFFu, "Out of bound address for Elf32_Addr");
        section_header_table[i].sh_addr = static_cast<llvm::ELF::Elf32_Addr>(address);
#endif
      }
    }
  }

  return true;
}

void *ELFObjectLoaderImpl::getSymbolAddress(const char *pName) const {
  if (mSymTab == NULL) {
    return NULL;
  }

#ifdef __LP64__
  const ELFSymbol<64> *symbol = mSymTab->getByName(pName);
#else
  const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
#endif
  if (symbol == NULL) {
    ALOGV("Request symbol '%s' is not found in the object!", pName);
    return NULL;
  }

  return symbol->getAddress(mObject->getHeader()->getMachine(),
                            /* autoAlloc */false);
}

size_t ELFObjectLoaderImpl::getSymbolSize(const char *pName) const {
  if (mSymTab == NULL) {
    return 0;
  }

#ifdef __LP64__
  const ELFSymbol<64> *symbol = mSymTab->getByName(pName);
#else
  const ELFSymbol<32> *symbol = mSymTab->getByName(pName);
#endif

  if (symbol == NULL) {
    ALOGV("Request symbol '%s' is not found in the object!", pName);
    return 0;
  }

  return static_cast<size_t>(symbol->getSize());

}

bool
ELFObjectLoaderImpl::getSymbolNameList(android::Vector<const char *>& pNameList,
                                       ObjectLoader::SymbolType pType) const {
  if (mSymTab == NULL) {
    return false;
  }

  unsigned elf_type;
  switch (pType) {
    case ObjectLoader::kFunctionType: {
      elf_type = llvm::ELF::STT_FUNC;
      break;
    }
    case ObjectLoader::kUnknownType: {
      break;
    }
    default: {
      assert(false && "Invalid symbol type given!");
      return false;
    }
  }

  for (size_t i = 0, e = mSymTab->size(); i != e; i++) {
#ifdef __LP64__
    ELFSymbol<64> *symbol = (*mSymTab)[i];
#else
    ELFSymbol<32> *symbol = (*mSymTab)[i];
#endif
    if (symbol == NULL) {
      continue;
    }

    if ((pType == ObjectLoader::kUnknownType) ||
        (symbol->getType() == elf_type)) {
      const char *symbol_name = symbol->getName();
      if (symbol_name != NULL) {
        pNameList.push_back(symbol_name);
      }
    }
  }

  return true;
}

ELFObjectLoaderImpl::~ELFObjectLoaderImpl() {
  delete mObject;
  return;
}