Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===// |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing dwarf debug info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | b14490d | 2010-03-09 00:39:24 +0000 | [diff] [blame] | 13 | |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 14 | #include "DwarfDebug.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 15 | #include "ByteStreamer.h" |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 16 | #include "DIEHash.h" |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 17 | #include "DebugLocEntry.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 18 | #include "DebugLocStream.h" |
| 19 | #include "DwarfAccelTable.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 20 | #include "DwarfCompileUnit.h" |
| 21 | #include "DwarfExpression.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 22 | #include "DwarfFile.h" |
David Blaikie | 2c86a72 | 2013-12-02 19:33:15 +0000 | [diff] [blame] | 23 | #include "DwarfUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
| 25 | #include "llvm/ADT/DenseMap.h" |
| 26 | #include "llvm/ADT/DenseSet.h" |
| 27 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
| 30 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 33 | #include "llvm/BinaryFormat/Dwarf.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | e541e0b | 2015-01-05 21:29:41 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/DIE.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/LexicalScopes.h" |
| 37 | #include "llvm/CodeGen/MachineBasicBlock.h" |
David Greene | 829b3e8 | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 43 | #include "llvm/IR/DebugInfoMetadata.h" |
| 44 | #include "llvm/IR/DebugLoc.h" |
| 45 | #include "llvm/IR/Function.h" |
| 46 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 47 | #include "llvm/IR/Module.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 49 | #include "llvm/MC/MCContext.h" |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 50 | #include "llvm/MC/MCDwarf.h" |
Chris Lattner | 4d2c0f9 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 51 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 4b7dadb | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 52 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 53 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 54 | #include "llvm/MC/MCTargetOptions.h" |
| 55 | #include "llvm/MC/MachineLocation.h" |
| 56 | #include "llvm/MC/SectionKind.h" |
| 57 | #include "llvm/Pass.h" |
| 58 | #include "llvm/Support/Casting.h" |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 59 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | cdf01b5 | 2009-10-13 06:47:08 +0000 | [diff] [blame] | 60 | #include "llvm/Support/Debug.h" |
| 61 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 6764643 | 2013-07-26 17:02:41 +0000 | [diff] [blame] | 62 | #include "llvm/Support/MD5.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 63 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 65 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 66 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 67 | #include "llvm/Target/TargetMachine.h" |
| 68 | #include "llvm/Target/TargetOptions.h" |
| 69 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 70 | #include "llvm/Target/TargetSubtargetInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 71 | #include <algorithm> |
| 72 | #include <cassert> |
| 73 | #include <cstddef> |
| 74 | #include <cstdint> |
| 75 | #include <iterator> |
| 76 | #include <string> |
| 77 | #include <utility> |
| 78 | #include <vector> |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 79 | |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 80 | using namespace llvm; |
| 81 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 82 | #define DEBUG_TYPE "dwarfdebug" |
| 83 | |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 84 | static cl::opt<bool> |
| 85 | DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, |
| 86 | cl::desc("Disable debug info printing")); |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 87 | |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 88 | static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier( |
| 89 | "use-dwarf-ranges-base-address-specifier", cl::Hidden, |
David Blaikie | c2be863 | 2017-08-01 14:50:50 +0000 | [diff] [blame] | 90 | cl::desc("Use base address specifiers in debug_ranges"), cl::init(false)); |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 91 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 92 | static cl::opt<bool> |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 93 | GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden, |
| 94 | cl::desc("Generate GNU-style pubnames and pubtypes"), |
| 95 | cl::init(false)); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 96 | |
Eric Christopher | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 97 | static cl::opt<bool> GenerateARangeSection("generate-arange-section", |
| 98 | cl::Hidden, |
| 99 | cl::desc("Generate dwarf aranges"), |
| 100 | cl::init(false)); |
| 101 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 102 | static cl::opt<bool> SplitDwarfCrossCuReferences( |
| 103 | "split-dwarf-cross-cu-references", cl::Hidden, |
| 104 | cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); |
| 105 | |
Eric Christopher | f07ee3a | 2014-01-27 23:50:03 +0000 | [diff] [blame] | 106 | enum DefaultOnOff { Default, Enable, Disable }; |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 107 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 108 | static cl::opt<DefaultOnOff> UnknownLocations( |
| 109 | "use-unknown-locations", cl::Hidden, |
| 110 | cl::desc("Make an absence of debug location information explicit."), |
| 111 | cl::values(clEnumVal(Default, "At top of block or after label"), |
| 112 | clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), |
| 113 | cl::init(Default)); |
| 114 | |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 115 | static cl::opt<DefaultOnOff> |
| 116 | DwarfAccelTables("dwarf-accel-tables", cl::Hidden, |
| 117 | cl::desc("Output prototype dwarf accelerator tables."), |
| 118 | cl::values(clEnumVal(Default, "Default for platform"), |
| 119 | clEnumVal(Enable, "Enabled"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 120 | clEnumVal(Disable, "Disabled")), |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 121 | cl::init(Default)); |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 122 | |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 123 | static cl::opt<DefaultOnOff> |
Eric Christopher | 4d36ca0 | 2013-08-26 23:24:35 +0000 | [diff] [blame] | 124 | DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden, |
| 125 | cl::desc("Generate DWARF pubnames and pubtypes sections"), |
| 126 | cl::values(clEnumVal(Default, "Default for platform"), |
| 127 | clEnumVal(Enable, "Enabled"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 128 | clEnumVal(Disable, "Disabled")), |
Eric Christopher | 4d36ca0 | 2013-08-26 23:24:35 +0000 | [diff] [blame] | 129 | cl::init(Default)); |
Eric Christopher | 7da2488 | 2013-08-19 21:07:38 +0000 | [diff] [blame] | 130 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 131 | enum LinkageNameOption { |
| 132 | DefaultLinkageNames, |
| 133 | AllLinkageNames, |
| 134 | AbstractLinkageNames |
| 135 | }; |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 136 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 137 | static cl::opt<LinkageNameOption> |
| 138 | DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, |
| 139 | cl::desc("Which DWARF linkage-name attributes to emit."), |
| 140 | cl::values(clEnumValN(DefaultLinkageNames, "Default", |
| 141 | "Default for platform"), |
| 142 | clEnumValN(AllLinkageNames, "All", "All"), |
| 143 | clEnumValN(AbstractLinkageNames, "Abstract", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 144 | "Abstract subprograms")), |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 145 | cl::init(DefaultLinkageNames)); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 146 | |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 147 | static const char *const DWARFGroupName = "dwarf"; |
| 148 | static const char *const DWARFGroupDescription = "DWARF Emission"; |
| 149 | static const char *const DbgTimerName = "writer"; |
| 150 | static const char *const DbgTimerDescription = "DWARF Debug Writer"; |
Bill Wendling | fcc1414 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 151 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 152 | void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 153 | BS.EmitInt8( |
| 154 | Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) |
| 155 | : dwarf::OperationEncodingString(Op)); |
| 156 | } |
| 157 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 158 | void DebugLocDwarfExpression::emitSigned(int64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 159 | BS.EmitSLEB128(Value, Twine(Value)); |
| 160 | } |
| 161 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 162 | void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 163 | BS.EmitULEB128(Value, Twine(Value)); |
| 164 | } |
| 165 | |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 166 | bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, |
| 167 | unsigned MachineReg) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 168 | // This information is not available while emitting .debug_loc entries. |
| 169 | return false; |
| 170 | } |
| 171 | |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 172 | bool DbgVariable::isBlockByrefVariable() const { |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 173 | assert(Var && "Invalid complex DbgVariable!"); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 174 | return Var->getType().resolve()->isBlockByrefStruct(); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 177 | const DIType *DbgVariable::getType() const { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 178 | DIType *Ty = Var->getType().resolve(); |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 179 | // FIXME: isBlockByrefVariable should be reformulated in terms of complex |
| 180 | // addresses instead. |
Duncan P. N. Exon Smith | 571baeb | 2015-04-14 01:59:58 +0000 | [diff] [blame] | 181 | if (Ty->isBlockByrefStruct()) { |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 182 | /* Byref variables, in Blocks, are declared by the programmer as |
| 183 | "SomeType VarName;", but the compiler creates a |
| 184 | __Block_byref_x_VarName struct, and gives the variable VarName |
| 185 | either the struct, or a pointer to the struct, as its type. This |
| 186 | is necessary for various behind-the-scenes things the compiler |
| 187 | needs to do with by-reference variables in blocks. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 188 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 189 | However, as far as the original *programmer* is concerned, the |
| 190 | variable should still have type 'SomeType', as originally declared. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 191 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 192 | The following function dives into the __Block_byref_x_VarName |
| 193 | struct to find the original type of the variable. This will be |
| 194 | passed back to the code generating the type for the Debug |
| 195 | Information Entry for the variable 'VarName'. 'VarName' will then |
| 196 | have the original type 'SomeType' in its debug information. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 197 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 198 | The original type 'SomeType' will be the type of the field named |
| 199 | 'VarName' inside the __Block_byref_x_VarName struct. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 200 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 201 | NOTE: In order for this to not completely fail on the debugger |
| 202 | side, the Debug Information Entry for the variable VarName needs to |
| 203 | have a DW_AT_location that tells the debugger how to unwind through |
| 204 | the pointers and __Block_byref_x_VarName struct to find the actual |
| 205 | value of the variable. The function addBlockByrefType does this. */ |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 206 | DIType *subType = Ty; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 207 | uint16_t tag = Ty->getTag(); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 208 | |
Eric Christopher | 9adc55f | 2013-09-04 19:53:21 +0000 | [diff] [blame] | 209 | if (tag == dwarf::DW_TAG_pointer_type) |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 210 | subType = resolve(cast<DIDerivedType>(Ty)->getBaseType()); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 211 | |
Duncan P. N. Exon Smith | 6ac940d | 2015-07-24 18:17:17 +0000 | [diff] [blame] | 212 | auto Elements = cast<DICompositeType>(subType)->getElements(); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 213 | for (unsigned i = 0, N = Elements.size(); i < N; ++i) { |
Duncan P. N. Exon Smith | 099ea1c | 2015-07-24 18:58:32 +0000 | [diff] [blame] | 214 | auto *DT = cast<DIDerivedType>(Elements[i]); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 215 | if (getName() == DT->getName()) |
Duncan P. N. Exon Smith | 848af38 | 2015-04-20 18:20:03 +0000 | [diff] [blame] | 216 | return resolve(DT->getBaseType()); |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 217 | } |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 218 | } |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 219 | return Ty; |
| 220 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 221 | |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 222 | ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 223 | if (FrameIndexExprs.size() == 1) |
| 224 | return FrameIndexExprs; |
| 225 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 226 | assert(llvm::all_of(FrameIndexExprs, |
| 227 | [](const FrameIndexExpr &A) { |
| 228 | return A.Expr->isFragment(); |
| 229 | }) && |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 230 | "multiple FI expressions without DW_OP_LLVM_fragment"); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 231 | std::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(), |
| 232 | [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { |
| 233 | return A.Expr->getFragmentInfo()->OffsetInBits < |
| 234 | B.Expr->getFragmentInfo()->OffsetInBits; |
| 235 | }); |
| 236 | return FrameIndexExprs; |
| 237 | } |
| 238 | |
George Burgess IV | 381fc0e | 2016-08-25 01:05:08 +0000 | [diff] [blame] | 239 | static const DwarfAccelTable::Atom TypeAtoms[] = { |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 240 | DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4), |
| 241 | DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2), |
| 242 | DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)}; |
| 243 | |
Chris Lattner | f0d6bd3 | 2010-04-05 05:11:15 +0000 | [diff] [blame] | 244 | DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 245 | : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), |
| 246 | InfoHolder(A, "info_string", DIEValueAllocator), |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 247 | SkeletonHolder(A, "skel_string", DIEValueAllocator), |
Eric Christopher | 364dbe0 | 2016-10-01 01:50:29 +0000 | [diff] [blame] | 248 | IsDarwin(A->TM.getTargetTriple().isOSDarwin()), |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 249 | AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 250 | dwarf::DW_FORM_data4)), |
| 251 | AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 252 | dwarf::DW_FORM_data4)), |
| 253 | AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 254 | dwarf::DW_FORM_data4)), |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 255 | AccelTypes(TypeAtoms) { |
Eric Christopher | 364dbe0 | 2016-10-01 01:50:29 +0000 | [diff] [blame] | 256 | const Triple &TT = Asm->TM.getTargetTriple(); |
Eric Christopher | ad9fe89 | 2012-04-02 17:58:52 +0000 | [diff] [blame] | 257 | |
Paul Robinson | 6c27a2c | 2015-12-16 19:58:30 +0000 | [diff] [blame] | 258 | // Make sure we know our "debugger tuning." The target option takes |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 259 | // precedence; fall back to triple-based defaults. |
Paul Robinson | 6c27a2c | 2015-12-16 19:58:30 +0000 | [diff] [blame] | 260 | if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) |
| 261 | DebuggerTuning = Asm->TM.Options.DebuggerTuning; |
Dimitry Andric | 2c36421 | 2016-01-07 22:09:12 +0000 | [diff] [blame] | 262 | else if (IsDarwin) |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 263 | DebuggerTuning = DebuggerKind::LLDB; |
| 264 | else if (TT.isPS4CPU()) |
| 265 | DebuggerTuning = DebuggerKind::SCE; |
| 266 | else |
| 267 | DebuggerTuning = DebuggerKind::GDB; |
| 268 | |
| 269 | // Turn on accelerator tables for LLDB by default. |
Eric Christopher | 574b5c8 | 2013-08-19 21:41:38 +0000 | [diff] [blame] | 270 | if (DwarfAccelTables == Default) |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 271 | HasDwarfAccelTables = tuneForLLDB(); |
Eric Christopher | 574b5c8 | 2013-08-19 21:41:38 +0000 | [diff] [blame] | 272 | else |
Eric Christopher | 5297df0 | 2013-08-26 20:58:35 +0000 | [diff] [blame] | 273 | HasDwarfAccelTables = DwarfAccelTables == Enable; |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 274 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 275 | HasAppleExtensionAttributes = tuneForLLDB(); |
| 276 | |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 277 | // Handle split DWARF. |
| 278 | HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); |
Eric Christopher | 2942431 | 2012-11-12 22:22:20 +0000 | [diff] [blame] | 279 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 280 | // SCE defaults to linkage names only for abstract subprograms. |
| 281 | if (DwarfLinkageNames == DefaultLinkageNames) |
| 282 | UseAllLinkageNames = !tuneForSCE(); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 283 | else |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 284 | UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 285 | |
Eric Christopher | 4c5bff3 | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 286 | unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 287 | unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber |
Eric Christopher | 793c747 | 2014-04-28 20:42:22 +0000 | [diff] [blame] | 288 | : MMI->getModule()->getDwarfVersion(); |
Reid Kleckner | 12d2c12 | 2015-08-05 22:26:20 +0000 | [diff] [blame] | 289 | // Use dwarf 4 by default if nothing is requested. |
| 290 | DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION; |
Manman Ren | ac8062b | 2013-07-02 23:40:10 +0000 | [diff] [blame] | 291 | |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 292 | // Work around a GDB bug. GDB doesn't support the standard opcode; |
| 293 | // SCE doesn't support GNU's; LLDB prefers the standard opcode, which |
| 294 | // is defined as of DWARF 3. |
| 295 | // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented |
| 296 | // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 |
| 297 | UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; |
Paul Robinson | 78cc082 | 2015-03-04 20:55:11 +0000 | [diff] [blame] | 298 | |
Adrian Prantl | 6323ddf | 2016-05-17 21:07:16 +0000 | [diff] [blame] | 299 | // GDB does not fully support the DWARF 4 representation for bitfields. |
| 300 | UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); |
| 301 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 302 | Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 303 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 304 | |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 305 | // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 306 | DwarfDebug::~DwarfDebug() = default; |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 307 | |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 308 | static bool isObjCClass(StringRef Name) { |
| 309 | return Name.startswith("+") || Name.startswith("-"); |
| 310 | } |
| 311 | |
| 312 | static bool hasObjCCategory(StringRef Name) { |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 313 | if (!isObjCClass(Name)) |
| 314 | return false; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 315 | |
Benjamin Kramer | 260de74 | 2013-08-24 12:15:54 +0000 | [diff] [blame] | 316 | return Name.find(") ") != StringRef::npos; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void getObjCClassCategory(StringRef In, StringRef &Class, |
| 320 | StringRef &Category) { |
| 321 | if (!hasObjCCategory(In)) { |
| 322 | Class = In.slice(In.find('[') + 1, In.find(' ')); |
| 323 | Category = ""; |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | Class = In.slice(In.find('[') + 1, In.find('(')); |
| 328 | Category = In.slice(In.find('[') + 1, In.find(' ')); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static StringRef getObjCMethodName(StringRef In) { |
| 332 | return In.slice(In.find(' ') + 1, In.find(']')); |
| 333 | } |
| 334 | |
| 335 | // Add the various names to the Dwarf accelerator table names. |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 336 | // TODO: Determine whether or not we should add names for programs |
| 337 | // that do not have a DW_AT_name or DW_AT_linkage_name field - this |
| 338 | // is only slightly different than the lookup of non-standard ObjC names. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 339 | void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 340 | if (!SP->isDefinition()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 341 | return; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 342 | addAccelName(SP->getName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 343 | |
| 344 | // If the linkage name is different than the name, go ahead and output |
| 345 | // that as well into the name table. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 346 | if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName()) |
| 347 | addAccelName(SP->getLinkageName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 348 | |
| 349 | // If this is an Objective-C selector name add it to the ObjC accelerator |
| 350 | // too. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 351 | if (isObjCClass(SP->getName())) { |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 352 | StringRef Class, Category; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 353 | getObjCClassCategory(SP->getName(), Class, Category); |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 354 | addAccelObjC(Class, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 355 | if (Category != "") |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 356 | addAccelObjC(Category, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 357 | // Also add the base method name to the name table. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 358 | addAccelName(getObjCMethodName(SP->getName()), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 362 | /// Check whether we should create a DIE for the given Scope, return true |
| 363 | /// if we don't create a DIE (the corresponding DIE is null). |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 364 | bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { |
| 365 | if (Scope->isAbstractScope()) |
| 366 | return false; |
| 367 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 368 | // We don't create a DIE if there is no Range. |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 369 | const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); |
| 370 | if (Ranges.empty()) |
| 371 | return true; |
| 372 | |
| 373 | if (Ranges.size() > 1) |
| 374 | return false; |
| 375 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 376 | // We don't create a DIE if we have a single Range and the end label |
| 377 | // is null. |
David Blaikie | cd4b8a2 | 2014-08-31 02:14:26 +0000 | [diff] [blame] | 378 | return !getLabelAfterInsn(Ranges.front().second); |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 381 | template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 382 | F(CU); |
| 383 | if (auto *SkelCU = CU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 384 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 385 | F(*SkelCU); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 386 | } |
| 387 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 388 | bool DwarfDebug::shareAcrossDWOCUs() const { |
| 389 | return SplitDwarfCrossCuReferences; |
| 390 | } |
| 391 | |
| 392 | void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, |
| 393 | LexicalScope *Scope) { |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 394 | assert(Scope && Scope->getScopeNode()); |
| 395 | assert(Scope->isAbstractScope()); |
| 396 | assert(!Scope->getInlinedAt()); |
| 397 | |
David Blaikie | b461468 | 2016-12-14 19:38:39 +0000 | [diff] [blame] | 398 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 399 | |
David Blaikie | 1ea9db2 | 2014-05-21 23:14:12 +0000 | [diff] [blame] | 400 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 401 | // was inlined from another compile unit. |
Mehdi Amini | 4181205 | 2017-05-29 06:25:30 +0000 | [diff] [blame] | 402 | if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) |
| 403 | // Avoid building the original CU if it won't be used |
| 404 | SrcCU.constructAbstractSubprogramScopeDIE(Scope); |
| 405 | else { |
| 406 | auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
| 407 | if (auto *SkelCU = CU.getSkeleton()) { |
| 408 | (shareAcrossDWOCUs() ? CU : SrcCU) |
| 409 | .constructAbstractSubprogramScopeDIE(Scope); |
| 410 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 411 | SkelCU->constructAbstractSubprogramScopeDIE(Scope); |
| 412 | } else |
| 413 | CU.constructAbstractSubprogramScopeDIE(Scope); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 414 | } |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 415 | } |
| 416 | |
David Blaikie | b3cee2f | 2017-05-25 18:50:28 +0000 | [diff] [blame] | 417 | bool DwarfDebug::hasDwarfPubSections(bool includeMinimalInlineScopes) const { |
| 418 | // Opting in to GNU Pubnames/types overrides the default to ensure these are |
| 419 | // generated for things like Gold's gdb_index generation. |
| 420 | if (GenerateGnuPubSections) |
| 421 | return true; |
| 422 | |
| 423 | if (DwarfPubSections == Default) |
| 424 | return tuneForGDB() && !includeMinimalInlineScopes; |
| 425 | |
| 426 | return DwarfPubSections == Enable; |
| 427 | } |
| 428 | |
| 429 | void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { |
| 430 | if (!hasDwarfPubSections(U.includeMinimalInlineScopes())) |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 431 | return; |
| 432 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 433 | U.addFlag(D, dwarf::DW_AT_GNU_pubnames); |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 436 | // Create new DwarfCompileUnit for the given metadata node with tag |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 437 | // DW_TAG_compile_unit. |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 438 | DwarfCompileUnit & |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 439 | DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { |
| 440 | if (auto *CU = CUMap.lookup(DIUnit)) |
| 441 | return *CU; |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 442 | StringRef FN = DIUnit->getFilename(); |
| 443 | CompilationDir = DIUnit->getDirectory(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 444 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 445 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
David Blaikie | bd57905 | 2014-04-28 21:14:27 +0000 | [diff] [blame] | 446 | InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 447 | DwarfCompileUnit &NewCU = *OwnedUnit; |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 448 | DIE &Die = NewCU.getUnitDie(); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 449 | InfoHolder.addUnit(std::move(OwnedUnit)); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 450 | if (useSplitDwarf()) { |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 451 | NewCU.setSkeleton(constructSkeletonCU(NewCU)); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 452 | NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 453 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 454 | } |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 455 | |
David Blaikie | 89daf77 | 2017-07-28 03:06:25 +0000 | [diff] [blame] | 456 | for (auto *IE : DIUnit->getImportedEntities()) |
| 457 | NewCU.addImportedEntity(IE); |
| 458 | |
David Blaikie | 7ac5149 | 2014-03-20 17:05:45 +0000 | [diff] [blame] | 459 | // LTO with assembly output shares a single line table amongst multiple CUs. |
| 460 | // To avoid the compilation directory being ambiguous, let the line table |
| 461 | // explicitly describe the directory of all files, never relying on the |
| 462 | // compilation directory. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 463 | if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) |
| 464 | Asm->OutStreamer->getContext().setMCLineTableCompilationDir( |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 465 | NewCU.getUniqueID(), CompilationDir); |
David Blaikie | 2666e24 | 2013-12-06 19:38:46 +0000 | [diff] [blame] | 466 | |
Eric Christopher | 9ea300f | 2017-03-29 23:34:27 +0000 | [diff] [blame] | 467 | StringRef Producer = DIUnit->getProducer(); |
| 468 | StringRef Flags = DIUnit->getFlags(); |
| 469 | if (!Flags.empty()) { |
| 470 | std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); |
| 471 | NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); |
| 472 | } else |
| 473 | NewCU.addString(Die, dwarf::DW_AT_producer, Producer); |
| 474 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 475 | NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 476 | DIUnit->getSourceLanguage()); |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 477 | NewCU.addString(Die, dwarf::DW_AT_name, FN); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 478 | |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 479 | if (!useSplitDwarf()) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 480 | NewCU.initStmtList(); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 481 | |
| 482 | // If we're using split dwarf the compilation dir is going to be in the |
| 483 | // skeleton CU and so we don't need to duplicate it here. |
| 484 | if (!CompilationDir.empty()) |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 485 | NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 486 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 487 | addGnuPubAttributes(NewCU, Die); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 488 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 489 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 490 | if (useAppleExtensionAttributes()) { |
| 491 | if (DIUnit->isOptimized()) |
| 492 | NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 493 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 494 | StringRef Flags = DIUnit->getFlags(); |
| 495 | if (!Flags.empty()) |
| 496 | NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 497 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 498 | if (unsigned RVer = DIUnit->getRuntimeVersion()) |
| 499 | NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
| 500 | dwarf::DW_FORM_data1, RVer); |
| 501 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 502 | |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 503 | if (useSplitDwarf()) |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 504 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 505 | else |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 506 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
Eric Christopher | d039baa | 2013-12-30 03:40:32 +0000 | [diff] [blame] | 507 | |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 508 | if (DIUnit->getDWOId()) { |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 509 | // This CU is either a clang module DWO or a skeleton CU. |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 510 | NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, |
| 511 | DIUnit->getDWOId()); |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 512 | if (!DIUnit->getSplitDebugFilename().empty()) |
| 513 | // This is a prefabricated skeleton CU. |
| 514 | NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, |
| 515 | DIUnit->getSplitDebugFilename()); |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 516 | } |
| 517 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 518 | CUMap.insert({DIUnit, &NewCU}); |
| 519 | CUDieMap.insert({&Die, &NewCU}); |
Devang Patel | eb1bb4e | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 520 | return NewCU; |
Devang Patel | 1a0df9a | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 521 | } |
| 522 | |
David Blaikie | 8912df1 | 2014-08-31 05:41:15 +0000 | [diff] [blame] | 523 | void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 524 | const DIImportedEntity *N) { |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 525 | if (isa<DILocalScope>(N->getScope())) |
| 526 | return; |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 527 | if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) |
| 528 | D->addChild(TheCU.constructImportedEntityDIE(N)); |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 531 | /// Sort and unique GVEs by comparing their fragment offset. |
| 532 | static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & |
| 533 | sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { |
| 534 | std::sort(GVEs.begin(), GVEs.end(), |
| 535 | [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 536 | if (A.Expr != B.Expr && A.Expr && B.Expr) { |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 537 | auto FragmentA = A.Expr->getFragmentInfo(); |
| 538 | auto FragmentB = B.Expr->getFragmentInfo(); |
| 539 | if (FragmentA && FragmentB) |
| 540 | return FragmentA->OffsetInBits < FragmentB->OffsetInBits; |
| 541 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 542 | return false; |
| 543 | }); |
| 544 | GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), |
| 545 | [](DwarfCompileUnit::GlobalExpr A, |
| 546 | DwarfCompileUnit::GlobalExpr B) { |
| 547 | return A.Expr == B.Expr; |
| 548 | }), |
| 549 | GVEs.end()); |
| 550 | return GVEs; |
| 551 | } |
| 552 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 553 | // Emit all Dwarf sections that should come prior to the content. Create |
| 554 | // global DIEs and emit initial debug info sections. This is invoked by |
| 555 | // the target AsmPrinter. |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 556 | void DwarfDebug::beginModule() { |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 557 | NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, |
| 558 | DWARFGroupDescription, TimePassesIsEnabled); |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 559 | if (DisableDebugInfoPrinting) |
| 560 | return; |
| 561 | |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 562 | const Module *M = MMI->getModule(); |
| 563 | |
Peter Collingbourne | b326986 | 2016-06-01 02:58:40 +0000 | [diff] [blame] | 564 | unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), |
| 565 | M->debug_compile_units_end()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 566 | // Tell MMI whether we have debug info. |
| 567 | MMI->setDebugInfoAvailability(NumDebugCUs > 0); |
| 568 | SingleCU = NumDebugCUs == 1; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 569 | DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> |
| 570 | GVMap; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 571 | for (const GlobalVariable &Global : M->globals()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 572 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 573 | Global.getDebugInfo(GVs); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 574 | for (auto *GVE : GVs) |
| 575 | GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 578 | for (DICompileUnit *CUNode : M->debug_compile_units()) { |
David Blaikie | 89daf77 | 2017-07-28 03:06:25 +0000 | [diff] [blame] | 579 | // FIXME: Move local imported entities into a list attached to the |
| 580 | // subprogram, then this search won't be needed and a |
| 581 | // getImportedEntities().empty() test should go below with the rest. |
| 582 | bool HasNonLocalImportedEntities = llvm::any_of( |
| 583 | CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { |
| 584 | return !isa<DILocalScope>(IE->getScope()); |
| 585 | }); |
| 586 | |
| 587 | if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && |
| 588 | CUNode->getRetainedTypes().empty() && |
| 589 | CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 590 | continue; |
| 591 | |
| 592 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 593 | |
| 594 | // Global Variables. |
Adrian Prantl | 2116dd3 | 2017-08-19 01:15:06 +0000 | [diff] [blame] | 595 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 596 | // Don't bother adding DIGlobalVariableExpressions listed in the CU if we |
| 597 | // already know about the variable and it isn't adding a constant |
| 598 | // expression. |
| 599 | auto &GVMapEntry = GVMap[GVE->getVariable()]; |
| 600 | auto *Expr = GVE->getExpression(); |
| 601 | if (!GVMapEntry.size() || (Expr && Expr->isConstant())) |
| 602 | GVMapEntry.push_back({nullptr, Expr}); |
| 603 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 604 | DenseSet<DIGlobalVariable *> Processed; |
| 605 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 606 | DIGlobalVariable *GV = GVE->getVariable(); |
| 607 | if (Processed.insert(GV).second) |
| 608 | CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); |
| 609 | } |
| 610 | |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 611 | for (auto *Ty : CUNode->getEnumTypes()) { |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 612 | // The enum types array by design contains pointers to |
| 613 | // MDNodes rather than DIRefs. Unique them here. |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 614 | CU.getOrCreateTypeDIE(cast<DIType>(Ty)); |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 615 | } |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 616 | for (auto *Ty : CUNode->getRetainedTypes()) { |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 617 | // The retained types array by design contains pointers to |
| 618 | // MDNodes rather than DIRefs. Unique them here. |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 619 | if (DIType *RT = dyn_cast<DIType>(Ty)) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 620 | // There is no point in force-emitting a forward declaration. |
| 621 | CU.getOrCreateTypeDIE(RT); |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 622 | } |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 623 | // Emit imported_modules last so that the relevant context is already |
| 624 | // available. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 625 | for (auto *IE : CUNode->getImportedEntities()) |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 626 | constructAndAddImportedEntityDIE(CU, IE); |
David Blaikie | dc69ebb | 2013-03-11 23:39:23 +0000 | [diff] [blame] | 627 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 628 | } |
| 629 | |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 630 | void DwarfDebug::finishVariableDefinitions() { |
| 631 | for (const auto &Var : ConcreteVariables) { |
| 632 | DIE *VariableDie = Var->getDIE(); |
David Blaikie | f73ae4f | 2014-08-12 00:00:31 +0000 | [diff] [blame] | 633 | assert(VariableDie); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 634 | // FIXME: Consider the time-space tradeoff of just storing the unit pointer |
| 635 | // in the ConcreteVariables list, rather than looking it up again here. |
| 636 | // DIE::getUnit isn't simple - it walks parent pointers, etc. |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 637 | DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie()); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 638 | assert(Unit); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 639 | Unit->finishVariableDefinition(*Var); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
David Blaikie | f7221ad | 2014-05-27 18:37:43 +0000 | [diff] [blame] | 643 | void DwarfDebug::finishSubprogramDefinitions() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 644 | for (const DISubprogram *SP : ProcessedSPNodes) { |
| 645 | assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); |
| 646 | forBothCUs( |
| 647 | getOrCreateDwarfCompileUnit(SP->getUnit()), |
| 648 | [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); |
| 649 | } |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void DwarfDebug::finalizeModuleInfo() { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 653 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 654 | |
David Blaikie | 3c2fff3 | 2014-05-27 18:37:48 +0000 | [diff] [blame] | 655 | finishSubprogramDefinitions(); |
| 656 | |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 657 | finishVariableDefinitions(); |
| 658 | |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 659 | // Include the DWO file name in the hash if there's more than one CU. |
| 660 | // This handles ThinLTO's situation where imported CUs may very easily be |
| 661 | // duplicate with the same CU partially imported into another ThinLTO unit. |
| 662 | StringRef DWOName; |
| 663 | if (CUMap.size() > 1) |
| 664 | DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; |
| 665 | |
Eric Christopher | ad10cb5 | 2013-12-04 23:24:38 +0000 | [diff] [blame] | 666 | // Handle anything that needs to be done on a per-unit basis after |
| 667 | // all other generation. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 668 | for (const auto &P : CUMap) { |
| 669 | auto &TheCU = *P.second; |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 670 | // Emit DW_AT_containing_type attribute to connect types with their |
| 671 | // vtable holding type. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 672 | TheCU.constructContainingTypeDIEs(); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 673 | |
Eric Christopher | 46e2343 | 2013-12-20 04:16:18 +0000 | [diff] [blame] | 674 | // Add CU specific attributes if we need to add any. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 675 | // If we're splitting the dwarf out now that we've got the entire |
| 676 | // CU then add the dwo id to it. |
| 677 | auto *SkCU = TheCU.getSkeleton(); |
| 678 | if (useSplitDwarf()) { |
| 679 | // Emit a unique identifier for this CU. |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 680 | uint64_t ID = |
| 681 | DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 682 | TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 683 | dwarf::DW_FORM_data8, ID); |
| 684 | SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 685 | dwarf::DW_FORM_data8, ID); |
David Blaikie | 3ffe4dd | 2014-03-25 05:34:24 +0000 | [diff] [blame] | 686 | |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 687 | // We don't keep track of which addresses are used in which CU so this |
| 688 | // is a bit pessimistic under LTO. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 689 | if (!AddrPool.isEmpty()) { |
| 690 | const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol(); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 691 | SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base, |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 692 | Sym, Sym); |
| 693 | } |
| 694 | if (!SkCU->getRangeLists().empty()) { |
| 695 | const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 696 | SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 697 | Sym, Sym); |
| 698 | } |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 699 | } |
Eric Christopher | d866720 | 2013-12-30 17:22:27 +0000 | [diff] [blame] | 700 | |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 701 | // If we have code split among multiple sections or non-contiguous |
| 702 | // ranges of code then emit a DW_AT_ranges attribute on the unit that will |
| 703 | // remain in the .o file, otherwise add a DW_AT_low_pc. |
| 704 | // FIXME: We should use ranges allow reordering of code ala |
| 705 | // .subsections_via_symbols in mach-o. This would mean turning on |
| 706 | // ranges for all subprogram DIEs for mach-o. |
David Blaikie | a8be0a7 | 2014-11-01 01:15:24 +0000 | [diff] [blame] | 707 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 708 | if (unsigned NumRanges = TheCU.getRanges().size()) { |
| 709 | if (NumRanges > 1) |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 710 | // A DW_AT_low_pc attribute may also be specified in combination with |
| 711 | // DW_AT_ranges to specify the default base address for use in |
| 712 | // location lists (see Section 2.6.2) and range lists (see Section |
| 713 | // 2.17.3). |
| 714 | U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 715 | else |
David Blaikie | 72d03ef | 2015-05-02 02:31:49 +0000 | [diff] [blame] | 716 | U.setBaseAddress(TheCU.getRanges().front().getStart()); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 717 | U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 718 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 719 | |
| 720 | auto *CUNode = cast<DICompileUnit>(P.first); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 721 | // If compile Unit has macros, emit "DW_AT_macro_info" attribute. |
| 722 | if (CUNode->getMacros()) |
| 723 | U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, |
| 724 | U.getMacroLabelBegin(), |
| 725 | TLOF.getDwarfMacinfoSection()->getBeginSymbol()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 728 | // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. |
| 729 | for (auto *CUNode : MMI->getModule()->debug_compile_units()) |
Adrian Prantl | 960e766 | 2017-07-27 15:24:20 +0000 | [diff] [blame] | 730 | if (CUNode->getDWOId()) |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 731 | getOrCreateDwarfCompileUnit(CUNode); |
| 732 | |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 733 | // Compute DIE offsets and sizes. |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 734 | InfoHolder.computeSizeAndOffsets(); |
| 735 | if (useSplitDwarf()) |
| 736 | SkeletonHolder.computeSizeAndOffsets(); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 739 | // Emit all Dwarf sections that should come after the content. |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 740 | void DwarfDebug::endModule() { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 741 | assert(CurFn == nullptr); |
| 742 | assert(CurMI == nullptr); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 743 | |
David Blaikie | 0dc623c | 2014-10-24 17:53:38 +0000 | [diff] [blame] | 744 | // If we aren't actually generating debug info (check beginModule - |
| 745 | // conditionalized on !DisableDebugInfoPrinting and the presence of the |
| 746 | // llvm.dbg.cu metadata node) |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 747 | if (!MMI->hasDebugInfo()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 748 | return; |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 749 | |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 750 | // Finalize the debug info for the module. |
| 751 | finalizeModuleInfo(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 752 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 753 | emitDebugStr(); |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 754 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 755 | if (useSplitDwarf()) |
| 756 | emitDebugLocDWO(); |
| 757 | else |
| 758 | // Emit info into a debug loc section. |
| 759 | emitDebugLoc(); |
| 760 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 761 | // Corresponding abbreviations into a abbrev section. |
| 762 | emitAbbreviations(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 763 | |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 764 | // Emit all the DIEs into a debug info section. |
| 765 | emitDebugInfo(); |
| 766 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 767 | // Emit info into a debug aranges section. |
Eric Christopher | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 768 | if (GenerateARangeSection) |
| 769 | emitDebugARanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 770 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 771 | // Emit info into a debug ranges section. |
| 772 | emitDebugRanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 773 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 774 | // Emit info into a debug macinfo section. |
| 775 | emitDebugMacinfo(); |
| 776 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 777 | if (useSplitDwarf()) { |
| 778 | emitDebugStrDWO(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 779 | emitDebugInfoDWO(); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 780 | emitDebugAbbrevDWO(); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 781 | emitDebugLineDWO(); |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 782 | // Emit DWO addresses. |
David Blaikie | d75fb28 | 2014-04-23 21:20:10 +0000 | [diff] [blame] | 783 | AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 784 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 785 | |
Eric Christopher | a876b82 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 786 | // Emit info into the dwarf accelerator table sections. |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 787 | if (useDwarfAccelTables()) { |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 788 | emitAccelNames(); |
| 789 | emitAccelObjC(); |
| 790 | emitAccelNamespaces(); |
| 791 | emitAccelTypes(); |
| 792 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 793 | |
Eric Christopher | 4b35818 | 2013-08-30 00:40:17 +0000 | [diff] [blame] | 794 | // Emit the pubnames and pubtypes sections if requested. |
David Blaikie | b3cee2f | 2017-05-25 18:50:28 +0000 | [diff] [blame] | 795 | // The condition is optimistically correct - any CU not using GMLT (& |
| 796 | // implicit/default pubnames state) might still have pubnames. |
| 797 | if (hasDwarfPubSections(/* gmlt */ false)) { |
David Blaikie | 70a3320 | 2013-09-19 17:33:35 +0000 | [diff] [blame] | 798 | emitDebugPubNames(GenerateGnuPubSections); |
| 799 | emitDebugPubTypes(GenerateGnuPubSections); |
Eric Christopher | 4b35818 | 2013-08-30 00:40:17 +0000 | [diff] [blame] | 800 | } |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 801 | |
Devang Patel | d070128 | 2010-08-02 17:32:15 +0000 | [diff] [blame] | 802 | // clean up. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 803 | // FIXME: AbstractVariables.clear(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 804 | } |
| 805 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 806 | void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV, |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 807 | const MDNode *ScopeNode) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 808 | const DILocalVariable *Cleansed = nullptr; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 809 | if (CU.getExistingAbstractVariable(IV, Cleansed)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 810 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 811 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 812 | CU.createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 813 | cast<DILocalScope>(ScopeNode))); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 814 | } |
| 815 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 816 | void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU, |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 817 | InlinedVariable IV, const MDNode *ScopeNode) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 818 | const DILocalVariable *Cleansed = nullptr; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 819 | if (CU.getExistingAbstractVariable(IV, Cleansed)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 820 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 821 | |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 822 | if (LexicalScope *Scope = |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 823 | LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 824 | CU.createAbstractVariable(Cleansed, Scope); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 825 | } |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 826 | |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 827 | // Collect variable information from side table maintained by MF. |
| 828 | void DwarfDebug::collectVariableInfoFromMFTable( |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 829 | DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) { |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 830 | SmallDenseMap<InlinedVariable, DbgVariable *> MFVars; |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 831 | for (const auto &VI : Asm->MF->getVariableDbgInfo()) { |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 832 | if (!VI.Var) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 833 | continue; |
Duncan P. N. Exon Smith | 78a9527 | 2015-04-16 22:12:59 +0000 | [diff] [blame] | 834 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 835 | "Expected inlined-at fields to agree"); |
| 836 | |
| 837 | InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt()); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 838 | Processed.insert(Var); |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 839 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
Jim Grosbach | a8683bb | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 840 | |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 841 | // If variable scope is not found then skip this variable. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 842 | if (!Scope) |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 843 | continue; |
Devang Patel | f6eeaeb | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 844 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 845 | ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode()); |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 846 | auto RegVar = llvm::make_unique<DbgVariable>(Var.first, Var.second); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 847 | RegVar->initializeMMI(VI.Expr, VI.Slot); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 848 | if (DbgVariable *DbgVar = MFVars.lookup(Var)) |
| 849 | DbgVar->addMMIEntry(*RegVar); |
| 850 | else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { |
| 851 | MFVars.insert({Var, RegVar.get()}); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 852 | ConcreteVariables.push_back(std::move(RegVar)); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 853 | } |
Devang Patel | 475d32a | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 854 | } |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 855 | } |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 856 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 857 | // Get .debug_loc entry for the instruction range starting at MI. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 858 | static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 859 | const DIExpression *Expr = MI->getDebugExpression(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 860 | assert(MI->getNumOperands() == 4); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 861 | if (MI->getOperand(0).isReg()) { |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 862 | auto RegOp = MI->getOperand(0); |
| 863 | auto Op1 = MI->getOperand(1); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 864 | // If the second operand is an immediate, this is a |
| 865 | // register-indirect address. |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 866 | assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset"); |
| 867 | MachineLocation MLoc(RegOp.getReg(), Op1.isImm()); |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 868 | return DebugLocEntry::Value(Expr, MLoc); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 869 | } |
| 870 | if (MI->getOperand(0).isImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 871 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 872 | if (MI->getOperand(0).isFPImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 873 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 874 | if (MI->getOperand(0).isCImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 875 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 876 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 877 | llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 880 | /// \brief If this and Next are describing different fragments of the same |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 881 | /// variable, merge them by appending Next's values to the current |
| 882 | /// list of values. |
| 883 | /// Return true if the merge was successful. |
| 884 | bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) { |
| 885 | if (Begin == Next.Begin) { |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 886 | auto *FirstExpr = cast<DIExpression>(Values[0].Expression); |
| 887 | auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 888 | if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment()) |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 889 | return false; |
| 890 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 891 | // We can only merge entries if none of the fragments overlap any others. |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 892 | // In doing so, we can take advantage of the fact that both lists are |
| 893 | // sorted. |
| 894 | for (unsigned i = 0, j = 0; i < Values.size(); ++i) { |
| 895 | for (; j < Next.Values.size(); ++j) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 896 | int res = DebugHandlerBase::fragmentCmp( |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 897 | cast<DIExpression>(Values[i].Expression), |
| 898 | cast<DIExpression>(Next.Values[j].Expression)); |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 899 | if (res == 0) // The two expressions overlap, we can't merge. |
| 900 | return false; |
| 901 | // Values[i] is entirely before Next.Values[j], |
| 902 | // so go back to the next entry of Values. |
| 903 | else if (res == -1) |
| 904 | break; |
| 905 | // Next.Values[j] is entirely before Values[i], so go on to the |
| 906 | // next entry of Next.Values. |
| 907 | } |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 908 | } |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 909 | |
| 910 | addValues(Next.Values); |
| 911 | End = Next.End; |
| 912 | return true; |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 913 | } |
| 914 | return false; |
| 915 | } |
| 916 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 917 | /// Build the location list for all DBG_VALUEs in the function that |
| 918 | /// describe the same variable. If the ranges of several independent |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 919 | /// fragments of the same variable overlap partially, split them up and |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 920 | /// combine the ranges. The resulting DebugLocEntries are will have |
| 921 | /// strict monotonically increasing begin addresses and will never |
| 922 | /// overlap. |
| 923 | // |
| 924 | // Input: |
| 925 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 926 | // Ranges History [var, loc, fragment ofs size] |
| 927 | // 0 | [x, (reg0, fragment 0, 32)] |
| 928 | // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 929 | // 2 | | ... |
| 930 | // 3 | [clobber reg0] |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 931 | // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of |
Eric Christopher | ffc5ff3 | 2015-02-17 20:02:28 +0000 | [diff] [blame] | 932 | // x. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 933 | // |
| 934 | // Output: |
| 935 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 936 | // [0-1] [x, (reg0, fragment 0, 32)] |
| 937 | // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)] |
| 938 | // [3-4] [x, (reg1, fragment 32, 32)] |
| 939 | // [4- ] [x, (mem, fragment 0, 64)] |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 940 | void |
| 941 | DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, |
| 942 | const DbgValueHistoryMap::InstrRanges &Ranges) { |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 943 | SmallVector<DebugLocEntry::Value, 4> OpenRanges; |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 944 | |
| 945 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 946 | const MachineInstr *Begin = I->first; |
| 947 | const MachineInstr *End = I->second; |
| 948 | assert(Begin->isDebugValue() && "Invalid History entry"); |
| 949 | |
| 950 | // Check if a variable is inaccessible in this range. |
Adrian Prantl | 5e1fa85 | 2014-08-12 21:55:58 +0000 | [diff] [blame] | 951 | if (Begin->getNumOperands() > 1 && |
| 952 | Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) { |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 953 | OpenRanges.clear(); |
| 954 | continue; |
| 955 | } |
| 956 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 957 | // If this fragment overlaps with any open ranges, truncate them. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 958 | const DIExpression *DIExpr = Begin->getDebugExpression(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 959 | auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 960 | return fragmentsOverlap(DIExpr, R.getExpression()); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 961 | }); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 962 | OpenRanges.erase(Last, OpenRanges.end()); |
| 963 | |
| 964 | const MCSymbol *StartLabel = getLabelBeforeInsn(Begin); |
| 965 | assert(StartLabel && "Forgot label before DBG_VALUE starting a range!"); |
| 966 | |
| 967 | const MCSymbol *EndLabel; |
| 968 | if (End != nullptr) |
| 969 | EndLabel = getLabelAfterInsn(End); |
| 970 | else if (std::next(I) == Ranges.end()) |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 971 | EndLabel = Asm->getFunctionEnd(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 972 | else |
| 973 | EndLabel = getLabelBeforeInsn(std::next(I)->first); |
| 974 | assert(EndLabel && "Forgot label after instruction ending a range!"); |
| 975 | |
| 976 | DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n"); |
| 977 | |
| 978 | auto Value = getDebugLocValue(Begin); |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 979 | DebugLocEntry Loc(StartLabel, EndLabel, Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 980 | bool couldMerge = false; |
| 981 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 982 | // If this is a fragment, it may belong to the current DebugLocEntry. |
| 983 | if (DIExpr->isFragment()) { |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 984 | // Add this value to the list of open ranges. |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 985 | OpenRanges.push_back(Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 986 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 987 | // Attempt to add the fragment to the last entry. |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 988 | if (!DebugLoc.empty()) |
| 989 | if (DebugLoc.back().MergeValues(Loc)) |
| 990 | couldMerge = true; |
| 991 | } |
| 992 | |
| 993 | if (!couldMerge) { |
| 994 | // Need to add a new DebugLocEntry. Add all values from still |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 995 | // valid non-overlapping fragments. |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 996 | if (OpenRanges.size()) |
| 997 | Loc.addValues(OpenRanges); |
| 998 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 999 | DebugLoc.push_back(std::move(Loc)); |
| 1000 | } |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Attempt to coalesce the ranges of two otherwise identical |
| 1003 | // DebugLocEntries. |
| 1004 | auto CurEntry = DebugLoc.rbegin(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1005 | DEBUG({ |
| 1006 | dbgs() << CurEntry->getValues().size() << " Values:\n"; |
Adrian Prantl | 6f8c1b6 | 2015-05-26 20:06:51 +0000 | [diff] [blame] | 1007 | for (auto &Value : CurEntry->getValues()) |
Adrian Prantl | dba58fb | 2016-02-29 22:28:22 +0000 | [diff] [blame] | 1008 | Value.dump(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1009 | dbgs() << "-----\n"; |
| 1010 | }); |
Adrian Prantl | 7570731 | 2015-05-26 20:06:48 +0000 | [diff] [blame] | 1011 | |
| 1012 | auto PrevEntry = std::next(CurEntry); |
| 1013 | if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) |
| 1014 | DebugLoc.pop_back(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1018 | DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU, |
| 1019 | LexicalScope &Scope, |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1020 | InlinedVariable IV) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1021 | ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode()); |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1022 | ConcreteVariables.push_back( |
| 1023 | llvm::make_unique<DbgVariable>(IV.first, IV.second)); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1024 | InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get()); |
| 1025 | return ConcreteVariables.back().get(); |
| 1026 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1027 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1028 | /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its |
| 1029 | /// enclosing lexical scope. The check ensures there are no other instructions |
| 1030 | /// in the same lexical scope preceding the DBG_VALUE and that its range is |
| 1031 | /// either open or otherwise rolls off the end of the scope. |
| 1032 | static bool validThroughout(LexicalScopes &LScopes, |
| 1033 | const MachineInstr *DbgValue, |
| 1034 | const MachineInstr *RangeEnd) { |
| 1035 | assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); |
| 1036 | auto MBB = DbgValue->getParent(); |
| 1037 | auto DL = DbgValue->getDebugLoc(); |
| 1038 | auto *LScope = LScopes.findLexicalScope(DL); |
| 1039 | // Scope doesn't exist; this is a dead DBG_VALUE. |
| 1040 | if (!LScope) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1041 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1042 | auto &LSRange = LScope->getRanges(); |
| 1043 | if (LSRange.size() == 0) |
| 1044 | return false; |
| 1045 | |
| 1046 | // Determine if the DBG_VALUE is valid at the beginning of its lexical block. |
| 1047 | const MachineInstr *LScopeBegin = LSRange.front().first; |
| 1048 | // Early exit if the lexical scope begins outside of the current block. |
| 1049 | if (LScopeBegin->getParent() != MBB) |
| 1050 | return false; |
| 1051 | MachineBasicBlock::const_reverse_iterator Pred(DbgValue); |
| 1052 | for (++Pred; Pred != MBB->rend(); ++Pred) { |
| 1053 | if (Pred->getFlag(MachineInstr::FrameSetup)) |
| 1054 | break; |
| 1055 | auto PredDL = Pred->getDebugLoc(); |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1056 | if (!PredDL || Pred->isMetaInstruction()) |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1057 | continue; |
| 1058 | // Check whether the instruction preceding the DBG_VALUE is in the same |
| 1059 | // (sub)scope as the DBG_VALUE. |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1060 | if (DL->getScope() == PredDL->getScope()) |
| 1061 | return false; |
| 1062 | auto *PredScope = LScopes.findLexicalScope(PredDL); |
| 1063 | if (!PredScope || LScope->dominates(PredScope)) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1064 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | // If the range of the DBG_VALUE is open-ended, report success. |
| 1068 | if (!RangeEnd) |
| 1069 | return true; |
| 1070 | |
| 1071 | // Fail if there are instructions belonging to our scope in another block. |
| 1072 | const MachineInstr *LScopeEnd = LSRange.back().second; |
| 1073 | if (LScopeEnd->getParent() != MBB) |
| 1074 | return false; |
| 1075 | |
| 1076 | // Single, constant DBG_VALUEs in the prologue are promoted to be live |
| 1077 | // throughout the function. This is a hack, presumably for DWARF v2 and not |
| 1078 | // necessarily correct. It would be much better to use a dbg.declare instead |
| 1079 | // if we know the constant is live throughout the scope. |
| 1080 | if (DbgValue->getOperand(0).isImm() && MBB->pred_empty()) |
| 1081 | return true; |
| 1082 | |
| 1083 | return false; |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1086 | // Find variables for each lexical scope. |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 1087 | void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1088 | const DISubprogram *SP, |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1089 | DenseSet<InlinedVariable> &Processed) { |
Eric Christopher | 270a12c | 2013-07-03 21:37:03 +0000 | [diff] [blame] | 1090 | // Grab the variable info that was squirreled away in the MMI side-table. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1091 | collectVariableInfoFromMFTable(TheCU, Processed); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1092 | |
Alexey Samsonov | 0436caa | 2014-04-30 23:02:40 +0000 | [diff] [blame] | 1093 | for (const auto &I : DbgValues) { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1094 | InlinedVariable IV = I.first; |
| 1095 | if (Processed.count(IV)) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1096 | continue; |
| 1097 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1098 | // Instruction ranges, specifying where IV is accessible. |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1099 | const auto &Ranges = I.second; |
| 1100 | if (Ranges.empty()) |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1101 | continue; |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1102 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1103 | LexicalScope *Scope = nullptr; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1104 | if (const DILocation *IA = IV.second) |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1105 | Scope = LScopes.findInlinedScope(IV.first->getScope(), IA); |
Duncan P. N. Exon Smith | b474937 | 2015-02-17 00:02:27 +0000 | [diff] [blame] | 1106 | else |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1107 | Scope = LScopes.findLexicalScope(IV.first->getScope()); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1108 | // If variable scope is not found then skip this variable. |
Devang Patel | fbd6c45 | 2010-05-21 00:10:20 +0000 | [diff] [blame] | 1109 | if (!Scope) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1110 | continue; |
| 1111 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1112 | Processed.insert(IV); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1113 | DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1114 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1115 | const MachineInstr *MInsn = Ranges.front().first; |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1116 | assert(MInsn->isDebugValue() && "History must begin with debug value"); |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1117 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1118 | // Check if there is a single DBG_VALUE, valid throughout the var's scope. |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1119 | if (Ranges.size() == 1 && |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1120 | validThroughout(LScopes, MInsn, Ranges.front().second)) { |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1121 | RegVar->initializeDbgValue(MInsn); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1122 | continue; |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1123 | } |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1124 | |
Eric Christopher | 59cc071 | 2013-01-28 17:33:26 +0000 | [diff] [blame] | 1125 | // Handle multiple DBG_VALUE instructions describing one variable. |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1126 | DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); |
Jakob Stoklund Olesen | 9c057ee | 2011-03-22 00:21:41 +0000 | [diff] [blame] | 1127 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1128 | // Build the location list for this variable. |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1129 | SmallVector<DebugLocEntry, 8> Entries; |
| 1130 | buildLocationList(Entries, Ranges); |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1131 | |
Adrian Prantl | 693e8de | 2016-02-29 17:06:46 +0000 | [diff] [blame] | 1132 | // If the variable has a DIBasicType, extract it. Basic types cannot have |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1133 | // unique identifiers, so don't bother resolving the type with the |
| 1134 | // identifier map. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1135 | const DIBasicType *BT = dyn_cast<DIBasicType>( |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1136 | static_cast<const Metadata *>(IV.first->getType())); |
| 1137 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1138 | // Finalize the entry by lowering it into a DWARF bytestream. |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1139 | for (auto &Entry : Entries) |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1140 | Entry.finalize(*Asm, List, BT); |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1141 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | // Collect info for variables that were optimized out. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1144 | for (const DILocalVariable *DV : SP->getVariables()) { |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1145 | if (Processed.insert(InlinedVariable(DV, nullptr)).second) |
| 1146 | if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope())) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1147 | createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr)); |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1148 | } |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1149 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1150 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1151 | // Process beginning of an instruction. |
Devang Patel | b5694e7 | 2010-10-26 17:49:02 +0000 | [diff] [blame] | 1152 | void DwarfDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1153 | DebugHandlerBase::beginInstruction(MI); |
| 1154 | assert(CurMI); |
| 1155 | |
David Blaikie | 7f2b717 | 2017-05-26 17:05:15 +0000 | [diff] [blame] | 1156 | const auto *SP = MI->getParent()->getParent()->getFunction()->getSubprogram(); |
| 1157 | if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
| 1158 | return; |
| 1159 | |
Paul Robinson | 4fa7b57a | 2016-12-09 19:15:32 +0000 | [diff] [blame] | 1160 | // Check if source location changes, but ignore DBG_VALUE and CFI locations. |
Robert Lougher | 51529eb | 2017-09-12 18:23:15 +0000 | [diff] [blame] | 1161 | if (MI->isMetaInstruction()) |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | const DebugLoc &DL = MI->getDebugLoc(); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1164 | // When we emit a line-0 record, we don't update PrevInstLoc; so look at |
| 1165 | // the last line number actually emitted, to see if it was line 0. |
| 1166 | unsigned LastAsmLine = |
| 1167 | Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); |
Paul Robinson | 96de8c7 | 2016-11-29 22:41:16 +0000 | [diff] [blame] | 1168 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1169 | if (DL == PrevInstLoc) { |
| 1170 | // If we have an ongoing unspecified location, nothing to do here. |
| 1171 | if (!DL) |
| 1172 | return; |
| 1173 | // We have an explicit location, same as the previous location. |
| 1174 | // But we might be coming back to it after a line 0 record. |
| 1175 | if (LastAsmLine == 0 && DL.getLine() != 0) { |
| 1176 | // Reinstate the source location but not marked as a statement. |
| 1177 | const MDNode *Scope = DL.getScope(); |
| 1178 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); |
Paul Robinson | 37a13dd | 2016-11-30 22:49:55 +0000 | [diff] [blame] | 1179 | } |
| 1180 | return; |
| 1181 | } |
| 1182 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1183 | if (!DL) { |
| 1184 | // We have an unspecified location, which might want to be line 0. |
| 1185 | // If we have already emitted a line-0 record, don't repeat it. |
| 1186 | if (LastAsmLine == 0) |
| 1187 | return; |
| 1188 | // If user said Don't Do That, don't do that. |
| 1189 | if (UnknownLocations == Disable) |
| 1190 | return; |
| 1191 | // See if we have a reason to emit a line-0 record now. |
| 1192 | // Reasons to emit a line-0 record include: |
| 1193 | // - User asked for it (UnknownLocations). |
| 1194 | // - Instruction has a label, so it's referenced from somewhere else, |
| 1195 | // possibly debug information; we want it to have a source location. |
| 1196 | // - Instruction is at the top of a block; we don't want to inherit the |
| 1197 | // location from the physically previous (maybe unrelated) block. |
| 1198 | if (UnknownLocations == Enable || PrevLabel || |
| 1199 | (PrevInstBB && PrevInstBB != MI->getParent())) { |
Paul Robinson | 8fec3da | 2016-12-14 00:27:35 +0000 | [diff] [blame] | 1200 | // Preserve the file and column numbers, if we can, to save space in |
| 1201 | // the encoded line table. |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1202 | // Do not update PrevInstLoc, it remembers the last non-0 line. |
Paul Robinson | 8fec3da | 2016-12-14 00:27:35 +0000 | [diff] [blame] | 1203 | const MDNode *Scope = nullptr; |
| 1204 | unsigned Column = 0; |
| 1205 | if (PrevInstLoc) { |
| 1206 | Scope = PrevInstLoc.getScope(); |
| 1207 | Column = PrevInstLoc.getCol(); |
| 1208 | } |
| 1209 | recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1210 | } |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | // We have an explicit location, different from the previous location. |
| 1215 | // Don't repeat a line-0 record, but otherwise emit the new location. |
| 1216 | // (The new location might be an explicit line 0, which we do emit.) |
Paul Robinson | 2dfb688 | 2016-12-16 23:54:33 +0000 | [diff] [blame] | 1217 | if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0) |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1218 | return; |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1219 | unsigned Flags = 0; |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1220 | if (DL == PrologEndLoc) { |
| 1221 | Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; |
| 1222 | PrologEndLoc = DebugLoc(); |
| 1223 | } |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1224 | // If the line changed, we call that a new statement; unless we went to |
| 1225 | // line 0 and came back, in which case it is not a new statement. |
| 1226 | unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; |
| 1227 | if (DL.getLine() && DL.getLine() != OldLine) |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1228 | Flags |= DWARF2_FLAG_IS_STMT; |
| 1229 | |
| 1230 | const MDNode *Scope = DL.getScope(); |
| 1231 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1232 | |
| 1233 | // If we're not at line 0, remember this location. |
| 1234 | if (DL.getLine()) |
| 1235 | PrevInstLoc = DL; |
Devang Patel | 75cc16c | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1238 | static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { |
| 1239 | // First known non-DBG_VALUE and non-frame setup location marks |
| 1240 | // the beginning of the function body. |
| 1241 | for (const auto &MBB : *MF) |
| 1242 | for (const auto &MI : MBB) |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 1243 | if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && |
Michael Kuperstein | 2b3c16c | 2015-10-08 07:48:49 +0000 | [diff] [blame] | 1244 | MI.getDebugLoc()) |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1245 | return MI.getDebugLoc(); |
| 1246 | return DebugLoc(); |
| 1247 | } |
| 1248 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1249 | // Gather pre-function debug information. Assumes being called immediately |
| 1250 | // after the function entry point has been emitted. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1251 | void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1252 | CurFn = MF; |
Eric Christopher | fedfa44 | 2013-11-01 23:14:17 +0000 | [diff] [blame] | 1253 | |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1254 | auto *SP = MF->getFunction()->getSubprogram(); |
| 1255 | assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1256 | if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 1257 | return; |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1258 | |
| 1259 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function |
| 1262 | // belongs to so that we add to the correct per-cu line table in the |
| 1263 | // non-asm case. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1264 | if (Asm->OutStreamer->hasRawTextSupport()) |
Rafael Espindola | b4eec1d | 2014-02-05 18:00:21 +0000 | [diff] [blame] | 1265 | // Use a single line table if we are generating assembly. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1266 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Manman Ren | 9d4c735 | 2013-05-21 00:57:22 +0000 | [diff] [blame] | 1267 | else |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1268 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); |
Manman Ren | 4e042a6 | 2013-02-05 21:52:47 +0000 | [diff] [blame] | 1269 | |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1270 | // Record beginning of function. |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1271 | PrologEndLoc = findPrologueEndLoc(MF); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1272 | if (PrologEndLoc) { |
Saleem Abdulrasool | fc07c72 | 2015-01-24 20:19:45 +0000 | [diff] [blame] | 1273 | // We'd like to list the prologue as "not statements" but GDB behaves |
| 1274 | // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1275 | auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); |
Duncan P. N. Exon Smith | fd07a2a | 2015-03-30 21:32:28 +0000 | [diff] [blame] | 1276 | recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT); |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1277 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1280 | void DwarfDebug::skippedNonDebugFunction() { |
| 1281 | // If we don't have a subprogram for this function then there will be a hole |
| 1282 | // in the range information. Keep note of this by setting the previously used |
| 1283 | // section to nullptr. |
| 1284 | PrevCU = nullptr; |
| 1285 | CurFn = nullptr; |
| 1286 | } |
| 1287 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1288 | // Gather and emit post-function debug information. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1289 | void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { |
| 1290 | const DISubprogram *SP = MF->getFunction()->getSubprogram(); |
| 1291 | |
David Blaikie | e75f963 | 2014-10-14 17:12:02 +0000 | [diff] [blame] | 1292 | assert(CurFn == MF && |
| 1293 | "endFunction should be called with the same function as beginFunction"); |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1294 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1295 | // Set DwarfDwarfCompileUnitID in MCContext to default value. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1296 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1297 | |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1298 | LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1299 | assert(!FnScope || SP == FnScope->getScopeNode()); |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 1300 | DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); |
David Blaikie | 263a008 | 2014-10-23 00:06:27 +0000 | [diff] [blame] | 1301 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1302 | DenseSet<InlinedVariable> ProcessedVars; |
David Blaikie | 263a008 | 2014-10-23 00:06:27 +0000 | [diff] [blame] | 1303 | collectVariableInfo(TheCU, SP, ProcessedVars); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1304 | |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1305 | // Add the range of this function to the list of ranges for the CU. |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 1306 | TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Under -gmlt, skip building the subprogram if there are no inlined |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1309 | // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram |
| 1310 | // is still needed as we need its source location. |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 1311 | if (!TheCU.getCUNode()->getDebugInfoForProfiling() && |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1312 | TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && |
David Blaikie | e1c7974 | 2014-09-30 21:28:32 +0000 | [diff] [blame] | 1313 | LScopes.getAbstractScopesList().empty() && !IsDarwin) { |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1314 | assert(InfoHolder.getScopeVariables().empty()); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1315 | PrevLabel = nullptr; |
| 1316 | CurFn = nullptr; |
| 1317 | return; |
| 1318 | } |
| 1319 | |
Adrian Prantl | 049d21c | 2014-10-13 20:44:58 +0000 | [diff] [blame] | 1320 | #ifndef NDEBUG |
| 1321 | size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); |
| 1322 | #endif |
Devang Patel | 7e62302 | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1323 | // Construct abstract scopes. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1324 | for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1325 | auto *SP = cast<DISubprogram>(AScope->getScopeNode()); |
David Blaikie | 4abe19e | 2014-05-12 18:23:35 +0000 | [diff] [blame] | 1326 | // Collect info for variables that were optimized out. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1327 | for (const DILocalVariable *DV : SP->getVariables()) { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1328 | if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second) |
David Blaikie | 4abe19e | 2014-05-12 18:23:35 +0000 | [diff] [blame] | 1329 | continue; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1330 | ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr), |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1331 | DV->getScope()); |
Adrian Prantl | 049d21c | 2014-10-13 20:44:58 +0000 | [diff] [blame] | 1332 | assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes |
| 1333 | && "ensureAbstractVariableIsCreated inserted abstract scopes"); |
Devang Patel | 5c0f85c | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 1334 | } |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1335 | constructAbstractSubprogramScopeDIE(TheCU, AScope); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1336 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1337 | |
David Blaikie | 38b74bf | 2016-12-15 23:37:38 +0000 | [diff] [blame] | 1338 | ProcessedSPNodes.insert(SP); |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1339 | TheCU.constructSubprogramScopeDIE(SP, FnScope); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1340 | if (auto *SkelCU = TheCU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 1341 | if (!LScopes.getAbstractScopesList().empty() && |
| 1342 | TheCU.getCUNode()->getSplitDebugInlining()) |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1343 | SkelCU->constructSubprogramScopeDIE(SP, FnScope); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1344 | |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1345 | // Clear debug info |
David Blaikie | 825bdd2 | 2014-05-21 22:41:17 +0000 | [diff] [blame] | 1346 | // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the |
| 1347 | // DbgVariables except those that are also in AbstractVariables (since they |
| 1348 | // can be used cross-function) |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1349 | InfoHolder.getScopeVariables().clear(); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1350 | PrevLabel = nullptr; |
| 1351 | CurFn = nullptr; |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1354 | // Register a source line with debug info. Returns the unique label that was |
| 1355 | // emitted and which provides correspondence to the source line list. |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1356 | void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, |
| 1357 | unsigned Flags) { |
Devang Patel | 2d9caf9 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1358 | StringRef Fn; |
Devang Patel | e01b75c | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1359 | StringRef Dir; |
Dan Gohman | 50849c6 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1360 | unsigned Src = 1; |
Diego Novillo | 282450d | 2014-03-03 18:53:17 +0000 | [diff] [blame] | 1361 | unsigned Discriminator = 0; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1362 | if (auto *Scope = cast_or_null<DIScope>(S)) { |
Duncan P. N. Exon Smith | b273d06 | 2015-04-16 01:37:00 +0000 | [diff] [blame] | 1363 | Fn = Scope->getFilename(); |
| 1364 | Dir = Scope->getDirectory(); |
Paul Robinson | bb92137 | 2017-09-07 22:15:44 +0000 | [diff] [blame] | 1365 | if (Line != 0 && getDwarfVersion() >= 4) |
| 1366 | if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) |
Dehao Chen | 6e0c844 | 2016-10-07 15:21:31 +0000 | [diff] [blame] | 1367 | Discriminator = LBF->getDiscriminator(); |
Dan Gohman | 50849c6 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1368 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1369 | unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID(); |
David Blaikie | c33b3cd | 2014-04-22 21:27:37 +0000 | [diff] [blame] | 1370 | Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID]) |
| 1371 | .getOrCreateSourceID(Fn, Dir); |
Dan Gohman | 50849c6 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1372 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1373 | Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0, |
| 1374 | Discriminator, Fn); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Bill Wendling | 806535f | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 1377 | //===----------------------------------------------------------------------===// |
| 1378 | // Emit Methods |
| 1379 | //===----------------------------------------------------------------------===// |
| 1380 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1381 | // Emit the debug info section. |
| 1382 | void DwarfDebug::emitDebugInfo() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1383 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1384 | Holder.emitUnits(/* UseOffsets */ false); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1387 | // Emit the abbreviation section. |
Eric Christopher | 3837195 | 2012-11-20 23:30:11 +0000 | [diff] [blame] | 1388 | void DwarfDebug::emitAbbreviations() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1389 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 1390 | |
| 1391 | Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 1392 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1393 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1394 | void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section, |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1395 | StringRef TableName) { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1396 | Accel.FinalizeTable(Asm, TableName); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1397 | Asm->OutStreamer->SwitchSection(Section); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1398 | |
| 1399 | // Emit the full data. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1400 | Accel.emit(Asm, Section->getBeginSymbol(), this); |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | // Emit visible names into a hashed accelerator table section. |
| 1404 | void DwarfDebug::emitAccelNames() { |
| 1405 | emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1406 | "Names"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 1409 | // Emit objective C classes and categories into a hashed accelerator table |
| 1410 | // section. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1411 | void DwarfDebug::emitAccelObjC() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1412 | emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1413 | "ObjC"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1416 | // Emit namespace dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1417 | void DwarfDebug::emitAccelNamespaces() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1418 | emitAccel(AccelNamespace, |
| 1419 | Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1420 | "namespac"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1423 | // Emit type dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1424 | void DwarfDebug::emitAccelTypes() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1425 | emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1426 | "types"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1429 | // Public name handling. |
| 1430 | // The format for the various pubnames: |
| 1431 | // |
| 1432 | // dwarf pubnames - offset/name pairs where the offset is the offset into the CU |
| 1433 | // for the DIE that is named. |
| 1434 | // |
| 1435 | // gnu pubnames - offset/index value/name tuples where the offset is the offset |
| 1436 | // into the CU and the index value is computed according to the type of value |
| 1437 | // for the DIE that is named. |
| 1438 | // |
| 1439 | // For type units the offset is the offset of the skeleton DIE. For split dwarf |
| 1440 | // it's the offset within the debug_info/debug_types dwo section, however, the |
| 1441 | // reference in the pubname header doesn't change. |
| 1442 | |
| 1443 | /// computeIndexValue - Compute the gdb index value for the DIE and CU. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1444 | static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, |
Eric Christopher | 0fe676a | 2013-11-21 00:48:22 +0000 | [diff] [blame] | 1445 | const DIE *Die) { |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1446 | // Entities that ended up only in a Type Unit reference the CU instead (since |
| 1447 | // the pub entry has offsets within the CU there's no real offset that can be |
| 1448 | // provided anyway). As it happens all such entities (namespaces and types, |
| 1449 | // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out |
| 1450 | // not to be true it would be necessary to persist this information from the |
| 1451 | // point at which the entry is added to the index data structure - since by |
| 1452 | // the time the index is built from that, the original type/namespace DIE in a |
| 1453 | // type unit has already been destroyed so it can't be queried for properties |
| 1454 | // like tag, etc. |
| 1455 | if (Die->getTag() == dwarf::DW_TAG_compile_unit) |
| 1456 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, |
| 1457 | dwarf::GIEL_EXTERNAL); |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1458 | dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; |
| 1459 | |
| 1460 | // We could have a specification DIE that has our most of our knowledge, |
| 1461 | // look for that now. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1462 | if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { |
| 1463 | DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); |
David Blaikie | 8dbcc3f | 2014-04-25 19:33:43 +0000 | [diff] [blame] | 1464 | if (SpecDIE.findAttribute(dwarf::DW_AT_external)) |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1465 | Linkage = dwarf::GIEL_EXTERNAL; |
| 1466 | } else if (Die->findAttribute(dwarf::DW_AT_external)) |
| 1467 | Linkage = dwarf::GIEL_EXTERNAL; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1468 | |
| 1469 | switch (Die->getTag()) { |
| 1470 | case dwarf::DW_TAG_class_type: |
| 1471 | case dwarf::DW_TAG_structure_type: |
| 1472 | case dwarf::DW_TAG_union_type: |
| 1473 | case dwarf::DW_TAG_enumeration_type: |
Eric Christopher | 261d234 | 2013-09-23 20:55:35 +0000 | [diff] [blame] | 1474 | return dwarf::PubIndexEntryDescriptor( |
| 1475 | dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus |
| 1476 | ? dwarf::GIEL_STATIC |
| 1477 | : dwarf::GIEL_EXTERNAL); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1478 | case dwarf::DW_TAG_typedef: |
| 1479 | case dwarf::DW_TAG_base_type: |
| 1480 | case dwarf::DW_TAG_subrange_type: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1481 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1482 | case dwarf::DW_TAG_namespace: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1483 | return dwarf::GIEK_TYPE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1484 | case dwarf::DW_TAG_subprogram: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1485 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1486 | case dwarf::DW_TAG_variable: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1487 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1488 | case dwarf::DW_TAG_enumerator: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1489 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, |
| 1490 | dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1491 | default: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1492 | return dwarf::GIEK_NONE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1493 | } |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Eric Christopher | 5f93bb9 | 2013-09-09 20:03:17 +0000 | [diff] [blame] | 1496 | /// emitDebugPubNames - Emit visible names into a debug pubnames section. |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1497 | void DwarfDebug::emitDebugPubNames(bool GnuStyle) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1498 | MCSection *PSec = GnuStyle |
| 1499 | ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() |
| 1500 | : Asm->getObjFileLowering().getDwarfPubNamesSection(); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1501 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1502 | emitDebugPubSection(GnuStyle, PSec, "Names", |
| 1503 | &DwarfCompileUnit::getGlobalNames); |
David Blaikie | 0f55e83 | 2014-03-11 23:18:15 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | void DwarfDebug::emitDebugPubSection( |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1507 | bool GnuStyle, MCSection *PSec, StringRef Name, |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1508 | const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) { |
David Blaikie | c3d9e9e | 2014-03-06 01:42:00 +0000 | [diff] [blame] | 1509 | for (const auto &NU : CUMap) { |
| 1510 | DwarfCompileUnit *TheU = NU.second; |
David Blaikie | 55bb8ac | 2014-03-11 23:23:39 +0000 | [diff] [blame] | 1511 | |
| 1512 | const auto &Globals = (TheU->*Accessor)(); |
| 1513 | |
David Blaikie | b3cee2f | 2017-05-25 18:50:28 +0000 | [diff] [blame] | 1514 | if (!hasDwarfPubSections(TheU->includeMinimalInlineScopes())) |
David Blaikie | ce2f1cb | 2014-03-11 23:35:06 +0000 | [diff] [blame] | 1515 | continue; |
| 1516 | |
David Blaikie | bbad0bb | 2014-11-01 01:03:39 +0000 | [diff] [blame] | 1517 | if (auto *Skeleton = TheU->getSkeleton()) |
David Blaikie | c3d9e9e | 2014-03-06 01:42:00 +0000 | [diff] [blame] | 1518 | TheU = Skeleton; |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1519 | |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1520 | // Start the dwarf pubnames section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1521 | Asm->OutStreamer->SwitchSection(PSec); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1522 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1523 | // Emit the header. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1524 | Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 1525 | MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); |
| 1526 | MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); |
David Blaikie | b7a1c4d | 2013-12-04 17:55:41 +0000 | [diff] [blame] | 1527 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1528 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1529 | Asm->OutStreamer->EmitLabel(BeginLabel); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1530 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1531 | Asm->OutStreamer->AddComment("DWARF Version"); |
David Majnemer | ed89b5c | 2013-08-21 06:13:34 +0000 | [diff] [blame] | 1532 | Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1533 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1534 | Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); |
Rafael Espindola | 857546e | 2015-06-16 23:22:02 +0000 | [diff] [blame] | 1535 | Asm->emitDwarfSymbolReference(TheU->getLabelBegin()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1536 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1537 | Asm->OutStreamer->AddComment("Compilation Unit Length"); |
David Blaikie | 983bfea | 2014-11-01 23:07:14 +0000 | [diff] [blame] | 1538 | Asm->EmitInt32(TheU->getLength()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1539 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1540 | // Emit the pubnames for this compilation unit. |
David Blaikie | 55bb8ac | 2014-03-11 23:23:39 +0000 | [diff] [blame] | 1541 | for (const auto &GI : Globals) { |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1542 | const char *Name = GI.getKeyData(); |
| 1543 | const DIE *Entity = GI.second; |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1544 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1545 | Asm->OutStreamer->AddComment("DIE offset"); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1546 | Asm->EmitInt32(Entity->getOffset()); |
| 1547 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1548 | if (GnuStyle) { |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 1549 | dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1550 | Asm->OutStreamer->AddComment( |
David Blaikie | efd0bcb | 2013-09-20 00:33:15 +0000 | [diff] [blame] | 1551 | Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " + |
David Blaikie | 404d304 | 2013-09-19 23:01:29 +0000 | [diff] [blame] | 1552 | dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); |
David Blaikie | d0a869d | 2013-09-19 22:19:37 +0000 | [diff] [blame] | 1553 | Asm->EmitInt8(Desc.toBits()); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1556 | Asm->OutStreamer->AddComment("External Name"); |
| 1557 | Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1560 | Asm->OutStreamer->AddComment("End Mark"); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1561 | Asm->EmitInt32(0); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1562 | Asm->OutStreamer->EmitLabel(EndLabel); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1566 | void DwarfDebug::emitDebugPubTypes(bool GnuStyle) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1567 | MCSection *PSec = GnuStyle |
| 1568 | ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() |
| 1569 | : Asm->getObjFileLowering().getDwarfPubTypesSection(); |
Eric Christopher | 8b3737f | 2013-09-13 00:34:58 +0000 | [diff] [blame] | 1570 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1571 | emitDebugPubSection(GnuStyle, PSec, "Types", |
| 1572 | &DwarfCompileUnit::getGlobalTypes); |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1575 | /// Emit null-terminated strings into a debug str section. |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1576 | void DwarfDebug::emitDebugStr() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1577 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1578 | Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection()); |
| 1579 | } |
| 1580 | |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1581 | void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1582 | const DebugLocStream::Entry &Entry) { |
| 1583 | auto &&Comments = DebugLocs.getComments(Entry); |
| 1584 | auto Comment = Comments.begin(); |
| 1585 | auto End = Comments.end(); |
| 1586 | for (uint8_t Byte : DebugLocs.getBytes(Entry)) |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1587 | Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : ""); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1590 | static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1591 | ByteStreamer &Streamer, |
| 1592 | const DebugLocEntry::Value &Value, |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1593 | DwarfExpression &DwarfExpr) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1594 | auto *DIExpr = Value.getExpression(); |
| 1595 | DIExpressionCursor ExprCursor(DIExpr); |
| 1596 | DwarfExpr.addFragmentOffset(DIExpr); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1597 | // Regular entry. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1598 | if (Value.isInt()) { |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1599 | if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || |
| 1600 | BT->getEncoding() == dwarf::DW_ATE_signed_char)) |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1601 | DwarfExpr.addSignedConstant(Value.getInt()); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 1602 | else |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1603 | DwarfExpr.addUnsignedConstant(Value.getInt()); |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1604 | } else if (Value.isLocation()) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1605 | MachineLocation Location = Value.getLoc(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1606 | if (Location.isIndirect()) |
| 1607 | DwarfExpr.setMemoryLocationKind(); |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1608 | DIExpressionCursor Cursor(DIExpr); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1609 | const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1610 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1611 | return; |
| 1612 | return DwarfExpr.addExpression(std::move(Cursor)); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 1613 | } else if (Value.isConstantFP()) { |
| 1614 | APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1615 | DwarfExpr.addUnsignedConstant(RawBytes); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1616 | } |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1617 | DwarfExpr.addExpression(std::move(ExprCursor)); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1620 | void DebugLocEntry::finalize(const AsmPrinter &AP, |
| 1621 | DebugLocStream::ListBuilder &List, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1622 | const DIBasicType *BT) { |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1623 | DebugLocStream::EntryBuilder Entry(List, Begin, End); |
| 1624 | BufferByteStreamer Streamer = Entry.getStreamer(); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1625 | DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1626 | const DebugLocEntry::Value &Value = Values[0]; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1627 | if (Value.isFragment()) { |
| 1628 | // Emit all fragments that belong to the same variable and range. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1629 | assert(llvm::all_of(Values, [](DebugLocEntry::Value P) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1630 | return P.isFragment(); |
| 1631 | }) && "all values are expected to be fragments"); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1632 | assert(std::is_sorted(Values.begin(), Values.end()) && |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1633 | "fragments are expected to be sorted"); |
Duncan P. N. Exon Smith | c82570b | 2015-04-13 18:53:11 +0000 | [diff] [blame] | 1634 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1635 | for (auto Fragment : Values) |
| 1636 | emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr); |
| 1637 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1638 | } else { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1639 | assert(Values.size() == 1 && "only fragments may have >1 value"); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1640 | emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1641 | } |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1642 | DwarfExpr.finalize(); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1645 | void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) { |
Duncan P. N. Exon Smith | 653c109 | 2015-05-06 19:11:20 +0000 | [diff] [blame] | 1646 | // Emit the size. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1647 | Asm->OutStreamer->AddComment("Loc expr size"); |
Duncan P. N. Exon Smith | 653c109 | 2015-05-06 19:11:20 +0000 | [diff] [blame] | 1648 | Asm->EmitInt16(DebugLocs.getBytes(Entry).size()); |
| 1649 | |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1650 | // Emit the entry. |
| 1651 | APByteStreamer Streamer(*Asm); |
| 1652 | emitDebugLocEntry(Streamer, Entry); |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Eric Christopher | 9046f94 | 2013-07-02 21:36:07 +0000 | [diff] [blame] | 1655 | // Emit locations into the debug loc section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1656 | void DwarfDebug::emitDebugLoc() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1657 | if (DebugLocs.getLists().empty()) |
| 1658 | return; |
| 1659 | |
Daniel Dunbar | fd95b01 | 2011-03-16 22:16:39 +0000 | [diff] [blame] | 1660 | // Start the dwarf loc section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1661 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1662 | Asm->getObjFileLowering().getDwarfLocSection()); |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1663 | unsigned char Size = Asm->MAI->getCodePointerSize(); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1664 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1665 | Asm->OutStreamer->EmitLabel(List.Label); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1666 | const DwarfCompileUnit *CU = List.CU; |
| 1667 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1668 | // Set up the range. This range is relative to the entry point of the |
| 1669 | // compile unit. This is a hard coded 0 for low_pc when we're emitting |
| 1670 | // ranges, or the DW_AT_low_pc on the compile unit otherwise. |
David Blaikie | ce34349 | 2014-11-03 21:15:30 +0000 | [diff] [blame] | 1671 | if (auto *Base = CU->getBaseAddress()) { |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1672 | Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); |
| 1673 | Asm->EmitLabelDifference(Entry.EndSym, Base, Size); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1674 | } else { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1675 | Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); |
| 1676 | Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1677 | } |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1678 | |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1679 | emitDebugLocEntryLocation(Entry); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1680 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1681 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 1682 | Asm->OutStreamer->EmitIntValue(0, Size); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | void DwarfDebug::emitDebugLocDWO() { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1687 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1688 | Asm->getObjFileLowering().getDwarfLocDWOSection()); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1689 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1690 | Asm->OutStreamer->EmitLabel(List.Label); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1691 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1692 | // Just always use start_length for now - at least that's one address |
| 1693 | // rather than two. We could get fancier and try to, say, reuse an |
| 1694 | // address we know we've emitted elsewhere (the start of the function? |
| 1695 | // The start of the CU or CU subrange that encloses this range?) |
Adrian Prantl | c4fbbcf | 2016-10-28 17:59:50 +0000 | [diff] [blame] | 1696 | Asm->EmitInt8(dwarf::DW_LLE_startx_length); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1697 | unsigned idx = AddrPool.getIndex(Entry.BeginSym); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1698 | Asm->EmitULEB128(idx); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1699 | Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1700 | |
| 1701 | emitDebugLocEntryLocation(Entry); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 1702 | } |
Adrian Prantl | c4fbbcf | 2016-10-28 17:59:50 +0000 | [diff] [blame] | 1703 | Asm->EmitInt8(dwarf::DW_LLE_end_of_list); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1704 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1707 | struct ArangeSpan { |
| 1708 | const MCSymbol *Start, *End; |
| 1709 | }; |
| 1710 | |
| 1711 | // Emit a debug aranges section, containing a CU lookup for any |
| 1712 | // address we can tie back to a CU. |
Eric Christopher | 7b30f2e4 | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 1713 | void DwarfDebug::emitDebugARanges() { |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1714 | // Provides a unique id per text section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1715 | MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1716 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1717 | // Filter labels by section. |
| 1718 | for (const SymbolCU &SCU : ArangeLabels) { |
| 1719 | if (SCU.Sym->isInSection()) { |
| 1720 | // Make a note of this symbol and it's section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1721 | MCSection *Section = &SCU.Sym->getSection(); |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1722 | if (!Section->getKind().isMetadata()) |
| 1723 | SectionMap[Section].push_back(SCU); |
| 1724 | } else { |
| 1725 | // Some symbols (e.g. common/bss on mach-o) can have no section but still |
| 1726 | // appear in the output. This sucks as we rely on sections to build |
| 1727 | // arange spans. We can do it without, but it's icky. |
| 1728 | SectionMap[nullptr].push_back(SCU); |
| 1729 | } |
| 1730 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1731 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1732 | DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; |
| 1733 | |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 1734 | for (auto &I : SectionMap) { |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1735 | MCSection *Section = I.first; |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 1736 | SmallVector<SymbolCU, 8> &List = I.second; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1737 | if (List.size() < 1) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1738 | continue; |
| 1739 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1740 | // If we have no section (e.g. common), just write out |
| 1741 | // individual spans for each symbol. |
| 1742 | if (!Section) { |
| 1743 | for (const SymbolCU &Cur : List) { |
| 1744 | ArangeSpan Span; |
| 1745 | Span.Start = Cur.Sym; |
| 1746 | Span.End = nullptr; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1747 | assert(Cur.CU); |
| 1748 | Spans[Cur.CU].push_back(Span); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1749 | } |
| 1750 | continue; |
| 1751 | } |
| 1752 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1753 | // Sort the symbols by offset within the section. |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 1754 | std::sort( |
| 1755 | List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) { |
| 1756 | unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; |
| 1757 | unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; |
Benjamin Kramer | 571e2fe | 2014-03-07 19:41:22 +0000 | [diff] [blame] | 1758 | |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 1759 | // Symbols with no order assigned should be placed at the end. |
| 1760 | // (e.g. section end labels) |
| 1761 | if (IA == 0) |
| 1762 | return false; |
| 1763 | if (IB == 0) |
| 1764 | return true; |
| 1765 | return IA < IB; |
| 1766 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1767 | |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1768 | // Insert a final terminator. |
| 1769 | List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); |
| 1770 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1771 | // Build spans between each label. |
| 1772 | const MCSymbol *StartSym = List[0].Sym; |
| 1773 | for (size_t n = 1, e = List.size(); n < e; n++) { |
| 1774 | const SymbolCU &Prev = List[n - 1]; |
| 1775 | const SymbolCU &Cur = List[n]; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1776 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1777 | // Try and build the longest span we can within the same CU. |
| 1778 | if (Cur.CU != Prev.CU) { |
| 1779 | ArangeSpan Span; |
| 1780 | Span.Start = StartSym; |
| 1781 | Span.End = Cur.Sym; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1782 | assert(Prev.CU); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1783 | Spans[Prev.CU].push_back(Span); |
| 1784 | StartSym = Cur.Sym; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1789 | // Start the dwarf aranges section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1790 | Asm->OutStreamer->SwitchSection( |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1791 | Asm->getObjFileLowering().getDwarfARangesSection()); |
| 1792 | |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1793 | unsigned PtrSize = Asm->MAI->getCodePointerSize(); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1794 | |
| 1795 | // Build a list of CUs used. |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1796 | std::vector<DwarfCompileUnit *> CUs; |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1797 | for (const auto &it : Spans) { |
| 1798 | DwarfCompileUnit *CU = it.first; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1799 | CUs.push_back(CU); |
| 1800 | } |
| 1801 | |
| 1802 | // Sort the CU list (again, to ensure consistent output order). |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 1803 | std::sort(CUs.begin(), CUs.end(), |
| 1804 | [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { |
| 1805 | return A->getUniqueID() < B->getUniqueID(); |
| 1806 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1807 | |
| 1808 | // Emit an arange table for each CU we used. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1809 | for (DwarfCompileUnit *CU : CUs) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1810 | std::vector<ArangeSpan> &List = Spans[CU]; |
| 1811 | |
David Blaikie | 27e35f2 | 2014-11-02 01:21:43 +0000 | [diff] [blame] | 1812 | // Describe the skeleton CU's offset and length, not the dwo file's. |
| 1813 | if (auto *Skel = CU->getSkeleton()) |
| 1814 | CU = Skel; |
| 1815 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1816 | // Emit size of content not including length itself. |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 1817 | unsigned ContentSize = |
| 1818 | sizeof(int16_t) + // DWARF ARange version number |
| 1819 | sizeof(int32_t) + // Offset of CU in the .debug_info section |
| 1820 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 1821 | sizeof(int8_t); // Segment Size (in bytes) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1822 | |
| 1823 | unsigned TupleSize = PtrSize * 2; |
| 1824 | |
| 1825 | // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. |
Benjamin Kramer | 8a68ab3 | 2014-01-07 19:28:14 +0000 | [diff] [blame] | 1826 | unsigned Padding = |
| 1827 | OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1828 | |
| 1829 | ContentSize += Padding; |
| 1830 | ContentSize += (List.size() + 1) * TupleSize; |
| 1831 | |
| 1832 | // For each compile unit, write the list of spans it covers. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1833 | Asm->OutStreamer->AddComment("Length of ARange Set"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1834 | Asm->EmitInt32(ContentSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1835 | Asm->OutStreamer->AddComment("DWARF Arange version number"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1836 | Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1837 | Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); |
Rafael Espindola | 857546e | 2015-06-16 23:22:02 +0000 | [diff] [blame] | 1838 | Asm->emitDwarfSymbolReference(CU->getLabelBegin()); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1839 | Asm->OutStreamer->AddComment("Address Size (in bytes)"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1840 | Asm->EmitInt8(PtrSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1841 | Asm->OutStreamer->AddComment("Segment Size (in bytes)"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1842 | Asm->EmitInt8(0); |
| 1843 | |
Petr Hosek | faef320 | 2016-06-01 01:59:58 +0000 | [diff] [blame] | 1844 | Asm->OutStreamer->emitFill(Padding, 0xff); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1845 | |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1846 | for (const ArangeSpan &Span : List) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1847 | Asm->EmitLabelReference(Span.Start, PtrSize); |
| 1848 | |
| 1849 | // Calculate the size as being from the span start to it's end. |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1850 | if (Span.End) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1851 | Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1852 | } else { |
| 1853 | // For symbols without an end marker (e.g. common), we |
| 1854 | // write a single arange entry containing just that one symbol. |
| 1855 | uint64_t Size = SymSize[Span.Start]; |
| 1856 | if (Size == 0) |
| 1857 | Size = 1; |
| 1858 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1859 | Asm->OutStreamer->EmitIntValue(Size, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1860 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1863 | Asm->OutStreamer->AddComment("ARange terminator"); |
| 1864 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
| 1865 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1866 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1869 | /// Emit address ranges into a debug ranges section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1870 | void DwarfDebug::emitDebugRanges() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1871 | if (CUMap.empty()) |
| 1872 | return; |
| 1873 | |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1874 | // Start the dwarf ranges section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1875 | Asm->OutStreamer->SwitchSection( |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 1876 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Eric Christopher | 4751d70 | 2013-11-23 00:05:29 +0000 | [diff] [blame] | 1877 | |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1878 | // Size for our labels. |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1879 | unsigned char Size = Asm->MAI->getCodePointerSize(); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1880 | |
| 1881 | // Grab the specific ranges for the compile units in the module. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1882 | for (const auto &I : CUMap) { |
| 1883 | DwarfCompileUnit *TheCU = I.second; |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1884 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1885 | if (auto *Skel = TheCU->getSkeleton()) |
| 1886 | TheCU = Skel; |
| 1887 | |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1888 | // Iterate over the misc ranges for the compile units in the module. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1889 | for (const RangeSpanList &List : TheCU->getRangeLists()) { |
Eric Christopher | f879064 | 2013-12-04 22:04:50 +0000 | [diff] [blame] | 1890 | // Emit our symbol so we can find the beginning of the range. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1891 | Asm->OutStreamer->EmitLabel(List.getSym()); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1892 | |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1893 | // Gather all the ranges that apply to the same section so they can share |
| 1894 | // a base address entry. |
| 1895 | MapVector<const MCSection *, std::vector<const RangeSpan *>> MV; |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1896 | for (const RangeSpan &Range : List.getRanges()) { |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1897 | MV[&Range.getStart()->getSection()].push_back(&Range); |
| 1898 | } |
| 1899 | |
| 1900 | auto *CUBase = TheCU->getBaseAddress(); |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1901 | bool BaseIsSet = false; |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1902 | for (const auto &P : MV) { |
| 1903 | // Don't bother with a base address entry if there's only one range in |
| 1904 | // this section in this range list - for example ranges for a CU will |
| 1905 | // usually consist of single regions from each of many sections |
| 1906 | // (-ffunction-sections, or just C++ inline functions) except under LTO |
| 1907 | // or optnone where there may be holes in a single CU's section |
| 1908 | // contrubutions. |
| 1909 | auto *Base = CUBase; |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 1910 | if (!Base && P.second.size() > 1 && |
| 1911 | UseDwarfRangesBaseAddressSpecifier) { |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1912 | BaseIsSet = true; |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1913 | // FIXME/use care: This may not be a useful base address if it's not |
| 1914 | // the lowest address/range in this object. |
| 1915 | Base = P.second.front()->getStart(); |
| 1916 | Asm->OutStreamer->EmitIntValue(-1, Size); |
| 1917 | Asm->OutStreamer->EmitSymbolValue(Base, Size); |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1918 | } else if (BaseIsSet) { |
| 1919 | BaseIsSet = false; |
| 1920 | Asm->OutStreamer->EmitIntValue(-1, Size); |
| 1921 | Asm->OutStreamer->EmitIntValue(0, Size); |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | for (const auto *RS : P.second) { |
| 1925 | const MCSymbol *Begin = RS->getStart(); |
| 1926 | const MCSymbol *End = RS->getEnd(); |
| 1927 | assert(Begin && "Range without a begin symbol?"); |
| 1928 | assert(End && "Range without an end symbol?"); |
| 1929 | if (Base) { |
| 1930 | Asm->EmitLabelDifference(Begin, Base, Size); |
| 1931 | Asm->EmitLabelDifference(End, Base, Size); |
| 1932 | } else { |
| 1933 | Asm->OutStreamer->EmitSymbolValue(Begin, Size); |
| 1934 | Asm->OutStreamer->EmitSymbolValue(End, Size); |
| 1935 | } |
Eric Christopher | ece0e90 | 2014-04-25 22:23:54 +0000 | [diff] [blame] | 1936 | } |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | // And terminate the list with two 0 values. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1940 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 1941 | Asm->OutStreamer->EmitIntValue(0, Size); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1942 | } |
Devang Patel | 12563b3 | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1943 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1946 | void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1947 | for (auto *MN : Nodes) { |
| 1948 | if (auto *M = dyn_cast<DIMacro>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1949 | emitMacro(*M); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1950 | else if (auto *F = dyn_cast<DIMacroFile>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1951 | emitMacroFile(*F, U); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1952 | else |
| 1953 | llvm_unreachable("Unexpected DI type!"); |
| 1954 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1957 | void DwarfDebug::emitMacro(DIMacro &M) { |
| 1958 | Asm->EmitULEB128(M.getMacinfoType()); |
| 1959 | Asm->EmitULEB128(M.getLine()); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1960 | StringRef Name = M.getName(); |
| 1961 | StringRef Value = M.getValue(); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1962 | Asm->OutStreamer->EmitBytes(Name); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1963 | if (!Value.empty()) { |
| 1964 | // There should be one space between macro name and macro value. |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1965 | Asm->EmitInt8(' '); |
| 1966 | Asm->OutStreamer->EmitBytes(Value); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1967 | } |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1968 | Asm->EmitInt8('\0'); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1971 | void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1972 | assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1973 | Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); |
| 1974 | Asm->EmitULEB128(F.getLine()); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1975 | DIFile *File = F.getFile(); |
| 1976 | unsigned FID = |
| 1977 | U.getOrCreateSourceID(File->getFilename(), File->getDirectory()); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1978 | Asm->EmitULEB128(FID); |
| 1979 | handleMacroNodes(F.getElements(), U); |
| 1980 | Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1983 | /// Emit macros into a debug macinfo section. |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1984 | void DwarfDebug::emitDebugMacinfo() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1985 | if (CUMap.empty()) |
| 1986 | return; |
| 1987 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1988 | // Start the dwarf macinfo section. |
| 1989 | Asm->OutStreamer->SwitchSection( |
| 1990 | Asm->getObjFileLowering().getDwarfMacinfoSection()); |
| 1991 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1992 | for (const auto &P : CUMap) { |
| 1993 | auto &TheCU = *P.second; |
| 1994 | auto *SkCU = TheCU.getSkeleton(); |
| 1995 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
| 1996 | auto *CUNode = cast<DICompileUnit>(P.first); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 1997 | Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); |
| 1998 | handleMacroNodes(CUNode->getMacros(), U); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 1999 | } |
| 2000 | Asm->OutStreamer->AddComment("End Of Macro List Mark"); |
| 2001 | Asm->EmitInt8(0); |
| 2002 | } |
| 2003 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2004 | // DWARF5 Experimental Separate Dwarf emitters. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2005 | |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2006 | void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2007 | std::unique_ptr<DwarfCompileUnit> NewU) { |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 2008 | NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name, |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 2009 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2010 | |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2011 | if (!CompilationDir.empty()) |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 2012 | NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2013 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2014 | addGnuPubAttributes(*NewU, Die); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2015 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2016 | SkeletonHolder.addUnit(std::move(NewU)); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2019 | // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list, |
| 2020 | // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id, |
David Blaikie | 96dea05 | 2014-03-24 21:31:35 +0000 | [diff] [blame] | 2021 | // DW_AT_addr_base, DW_AT_ranges_base. |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2022 | DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2023 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2024 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
David Blaikie | bd57905 | 2014-04-28 21:14:27 +0000 | [diff] [blame] | 2025 | CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2026 | DwarfCompileUnit &NewCU = *OwnedUnit; |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2027 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
Eric Christopher | 4c7765f | 2013-01-17 03:00:04 +0000 | [diff] [blame] | 2028 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2029 | NewCU.initStmtList(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2030 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2031 | initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 2032 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2033 | return NewCU; |
| 2034 | } |
| 2035 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2036 | // Emit the .debug_info.dwo section for separated dwarf. This contains the |
| 2037 | // compile units that would normally be in debug_info. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2038 | void DwarfDebug::emitDebugInfoDWO() { |
Eric Christopher | cdf218d | 2012-12-10 19:51:21 +0000 | [diff] [blame] | 2039 | assert(useSplitDwarf() && "No split dwarf debug info?"); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2040 | // Don't emit relocations into the dwo file. |
| 2041 | InfoHolder.emitUnits(/* UseOffsets */ true); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the |
| 2045 | // abbreviations for the .debug_info.dwo section. |
| 2046 | void DwarfDebug::emitDebugAbbrevDWO() { |
| 2047 | assert(useSplitDwarf() && "No split dwarf?"); |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 2048 | InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2049 | } |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2050 | |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2051 | void DwarfDebug::emitDebugLineDWO() { |
| 2052 | assert(useSplitDwarf() && "No split dwarf?"); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2053 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2054 | Asm->getObjFileLowering().getDwarfLineDWOSection()); |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 2055 | SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams()); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2058 | // Emit the .debug_str.dwo section for separated dwarf. This contains the |
| 2059 | // string section and is identical in format to traditional .debug_str |
| 2060 | // sections. |
| 2061 | void DwarfDebug::emitDebugStrDWO() { |
| 2062 | assert(useSplitDwarf() && "No split dwarf?"); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 2063 | MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); |
Eric Christopher | 2cbd576 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 2064 | InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 2065 | OffSec); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2066 | } |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2067 | |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2068 | MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { |
| 2069 | if (!useSplitDwarf()) |
| 2070 | return nullptr; |
| 2071 | if (SingleCU) |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 2072 | SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory()); |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2073 | return &SplitTypeUnitFileTable; |
| 2074 | } |
| 2075 | |
Adrian Prantl | ee5feaf | 2015-07-15 17:01:41 +0000 | [diff] [blame] | 2076 | uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2077 | MD5 Hash; |
| 2078 | Hash.update(Identifier); |
| 2079 | // ... take the least significant 8 bytes and return those. Our MD5 |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2080 | // implementation always returns its results in little endian, so we actually |
| 2081 | // need the "high" word. |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2082 | MD5::MD5Result Result; |
| 2083 | Hash.final(Result); |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2084 | return Result.high(); |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
David Blaikie | 15632ae | 2014-02-12 00:31:30 +0000 | [diff] [blame] | 2087 | void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2088 | StringRef Identifier, DIE &RefDie, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2089 | const DICompositeType *CTy) { |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2090 | // Fast path if we're building some type units and one has already used the |
| 2091 | // address pool we know we're going to throw away all this work anyway, so |
| 2092 | // don't bother building dependent types. |
| 2093 | if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) |
| 2094 | return; |
| 2095 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2096 | auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); |
| 2097 | if (!Ins.second) { |
| 2098 | CU.addDIETypeSignature(RefDie, Ins.first->second); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2099 | return; |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2102 | bool TopLevelType = TypeUnitsUnderConstruction.empty(); |
| 2103 | AddrPool.resetUsedFlag(); |
| 2104 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2105 | auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, |
| 2106 | getDwoLineTable(CU)); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2107 | DwarfTypeUnit &NewTU = *OwnedUnit; |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2108 | DIE &UnitDie = NewTU.getUnitDie(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2109 | TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2110 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2111 | NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
David Blaikie | 637cac4 | 2014-04-22 23:09:36 +0000 | [diff] [blame] | 2112 | CU.getLanguage()); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2113 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2114 | uint64_t Signature = makeTypeSignature(Identifier); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2115 | NewTU.setTypeSignature(Signature); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2116 | Ins.first->second = Signature; |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2117 | |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2118 | if (useSplitDwarf()) |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2119 | NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection()); |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2120 | else { |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2121 | CU.applyStmtList(UnitDie); |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2122 | NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature)); |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2123 | } |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2124 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2125 | NewTU.setType(NewTU.createTypeDIE(CTy)); |
| 2126 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2127 | if (TopLevelType) { |
| 2128 | auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); |
| 2129 | TypeUnitsUnderConstruction.clear(); |
| 2130 | |
| 2131 | // Types referencing entries in the address table cannot be placed in type |
| 2132 | // units. |
| 2133 | if (AddrPool.hasBeenUsed()) { |
| 2134 | |
| 2135 | // Remove all the types built while building this type. |
| 2136 | // This is pessimistic as some of these types might not be dependent on |
| 2137 | // the type that used an address. |
| 2138 | for (const auto &TU : TypeUnitsToAdd) |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2139 | TypeSignatures.erase(TU.second); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2140 | |
| 2141 | // Construct this type in the CU directly. |
| 2142 | // This is inefficient because all the dependent types will be rebuilt |
| 2143 | // from scratch, including building them in type units, discovering that |
| 2144 | // they depend on addresses, throwing them out and rebuilding them. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2145 | CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | // If the type wasn't dependent on fission addresses, finish adding the type |
| 2150 | // and all its dependent types. |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2151 | for (auto &TU : TypeUnitsToAdd) { |
| 2152 | InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); |
| 2153 | InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); |
| 2154 | } |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2155 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2156 | CU.addDIETypeSignature(RefDie, Signature); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2157 | } |
David Blaikie | 4bd13b7 | 2014-03-07 18:49:45 +0000 | [diff] [blame] | 2158 | |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2159 | // Accelerator table mutators - add each name along with its companion |
| 2160 | // DIE to the proper table while ensuring that the name that we're going |
| 2161 | // to reference is in the string table. We do this since the names we |
| 2162 | // add may not only be identical to the names in the DIE. |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2163 | void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) { |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2164 | if (!useDwarfAccelTables()) |
| 2165 | return; |
Duncan P. N. Exon Smith | f459994 | 2015-05-24 16:44:32 +0000 | [diff] [blame] | 2166 | AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2167 | } |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2168 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2169 | void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) { |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2170 | if (!useDwarfAccelTables()) |
| 2171 | return; |
Duncan P. N. Exon Smith | f459994 | 2015-05-24 16:44:32 +0000 | [diff] [blame] | 2172 | AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2173 | } |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2174 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2175 | void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) { |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2176 | if (!useDwarfAccelTables()) |
| 2177 | return; |
Duncan P. N. Exon Smith | f459994 | 2015-05-24 16:44:32 +0000 | [diff] [blame] | 2178 | AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2179 | } |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2180 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2181 | void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) { |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2182 | if (!useDwarfAccelTables()) |
| 2183 | return; |
Duncan P. N. Exon Smith | f459994 | 2015-05-24 16:44:32 +0000 | [diff] [blame] | 2184 | AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2185 | } |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 2186 | |
| 2187 | uint16_t DwarfDebug::getDwarfVersion() const { |
| 2188 | return Asm->OutStreamer->getContext().getDwarfVersion(); |
| 2189 | } |