llvm.org GIT mirror llvm / 4a6766e
[ObjectYAML] First bit of support for encoding DWARF in MachO This patch adds the starting support for encoding data from the MachO __DWARF segment. The first section supported is the __debug_str section because it is the simplest. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288774 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 2 years ago
6 changed file(s) with 334 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
103103 bool isEmpty() const;
104104 };
105105
106 struct DWARFData {
107 std::vector DebugStrings;
108
109 bool isEmpty() const;
110 };
111
106112 struct Object {
107113 FileHeader Header;
108114 std::vector LoadCommands;
109115 std::vector
Sections;
110116 LinkEditData LinkEdit;
117 DWARFData DWARF;
111118 };
112119
113120 struct FatHeader {
195202
196203 template <> struct MappingTraits {
197204 static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
205 };
206
207 template <> struct MappingTraits {
208 static void mapping(IO &IO, MachOYAML::DWARFData &DWARF);
198209 };
199210
200211 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
2222 MachOYAML::LoadCommand::~LoadCommand() {}
2323
2424 bool MachOYAML::LinkEditData::isEmpty() const {
25 return 0 == RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() + LazyBindOpcodes.size() + ExportTrie.Children.size() + NameList.size() + StringTable.size();
25 return 0 ==
26 RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() +
27 LazyBindOpcodes.size() + ExportTrie.Children.size() +
28 NameList.size() + StringTable.size();
29 }
30
31 bool MachOYAML::DWARFData::isEmpty() const {
32 return 0 == DebugStrings.size();
2633 }
2734
2835 namespace yaml {
100107 IO.mapOptional("LoadCommands", Object.LoadCommands);
101108 if(!Object.LinkEdit.isEmpty() || !IO.outputting())
102109 IO.mapOptional("LinkEditData", Object.LinkEdit);
110
111 if(!Object.DWARF.isEmpty() || !IO.outputting())
112 IO.mapOptional("DWARF", Object.DWARF);
103113
104114 if (IO.getContext() == &Object)
105115 IO.setContext(nullptr);
546556 IO.mapRequired("sdk", LoadCommand.sdk);
547557 }
548558
559 void MappingTraits::mapping(
560 IO &IO, MachOYAML::DWARFData &DWARF) {
561 IO.mapRequired("DebugStrings", DWARF.DebugStrings);
562 }
563
549564 } // namespace llvm::yaml
550565
551566 } // 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: 6
9 sizeofcmds: 1376
10 flags: 0x00000000
11 reserved: 0x00000000
12 LoadCommands:
13 - cmd: LC_UUID
14 cmdsize: 24
15 uuid: 9304404B-E522-3BBA-A861-AF5938908725
16 - cmd: LC_SYMTAB
17 cmdsize: 24
18 symoff: 4096
19 nsyms: 2
20 stroff: 4128
21 strsize: 28
22 - cmd: LC_SEGMENT_64
23 cmdsize: 72
24 segname: __PAGEZERO
25 vmaddr: 0
26 vmsize: 4294967296
27 fileoff: 0
28 filesize: 0
29 maxprot: 0
30 initprot: 0
31 nsects: 0
32 flags: 0
33 - cmd: LC_SEGMENT_64
34 cmdsize: 232
35 segname: __TEXT
36 vmaddr: 4294967296
37 vmsize: 4096
38 fileoff: 0
39 filesize: 0
40 maxprot: 7
41 initprot: 5
42 nsects: 2
43 flags: 0
44 Sections:
45 - sectname: __text
46 segname: __TEXT
47 addr: 0x0000000100000FA0
48 size: 22
49 offset: 0x00000000
50 align: 4
51 reloff: 0x00000000
52 nreloc: 0
53 flags: 0x80000400
54 reserved1: 0x00000000
55 reserved2: 0x00000000
56 reserved3: 0x00000000
57 - sectname: __unwind_info
58 segname: __TEXT
59 addr: 0x0000000100000FB8
60 size: 72
61 offset: 0x00000000
62 align: 2
63 reloff: 0x00000000
64 nreloc: 0
65 flags: 0x00000000
66 reserved1: 0x00000000
67 reserved2: 0x00000000
68 reserved3: 0x00000000
69 - cmd: LC_SEGMENT_64
70 cmdsize: 72
71 segname: __LINKEDIT
72 vmaddr: 4294971392
73 vmsize: 4096
74 fileoff: 4096
75 filesize: 60
76 maxprot: 7
77 initprot: 1
78 nsects: 0
79 flags: 0
80 - cmd: LC_SEGMENT_64
81 cmdsize: 952
82 segname: __DWARF
83 vmaddr: 4294975488
84 vmsize: 4096
85 fileoff: 8192
86 filesize: 765
87 maxprot: 7
88 initprot: 3
89 nsects: 11
90 flags: 0
91 Sections:
92 - sectname: __debug_line
93 segname: __DWARF
94 addr: 0x0000000100002000
95 size: 70
96 offset: 0x00002000
97 align: 0
98 reloff: 0x00000000
99 nreloc: 0
100 flags: 0x00000000
101 reserved1: 0x00000000
102 reserved2: 0x00000000
103 reserved3: 0x00000000
104 - sectname: __debug_pubnames
105 segname: __DWARF
106 addr: 0x0000000100002046
107 size: 27
108 offset: 0x00002046
109 align: 0
110 reloff: 0x00000000
111 nreloc: 0
112 flags: 0x00000000
113 reserved1: 0x00000000
114 reserved2: 0x00000000
115 reserved3: 0x00000000
116 - sectname: __debug_pubtypes
117 segname: __DWARF
118 addr: 0x0000000100002061
119 size: 35
120 offset: 0x00002061
121 align: 0
122 reloff: 0x00000000
123 nreloc: 0
124 flags: 0x00000000
125 reserved1: 0x00000000
126 reserved2: 0x00000000
127 reserved3: 0x00000000
128 - sectname: __debug_aranges
129 segname: __DWARF
130 addr: 0x0000000100002084
131 size: 48
132 offset: 0x00002084
133 align: 0
134 reloff: 0x00000000
135 nreloc: 0
136 flags: 0x00000000
137 reserved1: 0x00000000
138 reserved2: 0x00000000
139 reserved3: 0x00000000
140 - sectname: __debug_info
141 segname: __DWARF
142 addr: 0x00000001000020B4
143 size: 121
144 offset: 0x000020B4
145 align: 0
146 reloff: 0x00000000
147 nreloc: 0
148 flags: 0x00000000
149 reserved1: 0x00000000
150 reserved2: 0x00000000
151 reserved3: 0x00000000
152 - sectname: __debug_abbrev
153 segname: __DWARF
154 addr: 0x000000010000212D
155 size: 76
156 offset: 0x0000212D
157 align: 0
158 reloff: 0x00000000
159 nreloc: 0
160 flags: 0x00000000
161 reserved1: 0x00000000
162 reserved2: 0x00000000
163 reserved3: 0x00000000
164 - sectname: __debug_str
165 segname: __DWARF
166 addr: 0x0000000100002179
167 size: 142
168 offset: 0x00002179
169 align: 0
170 reloff: 0x00000000
171 nreloc: 0
172 flags: 0x00000000
173 reserved1: 0x00000000
174 reserved2: 0x00000000
175 reserved3: 0x00000000
176 - sectname: __apple_names
177 segname: __DWARF
178 addr: 0x0000000100002207
179 size: 60
180 offset: 0x00002207
181 align: 0
182 reloff: 0x00000000
183 nreloc: 0
184 flags: 0x00000000
185 reserved1: 0x00000000
186 reserved2: 0x00000000
187 reserved3: 0x00000000
188 - sectname: __apple_namespac
189 segname: __DWARF
190 addr: 0x0000000100002243
191 size: 36
192 offset: 0x00002243
193 align: 0
194 reloff: 0x00000000
195 nreloc: 0
196 flags: 0x00000000
197 reserved1: 0x00000000
198 reserved2: 0x00000000
199 reserved3: 0x00000000
200 - sectname: __apple_types
201 segname: __DWARF
202 addr: 0x0000000100002267
203 size: 114
204 offset: 0x00002267
205 align: 0
206 reloff: 0x00000000
207 nreloc: 0
208 flags: 0x00000000
209 reserved1: 0x00000000
210 reserved2: 0x00000000
211 reserved3: 0x00000000
212 - sectname: __apple_objc
213 segname: __DWARF
214 addr: 0x00000001000022D9
215 size: 36
216 offset: 0x000022D9
217 align: 0
218 reloff: 0x00000000
219 nreloc: 0
220 flags: 0x00000000
221 reserved1: 0x00000000
222 reserved2: 0x00000000
223 reserved3: 0x00000000
224 LinkEditData:
225 NameList:
226 - n_strx: 2
227 n_type: 0x0F
228 n_sect: 1
229 n_desc: 16
230 n_value: 4294967296
231 - n_strx: 22
232 n_type: 0x0F
233 n_sect: 1
234 n_desc: 0
235 n_value: 4294971296
236 StringTable:
237 - ''
238 - ''
239 - __mh_execute_header
240 - _main
241 DWARF:
242 DebugStrings:
243 - ''
244 - 'clang version 4.0.0 (trunk 288677) (llvm/trunk 288676)'
245 - hello_world.c
246 - /Users/cbieneman/dev/open-source/llvm-build-rel
247 - main
248 - argc
249 - argv
250 - int
251 - char
252 ...
253
254 #CHECK: DWARF:
255 #CHECK: DebugStrings:
256 #CHECK: - ''
257 #CHECK: - 'clang version 4.0.0 (trunk 288677) (llvm/trunk 288676)'
258 #CHECK: - hello_world.c
259 #CHECK: - /Users/cbieneman/dev/open-source/llvm-build-rel
260 #CHECK: - main
261 #CHECK: - argc
262 #CHECK: - argv
263 #CHECK: - int
264 #CHECK: - char
265
0 set(LLVM_LINK_COMPONENTS
1 DebugInfoDWARF
12 Object
23 ObjectYAML
34 Support
88
99 #include "Error.h"
1010 #include "obj2yaml.h"
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1112 #include "llvm/Object/MachOUniversal.h"
1213 #include "llvm/ObjectYAML/ObjectYAML.h"
1314 #include "llvm/Support/ErrorHandling.h"
3334 ArrayRef OpcodeBuffer, bool Lazy = false);
3435 void dumpExportTrie(std::unique_ptr &Y);
3536 void dumpSymbols(std::unique_ptr &Y);
37 void dumpDWARF(std::unique_ptr &Y);
38 void dumpDebugStrings(DWARFContextInMemory &DCtx, std::unique_ptr &Y);
3639
3740 public:
3841 MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
162165 dumpHeader(Y);
163166 dumpLoadCommands(Y);
164167 dumpLinkEdit(Y);
168 dumpDWARF(Y);
165169 return std::move(Y);
166170 }
167171
458462 }
459463 }
460464
465 void MachODumper::dumpDWARF(std::unique_ptr &Y) {
466 DWARFContextInMemory DICtx(Obj);
467 dumpDebugStrings(DICtx, Y);
468 }
469
470 void MachODumper::dumpDebugStrings(DWARFContextInMemory &DICtx,
471 std::unique_ptr &Y) {
472 StringRef RemainingTable = DICtx.getStringSection();
473 while (RemainingTable.size() > 0) {
474 auto SymbolPair = RemainingTable.split('\0');
475 RemainingTable = SymbolPair.second;
476 Y->DWARF.DebugStrings.push_back(SymbolPair.first);
477 }
478 }
479
461480 Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
462481 MachODumper Dumper(Obj);
463482 Expected> YAML = Dumper.dump();
4040 Error writeLoadCommands(raw_ostream &OS);
4141 Error writeSectionData(raw_ostream &OS);
4242 Error writeLinkEditData(raw_ostream &OS);
43 Error writeDWARFData(raw_ostream &OS,
44 std::vector &Sections);
4345 void writeBindOpcodes(raw_ostream &OS,
4446 std::vector &BindOpcodes);
4547 // LinkEdit writers
239241 if (0 == strncmp(&segname[0], "__LINKEDIT", 16)) {
240242 if (auto Err = writeLinkEditData(OS))
241243 return Err;
244 } else if (0 == strncmp(&segname[0], "__DWARF", 16)) {
245 if (auto Err = writeDWARFData(OS, LC.Sections))
246 return Err;
242247 } else {
243248 // Zero Fill any data between the end of the last thing we wrote and the
244249 // start of this section.
251256 // the
252257 // start of this section.
253258 assert(
254 OS.tell() - fileStart <= Sec.offset &&
259 (OS.tell() - fileStart <= Sec.offset ||
260 Sec.offset == (uint32_t)0) &&
255261 "Wrote too much data somewhere, section offsets don't line up.");
256262 currOffset = OS.tell() - fileStart;
257263 if (currOffset < Sec.offset) {
377383 return Error::success();
378384 }
379385
386 Error MachOWriter::writeDWARFData(raw_ostream &OS,
387 std::vector &Sections) {
388 for(auto Section : Sections) {
389 ZeroToOffset(OS, Section.offset);
390 if (0 == strncmp(&Section.sectname[0], "__debug_str", 16)) {
391 for (auto Str : Obj.DWARF.DebugStrings) {
392 OS.write(Str.data(), Str.size());
393 OS.write('\0');
394 }
395 }
396 }
397 return Error::success();
398 }
399
380400 Error MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
381401 MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
382402