@@ -2156,9 +2156,34 @@ ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f, bool forceHidden)
2156
2156
void ArchiveFile::addLazySymbols () {
2157
2157
// Avoid calling getMemoryBufferRef() on zero-symbol archive
2158
2158
// since that crashes.
2159
- if (file->isEmpty () || file-> getNumberOfSymbols () == 0 )
2159
+ if (file->isEmpty ())
2160
2160
return ;
2161
2161
2162
+ if (file->getNumberOfSymbols () == 0 ) {
2163
+ // No index, treat each child as a lazy object file.
2164
+ Error e = Error::success ();
2165
+ for (const object::Archive::Child &c : file->children (e)) {
2166
+ // Check `seen` but don't insert so a future eager load can still happen.
2167
+ if (seen.contains (c.getChildOffset ()))
2168
+ continue ;
2169
+ if (!seenLazy.insert (c.getChildOffset ()).second ) {
2170
+ continue ;
2171
+ }
2172
+ // First check seen.
2173
+ // Then, write to and check seenLazy
2174
+ // Then, get the file, check for error, and add it to inputs.
2175
+ auto file = childToObjectFile (c, /* lazy=*/ true );
2176
+ if (!file)
2177
+ error (toString (this ) +
2178
+ " : couldn't process child: " + toString (file.takeError ()));
2179
+ inputFiles.insert (*file);
2180
+ }
2181
+ if (e)
2182
+ error (toString (this ) +
2183
+ " : Archive::children failed: " + toString (std::move (e)));
2184
+ return ;
2185
+ }
2186
+
2162
2187
Error err = Error::success ();
2163
2188
auto child = file->child_begin (err);
2164
2189
// Ignore the I/O error here - will be reported later.
@@ -2188,16 +2213,17 @@ void ArchiveFile::addLazySymbols() {
2188
2213
2189
2214
static Expected<InputFile *>
2190
2215
loadArchiveMember (MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
2191
- uint64_t offsetInArchive, bool forceHidden, bool compatArch) {
2216
+ uint64_t offsetInArchive, bool forceHidden, bool compatArch,
2217
+ bool lazy) {
2192
2218
if (config->zeroModTime )
2193
2219
modTime = 0 ;
2194
2220
2195
2221
switch (identify_magic (mb.getBuffer ())) {
2196
2222
case file_magic::macho_object:
2197
- return make<ObjFile>(mb, modTime, archiveName, /* lazy= */ false , forceHidden,
2223
+ return make<ObjFile>(mb, modTime, archiveName, lazy, forceHidden,
2198
2224
compatArch);
2199
2225
case file_magic::bitcode:
2200
- return make<BitcodeFile>(mb, archiveName, offsetInArchive, /* lazy= */ false ,
2226
+ return make<BitcodeFile>(mb, archiveName, offsetInArchive, lazy,
2201
2227
forceHidden, compatArch);
2202
2228
default :
2203
2229
return createStringError (inconvertibleErrorCode (),
@@ -2206,22 +2232,11 @@ loadArchiveMember(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
2206
2232
}
2207
2233
}
2208
2234
2209
- Error ArchiveFile::fetch (const object::Archive::Child &c, StringRef reason) {
2235
+ Error ArchiveFile::fetch (const object::Archive::Child &c, StringRef reason,
2236
+ bool lazy) {
2210
2237
if (!seen.insert (c.getChildOffset ()).second )
2211
2238
return Error::success ();
2212
-
2213
- Expected<MemoryBufferRef> mb = c.getMemoryBufferRef ();
2214
- if (!mb)
2215
- return mb.takeError ();
2216
-
2217
- Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified ();
2218
- if (!modTime)
2219
- return modTime.takeError ();
2220
-
2221
- Expected<InputFile *> file =
2222
- loadArchiveMember (*mb, toTimeT (*modTime), getName (), c.getChildOffset (),
2223
- forceHidden, compatArch);
2224
-
2239
+ auto file = childToObjectFile (c, /* lazy=*/ false );
2225
2240
if (!file)
2226
2241
return file.takeError ();
2227
2242
@@ -2248,6 +2263,23 @@ void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
2248
2263
toMachOString (symCopy) + " : " + toString (std::move (e)));
2249
2264
}
2250
2265
2266
+ Expected<InputFile *>
2267
+ ArchiveFile::childToObjectFile (const llvm::object::Archive::Child &c,
2268
+ bool lazy) {
2269
+ Expected<MemoryBufferRef> mb = c.getMemoryBufferRef ();
2270
+ if (!mb)
2271
+ return mb.takeError ();
2272
+
2273
+ Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified ();
2274
+ if (!modTime)
2275
+ return modTime.takeError ();
2276
+
2277
+ Expected<InputFile *> file =
2278
+ loadArchiveMember (*mb, toTimeT (*modTime), getName (), c.getChildOffset (),
2279
+ forceHidden, compatArch, lazy);
2280
+ return file;
2281
+ }
2282
+
2251
2283
static macho::Symbol *createBitcodeSymbol (const lto::InputFile::Symbol &objSym,
2252
2284
BitcodeFile &file) {
2253
2285
StringRef name = saver ().save (objSym.getName ());
0 commit comments