llvm.org GIT mirror llvm / f99a09c
[yaml2obj][MachO] Don't fill dummy data for virtual sections Summary: Currently, MachOWriter::writeSectionData writes dummy data (0xdeadbeef) to fill section data areas in the file even if the section is a virtual one. Since virtual sections don't occupy any space in the file, writing dummy data could results the "OS.tell() - fileStart <= Sec.offset" assertion failure. This patch fixes the bug by simply not writing any dummy data for virtual sections. Reviewers: beanz, jhenderson, rupprecht, alexshap Reviewed By: alexshap Subscribers: compnerd, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62991 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363525 91177308-0d34-0410-b5e6-96231b3b80d8 Seiya Nuta 2 months ago
2 changed file(s) with 241 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 FileHeader:
4 magic: 0xFEEDFACE
5 cputype: 0x00000007
6 cpusubtype: 0x00000003
7 filetype: 0x00000001
8 ncmds: 4
9 sizeofcmds: 660
10 flags: 0x00002000
11 LoadCommands:
12 - cmd: LC_SEGMENT
13 cmdsize: 532
14 segname: ''
15 vmaddr: 0
16 vmsize: 175
17 fileoff: 688
18 filesize: 160
19 maxprot: 7
20 initprot: 7
21 nsects: 7
22 flags: 0
23 Sections:
24 - sectname: __text
25 segname: __TEXT
26 addr: 0x0000000000000000
27 size: 72
28 offset: 0x000002B0
29 align: 4
30 reloff: 0x00000350
31 nreloc: 7
32 flags: 0x80000400
33 reserved1: 0x00000000
34 reserved2: 0x00000000
35 reserved3: 0x00000000
36 - sectname: __data
37 segname: __DATA
38 addr: 0x0000000000000048
39 size: 4
40 offset: 0x000002F8
41 align: 2
42 reloff: 0x00000000
43 nreloc: 0
44 flags: 0x00000000
45 reserved1: 0x00000000
46 reserved2: 0x00000000
47 reserved3: 0x00000000
48 - sectname: __bss
49 segname: __DATA
50 addr: 0x00000000000000A0
51 size: 8
52 offset: 0x00000000
53 align: 2
54 reloff: 0x00000000
55 nreloc: 0
56 flags: 0x00000001
57 reserved1: 0x00000000
58 reserved2: 0x00000000
59 reserved3: 0x00000000
60 - sectname: __thread_bss
61 segname: __DATA
62 addr: 0x00000000000000A8
63 size: 7
64 offset: 0x00000000
65 align: 0
66 reloff: 0x00000000
67 nreloc: 0
68 flags: 0x00000012
69 reserved1: 0x00000000
70 reserved2: 0x00000000
71 reserved3: 0x00000000
72 - sectname: __thread_vars
73 segname: __DATA
74 addr: 0x000000000000004C
75 size: 12
76 offset: 0x000002FC
77 align: 0
78 reloff: 0x00000388
79 nreloc: 2
80 flags: 0x00000013
81 reserved1: 0x00000000
82 reserved2: 0x00000000
83 reserved3: 0x00000000
84 - cmd: LC_BUILD_VERSION
85 cmdsize: 24
86 platform: 1
87 minos: 658944
88 sdk: 0
89 ntools: 0
90 - cmd: LC_SYMTAB
91 cmdsize: 24
92 symoff: 928
93 nsyms: 7
94 stroff: 1012
95 strsize: 48
96 - cmd: LC_DYSYMTAB
97 cmdsize: 80
98 ilocalsym: 0
99 nlocalsym: 4
100 iextdefsym: 4
101 nextdefsym: 2
102 iundefsym: 6
103 nundefsym: 1
104 tocoff: 0
105 ntoc: 0
106 modtaboff: 0
107 nmodtab: 0
108 extrefsymoff: 0
109 nextrefsyms: 0
110 indirectsymoff: 0
111 nindirectsyms: 0
112 extreloff: 0
113 nextrel: 0
114 locreloff: 0
115 nlocrel: 0
116 LinkEditData:
117 NameList:
118 - n_strx: 44
119 n_type: 0x0E
120 n_sect: 2
121 n_desc: 0
122 n_value: 72
123 - n_strx: 41
124 n_type: 0x0E
125 n_sect: 3
126 n_desc: 0
127 n_value: 160
128 - n_strx: 38
129 n_type: 0x0E
130 n_sect: 3
131 n_desc: 0
132 n_value: 164
133 - n_strx: 1
134 n_type: 0x0E
135 n_sect: 4
136 n_desc: 0
137 n_value: 168
138 - n_strx: 35
139 n_type: 0x0F
140 n_sect: 5
141 n_desc: 0
142 n_value: 76
143 - n_strx: 29
144 n_type: 0x0F
145 n_sect: 1
146 n_desc: 0
147 n_value: 0
148 - n_strx: 13
149 n_type: 0x01
150 n_sect: 0
151 n_desc: 0
152 n_value: 0
153 StringTable:
154 - ''
155 - '_d$tlv$init'
156 - __tlv_bootstrap
157 - _main
158 - _d
159 - _c
160 - _b
161 - _a
162 - ''
163 ...
164
165 # CHECK: Sections:
166 # CHECK-NEXT: - sectname: __text
167 # CHECK-NEXT: segname: __TEXT
168 # CHECK-NEXT: addr: 0x0000000000000000
169 # CHECK-NEXT: size: 72
170 # CHECK-NEXT: offset: 0x000002B0
171 # CHECK-NEXT: align: 4
172 # CHECK-NEXT: reloff: 0x00000350
173 # CHECK-NEXT: nreloc: 7
174 # CHECK-NEXT: flags: 0x80000400
175 # CHECK-NEXT: reserved1: 0x00000000
176 # CHECK-NEXT: reserved2: 0x00000000
177 # CHECK-NEXT: reserved3: 0x00000000
178 # CHECK-NEXT: - sectname: __data
179 # CHECK-NEXT: segname: __DATA
180 # CHECK-NEXT: addr: 0x0000000000000048
181 # CHECK-NEXT: size: 4
182 # CHECK-NEXT: offset: 0x000002F8
183 # CHECK-NEXT: align: 2
184 # CHECK-NEXT: reloff: 0x00000000
185 # CHECK-NEXT: nreloc: 0
186 # CHECK-NEXT: flags: 0x00000000
187 # CHECK-NEXT: reserved1: 0x00000000
188 # CHECK-NEXT: reserved2: 0x00000000
189 # CHECK-NEXT: reserved3: 0x00000000
190 # CHECK-NEXT: - sectname: __bss
191 # CHECK-NEXT: segname: __DATA
192 # CHECK-NEXT: addr: 0x00000000000000A0
193 # CHECK-NEXT: size: 8
194 # CHECK-NEXT: offset: 0x00000000
195 # CHECK-NEXT: align: 2
196 # CHECK-NEXT: reloff: 0x00000000
197 # CHECK-NEXT: nreloc: 0
198 # CHECK-NEXT: flags: 0x00000001
199 # CHECK-NEXT: reserved1: 0x00000000
200 # CHECK-NEXT: reserved2: 0x00000000
201 # CHECK-NEXT: reserved3: 0x00000000
202 # CHECK-NEXT: - sectname: __thread_bss
203 # CHECK-NEXT: segname: __DATA
204 # CHECK-NEXT: addr: 0x00000000000000A8
205 # CHECK-NEXT: size: 7
206 # CHECK-NEXT: offset: 0x00000000
207 # CHECK-NEXT: align: 0
208 # CHECK-NEXT: reloff: 0x00000000
209 # CHECK-NEXT: nreloc: 0
210 # CHECK-NEXT: flags: 0x00000012
211 # CHECK-NEXT: reserved1: 0x00000000
212 # CHECK-NEXT: reserved2: 0x00000000
213 # CHECK-NEXT: reserved3: 0x00000000
214 # CHECK-NEXT: - sectname: __thread_vars
215 # CHECK-NEXT: segname: __DATA
216 # CHECK-NEXT: addr: 0x000000000000004C
217 # CHECK-NEXT: size: 12
218 # CHECK-NEXT: offset: 0x000002FC
219 # CHECK-NEXT: align: 0
220 # CHECK-NEXT: reloff: 0x00000388
221 # CHECK-NEXT: nreloc: 2
222 # CHECK-NEXT: flags: 0x00000013
223 # CHECK-NEXT: reserved1: 0x00000000
224 # CHECK-NEXT: reserved2: 0x00000000
225 # CHECK-NEXT: reserved3: 0x00000000
259259 }
260260 }
261261 return Error::success();
262 }
263
264 static bool isVirtualSection(uint8_t type) {
265 return (type == MachO::S_ZEROFILL ||
266 type == MachO::S_GB_ZEROFILL ||
267 type == MachO::S_THREAD_LOCAL_ZEROFILL);
262268 }
263269
264270 Error MachOWriter::writeSectionData(raw_ostream &OS) {
299305 } else if (0 == strncmp(&Sec.sectname[0], "__debug_line", 16)) {
300306 DWARFYAML::EmitDebugLine(OS, Obj.DWARF);
301307 }
302 } else {
303 // Fills section data with 0xDEADBEEF
304 Fill(OS, Sec.size, 0xDEADBEEFu);
308
309 continue;
305310 }
311
312 // Skip if it's a virtual section.
313 if (isVirtualSection(Sec.flags & MachO::SECTION_TYPE))
314 continue;
315
316 // Fill section data with 0xDEADBEEF
317 Fill(OS, Sec.size, 0xDEADBEEFu);
306318 }
307319 uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize
308320 : LC.Data.segment_command_data.filesize;