llvm.org GIT mirror llvm / 429d7a4
[obj2yaml][yaml2obj] Support for MachO weak bindings This adds support for YAML round tripping dyld info weak bindings. The storage and format of these is the same as regular bind opcodes, they are just interpreted differently by dyld. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270911 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
5 changed file(s) with 163 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
7373 struct LinkEditData {
7474 std::vector RebaseOpcodes;
7575 std::vector BindOpcodes;
76 std::vector WeakBindOpcodes;
7677 };
7778
7879 struct Object {
100100 IO &IO, MachOYAML::LinkEditData &LinkEditData) {
101101 IO.mapOptional("RebaseOpcodes", LinkEditData.RebaseOpcodes);
102102 IO.mapOptional("BindOpcodes", LinkEditData.BindOpcodes);
103 IO.mapOptional("WeakBindOpcodes", LinkEditData.WeakBindOpcodes);
103104 }
104105
105106 void MappingTraits::mapping(
0 # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 FileHeader:
4 magic: 0xFEEDFACF
5 cputype: 0x01000007
6 cpusubtype: 0x80000003
7 filetype: 0x00000002
8 ncmds: 4
9 sizeofcmds: 224
10 flags: 0x00218085
11 reserved: 0x00000000
12 LoadCommands:
13 - cmd: LC_SEGMENT_64
14 cmdsize: 72
15 segname: __LINKEDIT
16 vmaddr: 4294979584
17 vmsize: 4096
18 fileoff: 1024
19 filesize: 2508
20 maxprot: 7
21 initprot: 1
22 nsects: 0
23 flags: 0
24 - cmd: LC_DYLD_INFO_ONLY
25 cmdsize: 48
26 rebase_off: 1024
27 rebase_size: 8
28 bind_off: 0
29 bind_size: 0
30 weak_bind_off: 1032
31 weak_bind_size: 96
32 lazy_bind_off: 1128
33 lazy_bind_size: 624
34 export_off: 1752
35 export_size: 48
36 - cmd: LC_SYMTAB
37 cmdsize: 24
38 symoff: 1816
39 nsyms: 30
40 stroff: 2436
41 strsize: 1096
42 - cmd: LC_DYSYMTAB
43 cmdsize: 80
44 ilocalsym: 0
45 nlocalsym: 9
46 iextdefsym: 9
47 nextdefsym: 2
48 iundefsym: 11
49 nundefsym: 19
50 tocoff: 0
51 ntoc: 0
52 modtaboff: 0
53 nmodtab: 0
54 extrefsymoff: 0
55 nextrefsyms: 0
56 indirectsymoff: 2296
57 nindirectsyms: 35
58 extreloff: 0
59 nextrel: 0
60 locreloff: 0
61 nlocrel: 0
62 LinkEditData:
63 WeakBindOpcodes:
64 - Opcode: BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
65 Imm: 1
66 - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
67 Imm: 0
68 Symbol: __ZNSt3__14coutE
69 - Opcode: BIND_OPCODE_SET_TYPE_IMM
70 Imm: 1
71 - Opcode: BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
72 Imm: 2
73 ULEBExtraData:
74 - 0x0000000000000000
75 - Opcode: BIND_OPCODE_DO_BIND
76 Imm: 0
77 - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
78 Imm: 0
79 Symbol: __ZNSt3__15ctypeIcE2idE
80 - Opcode: BIND_OPCODE_DO_BIND
81 Imm: 0
82 - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
83 Imm: 0
84 Symbol: ___gxx_personality_v0
85 - Opcode: BIND_OPCODE_DO_BIND
86 Imm: 0
87 - Opcode: BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
88 Imm: 2
89 - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
90 Imm: 0
91 Symbol: dyld_stub_binder
92 - Opcode: BIND_OPCODE_DO_BIND
93 Imm: 0
94 - Opcode: BIND_OPCODE_DONE
95 Imm: 0
96 ...
97
98 #CHECK: LinkEditData:
99 #CHECK: WeakBindOpcodes:
100 #CHECK: - Opcode: BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
101 #CHECK: Imm: 1
102 #CHECK: - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
103 #CHECK: Imm: 0
104 #CHECK: Symbol: __ZNSt3__14coutE
105 #CHECK: - Opcode: BIND_OPCODE_SET_TYPE_IMM
106 #CHECK: Imm: 1
107 #CHECK: - Opcode: BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
108 #CHECK: Imm: 2
109 #CHECK: ULEBExtraData:
110 #CHECK: - 0x0000000000000000
111 #CHECK: - Opcode: BIND_OPCODE_DO_BIND
112 #CHECK: Imm: 0
113 #CHECK: - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
114 #CHECK: Imm: 0
115 #CHECK: Symbol: __ZNSt3__15ctypeIcE2idE
116 #CHECK: - Opcode: BIND_OPCODE_DO_BIND
117 #CHECK: Imm: 0
118 #CHECK: - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
119 #CHECK: Imm: 0
120 #CHECK: Symbol: ___gxx_personality_v0
121 #CHECK: - Opcode: BIND_OPCODE_DO_BIND
122 #CHECK: Imm: 0
123 #CHECK: - Opcode: BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
124 #CHECK: Imm: 2
125 #CHECK: - Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
126 #CHECK: Imm: 0
127 #CHECK: Symbol: dyld_stub_binder
128 #CHECK: - Opcode: BIND_OPCODE_DO_BIND
129 #CHECK: Imm: 0
130 #CHECK: - Opcode: BIND_OPCODE_DONE
131 #CHECK: Imm: 0
132
196196 void MachODumper::dumpLinkEdit(std::unique_ptr &Y) {
197197 dumpRebaseOpcodes(Y);
198198 dumpBindOpcodes(Y->LinkEdit.BindOpcodes, Obj.getDyldInfoBindOpcodes());
199 dumpBindOpcodes(Y->LinkEdit.WeakBindOpcodes,
200 Obj.getDyldInfoWeakBindOpcodes());
199201 }
200202
201203 void MachODumper::dumpRebaseOpcodes(std::unique_ptr &Y) {
4545 Error writeLoadCommands(raw_ostream &OS);
4646 Error writeSectionData(raw_ostream &OS);
4747 Error writeLinkEditData(raw_ostream &OS);
48 void writeBindOpcodes(raw_ostream &OS, uint64_t offset,
49 std::vector &BindOpcodes);
4850
4951 void ZeroToOffset(raw_ostream &OS, size_t offset);
5052
263265 return Error::success();
264266 }
265267
268 void MachOWriter::writeBindOpcodes(
269 raw_ostream &OS, uint64_t offset,
270 std::vector &BindOpcodes) {
271 ZeroToOffset(OS, offset);
272
273 for (auto Opcode : BindOpcodes) {
274 uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
275 OS.write(reinterpret_cast(&OpByte), 1);
276 for (auto Data : Opcode.ULEBExtraData) {
277 encodeULEB128(Data, OS);
278 }
279 for (auto Data : Opcode.SLEBExtraData) {
280 encodeSLEB128(Data, OS);
281 }
282 if (!Opcode.Symbol.empty()) {
283 OS.write(Opcode.Symbol.data(), Opcode.Symbol.size());
284 OS.write("\0", 1);
285 }
286 }
287 }
288
266289 Error MachOWriter::writeLinkEditData(raw_ostream &OS) {
267290 MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
268291 MachO::dyld_info_command *DyldInfoOnlyCmd = 0;
288311 }
289312 }
290313
291 ZeroToOffset(OS, DyldInfoOnlyCmd->bind_off);
292
293 for (auto Opcode : LinkEdit.BindOpcodes) {
294 uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
295 OS.write(reinterpret_cast(&OpByte), 1);
296 for (auto Data : Opcode.ULEBExtraData) {
297 encodeULEB128(Data, OS);
298 }
299 for (auto Data : Opcode.SLEBExtraData) {
300 encodeSLEB128(Data, OS);
301 }
302 if(!Opcode.Symbol.empty()) {
303 OS.write(Opcode.Symbol.data(), Opcode.Symbol.size());
304 OS.write("\0", 1);
305 }
306 }
314 writeBindOpcodes(OS, DyldInfoOnlyCmd->bind_off, LinkEdit.BindOpcodes);
315 writeBindOpcodes(OS, DyldInfoOnlyCmd->weak_bind_off,
316 LinkEdit.WeakBindOpcodes);
307317
308318 // Fill to the end of the string table
309319 ZeroToOffset(OS, SymtabCmd->stroff + SymtabCmd->strsize);