blob: b654797bb9c9d1f6bdc6d66e3e3d2d31bb6add5b [file] [log] [blame]
Logan3f3d31f2010-11-27 13:52:03 +08001/*
Stephen Hinescc366e52012-02-21 17:22:04 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan3f3d31f2010-11-27 13:52:03 +08003 *
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#ifndef BCC_SCRIPT_H
18#define BCC_SCRIPT_H
19
Logan Chien7890d432011-08-03 14:55:17 +080020#include <vector>
21#include <string>
22
Shih-wei Liao4ce024b2012-04-25 03:40:50 -070023#include <stdint.h>
Loganbe79ada2011-01-13 01:33:45 +080024#include <stddef.h>
25
Shih-wei Liao4ce024b2012-04-25 03:40:50 -070026#include <llvm/ADT/SmallVector.h>
27
28#include <bcc/bcc.h>
29#include <bcc/bcc_mccache.h>
30#include "bcc_internal.h"
31
32#include "Compiler.h"
33
Loganeaa0cc32010-12-29 01:04:20 +080034namespace llvm {
35 class Module;
Daniel Malea094881f2011-12-14 17:39:16 -050036 class GDBJITRegistrar;
Loganeaa0cc32010-12-29 01:04:20 +080037}
38
Logan3f3d31f2010-11-27 13:52:03 +080039namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080040 class ScriptCompiled;
41 class ScriptCached;
Shih-wei Liao4ce024b2012-04-25 03:40:50 -070042 class Source;
Zonr Chang2fcbd022012-01-06 21:04:31 +080043 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080044
45 namespace ScriptStatus {
46 enum StatusType {
47 Unknown,
48 Compiled,
Stephen Hines0e567862012-03-11 20:26:40 -070049 Cached
Logancf3e5212010-12-29 01:44:55 +080050 };
51 }
Logan3f3d31f2010-11-27 13:52:03 +080052
Zonr Chang4ea08862012-01-17 17:26:49 +080053 namespace ScriptObject {
54 enum ObjectType {
55 Unknown,
56 Relocatable,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -080057 SharedObject,
Zonr Chang4ea08862012-01-17 17:26:49 +080058 Executable,
59 };
60 }
61
Logan0647e9e2010-12-29 00:21:31 +080062 class Script {
Logan39736412010-12-29 00:24:04 +080063 private:
Loganf340bf72011-01-14 17:51:40 +080064 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080065
Logancf3e5212010-12-29 01:44:55 +080066 ScriptStatus::StatusType mStatus;
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080067 // The type of the object behind this script after compilation. For
68 // example, after returning from a successful call to prepareRelocatable(),
69 // the value of mObjectType will be ScriptObject::Relocatable.
Zonr Chang4ea08862012-01-17 17:26:49 +080070 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080071
72 union {
73 ScriptCompiled *mCompiled;
74 ScriptCached *mCached;
75 };
76
Logan Chien311c26f2011-07-11 14:30:34 +080077 std::string mCacheDir;
78 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080079
80 inline std::string getCachedObjectPath() const {
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080081 return std::string(mCacheDir + mCacheName + ".o");
Zonr Chang4ea08862012-01-17 17:26:49 +080082 }
83
84 inline std::string getCacheInfoPath() const {
Zonr Chang6692ab52012-01-17 17:29:13 +080085 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +080086 }
Loganecf4cbd2011-01-06 05:34:11 +080087
Logan42598052011-01-26 22:41:13 +080088 bool mIsContextSlotNotAvail;
89
Shih-wei Liao4ce024b2012-04-25 03:40:50 -070090 // This is the source associated with this object and is going to be
91 // compiled.
92 Source *mSource;
93
94 class DependencyInfo {
95 private:
96 MCO_ResourceType mSourceType;
97 std::string mSourceName;
98 uint8_t mSHA1[20];
99
100 public:
101 DependencyInfo(MCO_ResourceType pSourceType,
102 const std::string &pSourceName,
103 const uint8_t *pSHA1);
104
105 inline MCO_ResourceType getSourceType() const
106 { return mSourceType; }
107
108 inline const std::string getSourceName() const
109 { return mSourceName; }
110
111 inline const uint8_t *getSHA1Checksum() const
112 { return mSHA1; }
113 };
114 llvm::SmallVector<DependencyInfo *, 2> mDependencyInfos;
Logan3133c412011-01-06 06:15:40 +0800115
Logan Chien7890d432011-08-03 14:55:17 +0800116 // External Function List
117 std::vector<char const *> mUserDefinedExternalSymbols;
118
Loganecf4cbd2011-01-06 05:34:11 +0800119 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +0800120 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +0800121 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800122
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700123 // Reset the state of this script object
124 void resetState();
Logan474cbd22011-01-31 01:47:44 +0800125
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700126 public:
127 Script(Source &pSource);
Logan3f3d31f2010-11-27 13:52:03 +0800128
Logancf3e5212010-12-29 01:44:55 +0800129 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800130
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700131 // Reset this object with the new source supplied. Return false if this
132 // object remains unchanged after the call (e.g., the supplied source is
133 // the same with the one contain in this object.) If pPreserveCurrent is
134 // false, the current containing source will be destroyed after successfully
135 // reset.
136 bool reset(Source &pSource, bool pPreserveCurrent = false);
Loganeaa0cc32010-12-29 01:04:20 +0800137
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700138 // Merge (or link) another source into the current source associated with
139 // this Script object. Return false on error.
140 bool mergeSource(Source &pSource, bool pPreserveSource = false);
Loganecf4cbd2011-01-06 05:34:11 +0800141
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700142 // Add dependency information for this script given the source named
143 // pSourceName. pSHA1 is the SHA-1 checksum of the given source. Return
144 // false on error.
145 bool addSourceDependencyInfo(MCO_ResourceType pSourceType,
146 const std::string &pSourceName,
147 const uint8_t *pSHA1);
Loganeaa0cc32010-12-29 01:04:20 +0800148
Logan Chien7890d432011-08-03 14:55:17 +0800149 void markExternalSymbol(char const *name) {
150 mUserDefinedExternalSymbols.push_back(name);
151 }
152
153 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
154 return mUserDefinedExternalSymbols;
155 }
156
Logan Chien311c26f2011-07-11 14:30:34 +0800157 int prepareExecutable(char const *cacheDir,
158 char const *cacheName,
159 unsigned long flags);
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800160 int writeCache();
Loganeaa0cc32010-12-29 01:04:20 +0800161
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800162 /*
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700163 * Link the given bitcodes in mSource to shared object (.so).
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800164 *
165 * Currently, it requires one to provide the relocatable object files with
166 * given bitcodes to output a shared object.
167 *
168 * The usage of this function is flexible. You can have a relocatable object
169 * compiled before and pass it in objPath to generate shared object. If the
170 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
171 * you haven't done that yet) and then link the output relocatable object
Shih-wei Liao69341742012-03-03 01:45:36 -0800172 * file to .so in dsoPath.
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800173 *
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700174 * TODO: Currently, we only support to link a bitcode (i.e., mSource.)
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800175 *
176 */
Shih-wei Liao69341742012-03-03 01:45:36 -0800177 int prepareSharedObject(char const *objPath,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800178 char const *dsoPath,
179 unsigned long flags);
180
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800181 int prepareRelocatable(char const *objPath,
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800182 llvm::Reloc::Model RelocModel,
183 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700184
Logancf3e5212010-12-29 01:44:55 +0800185 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800186
Logancf3e5212010-12-29 01:44:55 +0800187 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800188
Loganbe79ada2011-01-13 01:33:45 +0800189 size_t getExportVarCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800190
Loganbe79ada2011-01-13 01:33:45 +0800191 size_t getExportFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800192
Stephen Hinescc366e52012-02-21 17:22:04 -0800193 size_t getExportForEachCount() const;
194
Loganbe79ada2011-01-13 01:33:45 +0800195 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800196
Loganbe79ada2011-01-13 01:33:45 +0800197 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800198
Stephen Hines071288a2011-01-27 14:38:26 -0800199 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800200
201 void getExportVarList(size_t size, void **list);
202
203 void getExportFuncList(size_t size, void **list);
204
Stephen Hinescc366e52012-02-21 17:22:04 -0800205 void getExportForEachList(size_t size, void **list);
206
Joseph Wenf36637f2011-07-06 18:27:12 -0700207 void getExportVarNameList(std::vector<std::string> &list);
208
209 void getExportFuncNameList(std::vector<std::string> &list);
210
Stephen Hinescc366e52012-02-21 17:22:04 -0800211 void getExportForEachNameList(std::vector<std::string> &list);
212
Loganbe79ada2011-01-13 01:33:45 +0800213 void getPragmaList(size_t size,
214 char const **keyList,
215 char const **valueList);
216
Loganf340bf72011-01-14 17:51:40 +0800217 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800218
Stephen Hines071288a2011-01-27 14:38:26 -0800219 void getObjectSlotList(size_t size, uint32_t *list);
220
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700221 size_t getELFSize() const;
222
223 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800224
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800225 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800226
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800227 bool isCacheable() const;
Loganeaa0cc32010-12-29 01:04:20 +0800228
Loganf340bf72011-01-14 17:51:40 +0800229 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800230 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
231 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800232 }
233 }
234
Loganf340bf72011-01-14 17:51:40 +0800235 int getError() {
236 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800237 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800238 return result;
239 }
Logan033f46e2011-01-06 05:51:24 +0800240
241 private:
Zonr Chang743dd712012-01-19 10:13:52 +0800242 //
243 // It returns 0 if there's a cache hit.
244 //
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800245 // Side effect: it will set mCacheDir, mCacheName.
Zonr Chang743dd712012-01-19 10:13:52 +0800246 int internalLoadCache(char const *cacheDir, char const *cacheName,
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800247 bool checkOnly);
Stephen Hines0e567862012-03-11 20:26:40 -0700248
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800249 int internalCompile(const CompilerOption&);
Logan3f3d31f2010-11-27 13:52:03 +0800250 };
251
252} // namespace bcc
253
254#endif // BCC_SCRIPT_H