llvm.org GIT mirror llvm / a49701d
Revert r196639 while I investigate a bot failure. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@196641 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 6 years ago
13 changed file(s) with 10 addition(s) and 342 deletion(s). Raw diff Collapse all Expand all
4747 class Triple;
4848 class Type;
4949
50 namespace object {
51 class Archive;
52 class ObjectFile;
53 }
54
5550 /// \brief Helper class for helping synchronize access to the global address map
5651 /// table.
5752 class ExecutionEngineState {
206201 /// destroyed, it destroys the Module as well.
207202 virtual void addModule(Module *M) {
208203 Modules.push_back(M);
209 }
210
211 /// addObjectFile - Add an ObjectFile to the execution engine.
212 ///
213 /// This method is only supported by MCJIT. MCJIT will immediately load the
214 /// object into memory and adds its symbols to the list used to resolve
215 /// external symbols while preparing other objects for execution.
216 ///
217 /// Objects added using this function will not be made executable until
218 /// needed by another object.
219 ///
220 /// MCJIT will take ownership of the ObjectFile.
221 virtual void addObjectFile(object::ObjectFile *O) {
222 llvm_unreachable(
223 "ExecutionEngine subclass doesn't implement addObjectFile.");
224 }
225
226 /// addArchive - Add an Archive to the execution engine.
227 ///
228 /// This method is only supported by MCJIT. MCJIT will use the archive to
229 /// resolve external symbols in objects it is loading. If a symbol is found
230 /// in the Archive the contained object file will be extracted (in memory)
231 /// and loaded for possible execution.
232 ///
233 /// MCJIT will take ownership of the Archive.
234 virtual void addArchive(object::Archive *A) {
235 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
236204 }
237205
238206 //===--------------------------------------------------------------------===//
1919 #include "llvm/Support/Memory.h"
2020
2121 namespace llvm {
22
23 namespace object {
24 class ObjectFile;
25 }
2622
2723 class RuntimeDyldImpl;
2824 class ObjectImage;
4844 /// instance returned from this function if successful. In the case of load
4945 /// failure, the input buffer will be deleted.
5046 ObjectImage *loadObject(ObjectBuffer *InputBuffer);
51
52 /// Prepare the referenced object file for execution.
53 /// Ownership of the input object is transferred to the ObjectImage
54 /// instance returned from this function if successful. In the case of load
55 /// failure, the input object will be deleted.
56 ObjectImage *loadObject(object::ObjectFile *InputObject);
5747
5848 /// Get the address of our local copy of the symbol. This may or may not
5949 /// be the address used for relocation (clients can copy the data around
2020 #include "llvm/IR/Function.h"
2121 #include "llvm/IR/Module.h"
2222 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/Object/Archive.h"
2423 #include "llvm/Support/DynamicLibrary.h"
2524 #include "llvm/Support/ErrorHandling.h"
2625 #include "llvm/Support/MemoryBuffer.h"
7877 Modules.clear();
7978 Dyld.deregisterEHFrames();
8079
81 LoadedObjectList::iterator it, end;
82 for (it = LoadedObjects.begin(), end = LoadedObjects.end(); it != end; ++it) {
83 ObjectImage *Obj = *it;
80 LoadedObjectMap::iterator it, end = LoadedObjects.end();
81 for (it = LoadedObjects.begin(); it != end; ++it) {
82 ObjectImage *Obj = it->second;
8483 if (Obj) {
8584 NotifyFreeingObject(*Obj);
8685 delete Obj;
8786 }
8887 }
8988 LoadedObjects.clear();
90
91
92 SmallVector::iterator ArIt, ArEnd;
93 for (ArIt = Archives.begin(), ArEnd = Archives.end(); ArIt != ArEnd; ++ArIt) {
94 object::Archive *A = *ArIt;
95 delete A;
96 }
97 Archives.clear();
98
9989 delete TM;
10090 }
10191
10999 return OwnedModules.removeModule(M);
110100 }
111101
112
113
114 void MCJIT::addObjectFile(object::ObjectFile *Obj) {
115 ObjectImage *LoadedObject = Dyld.loadObject(Obj);
116 if (!LoadedObject)
117 report_fatal_error(Dyld.getErrorString());
118
119 LoadedObjects.push_back(LoadedObject);
120
121 NotifyObjectEmitted(*LoadedObject);
122 }
123
124 void MCJIT::addArchive(object::Archive *A) {
125 Archives.push_back(A);
126 }
127102
128103
129104 void MCJIT::setObjectCache(ObjectCache* NewCache) {
195170 }
196171
197172 // Load the object into the dynamic linker.
198 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
173 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
199174 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
200 LoadedObjects.push_back(LoadedObject);
175 LoadedObjects[M] = LoadedObject;
201176 if (!LoadedObject)
202177 report_fatal_error(Dyld.getErrorString());
203178
294269 uint64_t Addr = getExistingSymbolAddress(Name);
295270 if (Addr)
296271 return Addr;
297
298 SmallVector::iterator I, E;
299 for (I = Archives.begin(), E = Archives.end(); I != E; ++I) {
300 object::Archive *A = *I;
301 // Look for our symbols in each Archive
302 object::Archive::child_iterator ChildIt = A->findSym(Name);
303 if (ChildIt != A->end_children()) {
304 OwningPtr ChildBin;
305 // FIXME: Support nested archives?
306 if (!ChildIt->getAsBinary(ChildBin) && ChildBin->isObject()) {
307 object::ObjectFile *OF = reinterpret_cast(
308 ChildBin.take());
309 // This causes the object file to be loaded.
310 addObjectFile(OF);
311 // The address should be here now.
312 Addr = getExistingSymbolAddress(Name);
313 if (Addr)
314 return Addr;
315 }
316 }
317 }
318272
319273 // If it hasn't already been generated, see if it's in one of our modules.
320274 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
205205
206206 OwningModuleContainer OwnedModules;
207207
208 SmallVector Archives;
209
210 typedef SmallVector LoadedObjectList;
211 LoadedObjectList LoadedObjects;
208 typedef DenseMap LoadedObjectMap;
209 LoadedObjectMap LoadedObjects;
212210
213211 // An optional ObjectCache to be notified of compiled objects and used to
214212 // perform lookup of pre-compiled code to avoid re-compilation.
228226 /// @name ExecutionEngine interface implementation
229227 /// @{
230228 virtual void addModule(Module *M);
231 virtual void addObjectFile(object::ObjectFile *O);
232 virtual void addArchive(object::Archive *O);
233229 virtual bool removeModule(Module *M);
234230
235231 /// FindFunctionNamed - Search all of the active modules to find the one that
1818 #include "llvm/Object/ObjectFile.h"
1919
2020 namespace llvm {
21
22 namespace object {
23 class ObjectFile;
24 }
2521
2622 class ObjectImageCommon : public ObjectImage {
2723 ObjectImageCommon(); // = delete
4541 {
4642 ObjFile = object::ObjectFile::createObjectFile(Buffer->getMemBuffer());
4743 }
48 ObjectImageCommon(object::ObjectFile* Input)
49 : ObjectImage(NULL), ObjFile(Input) {}
5044 virtual ~ObjectImageCommon() { delete ObjFile; }
5145
5246 virtual object::symbol_iterator begin_symbols() const
8181 return new ObjectImageCommon(InputBuffer);
8282 }
8383
84 ObjectImage *RuntimeDyldImpl::createObjectImageFromFile(ObjectFile *InputObject) {
85 return new ObjectImageCommon(InputObject);
86 }
87
88 ObjectImage *RuntimeDyldImpl::loadObject(ObjectFile *InputObject) {
89 return loadObject(createObjectImageFromFile(InputObject));
90 }
91
9284 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
93 return loadObject(createObjectImage(InputBuffer));
94 }
95
96 ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
9785 MutexGuard locked(lock);
9886
99 OwningPtr obj(InputObject);
87 OwningPtr obj(createObjectImage(InputBuffer));
10088 if (!obj)
101 return NULL;
89 report_fatal_error("Unable to create object image from memory buffer!");
10290
10391 // Save information about our target
10492 Arch = (Triple::ArchType)obj->getArch();
150138 if (si == obj->end_sections()) continue;
151139 Check(si->getContents(SectionData));
152140 Check(si->isText(IsCode));
153 const uint8_t* SymPtr = (const uint8_t*)InputObject->getData().data() +
141 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
154142 (uintptr_t)FileOffset;
155143 uintptr_t SectOffset = (uintptr_t)(SymPtr -
156144 (const uint8_t*)SectionData.begin());
572560
573561 RuntimeDyld::~RuntimeDyld() {
574562 delete Dyld;
575 }
576
577 ObjectImage *RuntimeDyld::loadObject(ObjectFile *InputObject) {
578 if (!Dyld) {
579 if (InputObject->isELF())
580 Dyld = new RuntimeDyldELF(MM);
581 else if (InputObject->isMachO())
582 Dyld = new RuntimeDyldMachO(MM);
583 else
584 report_fatal_error("Incompatible object format!");
585 } else {
586 if (!Dyld->isCompatibleFile(InputObject))
587 report_fatal_error("Incompatible object format!");
588 }
589
590 return Dyld->loadObject(InputObject);
591563 }
592564
593565 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
2424 #include "llvm/Object/ELFObjectFile.h"
2525 #include "llvm/Object/ObjectFile.h"
2626 #include "llvm/Support/ELF.h"
27 #include "llvm/Support/MemoryBuffer.h"
28
2927 using namespace llvm;
3028 using namespace llvm::object;
3129
177175 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
178176 }
179177 RegisteredEHFrameSections.clear();
180 }
181
182 ObjectImage *RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) {
183 if (!ObjFile)
184 return NULL;
185
186 error_code ec;
187 MemoryBuffer* Buffer = MemoryBuffer::getMemBuffer(ObjFile->getData(),
188 "",
189 false);
190
191 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
192 DyldELFObject > *Obj =
193 new DyldELFObject >(Buffer, ec);
194 return new ELFObjectImage >(NULL, Obj);
195 }
196 else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
197 DyldELFObject > *Obj =
198 new DyldELFObject >(Buffer, ec);
199 return new ELFObjectImage >(NULL, Obj);
200 }
201 else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
202 DyldELFObject > *Obj =
203 new DyldELFObject >(Buffer, ec);
204 return new ELFObjectImage >(NULL, Obj);
205 }
206 else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
207 DyldELFObject > *Obj =
208 new DyldELFObject >(Buffer, ec);
209 return new ELFObjectImage >(NULL, Obj);
210 }
211 else
212 llvm_unreachable("Unexpected ELF format");
213178 }
214179
215180 ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
14371402 return false;
14381403 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
14391404 }
1440
1441 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1442 return Obj->isELF();
1443 }
1444
14451405 } // namespace llvm
139139 const SymbolTableMap &Symbols,
140140 StubMap &Stubs);
141141 virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const;
142 virtual bool isCompatibleFile(const object::ObjectFile *Buffer) const;
143142 virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
144 virtual ObjectImage *createObjectImageFromFile(object::ObjectFile *Obj);
145143 virtual void registerEHFrames();
146144 virtual void deregisterEHFrames();
147145 virtual void finalizeLoad(ObjSectionToIDMap &SectionMap);
310310 virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {}
311311
312312 virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
313 virtual ObjectImage *createObjectImageFromFile(object::ObjectFile *InputObject);
314
315 // This is the implementation for the two public overloads
316 ObjectImage *loadObject(ObjectImage *InputObject);
317
318313 public:
319314 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
320315
321316 virtual ~RuntimeDyldImpl();
322317
323318 ObjectImage *loadObject(ObjectBuffer *InputBuffer);
324 ObjectImage *loadObject(object::ObjectFile *InputObject);
325319
326320 void *getSymbolAddress(StringRef Name) {
327321 // FIXME: Just look up as a function for now. Overly simple of course.
359353 StringRef getErrorString() { return ErrorStr; }
360354
361355 virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const = 0;
362 virtual bool isCompatibleFile(const ObjectFile *Obj) const = 0;
363356
364357 virtual void registerEHFrames();
365358
454454 return false;
455455 }
456456
457 bool RuntimeDyldMachO::isCompatibleFile(
458 const object::ObjectFile *Obj) const {
459 return Obj->isMachO();
460 }
461
462457 } // end namespace llvm
9393 const SymbolTableMap &Symbols,
9494 StubMap &Stubs);
9595 virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const;
96 virtual bool isCompatibleFile(const object::ObjectFile *Obj) const;
9796 virtual void registerEHFrames();
9897 virtual void finalizeLoad(ObjSectionToIDMap &SectionMap);
9998 };
None ; REQUIRES: shell
1
2 ; This first line will generate the .o files for the next run line
3 ; RUN: %lli_mcjit -extra-module=%p/Inputs/multi-module-b.ll -extra-module=%p/Inputs/multi-module-c.ll -enable-cache-manager %s
4
5 ; This line tests MCJIT object loading
6 ; RUN: %lli_mcjit -extra-object=%p/Inputs/multi-module-b.o -extra-object=%p/Inputs/multi-module-c.o %s
7
8 ; These lines put the object files into an archive
9 ; RUN: llvm-ar r %p/Inputs/load-object.a %p/Inputs/multi-module-b.o
10 ; RUN: llvm-ar r %p/Inputs/load-object.a %p/Inputs/multi-module-c.o
11
12 ; This line test MCJIT archive loading
13 ; RUN: %lli_mcjit -extra-archive=%p/Inputs/load-object.a %s
14
15 ; These lines clean up our temporary files
16 ; RUN: rm -f %p/Inputs/load-object-a.o
17 ; RUN: rm -f %p/Inputs/multi-module-b.o
18 ; RUN: rm -f %p/Inputs/multi-module-c.o
19 ; RUN: rm -f %p/Inputs/load-object.a
20
21 declare i32 @FB()
22
23 define i32 @main() {
24 %r = call i32 @FB( ) ; [#uses=1]
25 ret i32 %r
26 }
27
2525 #include "llvm/ExecutionEngine/JITEventListener.h"
2626 #include "llvm/ExecutionEngine/JITMemoryManager.h"
2727 #include "llvm/ExecutionEngine/MCJIT.h"
28 #include "llvm/ExecutionEngine/ObjectCache.h"
2928 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
3029 #include "llvm/IR/IRBuilder.h"
3130 #include "llvm/IR/Module.h"
3231 #include "llvm/IR/Type.h"
3332 #include "llvm/IR/TypeBuilder.h"
3433 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/Object/Archive.h"
36 #include "llvm/Object/ObjectFile.h"
3734 #include "llvm/Support/CommandLine.h"
3835 #include "llvm/Support/Debug.h"
3936 #include "llvm/Support/DynamicLibrary.h"
5249 #include "llvm/Support/raw_ostream.h"
5350 #include "llvm/Transforms/Instrumentation.h"
5451 #include
55 #include
5652
5753 #ifdef __CYGWIN__
5854 #include
140136 ExtraModules("extra-module",
141137 cl::desc("Extra modules to be loaded"),
142138 cl::value_desc("input bitcode"));
143
144 cl::list
145 ExtraObjects("extra-object",
146 cl::desc("Extra object files to be loaded"),
147 cl::value_desc("input object"));
148
149 cl::list
150 ExtraArchives("extra-archive",
151 cl::desc("Extra archive files to be loaded"),
152 cl::value_desc("input archive"));
153
154 cl::opt
155 EnableCacheManager("enable-cache-manager",
156 cl::desc("Use cache manager to save/load mdoules."),
157 cl::init(false));
158139
159140 cl::opt
160141 FakeArgv0("fake-argv0",
237218 cl::init(false));
238219 }
239220
240 //===----------------------------------------------------------------------===//
241 // Object cache
242 //
243 // This object cache implementation writes cached objects to disk using a
244 // filename provided in the module descriptor and tries to load a saved object
245 // using that filename if the file exists.
246 //
247 class LLIObjectCache : public ObjectCache {
248 public:
249 LLIObjectCache() { }
250 virtual ~LLIObjectCache() {}
251
252 virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
253 const std::string ModuleID = M->getModuleIdentifier();
254 std::string CacheName;
255 if (!getCacheFilename(ModuleID, CacheName))
256 return;
257 std::ofstream outfile(CacheName.c_str(), std::ofstream::binary);
258 outfile.write(Obj->getBufferStart(), Obj->getBufferSize());
259 outfile.close();
260 }
261
262 virtual MemoryBuffer* getObject(const Module* M) {
263 const std::string ModuleID = M->getModuleIdentifier();
264 std::string CacheName;
265 if (!getCacheFilename(ModuleID, CacheName))
266 return NULL;
267 // Load the object from the cache filename
268 OwningPtr IRObjectBuffer;
269 MemoryBuffer::getFile(CacheName.c_str(), IRObjectBuffer, -1, false);
270 // If the file isn't there, that's OK.
271 if (!IRObjectBuffer)
272 return NULL;
273 // MCJIT will want to write into this buffer, and we don't want that
274 // because the file has probably just been mmapped. Instead we make
275 // a copy. The filed-based buffer will be released when it goes
276 // out of scope.
277 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer());
278 }
279
280 private:
281 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
282 std::string Prefix("file:");
283 size_t PrefixLength = Prefix.length();
284 if (ModID.substr(0, PrefixLength) != Prefix)
285 return false;
286 CacheName = ModID.substr(PrefixLength);
287 size_t pos = CacheName.rfind('.');
288 CacheName.replace(pos, CacheName.length() - pos, ".o");
289 return true;
290 }
291 };
292
293221 static ExecutionEngine *EE = 0;
294 static LLIObjectCache *CacheManager = 0;
295222
296223 static void do_shutdown() {
297224 // Cygwin-1.5 invokes DLL's dtors before atexit handler.
298225 #ifndef DO_NOTHING_ATEXIT
299226 delete EE;
300 if (CacheManager)
301 delete CacheManager;
302227 llvm_shutdown();
303228 #endif
304229 }
372297 if (!Mod) {
373298 Err.print(argv[0], errs());
374299 return 1;
375 }
376
377 if (EnableCacheManager) {
378 if (UseMCJIT) {
379 std::string CacheName("file:");
380 CacheName.append(InputFile);
381 Mod->setModuleIdentifier(CacheName);
382 } else
383 errs() << "warning: -enable-cache-manager can only be used with MCJIT.";
384300 }
385301
386302 // If not jitting lazily, load the whole bitcode file eagerly too.
474390 exit(1);
475391 }
476392
477 if (EnableCacheManager) {
478 CacheManager = new LLIObjectCache;
479 EE->setObjectCache(CacheManager);
480 }
481
482393 // Load any additional modules specified on the command line.
483394 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
484395 Module *XMod = ParseIRFile(ExtraModules[i], Err, Context);
486397 Err.print(argv[0], errs());
487398 return 1;
488399 }
489 if (EnableCacheManager) {
490 if (UseMCJIT) {
491 std::string CacheName("file:");
492 CacheName.append(ExtraModules[i]);
493 XMod->setModuleIdentifier(CacheName);
494 }
495 // else, we already printed a warning above.
496 }
497400 EE->addModule(XMod);
498 }
499
500 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
501 object::ObjectFile *Obj = object::ObjectFile::createObjectFile(
502 ExtraObjects[i]);
503 if (!Obj) {
504 Err.print(argv[0], errs());
505 return 1;
506 }
507 EE->addObjectFile(Obj);
508 }
509
510 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
511 OwningPtr ArBuf;
512 error_code ec;
513 ec = MemoryBuffer::getFileOrSTDIN(ExtraArchives[i], ArBuf);
514 if (ec) {
515 Err.print(argv[0], errs());
516 return 1;
517 }
518 object::Archive *Ar = new object::Archive(ArBuf.take(), ec);
519 if (ec || !Ar) {
520 Err.print(argv[0], errs());
521 return 1;
522 }
523 EE->addArchive(Ar);
524401 }
525402
526403 // If the target is Cygwin/MingW and we are generating remote code, we