[PATCH] D104092: [llvm-objcopy][MachO] Do not strip symbols having the flag REFERENCED_DYNAMICALLY set

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 10 19:24:55 PDT 2021


alexshap created this revision.
alexshap added reviewers: smeenai, jhenderson, rmaz.
Herald added a reviewer: rupprecht.
Herald added a subscriber: abrachet.
alexshap requested review of this revision.
Herald added a subscriber: MaskRay.
Herald added a project: LLVM.

Do not strip symbols  having the flag REFERENCED_DYNAMICALLY set.

This is compatible with the behavior of cctools' strip and consistent with the documentation:

"REFERENCED_DYNAMICALLY (0x10)—Must be set for any symbol that might be referenced
by another image. The strip tool uses this bit to avoid removing symbols that must exist:
If the symbol has this bit set, strip does not strip it."

Test plan: make check-all


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104092

Files:
  llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-all-with-dwarf.yaml
  llvm/test/tools/llvm-objcopy/MachO/strip-all.test
  llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp


Index: llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
===================================================================
--- llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
+++ llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
@@ -99,6 +99,8 @@
       return false;
     if (Config.KeepUndefined && N->isUndefinedSymbol())
       return false;
+    if (N->n_desc & MachO::REFERENCED_DYNAMICALLY)
+      return false;
     if (Config.StripAll)
       return true;
     if (Config.DiscardMode == DiscardType::All && !(N->n_type & MachO::N_EXT))
Index: llvm/test/tools/llvm-objcopy/MachO/strip-all.test
===================================================================
--- llvm/test/tools/llvm-objcopy/MachO/strip-all.test
+++ llvm/test/tools/llvm-objcopy/MachO/strip-all.test
@@ -45,8 +45,20 @@
 # DWARF-NOT:     Name: __debug_line
 # DWARF:       ]
 
-## Make sure that all relocations and symbols are removed.
+## Make sure that all relocations and symbols
+## (except those which have the flag ReferencedDynamically set) are removed.
 # COMMON:      Relocations [
 # COMMON-NEXT: ]
-# COMMON:      Symbols [
+# COMMON-NEXT: Symbols [
+# COMMON-NEXT:    Symbol {
+# COMMON-NEXT:      Name: __mh_execute_header
+# COMMON-NEXT:      Extern
+# COMMON-NEXT:      Type: Section
+# COMMON-NEXT:      Section: __text
+# COMMON-NEXT:      RefType: UndefinedNonLazy
+# COMMON-NEXT:      Flags [ (0x10)
+# COMMON-NEXT:        ReferencedDynamically (0x10)
+# COMMON-NEXT:      ]
+# COMMON-NEXT:      Value:
+# COMMON-NEXT:    }
 # COMMON-NEXT: ]
Index: llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-all-with-dwarf.yaml
===================================================================
--- llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-all-with-dwarf.yaml
+++ llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-all-with-dwarf.yaml
@@ -254,9 +254,9 @@
   - cmd:             LC_SYMTAB
     cmdsize:         24
     symoff:          2136
-    nsyms:           3
-    stroff:          2184
-    strsize:         20
+    nsyms:           4
+    stroff:          2204
+    strsize:         40
   - cmd:             LC_DYSYMTAB
     cmdsize:         80
     ilocalsym:       0
@@ -279,23 +279,29 @@
     nlocrel:         0
 LinkEditData:
   NameList:
-    - n_strx:          1
+    - n_strx:          21
       n_type:          0x0E
       n_sect:          2
       n_desc:          0
       n_value:         36
-    - n_strx:          11
+    - n_strx:          31
       n_type:          0x0F
       n_sect:          1
       n_desc:          0
       n_value:         0
-    - n_strx:          6
+    - n_strx:          1
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          16
+      n_value:         4294967296
+    - n_strx:          26
       n_type:          0x01
       n_sect:          0
       n_desc:          512
       n_value:         4
   StringTable:
     - ''
+    - __mh_execute_header
     - _bar
     - _foo
     - _main


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104092.351335.patch
Type: text/x-patch
Size: 2950 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210611/6432cd26/attachment.bin>


More information about the llvm-commits mailing list