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 "bcc/Source.h"
18
19#include <new>
20
21#include <llvm/Bitcode/ReaderWriter.h>
22#include <llvm/LLVMContext.h>
23#include <llvm/Linker.h>
24#include <llvm/Module.h>
25#include <llvm/Support/MemoryBuffer.h>
26#include <llvm/Support/system_error.h>
27
28#include "bcc/BCCContext.h"
29#include "bcc/Support/Log.h"
30
31#include "BCCContextImpl.h"
32
33namespace {
34
35// Helper function to load the bitcode. This uses "bitcode lazy load" feature to
36// reduce the startup time. On success, return the LLVM module object created
37// and take the ownership of input memory buffer (i.e., pInput). On error,
38// return NULL and will NOT take the ownership of pInput.
39static inline llvm::Module *helper_load_bitcode(llvm::LLVMContext &pContext,
40                                                llvm::MemoryBuffer *pInput) {
41  std::string error;
42  llvm::Module *module = llvm::getLazyBitcodeModule(pInput, pContext, &error);
43
44  if (module == NULL) {
45    ALOGE("Unable to parse the given bitcode file `%s'! (%s)",
46          pInput->getBufferIdentifier(), error.c_str());
47  }
48
49  return module;
50}
51
52} // end anonymous namespace
53
54namespace bcc {
55
56Source *Source::CreateFromBuffer(BCCContext &pContext,
57                                 const char *pName,
58                                 const char *pBitcode,
59                                 size_t pBitcodeSize) {
60  llvm::StringRef input_data(pBitcode, pBitcodeSize);
61  llvm::MemoryBuffer *input_memory =
62      llvm::MemoryBuffer::getMemBuffer(input_data, "", false);
63
64  if (input_memory == NULL) {
65    ALOGE("Unable to load bitcode `%s' from buffer!", pName);
66    return NULL;
67  }
68
69  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
70                                             input_memory);
71  if (module == NULL) {
72    delete input_memory;
73    return NULL;
74  }
75
76  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
77  if (result == NULL) {
78    delete module;
79  }
80
81  return result;
82}
83
84Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) {
85  llvm::OwningPtr<llvm::MemoryBuffer> input_data;
86
87  llvm::error_code ec = llvm::MemoryBuffer::getFile(pPath, input_data);
88  if (ec != llvm::error_code::success()) {
89    ALOGE("Failed to load bitcode from path %s! (%s)", pPath.c_str(),
90                                                       ec.message().c_str());
91    return NULL;
92  }
93
94  llvm::MemoryBuffer *input_memory = input_data.take();
95  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
96                                             input_memory);
97  if (module == NULL) {
98    delete input_memory;
99    return NULL;
100  }
101
102  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
103  if (result == NULL) {
104    delete module;
105  }
106
107  return result;
108}
109
110Source *Source::CreateFromFd(BCCContext &pContext, int pFd) {
111  llvm::OwningPtr<llvm::MemoryBuffer> input_data;
112
113  llvm::error_code ec =
114      llvm::MemoryBuffer::getOpenFile(pFd, /* Filename */"", input_data);
115
116  if (ec != llvm::error_code::success()) {
117    ALOGE("Failed to load bitcode from file descriptor %d! (%s)",
118          pFd, ec.message().c_str());
119    return NULL;
120  }
121
122  llvm::MemoryBuffer *input_memory = input_data.take();
123  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
124                                             input_memory);
125  if (module == NULL) {
126    delete input_memory;
127    return NULL;
128  }
129
130  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
131  if (result == NULL) {
132    delete module;
133  }
134
135  return result;
136}
137
138Source *Source::CreateFromModule(BCCContext &pContext, llvm::Module &pModule,
139                                 bool pNoDelete) {
140  Source *result = new (std::nothrow) Source(pContext, pModule, pNoDelete);
141  if (result == NULL) {
142    ALOGE("Out of memory during Source object allocation for `%s'!",
143          pModule.getModuleIdentifier().c_str());
144  }
145  return result;
146}
147
148Source::Source(BCCContext &pContext, llvm::Module &pModule, bool pNoDelete)
149  : mContext(pContext), mModule(&pModule), mNoDelete(pNoDelete) {
150    pContext.addSource(*this);
151}
152
153Source::~Source() {
154  mContext.removeSource(*this);
155  if (!mNoDelete)
156    delete mModule;
157}
158
159bool Source::merge(Source &pSource, bool pPreserveSource) {
160  std::string error;
161  llvm::Linker::LinkerMode mode =
162      ((pPreserveSource) ? llvm::Linker::PreserveSource :
163                           llvm::Linker::DestroySource);
164
165  if (llvm::Linker::LinkModules(mModule, &pSource.getModule(),
166                                mode, &error) != 0) {
167    ALOGE("Failed to link source `%s' with `%s' (%s)!",
168          getIdentifier().c_str(),
169          pSource.getIdentifier().c_str(),
170          error.c_str());
171    return false;
172  }
173
174  if (!pPreserveSource) {
175    pSource.mNoDelete = true;
176    delete &pSource;
177  }
178
179  return true;
180}
181
182Source *Source::CreateEmpty(BCCContext &pContext, const std::string &pName) {
183  // Create an empty module
184  llvm::Module *module =
185      new (std::nothrow) llvm::Module(pName, pContext.mImpl->mLLVMContext);
186
187  if (module == NULL) {
188    ALOGE("Out of memory when creating empty LLVM module `%s'!", pName.c_str());
189    return NULL;
190  }
191
192  Source *result = CreateFromModule(pContext, *module, /* pNoDelete */false);
193  if (result == NULL) {
194    delete module;
195  }
196
197  return result;
198}
199
200const std::string &Source::getIdentifier() const {
201  return mModule->getModuleIdentifier();
202}
203
204} // namespace bcc
205