[llvm-branch-commits] [lld] d0c4be4 - [lld-macho] Emit empty string as first entry of string table

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 1 15:10:14 PST 2020


Author: Jez Ng
Date: 2020-12-01T15:05:20-08:00
New Revision: d0c4be42e35d8cff069f91a45b76ea24187c233d

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

LOG: [lld-macho] Emit empty string as first entry of string table

ld64 emits string tables which start with a space and a zero byte. We
match its behavior here since some tools depend on it.

Similar rationale as {D89561}.

Reviewed By: #lld-macho, smeenai

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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.h
    lld/test/MachO/symtab.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index e52bc480e9aa..56f4bf66ce23 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -398,11 +398,10 @@ class StringTableSection : public LinkEditSection {
   void writeTo(uint8_t *buf) const override;
 
 private:
-  // An n_strx value of 0 always indicates the empty string, so we must locate
-  // our non-empty string values at positive offsets in the string table.
-  // Therefore we insert a dummy value at position zero.
-  std::vector<StringRef> strings{"\0"};
-  size_t size = 1;
+  // ld64 emits string tables which start with a space and a zero byte. We
+  // match its behavior here since some tools depend on it.
+  std::vector<StringRef> strings{" "};
+  size_t size = 2;
 };
 
 struct SymtabEntry {

diff  --git a/lld/test/MachO/symtab.s b/lld/test/MachO/symtab.s
index 22dc80ad62f3..a4dce665acb4 100644
--- a/lld/test/MachO/symtab.s
+++ b/lld/test/MachO/symtab.s
@@ -4,11 +4,11 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o %t/libfoo.o
 # RUN: %lld -dylib %t/libfoo.o -o %t/libfoo.dylib
 # RUN: %lld -lSystem %t/test.o %t/libfoo.dylib -o %t/test
-# RUN: llvm-readobj --syms --macho-dysymtab %t/test | FileCheck %s
 
+# RUN: llvm-readobj --syms --macho-dysymtab %t/test | FileCheck %s
 # CHECK:      Symbols [
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: _local (1)
+# CHECK-NEXT:     Name: _local (2)
 # CHECK-NEXT:     Type: Section (0xE)
 # CHECK-NEXT:     Section: __data (0x4)
 # CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
@@ -17,7 +17,7 @@
 # CHECK-NEXT:     Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: _main (8)
+# CHECK-NEXT:     Name: _main (9)
 # CHECK-NEXT:     Extern
 # CHECK-NEXT:     Type: Section (0xE)
 # CHECK-NEXT:     Section: __text (0x1)
@@ -27,7 +27,7 @@
 # CHECK-NEXT:     Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: _external (54)
+# CHECK-NEXT:     Name: _external (55)
 # CHECK-NEXT:     Extern
 # CHECK-NEXT:     Type: Section (0xE)
 # CHECK-NEXT:     Section: __data (0x4)
@@ -37,7 +37,7 @@
 # CHECK-NEXT:     Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: _external_weak (64)
+# CHECK-NEXT:     Name: _external_weak (65)
 # CHECK-NEXT:     Extern
 # CHECK-NEXT:     Type: Section (0xE)
 # CHECK-NEXT:     Section: __text (0x1)
@@ -48,7 +48,7 @@
 # CHECK-NEXT:     Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: __dyld_private (102)
+# CHECK-NEXT:     Name: __dyld_private (103)
 # CHECK-NEXT:     Extern
 # CHECK-NEXT:     Type: Section (0xE)
 # CHECK-NEXT:     Section: __data (0x4)
@@ -58,7 +58,7 @@
 # CHECK-NEXT:     Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: dyld_stub_binder (14)
+# CHECK-NEXT:     Name: dyld_stub_binder (15)
 # CHECK-NEXT:     Type: Undef (0x0)
 # CHECK-NEXT:     Section:  (0x0)
 # CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
@@ -67,7 +67,7 @@
 # CHECK-NEXT:     Value: 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: _dynamic (79)
+# CHECK-NEXT:     Name: _dynamic (80)
 # CHECK-NEXT:     Type: Undef (0x0)
 # CHECK-NEXT:     Section:  (0x0)
 # CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
@@ -84,6 +84,11 @@
 # CHECK-NEXT:   iundefsym: 5
 # CHECK-NEXT:   nundefsym: 2
 
+## Verify that the first entry in the StringTable is a space.
+# RUN: obj2yaml %t/test | FileCheck %s --check-prefix=YAML
+# YAML:      StringTable:
+# YAML-NEXT: ' '
+
 #--- libfoo.s
 .globl _dynamic
 _dynamic:


        


More information about the llvm-branch-commits mailing list