@@ -159,12 +159,11 @@ class COFFHeaderMaterializationUnit : public MaterializationUnit {
159
159
namespace llvm {
160
160
namespace orc {
161
161
162
- Expected<std::unique_ptr<COFFPlatform>>
163
- COFFPlatform::Create (ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
164
- JITDylib &PlatformJD, const char *OrcRuntimePath,
165
- LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime,
166
- const char *VCRuntimePath,
167
- std::optional<SymbolAliasMap> RuntimeAliases) {
162
+ Expected<std::unique_ptr<COFFPlatform>> COFFPlatform::Create (
163
+ ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
164
+ JITDylib &PlatformJD, std::unique_ptr<MemoryBuffer> OrcRuntimeArchiveBuffer,
165
+ LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime,
166
+ const char *VCRuntimePath, std::optional<SymbolAliasMap> RuntimeAliases) {
168
167
169
168
// If the target is not supported then bail out immediately.
170
169
if (!supportedTarget (ES.getTargetTriple ()))
@@ -174,6 +173,22 @@ COFFPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
174
173
175
174
auto &EPC = ES.getExecutorProcessControl ();
176
175
176
+ auto GeneratorArchive =
177
+ object::Archive::create (OrcRuntimeArchiveBuffer->getMemBufferRef ());
178
+ if (!GeneratorArchive)
179
+ return GeneratorArchive.takeError ();
180
+
181
+ auto OrcRuntimeArchiveGenerator = StaticLibraryDefinitionGenerator::Create (
182
+ ObjLinkingLayer, nullptr , std::move (*GeneratorArchive));
183
+ if (!OrcRuntimeArchiveGenerator)
184
+ return OrcRuntimeArchiveGenerator.takeError ();
185
+
186
+ // We need a second instance of the archive (for now) for the Platform. We
187
+ // can `cantFail` this call, since if it were going to fail it would have
188
+ // failed above.
189
+ auto RuntimeArchive = cantFail (
190
+ object::Archive::create (OrcRuntimeArchiveBuffer->getMemBufferRef ()));
191
+
177
192
// Create default aliases if the caller didn't supply any.
178
193
if (!RuntimeAliases)
179
194
RuntimeAliases = standardPlatformAliases (ES);
@@ -199,13 +214,30 @@ COFFPlatform::Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
199
214
// Create the instance.
200
215
Error Err = Error::success ();
201
216
auto P = std::unique_ptr<COFFPlatform>(new COFFPlatform (
202
- ES, ObjLinkingLayer, PlatformJD, OrcRuntimePath,
217
+ ES, ObjLinkingLayer, PlatformJD, std::move (*OrcRuntimeArchiveGenerator),
218
+ std::move (OrcRuntimeArchiveBuffer), std::move (RuntimeArchive),
203
219
std::move (LoadDynLibrary), StaticVCRuntime, VCRuntimePath, Err));
204
220
if (Err)
205
221
return std::move (Err);
206
222
return std::move (P);
207
223
}
208
224
225
+ Expected<std::unique_ptr<COFFPlatform>>
226
+ COFFPlatform::Create (ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
227
+ JITDylib &PlatformJD, const char *OrcRuntimePath,
228
+ LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime,
229
+ const char *VCRuntimePath,
230
+ std::optional<SymbolAliasMap> RuntimeAliases) {
231
+
232
+ auto ArchiveBuffer = MemoryBuffer::getFile (OrcRuntimePath);
233
+ if (!ArchiveBuffer)
234
+ return createFileError (OrcRuntimePath, ArchiveBuffer.getError ());
235
+
236
+ return Create (ES, ObjLinkingLayer, PlatformJD, std::move (*ArchiveBuffer),
237
+ std::move (LoadDynLibrary), StaticVCRuntime, VCRuntimePath,
238
+ std::move (RuntimeAliases));
239
+ }
240
+
209
241
Expected<MemoryBufferRef> COFFPlatform::getPerJDObjectFile () {
210
242
auto PerJDObj = OrcRuntimeArchive->findSym (" __orc_rt_coff_per_jd_marker" );
211
243
if (!PerJDObj)
@@ -349,37 +381,22 @@ bool COFFPlatform::supportedTarget(const Triple &TT) {
349
381
}
350
382
}
351
383
352
- COFFPlatform::COFFPlatform (ExecutionSession &ES,
353
- ObjectLinkingLayer &ObjLinkingLayer,
354
- JITDylib &PlatformJD, const char *OrcRuntimePath,
355
- LoadDynamicLibrary LoadDynamicLibrary,
356
- bool StaticVCRuntime, const char *VCRuntimePath,
357
- Error &Err)
384
+ COFFPlatform::COFFPlatform (
385
+ ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
386
+ JITDylib &PlatformJD,
387
+ std::unique_ptr<StaticLibraryDefinitionGenerator> OrcRuntimeGenerator,
388
+ std::unique_ptr<MemoryBuffer> OrcRuntimeArchiveBuffer,
389
+ std::unique_ptr<object::Archive> OrcRuntimeArchive,
390
+ LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime,
391
+ const char *VCRuntimePath, Error &Err)
358
392
: ES(ES), ObjLinkingLayer(ObjLinkingLayer),
359
- LoadDynLibrary (std::move(LoadDynamicLibrary)),
393
+ LoadDynLibrary (std::move(LoadDynLibrary)),
394
+ OrcRuntimeArchiveBuffer(std::move(OrcRuntimeArchiveBuffer)),
395
+ OrcRuntimeArchive(std::move(OrcRuntimeArchive)),
360
396
StaticVCRuntime(StaticVCRuntime),
361
397
COFFHeaderStartSymbol(ES.intern(" __ImageBase" )) {
362
398
ErrorAsOutParameter _ (&Err);
363
399
364
- // Create a generator for the ORC runtime archive.
365
- auto OrcRuntimeArchiveGenerator =
366
- StaticLibraryDefinitionGenerator::Load (ObjLinkingLayer, OrcRuntimePath);
367
- if (!OrcRuntimeArchiveGenerator) {
368
- Err = OrcRuntimeArchiveGenerator.takeError ();
369
- return ;
370
- }
371
-
372
- auto ArchiveBuffer = MemoryBuffer::getFile (OrcRuntimePath);
373
- if (!ArchiveBuffer) {
374
- Err = createFileError (OrcRuntimePath, ArchiveBuffer.getError ());
375
- return ;
376
- }
377
- OrcRuntimeArchiveBuffer = std::move (*ArchiveBuffer);
378
- OrcRuntimeArchive =
379
- std::make_unique<object::Archive>(*OrcRuntimeArchiveBuffer, Err);
380
- if (Err)
381
- return ;
382
-
383
400
Bootstrapping.store (true );
384
401
ObjLinkingLayer.addPlugin (std::make_unique<COFFPlatformPlugin>(*this ));
385
402
@@ -392,7 +409,7 @@ COFFPlatform::COFFPlatform(ExecutionSession &ES,
392
409
}
393
410
VCRuntimeBootstrap = std::move (*VCRT);
394
411
395
- for (auto &Lib : (*OrcRuntimeArchiveGenerator) ->getImportedDynamicLibraries ())
412
+ for (auto &Lib : OrcRuntimeGenerator ->getImportedDynamicLibraries ())
396
413
DylibsToPreload.insert (Lib);
397
414
398
415
auto ImportedLibs =
@@ -406,7 +423,7 @@ COFFPlatform::COFFPlatform(ExecutionSession &ES,
406
423
for (auto &Lib : *ImportedLibs)
407
424
DylibsToPreload.insert (Lib);
408
425
409
- PlatformJD.addGenerator (std::move (*OrcRuntimeArchiveGenerator ));
426
+ PlatformJD.addGenerator (std::move (OrcRuntimeGenerator ));
410
427
411
428
// PlatformJD hasn't been set up by the platform yet (since we're creating
412
429
// the platform now), so set it up.
@@ -416,10 +433,10 @@ COFFPlatform::COFFPlatform(ExecutionSession &ES,
416
433
}
417
434
418
435
for (auto & Lib : DylibsToPreload)
419
- if (auto E2 = LoadDynLibrary (PlatformJD, Lib)) {
420
- Err = std::move (E2 );
421
- return ;
422
- }
436
+ if (auto E2 = this -> LoadDynLibrary (PlatformJD, Lib)) {
437
+ Err = std::move (E2 );
438
+ return ;
439
+ }
423
440
424
441
if (StaticVCRuntime)
425
442
if (auto E2 = VCRuntimeBootstrap->initializeStaticVCRuntime (PlatformJD)) {
0 commit comments