@@ -75,26 +75,19 @@ std::optional<MemoryBufferRef> readFile(StringRef path) {
7575}
7676
7777InputFile *createObjectFile (MemoryBufferRef mb, StringRef archiveName,
78- uint64_t offsetInArchive) {
78+ uint64_t offsetInArchive, bool lazy ) {
7979 file_magic magic = identify_magic (mb.getBuffer ());
8080 if (magic == file_magic::wasm_object) {
8181 std::unique_ptr<Binary> bin =
8282 CHECK (createBinary (mb), mb.getBufferIdentifier ());
8383 auto *obj = cast<WasmObjectFile>(bin.get ());
8484 if (obj->isSharedObject ())
8585 return make<SharedFile>(mb);
86- return make<ObjFile>(mb, archiveName);
86+ return make<ObjFile>(mb, archiveName, lazy );
8787 }
8888
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);
9891}
9992
10093// Relocations contain either symbol or type indices. This function takes a
@@ -391,9 +384,30 @@ static bool shouldMerge(const WasmSegment &seg) {
391384 return true ;
392385}
393386
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+
397411 std::unique_ptr<Binary> bin = CHECK (createBinary (mb), toString (this ));
398412
399413 auto *obj = dyn_cast<WasmObjectFile>(bin.get ());
@@ -406,6 +420,11 @@ void ObjFile::parse(bool ignoreComdats) {
406420 wasmObj.reset (obj);
407421
408422 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 " );
409428
410429 // Build up a map of function indices to table indices for use when
411430 // verifying the existing table index relocations
@@ -717,43 +736,6 @@ void StubFile::parse() {
717736 }
718737}
719738
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-
757739static uint8_t mapVisibility (GlobalValue::VisibilityTypes gvVisibility) {
758740 switch (gvVisibility) {
759741 case GlobalValue::DefaultVisibility:
@@ -790,8 +772,9 @@ static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
790772}
791773
792774BitcodeFile::BitcodeFile (MemoryBufferRef m, StringRef archiveName,
793- uint64_t offsetInArchive)
775+ uint64_t offsetInArchive, bool lazy )
794776 : InputFile(BitcodeKind, m) {
777+ this ->lazy = lazy;
795778 this ->archiveName = std::string (archiveName);
796779
797780 std::string path = mb.getBufferIdentifier ().str ();
@@ -817,6 +800,20 @@ BitcodeFile::BitcodeFile(MemoryBufferRef m, StringRef archiveName,
817800
818801bool BitcodeFile::doneLTO = false ;
819802
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+
820817void BitcodeFile::parse (StringRef symName) {
821818 if (doneLTO) {
822819 error (toString (this ) + " : attempt to add bitcode file after LTO (" + symName + " )" );
0 commit comments