Skip to content

Commit 342bd4b

Browse files
authored
[orc] Add the name of static archives to the name of their member objects (#99407)
Changes "MyObj.o" to "/path/to/libMyLib.a(MyObj.o)". This allows us to differentiate between objects that have the same basename but came from different archives. It also fixes a bug where if two such objects were both linked and both have initializer sections their initializer symbol would cause a duplicate symbol error. rdar://131782514
1 parent 0c4023a commit 342bd4b

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class StaticLibraryDefinitionGenerator : public DefinitionGenerator {
328328
std::unique_ptr<MemoryBuffer> ArchiveBuffer;
329329
std::unique_ptr<object::Archive> Archive;
330330
DenseMap<SymbolStringPtr, MemoryBufferRef> ObjectFilesMap;
331+
BumpPtrAllocator ObjFileNameStorage;
331332
};
332333

333334
/// A utility class to create COFF dllimport GOT symbols (__imp_*) and PLT

llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/MC/TargetRegistry.h"
1818
#include "llvm/Object/MachOUniversal.h"
1919
#include "llvm/Support/FormatVariadic.h"
20+
#include "llvm/Support/StringSaver.h"
2021
#include "llvm/Target/TargetMachine.h"
2122
#include <string>
2223

@@ -422,6 +423,7 @@ Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
422423
DenseMap<uint64_t, MemoryBufferRef> MemoryBuffers;
423424
DenseSet<uint64_t> Visited;
424425
DenseSet<uint64_t> Excluded;
426+
StringSaver FileNames(ObjFileNameStorage);
425427
for (auto &S : Archive->symbols()) {
426428
StringRef SymName = S.getName();
427429
auto Member = S.getMember();
@@ -438,7 +440,17 @@ Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
438440
Excluded.insert(DataOffset);
439441
continue;
440442
}
441-
MemoryBuffers[DataOffset] = (*Child)->getMemoryBufferRef();
443+
444+
// Give members of the archive a name that contains the archive path so
445+
// that they can be differentiated from a member with the same name in a
446+
// different archive. This also ensure initializer symbols names will be
447+
// unique within a JITDylib.
448+
StringRef FullName = FileNames.save(Archive->getFileName() + "(" +
449+
(*Child)->getFileName() + ")");
450+
MemoryBufferRef MemBuffer((*Child)->getMemoryBufferRef().getBuffer(),
451+
FullName);
452+
453+
MemoryBuffers[DataOffset] = MemBuffer;
442454
}
443455
if (!Excluded.count(DataOffset))
444456
ObjectFilesMap[L.getExecutionSession().intern(SymName)] =
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Check that the generated __inits symbol name does not clash between objects
2+
# with the same base name in two different static archives. Otherwise we get a
3+
# duplicate symbol error.
4+
5+
# RUN: rm -rf %t && mkdir -p %t
6+
# RUN: split-file %s %t
7+
8+
# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
9+
# RUN: -o %t/dir1/myobj.o %t/dir1/myobj.s
10+
# RUN: llvm-ar crs %t/libmyobj1.a %t/dir1/myobj.o
11+
12+
# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
13+
# RUN: -o %t/dir2/myobj.o %t/dir2/myobj.s
14+
# RUN: llvm-ar crs %t/libmyobj2.a %t/dir2/myobj.o
15+
16+
# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
17+
# RUN: -o %t/main.o %t/main.s
18+
19+
# RUN: llvm-jitlink -noexec %t/main.o -lmyobj1 -lmyobj2 -L%t
20+
21+
#--- dir1/myobj.s
22+
.section __TEXT,__text,regular,pure_instructions
23+
.build_version macos, 15, 0 sdk_version 15, 0
24+
.globl _myobj1
25+
.p2align 4, 0x90
26+
_myobj1: ## @f
27+
retq
28+
29+
.section __DATA,__mod_init_func,mod_init_funcs
30+
.p2align 3, 0x0
31+
.quad _myobj1
32+
33+
.subsections_via_symbols
34+
35+
#--- dir2/myobj.s
36+
.section __TEXT,__text,regular,pure_instructions
37+
.build_version macos, 15, 0 sdk_version 15, 0
38+
.globl _myobj2
39+
.p2align 4, 0x90
40+
_myobj2: ## @f
41+
retq
42+
43+
.section __DATA,__mod_init_func,mod_init_funcs
44+
.p2align 3, 0x0
45+
.quad _myobj2
46+
47+
.subsections_via_symbols
48+
49+
#--- main.s
50+
51+
.section __TEXT,__text,regular,pure_instructions
52+
53+
.globl _main
54+
.p2align 4, 0x90
55+
_main:
56+
pushq %rbp
57+
movq %rsp, %rbp
58+
callq _myobj1
59+
callq _myobj2
60+
xorl %eax, %eax
61+
popq %rbp
62+
retq
63+
64+
.subsections_via_symbols

0 commit comments

Comments
 (0)