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