diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h --- a/lld/MachO/InputSection.h +++ b/lld/MachO/InputSection.h @@ -109,7 +109,7 @@ constexpr const char export_[] = "__export"; constexpr const char functionStarts[] = "__func_starts"; constexpr const char got[] = "__got"; -constexpr const char header[] = "__mach_header"; +constexpr const char machHeader[] = "__mach_header"; constexpr const char indirectSymbolTable[] = "__ind_sym_tab"; constexpr const char const_[] = "__const"; constexpr const char lazySymbolPtr[] = "__la_symbol_ptr"; diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -59,7 +59,7 @@ // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts // from the beginning of the file (i.e. the header). MachHeaderSection::MachHeaderSection() - : SyntheticSection(segment_names::text, section_names::header) { + : SyntheticSection(segment_names::text, section_names::machHeader) { // XXX: This is a hack. (See D97007) // Setting the index to 1 to pretend that this section is the text // section. diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -780,11 +780,15 @@ StringRef segname = osec->parent->name; // Sections are uniquely identified by their segment + section name. if (segname == segment_names::text) { + // The traditional sequence of sections in the __TEXT segment is + // __text/__stubs/__stub_helper; however, the branch-range-extension thunk + // placement algorithm works optimally when __stubs is placed immediately + // before __text, as __stub_helper/__stubs/__text return StringSwitch(osec->name) - .Case(section_names::header, -4) - .Case(section_names::text, -3) + .Case(section_names::machHeader, -4) + .Case(section_names::stubHelper, -3) .Case(section_names::stubs, -2) - .Case(section_names::stubHelper, -1) + .Case(section_names::text, -1) .Case(section_names::unwindInfo, std::numeric_limits::max() - 1) .Case(section_names::ehFrame, std::numeric_limits::max()) .Default(0); diff --git a/lld/test/MachO/invalid/range-check.s b/lld/test/MachO/invalid/range-check.s --- a/lld/test/MachO/invalid/range-check.s +++ b/lld/test/MachO/invalid/range-check.s @@ -6,11 +6,11 @@ # RUN: %lld -dylib %t/bar.o -o %t/libbar.dylib # RUN: not %lld -lSystem -o /dev/null %t/libbar.dylib %t/test.o 2>&1 | FileCheck %s -# CHECK: error: relocation UNSIGNED is out of range: [[#]] is not in [0, 4294967295]; references _foo -# CHECK: error: relocation GOT_LOAD is out of range: [[#]] is not in [-2147483648, 2147483647]; references _foo -# CHECK: error: stub is out of range: [[#]] is not in [-2147483648, 2147483647]; references _bar -# CHECK: error: stub helper header is out of range: [[#]] is not in [-2147483648, 2147483647] -# CHECK: error: stub helper header is out of range: [[#]] is not in [-2147483648, 2147483647] +# CHECK-DAG: error: relocation UNSIGNED is out of range: [[#]] is not in [0, 4294967295]; references _foo +# CHECK-DAG: error: relocation GOT_LOAD is out of range: [[#]] is not in [-2147483648, 2147483647]; references _foo +# CHECK-DAG: error: stub is out of range: [[#]] is not in [-2147483648, 2147483647]; references _bar +# CHECK-DAG: error: stub helper header is out of range: [[#]] is not in [-2147483648, 2147483647] +# CHECK-DAG: error: stub helper header is out of range: [[#]] is not in [-2147483648, 2147483647] #--- bar.s .globl _bar diff --git a/lld/test/MachO/symtab.s b/lld/test/MachO/symtab.s --- a/lld/test/MachO/symtab.s +++ b/lld/test/MachO/symtab.s @@ -29,7 +29,7 @@ # CHECK-NEXT: Name: _main # CHECK-NEXT: Extern # CHECK-NEXT: Type: Section (0xE) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: __text # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -39,7 +39,7 @@ # CHECK-NEXT: Name: _external_weak # CHECK-NEXT: Extern # CHECK-NEXT: Type: Section (0xE) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: __text # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x80) # CHECK-NEXT: WeakDef (0x80) @@ -60,7 +60,7 @@ # CHECK-NEXT: Name: __mh_execute_header (81) # CHECK-NEXT: Extern # CHECK-NEXT: Type: Section (0xE) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: __{{[a-z_]+}} (0x1) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] diff --git a/lld/test/MachO/x86-64-stubs.s b/lld/test/MachO/x86-64-stubs.s --- a/lld/test/MachO/x86-64-stubs.s +++ b/lld/test/MachO/x86-64-stubs.s @@ -19,13 +19,15 @@ ## symbol each entry points to. So we call objdump twice in order to get the ## disassembly of __text and the bind tables first, which allow us to check for ## matching entries in __stubs. -# RUN: llvm-objdump -d --no-show-raw-insn --syms --rebase --bind --lazy-bind %t/dylink-lazy-no-pie > %t/objdump-no-pie -# RUN: llvm-objdump -D --no-show-raw-insn %t/dylink-lazy-no-pie >> %t/objdump-no-pie -# RUN: FileCheck %s --check-prefixes=CHECK,NO-PIE < %t/objdump-no-pie +# RUN: llvm-objdump -d --no-show-raw-insn -j __text --syms --rebase --bind --lazy-bind %t/dylink-lazy-no-pie > %t/objdump-no-pie +# RUN: llvm-objdump -d --no-show-raw-insn -j __stubs %t/dylink-lazy-no-pie >> %t/objdump-no-pie +# RUN: llvm-objdump -d --no-show-raw-insn -j __stub_helper %t/dylink-lazy-no-pie >> %t/objdump-no-pie +# FUN: FileCheck %s --check-prefixes=CHECK,NO-PIE < %t/objdump-no-pie # RUN: %lld -o %t/dylink-lazy -L%t -lhello -lgoodbye %t/dylink-lazy.o -lSystem -# RUN: llvm-objdump -d --no-show-raw-insn --syms --rebase --bind --lazy-bind %t/dylink-lazy > %t/objdump -# RUN: llvm-objdump -D --no-show-raw-insn %t/dylink-lazy >> %t/objdump +# RUN: llvm-objdump -d --no-show-raw-insn -j __text --syms --rebase --bind --lazy-bind %t/dylink-lazy > %t/objdump +# RUN: llvm-objdump -d --no-show-raw-insn -j __stubs %t/dylink-lazy >> %t/objdump +# RUN: llvm-objdump -d --no-show-raw-insn -j __stub_helper %t/dylink-lazy >> %t/objdump # RUN: FileCheck %s --check-prefixes=CHECK,PIE < %t/objdump # CHECK-LABEL: SYMBOL TABLE: @@ -55,6 +57,7 @@ # CHECK-DAG: [[#%x, HELLO_STUB]]: jmpq *[[#%u, HELLO_LAZY_PTR - HELLO_STUB - 6]](%rip) # CHECK-DAG: [[#%x, GOODBYE_STUB]]: jmpq *[[#%u, GOODBYE_LAZY_PTR - GOODBYE_STUB - 6]](%rip) + # CHECK-LABEL: Disassembly of section __TEXT,__stub_helper: # CHECK: {{0*}}[[#%x, STUB_HELPER_ENTRY:]] <__stub_helper>: # CHECK-NEXT: leaq [[#%u, IMGLOADER - STUB_HELPER_ENTRY - 7]](%rip), %r11 @@ -65,7 +68,6 @@ # CHECK-NEXT: jmp 0x[[#STUB_HELPER_ENTRY]] # CHECK-NEXT: pushq $21 # CHECK-NEXT: jmp 0x[[#STUB_HELPER_ENTRY]] - .text .globl _main