llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

llvm-objcopy MachO: cant find sections in object file when update-section is called

Open Nechda opened this issue 1 year ago • 4 comments

Hi!

I've noticed that when I try to update a section in Mach-O binaries, I receive a failure message:

/opt/llvm-bins/bin/llvm-objcopy: error: 'out.o': could not find segment with name '__DATA'

How to reproduce:

  1. Build llvm from source:
git checkout llvmorg-19.1.7
mkdir build && cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="llvm;clang" ../llvm
ninja clang llvm-readelf llvm-objcopy
  1. Compile example.cpp with builded tools:
#include <stdio.h>

static const char __attribute__((section("__DATA,__storage"))) storage[0x1000] = {0};

int main() {
  for(int i = 0; i < 16; ++i) {
    int val = storage[i];
    printf("0x%02X ", val);
  }
  return 0;
}
./bin/clang++ example.cpp -c -o out.o --target=arm64-apple-macos11
  1. Use llvm-readelf to observe sections:
./bin/llvm-readelf -S out.o

Output has:

Section {
    Index: 1
    Name: __storage (5F 5F 73 74 6F 72 61 67 65 00 00 00 00 00 00 00)
    Segment: __DATA (5F 5F 44 41 54 41 00 00 00 00 00 00 00 00 00 00)
    Address: 0x78
    Size: 0x1000
    Offset: 672
    Alignment: 0
    RelocationOffset: 0x0
    RelocationCount: 0
    Type: Regular (0x0)
    Attributes [ (0x0)
    ]
    Reserved1: 0x0
    Reserved2: 0x0
    Reserved3: 0x0
  }
  1. Updating the section content using llvm-objcopy:
./bin/llvm-objcopy --update-section=__DATA,__storage=example.cpp out.o

Error:

./bin/llvm-objcopy: error: 'out.o': could not find segment with name '__DATA'

Nechda avatar Feb 17 '25 14:02 Nechda

@llvm/issue-subscribers-tools-llvm-objcopy-strip

Author: Dmitry Nechitaev (Nechda)

Hi!

I've noticed that when I try to update a section in Mach-O binaries, I receive a failure message:

/opt/llvm-bins/bin/llvm-objcopy: error: 'out.o': could not find segment with name '__DATA'

How to reproduce:

  1. Build llvm from source:
git checkout llvmorg-19.1.7
mkdir build &amp;&amp; cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="llvm;clang" ../llvm
ninja clang llvm-readelf llvm-objcopy
  1. Compile example.cpp with builded tools:
#include &lt;stdio.h&gt;

static const char __attribute__((section("__DATA,__storage"))) storage[0x1000] = {0};

int main() {
  for(int i = 0; i &lt; 16; ++i) {
    int val = storage[i];
    printf("0x%02X ", val);
  }
  return 0;
}
./bin/clang++ example.cpp -c -o out.o --target=arm64-apple-macos11
  1. Use llvm-readelf to observe sections:
./bin/llvm-readelf -S out.o

Output has:

Section {
    Index: 1
    Name: __storage (5F 5F 73 74 6F 72 61 67 65 00 00 00 00 00 00 00)
    Segment: __DATA (5F 5F 44 41 54 41 00 00 00 00 00 00 00 00 00 00)
    Address: 0x78
    Size: 0x1000
    Offset: 672
    Alignment: 0
    RelocationOffset: 0x0
    RelocationCount: 0
    Type: Regular (0x0)
    Attributes [ (0x0)
    ]
    Reserved1: 0x0
    Reserved2: 0x0
    Reserved3: 0x0
  }
  1. Updating the section content using llvm-objcopy:
./bin/llvm-objcopy --update-section=__DATA,__storage=example.cpp out.o

Error:

./bin/llvm-objcopy: error: 'out.o': could not find segment with name '__DATA'

llvmbot avatar Feb 17 '25 14:02 llvmbot

If you do obj2yaml on out.o you see that the segment name comes up as ''

bin/obj2yaml out.o
--- !mach-o
FileHeader:
  magic:           0xFEEDFACF
  cputype:         0x100000C
  cpusubtype:      0x0
  filetype:        0x1
  ncmds:           4
  sizeofcmds:      520
  flags:           0x2000
  reserved:        0x0
LoadCommands:
  - cmd:             LC_SEGMENT_64
    cmdsize:         392
    segname:         ''
    vmaddr:          0
    vmsize:          176
    fileoff:         552
    filesize:        176
    maxprot:         7
    initprot:        7
    nsects:          4
    flags:           0

The segment names are only present in Sections:

Sections:
     - sectname:        __text
       segname:         __TEXT
       addr:            0x0
       size:            120
       offset:          0x228
       align:           2
       reloff:          0x2D8
       nreloc:          5
       flags:           0x80000400
       reserved1:       0x0
       reserved2:       0x0
       reserved3:       0x0
       content:         FFC300D1FD7B02A9FD830091BFC31FB8BF831FB801000014A8835FB8084100714A02005401000014A9839FB808000090080100910869E938A8431FB8A8435FB8E9030091280100F900000090000000910000009401000014A8835FB808050011A8831FB8EDFFFF1700008052FD7B42A9FFC30091C0035FD6
       relocations:
         - address:         0x50
           symbolnum:       7
           pcrel:           true
           length:          2
           extern:          true
           type:            2
           scattered:       false
           value:           0
         - address:         0x4C
           symbolnum:       2
           pcrel:           false
           length:          2
           extern:          true
           type:            4
           scattered:       false
           value:           0
         - address:         0x48
           symbolnum:       2
           pcrel:           true
           length:          2
           extern:          true
           type:            3
           scattered:       false
           value:           0
         - address:         0x30
           symbolnum:       1
           pcrel:           false
           length:          2
           extern:          true
           type:            4
           scattered:       false
           value:           0
         - address:         0x2C
           symbolnum:       1
           pcrel:           true
           length:          2
           extern:          true
           type:            3
           scattered:       false
           value:           0
     - sectname:        __storage
       segname:         __DATA
       addr:            0x78
       size:            10
       offset:          0x2A0
       align:           0
       reloff:          0x0
       nreloc:          0
       flags:           0x0
       reserved1:       0x0
       reserved2:       0x0

We try to find the segment name in the following piece of code

  auto FoundSeg =
      llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
        return LC.getSegmentName() == SegName;
   });

but LC.getSegmentName returns '' leading to the error you see.

svs-quic avatar Feb 18 '25 08:02 svs-quic

Hm, should we change the section-finding policy for the MH_OBJECT file type? It seems that the sections know its segment.

When I apply such patch:

--- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
@@ -341,6 +341,9 @@ static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
 static Expected<Section &> findSection(StringRef SecName, Object &O) {
   StringRef SegName;
   std::tie(SegName, SecName) = SecName.split(",");
+  for (const auto& LC : O.LoadCommands)
+    for (const auto& Sec : LC.Sections)
+      llvm::errs() << Sec->Segname << ',' << Sec->Sectname << '\n';
   auto FoundSeg =
       llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
         return LC.getSegmentName() == SegName;

llvm-objcopy dumps correct pair for each section:

user@dev:~/build$ ./bin/llvm-objcopy --update-section=__DATA,__storage=example.cc out.o
__TEXT,__text
__DATA,__storage
__TEXT,__cstring
__LD,__compact_unwind
./bin/llvm-objcopy: error: 'out.o': could not find segment with name '__DATA'

Nechda avatar Feb 18 '25 09:02 Nechda

I'm not the best person to answer that. Maybe someone who has more experience working on MachO can respond.

svs-quic avatar Feb 18 '25 15:02 svs-quic