1 From 57dc16fbe3078b4f27e804a2a3a6594e46897c3c Mon Sep 17 00:00:00 2001
2 From: Mike Hommey <mh@glandium.org>
3 Date: Sat, 3 Jun 2023 09:31:42 +0200
4 Subject: [PATCH] [llvm] Strip stabs symbols in Mach-O when stripping debug
8 llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp | 3 +
9 .../MachO/Inputs/strip-stabs.yaml | 248 ++++++++++++++++++
10 .../tools/llvm-objcopy/MachO/strip-stabs.test | 17 ++
11 3 files changed, 268 insertions(+)
12 create mode 100644 llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-stabs.yaml
13 create mode 100644 llvm/test/tools/llvm-objcopy/MachO/strip-stabs.test
15 diff --git a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
16 index d37241682efe..e26b363df21c 100644
17 --- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
18 +++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
19 @@ -112,6 +112,9 @@ static void updateAndRemoveSymbols(const CommonConfig &Config,
20 if (Config.DiscardMode == DiscardType::All && !(N->n_type & MachO::N_EXT))
22 // This behavior is consistent with cctools' strip.
23 + if (Config.StripDebug && (N->n_type & MachO::N_STAB))
25 + // This behavior is consistent with cctools' strip.
26 if (MachOConfig.StripSwiftSymbols &&
27 (Obj.Header.Flags & MachO::MH_DYLDLINK) && Obj.SwiftVersion &&
28 *Obj.SwiftVersion && N->isSwiftSymbol())
29 diff --git a/llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-stabs.yaml b/llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-stabs.yaml
31 index 000000000000..3259aa228fed
33 +++ b/llvm/test/tools/llvm-objcopy/MachO/Inputs/strip-stabs.yaml
39 + cpusubtype: 0x80000003
46 + - cmd: LC_SEGMENT_64
57 + - cmd: LC_SEGMENT_64
81 + content: 554889E531C05DC3
82 + - sectname: __unwind_info
94 + - sectname: __eh_frame
106 + content: 1400000000000000017A520001781001100C070890010000
107 + - cmd: LC_SEGMENT_64
109 + segname: __LINKEDIT
118 + - cmd: LC_DYLD_INFO_ONLY
156 + - cmd: LC_LOAD_DYLINKER
159 + Content: '/usr/lib/dyld'
163 + uuid: 4C4C44DE-5555-3144-A19D-79B149A02D5F
164 + - cmd: LC_BUILD_VERSION
177 + - cmd: LC_LOAD_DYLIB
182 + current_version: 86467587
183 + compatibility_version: 65536
184 + Content: '/usr/lib/libSystem.B.dylib'
186 + - cmd: LC_FUNCTION_STARTS
190 + - cmd: LC_DATA_IN_CODE
221 + Name: _mh_execute_header
241 + n_value: 4294968176
256 + n_value: 4294968176
261 + n_value: 4294967296
271 + - __mh_execute_header
273 + - '/tmp/test-6aa924.o'
281 + FunctionStarts: [ 0x370 ]
283 diff --git a/llvm/test/tools/llvm-objcopy/MachO/strip-stabs.test b/llvm/test/tools/llvm-objcopy/MachO/strip-stabs.test
285 index 000000000000..90c00f60a152
287 +++ b/llvm/test/tools/llvm-objcopy/MachO/strip-stabs.test
289 +## Show that llvm-objcopy/llvm-strip stabs symbols and debug sections.
291 +# RUN: yaml2obj %p/Inputs/strip-stabs.yaml -o %t
293 +# RUN: llvm-objcopy --strip-debug %t %t.stripped
295 +## Make sure that stabs symbols are stripped.
296 +# RUN: llvm-readobj --symbols %t | FileCheck %s --check-prefix=SYM
297 +# RUN: llvm-readobj --symbols %t.stripped | FileCheck %s --check-prefix=SYM_STRIP
300 +# SYM-COUNT-5: Type: SymDebugTable ({{.*}})
303 +# SYM_STRIP: Symbols [
304 +# SYM_STRIP-NOT: Type: SymDebugTable ({{.*}})