@@ -2159,9 +2159,34 @@ ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f, bool forceHidden)
21592159void ArchiveFile::addLazySymbols () {
21602160 // Avoid calling getMemoryBufferRef() on zero-symbol archive
21612161 // since that crashes.
2162- if (file->isEmpty () || file-> getNumberOfSymbols () == 0 )
2162+ if (file->isEmpty ())
21632163 return ;
21642164
2165+ if (file->getNumberOfSymbols () == 0 ) {
2166+ // No index, treat each child as a lazy object file.
2167+ Error e = Error::success ();
2168+ for (const object::Archive::Child &c : file->children (e)) {
2169+ // Check `seen` but don't insert so a future eager load can still happen.
2170+ if (seen.contains (c.getChildOffset ()))
2171+ continue ;
2172+ if (!seenLazy.insert (c.getChildOffset ()).second ) {
2173+ continue ;
2174+ }
2175+ // First check seen.
2176+ // Then, write to and check seenLazy
2177+ // Then, get the file, check for error, and add it to inputs.
2178+ auto file = childToObjectFile (c, /* lazy=*/ true );
2179+ if (!file)
2180+ error (toString (this ) +
2181+ " : couldn't process child: " + toString (file.takeError ()));
2182+ inputFiles.insert (*file);
2183+ }
2184+ if (e)
2185+ error (toString (this ) +
2186+ " : Archive::children failed: " + toString (std::move (e)));
2187+ return ;
2188+ }
2189+
21652190 Error err = Error::success ();
21662191 auto child = file->child_begin (err);
21672192 // Ignore the I/O error here - will be reported later.
@@ -2191,16 +2216,17 @@ void ArchiveFile::addLazySymbols() {
21912216
21922217static Expected<InputFile *>
21932218loadArchiveMember (MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
2194- uint64_t offsetInArchive, bool forceHidden, bool compatArch) {
2219+ uint64_t offsetInArchive, bool forceHidden, bool compatArch,
2220+ bool lazy) {
21952221 if (config->zeroModTime )
21962222 modTime = 0 ;
21972223
21982224 switch (identify_magic (mb.getBuffer ())) {
21992225 case file_magic::macho_object:
2200- return make<ObjFile>(mb, modTime, archiveName, /* lazy= */ false , forceHidden,
2226+ return make<ObjFile>(mb, modTime, archiveName, lazy, forceHidden,
22012227 compatArch);
22022228 case file_magic::bitcode:
2203- return make<BitcodeFile>(mb, archiveName, offsetInArchive, /* lazy= */ false ,
2229+ return make<BitcodeFile>(mb, archiveName, offsetInArchive, lazy,
22042230 forceHidden, compatArch);
22052231 default :
22062232 return createStringError (inconvertibleErrorCode (),
@@ -2209,22 +2235,11 @@ loadArchiveMember(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
22092235 }
22102236}
22112237
2212- Error ArchiveFile::fetch (const object::Archive::Child &c, StringRef reason) {
2238+ Error ArchiveFile::fetch (const object::Archive::Child &c, StringRef reason,
2239+ bool lazy) {
22132240 if (!seen.insert (c.getChildOffset ()).second )
22142241 return Error::success ();
2215-
2216- Expected<MemoryBufferRef> mb = c.getMemoryBufferRef ();
2217- if (!mb)
2218- return mb.takeError ();
2219-
2220- Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified ();
2221- if (!modTime)
2222- return modTime.takeError ();
2223-
2224- Expected<InputFile *> file =
2225- loadArchiveMember (*mb, toTimeT (*modTime), getName (), c.getChildOffset (),
2226- forceHidden, compatArch);
2227-
2242+ auto file = childToObjectFile (c, /* lazy=*/ false );
22282243 if (!file)
22292244 return file.takeError ();
22302245
@@ -2251,6 +2266,23 @@ void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
22512266 toMachOString (symCopy) + " : " + toString (std::move (e)));
22522267}
22532268
2269+ Expected<InputFile *>
2270+ ArchiveFile::childToObjectFile (const llvm::object::Archive::Child &c,
2271+ bool lazy) {
2272+ Expected<MemoryBufferRef> mb = c.getMemoryBufferRef ();
2273+ if (!mb)
2274+ return mb.takeError ();
2275+
2276+ Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified ();
2277+ if (!modTime)
2278+ return modTime.takeError ();
2279+
2280+ Expected<InputFile *> file =
2281+ loadArchiveMember (*mb, toTimeT (*modTime), getName (), c.getChildOffset (),
2282+ forceHidden, compatArch, lazy);
2283+ return file;
2284+ }
2285+
22542286static macho::Symbol *createBitcodeSymbol (const lto::InputFile::Symbol &objSym,
22552287 BitcodeFile &file) {
22562288 StringRef name = saver ().save (objSym.getName ());
0 commit comments