ELFObjectLoaderImpl.cpp revision b519fe3f1d780873608f41d69316054e05c7e918
1/*
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
27#include "bcc/ExecutionEngine/SymbolResolverInterface.h"
28#include "bcc/Support/Log.h"
29
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) {
93        section_header_table[i].sh_addr =
94            reinterpret_cast<llvm::ELF::Elf32_Addr>(section->getBuffer());
95      }
96    }
97  }
98
99  return true;
100}
101
102void *ELFObjectLoaderImpl::getSymbolAddress(const char *pName) const {
103  if (mSymTab == NULL) {
104    return NULL;
105  }
106
107  ELFSymbol<32> *symbol = mSymTab->getByName(pName);
108  if (symbol == NULL) {
109    ALOGV("Request symbol '%s' is not found in the object!", pName);
110    return NULL;
111  }
112
113  return symbol->getAddress(mObject->getHeader()->getMachine(),
114                            /* autoAlloc */false);
115}
116
117ELFObjectLoaderImpl::~ELFObjectLoaderImpl() {
118  delete mObject;
119  return;
120}
121