1 | //===- DebugLineSectionEmitter.h --------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DEBUGLINESECTIONEMITTER_H |
10 | #define LLVM_LIB_DWARFLINKER_PARALLEL_DEBUGLINESECTIONEMITTER_H |
11 | |
12 | #include "DWARFEmitterImpl.h" |
13 | #include "llvm/DWARFLinker/AddressesMap.h" |
14 | #include "llvm/DWARFLinker/Parallel/DWARFLinker.h" |
15 | #include "llvm/DebugInfo/DWARF/DWARFObject.h" |
16 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
17 | #include "llvm/MC/TargetRegistry.h" |
18 | |
19 | namespace llvm { |
20 | namespace dwarf_linker { |
21 | namespace parallel { |
22 | |
23 | /// This class emits specified line table into the .debug_line section. |
24 | class DebugLineSectionEmitter { |
25 | public: |
26 | DebugLineSectionEmitter(const Triple &TheTriple, DwarfUnit &U) |
27 | : TheTriple(TheTriple), U(U) {} |
28 | |
29 | Error emit(const DWARFDebugLine::LineTable &LineTable) { |
30 | // FIXME: remove dependence on MCDwarfLineAddr::encode. |
31 | // As we reuse MCDwarfLineAddr::encode, we need to create/initialize |
32 | // some MC* classes. |
33 | if (Error Err = init(TheTriple)) |
34 | return Err; |
35 | |
36 | // Get descriptor for output .debug_line section. |
37 | SectionDescriptor &OutSection = |
38 | U.getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLine); |
39 | |
40 | // unit_length. |
41 | OutSection.emitUnitLength(Length: 0xBADDEF); |
42 | uint64_t OffsetAfterUnitLength = OutSection.OS.tell(); |
43 | |
44 | // Emit prologue. |
45 | emitLineTablePrologue(P: LineTable.Prologue, Section&: OutSection); |
46 | |
47 | // Emit rows. |
48 | emitLineTableRows(LineTable, Section&: OutSection); |
49 | uint64_t OffsetAfterEnd = OutSection.OS.tell(); |
50 | |
51 | // Update unit length field with actual length value. |
52 | assert(OffsetAfterUnitLength - |
53 | OutSection.getFormParams().getDwarfOffsetByteSize() < |
54 | OffsetAfterUnitLength); |
55 | OutSection.apply(PatchOffset: OffsetAfterUnitLength - |
56 | OutSection.getFormParams().getDwarfOffsetByteSize(), |
57 | AttrForm: dwarf::DW_FORM_sec_offset, |
58 | Val: OffsetAfterEnd - OffsetAfterUnitLength); |
59 | |
60 | return Error::success(); |
61 | } |
62 | |
63 | private: |
64 | Error init(Triple TheTriple) { |
65 | std::string ErrorStr; |
66 | std::string TripleName; |
67 | |
68 | // Get the target. |
69 | const Target *TheTarget = |
70 | TargetRegistry::lookupTarget(ArchName: TripleName, TheTriple, Error&: ErrorStr); |
71 | if (!TheTarget) |
72 | return createStringError(EC: std::errc::invalid_argument, Fmt: ErrorStr.c_str()); |
73 | TripleName = TheTriple.getTriple(); |
74 | |
75 | // Create all the MC Objects. |
76 | MRI.reset(p: TheTarget->createMCRegInfo(TT: TripleName)); |
77 | if (!MRI) |
78 | return createStringError(EC: std::errc::invalid_argument, |
79 | Fmt: "no register info for target %s" , |
80 | Vals: TripleName.c_str()); |
81 | |
82 | MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags(); |
83 | MAI.reset(p: TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple: TripleName, Options: MCOptions)); |
84 | if (!MAI) |
85 | return createStringError(EC: std::errc::invalid_argument, |
86 | Fmt: "no asm info for target %s" , Vals: TripleName.c_str()); |
87 | |
88 | MSTI.reset(p: TheTarget->createMCSubtargetInfo(TheTriple: TripleName, CPU: "" , Features: "" )); |
89 | if (!MSTI) |
90 | return createStringError(EC: std::errc::invalid_argument, |
91 | Fmt: "no subtarget info for target %s" , |
92 | Vals: TripleName.c_str()); |
93 | |
94 | MC.reset(p: new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(), nullptr, |
95 | nullptr, true, "__DWARF" )); |
96 | |
97 | return Error::success(); |
98 | } |
99 | |
100 | void emitLineTablePrologue(const DWARFDebugLine::Prologue &P, |
101 | SectionDescriptor &Section) { |
102 | // version (uhalf). |
103 | Section.emitIntVal(Val: P.getVersion(), Size: 2); |
104 | if (P.getVersion() == 5) { |
105 | // address_size (ubyte). |
106 | Section.emitIntVal(Val: P.getAddressSize(), Size: 1); |
107 | |
108 | // segment_selector_size (ubyte). |
109 | Section.emitIntVal(Val: P.SegSelectorSize, Size: 1); |
110 | } |
111 | |
112 | // header_length. |
113 | Section.emitOffset(Val: 0xBADDEF); |
114 | |
115 | uint64_t OffsetAfterPrologueLength = Section.OS.tell(); |
116 | emitLineTableProloguePayload(P, Section); |
117 | uint64_t OffsetAfterPrologueEnd = Section.OS.tell(); |
118 | |
119 | // Update prologue length field with actual length value. |
120 | Section.apply(PatchOffset: OffsetAfterPrologueLength - |
121 | Section.getFormParams().getDwarfOffsetByteSize(), |
122 | AttrForm: dwarf::DW_FORM_sec_offset, |
123 | Val: OffsetAfterPrologueEnd - OffsetAfterPrologueLength); |
124 | } |
125 | |
126 | void |
127 | emitLineTablePrologueV2IncludeAndFileTable(const DWARFDebugLine::Prologue &P, |
128 | SectionDescriptor &Section) { |
129 | // include_directories (sequence of path names). |
130 | for (const DWARFFormValue &Include : P.IncludeDirectories) { |
131 | std::optional<const char *> IncludeStr = dwarf::toString(V: Include); |
132 | if (!IncludeStr) { |
133 | U.warn(Warning: "cann't read string from line table." ); |
134 | return; |
135 | } |
136 | |
137 | Section.emitString(StringForm: Include.getForm(), StringVal: *IncludeStr); |
138 | } |
139 | // The last entry is followed by a single null byte. |
140 | Section.emitIntVal(Val: 0, Size: 1); |
141 | |
142 | // file_names (sequence of file entries). |
143 | for (const DWARFDebugLine::FileNameEntry &File : P.FileNames) { |
144 | std::optional<const char *> FileNameStr = dwarf::toString(V: File.Name); |
145 | if (!FileNameStr) { |
146 | U.warn(Warning: "cann't read string from line table." ); |
147 | return; |
148 | } |
149 | |
150 | // A null-terminated string containing the full or relative path name of a |
151 | // source file. |
152 | Section.emitString(StringForm: File.Name.getForm(), StringVal: *FileNameStr); |
153 | |
154 | // An unsigned LEB128 number representing the directory index of a |
155 | // directory in the include_directories section. |
156 | encodeULEB128(Value: File.DirIdx, OS&: Section.OS); |
157 | // An unsigned LEB128 number representing the (implementation-defined) |
158 | // time of last modification for the file, or 0 if not available. |
159 | encodeULEB128(Value: File.ModTime, OS&: Section.OS); |
160 | // An unsigned LEB128 number representing the length in bytes of the file, |
161 | // or 0 if not available. |
162 | encodeULEB128(Value: File.Length, OS&: Section.OS); |
163 | } |
164 | // The last entry is followed by a single null byte. |
165 | Section.emitIntVal(Val: 0, Size: 1); |
166 | } |
167 | |
168 | void |
169 | emitLineTablePrologueV5IncludeAndFileTable(const DWARFDebugLine::Prologue &P, |
170 | SectionDescriptor &Section) { |
171 | if (P.IncludeDirectories.empty()) { |
172 | // directory_entry_format_count(ubyte). |
173 | Section.emitIntVal(Val: 0, Size: 1); |
174 | } else { |
175 | // directory_entry_format_count(ubyte). |
176 | Section.emitIntVal(Val: 1, Size: 1); |
177 | |
178 | // directory_entry_format (sequence of ULEB128 pairs). |
179 | encodeULEB128(Value: dwarf::DW_LNCT_path, OS&: Section.OS); |
180 | encodeULEB128(Value: P.IncludeDirectories[0].getForm(), OS&: Section.OS); |
181 | } |
182 | |
183 | // directories_count (ULEB128). |
184 | encodeULEB128(Value: P.IncludeDirectories.size(), OS&: Section.OS); |
185 | // directories (sequence of directory names). |
186 | for (auto Include : P.IncludeDirectories) { |
187 | std::optional<const char *> IncludeStr = dwarf::toString(V: Include); |
188 | if (!IncludeStr) { |
189 | U.warn(Warning: "cann't read string from line table." ); |
190 | return; |
191 | } |
192 | |
193 | Section.emitString(StringForm: Include.getForm(), StringVal: *IncludeStr); |
194 | } |
195 | |
196 | bool HasChecksums = P.ContentTypes.HasMD5; |
197 | bool HasInlineSources = P.ContentTypes.HasSource; |
198 | |
199 | dwarf::Form FileNameForm = dwarf::DW_FORM_string; |
200 | dwarf::Form LLVMSourceForm = dwarf::DW_FORM_string; |
201 | |
202 | if (P.FileNames.empty()) { |
203 | // file_name_entry_format_count (ubyte). |
204 | Section.emitIntVal(Val: 0, Size: 1); |
205 | } else { |
206 | FileNameForm = P.FileNames[0].Name.getForm(); |
207 | LLVMSourceForm = P.FileNames[0].Source.getForm(); |
208 | |
209 | // file_name_entry_format_count (ubyte). |
210 | Section.emitIntVal( |
211 | Val: 2 + (HasChecksums ? 1 : 0) + (HasInlineSources ? 1 : 0), Size: 1); |
212 | |
213 | // file_name_entry_format (sequence of ULEB128 pairs). |
214 | encodeULEB128(Value: dwarf::DW_LNCT_path, OS&: Section.OS); |
215 | encodeULEB128(Value: FileNameForm, OS&: Section.OS); |
216 | |
217 | encodeULEB128(Value: dwarf::DW_LNCT_directory_index, OS&: Section.OS); |
218 | encodeULEB128(Value: dwarf::DW_FORM_data1, OS&: Section.OS); |
219 | |
220 | if (HasChecksums) { |
221 | encodeULEB128(Value: dwarf::DW_LNCT_MD5, OS&: Section.OS); |
222 | encodeULEB128(Value: dwarf::DW_FORM_data16, OS&: Section.OS); |
223 | } |
224 | |
225 | if (HasInlineSources) { |
226 | encodeULEB128(Value: dwarf::DW_LNCT_LLVM_source, OS&: Section.OS); |
227 | encodeULEB128(Value: LLVMSourceForm, OS&: Section.OS); |
228 | } |
229 | } |
230 | |
231 | // file_names_count (ULEB128). |
232 | encodeULEB128(Value: P.FileNames.size(), OS&: Section.OS); |
233 | |
234 | // file_names (sequence of file name entries). |
235 | for (auto File : P.FileNames) { |
236 | std::optional<const char *> FileNameStr = dwarf::toString(V: File.Name); |
237 | if (!FileNameStr) { |
238 | U.warn(Warning: "cann't read string from line table." ); |
239 | return; |
240 | } |
241 | |
242 | // A null-terminated string containing the full or relative path name of a |
243 | // source file. |
244 | Section.emitString(StringForm: FileNameForm, StringVal: *FileNameStr); |
245 | Section.emitIntVal(Val: File.DirIdx, Size: 1); |
246 | |
247 | if (HasChecksums) { |
248 | assert((File.Checksum.size() == 16) && |
249 | "checksum size is not equal to 16 bytes." ); |
250 | Section.emitBinaryData( |
251 | Data: StringRef(reinterpret_cast<const char *>(File.Checksum.data()), |
252 | File.Checksum.size())); |
253 | } |
254 | |
255 | if (HasInlineSources) { |
256 | std::optional<const char *> FileSourceStr = |
257 | dwarf::toString(V: File.Source); |
258 | if (!FileSourceStr) { |
259 | U.warn(Warning: "cann't read string from line table." ); |
260 | return; |
261 | } |
262 | |
263 | Section.emitString(StringForm: LLVMSourceForm, StringVal: *FileSourceStr); |
264 | } |
265 | } |
266 | } |
267 | |
268 | void emitLineTableProloguePayload(const DWARFDebugLine::Prologue &P, |
269 | SectionDescriptor &Section) { |
270 | // minimum_instruction_length (ubyte). |
271 | Section.emitIntVal(Val: P.MinInstLength, Size: 1); |
272 | if (P.FormParams.Version >= 4) { |
273 | // maximum_operations_per_instruction (ubyte). |
274 | Section.emitIntVal(Val: P.MaxOpsPerInst, Size: 1); |
275 | } |
276 | // default_is_stmt (ubyte). |
277 | Section.emitIntVal(Val: P.DefaultIsStmt, Size: 1); |
278 | // line_base (sbyte). |
279 | Section.emitIntVal(Val: P.LineBase, Size: 1); |
280 | // line_range (ubyte). |
281 | Section.emitIntVal(Val: P.LineRange, Size: 1); |
282 | // opcode_base (ubyte). |
283 | Section.emitIntVal(Val: P.OpcodeBase, Size: 1); |
284 | |
285 | // standard_opcode_lengths (array of ubyte). |
286 | for (auto Length : P.StandardOpcodeLengths) |
287 | Section.emitIntVal(Val: Length, Size: 1); |
288 | |
289 | if (P.FormParams.Version < 5) |
290 | emitLineTablePrologueV2IncludeAndFileTable(P, Section); |
291 | else |
292 | emitLineTablePrologueV5IncludeAndFileTable(P, Section); |
293 | } |
294 | |
295 | void emitLineTableRows(const DWARFDebugLine::LineTable &LineTable, |
296 | SectionDescriptor &Section) { |
297 | |
298 | MCDwarfLineTableParams Params; |
299 | Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase; |
300 | Params.DWARF2LineBase = LineTable.Prologue.LineBase; |
301 | Params.DWARF2LineRange = LineTable.Prologue.LineRange; |
302 | |
303 | SmallString<128> EncodingBuffer; |
304 | |
305 | if (LineTable.Rows.empty()) { |
306 | // We only have the dummy entry, dsymutil emits an entry with a 0 |
307 | // address in that case. |
308 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta: std::numeric_limits<int64_t>::max(), |
309 | AddrDelta: 0, OS&: EncodingBuffer); |
310 | Section.OS.write(Ptr: EncodingBuffer.c_str(), Size: EncodingBuffer.size()); |
311 | return; |
312 | } |
313 | |
314 | // Line table state machine fields |
315 | unsigned FileNum = 1; |
316 | unsigned LastLine = 1; |
317 | unsigned Column = 0; |
318 | unsigned Discriminator = 0; |
319 | unsigned IsStatement = 1; |
320 | unsigned Isa = 0; |
321 | uint64_t Address = -1ULL; |
322 | |
323 | unsigned RowsSinceLastSequence = 0; |
324 | |
325 | for (const DWARFDebugLine::Row &Row : LineTable.Rows) { |
326 | int64_t AddressDelta; |
327 | if (Address == -1ULL) { |
328 | Section.emitIntVal(Val: dwarf::DW_LNS_extended_op, Size: 1); |
329 | encodeULEB128(Value: Section.getFormParams().AddrSize + 1, OS&: Section.OS); |
330 | Section.emitIntVal(Val: dwarf::DW_LNE_set_address, Size: 1); |
331 | Section.emitIntVal(Val: Row.Address.Address, |
332 | Size: Section.getFormParams().AddrSize); |
333 | AddressDelta = 0; |
334 | } else { |
335 | AddressDelta = |
336 | (Row.Address.Address - Address) / LineTable.Prologue.MinInstLength; |
337 | } |
338 | |
339 | // FIXME: code copied and transformed from |
340 | // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share this |
341 | // code, but the current compatibility requirement with classic dsymutil |
342 | // makes it hard. Revisit that once this requirement is dropped. |
343 | |
344 | if (FileNum != Row.File) { |
345 | FileNum = Row.File; |
346 | Section.emitIntVal(Val: dwarf::DW_LNS_set_file, Size: 1); |
347 | encodeULEB128(Value: FileNum, OS&: Section.OS); |
348 | } |
349 | if (Column != Row.Column) { |
350 | Column = Row.Column; |
351 | Section.emitIntVal(Val: dwarf::DW_LNS_set_column, Size: 1); |
352 | encodeULEB128(Value: Column, OS&: Section.OS); |
353 | } |
354 | if (Discriminator != Row.Discriminator && MC->getDwarfVersion() >= 4) { |
355 | Discriminator = Row.Discriminator; |
356 | unsigned Size = getULEB128Size(Value: Discriminator); |
357 | Section.emitIntVal(Val: dwarf::DW_LNS_extended_op, Size: 1); |
358 | encodeULEB128(Value: Size + 1, OS&: Section.OS); |
359 | Section.emitIntVal(Val: dwarf::DW_LNE_set_discriminator, Size: 1); |
360 | encodeULEB128(Value: Discriminator, OS&: Section.OS); |
361 | } |
362 | Discriminator = 0; |
363 | |
364 | if (Isa != Row.Isa) { |
365 | Isa = Row.Isa; |
366 | Section.emitIntVal(Val: dwarf::DW_LNS_set_isa, Size: 1); |
367 | encodeULEB128(Value: Isa, OS&: Section.OS); |
368 | } |
369 | if (IsStatement != Row.IsStmt) { |
370 | IsStatement = Row.IsStmt; |
371 | Section.emitIntVal(Val: dwarf::DW_LNS_negate_stmt, Size: 1); |
372 | } |
373 | if (Row.BasicBlock) |
374 | Section.emitIntVal(Val: dwarf::DW_LNS_set_basic_block, Size: 1); |
375 | |
376 | if (Row.PrologueEnd) |
377 | Section.emitIntVal(Val: dwarf::DW_LNS_set_prologue_end, Size: 1); |
378 | |
379 | if (Row.EpilogueBegin) |
380 | Section.emitIntVal(Val: dwarf::DW_LNS_set_epilogue_begin, Size: 1); |
381 | |
382 | int64_t LineDelta = int64_t(Row.Line) - LastLine; |
383 | if (!Row.EndSequence) { |
384 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta, AddrDelta: AddressDelta, |
385 | OS&: EncodingBuffer); |
386 | Section.OS.write(Ptr: EncodingBuffer.c_str(), Size: EncodingBuffer.size()); |
387 | EncodingBuffer.resize(N: 0); |
388 | Address = Row.Address.Address; |
389 | LastLine = Row.Line; |
390 | RowsSinceLastSequence++; |
391 | } else { |
392 | if (LineDelta) { |
393 | Section.emitIntVal(Val: dwarf::DW_LNS_advance_line, Size: 1); |
394 | encodeSLEB128(Value: LineDelta, OS&: Section.OS); |
395 | } |
396 | if (AddressDelta) { |
397 | Section.emitIntVal(Val: dwarf::DW_LNS_advance_pc, Size: 1); |
398 | encodeULEB128(Value: AddressDelta, OS&: Section.OS); |
399 | } |
400 | MCDwarfLineAddr::encode(Context&: *MC, Params, |
401 | LineDelta: std::numeric_limits<int64_t>::max(), AddrDelta: 0, |
402 | OS&: EncodingBuffer); |
403 | Section.OS.write(Ptr: EncodingBuffer.c_str(), Size: EncodingBuffer.size()); |
404 | EncodingBuffer.resize(N: 0); |
405 | Address = -1ULL; |
406 | LastLine = FileNum = IsStatement = 1; |
407 | RowsSinceLastSequence = Column = Discriminator = Isa = 0; |
408 | } |
409 | } |
410 | |
411 | if (RowsSinceLastSequence) { |
412 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta: std::numeric_limits<int64_t>::max(), |
413 | AddrDelta: 0, OS&: EncodingBuffer); |
414 | Section.OS.write(Ptr: EncodingBuffer.c_str(), Size: EncodingBuffer.size()); |
415 | EncodingBuffer.resize(N: 0); |
416 | } |
417 | } |
418 | |
419 | Triple TheTriple; |
420 | DwarfUnit &U; |
421 | |
422 | std::unique_ptr<MCRegisterInfo> MRI; |
423 | std::unique_ptr<MCAsmInfo> MAI; |
424 | std::unique_ptr<MCContext> MC; |
425 | std::unique_ptr<MCSubtargetInfo> MSTI; |
426 | }; |
427 | |
428 | } // end of namespace parallel |
429 | } // end of namespace dwarf_linker |
430 | } // end of namespace llvm |
431 | |
432 | #endif // LLVM_LIB_DWARFLINKER_PARALLEL_DEBUGLINESECTIONEMITTER_H |
433 | |