Skip to content

[orc] Add the name of static archives to the name of their member objects #99407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class StaticLibraryDefinitionGenerator : public DefinitionGenerator {
std::unique_ptr<MemoryBuffer> ArchiveBuffer;
std::unique_ptr<object::Archive> Archive;
DenseMap<SymbolStringPtr, MemoryBufferRef> ObjectFilesMap;
BumpPtrAllocator ObjFileNameStorage;
};

/// A utility class to create COFF dllimport GOT symbols (__imp_*) and PLT
Expand Down
14 changes: 13 additions & 1 deletion llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Target/TargetMachine.h"
#include <string>

Expand Down Expand Up @@ -422,6 +423,7 @@ Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
DenseMap<uint64_t, MemoryBufferRef> MemoryBuffers;
DenseSet<uint64_t> Visited;
DenseSet<uint64_t> Excluded;
StringSaver FileNames(ObjFileNameStorage);
for (auto &S : Archive->symbols()) {
StringRef SymName = S.getName();
auto Member = S.getMember();
Expand All @@ -438,7 +440,17 @@ Error StaticLibraryDefinitionGenerator::buildObjectFilesMap() {
Excluded.insert(DataOffset);
continue;
}
MemoryBuffers[DataOffset] = (*Child)->getMemoryBufferRef();

// Give members of the archive a name that contains the archive path so
// that they can be differentiated from a member with the same name in a
// different archive. This also ensure initializer symbols names will be
// unique within a JITDylib.
StringRef FullName = FileNames.save(Archive->getFileName() + "(" +
(*Child)->getFileName() + ")");
MemoryBufferRef MemBuffer((*Child)->getMemoryBufferRef().getBuffer(),
FullName);

MemoryBuffers[DataOffset] = MemBuffer;
}
if (!Excluded.count(DataOffset))
ObjectFilesMap[L.getExecutionSession().intern(SymName)] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Check that the generated __inits symbol name does not clash between objects
# with the same base name in two different static archives. Otherwise we get a
# duplicate symbol error.

# RUN: rm -rf %t && mkdir -p %t
# RUN: split-file %s %t

# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/dir1/myobj.o %t/dir1/myobj.s
# RUN: llvm-ar crs %t/libmyobj1.a %t/dir1/myobj.o

# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/dir2/myobj.o %t/dir2/myobj.s
# RUN: llvm-ar crs %t/libmyobj2.a %t/dir2/myobj.o

# RUN: llvm-mc -triple x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/main.o %t/main.s

# RUN: llvm-jitlink -noexec %t/main.o -lmyobj1 -lmyobj2 -L%t

#--- dir1/myobj.s
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 15, 0 sdk_version 15, 0
.globl _myobj1
.p2align 4, 0x90
_myobj1: ## @f
retq

.section __DATA,__mod_init_func,mod_init_funcs
.p2align 3, 0x0
.quad _myobj1

.subsections_via_symbols

#--- dir2/myobj.s
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 15, 0 sdk_version 15, 0
.globl _myobj2
.p2align 4, 0x90
_myobj2: ## @f
retq

.section __DATA,__mod_init_func,mod_init_funcs
.p2align 3, 0x0
.quad _myobj2

.subsections_via_symbols

#--- main.s

.section __TEXT,__text,regular,pure_instructions

.globl _main
.p2align 4, 0x90
_main:
pushq %rbp
movq %rsp, %rbp
callq _myobj1
callq _myobj2
xorl %eax, %eax
popq %rbp
retq

.subsections_via_symbols
Loading