llvm.org GIT mirror llvm / c2813d9
[ObjectYAML] Support for DWARF Pub Sections This patch adds support for YAML<->DWARF round tripping for pub* section data. The patch supports both GNU and non-GNU style entries. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290139 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 2 years ago
7 changed file(s) with 470 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
4848 std::vector Descriptors;
4949 };
5050
51 struct PubEntry {
52 llvm::yaml::Hex32 DieOffset;
53 llvm::yaml::Hex8 Descriptor;
54 StringRef Name;
55 };
56
57 struct PubSection {
58 PubSection() : IsGNUStyle(false) {}
59
60 uint32_t Length;
61 uint16_t Version;
62 uint32_t UnitOffset;
63 uint32_t UnitSize;
64 bool IsGNUStyle;
65 std::vector Entries;
66 };
67
5168 struct Data {
5269 std::vector AbbrevDecls;
5370 std::vector DebugStrings;
5471 std::vector ARanges;
72 PubSection PubNames;
73 PubSection PubTypes;
74
75 PubSection GNUPubNames;
76 PubSection GNUPubTypes;
5577
5678 bool isEmpty() const;
5779 };
6486 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Abbrev)
6587 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARangeDescriptor)
6688 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARange)
89 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::PubEntry)
6790
6891 namespace llvm {
6992 namespace yaml {
86109
87110 template <> struct MappingTraits {
88111 static void mapping(IO &IO, DWARFYAML::ARange &Range);
112 };
113
114 template <> struct MappingTraits {
115 static void mapping(IO &IO, DWARFYAML::PubEntry &Entry);
116 };
117
118 template <> struct MappingTraits {
119 static void mapping(IO &IO, DWARFYAML::PubSection &Section);
89120 };
90121
91122 #define HANDLE_DW_TAG(unused, name) \
2727 IO.mapOptional("debug_abbrev", DWARF.AbbrevDecls);
2828 if(!DWARF.ARanges.empty() || !IO.outputting())
2929 IO.mapOptional("debug_aranges", DWARF.ARanges);
30 if(!DWARF.PubNames.Entries.empty() || !IO.outputting())
31 IO.mapOptional("debug_pubnames", DWARF.PubNames);
32 if(!DWARF.PubTypes.Entries.empty() || !IO.outputting())
33 IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
34 if(!DWARF.GNUPubNames.Entries.empty() || !IO.outputting())
35 IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
36 if(!DWARF.GNUPubTypes.Entries.empty() || !IO.outputting())
37 IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
3038 }
3139
3240 void MappingTraits::mapping(
5967 IO.mapRequired("Descriptors", Range.Descriptors);
6068 }
6169
70 void MappingTraits::mapping(IO &IO,
71 DWARFYAML::PubEntry &Entry) {
72 IO.mapRequired("DieOffset", Entry.DieOffset);
73 if (reinterpret_cast(IO.getContext())->IsGNUStyle)
74 IO.mapRequired("Descriptor", Entry.Descriptor);
75 IO.mapRequired("Name", Entry.Name);
76 }
77
78 void MappingTraits::mapping(
79 IO &IO, DWARFYAML::PubSection &Section) {
80 auto OldContext = IO.getContext();
81 IO.setContext(&Section);
82
83 IO.mapRequired("Length", Section.Length);
84 IO.mapRequired("Version", Section.Version);
85 IO.mapRequired("UnitOffset", Section.UnitOffset);
86 IO.mapRequired("UnitSize", Section.UnitSize);
87 IO.mapRequired("Entries", Section.Entries);
88
89 IO.setContext(OldContext);
90 }
91
6292 } // namespace llvm::yaml
6393
6494 } // namespace llvm
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 FileHeader:
4 magic: 0xFEEDFACF
5 cputype: 0x01000007
6 cpusubtype: 0x00000003
7 filetype: 0x0000000A
8 ncmds: 5
9 sizeofcmds: 1800
10 flags: 0x00000000
11 reserved: 0x00000000
12 LoadCommands:
13 - cmd: LC_SEGMENT_64
14 cmdsize: 72
15 segname: __PAGEZERO
16 vmaddr: 0
17 vmsize: 4294967296
18 fileoff: 0
19 filesize: 0
20 maxprot: 0
21 initprot: 0
22 nsects: 0
23 flags: 0
24 - cmd: LC_SEGMENT_64
25 cmdsize: 472
26 segname: __TEXT
27 vmaddr: 4294967296
28 vmsize: 4096
29 fileoff: 0
30 filesize: 0
31 maxprot: 7
32 initprot: 5
33 nsects: 5
34 flags: 0
35 Sections:
36 - sectname: __text
37 segname: __TEXT
38 addr: 0x0000000100000F50
39 size: 52
40 offset: 0x00000000
41 align: 4
42 reloff: 0x00000000
43 nreloc: 0
44 flags: 0x80000400
45 reserved1: 0x00000000
46 reserved2: 0x00000000
47 reserved3: 0x00000000
48 - sectname: __stubs
49 segname: __TEXT
50 addr: 0x0000000100000F84
51 size: 6
52 offset: 0x00000000
53 align: 1
54 reloff: 0x00000000
55 nreloc: 0
56 flags: 0x80000408
57 reserved1: 0x00000000
58 reserved2: 0x00000006
59 reserved3: 0x00000000
60 - sectname: __stub_helper
61 segname: __TEXT
62 addr: 0x0000000100000F8C
63 size: 26
64 offset: 0x00000000
65 align: 2
66 reloff: 0x00000000
67 nreloc: 0
68 flags: 0x80000400
69 reserved1: 0x00000000
70 reserved2: 0x00000000
71 reserved3: 0x00000000
72 - sectname: __cstring
73 segname: __TEXT
74 addr: 0x0000000100000FA6
75 size: 14
76 offset: 0x00000000
77 align: 0
78 reloff: 0x00000000
79 nreloc: 0
80 flags: 0x00000002
81 reserved1: 0x00000000
82 reserved2: 0x00000000
83 reserved3: 0x00000000
84 - sectname: __unwind_info
85 segname: __TEXT
86 addr: 0x0000000100000FB4
87 size: 72
88 offset: 0x00000000
89 align: 2
90 reloff: 0x00000000
91 nreloc: 0
92 flags: 0x00000000
93 reserved1: 0x00000000
94 reserved2: 0x00000000
95 reserved3: 0x00000000
96 - cmd: LC_SEGMENT_64
97 cmdsize: 232
98 segname: __DATA
99 vmaddr: 4294971392
100 vmsize: 4096
101 fileoff: 0
102 filesize: 0
103 maxprot: 7
104 initprot: 3
105 nsects: 2
106 flags: 0
107 Sections:
108 - sectname: __nl_symbol_ptr
109 segname: __DATA
110 addr: 0x0000000100001000
111 size: 16
112 offset: 0x00000000
113 align: 3
114 reloff: 0x00000000
115 nreloc: 0
116 flags: 0x00000006
117 reserved1: 0x00000001
118 reserved2: 0x00000000
119 reserved3: 0x00000000
120 - sectname: __la_symbol_ptr
121 segname: __DATA
122 addr: 0x0000000100001010
123 size: 8
124 offset: 0x00000000
125 align: 3
126 reloff: 0x00000000
127 nreloc: 0
128 flags: 0x00000007
129 reserved1: 0x00000003
130 reserved2: 0x00000000
131 reserved3: 0x00000000
132 - cmd: LC_SEGMENT_64
133 cmdsize: 72
134 segname: __LINKEDIT
135 vmaddr: 4294975488
136 vmsize: 4096
137 fileoff: 4096
138 filesize: 60
139 maxprot: 7
140 initprot: 1
141 nsects: 0
142 flags: 0
143 - cmd: LC_SEGMENT_64
144 cmdsize: 952
145 segname: __DWARF
146 vmaddr: 4294979584
147 vmsize: 4096
148 fileoff: 8192
149 filesize: 764
150 maxprot: 7
151 initprot: 3
152 nsects: 11
153 flags: 0
154 Sections:
155 - sectname: __debug_line
156 segname: __DWARF
157 addr: 0x0000000100003000
158 size: 69
159 offset: 0x00002000
160 align: 0
161 reloff: 0x00000000
162 nreloc: 0
163 flags: 0x00000000
164 reserved1: 0x00000000
165 reserved2: 0x00000000
166 reserved3: 0x00000000
167 - sectname: __debug_pubnames
168 segname: __DWARF
169 addr: 0x0000000100003045
170 size: 27
171 offset: 0x00002045
172 align: 0
173 reloff: 0x00000000
174 nreloc: 0
175 flags: 0x00000000
176 reserved1: 0x00000000
177 reserved2: 0x00000000
178 reserved3: 0x00000000
179 - sectname: __debug_pubtypes
180 segname: __DWARF
181 addr: 0x0000000100003060
182 size: 35
183 offset: 0x00002060
184 align: 0
185 reloff: 0x00000000
186 nreloc: 0
187 flags: 0x00000000
188 reserved1: 0x00000000
189 reserved2: 0x00000000
190 reserved3: 0x00000000
191 - sectname: __debug_aranges
192 segname: __DWARF
193 addr: 0x0000000100003083
194 size: 48
195 offset: 0x00002083
196 align: 0
197 reloff: 0x00000000
198 nreloc: 0
199 flags: 0x00000000
200 reserved1: 0x00000000
201 reserved2: 0x00000000
202 reserved3: 0x00000000
203 - sectname: __debug_info
204 segname: __DWARF
205 addr: 0x00000001000030B3
206 size: 121
207 offset: 0x000020B3
208 align: 0
209 reloff: 0x00000000
210 nreloc: 0
211 flags: 0x00000000
212 reserved1: 0x00000000
213 reserved2: 0x00000000
214 reserved3: 0x00000000
215 - sectname: __debug_abbrev
216 segname: __DWARF
217 addr: 0x000000010000312C
218 size: 76
219 offset: 0x0000212C
220 align: 0
221 reloff: 0x00000000
222 nreloc: 0
223 flags: 0x00000000
224 reserved1: 0x00000000
225 reserved2: 0x00000000
226 reserved3: 0x00000000
227 - sectname: __debug_str
228 segname: __DWARF
229 addr: 0x0000000100003178
230 size: 142
231 offset: 0x00002178
232 align: 0
233 reloff: 0x00000000
234 nreloc: 0
235 flags: 0x00000000
236 reserved1: 0x00000000
237 reserved2: 0x00000000
238 reserved3: 0x00000000
239 - sectname: __apple_names
240 segname: __DWARF
241 addr: 0x0000000100003206
242 size: 60
243 offset: 0x00002206
244 align: 0
245 reloff: 0x00000000
246 nreloc: 0
247 flags: 0x00000000
248 reserved1: 0x00000000
249 reserved2: 0x00000000
250 reserved3: 0x00000000
251 - sectname: __apple_namespac
252 segname: __DWARF
253 addr: 0x0000000100003242
254 size: 36
255 offset: 0x00002242
256 align: 0
257 reloff: 0x00000000
258 nreloc: 0
259 flags: 0x00000000
260 reserved1: 0x00000000
261 reserved2: 0x00000000
262 reserved3: 0x00000000
263 - sectname: __apple_types
264 segname: __DWARF
265 addr: 0x0000000100003266
266 size: 114
267 offset: 0x00002266
268 align: 0
269 reloff: 0x00000000
270 nreloc: 0
271 flags: 0x00000000
272 reserved1: 0x00000000
273 reserved2: 0x00000000
274 reserved3: 0x00000000
275 - sectname: __apple_objc
276 segname: __DWARF
277 addr: 0x00000001000032D8
278 size: 36
279 offset: 0x000022D8
280 align: 0
281 reloff: 0x00000000
282 nreloc: 0
283 flags: 0x00000000
284 reserved1: 0x00000000
285 reserved2: 0x00000000
286 reserved3: 0x00000000
287 LinkEditData:
288 NameList:
289 - n_strx: 2
290 n_type: 0x0F
291 n_sect: 1
292 n_desc: 16
293 n_value: 4294967296
294 - n_strx: 22
295 n_type: 0x0F
296 n_sect: 1
297 n_desc: 0
298 n_value: 4294971216
299 StringTable:
300 - ''
301 - ''
302 - __mh_execute_header
303 - _main
304 DWARF:
305 debug_str:
306 - ''
307 - 'clang version 4.0.0 (trunk 288923) (llvm/trunk 288991)'
308 - hello_world.c
309 - /Users/cbieneman/dev/open-source/llvm-build-rel
310 - main
311 - argc
312 - argv
313 - int
314 - char
315 debug_pubnames:
316 Length: 23
317 Version: 2
318 UnitOffset: 0
319 UnitSize: 121
320 Entries:
321 - DieOffset: 0x0000002A
322 Name: main
323 debug_pubtypes:
324 Length: 31
325 Version: 2
326 UnitOffset: 0
327 UnitSize: 121
328 Entries:
329 - DieOffset: 0x00000060
330 Name: int
331 - DieOffset: 0x00000071
332 Name: char
333 ...
334
335 #CHECK: DWARF:
336 #CHECK: debug_pubnames:
337 #CHECK: Length: 23
338 #CHECK: Version: 2
339 #CHECK: UnitOffset: 0
340 #CHECK: UnitSize: 121
341 #CHECK: Entries:
342 #CHECK: - DieOffset: 0x0000002A
343 #CHECK: Name: main
344 #CHECK: debug_pubtypes:
345 #CHECK: Length: 31
346 #CHECK: Version: 2
347 #CHECK: UnitOffset: 0
348 #CHECK: UnitSize: 121
349 #CHECK: Entries:
350 #CHECK: - DieOffset: 0x00000060
351 #CHECK: Name: int
352 #CHECK: - DieOffset: 0x00000071
353 #CHECK: Name: char
354 #CHECK: ...
6666 }
6767 }
6868
69 void dumpPubSection(DWARFContextInMemory &DCtx, DWARFYAML::PubSection &Y,
70 StringRef Section) {
71 DataExtractor PubSectionData(Section, DCtx.isLittleEndian(), 0);
72 uint32_t Offset = 0;
73 Y.Length = PubSectionData.getU32(&Offset);
74 Y.Version = PubSectionData.getU16(&Offset);
75 Y.UnitOffset = PubSectionData.getU32(&Offset);
76 Y.UnitSize = PubSectionData.getU32(&Offset);
77 while (Offset < Y.Length) {
78 DWARFYAML::PubEntry NewEntry;
79 NewEntry.DieOffset = PubSectionData.getU32(&Offset);
80 if (Y.IsGNUStyle)
81 NewEntry.Descriptor = PubSectionData.getU8(&Offset);
82 NewEntry.Name = PubSectionData.getCStr(&Offset);
83 Y.Entries.push_back(NewEntry);
84 }
85 }
86
87 void dumpDebugPubSections(DWARFContextInMemory &DCtx, DWARFYAML::Data &Y) {
88 Y.PubNames.IsGNUStyle = false;
89 dumpPubSection(DCtx, Y.PubNames, DCtx.getPubNamesSection());
90
91 Y.PubTypes.IsGNUStyle = false;
92 dumpPubSection(DCtx, Y.PubTypes, DCtx.getPubTypesSection());
93
94 Y.GNUPubNames.IsGNUStyle = true;
95 dumpPubSection(DCtx, Y.GNUPubNames, DCtx.getGnuPubNamesSection());
96
97 Y.GNUPubTypes.IsGNUStyle = true;
98 dumpPubSection(DCtx, Y.GNUPubTypes, DCtx.getGnuPubTypesSection());
99 }
100
69101 std::error_code dwarf2yaml(DWARFContextInMemory &DCtx,
70102 DWARFYAML::Data &Y) {
71103 dumpDebugAbbrev(DCtx, Y);
72104 dumpDebugStrings(DCtx, Y);
73105 dumpDebugARanges(DCtx, Y);
106 dumpDebugPubSections(DCtx, Y);
74107
75108 return obj2yaml_error::success;
76109 }
6565 ZeroFillBytes(OS, Range.AddrSize * 2);
6666 }
6767 }
68
69 void yaml2pubsection(raw_ostream &OS, const DWARFYAML::PubSection &Sect) {
70 OS.write(reinterpret_cast(&Sect.Length), 4);
71 OS.write(reinterpret_cast(&Sect.Version), 2);
72 OS.write(reinterpret_cast(&Sect.UnitOffset), 4);
73 OS.write(reinterpret_cast(&Sect.UnitSize), 4);
74 for (auto Entry : Sect.Entries) {
75 OS.write(reinterpret_cast(&Entry.DieOffset), 4);
76 if (Sect.IsGNUStyle)
77 OS.write(reinterpret_cast(&Entry.Descriptor), 4);
78 OS.write(Entry.Name.data(), Entry.Name.size());
79 OS.write('\0');
80 }
81 }
393393 yaml2debug_abbrev(OS, Obj.DWARF);
394394 } else if (0 == strncmp(&Section.sectname[0], "__debug_aranges", 16)) {
395395 yaml2debug_aranges(OS, Obj.DWARF);
396 } else if (0 == strncmp(&Section.sectname[0], "__debug_pubnames", 16)) {
397 yaml2pubsection(OS, Obj.DWARF.PubNames);
398 } else if (0 == strncmp(&Section.sectname[0], "__debug_pubtypes", 16)) {
399 yaml2pubsection(OS, Obj.DWARF.PubTypes);
396400 }
397401 }
398402 return Error::success();
2424
2525 namespace DWARFYAML {
2626 struct Data;
27 struct PubSection;
2728 }
2829
2930 namespace yaml {
4243 const llvm::DWARFYAML::Data &DI);
4344
4445 void yaml2debug_aranges(llvm::raw_ostream &OS, const llvm::DWARFYAML::Data &DI);
46 void yaml2pubsection(llvm::raw_ostream &OS,
47 const llvm::DWARFYAML::PubSection &Sect);
4548
4649 #endif