Skip to content

Commit 24706cd

Browse files
committed
[lld-macho] Avoid force-loading the same archive twice
We need to dedup archive loads (similar to what we do for dylib loads). I noticed this issue after building some Swift stuff that used `-force_load_swift_libs`, as it caused some Swift archives to be loaded many times. Reviewed By: #lld-macho, thakis, MaskRay Differential Revision: https://reviews.llvm.org/D104353
1 parent 6765b9c commit 24706cd

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

lld/MachO/Driver.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ static std::vector<ArchiveMember> getArchiveMembers(MemoryBufferRef mb) {
235235
return v;
236236
}
237237

238+
static DenseMap<StringRef, ArchiveFile *> loadedArchives;
239+
238240
static InputFile *addFile(StringRef path, bool forceLoadArchive,
239241
bool isExplicit = true,
240242
bool isBundleLoader = false) {
@@ -247,6 +249,13 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
247249
file_magic magic = identify_magic(mbref.getBuffer());
248250
switch (magic) {
249251
case file_magic::archive: {
252+
// Avoid loading archives twice. If the archives are being force-loaded,
253+
// loading them twice would create duplicate symbol errors. In the
254+
// non-force-loading case, this is just a minor performance optimization.
255+
ArchiveFile *&cachedFile = loadedArchives[path];
256+
if (cachedFile)
257+
return cachedFile;
258+
250259
std::unique_ptr<object::Archive> file = CHECK(
251260
object::Archive::create(mbref), path + ": failed to parse archive");
252261

@@ -286,7 +295,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
286295
}
287296
}
288297

289-
newFile = make<ArchiveFile>(std::move(file));
298+
newFile = cachedFile = make<ArchiveFile>(std::move(file));
290299
break;
291300
}
292301
case file_magic::macho_object:

lld/test/MachO/archive.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
# ALL-LOAD: T _main
3333
# ALL-LOAD: T _unused
3434

35+
## Multiple archives defining the same symbols aren't an issue, due to lazy
36+
## loading
37+
# RUN: cp %t/test.a %t/test2.a
38+
# RUN: %lld %t/test.a %t/test2.a %t/main.o -o /dev/null
39+
3540
#--- 2.s
3641
.globl _boo
3742
_boo:

lld/test/MachO/force-load.s

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
# REQUIRES: x86
22
# RUN: rm -rf %t; split-file %s %t
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/archive-foo.s -o %t/archive-foo.o
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/archive-baz.s -o %t/archive-baz.o
45
# RUN: llvm-ar rcs %t/foo.a %t/archive-foo.o
6+
# RUN: llvm-ar rcs %t/baz.a %t/archive-baz.o
57
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
68
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
79

8-
# RUN: %lld -force_load %t/foo.a %t/foo.o %t/test.o -o %t/test-force-load-first
10+
# RUN: %lld -lSystem -force_load %t/foo.a %t/foo.o %t/test.o -o %t/test-force-load-first
911
# FORCE-LOAD-FIRST: __TEXT,archive _foo
1012
# RUN: llvm-objdump --syms %t/test-force-load-first | FileCheck %s --check-prefix=FORCE-LOAD-FIRST
1113

12-
# RUN: %lld %t/foo.o -force_load %t/foo.a %t/test.o -o %t/test-force-load-second
14+
# RUN: %lld %t/foo.o -lSystem -force_load %t/foo.a %t/test.o -o %t/test-force-load-second
1315
# RUN: llvm-objdump --syms %t/test-force-load-second | FileCheck %s --check-prefix=FORCE-LOAD-SECOND
1416
# FORCE-LOAD-SECOND: __TEXT,obj _foo
1517

18+
## Force-loading the same path twice is fine
19+
# RUN: %lld -lSystem %t/foo.o -force_load %t/foo.a -force_load %t/foo.a %t/test.o -o /dev/null
20+
21+
## Note that we do not call realpath() before dedup'ing the force-load
22+
## arguments, so this is an error.
23+
# RUN: cd %t; not %lld -lSystem %t/foo.o -force_load %t/foo.a -force_load foo.a \
24+
# RUN: %t/test.o -o /dev/null 2>&1
25+
26+
# DUP: error: duplicate symbol: _bar
27+
28+
## Force-loading two different paths w/o conflicting symbols is fine
29+
# RUN: %lld -lSystem -force_load %t/foo.a -force_load %t/baz.a %t/test.o -o %t/test-two-force-loads
30+
# RUN: llvm-objdump --syms %t/test-two-force-loads | FileCheck %s --check-prefix=TWICE
31+
# TWICE-DAG: __TEXT,archive _foo
32+
# TWICE-DAG: __TEXT,archive _bar
33+
# TWICE-DAG: __TEXT,archive _baz
34+
1635
#--- archive-foo.s
1736
.section __TEXT,archive
18-
.globl _foo
37+
.globl _foo, _bar
1938
.weak_definition _foo
2039
_foo:
40+
_bar:
41+
42+
#--- archive-baz.s
43+
.section __TEXT,archive
44+
.globl _baz
45+
_baz:
2146

2247
#--- foo.s
2348
.section __TEXT,obj

0 commit comments

Comments
 (0)