Issue 238320
Summary [llvm-objcopy][MachO] --add-section on an object file creates a second segment that ld rejects
Labels new issue
Assignees
Reporter ashoror
    `llvm-objcopy --add-section` with a new segment name creates a second `LC_SEGMENT` on MachO object files . An object file normally has one segment that holds all sections, and Apple's `ld` rejects the result: `ld -r` on every arch, and the final link on armv7k (which uses ld-classic).

Tested on LLVM 23.1.4, macOS 26.6.1 arm64, Xcode 26.5 (`ld-1267`).

### Sample
```c
// plain.c
void foo(void) {}
```
```console
clang --target=arm64-apple-macos -c plain.c -o plain.o
printf 'payload\n' > payload.bin
llvm-objcopy --add-section=__FOO,__bar=payload.bin plain.o out.o

otool -l plain.o | grep -c 'cmd LC_SEGMENT'
1
otool -l out.o | grep -c 'cmd LC_SEGMENT'
2

ld -r -arch arm64 out.o -o relinked.o
ld: more than one LC_SEGMENT found in object file file 'out.o' for architecture arm64
```

The same happens on armv7k at a normal link, because ld uses ld-classic for armv7k:
```console
clang --target=armv7k-apple-watchos -c plain.c -o plain-armv7k.o
llvm-objcopy --add-section=__FOO,__bar=payload.bin plain-armv7k.o out-armv7k.o
xcrun --sdk watchos clang -target armv7k-apple-watchos8.0 -dynamiclib out-armv7k.o -o out.dylib
ld: more than one LC_SEGMENT found in object file file 'out-armv7k.o'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

A final link on arm64 (ld-prime) accepts the file, so the problem is easy to miss.

I verified everything myself. Claude Code helped me identify the issue.
Assisted-by: Claude Code

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to