[llvm] [llvm-objcopy] Always update indirectsymoff in MachO (PR #117726)

Richard Dzenis via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 27 00:08:01 PST 2024


https://github.com/RIscRIpt updated https://github.com/llvm/llvm-project/pull/117726

>From a1105e9ef62522e38b64c42b149ef61e7609a9ce Mon Sep 17 00:00:00 2001
From: Richard Dzenis <richard.dzenis at zimperium.com>
Date: Tue, 26 Nov 2024 17:23:24 +0200
Subject: [PATCH 1/3] [llvm-objcopy] Always update indirectsymoff in MachO
 (#117723)

Let's say we've run llvm-strip over some MachO. The resulting MachO
became smaller and there are no indirect symbols anymore.

Then according to previous code we would not update indirectsymoff
field. This would lead to `MachOWriter::totalSize()` report size that is
larger than the new MachO. As a result we would get MachO that has zero
bytes past contents of the very last load command.

Codesign has a strict check that size of MachO file must be equal to

    lastLoadCommand.offset + lastLoadCommand.size

If this is not satisfied codesign reports the following error

    main executable failed strict validation
---
 llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
index a3d4ba3a94f7ac..551ff4501a66fb 100644
--- a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
@@ -361,11 +361,10 @@ Error MachOLayoutBuilder::layoutTail(uint64_t Offset) {
         return createStringError(llvm::errc::not_supported,
                                  "shared library is not yet supported");
 
-      if (!O.IndirectSymTable.Symbols.empty()) {
-        MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
-        MLC.dysymtab_command_data.nindirectsyms =
-            O.IndirectSymTable.Symbols.size();
-      }
+      MLC.dysymtab_command_data.nindirectsyms =
+          O.IndirectSymTable.Symbols.size();
+      MLC.dysymtab_command_data.indirectsymoff =
+          O.IndirectSymTable.Symbols.empty() ? 0 : StartOfIndirectSymbols;
 
       updateDySymTab(MLC);
       break;

>From ab32979eb1fca1064266e10ef9623c993d25ca0a Mon Sep 17 00:00:00 2001
From: Richard Dzenis <richard.dzenis at zimperium.com>
Date: Wed, 27 Nov 2024 07:49:16 +0200
Subject: [PATCH 2/3] fixup! [llvm-objcopy] Always update indirectsymoff in
 MachO (#117723)

---
 llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
index 551ff4501a66fb..93bc6631e64c86 100644
--- a/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
@@ -360,12 +360,10 @@ Error MachOLayoutBuilder::layoutTail(uint64_t Offset) {
           MLC.dysymtab_command_data.nextrel != 0)
         return createStringError(llvm::errc::not_supported,
                                  "shared library is not yet supported");
-
+      MLC.dysymtab_command_data.indirectsymoff =
+          O.IndirectSymTable.Symbols.size() ? StartOfIndirectSymbols : 0;
       MLC.dysymtab_command_data.nindirectsyms =
           O.IndirectSymTable.Symbols.size();
-      MLC.dysymtab_command_data.indirectsymoff =
-          O.IndirectSymTable.Symbols.empty() ? 0 : StartOfIndirectSymbols;
-
       updateDySymTab(MLC);
       break;
     }

>From 6677e7f5978e790a471b41ed15d7edc9f09dee57 Mon Sep 17 00:00:00 2001
From: Richard Dzenis <richard.dzenis at zimperium.com>
Date: Wed, 27 Nov 2024 10:03:53 +0200
Subject: [PATCH 3/3] Add lit test to check llvm-strip zeroes out
 indirectsymoff in MachO

---
 .../MachO/symtbl-zero-indirectsymoff.test     | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 llvm/test/tools/llvm-objcopy/MachO/symtbl-zero-indirectsymoff.test

diff --git a/llvm/test/tools/llvm-objcopy/MachO/symtbl-zero-indirectsymoff.test b/llvm/test/tools/llvm-objcopy/MachO/symtbl-zero-indirectsymoff.test
new file mode 100644
index 00000000000000..2392f215091d57
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/symtbl-zero-indirectsymoff.test
@@ -0,0 +1,45 @@
+## Show that llvm-strip correctly zeroes out indirectsymoff in LC_DYSYMTAB if there are no symbols
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-strip --strip-all %t -o %t.stripped
+# RUN: obj2yaml %t.stripped | FileCheck %s
+
+# CHECK: indirectsymoff: 0
+
+--- !mach-o
+FileHeader:
+  magic:           0xFEEDFACF
+  cputype:         0x01000007
+  cpusubtype:      0x80000003
+  filetype:        0x00000002
+  ncmds:           2
+  sizeofcmds:      104
+  flags:           0x00200085
+  reserved:        0x00000000
+LoadCommands:
+  - cmd:             LC_SYMTAB
+    cmdsize:         24
+    symoff:          0
+    nsyms:           0
+    stroff:          0
+    strsize:         0
+  - cmd:             LC_DYSYMTAB
+    cmdsize:         80
+    ilocalsym:       0
+    nlocalsym:       0
+    iextdefsym:      0
+    nextdefsym:      0
+    iundefsym:       0
+    nundefsym:       0
+    tocoff:          0
+    ntoc:            0
+    modtaboff:       0
+    nmodtab:         0
+    extrefsymoff:    0
+    nextrefsyms:     0
+    indirectsymoff:  42
+    nindirectsyms:   0
+    extreloff:       0
+    nextrel:         0
+    locreloff:       0
+    nlocrel:         0
+...



More information about the llvm-commits mailing list