Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 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 | #ifndef BCC_SCRIPT_H |
| 18 | #define BCC_SCRIPT_H |
| 19 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 20 | #include <vector> |
| 21 | #include <string> |
| 22 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 23 | #include <stdint.h> |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 24 | #include <stddef.h> |
| 25 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 26 | #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 | |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 34 | namespace llvm { |
| 35 | class Module; |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 36 | class GDBJITRegistrar; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 37 | } |
| 38 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 39 | namespace bcc { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 40 | class ScriptCompiled; |
| 41 | class ScriptCached; |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 42 | class Source; |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 43 | struct CompilerOption; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 44 | |
| 45 | namespace ScriptStatus { |
| 46 | enum StatusType { |
| 47 | Unknown, |
| 48 | Compiled, |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 49 | Cached |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 50 | }; |
| 51 | } |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 52 | |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 53 | namespace ScriptObject { |
| 54 | enum ObjectType { |
| 55 | Unknown, |
| 56 | Relocatable, |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 57 | SharedObject, |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 58 | Executable, |
| 59 | }; |
| 60 | } |
| 61 | |
Logan | 0647e9e | 2010-12-29 00:21:31 +0800 | [diff] [blame] | 62 | class Script { |
Logan | 3973641 | 2010-12-29 00:24:04 +0800 | [diff] [blame] | 63 | private: |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 64 | int mErrorCode; |
Logan | 3973641 | 2010-12-29 00:24:04 +0800 | [diff] [blame] | 65 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 66 | ScriptStatus::StatusType mStatus; |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 67 | // 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 Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 70 | ScriptObject::ObjectType mObjectType; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 71 | |
| 72 | union { |
| 73 | ScriptCompiled *mCompiled; |
| 74 | ScriptCached *mCached; |
| 75 | }; |
| 76 | |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 77 | std::string mCacheDir; |
| 78 | std::string mCacheName; |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 79 | |
| 80 | inline std::string getCachedObjectPath() const { |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 81 | return std::string(mCacheDir + mCacheName + ".o"); |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | inline std::string getCacheInfoPath() const { |
Zonr Chang | 6692ab5 | 2012-01-17 17:29:13 +0800 | [diff] [blame] | 85 | return getCachedObjectPath().append(".info"); |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 86 | } |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 87 | |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 88 | bool mIsContextSlotNotAvail; |
| 89 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 90 | // 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; |
Logan | 3133c41 | 2011-01-06 06:15:40 +0800 | [diff] [blame] | 115 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 116 | // External Function List |
| 117 | std::vector<char const *> mUserDefinedExternalSymbols; |
| 118 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 119 | // Register Symbol Lookup Function |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 120 | BCCSymbolLookupFn mpExtSymbolLookupFn; |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 121 | void *mpExtSymbolLookupFnContext; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 122 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 123 | // Reset the state of this script object |
| 124 | void resetState(); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 125 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 126 | public: |
| 127 | Script(Source &pSource); |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 128 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 129 | ~Script(); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 130 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 131 | // 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); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 137 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 138 | // 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); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 141 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 142 | // 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); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 148 | |
Logan Chien | 7890d43 | 2011-08-03 14:55:17 +0800 | [diff] [blame] | 149 | 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 Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 157 | int prepareExecutable(char const *cacheDir, |
| 158 | char const *cacheName, |
| 159 | unsigned long flags); |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 160 | int writeCache(); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 161 | |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 162 | /* |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 163 | * Link the given bitcodes in mSource to shared object (.so). |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 164 | * |
| 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 Liao | 6934174 | 2012-03-03 01:45:36 -0800 | [diff] [blame] | 172 | * file to .so in dsoPath. |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 173 | * |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame^] | 174 | * TODO: Currently, we only support to link a bitcode (i.e., mSource.) |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 175 | * |
| 176 | */ |
Shih-wei Liao | 6934174 | 2012-03-03 01:45:36 -0800 | [diff] [blame] | 177 | int prepareSharedObject(char const *objPath, |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 178 | char const *dsoPath, |
| 179 | unsigned long flags); |
| 180 | |
Shih-wei Liao | d8ed6a9 | 2012-03-03 01:44:29 -0800 | [diff] [blame] | 181 | int prepareRelocatable(char const *objPath, |
Shih-wei Liao | 6a60f4e | 2012-01-17 02:58:40 -0800 | [diff] [blame] | 182 | llvm::Reloc::Model RelocModel, |
| 183 | unsigned long flags); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 184 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 185 | char const *getCompilerErrorMessage(); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 186 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 187 | void *lookup(const char *name); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 188 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 189 | size_t getExportVarCount() const; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 190 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 191 | size_t getExportFuncCount() const; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 192 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 193 | size_t getExportForEachCount() const; |
| 194 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 195 | size_t getPragmaCount() const; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 196 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 197 | size_t getFuncCount() const; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 198 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 199 | size_t getObjectSlotCount() const; |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 200 | |
| 201 | void getExportVarList(size_t size, void **list); |
| 202 | |
| 203 | void getExportFuncList(size_t size, void **list); |
| 204 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 205 | void getExportForEachList(size_t size, void **list); |
| 206 | |
Joseph Wen | f36637f | 2011-07-06 18:27:12 -0700 | [diff] [blame] | 207 | void getExportVarNameList(std::vector<std::string> &list); |
| 208 | |
| 209 | void getExportFuncNameList(std::vector<std::string> &list); |
| 210 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 211 | void getExportForEachNameList(std::vector<std::string> &list); |
| 212 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 213 | void getPragmaList(size_t size, |
| 214 | char const **keyList, |
| 215 | char const **valueList); |
| 216 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 217 | void getFuncInfoList(size_t size, FuncInfo *list); |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 218 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 219 | void getObjectSlotList(size_t size, uint32_t *list); |
| 220 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 221 | size_t getELFSize() const; |
| 222 | |
| 223 | const char *getELF() const; |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 224 | |
Shih-wei Liao | ce82d49 | 2011-01-20 12:34:03 -0800 | [diff] [blame] | 225 | int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext); |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 226 | |
Shih-wei Liao | abc7f51 | 2012-01-18 00:34:07 -0800 | [diff] [blame] | 227 | bool isCacheable() const; |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 228 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 229 | void setError(int error) { |
Logan | 3973641 | 2010-12-29 00:24:04 +0800 | [diff] [blame] | 230 | if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) { |
| 231 | mErrorCode = error; |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 235 | int getError() { |
| 236 | int result = mErrorCode; |
Logan | 3973641 | 2010-12-29 00:24:04 +0800 | [diff] [blame] | 237 | mErrorCode = BCC_NO_ERROR; |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 238 | return result; |
| 239 | } |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 240 | |
| 241 | private: |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 242 | // |
| 243 | // It returns 0 if there's a cache hit. |
| 244 | // |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 245 | // Side effect: it will set mCacheDir, mCacheName. |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 246 | int internalLoadCache(char const *cacheDir, char const *cacheName, |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 247 | bool checkOnly); |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 248 | |
Shih-wei Liao | 9e81e37 | 2012-01-17 16:38:40 -0800 | [diff] [blame] | 249 | int internalCompile(const CompilerOption&); |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // namespace bcc |
| 253 | |
| 254 | #endif // BCC_SCRIPT_H |