Skip to content

Commit

Permalink
[JITLink][MachO] Re-apply b64afad, MachO linker-private support, with…
Browse files Browse the repository at this point in the history
… fixes.

Global symbols with linker-private prefixes should be resolvable across object
boundaries, but internal symbols with linker-private prefixes should not.
  • Loading branch information
lhames committed Mar 15, 2020
1 parent 4dfe92e commit 9c9eb60
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
10 changes: 6 additions & 4 deletions llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
Expand Up @@ -64,12 +64,14 @@ Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) {
}

Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) {
if (Name.startswith("l"))
return Scope::Local;
if (Type & MachO::N_PEXT)
return Scope::Hidden;
if (Type & MachO::N_EXT)
return Scope::Default;
if (Type & MachO::N_EXT) {
if (Name.startswith("l"))
return Scope::Hidden;
else
return Scope::Default;
}
return Scope::Local;
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp
Expand Up @@ -560,7 +560,8 @@ class MachOJITLinker_arm64 : public JITLinker<MachOJITLinker_arm64> {
*(ulittle32_t *)FixupPtr = Value;
break;
}
case Pointer64: {
case Pointer64:
case Pointer64Anon: {
uint64_t Value = E.getTarget().getAddress() + E.getAddend();
*(ulittle64_t *)FixupPtr = Value;
break;
Expand Down
14 changes: 11 additions & 3 deletions llvm/lib/ExecutionEngine/Orc/Mangling.cpp
Expand Up @@ -89,6 +89,8 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
if (!Obj)
return Obj.takeError();

bool IsMachO = isa<object::MachOObjectFile>(Obj->get());

SymbolFlagsMap SymbolFlags;
for (auto &Sym : (*Obj)->symbols()) {
// Skip symbols not defined in this object file.
Expand All @@ -113,14 +115,20 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
if (!SymFlags)
return SymFlags.takeError();

// Strip the 'exported' flag from MachO linker-private symbols.
if (IsMachO && Name->startswith("l"))
*SymFlags &= ~JITSymbolFlags::Exported;

SymbolFlags[InternedName] = std::move(*SymFlags);
}

SymbolStringPtr InitSymbol;

if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get())) {
for (auto &Sec : MachOObj->sections()) {
auto SecType = MachOObj->getSectionType(Sec);
if (IsMachO) {
auto &MachOObj = cast<object::MachOObjectFile>(*Obj->get());
for (auto &Sec : MachOObj.sections()) {
auto SecType = MachOObj.getSectionType(Sec);
if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
std::string InitSymString;
raw_string_ostream(InitSymString)
Expand Down
@@ -0,0 +1,12 @@
# Supplies a global definition, l_foo, with a linker-private prefix. Since this
# definition is marked as global it should be resolvable outside the object.

.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 14
.globl l_foo
.p2align 4, 0x90
l_foo:
xorl %eax, %eax
retq

.subsections_via_symbols
@@ -0,0 +1,12 @@
# Supplies an internal definition, l_foo, with a linker-private prefix. Since
# this definition is not marked as global it should not be resolvable outside
# the object.

.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 14
.p2align 4, 0x90
l_foo:
xorl %eax, %eax
retq

.subsections_via_symbols
@@ -0,0 +1,22 @@
# RUN: rm -rf %t && mkdir -p %t
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/global_lp_def.o %S/Inputs/MachO_global_linker_private_def.s
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/internal_lp_def.o %S/Inputs/MachO_internal_linker_private_def.s
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
# RUN: -o %t/macho_lp_test.o %s
# RUN: llvm-jitlink -noexec %t/global_lp_def.o %t/macho_lp_test.o
# RUN: not llvm-jitlink -noexec %t/internal_lp_def.o %t/macho_lp_test.o
#
# Check that we can resolve global symbols whose names start with the
# linker-private prefix 'l' across object boundaries, and that we can't resolve
# internals with the linker-private prefix across object boundaries.

.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 14
.globl _main
.p2align 4, 0x90
_main:
jmp l_foo

.subsections_via_symbols

0 comments on commit 9c9eb60

Please sign in to comment.