@@ -75,26 +75,19 @@ std::optional<MemoryBufferRef> readFile(StringRef path) {
75
75
}
76
76
77
77
InputFile *createObjectFile (MemoryBufferRef mb, StringRef archiveName,
78
- uint64_t offsetInArchive) {
78
+ uint64_t offsetInArchive, bool lazy ) {
79
79
file_magic magic = identify_magic (mb.getBuffer ());
80
80
if (magic == file_magic::wasm_object) {
81
81
std::unique_ptr<Binary> bin =
82
82
CHECK (createBinary (mb), mb.getBufferIdentifier ());
83
83
auto *obj = cast<WasmObjectFile>(bin.get ());
84
84
if (obj->isSharedObject ())
85
85
return make<SharedFile>(mb);
86
- return make<ObjFile>(mb, archiveName);
86
+ return make<ObjFile>(mb, archiveName, lazy );
87
87
}
88
88
89
- if (magic == file_magic::bitcode)
90
- return make<BitcodeFile>(mb, archiveName, offsetInArchive);
91
-
92
- std::string name = mb.getBufferIdentifier ().str ();
93
- if (!archiveName.empty ()) {
94
- name = archiveName.str () + " (" + name + " )" ;
95
- }
96
-
97
- fatal (" unknown file type: " + name);
89
+ assert (magic == file_magic::bitcode);
90
+ return make<BitcodeFile>(mb, archiveName, offsetInArchive, lazy);
98
91
}
99
92
100
93
// Relocations contain either symbol or type indices. This function takes a
@@ -391,9 +384,30 @@ static bool shouldMerge(const WasmSegment &seg) {
391
384
return true ;
392
385
}
393
386
394
- void ObjFile::parse (bool ignoreComdats) {
395
- // Parse a memory buffer as a wasm file.
396
- LLVM_DEBUG (dbgs () << " Parsing object: " << toString (this ) << " \n " );
387
+ void ObjFile::parseLazy () {
388
+ LLVM_DEBUG (dbgs () << " ObjFile::parseLazy: " << toString (this ) << " \n " );
389
+ for (const SymbolRef &sym : wasmObj->symbols ()) {
390
+ const WasmSymbol &wasmSym = wasmObj->getWasmSymbol (sym.getRawDataRefImpl ());
391
+ if (!wasmSym.isDefined ())
392
+ continue ;
393
+ symtab->addLazy (wasmSym.Info .Name , this );
394
+ // addLazy() may trigger this->extract() if an existing symbol is an
395
+ // undefined symbol. If that happens, this function has served its purpose,
396
+ // and we can exit from the loop early.
397
+ if (!lazy)
398
+ break ;
399
+ }
400
+ }
401
+
402
+ ObjFile::ObjFile (MemoryBufferRef m, StringRef archiveName, bool lazy)
403
+ : InputFile(ObjectKind, m) {
404
+ this ->lazy = lazy;
405
+ this ->archiveName = std::string (archiveName);
406
+
407
+ // If this isn't part of an archive, it's eagerly linked, so mark it live.
408
+ if (archiveName.empty ())
409
+ markLive ();
410
+
397
411
std::unique_ptr<Binary> bin = CHECK (createBinary (mb), toString (this ));
398
412
399
413
auto *obj = dyn_cast<WasmObjectFile>(bin.get ());
@@ -406,6 +420,11 @@ void ObjFile::parse(bool ignoreComdats) {
406
420
wasmObj.reset (obj);
407
421
408
422
checkArch (obj->getArch ());
423
+ }
424
+
425
+ void ObjFile::parse (bool ignoreComdats) {
426
+ // Parse a memory buffer as a wasm file.
427
+ LLVM_DEBUG (dbgs () << " ObjFile::parse: " << toString (this ) << " \n " );
409
428
410
429
// Build up a map of function indices to table indices for use when
411
430
// verifying the existing table index relocations
@@ -717,43 +736,6 @@ void StubFile::parse() {
717
736
}
718
737
}
719
738
720
- void ArchiveFile::parse () {
721
- // Parse a MemoryBufferRef as an archive file.
722
- LLVM_DEBUG (dbgs () << " Parsing library: " << toString (this ) << " \n " );
723
- file = CHECK (Archive::create (mb), toString (this ));
724
-
725
- // Read the symbol table to construct Lazy symbols.
726
- int count = 0 ;
727
- for (const Archive::Symbol &sym : file->symbols ()) {
728
- symtab->addLazy (this , &sym);
729
- ++count;
730
- }
731
- LLVM_DEBUG (dbgs () << " Read " << count << " symbols\n " );
732
- (void ) count;
733
- }
734
-
735
- void ArchiveFile::addMember (const Archive::Symbol *sym) {
736
- const Archive::Child &c =
737
- CHECK (sym->getMember (),
738
- " could not get the member for symbol " + sym->getName ());
739
-
740
- // Don't try to load the same member twice (this can happen when members
741
- // mutually reference each other).
742
- if (!seen.insert (c.getChildOffset ()).second )
743
- return ;
744
-
745
- LLVM_DEBUG (dbgs () << " loading lazy: " << sym->getName () << " \n " );
746
- LLVM_DEBUG (dbgs () << " from archive: " << toString (this ) << " \n " );
747
-
748
- MemoryBufferRef mb =
749
- CHECK (c.getMemoryBufferRef (),
750
- " could not get the buffer for the member defining symbol " +
751
- sym->getName ());
752
-
753
- InputFile *obj = createObjectFile (mb, getName (), c.getChildOffset ());
754
- symtab->addFile (obj, sym->getName ());
755
- }
756
-
757
739
static uint8_t mapVisibility (GlobalValue::VisibilityTypes gvVisibility) {
758
740
switch (gvVisibility) {
759
741
case GlobalValue::DefaultVisibility:
@@ -790,8 +772,9 @@ static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
790
772
}
791
773
792
774
BitcodeFile::BitcodeFile (MemoryBufferRef m, StringRef archiveName,
793
- uint64_t offsetInArchive)
775
+ uint64_t offsetInArchive, bool lazy )
794
776
: InputFile(BitcodeKind, m) {
777
+ this ->lazy = lazy;
795
778
this ->archiveName = std::string (archiveName);
796
779
797
780
std::string path = mb.getBufferIdentifier ().str ();
@@ -817,6 +800,20 @@ BitcodeFile::BitcodeFile(MemoryBufferRef m, StringRef archiveName,
817
800
818
801
bool BitcodeFile::doneLTO = false ;
819
802
803
+ void BitcodeFile::parseLazy () {
804
+ for (auto [i, irSym] : llvm::enumerate (obj->symbols ())) {
805
+ if (irSym.isUndefined ())
806
+ continue ;
807
+ StringRef name = saver ().save (irSym.getName ());
808
+ symtab->addLazy (name, this );
809
+ // addLazy() may trigger this->extract() if an existing symbol is an
810
+ // undefined symbol. If that happens, this function has served its purpose,
811
+ // and we can exit from the loop early.
812
+ if (!lazy)
813
+ break ;
814
+ }
815
+ }
816
+
820
817
void BitcodeFile::parse (StringRef symName) {
821
818
if (doneLTO) {
822
819
error (toString (this ) + " : attempt to add bitcode file after LTO (" + symName + " )" );
0 commit comments