MCDwarf.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
  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. #include "llvm/MC/MCDwarf.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/Hashing.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/BinaryFormat/Dwarf.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/MC/MCAsmInfo.h"
  20. #include "llvm/MC/MCContext.h"
  21. #include "llvm/MC/MCExpr.h"
  22. #include "llvm/MC/MCObjectFileInfo.h"
  23. #include "llvm/MC/MCObjectStreamer.h"
  24. #include "llvm/MC/MCRegisterInfo.h"
  25. #include "llvm/MC/MCSection.h"
  26. #include "llvm/MC/MCStreamer.h"
  27. #include "llvm/MC/MCSymbol.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/Endian.h"
  30. #include "llvm/Support/EndianStream.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/LEB128.h"
  33. #include "llvm/Support/MathExtras.h"
  34. #include "llvm/Support/Path.h"
  35. #include "llvm/Support/SourceMgr.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <cassert>
  38. #include <cstdint>
  39. #include <optional>
  40. #include <string>
  41. #include <utility>
  42. #include <vector>
  43. using namespace llvm;
  44. MCSymbol *mcdwarf::emitListsTableHeaderStart(MCStreamer &S) {
  45. MCSymbol *Start = S.getContext().createTempSymbol("debug_list_header_start");
  46. MCSymbol *End = S.getContext().createTempSymbol("debug_list_header_end");
  47. auto DwarfFormat = S.getContext().getDwarfFormat();
  48. if (DwarfFormat == dwarf::DWARF64) {
  49. S.AddComment("DWARF64 mark");
  50. S.emitInt32(dwarf::DW_LENGTH_DWARF64);
  51. }
  52. S.AddComment("Length");
  53. S.emitAbsoluteSymbolDiff(End, Start,
  54. dwarf::getDwarfOffsetByteSize(DwarfFormat));
  55. S.emitLabel(Start);
  56. S.AddComment("Version");
  57. S.emitInt16(S.getContext().getDwarfVersion());
  58. S.AddComment("Address size");
  59. S.emitInt8(S.getContext().getAsmInfo()->getCodePointerSize());
  60. S.AddComment("Segment selector size");
  61. S.emitInt8(0);
  62. return End;
  63. }
  64. static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
  65. unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
  66. if (MinInsnLength == 1)
  67. return AddrDelta;
  68. if (AddrDelta % MinInsnLength != 0) {
  69. // TODO: report this error, but really only once.
  70. ;
  71. }
  72. return AddrDelta / MinInsnLength;
  73. }
  74. MCDwarfLineStr::MCDwarfLineStr(MCContext &Ctx) {
  75. UseRelocs = Ctx.getAsmInfo()->doesDwarfUseRelocationsAcrossSections();
  76. if (UseRelocs)
  77. LineStrLabel =
  78. Ctx.getObjectFileInfo()->getDwarfLineStrSection()->getBeginSymbol();
  79. }
  80. //
  81. // This is called when an instruction is assembled into the specified section
  82. // and if there is information from the last .loc directive that has yet to have
  83. // a line entry made for it is made.
  84. //
  85. void MCDwarfLineEntry::make(MCStreamer *MCOS, MCSection *Section) {
  86. if (!MCOS->getContext().getDwarfLocSeen())
  87. return;
  88. // Create a symbol at in the current section for use in the line entry.
  89. MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
  90. // Set the value of the symbol to use for the MCDwarfLineEntry.
  91. MCOS->emitLabel(LineSym);
  92. // Get the current .loc info saved in the context.
  93. const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
  94. // Create a (local) line entry with the symbol and the current .loc info.
  95. MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
  96. // clear DwarfLocSeen saying the current .loc info is now used.
  97. MCOS->getContext().clearDwarfLocSeen();
  98. // Add the line entry to this section's entries.
  99. MCOS->getContext()
  100. .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
  101. .getMCLineSections()
  102. .addLineEntry(LineEntry, Section);
  103. }
  104. //
  105. // This helper routine returns an expression of End - Start - IntVal .
  106. //
  107. static inline const MCExpr *makeEndMinusStartExpr(MCContext &Ctx,
  108. const MCSymbol &Start,
  109. const MCSymbol &End,
  110. int IntVal) {
  111. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  112. const MCExpr *Res = MCSymbolRefExpr::create(&End, Variant, Ctx);
  113. const MCExpr *RHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
  114. const MCExpr *Res1 = MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, Ctx);
  115. const MCExpr *Res2 = MCConstantExpr::create(IntVal, Ctx);
  116. const MCExpr *Res3 = MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, Ctx);
  117. return Res3;
  118. }
  119. //
  120. // This helper routine returns an expression of Start + IntVal .
  121. //
  122. static inline const MCExpr *
  123. makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal) {
  124. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  125. const MCExpr *LHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
  126. const MCExpr *RHS = MCConstantExpr::create(IntVal, Ctx);
  127. const MCExpr *Res = MCBinaryExpr::create(MCBinaryExpr::Add, LHS, RHS, Ctx);
  128. return Res;
  129. }
  130. void MCLineSection::addEndEntry(MCSymbol *EndLabel) {
  131. auto *Sec = &EndLabel->getSection();
  132. // The line table may be empty, which we should skip adding an end entry.
  133. // There are two cases:
  134. // (1) MCAsmStreamer - emitDwarfLocDirective emits a location directive in
  135. // place instead of adding a line entry if the target has
  136. // usesDwarfFileAndLocDirectives.
  137. // (2) MCObjectStreamer - if a function has incomplete debug info where
  138. // instructions don't have DILocations, the line entries are missing.
  139. auto I = MCLineDivisions.find(Sec);
  140. if (I != MCLineDivisions.end()) {
  141. auto &Entries = I->second;
  142. auto EndEntry = Entries.back();
  143. EndEntry.setEndLabel(EndLabel);
  144. Entries.push_back(EndEntry);
  145. }
  146. }
  147. //
  148. // This emits the Dwarf line table for the specified section from the entries
  149. // in the LineSection.
  150. //
  151. void MCDwarfLineTable::emitOne(
  152. MCStreamer *MCOS, MCSection *Section,
  153. const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
  154. unsigned FileNum, LastLine, Column, Flags, Isa, Discriminator;
  155. MCSymbol *LastLabel;
  156. auto init = [&]() {
  157. FileNum = 1;
  158. LastLine = 1;
  159. Column = 0;
  160. Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
  161. Isa = 0;
  162. Discriminator = 0;
  163. LastLabel = nullptr;
  164. };
  165. init();
  166. // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
  167. bool EndEntryEmitted = false;
  168. for (const MCDwarfLineEntry &LineEntry : LineEntries) {
  169. MCSymbol *Label = LineEntry.getLabel();
  170. const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
  171. if (LineEntry.IsEndEntry) {
  172. MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, Label,
  173. asmInfo->getCodePointerSize());
  174. init();
  175. EndEntryEmitted = true;
  176. continue;
  177. }
  178. int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
  179. if (FileNum != LineEntry.getFileNum()) {
  180. FileNum = LineEntry.getFileNum();
  181. MCOS->emitInt8(dwarf::DW_LNS_set_file);
  182. MCOS->emitULEB128IntValue(FileNum);
  183. }
  184. if (Column != LineEntry.getColumn()) {
  185. Column = LineEntry.getColumn();
  186. MCOS->emitInt8(dwarf::DW_LNS_set_column);
  187. MCOS->emitULEB128IntValue(Column);
  188. }
  189. if (Discriminator != LineEntry.getDiscriminator() &&
  190. MCOS->getContext().getDwarfVersion() >= 4) {
  191. Discriminator = LineEntry.getDiscriminator();
  192. unsigned Size = getULEB128Size(Discriminator);
  193. MCOS->emitInt8(dwarf::DW_LNS_extended_op);
  194. MCOS->emitULEB128IntValue(Size + 1);
  195. MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
  196. MCOS->emitULEB128IntValue(Discriminator);
  197. }
  198. if (Isa != LineEntry.getIsa()) {
  199. Isa = LineEntry.getIsa();
  200. MCOS->emitInt8(dwarf::DW_LNS_set_isa);
  201. MCOS->emitULEB128IntValue(Isa);
  202. }
  203. if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
  204. Flags = LineEntry.getFlags();
  205. MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
  206. }
  207. if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
  208. MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
  209. if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
  210. MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
  211. if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
  212. MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
  213. // At this point we want to emit/create the sequence to encode the delta in
  214. // line numbers and the increment of the address from the previous Label
  215. // and the current Label.
  216. MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
  217. asmInfo->getCodePointerSize());
  218. Discriminator = 0;
  219. LastLine = LineEntry.getLine();
  220. LastLabel = Label;
  221. }
  222. // Generate DWARF line end entry.
  223. // We do not need this for DwarfDebug that explicitly terminates the line
  224. // table using ranges whenever CU or section changes. However, the MC path
  225. // does not track ranges nor terminate the line table. In that case,
  226. // conservatively use the section end symbol to end the line table.
  227. if (!EndEntryEmitted)
  228. MCOS->emitDwarfLineEndEntry(Section, LastLabel);
  229. }
  230. //
  231. // This emits the Dwarf file and the line tables.
  232. //
  233. void MCDwarfLineTable::emit(MCStreamer *MCOS, MCDwarfLineTableParams Params) {
  234. MCContext &context = MCOS->getContext();
  235. auto &LineTables = context.getMCDwarfLineTables();
  236. // Bail out early so we don't switch to the debug_line section needlessly and
  237. // in doing so create an unnecessary (if empty) section.
  238. if (LineTables.empty())
  239. return;
  240. // In a v5 non-split line table, put the strings in a separate section.
  241. std::optional<MCDwarfLineStr> LineStr;
  242. if (context.getDwarfVersion() >= 5)
  243. LineStr.emplace(context);
  244. // Switch to the section where the table will be emitted into.
  245. MCOS->switchSection(context.getObjectFileInfo()->getDwarfLineSection());
  246. // Handle the rest of the Compile Units.
  247. for (const auto &CUIDTablePair : LineTables) {
  248. CUIDTablePair.second.emitCU(MCOS, Params, LineStr);
  249. }
  250. if (LineStr)
  251. LineStr->emitSection(MCOS);
  252. }
  253. void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params,
  254. MCSection *Section) const {
  255. if (!HasSplitLineTable)
  256. return;
  257. std::optional<MCDwarfLineStr> NoLineStr(std::nullopt);
  258. MCOS.switchSection(Section);
  259. MCOS.emitLabel(Header.Emit(&MCOS, Params, std::nullopt, NoLineStr).second);
  260. }
  261. std::pair<MCSymbol *, MCSymbol *>
  262. MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  263. std::optional<MCDwarfLineStr> &LineStr) const {
  264. static const char StandardOpcodeLengths[] = {
  265. 0, // length of DW_LNS_copy
  266. 1, // length of DW_LNS_advance_pc
  267. 1, // length of DW_LNS_advance_line
  268. 1, // length of DW_LNS_set_file
  269. 1, // length of DW_LNS_set_column
  270. 0, // length of DW_LNS_negate_stmt
  271. 0, // length of DW_LNS_set_basic_block
  272. 0, // length of DW_LNS_const_add_pc
  273. 1, // length of DW_LNS_fixed_advance_pc
  274. 0, // length of DW_LNS_set_prologue_end
  275. 0, // length of DW_LNS_set_epilogue_begin
  276. 1 // DW_LNS_set_isa
  277. };
  278. assert(std::size(StandardOpcodeLengths) >=
  279. (Params.DWARF2LineOpcodeBase - 1U));
  280. return Emit(MCOS, Params,
  281. ArrayRef(StandardOpcodeLengths, Params.DWARF2LineOpcodeBase - 1),
  282. LineStr);
  283. }
  284. static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
  285. MCContext &Context = OS.getContext();
  286. assert(!isa<MCSymbolRefExpr>(Expr));
  287. if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
  288. return Expr;
  289. MCSymbol *ABS = Context.createTempSymbol();
  290. OS.emitAssignment(ABS, Expr);
  291. return MCSymbolRefExpr::create(ABS, Context);
  292. }
  293. static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
  294. const MCExpr *ABS = forceExpAbs(OS, Value);
  295. OS.emitValue(ABS, Size);
  296. }
  297. void MCDwarfLineStr::emitSection(MCStreamer *MCOS) {
  298. // Switch to the .debug_line_str section.
  299. MCOS->switchSection(
  300. MCOS->getContext().getObjectFileInfo()->getDwarfLineStrSection());
  301. SmallString<0> Data = getFinalizedData();
  302. MCOS->emitBinaryData(Data.str());
  303. }
  304. SmallString<0> MCDwarfLineStr::getFinalizedData() {
  305. // Emit the strings without perturbing the offsets we used.
  306. if (!LineStrings.isFinalized())
  307. LineStrings.finalizeInOrder();
  308. SmallString<0> Data;
  309. Data.resize(LineStrings.getSize());
  310. LineStrings.write((uint8_t *)Data.data());
  311. return Data;
  312. }
  313. void MCDwarfLineStr::emitRef(MCStreamer *MCOS, StringRef Path) {
  314. int RefSize =
  315. dwarf::getDwarfOffsetByteSize(MCOS->getContext().getDwarfFormat());
  316. size_t Offset = LineStrings.add(Path);
  317. if (UseRelocs) {
  318. MCContext &Ctx = MCOS->getContext();
  319. MCOS->emitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
  320. } else
  321. MCOS->emitIntValue(Offset, RefSize);
  322. }
  323. void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
  324. // First the directory table.
  325. for (auto &Dir : MCDwarfDirs) {
  326. MCOS->emitBytes(Dir); // The DirectoryName, and...
  327. MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
  328. }
  329. MCOS->emitInt8(0); // Terminate the directory list.
  330. // Second the file table.
  331. for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
  332. assert(!MCDwarfFiles[i].Name.empty());
  333. MCOS->emitBytes(MCDwarfFiles[i].Name); // FileName and...
  334. MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
  335. MCOS->emitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
  336. MCOS->emitInt8(0); // Last modification timestamp (always 0).
  337. MCOS->emitInt8(0); // File size (always 0).
  338. }
  339. MCOS->emitInt8(0); // Terminate the file list.
  340. }
  341. static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
  342. bool EmitMD5, bool HasSource,
  343. std::optional<MCDwarfLineStr> &LineStr) {
  344. assert(!DwarfFile.Name.empty());
  345. if (LineStr)
  346. LineStr->emitRef(MCOS, DwarfFile.Name);
  347. else {
  348. MCOS->emitBytes(DwarfFile.Name); // FileName and...
  349. MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
  350. }
  351. MCOS->emitULEB128IntValue(DwarfFile.DirIndex); // Directory number.
  352. if (EmitMD5) {
  353. const MD5::MD5Result &Cksum = *DwarfFile.Checksum;
  354. MCOS->emitBinaryData(
  355. StringRef(reinterpret_cast<const char *>(Cksum.data()), Cksum.size()));
  356. }
  357. if (HasSource) {
  358. if (LineStr)
  359. LineStr->emitRef(MCOS, DwarfFile.Source.value_or(StringRef()));
  360. else {
  361. MCOS->emitBytes(DwarfFile.Source.value_or(StringRef())); // Source and...
  362. MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
  363. }
  364. }
  365. }
  366. void MCDwarfLineTableHeader::emitV5FileDirTables(
  367. MCStreamer *MCOS, std::optional<MCDwarfLineStr> &LineStr) const {
  368. // The directory format, which is just a list of the directory paths. In a
  369. // non-split object, these are references to .debug_line_str; in a split
  370. // object, they are inline strings.
  371. MCOS->emitInt8(1);
  372. MCOS->emitULEB128IntValue(dwarf::DW_LNCT_path);
  373. MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
  374. : dwarf::DW_FORM_string);
  375. MCOS->emitULEB128IntValue(MCDwarfDirs.size() + 1);
  376. // Try not to emit an empty compilation directory.
  377. SmallString<256> Dir;
  378. StringRef CompDir = MCOS->getContext().getCompilationDir();
  379. if (!CompilationDir.empty()) {
  380. Dir = CompilationDir;
  381. MCOS->getContext().remapDebugPath(Dir);
  382. CompDir = Dir.str();
  383. if (LineStr)
  384. CompDir = LineStr->getSaver().save(CompDir);
  385. }
  386. if (LineStr) {
  387. // Record path strings, emit references here.
  388. LineStr->emitRef(MCOS, CompDir);
  389. for (const auto &Dir : MCDwarfDirs)
  390. LineStr->emitRef(MCOS, Dir);
  391. } else {
  392. // The list of directory paths. Compilation directory comes first.
  393. MCOS->emitBytes(CompDir);
  394. MCOS->emitBytes(StringRef("\0", 1));
  395. for (const auto &Dir : MCDwarfDirs) {
  396. MCOS->emitBytes(Dir); // The DirectoryName, and...
  397. MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
  398. }
  399. }
  400. // The file format, which is the inline null-terminated filename and a
  401. // directory index. We don't track file size/timestamp so don't emit them
  402. // in the v5 table. Emit MD5 checksums and source if we have them.
  403. uint64_t Entries = 2;
  404. if (HasAllMD5)
  405. Entries += 1;
  406. if (HasSource)
  407. Entries += 1;
  408. MCOS->emitInt8(Entries);
  409. MCOS->emitULEB128IntValue(dwarf::DW_LNCT_path);
  410. MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
  411. : dwarf::DW_FORM_string);
  412. MCOS->emitULEB128IntValue(dwarf::DW_LNCT_directory_index);
  413. MCOS->emitULEB128IntValue(dwarf::DW_FORM_udata);
  414. if (HasAllMD5) {
  415. MCOS->emitULEB128IntValue(dwarf::DW_LNCT_MD5);
  416. MCOS->emitULEB128IntValue(dwarf::DW_FORM_data16);
  417. }
  418. if (HasSource) {
  419. MCOS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
  420. MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
  421. : dwarf::DW_FORM_string);
  422. }
  423. // Then the counted list of files. The root file is file #0, then emit the
  424. // files as provide by .file directives.
  425. // MCDwarfFiles has an unused element [0] so use size() not size()+1.
  426. // But sometimes MCDwarfFiles is empty, in which case we still emit one file.
  427. MCOS->emitULEB128IntValue(MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size());
  428. // To accommodate assembler source written for DWARF v4 but trying to emit
  429. // v5: If we didn't see a root file explicitly, replicate file #1.
  430. assert((!RootFile.Name.empty() || MCDwarfFiles.size() >= 1) &&
  431. "No root file and no .file directives");
  432. emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
  433. HasAllMD5, HasSource, LineStr);
  434. for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
  435. emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
  436. }
  437. std::pair<MCSymbol *, MCSymbol *>
  438. MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  439. ArrayRef<char> StandardOpcodeLengths,
  440. std::optional<MCDwarfLineStr> &LineStr) const {
  441. MCContext &context = MCOS->getContext();
  442. // Create a symbol at the beginning of the line table.
  443. MCSymbol *LineStartSym = Label;
  444. if (!LineStartSym)
  445. LineStartSym = context.createTempSymbol();
  446. // Set the value of the symbol, as we are at the start of the line table.
  447. MCOS->emitDwarfLineStartLabel(LineStartSym);
  448. unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
  449. MCSymbol *LineEndSym = MCOS->emitDwarfUnitLength("debug_line", "unit length");
  450. // Next 2 bytes is the Version.
  451. unsigned LineTableVersion = context.getDwarfVersion();
  452. MCOS->emitInt16(LineTableVersion);
  453. // In v5, we get address info next.
  454. if (LineTableVersion >= 5) {
  455. MCOS->emitInt8(context.getAsmInfo()->getCodePointerSize());
  456. MCOS->emitInt8(0); // Segment selector; same as EmitGenDwarfAranges.
  457. }
  458. // Create symbols for the start/end of the prologue.
  459. MCSymbol *ProStartSym = context.createTempSymbol("prologue_start");
  460. MCSymbol *ProEndSym = context.createTempSymbol("prologue_end");
  461. // Length of the prologue, is the next 4 bytes (8 bytes for DWARF64). This is
  462. // actually the length from after the length word, to the end of the prologue.
  463. MCOS->emitAbsoluteSymbolDiff(ProEndSym, ProStartSym, OffsetSize);
  464. MCOS->emitLabel(ProStartSym);
  465. // Parameters of the state machine, are next.
  466. MCOS->emitInt8(context.getAsmInfo()->getMinInstAlignment());
  467. // maximum_operations_per_instruction
  468. // For non-VLIW architectures this field is always 1.
  469. // FIXME: VLIW architectures need to update this field accordingly.
  470. if (LineTableVersion >= 4)
  471. MCOS->emitInt8(1);
  472. MCOS->emitInt8(DWARF2_LINE_DEFAULT_IS_STMT);
  473. MCOS->emitInt8(Params.DWARF2LineBase);
  474. MCOS->emitInt8(Params.DWARF2LineRange);
  475. MCOS->emitInt8(StandardOpcodeLengths.size() + 1);
  476. // Standard opcode lengths
  477. for (char Length : StandardOpcodeLengths)
  478. MCOS->emitInt8(Length);
  479. // Put out the directory and file tables. The formats vary depending on
  480. // the version.
  481. if (LineTableVersion >= 5)
  482. emitV5FileDirTables(MCOS, LineStr);
  483. else
  484. emitV2FileDirTables(MCOS);
  485. // This is the end of the prologue, so set the value of the symbol at the
  486. // end of the prologue (that was used in a previous expression).
  487. MCOS->emitLabel(ProEndSym);
  488. return std::make_pair(LineStartSym, LineEndSym);
  489. }
  490. void MCDwarfLineTable::emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  491. std::optional<MCDwarfLineStr> &LineStr) const {
  492. MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
  493. // Put out the line tables.
  494. for (const auto &LineSec : MCLineSections.getMCLineEntries())
  495. emitOne(MCOS, LineSec.first, LineSec.second);
  496. // This is the end of the section, so set the value of the symbol at the end
  497. // of this section (that was used in a previous expression).
  498. MCOS->emitLabel(LineEndSym);
  499. }
  500. Expected<unsigned>
  501. MCDwarfLineTable::tryGetFile(StringRef &Directory, StringRef &FileName,
  502. std::optional<MD5::MD5Result> Checksum,
  503. std::optional<StringRef> Source,
  504. uint16_t DwarfVersion, unsigned FileNumber) {
  505. return Header.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
  506. FileNumber);
  507. }
  508. static bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
  509. StringRef &FileName,
  510. std::optional<MD5::MD5Result> Checksum) {
  511. if (RootFile.Name.empty() || StringRef(RootFile.Name) != FileName)
  512. return false;
  513. return RootFile.Checksum == Checksum;
  514. }
  515. Expected<unsigned>
  516. MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, StringRef &FileName,
  517. std::optional<MD5::MD5Result> Checksum,
  518. std::optional<StringRef> Source,
  519. uint16_t DwarfVersion, unsigned FileNumber) {
  520. if (Directory == CompilationDir)
  521. Directory = "";
  522. if (FileName.empty()) {
  523. FileName = "<stdin>";
  524. Directory = "";
  525. }
  526. assert(!FileName.empty());
  527. // Keep track of whether any or all files have an MD5 checksum.
  528. // If any files have embedded source, they all must.
  529. if (MCDwarfFiles.empty()) {
  530. trackMD5Usage(Checksum.has_value());
  531. HasSource = (Source != std::nullopt);
  532. }
  533. if (DwarfVersion >= 5 && isRootFile(RootFile, Directory, FileName, Checksum))
  534. return 0;
  535. if (FileNumber == 0) {
  536. // File numbers start with 1 and/or after any file numbers
  537. // allocated by inline-assembler .file directives.
  538. FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
  539. SmallString<256> Buffer;
  540. auto IterBool = SourceIdMap.insert(
  541. std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
  542. FileNumber));
  543. if (!IterBool.second)
  544. return IterBool.first->second;
  545. }
  546. // Make space for this FileNumber in the MCDwarfFiles vector if needed.
  547. if (FileNumber >= MCDwarfFiles.size())
  548. MCDwarfFiles.resize(FileNumber + 1);
  549. // Get the new MCDwarfFile slot for this FileNumber.
  550. MCDwarfFile &File = MCDwarfFiles[FileNumber];
  551. // It is an error to see the same number more than once.
  552. if (!File.Name.empty())
  553. return make_error<StringError>("file number already allocated",
  554. inconvertibleErrorCode());
  555. // If any files have embedded source, they all must.
  556. if (HasSource != (Source != std::nullopt))
  557. return make_error<StringError>("inconsistent use of embedded source",
  558. inconvertibleErrorCode());
  559. if (Directory.empty()) {
  560. // Separate the directory part from the basename of the FileName.
  561. StringRef tFileName = sys::path::filename(FileName);
  562. if (!tFileName.empty()) {
  563. Directory = sys::path::parent_path(FileName);
  564. if (!Directory.empty())
  565. FileName = tFileName;
  566. }
  567. }
  568. // Find or make an entry in the MCDwarfDirs vector for this Directory.
  569. // Capture directory name.
  570. unsigned DirIndex;
  571. if (Directory.empty()) {
  572. // For FileNames with no directories a DirIndex of 0 is used.
  573. DirIndex = 0;
  574. } else {
  575. DirIndex = llvm::find(MCDwarfDirs, Directory) - MCDwarfDirs.begin();
  576. if (DirIndex >= MCDwarfDirs.size())
  577. MCDwarfDirs.push_back(std::string(Directory));
  578. // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
  579. // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
  580. // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
  581. // are stored at MCDwarfFiles[FileNumber].Name .
  582. DirIndex++;
  583. }
  584. File.Name = std::string(FileName);
  585. File.DirIndex = DirIndex;
  586. File.Checksum = Checksum;
  587. trackMD5Usage(Checksum.has_value());
  588. File.Source = Source;
  589. if (Source)
  590. HasSource = true;
  591. // return the allocated FileNumber.
  592. return FileNumber;
  593. }
  594. /// Utility function to emit the encoding to a streamer.
  595. void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  596. int64_t LineDelta, uint64_t AddrDelta) {
  597. MCContext &Context = MCOS->getContext();
  598. SmallString<256> Tmp;
  599. raw_svector_ostream OS(Tmp);
  600. MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
  601. MCOS->emitBytes(OS.str());
  602. }
  603. /// Given a special op, return the address skip amount (in units of
  604. /// DWARF2_LINE_MIN_INSN_LENGTH).
  605. static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
  606. return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
  607. }
  608. /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
  609. void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
  610. int64_t LineDelta, uint64_t AddrDelta,
  611. raw_ostream &OS) {
  612. uint64_t Temp, Opcode;
  613. bool NeedCopy = false;
  614. // The maximum address skip amount that can be encoded with a special op.
  615. uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
  616. // Scale the address delta by the minimum instruction length.
  617. AddrDelta = ScaleAddrDelta(Context, AddrDelta);
  618. // A LineDelta of INT64_MAX is a signal that this is actually a
  619. // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
  620. // end_sequence to emit the matrix entry.
  621. if (LineDelta == INT64_MAX) {
  622. if (AddrDelta == MaxSpecialAddrDelta)
  623. OS << char(dwarf::DW_LNS_const_add_pc);
  624. else if (AddrDelta) {
  625. OS << char(dwarf::DW_LNS_advance_pc);
  626. encodeULEB128(AddrDelta, OS);
  627. }
  628. OS << char(dwarf::DW_LNS_extended_op);
  629. OS << char(1);
  630. OS << char(dwarf::DW_LNE_end_sequence);
  631. return;
  632. }
  633. // Bias the line delta by the base.
  634. Temp = LineDelta - Params.DWARF2LineBase;
  635. // If the line increment is out of range of a special opcode, we must encode
  636. // it with DW_LNS_advance_line.
  637. if (Temp >= Params.DWARF2LineRange ||
  638. Temp + Params.DWARF2LineOpcodeBase > 255) {
  639. OS << char(dwarf::DW_LNS_advance_line);
  640. encodeSLEB128(LineDelta, OS);
  641. LineDelta = 0;
  642. Temp = 0 - Params.DWARF2LineBase;
  643. NeedCopy = true;
  644. }
  645. // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
  646. if (LineDelta == 0 && AddrDelta == 0) {
  647. OS << char(dwarf::DW_LNS_copy);
  648. return;
  649. }
  650. // Bias the opcode by the special opcode base.
  651. Temp += Params.DWARF2LineOpcodeBase;
  652. // Avoid overflow when addr_delta is large.
  653. if (AddrDelta < 256 + MaxSpecialAddrDelta) {
  654. // Try using a special opcode.
  655. Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
  656. if (Opcode <= 255) {
  657. OS << char(Opcode);
  658. return;
  659. }
  660. // Try using DW_LNS_const_add_pc followed by special op.
  661. Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
  662. if (Opcode <= 255) {
  663. OS << char(dwarf::DW_LNS_const_add_pc);
  664. OS << char(Opcode);
  665. return;
  666. }
  667. }
  668. // Otherwise use DW_LNS_advance_pc.
  669. OS << char(dwarf::DW_LNS_advance_pc);
  670. encodeULEB128(AddrDelta, OS);
  671. if (NeedCopy)
  672. OS << char(dwarf::DW_LNS_copy);
  673. else {
  674. assert(Temp <= 255 && "Buggy special opcode encoding.");
  675. OS << char(Temp);
  676. }
  677. }
  678. // Utility function to write a tuple for .debug_abbrev.
  679. static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
  680. MCOS->emitULEB128IntValue(Name);
  681. MCOS->emitULEB128IntValue(Form);
  682. }
  683. // When generating dwarf for assembly source files this emits
  684. // the data for .debug_abbrev section which contains three DIEs.
  685. static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
  686. MCContext &context = MCOS->getContext();
  687. MCOS->switchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
  688. // DW_TAG_compile_unit DIE abbrev (1).
  689. MCOS->emitULEB128IntValue(1);
  690. MCOS->emitULEB128IntValue(dwarf::DW_TAG_compile_unit);
  691. MCOS->emitInt8(dwarf::DW_CHILDREN_yes);
  692. dwarf::Form SecOffsetForm =
  693. context.getDwarfVersion() >= 4
  694. ? dwarf::DW_FORM_sec_offset
  695. : (context.getDwarfFormat() == dwarf::DWARF64 ? dwarf::DW_FORM_data8
  696. : dwarf::DW_FORM_data4);
  697. EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, SecOffsetForm);
  698. if (context.getGenDwarfSectionSyms().size() > 1 &&
  699. context.getDwarfVersion() >= 3) {
  700. EmitAbbrev(MCOS, dwarf::DW_AT_ranges, SecOffsetForm);
  701. } else {
  702. EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
  703. EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
  704. }
  705. EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
  706. if (!context.getCompilationDir().empty())
  707. EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
  708. StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
  709. if (!DwarfDebugFlags.empty())
  710. EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
  711. EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
  712. EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
  713. EmitAbbrev(MCOS, 0, 0);
  714. // DW_TAG_label DIE abbrev (2).
  715. MCOS->emitULEB128IntValue(2);
  716. MCOS->emitULEB128IntValue(dwarf::DW_TAG_label);
  717. MCOS->emitInt8(dwarf::DW_CHILDREN_no);
  718. EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
  719. EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
  720. EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
  721. EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
  722. EmitAbbrev(MCOS, 0, 0);
  723. // Terminate the abbreviations for this compilation unit.
  724. MCOS->emitInt8(0);
  725. }
  726. // When generating dwarf for assembly source files this emits the data for
  727. // .debug_aranges section. This section contains a header and a table of pairs
  728. // of PointerSize'ed values for the address and size of section(s) with line
  729. // table entries.
  730. static void EmitGenDwarfAranges(MCStreamer *MCOS,
  731. const MCSymbol *InfoSectionSymbol) {
  732. MCContext &context = MCOS->getContext();
  733. auto &Sections = context.getGenDwarfSectionSyms();
  734. MCOS->switchSection(context.getObjectFileInfo()->getDwarfARangesSection());
  735. unsigned UnitLengthBytes =
  736. dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
  737. unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
  738. // This will be the length of the .debug_aranges section, first account for
  739. // the size of each item in the header (see below where we emit these items).
  740. int Length = UnitLengthBytes + 2 + OffsetSize + 1 + 1;
  741. // Figure the padding after the header before the table of address and size
  742. // pairs who's values are PointerSize'ed.
  743. const MCAsmInfo *asmInfo = context.getAsmInfo();
  744. int AddrSize = asmInfo->getCodePointerSize();
  745. int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
  746. if (Pad == 2 * AddrSize)
  747. Pad = 0;
  748. Length += Pad;
  749. // Add the size of the pair of PointerSize'ed values for the address and size
  750. // of each section we have in the table.
  751. Length += 2 * AddrSize * Sections.size();
  752. // And the pair of terminating zeros.
  753. Length += 2 * AddrSize;
  754. // Emit the header for this section.
  755. if (context.getDwarfFormat() == dwarf::DWARF64)
  756. // The DWARF64 mark.
  757. MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
  758. // The 4 (8 for DWARF64) byte length not including the length of the unit
  759. // length field itself.
  760. MCOS->emitIntValue(Length - UnitLengthBytes, OffsetSize);
  761. // The 2 byte version, which is 2.
  762. MCOS->emitInt16(2);
  763. // The 4 (8 for DWARF64) byte offset to the compile unit in the .debug_info
  764. // from the start of the .debug_info.
  765. if (InfoSectionSymbol)
  766. MCOS->emitSymbolValue(InfoSectionSymbol, OffsetSize,
  767. asmInfo->needsDwarfSectionOffsetDirective());
  768. else
  769. MCOS->emitIntValue(0, OffsetSize);
  770. // The 1 byte size of an address.
  771. MCOS->emitInt8(AddrSize);
  772. // The 1 byte size of a segment descriptor, we use a value of zero.
  773. MCOS->emitInt8(0);
  774. // Align the header with the padding if needed, before we put out the table.
  775. for(int i = 0; i < Pad; i++)
  776. MCOS->emitInt8(0);
  777. // Now emit the table of pairs of PointerSize'ed values for the section
  778. // addresses and sizes.
  779. for (MCSection *Sec : Sections) {
  780. const MCSymbol *StartSymbol = Sec->getBeginSymbol();
  781. MCSymbol *EndSymbol = Sec->getEndSymbol(context);
  782. assert(StartSymbol && "StartSymbol must not be NULL");
  783. assert(EndSymbol && "EndSymbol must not be NULL");
  784. const MCExpr *Addr = MCSymbolRefExpr::create(
  785. StartSymbol, MCSymbolRefExpr::VK_None, context);
  786. const MCExpr *Size =
  787. makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
  788. MCOS->emitValue(Addr, AddrSize);
  789. emitAbsValue(*MCOS, Size, AddrSize);
  790. }
  791. // And finally the pair of terminating zeros.
  792. MCOS->emitIntValue(0, AddrSize);
  793. MCOS->emitIntValue(0, AddrSize);
  794. }
  795. // When generating dwarf for assembly source files this emits the data for
  796. // .debug_info section which contains three parts. The header, the compile_unit
  797. // DIE and a list of label DIEs.
  798. static void EmitGenDwarfInfo(MCStreamer *MCOS,
  799. const MCSymbol *AbbrevSectionSymbol,
  800. const MCSymbol *LineSectionSymbol,
  801. const MCSymbol *RangesSymbol) {
  802. MCContext &context = MCOS->getContext();
  803. MCOS->switchSection(context.getObjectFileInfo()->getDwarfInfoSection());
  804. // Create a symbol at the start and end of this section used in here for the
  805. // expression to calculate the length in the header.
  806. MCSymbol *InfoStart = context.createTempSymbol();
  807. MCOS->emitLabel(InfoStart);
  808. MCSymbol *InfoEnd = context.createTempSymbol();
  809. // First part: the header.
  810. unsigned UnitLengthBytes =
  811. dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
  812. unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
  813. if (context.getDwarfFormat() == dwarf::DWARF64)
  814. // Emit DWARF64 mark.
  815. MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
  816. // The 4 (8 for DWARF64) byte total length of the information for this
  817. // compilation unit, not including the unit length field itself.
  818. const MCExpr *Length =
  819. makeEndMinusStartExpr(context, *InfoStart, *InfoEnd, UnitLengthBytes);
  820. emitAbsValue(*MCOS, Length, OffsetSize);
  821. // The 2 byte DWARF version.
  822. MCOS->emitInt16(context.getDwarfVersion());
  823. // The DWARF v5 header has unit type, address size, abbrev offset.
  824. // Earlier versions have abbrev offset, address size.
  825. const MCAsmInfo &AsmInfo = *context.getAsmInfo();
  826. int AddrSize = AsmInfo.getCodePointerSize();
  827. if (context.getDwarfVersion() >= 5) {
  828. MCOS->emitInt8(dwarf::DW_UT_compile);
  829. MCOS->emitInt8(AddrSize);
  830. }
  831. // The 4 (8 for DWARF64) byte offset to the debug abbrevs from the start of
  832. // the .debug_abbrev.
  833. if (AbbrevSectionSymbol)
  834. MCOS->emitSymbolValue(AbbrevSectionSymbol, OffsetSize,
  835. AsmInfo.needsDwarfSectionOffsetDirective());
  836. else
  837. // Since the abbrevs are at the start of the section, the offset is zero.
  838. MCOS->emitIntValue(0, OffsetSize);
  839. if (context.getDwarfVersion() <= 4)
  840. MCOS->emitInt8(AddrSize);
  841. // Second part: the compile_unit DIE.
  842. // The DW_TAG_compile_unit DIE abbrev (1).
  843. MCOS->emitULEB128IntValue(1);
  844. // DW_AT_stmt_list, a 4 (8 for DWARF64) byte offset from the start of the
  845. // .debug_line section.
  846. if (LineSectionSymbol)
  847. MCOS->emitSymbolValue(LineSectionSymbol, OffsetSize,
  848. AsmInfo.needsDwarfSectionOffsetDirective());
  849. else
  850. // The line table is at the start of the section, so the offset is zero.
  851. MCOS->emitIntValue(0, OffsetSize);
  852. if (RangesSymbol) {
  853. // There are multiple sections containing code, so we must use
  854. // .debug_ranges/.debug_rnglists. AT_ranges, the 4/8 byte offset from the
  855. // start of the .debug_ranges/.debug_rnglists.
  856. MCOS->emitSymbolValue(RangesSymbol, OffsetSize);
  857. } else {
  858. // If we only have one non-empty code section, we can use the simpler
  859. // AT_low_pc and AT_high_pc attributes.
  860. // Find the first (and only) non-empty text section
  861. auto &Sections = context.getGenDwarfSectionSyms();
  862. const auto TextSection = Sections.begin();
  863. assert(TextSection != Sections.end() && "No text section found");
  864. MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
  865. MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
  866. assert(StartSymbol && "StartSymbol must not be NULL");
  867. assert(EndSymbol && "EndSymbol must not be NULL");
  868. // AT_low_pc, the first address of the default .text section.
  869. const MCExpr *Start = MCSymbolRefExpr::create(
  870. StartSymbol, MCSymbolRefExpr::VK_None, context);
  871. MCOS->emitValue(Start, AddrSize);
  872. // AT_high_pc, the last address of the default .text section.
  873. const MCExpr *End = MCSymbolRefExpr::create(
  874. EndSymbol, MCSymbolRefExpr::VK_None, context);
  875. MCOS->emitValue(End, AddrSize);
  876. }
  877. // AT_name, the name of the source file. Reconstruct from the first directory
  878. // and file table entries.
  879. const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
  880. if (MCDwarfDirs.size() > 0) {
  881. MCOS->emitBytes(MCDwarfDirs[0]);
  882. MCOS->emitBytes(sys::path::get_separator());
  883. }
  884. const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = context.getMCDwarfFiles();
  885. // MCDwarfFiles might be empty if we have an empty source file.
  886. // If it's not empty, [0] is unused and [1] is the first actual file.
  887. assert(MCDwarfFiles.empty() || MCDwarfFiles.size() >= 2);
  888. const MCDwarfFile &RootFile =
  889. MCDwarfFiles.empty()
  890. ? context.getMCDwarfLineTable(/*CUID=*/0).getRootFile()
  891. : MCDwarfFiles[1];
  892. MCOS->emitBytes(RootFile.Name);
  893. MCOS->emitInt8(0); // NULL byte to terminate the string.
  894. // AT_comp_dir, the working directory the assembly was done in.
  895. if (!context.getCompilationDir().empty()) {
  896. MCOS->emitBytes(context.getCompilationDir());
  897. MCOS->emitInt8(0); // NULL byte to terminate the string.
  898. }
  899. // AT_APPLE_flags, the command line arguments of the assembler tool.
  900. StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
  901. if (!DwarfDebugFlags.empty()){
  902. MCOS->emitBytes(DwarfDebugFlags);
  903. MCOS->emitInt8(0); // NULL byte to terminate the string.
  904. }
  905. // AT_producer, the version of the assembler tool.
  906. StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
  907. if (!DwarfDebugProducer.empty())
  908. MCOS->emitBytes(DwarfDebugProducer);
  909. else
  910. MCOS->emitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
  911. MCOS->emitInt8(0); // NULL byte to terminate the string.
  912. // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
  913. // draft has no standard code for assembler.
  914. MCOS->emitInt16(dwarf::DW_LANG_Mips_Assembler);
  915. // Third part: the list of label DIEs.
  916. // Loop on saved info for dwarf labels and create the DIEs for them.
  917. const std::vector<MCGenDwarfLabelEntry> &Entries =
  918. MCOS->getContext().getMCGenDwarfLabelEntries();
  919. for (const auto &Entry : Entries) {
  920. // The DW_TAG_label DIE abbrev (2).
  921. MCOS->emitULEB128IntValue(2);
  922. // AT_name, of the label without any leading underbar.
  923. MCOS->emitBytes(Entry.getName());
  924. MCOS->emitInt8(0); // NULL byte to terminate the string.
  925. // AT_decl_file, index into the file table.
  926. MCOS->emitInt32(Entry.getFileNumber());
  927. // AT_decl_line, source line number.
  928. MCOS->emitInt32(Entry.getLineNumber());
  929. // AT_low_pc, start address of the label.
  930. const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
  931. MCSymbolRefExpr::VK_None, context);
  932. MCOS->emitValue(AT_low_pc, AddrSize);
  933. }
  934. // Add the NULL DIE terminating the Compile Unit DIE's.
  935. MCOS->emitInt8(0);
  936. // Now set the value of the symbol at the end of the info section.
  937. MCOS->emitLabel(InfoEnd);
  938. }
  939. // When generating dwarf for assembly source files this emits the data for
  940. // .debug_ranges section. We only emit one range list, which spans all of the
  941. // executable sections of this file.
  942. static MCSymbol *emitGenDwarfRanges(MCStreamer *MCOS) {
  943. MCContext &context = MCOS->getContext();
  944. auto &Sections = context.getGenDwarfSectionSyms();
  945. const MCAsmInfo *AsmInfo = context.getAsmInfo();
  946. int AddrSize = AsmInfo->getCodePointerSize();
  947. MCSymbol *RangesSymbol;
  948. if (MCOS->getContext().getDwarfVersion() >= 5) {
  949. MCOS->switchSection(context.getObjectFileInfo()->getDwarfRnglistsSection());
  950. MCSymbol *EndSymbol = mcdwarf::emitListsTableHeaderStart(*MCOS);
  951. MCOS->AddComment("Offset entry count");
  952. MCOS->emitInt32(0);
  953. RangesSymbol = context.createTempSymbol("debug_rnglist0_start");
  954. MCOS->emitLabel(RangesSymbol);
  955. for (MCSection *Sec : Sections) {
  956. const MCSymbol *StartSymbol = Sec->getBeginSymbol();
  957. const MCSymbol *EndSymbol = Sec->getEndSymbol(context);
  958. const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
  959. StartSymbol, MCSymbolRefExpr::VK_None, context);
  960. const MCExpr *SectionSize =
  961. makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
  962. MCOS->emitInt8(dwarf::DW_RLE_start_length);
  963. MCOS->emitValue(SectionStartAddr, AddrSize);
  964. MCOS->emitULEB128Value(SectionSize);
  965. }
  966. MCOS->emitInt8(dwarf::DW_RLE_end_of_list);
  967. MCOS->emitLabel(EndSymbol);
  968. } else {
  969. MCOS->switchSection(context.getObjectFileInfo()->getDwarfRangesSection());
  970. RangesSymbol = context.createTempSymbol("debug_ranges_start");
  971. MCOS->emitLabel(RangesSymbol);
  972. for (MCSection *Sec : Sections) {
  973. const MCSymbol *StartSymbol = Sec->getBeginSymbol();
  974. const MCSymbol *EndSymbol = Sec->getEndSymbol(context);
  975. // Emit a base address selection entry for the section start.
  976. const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
  977. StartSymbol, MCSymbolRefExpr::VK_None, context);
  978. MCOS->emitFill(AddrSize, 0xFF);
  979. MCOS->emitValue(SectionStartAddr, AddrSize);
  980. // Emit a range list entry spanning this section.
  981. const MCExpr *SectionSize =
  982. makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
  983. MCOS->emitIntValue(0, AddrSize);
  984. emitAbsValue(*MCOS, SectionSize, AddrSize);
  985. }
  986. // Emit end of list entry
  987. MCOS->emitIntValue(0, AddrSize);
  988. MCOS->emitIntValue(0, AddrSize);
  989. }
  990. return RangesSymbol;
  991. }
  992. //
  993. // When generating dwarf for assembly source files this emits the Dwarf
  994. // sections.
  995. //
  996. void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
  997. MCContext &context = MCOS->getContext();
  998. // Create the dwarf sections in this order (.debug_line already created).
  999. const MCAsmInfo *AsmInfo = context.getAsmInfo();
  1000. bool CreateDwarfSectionSymbols =
  1001. AsmInfo->doesDwarfUseRelocationsAcrossSections();
  1002. MCSymbol *LineSectionSymbol = nullptr;
  1003. if (CreateDwarfSectionSymbols)
  1004. LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
  1005. MCSymbol *AbbrevSectionSymbol = nullptr;
  1006. MCSymbol *InfoSectionSymbol = nullptr;
  1007. MCSymbol *RangesSymbol = nullptr;
  1008. // Create end symbols for each section, and remove empty sections
  1009. MCOS->getContext().finalizeDwarfSections(*MCOS);
  1010. // If there are no sections to generate debug info for, we don't need
  1011. // to do anything
  1012. if (MCOS->getContext().getGenDwarfSectionSyms().empty())
  1013. return;
  1014. // We only use the .debug_ranges section if we have multiple code sections,
  1015. // and we are emitting a DWARF version which supports it.
  1016. const bool UseRangesSection =
  1017. MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
  1018. MCOS->getContext().getDwarfVersion() >= 3;
  1019. CreateDwarfSectionSymbols |= UseRangesSection;
  1020. MCOS->switchSection(context.getObjectFileInfo()->getDwarfInfoSection());
  1021. if (CreateDwarfSectionSymbols) {
  1022. InfoSectionSymbol = context.createTempSymbol();
  1023. MCOS->emitLabel(InfoSectionSymbol);
  1024. }
  1025. MCOS->switchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
  1026. if (CreateDwarfSectionSymbols) {
  1027. AbbrevSectionSymbol = context.createTempSymbol();
  1028. MCOS->emitLabel(AbbrevSectionSymbol);
  1029. }
  1030. MCOS->switchSection(context.getObjectFileInfo()->getDwarfARangesSection());
  1031. // Output the data for .debug_aranges section.
  1032. EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
  1033. if (UseRangesSection) {
  1034. RangesSymbol = emitGenDwarfRanges(MCOS);
  1035. assert(RangesSymbol);
  1036. }
  1037. // Output the data for .debug_abbrev section.
  1038. EmitGenDwarfAbbrev(MCOS);
  1039. // Output the data for .debug_info section.
  1040. EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol, RangesSymbol);
  1041. }
  1042. //
  1043. // When generating dwarf for assembly source files this is called when symbol
  1044. // for a label is created. If this symbol is not a temporary and is in the
  1045. // section that dwarf is being generated for, save the needed info to create
  1046. // a dwarf label.
  1047. //
  1048. void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
  1049. SourceMgr &SrcMgr, SMLoc &Loc) {
  1050. // We won't create dwarf labels for temporary symbols.
  1051. if (Symbol->isTemporary())
  1052. return;
  1053. MCContext &context = MCOS->getContext();
  1054. // We won't create dwarf labels for symbols in sections that we are not
  1055. // generating debug info for.
  1056. if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
  1057. return;
  1058. // The dwarf label's name does not have the symbol name's leading
  1059. // underbar if any.
  1060. StringRef Name = Symbol->getName();
  1061. if (Name.startswith("_"))
  1062. Name = Name.substr(1, Name.size()-1);
  1063. // Get the dwarf file number to be used for the dwarf label.
  1064. unsigned FileNumber = context.getGenDwarfFileNumber();
  1065. // Finding the line number is the expensive part which is why we just don't
  1066. // pass it in as for some symbols we won't create a dwarf label.
  1067. unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
  1068. unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
  1069. // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
  1070. // values so that they don't have things like an ARM thumb bit from the
  1071. // original symbol. So when used they won't get a low bit set after
  1072. // relocation.
  1073. MCSymbol *Label = context.createTempSymbol();
  1074. MCOS->emitLabel(Label);
  1075. // Create and entry for the info and add it to the other entries.
  1076. MCOS->getContext().addMCGenDwarfLabelEntry(
  1077. MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
  1078. }
  1079. static int getDataAlignmentFactor(MCStreamer &streamer) {
  1080. MCContext &context = streamer.getContext();
  1081. const MCAsmInfo *asmInfo = context.getAsmInfo();
  1082. int size = asmInfo->getCalleeSaveStackSlotSize();
  1083. if (asmInfo->isStackGrowthDirectionUp())
  1084. return size;
  1085. else
  1086. return -size;
  1087. }
  1088. static unsigned getSizeForEncoding(MCStreamer &streamer,
  1089. unsigned symbolEncoding) {
  1090. MCContext &context = streamer.getContext();
  1091. unsigned format = symbolEncoding & 0x0f;
  1092. switch (format) {
  1093. default: llvm_unreachable("Unknown Encoding");
  1094. case dwarf::DW_EH_PE_absptr:
  1095. case dwarf::DW_EH_PE_signed:
  1096. return context.getAsmInfo()->getCodePointerSize();
  1097. case dwarf::DW_EH_PE_udata2:
  1098. case dwarf::DW_EH_PE_sdata2:
  1099. return 2;
  1100. case dwarf::DW_EH_PE_udata4:
  1101. case dwarf::DW_EH_PE_sdata4:
  1102. return 4;
  1103. case dwarf::DW_EH_PE_udata8:
  1104. case dwarf::DW_EH_PE_sdata8:
  1105. return 8;
  1106. }
  1107. }
  1108. static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
  1109. unsigned symbolEncoding, bool isEH) {
  1110. MCContext &context = streamer.getContext();
  1111. const MCAsmInfo *asmInfo = context.getAsmInfo();
  1112. const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
  1113. symbolEncoding,
  1114. streamer);
  1115. unsigned size = getSizeForEncoding(streamer, symbolEncoding);
  1116. if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
  1117. emitAbsValue(streamer, v, size);
  1118. else
  1119. streamer.emitValue(v, size);
  1120. }
  1121. static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
  1122. unsigned symbolEncoding) {
  1123. MCContext &context = streamer.getContext();
  1124. const MCAsmInfo *asmInfo = context.getAsmInfo();
  1125. const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
  1126. symbolEncoding,
  1127. streamer);
  1128. unsigned size = getSizeForEncoding(streamer, symbolEncoding);
  1129. streamer.emitValue(v, size);
  1130. }
  1131. namespace {
  1132. class FrameEmitterImpl {
  1133. int CFAOffset = 0;
  1134. int InitialCFAOffset = 0;
  1135. bool IsEH;
  1136. MCObjectStreamer &Streamer;
  1137. public:
  1138. FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
  1139. : IsEH(IsEH), Streamer(Streamer) {}
  1140. /// Emit the unwind information in a compact way.
  1141. void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
  1142. const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
  1143. void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
  1144. bool LastInSection, const MCSymbol &SectionStart);
  1145. void emitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
  1146. MCSymbol *BaseLabel);
  1147. void emitCFIInstruction(const MCCFIInstruction &Instr);
  1148. };
  1149. } // end anonymous namespace
  1150. static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
  1151. Streamer.emitInt8(Encoding);
  1152. }
  1153. void FrameEmitterImpl::emitCFIInstruction(const MCCFIInstruction &Instr) {
  1154. int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
  1155. auto *MRI = Streamer.getContext().getRegisterInfo();
  1156. switch (Instr.getOperation()) {
  1157. case MCCFIInstruction::OpRegister: {
  1158. unsigned Reg1 = Instr.getRegister();
  1159. unsigned Reg2 = Instr.getRegister2();
  1160. if (!IsEH) {
  1161. Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
  1162. Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
  1163. }
  1164. Streamer.emitInt8(dwarf::DW_CFA_register);
  1165. Streamer.emitULEB128IntValue(Reg1);
  1166. Streamer.emitULEB128IntValue(Reg2);
  1167. return;
  1168. }
  1169. case MCCFIInstruction::OpWindowSave:
  1170. Streamer.emitInt8(dwarf::DW_CFA_GNU_window_save);
  1171. return;
  1172. case MCCFIInstruction::OpNegateRAState:
  1173. Streamer.emitInt8(dwarf::DW_CFA_AARCH64_negate_ra_state);
  1174. return;
  1175. case MCCFIInstruction::OpUndefined: {
  1176. unsigned Reg = Instr.getRegister();
  1177. Streamer.emitInt8(dwarf::DW_CFA_undefined);
  1178. Streamer.emitULEB128IntValue(Reg);
  1179. return;
  1180. }
  1181. case MCCFIInstruction::OpAdjustCfaOffset:
  1182. case MCCFIInstruction::OpDefCfaOffset: {
  1183. const bool IsRelative =
  1184. Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
  1185. Streamer.emitInt8(dwarf::DW_CFA_def_cfa_offset);
  1186. if (IsRelative)
  1187. CFAOffset += Instr.getOffset();
  1188. else
  1189. CFAOffset = Instr.getOffset();
  1190. Streamer.emitULEB128IntValue(CFAOffset);
  1191. return;
  1192. }
  1193. case MCCFIInstruction::OpDefCfa: {
  1194. unsigned Reg = Instr.getRegister();
  1195. if (!IsEH)
  1196. Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
  1197. Streamer.emitInt8(dwarf::DW_CFA_def_cfa);
  1198. Streamer.emitULEB128IntValue(Reg);
  1199. CFAOffset = Instr.getOffset();
  1200. Streamer.emitULEB128IntValue(CFAOffset);
  1201. return;
  1202. }
  1203. case MCCFIInstruction::OpDefCfaRegister: {
  1204. unsigned Reg = Instr.getRegister();
  1205. if (!IsEH)
  1206. Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
  1207. Streamer.emitInt8(dwarf::DW_CFA_def_cfa_register);
  1208. Streamer.emitULEB128IntValue(Reg);
  1209. return;
  1210. }
  1211. // TODO: Implement `_sf` variants if/when they need to be emitted.
  1212. case MCCFIInstruction::OpLLVMDefAspaceCfa: {
  1213. unsigned Reg = Instr.getRegister();
  1214. if (!IsEH)
  1215. Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
  1216. Streamer.emitIntValue(dwarf::DW_CFA_LLVM_def_aspace_cfa, 1);
  1217. Streamer.emitULEB128IntValue(Reg);
  1218. CFAOffset = Instr.getOffset();
  1219. Streamer.emitULEB128IntValue(CFAOffset);
  1220. Streamer.emitULEB128IntValue(Instr.getAddressSpace());
  1221. return;
  1222. }
  1223. case MCCFIInstruction::OpOffset:
  1224. case MCCFIInstruction::OpRelOffset: {
  1225. const bool IsRelative =
  1226. Instr.getOperation() == MCCFIInstruction::OpRelOffset;
  1227. unsigned Reg = Instr.getRegister();
  1228. if (!IsEH)
  1229. Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
  1230. int Offset = Instr.getOffset();
  1231. if (IsRelative)
  1232. Offset -= CFAOffset;
  1233. Offset = Offset / dataAlignmentFactor;
  1234. if (Offset < 0) {
  1235. Streamer.emitInt8(dwarf::DW_CFA_offset_extended_sf);
  1236. Streamer.emitULEB128IntValue(Reg);
  1237. Streamer.emitSLEB128IntValue(Offset);
  1238. } else if (Reg < 64) {
  1239. Streamer.emitInt8(dwarf::DW_CFA_offset + Reg);
  1240. Streamer.emitULEB128IntValue(Offset);
  1241. } else {
  1242. Streamer.emitInt8(dwarf::DW_CFA_offset_extended);
  1243. Streamer.emitULEB128IntValue(Reg);
  1244. Streamer.emitULEB128IntValue(Offset);
  1245. }
  1246. return;
  1247. }
  1248. case MCCFIInstruction::OpRememberState:
  1249. Streamer.emitInt8(dwarf::DW_CFA_remember_state);
  1250. return;
  1251. case MCCFIInstruction::OpRestoreState:
  1252. Streamer.emitInt8(dwarf::DW_CFA_restore_state);
  1253. return;
  1254. case MCCFIInstruction::OpSameValue: {
  1255. unsigned Reg = Instr.getRegister();
  1256. Streamer.emitInt8(dwarf::DW_CFA_same_value);
  1257. Streamer.emitULEB128IntValue(Reg);
  1258. return;
  1259. }
  1260. case MCCFIInstruction::OpRestore: {
  1261. unsigned Reg = Instr.getRegister();
  1262. if (!IsEH)
  1263. Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
  1264. if (Reg < 64) {
  1265. Streamer.emitInt8(dwarf::DW_CFA_restore | Reg);
  1266. } else {
  1267. Streamer.emitInt8(dwarf::DW_CFA_restore_extended);
  1268. Streamer.emitULEB128IntValue(Reg);
  1269. }
  1270. return;
  1271. }
  1272. case MCCFIInstruction::OpGnuArgsSize:
  1273. Streamer.emitInt8(dwarf::DW_CFA_GNU_args_size);
  1274. Streamer.emitULEB128IntValue(Instr.getOffset());
  1275. return;
  1276. case MCCFIInstruction::OpEscape:
  1277. Streamer.emitBytes(Instr.getValues());
  1278. return;
  1279. }
  1280. llvm_unreachable("Unhandled case in switch");
  1281. }
  1282. /// Emit frame instructions to describe the layout of the frame.
  1283. void FrameEmitterImpl::emitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
  1284. MCSymbol *BaseLabel) {
  1285. for (const MCCFIInstruction &Instr : Instrs) {
  1286. MCSymbol *Label = Instr.getLabel();
  1287. // Throw out move if the label is invalid.
  1288. if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
  1289. // Advance row if new location.
  1290. if (BaseLabel && Label) {
  1291. MCSymbol *ThisSym = Label;
  1292. if (ThisSym != BaseLabel) {
  1293. Streamer.emitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
  1294. BaseLabel = ThisSym;
  1295. }
  1296. }
  1297. emitCFIInstruction(Instr);
  1298. }
  1299. }
  1300. /// Emit the unwind information in a compact way.
  1301. void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
  1302. MCContext &Context = Streamer.getContext();
  1303. const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
  1304. // range-start range-length compact-unwind-enc personality-func lsda
  1305. // _foo LfooEnd-_foo 0x00000023 0 0
  1306. // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
  1307. //
  1308. // .section __LD,__compact_unwind,regular,debug
  1309. //
  1310. // # compact unwind for _foo
  1311. // .quad _foo
  1312. // .set L1,LfooEnd-_foo
  1313. // .long L1
  1314. // .long 0x01010001
  1315. // .quad 0
  1316. // .quad 0
  1317. //
  1318. // # compact unwind for _bar
  1319. // .quad _bar
  1320. // .set L2,LbarEnd-_bar
  1321. // .long L2
  1322. // .long 0x01020011
  1323. // .quad __gxx_personality
  1324. // .quad except_tab1
  1325. uint32_t Encoding = Frame.CompactUnwindEncoding;
  1326. if (!Encoding) return;
  1327. bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
  1328. // The encoding needs to know we have an LSDA.
  1329. if (!DwarfEHFrameOnly && Frame.Lsda)
  1330. Encoding |= 0x40000000;
  1331. // Range Start
  1332. unsigned FDEEncoding = MOFI->getFDEEncoding();
  1333. unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
  1334. Streamer.emitSymbolValue(Frame.Begin, Size);
  1335. // Range Length
  1336. const MCExpr *Range =
  1337. makeEndMinusStartExpr(Context, *Frame.Begin, *Frame.End, 0);
  1338. emitAbsValue(Streamer, Range, 4);
  1339. // Compact Encoding
  1340. Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
  1341. Streamer.emitIntValue(Encoding, Size);
  1342. // Personality Function
  1343. Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
  1344. if (!DwarfEHFrameOnly && Frame.Personality)
  1345. Streamer.emitSymbolValue(Frame.Personality, Size);
  1346. else
  1347. Streamer.emitIntValue(0, Size); // No personality fn
  1348. // LSDA
  1349. Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
  1350. if (!DwarfEHFrameOnly && Frame.Lsda)
  1351. Streamer.emitSymbolValue(Frame.Lsda, Size);
  1352. else
  1353. Streamer.emitIntValue(0, Size); // No LSDA
  1354. }
  1355. static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
  1356. if (IsEH)
  1357. return 1;
  1358. switch (DwarfVersion) {
  1359. case 2:
  1360. return 1;
  1361. case 3:
  1362. return 3;
  1363. case 4:
  1364. case 5:
  1365. return 4;
  1366. }
  1367. llvm_unreachable("Unknown version");
  1368. }
  1369. const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
  1370. MCContext &context = Streamer.getContext();
  1371. const MCRegisterInfo *MRI = context.getRegisterInfo();
  1372. const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
  1373. MCSymbol *sectionStart = context.createTempSymbol();
  1374. Streamer.emitLabel(sectionStart);
  1375. MCSymbol *sectionEnd = context.createTempSymbol();
  1376. dwarf::DwarfFormat Format = IsEH ? dwarf::DWARF32 : context.getDwarfFormat();
  1377. unsigned UnitLengthBytes = dwarf::getUnitLengthFieldByteSize(Format);
  1378. unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format);
  1379. bool IsDwarf64 = Format == dwarf::DWARF64;
  1380. if (IsDwarf64)
  1381. // DWARF64 mark
  1382. Streamer.emitInt32(dwarf::DW_LENGTH_DWARF64);
  1383. // Length
  1384. const MCExpr *Length = makeEndMinusStartExpr(context, *sectionStart,
  1385. *sectionEnd, UnitLengthBytes);
  1386. emitAbsValue(Streamer, Length, OffsetSize);
  1387. // CIE ID
  1388. uint64_t CIE_ID =
  1389. IsEH ? 0 : (IsDwarf64 ? dwarf::DW64_CIE_ID : dwarf::DW_CIE_ID);
  1390. Streamer.emitIntValue(CIE_ID, OffsetSize);
  1391. // Version
  1392. uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
  1393. Streamer.emitInt8(CIEVersion);
  1394. if (IsEH) {
  1395. SmallString<8> Augmentation;
  1396. Augmentation += "z";
  1397. if (Frame.Personality)
  1398. Augmentation += "P";
  1399. if (Frame.Lsda)
  1400. Augmentation += "L";
  1401. Augmentation += "R";
  1402. if (Frame.IsSignalFrame)
  1403. Augmentation += "S";
  1404. if (Frame.IsBKeyFrame)
  1405. Augmentation += "B";
  1406. if (Frame.IsMTETaggedFrame)
  1407. Augmentation += "G";
  1408. Streamer.emitBytes(Augmentation);
  1409. }
  1410. Streamer.emitInt8(0);
  1411. if (CIEVersion >= 4) {
  1412. // Address Size
  1413. Streamer.emitInt8(context.getAsmInfo()->getCodePointerSize());
  1414. // Segment Descriptor Size
  1415. Streamer.emitInt8(0);
  1416. }
  1417. // Code Alignment Factor
  1418. Streamer.emitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
  1419. // Data Alignment Factor
  1420. Streamer.emitSLEB128IntValue(getDataAlignmentFactor(Streamer));
  1421. // Return Address Register
  1422. unsigned RAReg = Frame.RAReg;
  1423. if (RAReg == static_cast<unsigned>(INT_MAX))
  1424. RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
  1425. if (CIEVersion == 1) {
  1426. assert(RAReg <= 255 &&
  1427. "DWARF 2 encodes return_address_register in one byte");
  1428. Streamer.emitInt8(RAReg);
  1429. } else {
  1430. Streamer.emitULEB128IntValue(RAReg);
  1431. }
  1432. // Augmentation Data Length (optional)
  1433. unsigned augmentationLength = 0;
  1434. if (IsEH) {
  1435. if (Frame.Personality) {
  1436. // Personality Encoding
  1437. augmentationLength += 1;
  1438. // Personality
  1439. augmentationLength +=
  1440. getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
  1441. }
  1442. if (Frame.Lsda)
  1443. augmentationLength += 1;
  1444. // Encoding of the FDE pointers
  1445. augmentationLength += 1;
  1446. Streamer.emitULEB128IntValue(augmentationLength);
  1447. // Augmentation Data (optional)
  1448. if (Frame.Personality) {
  1449. // Personality Encoding
  1450. emitEncodingByte(Streamer, Frame.PersonalityEncoding);
  1451. // Personality
  1452. EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
  1453. }
  1454. if (Frame.Lsda)
  1455. emitEncodingByte(Streamer, Frame.LsdaEncoding);
  1456. // Encoding of the FDE pointers
  1457. emitEncodingByte(Streamer, MOFI->getFDEEncoding());
  1458. }
  1459. // Initial Instructions
  1460. const MCAsmInfo *MAI = context.getAsmInfo();
  1461. if (!Frame.IsSimple) {
  1462. const std::vector<MCCFIInstruction> &Instructions =
  1463. MAI->getInitialFrameState();
  1464. emitCFIInstructions(Instructions, nullptr);
  1465. }
  1466. InitialCFAOffset = CFAOffset;
  1467. // Padding
  1468. Streamer.emitValueToAlignment(Align(IsEH ? 4 : MAI->getCodePointerSize()));
  1469. Streamer.emitLabel(sectionEnd);
  1470. return *sectionStart;
  1471. }
  1472. void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
  1473. const MCDwarfFrameInfo &frame,
  1474. bool LastInSection,
  1475. const MCSymbol &SectionStart) {
  1476. MCContext &context = Streamer.getContext();
  1477. MCSymbol *fdeStart = context.createTempSymbol();
  1478. MCSymbol *fdeEnd = context.createTempSymbol();
  1479. const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
  1480. CFAOffset = InitialCFAOffset;
  1481. dwarf::DwarfFormat Format = IsEH ? dwarf::DWARF32 : context.getDwarfFormat();
  1482. unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format);
  1483. if (Format == dwarf::DWARF64)
  1484. // DWARF64 mark
  1485. Streamer.emitInt32(dwarf::DW_LENGTH_DWARF64);
  1486. // Length
  1487. const MCExpr *Length = makeEndMinusStartExpr(context, *fdeStart, *fdeEnd, 0);
  1488. emitAbsValue(Streamer, Length, OffsetSize);
  1489. Streamer.emitLabel(fdeStart);
  1490. // CIE Pointer
  1491. const MCAsmInfo *asmInfo = context.getAsmInfo();
  1492. if (IsEH) {
  1493. const MCExpr *offset =
  1494. makeEndMinusStartExpr(context, cieStart, *fdeStart, 0);
  1495. emitAbsValue(Streamer, offset, OffsetSize);
  1496. } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
  1497. const MCExpr *offset =
  1498. makeEndMinusStartExpr(context, SectionStart, cieStart, 0);
  1499. emitAbsValue(Streamer, offset, OffsetSize);
  1500. } else {
  1501. Streamer.emitSymbolValue(&cieStart, OffsetSize,
  1502. asmInfo->needsDwarfSectionOffsetDirective());
  1503. }
  1504. // PC Begin
  1505. unsigned PCEncoding =
  1506. IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
  1507. unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
  1508. emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
  1509. // PC Range
  1510. const MCExpr *Range =
  1511. makeEndMinusStartExpr(context, *frame.Begin, *frame.End, 0);
  1512. emitAbsValue(Streamer, Range, PCSize);
  1513. if (IsEH) {
  1514. // Augmentation Data Length
  1515. unsigned augmentationLength = 0;
  1516. if (frame.Lsda)
  1517. augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
  1518. Streamer.emitULEB128IntValue(augmentationLength);
  1519. // Augmentation Data
  1520. if (frame.Lsda)
  1521. emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
  1522. }
  1523. // Call Frame Instructions
  1524. emitCFIInstructions(frame.Instructions, frame.Begin);
  1525. // Padding
  1526. // The size of a .eh_frame section has to be a multiple of the alignment
  1527. // since a null CIE is interpreted as the end. Old systems overaligned
  1528. // .eh_frame, so we do too and account for it in the last FDE.
  1529. unsigned Alignment = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
  1530. Streamer.emitValueToAlignment(Align(Alignment));
  1531. Streamer.emitLabel(fdeEnd);
  1532. }
  1533. namespace {
  1534. struct CIEKey {
  1535. static const CIEKey getEmptyKey() {
  1536. return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX),
  1537. false, false);
  1538. }
  1539. static const CIEKey getTombstoneKey() {
  1540. return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX),
  1541. false, false);
  1542. }
  1543. CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
  1544. unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
  1545. unsigned RAReg, bool IsBKeyFrame, bool IsMTETaggedFrame)
  1546. : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
  1547. LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
  1548. IsSimple(IsSimple), RAReg(RAReg), IsBKeyFrame(IsBKeyFrame),
  1549. IsMTETaggedFrame(IsMTETaggedFrame) {}
  1550. explicit CIEKey(const MCDwarfFrameInfo &Frame)
  1551. : Personality(Frame.Personality),
  1552. PersonalityEncoding(Frame.PersonalityEncoding),
  1553. LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
  1554. IsSimple(Frame.IsSimple), RAReg(Frame.RAReg),
  1555. IsBKeyFrame(Frame.IsBKeyFrame),
  1556. IsMTETaggedFrame(Frame.IsMTETaggedFrame) {}
  1557. StringRef PersonalityName() const {
  1558. if (!Personality)
  1559. return StringRef();
  1560. return Personality->getName();
  1561. }
  1562. bool operator<(const CIEKey &Other) const {
  1563. return std::make_tuple(PersonalityName(), PersonalityEncoding, LsdaEncoding,
  1564. IsSignalFrame, IsSimple, RAReg, IsBKeyFrame,
  1565. IsMTETaggedFrame) <
  1566. std::make_tuple(Other.PersonalityName(), Other.PersonalityEncoding,
  1567. Other.LsdaEncoding, Other.IsSignalFrame,
  1568. Other.IsSimple, Other.RAReg, Other.IsBKeyFrame,
  1569. Other.IsMTETaggedFrame);
  1570. }
  1571. const MCSymbol *Personality;
  1572. unsigned PersonalityEncoding;
  1573. unsigned LsdaEncoding;
  1574. bool IsSignalFrame;
  1575. bool IsSimple;
  1576. unsigned RAReg;
  1577. bool IsBKeyFrame;
  1578. bool IsMTETaggedFrame;
  1579. };
  1580. } // end anonymous namespace
  1581. namespace llvm {
  1582. template <> struct DenseMapInfo<CIEKey> {
  1583. static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
  1584. static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
  1585. static unsigned getHashValue(const CIEKey &Key) {
  1586. return static_cast<unsigned>(
  1587. hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
  1588. Key.IsSignalFrame, Key.IsSimple, Key.RAReg,
  1589. Key.IsBKeyFrame, Key.IsMTETaggedFrame));
  1590. }
  1591. static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
  1592. return LHS.Personality == RHS.Personality &&
  1593. LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
  1594. LHS.LsdaEncoding == RHS.LsdaEncoding &&
  1595. LHS.IsSignalFrame == RHS.IsSignalFrame &&
  1596. LHS.IsSimple == RHS.IsSimple && LHS.RAReg == RHS.RAReg &&
  1597. LHS.IsBKeyFrame == RHS.IsBKeyFrame &&
  1598. LHS.IsMTETaggedFrame == RHS.IsMTETaggedFrame;
  1599. }
  1600. };
  1601. } // end namespace llvm
  1602. void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
  1603. bool IsEH) {
  1604. MCContext &Context = Streamer.getContext();
  1605. const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
  1606. const MCAsmInfo *AsmInfo = Context.getAsmInfo();
  1607. FrameEmitterImpl Emitter(IsEH, Streamer);
  1608. ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
  1609. // Emit the compact unwind info if available.
  1610. bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
  1611. if (IsEH && MOFI->getCompactUnwindSection()) {
  1612. Streamer.generateCompactUnwindEncodings(MAB);
  1613. bool SectionEmitted = false;
  1614. for (const MCDwarfFrameInfo &Frame : FrameArray) {
  1615. if (Frame.CompactUnwindEncoding == 0) continue;
  1616. if (!SectionEmitted) {
  1617. Streamer.switchSection(MOFI->getCompactUnwindSection());
  1618. Streamer.emitValueToAlignment(Align(AsmInfo->getCodePointerSize()));
  1619. SectionEmitted = true;
  1620. }
  1621. NeedsEHFrameSection |=
  1622. Frame.CompactUnwindEncoding ==
  1623. MOFI->getCompactUnwindDwarfEHFrameOnly();
  1624. Emitter.EmitCompactUnwind(Frame);
  1625. }
  1626. }
  1627. if (!NeedsEHFrameSection) return;
  1628. MCSection &Section =
  1629. IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
  1630. : *MOFI->getDwarfFrameSection();
  1631. Streamer.switchSection(&Section);
  1632. MCSymbol *SectionStart = Context.createTempSymbol();
  1633. Streamer.emitLabel(SectionStart);
  1634. DenseMap<CIEKey, const MCSymbol *> CIEStarts;
  1635. const MCSymbol *DummyDebugKey = nullptr;
  1636. bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
  1637. // Sort the FDEs by their corresponding CIE before we emit them.
  1638. // This isn't technically necessary according to the DWARF standard,
  1639. // but the Android libunwindstack rejects eh_frame sections where
  1640. // an FDE refers to a CIE other than the closest previous CIE.
  1641. std::vector<MCDwarfFrameInfo> FrameArrayX(FrameArray.begin(), FrameArray.end());
  1642. llvm::stable_sort(FrameArrayX,
  1643. [](const MCDwarfFrameInfo &X, const MCDwarfFrameInfo &Y) {
  1644. return CIEKey(X) < CIEKey(Y);
  1645. });
  1646. for (auto I = FrameArrayX.begin(), E = FrameArrayX.end(); I != E;) {
  1647. const MCDwarfFrameInfo &Frame = *I;
  1648. ++I;
  1649. if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
  1650. MOFI->getCompactUnwindDwarfEHFrameOnly())
  1651. // Don't generate an EH frame if we don't need one. I.e., it's taken care
  1652. // of by the compact unwind encoding.
  1653. continue;
  1654. CIEKey Key(Frame);
  1655. const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
  1656. if (!CIEStart)
  1657. CIEStart = &Emitter.EmitCIE(Frame);
  1658. Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
  1659. }
  1660. }
  1661. void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
  1662. uint64_t AddrDelta) {
  1663. MCContext &Context = Streamer.getContext();
  1664. SmallString<256> Tmp;
  1665. raw_svector_ostream OS(Tmp);
  1666. MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
  1667. Streamer.emitBytes(OS.str());
  1668. }
  1669. void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
  1670. uint64_t AddrDelta,
  1671. raw_ostream &OS) {
  1672. // Scale the address delta by the minimum instruction length.
  1673. AddrDelta = ScaleAddrDelta(Context, AddrDelta);
  1674. if (AddrDelta == 0)
  1675. return;
  1676. support::endianness E =
  1677. Context.getAsmInfo()->isLittleEndian() ? support::little : support::big;
  1678. if (isUIntN(6, AddrDelta)) {
  1679. uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
  1680. OS << Opcode;
  1681. } else if (isUInt<8>(AddrDelta)) {
  1682. OS << uint8_t(dwarf::DW_CFA_advance_loc1);
  1683. OS << uint8_t(AddrDelta);
  1684. } else if (isUInt<16>(AddrDelta)) {
  1685. OS << uint8_t(dwarf::DW_CFA_advance_loc2);
  1686. support::endian::write<uint16_t>(OS, AddrDelta, E);
  1687. } else {
  1688. assert(isUInt<32>(AddrDelta));
  1689. OS << uint8_t(dwarf::DW_CFA_advance_loc4);
  1690. support::endian::write<uint32_t>(OS, AddrDelta, E);
  1691. }
  1692. }