librsloader.cpp revision 50b814ae8797a929bc0416d6a97e3551b24eaf30
1/*
2 * Copyright 2011, 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 "librsloader.h"
18
19#include "ELFObject.h"
20#include "ELFSectionSymTab.h"
21#include "ELFSymbol.h"
22
23#include "utils/serialize.h"
24
25#define LOG_TAG "bcc"
26#include "cutils/log.h"
27
28#include <llvm/ADT/OwningPtr.h>
29
30static inline RSExecRef wrap(ELFObject<32> *object) {
31  return reinterpret_cast<RSExecRef>(object);
32}
33
34static inline ELFObject<32> *unwrap(RSExecRef object) {
35  return reinterpret_cast<ELFObject<32> *>(object);
36}
37
38extern "C" RSExecRef
39rsloaderCreateExec(unsigned char const *buf,
40                   size_t buf_size,
41                   void *(*find_symbol)(void *, char const *),
42                   void *find_symbol_context) {
43
44  ArchiveReaderLE AR(buf, buf_size);
45
46  llvm::OwningPtr<ELFObject<32> > object(ELFObject<32>::read(AR));
47  if (!object) {
48    ALOGE("Unable to load the ELF object.");
49    return NULL;
50  }
51
52  //object->print();
53  object->relocate(find_symbol, find_symbol_context);
54  if (object->getMissingSymbols()) {
55    return NULL;
56  }
57
58  return wrap(object.take());
59}
60
61extern "C" void rsloaderDisposeExec(RSExecRef object) {
62  delete unwrap(object);
63}
64
65extern "C" void *rsloaderGetSymbolAddress(RSExecRef object_,
66                                          char const *name) {
67  ELFObject<32> *object = unwrap(object_);
68
69  ELFSectionSymTab<32> *symtab =
70    static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab"));
71
72  if (!symtab) {
73    return NULL;
74  }
75
76  ELFSymbol<32> *symbol = symtab->getByName(name);
77
78  if (!symbol) {
79    ALOGV("Symbol not found: %s\n", name);
80    return NULL;
81  }
82
83  int machine = object->getHeader()->getMachine();
84
85  return symbol->getAddress(machine, false);
86}
87
88extern "C" size_t rsloaderGetSymbolSize(RSExecRef object_, char const *name) {
89  ELFObject<32> *object = unwrap(object_);
90
91  ELFSectionSymTab<32> *symtab =
92    static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab"));
93
94  if (!symtab) {
95    return NULL;
96  }
97
98  ELFSymbol<32> *symbol = symtab->getByName(name);
99
100  if (!symbol) {
101    ALOGV("Symbol not found: %s\n", name);
102    return NULL;
103  }
104
105  return (size_t)symbol->getSize();
106}
107
108extern "C" size_t rsloaderGetFuncCount(RSExecRef object) {
109  ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>(
110    unwrap(object)->getSectionByName(".symtab"));
111
112  if (!symtab) {
113    return 0;
114  }
115
116  return symtab->getFuncCount();
117}
118
119extern "C" void rsloaderGetFuncNameList(RSExecRef object,
120                                        size_t size,
121                                        char const **list) {
122  ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>(
123    unwrap(object)->getSectionByName(".symtab"));
124
125  if (symtab) {
126    symtab->getFuncNameList(size, list);
127  }
128}
129