Skip to content

[lld-macho] Remove symbols to __mod_init_func with -init_offsets #97156

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 6, 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
11 changes: 11 additions & 0 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,12 @@ static void handleExplicitExports() {
}
}

static void eraseInitializerSymbols() {
for (ConcatInputSection *isec : in.initOffsets->inputs())
for (Defined *sym : isec->symbols)
sym->used = false;
}

namespace lld {
namespace macho {
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
Expand Down Expand Up @@ -1971,6 +1977,11 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
if (config->deadStrip)
markLive();

// Ensure that no symbols point inside __mod_init_func sections if they are
// removed due to -init_offsets. This must run after dead stripping.
if (config->emitInitOffsets)
eraseInitializerSymbols();

// Categories are not subject to dead-strip. The __objc_catlist section is
// marked as NO_DEAD_STRIP and that propagates into all category data.
if (args.hasArg(OPT_check_category_conflicts))
Expand Down
12 changes: 11 additions & 1 deletion lld/MachO/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,17 @@ void Writer::treatSpecialUndefineds() {

static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec,
const lld::macho::Reloc &r) {
assert(sym->isLive());
if (!sym->isLive()) {
if (Defined *defined = dyn_cast<Defined>(sym)) {
if (config->emitInitOffsets &&
defined->isec()->getName() == section_names::moduleInitFunc)
fatal(isec->getLocation(r.offset) + ": cannot reference " +
sym->getName() +
" defined in __mod_init_func when -init_offsets is used");
}
assert(false && "referenced symbol must be live");
}

const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);

if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
Expand Down
6 changes: 5 additions & 1 deletion lld/test/MachO/init-offsets.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# RUN: llvm-objcopy --dump-section=__TEXT,__init_offsets=%t/section.bin %t/out
# RUN: echo "__TEXT,__init_offsets contents:" >> %t/dump.txt
# RUN: od -An -txI %t/section.bin >> %t/dump.txt
# RUN: FileCheck --check-prefix=CONTENT %s < %t/dump.txt
# RUN: FileCheck --check-prefix=CONTENT --implicit-check-not=_init_ptr %s < %t/dump.txt

## This test checks that:
## - __mod_init_func is replaced by __init_offsets.
Expand All @@ -21,6 +21,7 @@
## command line, and in the order they show up within __mod_init_func.
## - for undefined and dylib symbols, stubs are created, and the offsets point to those.
## - offsets are relative to __TEXT's address, they aren't an absolute virtual address.
## - symbols defined within __mod_init_func are ignored.

# FLAGS: sectname __init_offsets
# FLAGS-NEXT: segname __TEXT
Expand Down Expand Up @@ -48,13 +49,15 @@

#--- first.s
.globl _first_init, ___isnan, _main
.globl _init_ptr_1
.text
_first_init:
ret
_main:
ret

.section __DATA,__mod_init_func,mod_init_funcs
_init_ptr_1:
.quad _first_init
.quad ___isnan

Expand All @@ -68,6 +71,7 @@ _second_init:

.section __DATA,__mod_init_func,mod_init_funcs
.quad _undefined
_init_ptr_2:
.quad _second_init

.subsections_via_symbols
16 changes: 16 additions & 0 deletions lld/test/MachO/invalid/init-offsets.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# REQUIRES: x86

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: not %lld -lSystem -init_offsets %t.o -o /dev/null 2>&1 | FileCheck %s

# CHECK: error: {{.*}}init-offsets.s.tmp.o:(symbol _main+0x3): cannot reference _init_slot defined in __mod_init_func when -init_offsets is used

.globl _main
.text
_main:
leaq _init_slot(%rip), %rax

.section __DATA,__mod_init_func,mod_init_funcs
_init_slot:
.quad _main

Loading