[lld] 622150a - [lld-macho] Put GOT into `__DATA` segment where appropriate

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 26 08:38:08 PDT 2021


Author: Jez Ng
Date: 2021-10-26T11:38:01-04:00
New Revision: 622150ad5f21149ae010188a06707d07b4d3865b

URL: https://github.com/llvm/llvm-project/commit/622150ad5f21149ae010188a06707d07b4d3865b
DIFF: https://github.com/llvm/llvm-project/commit/622150ad5f21149ae010188a06707d07b4d3865b.diff

LOG: [lld-macho] Put GOT into `__DATA` segment where appropriate

We were previously always emitting the GOT into `__DATA_CONST`, even for
target platforms where it should end up in `__DATA`.

I stumbled onto this while trying to use the `class-dump` tool -- with
the wrong segment names, it fails to locate the ObjC runtime info and
therefore fails to dump any classes.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D112500

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp
    lld/test/MachO/builtin-rename.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 8b5c6e95ed1a..77a63563cc43 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -259,7 +259,7 @@ void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
 }
 
 GotSection::GotSection()
-    : NonLazyPointerSectionBase(segment_names::dataConst, section_names::got) {
+    : NonLazyPointerSectionBase(segment_names::data, section_names::got) {
   flags = S_NON_LAZY_SYMBOL_POINTERS;
 }
 

diff  --git a/lld/test/MachO/builtin-rename.s b/lld/test/MachO/builtin-rename.s
index 9191ba7cd5c6..c1e511bc64d8 100644
--- a/lld/test/MachO/builtin-rename.s
+++ b/lld/test/MachO/builtin-rename.s
@@ -55,6 +55,23 @@
 # YDATA-DAG: __DATA_CONST,__objc_imageinfo __DATA__objc_imageinfo
 # YDATA-DAG: __DATA_CONST,__nl_symbol_ptr __IMPORT__pointers
 
+## LLD doesn't support defining symbols in synthetic sections, so we test them
+## via this slightly more awkward route.
+# RUN: llvm-readobj --section-headers %t/ydata | \
+# RUN:     FileCheck %s --check-prefix=SYNTH -DSEGNAME=__DATA_CONST
+# RUN: llvm-readobj --section-headers %t/ndata | \
+# RUN:     FileCheck %s --check-prefix=SYNTH -DSEGNAME=__DATA
+# RUN: llvm-readobj --section-headers %t/nopie | \
+# RUN:     FileCheck %s --check-prefix=SYNTH -DSEGNAME=__DATA
+# RUN: llvm-readobj --section-headers %t/old | \
+# RUN:     FileCheck %s --check-prefix=SYNTH -DSEGNAME=__DATA
+
+# SYNTH:      Name: __got
+# SYNTH-NEXT: Segment: [[SEGNAME]] ({{.*}})
+## Note that __la_symbol_ptr always remains in the non-const data segment.
+# SYNTH:      Name: __la_symbol_ptr
+# SYNTH-NEXT: Segment: __DATA ({{.*}})
+
 #--- renames.s
 .section __DATA,__auth_got
 .global __DATA__auth_got
@@ -81,13 +98,6 @@ __DATA__const:
 __DATA__cfstring:
   .space 8
 
-# FIXME: error: conflicts with synthetic section ...
-# FIXME: we can't explicitly define syms in synthetic sections
-# COM: .section __DATA,__got
-# COM: .global __DATA__got
-# COM: __DATA__got:
-# COM:   .space 8
-
 .section __DATA,__mod_init_func,mod_init_funcs
 .global __DATA__mod_init_func
 __DATA__mod_init_func:
@@ -128,13 +138,6 @@ __DATA__objc_protolist:
 __DATA__objc_imageinfo:
   .space 8
 
-# FIXME: error: conflicts with synthetic section ...
-# FIXME: we can't explicitly define syms in synthetic sections
-# COM: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
-# COM: .global __DATA__la_symbol_ptr
-# COM: __DATA__la_symbol_ptr:
-# COM:   .space 8
-
 .section __IMPORT,__pointers,non_lazy_symbol_pointers
 .global __IMPORT__pointers
 __IMPORT__pointers:
@@ -150,4 +153,6 @@ __TEXT__StaticInit:
 .text
 .global _main
 _main:
+  mov ___nan at GOTPCREL(%rip), %rax ## ensure the __got section is created
+  callq ___isnan ## ensure the __la_symbol_ptr section is created
   ret


        


More information about the llvm-commits mailing list