Skip to content

Commit 9c97aa5

Browse files
authored
[llvm-objcopy] Always update indirectsymoff in MachO (#117726)
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 Fixes #117723
1 parent 26ffca0 commit 9c97aa5

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,10 @@ Error MachOLayoutBuilder::layoutTail(uint64_t Offset) {
360360
MLC.dysymtab_command_data.nextrel != 0)
361361
return createStringError(llvm::errc::not_supported,
362362
"shared library is not yet supported");
363-
364-
if (!O.IndirectSymTable.Symbols.empty()) {
365-
MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
366-
MLC.dysymtab_command_data.nindirectsyms =
367-
O.IndirectSymTable.Symbols.size();
368-
}
369-
363+
MLC.dysymtab_command_data.indirectsymoff =
364+
O.IndirectSymTable.Symbols.size() ? StartOfIndirectSymbols : 0;
365+
MLC.dysymtab_command_data.nindirectsyms =
366+
O.IndirectSymTable.Symbols.size();
370367
updateDySymTab(MLC);
371368
break;
372369
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Show that llvm-strip correctly zeroes out indirectsymoff and indirectsyms
2+
## in LC_DYSYMTAB if there are no symbols
3+
#
4+
# RUN: yaml2obj %s -o %t
5+
# RUN: llvm-strip --strip-all %t -o %t.stripped
6+
# RUN: obj2yaml %t.stripped | FileCheck %s
7+
8+
# CHECK: indirectsymoff: 0
9+
# CHECK-NEXT: nindirectsyms: 0
10+
11+
--- !mach-o
12+
FileHeader:
13+
magic: 0xFEEDFACF
14+
cputype: 0x01000007
15+
cpusubtype: 0x80000003
16+
filetype: 0x00000002
17+
ncmds: 2
18+
sizeofcmds: 104
19+
flags: 0x00200085
20+
reserved: 0x00000000
21+
LoadCommands:
22+
- cmd: LC_SYMTAB
23+
cmdsize: 24
24+
symoff: 0
25+
nsyms: 0
26+
stroff: 0
27+
strsize: 0
28+
- cmd: LC_DYSYMTAB
29+
cmdsize: 80
30+
ilocalsym: 0
31+
nlocalsym: 0
32+
iextdefsym: 0
33+
nextdefsym: 0
34+
iundefsym: 0
35+
nundefsym: 0
36+
tocoff: 0
37+
ntoc: 0
38+
modtaboff: 0
39+
nmodtab: 0
40+
extrefsymoff: 0
41+
nextrefsyms: 0
42+
indirectsymoff: 42
43+
nindirectsyms: 0
44+
extreloff: 0
45+
nextrel: 0
46+
locreloff: 0
47+
nlocrel: 0
48+
...

0 commit comments

Comments
 (0)