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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains support for writing dwarf debug info into asm files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Chris Lattner | b14490d | 2010-03-09 00:39:24 +0000 | [diff] [blame] | 12 | |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 13 | #include "DwarfDebug.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 14 | #include "ByteStreamer.h" |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 15 | #include "DIEHash.h" |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 16 | #include "DebugLocEntry.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 17 | #include "DebugLocStream.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "DwarfCompileUnit.h" |
| 19 | #include "DwarfExpression.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 20 | #include "DwarfFile.h" |
David Blaikie | 2c86a72 | 2013-12-02 19:33:15 +0000 | [diff] [blame] | 21 | #include "DwarfUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/APInt.h" |
| 23 | #include "llvm/ADT/DenseMap.h" |
| 24 | #include "llvm/ADT/DenseSet.h" |
| 25 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 31 | #include "llvm/BinaryFormat/Dwarf.h" |
Jonas Devlieghere | 855fc3b | 2018-01-29 14:52:41 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/AccelTable.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | e541e0b | 2015-01-05 21:29:41 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/DIE.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/LexicalScopes.h" |
| 36 | #include "llvm/CodeGen/MachineBasicBlock.h" |
David Greene | 829b3e8 | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineOperand.h" |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 43 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 45 | #include "llvm/IR/DebugInfoMetadata.h" |
| 46 | #include "llvm/IR/DebugLoc.h" |
| 47 | #include "llvm/IR/Function.h" |
| 48 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Module.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 50 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 51 | #include "llvm/MC/MCContext.h" |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 52 | #include "llvm/MC/MCDwarf.h" |
Chris Lattner | 4d2c0f9 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 53 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 4b7dadb | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 54 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 55 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 56 | #include "llvm/MC/MCTargetOptions.h" |
| 57 | #include "llvm/MC/MachineLocation.h" |
| 58 | #include "llvm/MC/SectionKind.h" |
| 59 | #include "llvm/Pass.h" |
| 60 | #include "llvm/Support/Casting.h" |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 61 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | cdf01b5 | 2009-10-13 06:47:08 +0000 | [diff] [blame] | 62 | #include "llvm/Support/Debug.h" |
| 63 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 6764643 | 2013-07-26 17:02:41 +0000 | [diff] [blame] | 64 | #include "llvm/Support/MD5.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 65 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 66 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 67 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 68 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 69 | #include "llvm/Target/TargetMachine.h" |
| 70 | #include "llvm/Target/TargetOptions.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 | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 92 | static cl::opt<bool> GenerateARangeSection("generate-arange-section", |
| 93 | cl::Hidden, |
| 94 | cl::desc("Generate dwarf aranges"), |
| 95 | cl::init(false)); |
| 96 | |
Pavel Labath | f9adc20 | 2018-07-20 12:59:05 +0000 | [diff] [blame] | 97 | static cl::opt<bool> |
| 98 | GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, |
| 99 | cl::desc("Generate DWARF4 type units."), |
| 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 | |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 115 | static cl::opt<AccelTableKind> AccelTables( |
| 116 | "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."), |
| 117 | cl::values(clEnumValN(AccelTableKind::Default, "Default", |
| 118 | "Default for platform"), |
| 119 | clEnumValN(AccelTableKind::None, "Disable", "Disabled."), |
| 120 | clEnumValN(AccelTableKind::Apple, "Apple", "Apple"), |
| 121 | clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")), |
| 122 | cl::init(AccelTableKind::Default)); |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 123 | |
Alexey Bataev | 0d6aead | 2018-02-20 15:28:08 +0000 | [diff] [blame] | 124 | static cl::opt<DefaultOnOff> |
| 125 | DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, |
| 126 | cl::desc("Use inlined strings rather than string section."), |
| 127 | cl::values(clEnumVal(Default, "Default for platform"), |
| 128 | clEnumVal(Enable, "Enabled"), |
| 129 | clEnumVal(Disable, "Disabled")), |
| 130 | cl::init(Default)); |
| 131 | |
Alexey Bataev | 648ed2d | 2018-03-20 16:04:40 +0000 | [diff] [blame] | 132 | static cl::opt<bool> |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 133 | NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, |
| 134 | cl::desc("Disable emission .debug_ranges section."), |
| 135 | cl::init(false)); |
| 136 | |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 137 | static cl::opt<DefaultOnOff> DwarfSectionsAsReferences( |
| 138 | "dwarf-sections-as-references", cl::Hidden, |
| 139 | cl::desc("Use sections+offset as references rather than labels."), |
| 140 | cl::values(clEnumVal(Default, "Default for platform"), |
| 141 | clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), |
| 142 | cl::init(Default)); |
| 143 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 144 | enum LinkageNameOption { |
| 145 | DefaultLinkageNames, |
| 146 | AllLinkageNames, |
| 147 | AbstractLinkageNames |
| 148 | }; |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 149 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 150 | static cl::opt<LinkageNameOption> |
| 151 | DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, |
| 152 | cl::desc("Which DWARF linkage-name attributes to emit."), |
| 153 | cl::values(clEnumValN(DefaultLinkageNames, "Default", |
| 154 | "Default for platform"), |
| 155 | clEnumValN(AllLinkageNames, "All", "All"), |
| 156 | clEnumValN(AbstractLinkageNames, "Abstract", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 157 | "Abstract subprograms")), |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 158 | cl::init(DefaultLinkageNames)); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 159 | |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 160 | static const char *const DWARFGroupName = "dwarf"; |
| 161 | static const char *const DWARFGroupDescription = "DWARF Emission"; |
| 162 | static const char *const DbgTimerName = "writer"; |
| 163 | static const char *const DbgTimerDescription = "DWARF Debug Writer"; |
Bill Wendling | fcc1414 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 164 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 165 | void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 166 | BS.EmitInt8( |
| 167 | Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) |
| 168 | : dwarf::OperationEncodingString(Op)); |
| 169 | } |
| 170 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 171 | void DebugLocDwarfExpression::emitSigned(int64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 172 | BS.EmitSLEB128(Value, Twine(Value)); |
| 173 | } |
| 174 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 175 | void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 176 | BS.EmitULEB128(Value, Twine(Value)); |
| 177 | } |
| 178 | |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 179 | bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, |
| 180 | unsigned MachineReg) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 181 | // This information is not available while emitting .debug_loc entries. |
| 182 | return false; |
| 183 | } |
| 184 | |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 185 | bool DbgVariable::isBlockByrefVariable() const { |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 186 | assert(getVariable() && "Invalid complex DbgVariable!"); |
| 187 | return getVariable()->getType().resolve()->isBlockByrefStruct(); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 190 | const DIType *DbgVariable::getType() const { |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 191 | DIType *Ty = getVariable()->getType().resolve(); |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 192 | // FIXME: isBlockByrefVariable should be reformulated in terms of complex |
| 193 | // addresses instead. |
Duncan P. N. Exon Smith | 571baeb | 2015-04-14 01:59:58 +0000 | [diff] [blame] | 194 | if (Ty->isBlockByrefStruct()) { |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 195 | /* Byref variables, in Blocks, are declared by the programmer as |
| 196 | "SomeType VarName;", but the compiler creates a |
| 197 | __Block_byref_x_VarName struct, and gives the variable VarName |
| 198 | either the struct, or a pointer to the struct, as its type. This |
| 199 | is necessary for various behind-the-scenes things the compiler |
| 200 | needs to do with by-reference variables in blocks. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 201 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 202 | However, as far as the original *programmer* is concerned, the |
| 203 | variable should still have type 'SomeType', as originally declared. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 204 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 205 | The following function dives into the __Block_byref_x_VarName |
| 206 | struct to find the original type of the variable. This will be |
| 207 | passed back to the code generating the type for the Debug |
| 208 | Information Entry for the variable 'VarName'. 'VarName' will then |
| 209 | have the original type 'SomeType' in its debug information. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 210 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 211 | The original type 'SomeType' will be the type of the field named |
| 212 | 'VarName' inside the __Block_byref_x_VarName struct. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 213 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 214 | NOTE: In order for this to not completely fail on the debugger |
| 215 | side, the Debug Information Entry for the variable VarName needs to |
| 216 | have a DW_AT_location that tells the debugger how to unwind through |
| 217 | the pointers and __Block_byref_x_VarName struct to find the actual |
| 218 | value of the variable. The function addBlockByrefType does this. */ |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 219 | DIType *subType = Ty; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 220 | uint16_t tag = Ty->getTag(); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 221 | |
Eric Christopher | 9adc55f | 2013-09-04 19:53:21 +0000 | [diff] [blame] | 222 | if (tag == dwarf::DW_TAG_pointer_type) |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 223 | subType = resolve(cast<DIDerivedType>(Ty)->getBaseType()); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 224 | |
Duncan P. N. Exon Smith | 6ac940d | 2015-07-24 18:17:17 +0000 | [diff] [blame] | 225 | auto Elements = cast<DICompositeType>(subType)->getElements(); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 226 | 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] | 227 | auto *DT = cast<DIDerivedType>(Elements[i]); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 228 | if (getName() == DT->getName()) |
Duncan P. N. Exon Smith | 848af38 | 2015-04-20 18:20:03 +0000 | [diff] [blame] | 229 | return resolve(DT->getBaseType()); |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 230 | } |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 231 | } |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 232 | return Ty; |
| 233 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 234 | |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 235 | ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 236 | if (FrameIndexExprs.size() == 1) |
| 237 | return FrameIndexExprs; |
| 238 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 239 | assert(llvm::all_of(FrameIndexExprs, |
| 240 | [](const FrameIndexExpr &A) { |
| 241 | return A.Expr->isFragment(); |
| 242 | }) && |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 243 | "multiple FI expressions without DW_OP_LLVM_fragment"); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 244 | llvm::sort(FrameIndexExprs, |
Mandeep Singh Grang | e92f0cf | 2018-04-06 18:08:42 +0000 | [diff] [blame] | 245 | [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { |
| 246 | return A.Expr->getFragmentInfo()->OffsetInBits < |
| 247 | B.Expr->getFragmentInfo()->OffsetInBits; |
| 248 | }); |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 249 | |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 250 | return FrameIndexExprs; |
| 251 | } |
| 252 | |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 253 | void DbgVariable::addMMIEntry(const DbgVariable &V) { |
| 254 | assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry"); |
| 255 | assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry"); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 256 | assert(V.getVariable() == getVariable() && "conflicting variable"); |
| 257 | assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location"); |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 258 | |
| 259 | assert(!FrameIndexExprs.empty() && "Expected an MMI entry"); |
| 260 | assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry"); |
| 261 | |
Daniel Jasper | 4d93120 | 2017-10-12 13:25:05 +0000 | [diff] [blame] | 262 | // FIXME: This logic should not be necessary anymore, as we now have proper |
| 263 | // deduplication. However, without it, we currently run into the assertion |
| 264 | // below, which means that we are likely dealing with broken input, i.e. two |
| 265 | // non-fragment entries for the same variable at different frame indices. |
| 266 | if (FrameIndexExprs.size()) { |
| 267 | auto *Expr = FrameIndexExprs.back().Expr; |
| 268 | if (!Expr || !Expr->isFragment()) |
| 269 | return; |
| 270 | } |
| 271 | |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 272 | for (const auto &FIE : V.FrameIndexExprs) |
| 273 | // Ignore duplicate entries. |
| 274 | if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) { |
| 275 | return FIE.FI == Other.FI && FIE.Expr == Other.Expr; |
| 276 | })) |
| 277 | FrameIndexExprs.push_back(FIE); |
| 278 | |
| 279 | assert((FrameIndexExprs.size() == 1 || |
| 280 | llvm::all_of(FrameIndexExprs, |
| 281 | [](FrameIndexExpr &FIE) { |
| 282 | return FIE.Expr && FIE.Expr->isFragment(); |
| 283 | })) && |
| 284 | "conflicting locations for variable"); |
| 285 | } |
| 286 | |
Pavel Labath | f9adc20 | 2018-07-20 12:59:05 +0000 | [diff] [blame] | 287 | static AccelTableKind computeAccelTableKind(unsigned DwarfVersion, |
| 288 | bool GenerateTypeUnits, |
| 289 | DebuggerKind Tuning, |
| 290 | const Triple &TT) { |
| 291 | // Honor an explicit request. |
| 292 | if (AccelTables != AccelTableKind::Default) |
| 293 | return AccelTables; |
| 294 | |
| 295 | // Accelerator tables with type units are currently not supported. |
| 296 | if (GenerateTypeUnits) |
| 297 | return AccelTableKind::None; |
| 298 | |
| 299 | // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5 |
| 300 | // always implies debug_names. For lower standard versions we use apple |
| 301 | // accelerator tables on apple platforms and debug_names elsewhere. |
| 302 | if (DwarfVersion >= 5) |
| 303 | return AccelTableKind::Dwarf; |
| 304 | if (Tuning == DebuggerKind::LLDB) |
| 305 | return TT.isOSBinFormatMachO() ? AccelTableKind::Apple |
| 306 | : AccelTableKind::Dwarf; |
| 307 | return AccelTableKind::None; |
| 308 | } |
| 309 | |
Chris Lattner | f0d6bd3 | 2010-04-05 05:11:15 +0000 | [diff] [blame] | 310 | DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 311 | : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), |
| 312 | InfoHolder(A, "info_string", DIEValueAllocator), |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 313 | SkeletonHolder(A, "skel_string", DIEValueAllocator), |
Pavel Labath | a7c457d | 2018-02-19 16:12:20 +0000 | [diff] [blame] | 314 | IsDarwin(A->TM.getTargetTriple().isOSDarwin()) { |
Eric Christopher | 364dbe0 | 2016-10-01 01:50:29 +0000 | [diff] [blame] | 315 | const Triple &TT = Asm->TM.getTargetTriple(); |
Eric Christopher | ad9fe89 | 2012-04-02 17:58:52 +0000 | [diff] [blame] | 316 | |
Tom Weaver | 4db70d9 | 2019-01-25 16:29:35 +0000 | [diff] [blame] | 317 | // Make sure we know our "debugger tuning". The target option takes |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 318 | // precedence; fall back to triple-based defaults. |
Paul Robinson | 6c27a2c | 2015-12-16 19:58:30 +0000 | [diff] [blame] | 319 | if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) |
| 320 | DebuggerTuning = Asm->TM.Options.DebuggerTuning; |
Dimitry Andric | 2c36421 | 2016-01-07 22:09:12 +0000 | [diff] [blame] | 321 | else if (IsDarwin) |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 322 | DebuggerTuning = DebuggerKind::LLDB; |
| 323 | else if (TT.isPS4CPU()) |
| 324 | DebuggerTuning = DebuggerKind::SCE; |
| 325 | else |
| 326 | DebuggerTuning = DebuggerKind::GDB; |
| 327 | |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 328 | if (DwarfInlinedStrings == Default) |
| 329 | UseInlineStrings = TT.isNVPTX(); |
| 330 | else |
| 331 | UseInlineStrings = DwarfInlinedStrings == Enable; |
| 332 | |
Alexey Bataev | 2a03d42 | 2018-06-29 14:23:28 +0000 | [diff] [blame] | 333 | UseLocSection = !TT.isNVPTX(); |
| 334 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 335 | HasAppleExtensionAttributes = tuneForLLDB(); |
| 336 | |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 337 | // Handle split DWARF. |
| 338 | HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); |
Eric Christopher | 2942431 | 2012-11-12 22:22:20 +0000 | [diff] [blame] | 339 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 340 | // SCE defaults to linkage names only for abstract subprograms. |
| 341 | if (DwarfLinkageNames == DefaultLinkageNames) |
| 342 | UseAllLinkageNames = !tuneForSCE(); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 343 | else |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 344 | UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 345 | |
Eric Christopher | 4c5bff3 | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 346 | unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 347 | unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber |
Eric Christopher | 793c747 | 2014-04-28 20:42:22 +0000 | [diff] [blame] | 348 | : MMI->getModule()->getDwarfVersion(); |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 349 | // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2. |
| 350 | DwarfVersion = |
| 351 | TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION); |
Manman Ren | ac8062b | 2013-07-02 23:40:10 +0000 | [diff] [blame] | 352 | |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 353 | UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX(); |
Alexey Bataev | 648ed2d | 2018-03-20 16:04:40 +0000 | [diff] [blame] | 354 | |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 355 | // Use sections as references. Force for NVPTX. |
| 356 | if (DwarfSectionsAsReferences == Default) |
| 357 | UseSectionsAsReferences = TT.isNVPTX(); |
| 358 | else |
| 359 | UseSectionsAsReferences = DwarfSectionsAsReferences == Enable; |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 360 | |
Jonas Devlieghere | 8acb74e | 2018-08-01 12:53:06 +0000 | [diff] [blame] | 361 | // Don't generate type units for unsupported object file formats. |
| 362 | GenerateTypeUnits = |
| 363 | A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits; |
Pavel Labath | f9adc20 | 2018-07-20 12:59:05 +0000 | [diff] [blame] | 364 | |
| 365 | TheAccelTableKind = computeAccelTableKind( |
| 366 | DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple()); |
| 367 | |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 368 | // Work around a GDB bug. GDB doesn't support the standard opcode; |
| 369 | // SCE doesn't support GNU's; LLDB prefers the standard opcode, which |
| 370 | // is defined as of DWARF 3. |
| 371 | // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented |
| 372 | // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 |
| 373 | UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; |
Paul Robinson | 78cc082 | 2015-03-04 20:55:11 +0000 | [diff] [blame] | 374 | |
Adrian Prantl | 6323ddf | 2016-05-17 21:07:16 +0000 | [diff] [blame] | 375 | // GDB does not fully support the DWARF 4 representation for bitfields. |
| 376 | UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); |
| 377 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 378 | // The DWARF v5 string offsets table has - possibly shared - contributions |
| 379 | // from each compile and type unit each preceded by a header. The string |
| 380 | // offsets table used by the pre-DWARF v5 split-DWARF implementation uses |
| 381 | // a monolithic string offsets table without any header. |
| 382 | UseSegmentedStringOffsetsTable = DwarfVersion >= 5; |
| 383 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 384 | Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 385 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 386 | |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 387 | // 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] | 388 | DwarfDebug::~DwarfDebug() = default; |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 389 | |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 390 | static bool isObjCClass(StringRef Name) { |
| 391 | return Name.startswith("+") || Name.startswith("-"); |
| 392 | } |
| 393 | |
| 394 | static bool hasObjCCategory(StringRef Name) { |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 395 | if (!isObjCClass(Name)) |
| 396 | return false; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 397 | |
Benjamin Kramer | 260de74 | 2013-08-24 12:15:54 +0000 | [diff] [blame] | 398 | return Name.find(") ") != StringRef::npos; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void getObjCClassCategory(StringRef In, StringRef &Class, |
| 402 | StringRef &Category) { |
| 403 | if (!hasObjCCategory(In)) { |
| 404 | Class = In.slice(In.find('[') + 1, In.find(' ')); |
| 405 | Category = ""; |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | Class = In.slice(In.find('[') + 1, In.find('(')); |
| 410 | Category = In.slice(In.find('[') + 1, In.find(' ')); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static StringRef getObjCMethodName(StringRef In) { |
| 414 | return In.slice(In.find(' ') + 1, In.find(']')); |
| 415 | } |
| 416 | |
| 417 | // Add the various names to the Dwarf accelerator table names. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 418 | void DwarfDebug::addSubprogramNames(const DICompileUnit &CU, |
| 419 | const DISubprogram *SP, DIE &Die) { |
| 420 | if (getAccelTableKind() != AccelTableKind::Apple && |
| 421 | CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None) |
| 422 | return; |
| 423 | |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 424 | if (!SP->isDefinition()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 425 | return; |
Pavel Labath | 889bf9f | 2018-04-09 08:41:57 +0000 | [diff] [blame] | 426 | |
| 427 | if (SP->getName() != "") |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 428 | addAccelName(CU, SP->getName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 429 | |
Pavel Labath | 2a6afe5 | 2018-05-14 14:13:20 +0000 | [diff] [blame] | 430 | // If the linkage name is different than the name, go ahead and output that as |
| 431 | // well into the name table. Only do that if we are going to actually emit |
| 432 | // that name. |
| 433 | if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() && |
| 434 | (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP))) |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 435 | addAccelName(CU, SP->getLinkageName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 436 | |
| 437 | // If this is an Objective-C selector name add it to the ObjC accelerator |
| 438 | // too. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 439 | if (isObjCClass(SP->getName())) { |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 440 | StringRef Class, Category; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 441 | getObjCClassCategory(SP->getName(), Class, Category); |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 442 | addAccelObjC(CU, Class, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 443 | if (Category != "") |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 444 | addAccelObjC(CU, Category, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 445 | // Also add the base method name to the name table. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 446 | addAccelName(CU, getObjCMethodName(SP->getName()), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 450 | /// Check whether we should create a DIE for the given Scope, return true |
| 451 | /// if we don't create a DIE (the corresponding DIE is null). |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 452 | bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { |
| 453 | if (Scope->isAbstractScope()) |
| 454 | return false; |
| 455 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 456 | // We don't create a DIE if there is no Range. |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 457 | const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); |
| 458 | if (Ranges.empty()) |
| 459 | return true; |
| 460 | |
| 461 | if (Ranges.size() > 1) |
| 462 | return false; |
| 463 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 464 | // We don't create a DIE if we have a single Range and the end label |
| 465 | // is null. |
David Blaikie | cd4b8a2 | 2014-08-31 02:14:26 +0000 | [diff] [blame] | 466 | return !getLabelAfterInsn(Ranges.front().second); |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 469 | template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 470 | F(CU); |
| 471 | if (auto *SkelCU = CU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 472 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 473 | F(*SkelCU); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 474 | } |
| 475 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 476 | bool DwarfDebug::shareAcrossDWOCUs() const { |
| 477 | return SplitDwarfCrossCuReferences; |
| 478 | } |
| 479 | |
| 480 | void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, |
| 481 | LexicalScope *Scope) { |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 482 | assert(Scope && Scope->getScopeNode()); |
| 483 | assert(Scope->isAbstractScope()); |
| 484 | assert(!Scope->getInlinedAt()); |
| 485 | |
David Blaikie | b461468 | 2016-12-14 19:38:39 +0000 | [diff] [blame] | 486 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 487 | |
David Blaikie | 1ea9db2 | 2014-05-21 23:14:12 +0000 | [diff] [blame] | 488 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 489 | // was inlined from another compile unit. |
Mehdi Amini | 4181205 | 2017-05-29 06:25:30 +0000 | [diff] [blame] | 490 | if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) |
| 491 | // Avoid building the original CU if it won't be used |
| 492 | SrcCU.constructAbstractSubprogramScopeDIE(Scope); |
| 493 | else { |
| 494 | auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
| 495 | if (auto *SkelCU = CU.getSkeleton()) { |
| 496 | (shareAcrossDWOCUs() ? CU : SrcCU) |
| 497 | .constructAbstractSubprogramScopeDIE(Scope); |
| 498 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 499 | SkelCU->constructAbstractSubprogramScopeDIE(Scope); |
| 500 | } else |
| 501 | CU.constructAbstractSubprogramScopeDIE(Scope); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 502 | } |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 505 | void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP, |
| 506 | DwarfCompileUnit &CU, DIE &ScopeDIE, |
| 507 | const MachineFunction &MF) { |
| 508 | // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if |
| 509 | // the subprogram is required to have one. |
| 510 | if (!SP.areAllCallsDescribed() || !SP.isDefinition()) |
| 511 | return; |
| 512 | |
| 513 | // Use DW_AT_call_all_calls to express that call site entries are present |
| 514 | // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls |
| 515 | // because one of its requirements is not met: call site entries for |
| 516 | // optimized-out calls are elided. |
| 517 | CU.addFlag(ScopeDIE, dwarf::DW_AT_call_all_calls); |
| 518 | |
| 519 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 520 | assert(TII && "TargetInstrInfo not found: cannot label tail calls"); |
| 521 | |
| 522 | // Emit call site entries for each call or tail call in the function. |
| 523 | for (const MachineBasicBlock &MBB : MF) { |
| 524 | for (const MachineInstr &MI : MBB.instrs()) { |
| 525 | // Skip instructions which aren't calls. Both calls and tail-calling jump |
| 526 | // instructions (e.g TAILJMPd64) are classified correctly here. |
| 527 | if (!MI.isCall()) |
| 528 | continue; |
| 529 | |
| 530 | // TODO: Add support for targets with delay slots (see: beginInstruction). |
| 531 | if (MI.hasDelaySlot()) |
| 532 | return; |
| 533 | |
| 534 | // If this is a direct call, find the callee's subprogram. |
| 535 | const MachineOperand &CalleeOp = MI.getOperand(0); |
| 536 | if (!CalleeOp.isGlobal()) |
| 537 | continue; |
| 538 | const Function *CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal()); |
| 539 | if (!CalleeDecl || !CalleeDecl->getSubprogram()) |
| 540 | continue; |
| 541 | |
| 542 | // TODO: Omit call site entries for runtime calls (objc_msgSend, etc). |
| 543 | // TODO: Add support for indirect calls. |
| 544 | |
| 545 | bool IsTail = TII->isTailCall(MI); |
| 546 | |
| 547 | // For tail calls, no return PC information is needed. For regular calls, |
| 548 | // the return PC is needed to disambiguate paths in the call graph which |
| 549 | // could lead to some target function. |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 550 | const MCExpr *PCOffset = |
| 551 | IsTail ? nullptr : getFunctionLocalOffsetAfterInsn(&MI); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 552 | |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 553 | assert((IsTail || PCOffset) && "Call without return PC information"); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 554 | LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> " |
| 555 | << CalleeDecl->getName() << (IsTail ? " [tail]" : "") |
| 556 | << "\n"); |
| 557 | CU.constructCallSiteEntryDIE(ScopeDIE, *CalleeDecl->getSubprogram(), |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 558 | IsTail, PCOffset); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
David Blaikie | b3cee2f | 2017-05-25 18:50:28 +0000 | [diff] [blame] | 563 | void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 564 | if (!U.hasDwarfPubSections()) |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 565 | return; |
| 566 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 567 | U.addFlag(D, dwarf::DW_AT_GNU_pubnames); |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 568 | } |
| 569 | |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 570 | void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit, |
| 571 | DwarfCompileUnit &NewCU) { |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 572 | DIE &Die = NewCU.getUnitDie(); |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 573 | StringRef FN = DIUnit->getFilename(); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 574 | |
Eric Christopher | 9ea300f | 2017-03-29 23:34:27 +0000 | [diff] [blame] | 575 | StringRef Producer = DIUnit->getProducer(); |
| 576 | StringRef Flags = DIUnit->getFlags(); |
Jonas Devlieghere | caacedb | 2018-08-08 16:33:22 +0000 | [diff] [blame] | 577 | if (!Flags.empty() && !useAppleExtensionAttributes()) { |
Eric Christopher | 9ea300f | 2017-03-29 23:34:27 +0000 | [diff] [blame] | 578 | std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); |
| 579 | NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); |
| 580 | } else |
| 581 | NewCU.addString(Die, dwarf::DW_AT_producer, Producer); |
| 582 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 583 | 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] | 584 | DIUnit->getSourceLanguage()); |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 585 | NewCU.addString(Die, dwarf::DW_AT_name, FN); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 586 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 587 | // Add DW_str_offsets_base to the unit DIE, except for split units. |
| 588 | if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) |
| 589 | NewCU.addStringOffsetsStart(); |
| 590 | |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 591 | if (!useSplitDwarf()) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 592 | NewCU.initStmtList(); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 593 | |
| 594 | // If we're using split dwarf the compilation dir is going to be in the |
| 595 | // skeleton CU and so we don't need to duplicate it here. |
| 596 | if (!CompilationDir.empty()) |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 597 | NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 598 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 599 | addGnuPubAttributes(NewCU, Die); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 600 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 601 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 602 | if (useAppleExtensionAttributes()) { |
| 603 | if (DIUnit->isOptimized()) |
| 604 | NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 605 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 606 | StringRef Flags = DIUnit->getFlags(); |
| 607 | if (!Flags.empty()) |
| 608 | NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 609 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 610 | if (unsigned RVer = DIUnit->getRuntimeVersion()) |
| 611 | NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
| 612 | dwarf::DW_FORM_data1, RVer); |
| 613 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 614 | |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 615 | if (DIUnit->getDWOId()) { |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 616 | // This CU is either a clang module DWO or a skeleton CU. |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 617 | NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, |
| 618 | DIUnit->getDWOId()); |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 619 | if (!DIUnit->getSplitDebugFilename().empty()) |
| 620 | // This is a prefabricated skeleton CU. |
| 621 | NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, |
| 622 | DIUnit->getSplitDebugFilename()); |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 623 | } |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 624 | } |
| 625 | // Create new DwarfCompileUnit for the given metadata node with tag |
| 626 | // DW_TAG_compile_unit. |
| 627 | DwarfCompileUnit & |
| 628 | DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { |
| 629 | if (auto *CU = CUMap.lookup(DIUnit)) |
| 630 | return *CU; |
| 631 | |
| 632 | CompilationDir = DIUnit->getDirectory(); |
| 633 | |
| 634 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
| 635 | InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); |
| 636 | DwarfCompileUnit &NewCU = *OwnedUnit; |
| 637 | InfoHolder.addUnit(std::move(OwnedUnit)); |
| 638 | |
David Blaikie | 693f617 | 2018-12-18 19:40:22 +0000 | [diff] [blame] | 639 | for (auto *IE : DIUnit->getImportedEntities()) |
| 640 | NewCU.addImportedEntity(IE); |
| 641 | |
David Blaikie | b3c56af | 2018-12-20 20:46:55 +0000 | [diff] [blame] | 642 | // LTO with assembly output shares a single line table amongst multiple CUs. |
| 643 | // To avoid the compilation directory being ambiguous, let the line table |
| 644 | // explicitly describe the directory of all files, never relying on the |
| 645 | // compilation directory. |
| 646 | if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) |
| 647 | Asm->OutStreamer->emitDwarfFile0Directive( |
| 648 | CompilationDir, DIUnit->getFilename(), |
| 649 | NewCU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource(), |
| 650 | NewCU.getUniqueID()); |
| 651 | |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 652 | if (useSplitDwarf()) { |
| 653 | NewCU.setSkeleton(constructSkeletonCU(NewCU)); |
| 654 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); |
| 655 | } else { |
| 656 | finishUnitAttributes(DIUnit, NewCU); |
| 657 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
| 658 | } |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 659 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 660 | CUMap.insert({DIUnit, &NewCU}); |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 661 | CUDieMap.insert({&NewCU.getUnitDie(), &NewCU}); |
Devang Patel | eb1bb4e | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 662 | return NewCU; |
Devang Patel | 1a0df9a | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 663 | } |
| 664 | |
David Blaikie | 8912df1 | 2014-08-31 05:41:15 +0000 | [diff] [blame] | 665 | void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 666 | const DIImportedEntity *N) { |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 667 | if (isa<DILocalScope>(N->getScope())) |
| 668 | return; |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 669 | if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) |
| 670 | D->addChild(TheCU.constructImportedEntityDIE(N)); |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 673 | /// Sort and unique GVEs by comparing their fragment offset. |
| 674 | static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & |
| 675 | sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 676 | llvm::sort( |
| 677 | GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { |
| 678 | // Sort order: first null exprs, then exprs without fragment |
| 679 | // info, then sort by fragment offset in bits. |
| 680 | // FIXME: Come up with a more comprehensive comparator so |
| 681 | // the sorting isn't non-deterministic, and so the following |
| 682 | // std::unique call works correctly. |
| 683 | if (!A.Expr || !B.Expr) |
| 684 | return !!B.Expr; |
| 685 | auto FragmentA = A.Expr->getFragmentInfo(); |
| 686 | auto FragmentB = B.Expr->getFragmentInfo(); |
| 687 | if (!FragmentA || !FragmentB) |
| 688 | return !!FragmentB; |
| 689 | return FragmentA->OffsetInBits < FragmentB->OffsetInBits; |
| 690 | }); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 691 | GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), |
| 692 | [](DwarfCompileUnit::GlobalExpr A, |
| 693 | DwarfCompileUnit::GlobalExpr B) { |
| 694 | return A.Expr == B.Expr; |
| 695 | }), |
| 696 | GVEs.end()); |
| 697 | return GVEs; |
| 698 | } |
| 699 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 700 | // Emit all Dwarf sections that should come prior to the content. Create |
| 701 | // global DIEs and emit initial debug info sections. This is invoked by |
| 702 | // the target AsmPrinter. |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 703 | void DwarfDebug::beginModule() { |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 704 | NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, |
| 705 | DWARFGroupDescription, TimePassesIsEnabled); |
Matthias Braun | 8763c0c | 2018-10-31 17:18:41 +0000 | [diff] [blame] | 706 | if (DisableDebugInfoPrinting) { |
| 707 | MMI->setDebugInfoAvailability(false); |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 708 | return; |
Matthias Braun | 8763c0c | 2018-10-31 17:18:41 +0000 | [diff] [blame] | 709 | } |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 710 | |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 711 | const Module *M = MMI->getModule(); |
| 712 | |
Peter Collingbourne | b326986 | 2016-06-01 02:58:40 +0000 | [diff] [blame] | 713 | unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), |
| 714 | M->debug_compile_units_end()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 715 | // Tell MMI whether we have debug info. |
Matthias Braun | 8763c0c | 2018-10-31 17:18:41 +0000 | [diff] [blame] | 716 | assert(MMI->hasDebugInfo() == (NumDebugCUs > 0) && |
| 717 | "DebugInfoAvailabilty initialized unexpectedly"); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 718 | SingleCU = NumDebugCUs == 1; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 719 | DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> |
| 720 | GVMap; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 721 | for (const GlobalVariable &Global : M->globals()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 722 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 723 | Global.getDebugInfo(GVs); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 724 | for (auto *GVE : GVs) |
| 725 | GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 728 | // Create the symbol that designates the start of the unit's contribution |
| 729 | // to the string offsets table. In a split DWARF scenario, only the skeleton |
| 730 | // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol). |
| 731 | if (useSegmentedStringOffsetsTable()) |
| 732 | (useSplitDwarf() ? SkeletonHolder : InfoHolder) |
| 733 | .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base")); |
| 734 | |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 735 | |
| 736 | // Create the symbols that designates the start of the DWARF v5 range list |
| 737 | // and locations list tables. They are located past the table headers. |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 738 | if (getDwarfVersion() >= 5) { |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 739 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
| 740 | Holder.setRnglistsTableBaseSym( |
| 741 | Asm->createTempSymbol("rnglists_table_base")); |
| 742 | Holder.setLoclistsTableBaseSym( |
| 743 | Asm->createTempSymbol("loclists_table_base")); |
| 744 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 745 | if (useSplitDwarf()) |
| 746 | InfoHolder.setRnglistsTableBaseSym( |
| 747 | Asm->createTempSymbol("rnglists_dwo_table_base")); |
| 748 | } |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 749 | |
George Rimar | 425f751 | 2018-09-20 09:17:36 +0000 | [diff] [blame] | 750 | // Create the symbol that points to the first entry following the debug |
| 751 | // address table (.debug_addr) header. |
| 752 | AddrPool.setLabel(Asm->createTempSymbol("addr_table_base")); |
| 753 | |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 754 | for (DICompileUnit *CUNode : M->debug_compile_units()) { |
David Blaikie | 89daf77 | 2017-07-28 03:06:25 +0000 | [diff] [blame] | 755 | // FIXME: Move local imported entities into a list attached to the |
| 756 | // subprogram, then this search won't be needed and a |
| 757 | // getImportedEntities().empty() test should go below with the rest. |
| 758 | bool HasNonLocalImportedEntities = llvm::any_of( |
| 759 | CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { |
| 760 | return !isa<DILocalScope>(IE->getScope()); |
| 761 | }); |
| 762 | |
| 763 | if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && |
| 764 | CUNode->getRetainedTypes().empty() && |
| 765 | CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 766 | continue; |
| 767 | |
| 768 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 769 | |
| 770 | // Global Variables. |
Adrian Prantl | 2116dd3 | 2017-08-19 01:15:06 +0000 | [diff] [blame] | 771 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 772 | // Don't bother adding DIGlobalVariableExpressions listed in the CU if we |
| 773 | // already know about the variable and it isn't adding a constant |
| 774 | // expression. |
| 775 | auto &GVMapEntry = GVMap[GVE->getVariable()]; |
| 776 | auto *Expr = GVE->getExpression(); |
| 777 | if (!GVMapEntry.size() || (Expr && Expr->isConstant())) |
| 778 | GVMapEntry.push_back({nullptr, Expr}); |
| 779 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 780 | DenseSet<DIGlobalVariable *> Processed; |
| 781 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 782 | DIGlobalVariable *GV = GVE->getVariable(); |
| 783 | if (Processed.insert(GV).second) |
| 784 | CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); |
| 785 | } |
| 786 | |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 787 | for (auto *Ty : CUNode->getEnumTypes()) { |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 788 | // The enum types array by design contains pointers to |
| 789 | // MDNodes rather than DIRefs. Unique them here. |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 790 | CU.getOrCreateTypeDIE(cast<DIType>(Ty)); |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 791 | } |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 792 | for (auto *Ty : CUNode->getRetainedTypes()) { |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 793 | // The retained types array by design contains pointers to |
| 794 | // MDNodes rather than DIRefs. Unique them here. |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 795 | if (DIType *RT = dyn_cast<DIType>(Ty)) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 796 | // There is no point in force-emitting a forward declaration. |
| 797 | CU.getOrCreateTypeDIE(RT); |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 798 | } |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 799 | // Emit imported_modules last so that the relevant context is already |
| 800 | // available. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 801 | for (auto *IE : CUNode->getImportedEntities()) |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 802 | constructAndAddImportedEntityDIE(CU, IE); |
David Blaikie | dc69ebb | 2013-03-11 23:39:23 +0000 | [diff] [blame] | 803 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 806 | void DwarfDebug::finishEntityDefinitions() { |
| 807 | for (const auto &Entity : ConcreteEntities) { |
| 808 | DIE *Die = Entity->getDIE(); |
| 809 | assert(Die); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 810 | // FIXME: Consider the time-space tradeoff of just storing the unit pointer |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 811 | // in the ConcreteEntities list, rather than looking it up again here. |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 812 | // DIE::getUnit isn't simple - it walks parent pointers, etc. |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 813 | DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie()); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 814 | assert(Unit); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 815 | Unit->finishEntityDefinition(Entity.get()); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
David Blaikie | f7221ad | 2014-05-27 18:37:43 +0000 | [diff] [blame] | 819 | void DwarfDebug::finishSubprogramDefinitions() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 820 | for (const DISubprogram *SP : ProcessedSPNodes) { |
| 821 | assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); |
| 822 | forBothCUs( |
| 823 | getOrCreateDwarfCompileUnit(SP->getUnit()), |
| 824 | [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); |
| 825 | } |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void DwarfDebug::finalizeModuleInfo() { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 829 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 830 | |
David Blaikie | 3c2fff3 | 2014-05-27 18:37:48 +0000 | [diff] [blame] | 831 | finishSubprogramDefinitions(); |
| 832 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 833 | finishEntityDefinitions(); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 834 | |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 835 | // Include the DWO file name in the hash if there's more than one CU. |
| 836 | // This handles ThinLTO's situation where imported CUs may very easily be |
| 837 | // duplicate with the same CU partially imported into another ThinLTO unit. |
| 838 | StringRef DWOName; |
| 839 | if (CUMap.size() > 1) |
| 840 | DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; |
| 841 | |
Eric Christopher | ad10cb5 | 2013-12-04 23:24:38 +0000 | [diff] [blame] | 842 | // Handle anything that needs to be done on a per-unit basis after |
| 843 | // all other generation. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 844 | for (const auto &P : CUMap) { |
| 845 | auto &TheCU = *P.second; |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 846 | if (TheCU.getCUNode()->isDebugDirectivesOnly()) |
| 847 | continue; |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 848 | // Emit DW_AT_containing_type attribute to connect types with their |
| 849 | // vtable holding type. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 850 | TheCU.constructContainingTypeDIEs(); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 851 | |
Eric Christopher | 46e2343 | 2013-12-20 04:16:18 +0000 | [diff] [blame] | 852 | // Add CU specific attributes if we need to add any. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 853 | // If we're splitting the dwarf out now that we've got the entire |
| 854 | // CU then add the dwo id to it. |
| 855 | auto *SkCU = TheCU.getSkeleton(); |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 856 | if (useSplitDwarf() && !empty(TheCU.getUnitDie().children())) { |
| 857 | finishUnitAttributes(TheCU.getCUNode(), TheCU); |
| 858 | TheCU.addString(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_name, |
| 859 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
| 860 | SkCU->addString(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_name, |
| 861 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 862 | // Emit a unique identifier for this CU. |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 863 | uint64_t ID = |
| 864 | DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 865 | if (getDwarfVersion() >= 5) { |
| 866 | TheCU.setDWOId(ID); |
| 867 | SkCU->setDWOId(ID); |
| 868 | } else { |
| 869 | TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 870 | dwarf::DW_FORM_data8, ID); |
| 871 | SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 872 | dwarf::DW_FORM_data8, ID); |
| 873 | } |
George Rimar | 425f751 | 2018-09-20 09:17:36 +0000 | [diff] [blame] | 874 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 875 | if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 876 | const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 877 | SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 878 | Sym, Sym); |
| 879 | } |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 880 | } else if (SkCU) { |
| 881 | finishUnitAttributes(SkCU->getCUNode(), *SkCU); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 882 | } |
Eric Christopher | d866720 | 2013-12-30 17:22:27 +0000 | [diff] [blame] | 883 | |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 884 | // If we have code split among multiple sections or non-contiguous |
| 885 | // ranges of code then emit a DW_AT_ranges attribute on the unit that will |
| 886 | // remain in the .o file, otherwise add a DW_AT_low_pc. |
| 887 | // FIXME: We should use ranges allow reordering of code ala |
| 888 | // .subsections_via_symbols in mach-o. This would mean turning on |
| 889 | // ranges for all subprogram DIEs for mach-o. |
David Blaikie | a8be0a7 | 2014-11-01 01:15:24 +0000 | [diff] [blame] | 890 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 891 | |
| 892 | // We don't keep track of which addresses are used in which CU so this |
| 893 | // is a bit pessimistic under LTO. |
David Blaikie | 560ff35 | 2018-12-14 22:44:46 +0000 | [diff] [blame] | 894 | if (!AddrPool.isEmpty() && |
| 895 | (getDwarfVersion() >= 5 || |
| 896 | (SkCU && !empty(TheCU.getUnitDie().children())))) |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 897 | U.addAddrTableBase(); |
| 898 | |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 899 | if (unsigned NumRanges = TheCU.getRanges().size()) { |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 900 | if (NumRanges > 1 && useRangesSection()) |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 901 | // A DW_AT_low_pc attribute may also be specified in combination with |
| 902 | // DW_AT_ranges to specify the default base address for use in |
| 903 | // location lists (see Section 2.6.2) and range lists (see Section |
| 904 | // 2.17.3). |
| 905 | 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] | 906 | else |
David Blaikie | 72d03ef | 2015-05-02 02:31:49 +0000 | [diff] [blame] | 907 | U.setBaseAddress(TheCU.getRanges().front().getStart()); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 908 | U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 909 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 910 | |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 911 | if (getDwarfVersion() >= 5) { |
| 912 | if (U.hasRangeLists()) |
| 913 | U.addRnglistsBase(); |
| 914 | |
| 915 | if (!DebugLocs.getLists().empty() && !useSplitDwarf()) |
| 916 | U.addLoclistsBase(); |
| 917 | } |
Wolfgang Pieb | 9ea6508 | 2018-07-26 22:48:52 +0000 | [diff] [blame] | 918 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 919 | auto *CUNode = cast<DICompileUnit>(P.first); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 920 | // If compile Unit has macros, emit "DW_AT_macro_info" attribute. |
| 921 | if (CUNode->getMacros()) |
| 922 | U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, |
| 923 | U.getMacroLabelBegin(), |
| 924 | TLOF.getDwarfMacinfoSection()->getBeginSymbol()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 927 | // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. |
| 928 | for (auto *CUNode : MMI->getModule()->debug_compile_units()) |
Adrian Prantl | 960e766 | 2017-07-27 15:24:20 +0000 | [diff] [blame] | 929 | if (CUNode->getDWOId()) |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 930 | getOrCreateDwarfCompileUnit(CUNode); |
| 931 | |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 932 | // Compute DIE offsets and sizes. |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 933 | InfoHolder.computeSizeAndOffsets(); |
| 934 | if (useSplitDwarf()) |
| 935 | SkeletonHolder.computeSizeAndOffsets(); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 938 | // Emit all Dwarf sections that should come after the content. |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 939 | void DwarfDebug::endModule() { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 940 | assert(CurFn == nullptr); |
| 941 | assert(CurMI == nullptr); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 942 | |
David Blaikie | 0dc623c | 2014-10-24 17:53:38 +0000 | [diff] [blame] | 943 | // If we aren't actually generating debug info (check beginModule - |
| 944 | // conditionalized on !DisableDebugInfoPrinting and the presence of the |
| 945 | // llvm.dbg.cu metadata node) |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 946 | if (!MMI->hasDebugInfo()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 947 | return; |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 948 | |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 949 | // Finalize the debug info for the module. |
| 950 | finalizeModuleInfo(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 951 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 952 | emitDebugStr(); |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 953 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 954 | if (useSplitDwarf()) |
| 955 | emitDebugLocDWO(); |
| 956 | else |
| 957 | // Emit info into a debug loc section. |
| 958 | emitDebugLoc(); |
| 959 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 960 | // Corresponding abbreviations into a abbrev section. |
| 961 | emitAbbreviations(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 962 | |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 963 | // Emit all the DIEs into a debug info section. |
| 964 | emitDebugInfo(); |
| 965 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 966 | // Emit info into a debug aranges section. |
Eric Christopher | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 967 | if (GenerateARangeSection) |
| 968 | emitDebugARanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 969 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 970 | // Emit info into a debug ranges section. |
| 971 | emitDebugRanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 972 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 973 | // Emit info into a debug macinfo section. |
| 974 | emitDebugMacinfo(); |
| 975 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 976 | if (useSplitDwarf()) { |
| 977 | emitDebugStrDWO(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 978 | emitDebugInfoDWO(); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 979 | emitDebugAbbrevDWO(); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 980 | emitDebugLineDWO(); |
David Blaikie | 32e09de | 2018-10-20 08:12:36 +0000 | [diff] [blame] | 981 | emitDebugRangesDWO(); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 982 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 983 | |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 984 | emitDebugAddr(); |
| 985 | |
Eric Christopher | a876b82 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 986 | // Emit info into the dwarf accelerator table sections. |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 987 | switch (getAccelTableKind()) { |
| 988 | case AccelTableKind::Apple: |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 989 | emitAccelNames(); |
| 990 | emitAccelObjC(); |
| 991 | emitAccelNamespaces(); |
| 992 | emitAccelTypes(); |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 993 | break; |
| 994 | case AccelTableKind::Dwarf: |
| 995 | emitAccelDebugNames(); |
| 996 | break; |
| 997 | case AccelTableKind::None: |
| 998 | break; |
| 999 | case AccelTableKind::Default: |
| 1000 | llvm_unreachable("Default should have already been resolved."); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1001 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1002 | |
Eric Christopher | 4b35818 | 2013-08-30 00:40:17 +0000 | [diff] [blame] | 1003 | // Emit the pubnames and pubtypes sections if requested. |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1004 | emitDebugPubSections(); |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1005 | |
Devang Patel | d070128 | 2010-08-02 17:32:15 +0000 | [diff] [blame] | 1006 | // clean up. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1007 | // FIXME: AbstractVariables.clear(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1010 | void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU, |
| 1011 | const DINode *Node, |
| 1012 | const MDNode *ScopeNode) { |
| 1013 | if (CU.getExistingAbstractEntity(Node)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 1014 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 1015 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1016 | CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1017 | cast<DILocalScope>(ScopeNode))); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1020 | void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU, |
| 1021 | const DINode *Node, const MDNode *ScopeNode) { |
| 1022 | if (CU.getExistingAbstractEntity(Node)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 1023 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 1024 | |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 1025 | if (LexicalScope *Scope = |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1026 | LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1027 | CU.createAbstractEntity(Node, Scope); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 1028 | } |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1029 | |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1030 | // Collect variable information from side table maintained by MF. |
| 1031 | void DwarfDebug::collectVariableInfoFromMFTable( |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1032 | DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) { |
| 1033 | SmallDenseMap<InlinedEntity, DbgVariable *> MFVars; |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1034 | for (const auto &VI : Asm->MF->getVariableDbgInfo()) { |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 1035 | if (!VI.Var) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 1036 | continue; |
Duncan P. N. Exon Smith | 78a9527 | 2015-04-16 22:12:59 +0000 | [diff] [blame] | 1037 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 1038 | "Expected inlined-at fields to agree"); |
| 1039 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1040 | InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt()); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1041 | Processed.insert(Var); |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 1042 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
Jim Grosbach | a8683bb | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1043 | |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 1044 | // If variable scope is not found then skip this variable. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1045 | if (!Scope) |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 1046 | continue; |
Devang Patel | f6eeaeb | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1047 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1048 | ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode()); |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1049 | auto RegVar = llvm::make_unique<DbgVariable>( |
| 1050 | cast<DILocalVariable>(Var.first), Var.second); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1051 | RegVar->initializeMMI(VI.Expr, VI.Slot); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 1052 | if (DbgVariable *DbgVar = MFVars.lookup(Var)) |
| 1053 | DbgVar->addMMIEntry(*RegVar); |
| 1054 | else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { |
| 1055 | MFVars.insert({Var, RegVar.get()}); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1056 | ConcreteEntities.push_back(std::move(RegVar)); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 1057 | } |
Devang Patel | 475d32a | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 1058 | } |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1059 | } |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1060 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1061 | // Get .debug_loc entry for the instruction range starting at MI. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1062 | static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1063 | const DIExpression *Expr = MI->getDebugExpression(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1064 | assert(MI->getNumOperands() == 4); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1065 | if (MI->getOperand(0).isReg()) { |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 1066 | auto RegOp = MI->getOperand(0); |
| 1067 | auto Op1 = MI->getOperand(1); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1068 | // If the second operand is an immediate, this is a |
| 1069 | // register-indirect address. |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 1070 | assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset"); |
| 1071 | MachineLocation MLoc(RegOp.getReg(), Op1.isImm()); |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 1072 | return DebugLocEntry::Value(Expr, MLoc); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (MI->getOperand(0).isImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 1075 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 1076 | if (MI->getOperand(0).isFPImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 1077 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 1078 | if (MI->getOperand(0).isCImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 1079 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 1080 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1081 | llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1084 | /// If this and Next are describing different fragments of the same |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 1085 | /// variable, merge them by appending Next's values to the current |
| 1086 | /// list of values. |
| 1087 | /// Return true if the merge was successful. |
| 1088 | bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) { |
| 1089 | if (Begin == Next.Begin) { |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 1090 | auto *FirstExpr = cast<DIExpression>(Values[0].Expression); |
| 1091 | auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1092 | if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment()) |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 1093 | return false; |
| 1094 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1095 | // 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] | 1096 | // In doing so, we can take advantage of the fact that both lists are |
| 1097 | // sorted. |
| 1098 | for (unsigned i = 0, j = 0; i < Values.size(); ++i) { |
| 1099 | for (; j < Next.Values.size(); ++j) { |
Bjorn Pettersson | a223f815 | 2018-03-12 18:02:39 +0000 | [diff] [blame] | 1100 | int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp( |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1101 | cast<DIExpression>(Next.Values[j].Expression)); |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 1102 | if (res == 0) // The two expressions overlap, we can't merge. |
| 1103 | return false; |
| 1104 | // Values[i] is entirely before Next.Values[j], |
| 1105 | // so go back to the next entry of Values. |
| 1106 | else if (res == -1) |
| 1107 | break; |
| 1108 | // Next.Values[j] is entirely before Values[i], so go on to the |
| 1109 | // next entry of Next.Values. |
| 1110 | } |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 1111 | } |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 1112 | |
| 1113 | addValues(Next.Values); |
| 1114 | End = Next.End; |
| 1115 | return true; |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 1116 | } |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1120 | /// Build the location list for all DBG_VALUEs in the function that |
| 1121 | /// describe the same variable. If the ranges of several independent |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1122 | /// fragments of the same variable overlap partially, split them up and |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1123 | /// combine the ranges. The resulting DebugLocEntries are will have |
| 1124 | /// strict monotonically increasing begin addresses and will never |
| 1125 | /// overlap. |
| 1126 | // |
| 1127 | // Input: |
| 1128 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1129 | // Ranges History [var, loc, fragment ofs size] |
| 1130 | // 0 | [x, (reg0, fragment 0, 32)] |
| 1131 | // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1132 | // 2 | | ... |
| 1133 | // 3 | [clobber reg0] |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1134 | // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of |
Eric Christopher | ffc5ff3 | 2015-02-17 20:02:28 +0000 | [diff] [blame] | 1135 | // x. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1136 | // |
| 1137 | // Output: |
| 1138 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1139 | // [0-1] [x, (reg0, fragment 0, 32)] |
| 1140 | // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)] |
| 1141 | // [3-4] [x, (reg1, fragment 32, 32)] |
| 1142 | // [4- ] [x, (mem, fragment 0, 64)] |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 1143 | void |
| 1144 | DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, |
| 1145 | const DbgValueHistoryMap::InstrRanges &Ranges) { |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 1146 | SmallVector<DebugLocEntry::Value, 4> OpenRanges; |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1147 | |
| 1148 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 1149 | const MachineInstr *Begin = I->first; |
| 1150 | const MachineInstr *End = I->second; |
| 1151 | assert(Begin->isDebugValue() && "Invalid History entry"); |
| 1152 | |
| 1153 | // Check if a variable is inaccessible in this range. |
Adrian Prantl | 5e1fa85 | 2014-08-12 21:55:58 +0000 | [diff] [blame] | 1154 | if (Begin->getNumOperands() > 1 && |
| 1155 | Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) { |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1156 | OpenRanges.clear(); |
| 1157 | continue; |
| 1158 | } |
| 1159 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1160 | // 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] | 1161 | const DIExpression *DIExpr = Begin->getDebugExpression(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 1162 | auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) { |
Bjorn Pettersson | a223f815 | 2018-03-12 18:02:39 +0000 | [diff] [blame] | 1163 | return DIExpr->fragmentsOverlap(R.getExpression()); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1164 | }); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1165 | OpenRanges.erase(Last, OpenRanges.end()); |
| 1166 | |
| 1167 | const MCSymbol *StartLabel = getLabelBeforeInsn(Begin); |
| 1168 | assert(StartLabel && "Forgot label before DBG_VALUE starting a range!"); |
| 1169 | |
| 1170 | const MCSymbol *EndLabel; |
| 1171 | if (End != nullptr) |
| 1172 | EndLabel = getLabelAfterInsn(End); |
| 1173 | else if (std::next(I) == Ranges.end()) |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 1174 | EndLabel = Asm->getFunctionEnd(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1175 | else |
| 1176 | EndLabel = getLabelBeforeInsn(std::next(I)->first); |
| 1177 | assert(EndLabel && "Forgot label after instruction ending a range!"); |
| 1178 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1179 | LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n"); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1180 | |
| 1181 | auto Value = getDebugLocValue(Begin); |
David Stenberg | 33b192d | 2019-01-09 09:58:59 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Omit entries with empty ranges as they do not have any effect in DWARF. |
| 1184 | if (StartLabel == EndLabel) { |
| 1185 | // If this is a fragment, we must still add the value to the list of |
| 1186 | // open ranges, since it may describe non-overlapping parts of the |
| 1187 | // variable. |
| 1188 | if (DIExpr->isFragment()) |
| 1189 | OpenRanges.push_back(Value); |
| 1190 | LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n"); |
| 1191 | continue; |
| 1192 | } |
| 1193 | |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 1194 | DebugLocEntry Loc(StartLabel, EndLabel, Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1195 | bool couldMerge = false; |
| 1196 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1197 | // If this is a fragment, it may belong to the current DebugLocEntry. |
| 1198 | if (DIExpr->isFragment()) { |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1199 | // Add this value to the list of open ranges. |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 1200 | OpenRanges.push_back(Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1201 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1202 | // Attempt to add the fragment to the last entry. |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1203 | if (!DebugLoc.empty()) |
| 1204 | if (DebugLoc.back().MergeValues(Loc)) |
| 1205 | couldMerge = true; |
| 1206 | } |
| 1207 | |
| 1208 | if (!couldMerge) { |
| 1209 | // Need to add a new DebugLocEntry. Add all values from still |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1210 | // valid non-overlapping fragments. |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 1211 | if (OpenRanges.size()) |
| 1212 | Loc.addValues(OpenRanges); |
| 1213 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1214 | DebugLoc.push_back(std::move(Loc)); |
| 1215 | } |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Attempt to coalesce the ranges of two otherwise identical |
| 1218 | // DebugLocEntries. |
| 1219 | auto CurEntry = DebugLoc.rbegin(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1220 | LLVM_DEBUG({ |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1221 | dbgs() << CurEntry->getValues().size() << " Values:\n"; |
Adrian Prantl | 6f8c1b6 | 2015-05-26 20:06:51 +0000 | [diff] [blame] | 1222 | for (auto &Value : CurEntry->getValues()) |
Adrian Prantl | dba58fb | 2016-02-29 22:28:22 +0000 | [diff] [blame] | 1223 | Value.dump(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1224 | dbgs() << "-----\n"; |
| 1225 | }); |
Adrian Prantl | 7570731 | 2015-05-26 20:06:48 +0000 | [diff] [blame] | 1226 | |
| 1227 | auto PrevEntry = std::next(CurEntry); |
| 1228 | if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) |
| 1229 | DebugLoc.pop_back(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1233 | DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU, |
| 1234 | LexicalScope &Scope, |
| 1235 | const DINode *Node, |
| 1236 | const DILocation *Location, |
| 1237 | const MCSymbol *Sym) { |
| 1238 | ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode()); |
| 1239 | if (isa<const DILocalVariable>(Node)) { |
| 1240 | ConcreteEntities.push_back( |
| 1241 | llvm::make_unique<DbgVariable>(cast<const DILocalVariable>(Node), |
| 1242 | Location)); |
| 1243 | InfoHolder.addScopeVariable(&Scope, |
| 1244 | cast<DbgVariable>(ConcreteEntities.back().get())); |
| 1245 | } else if (isa<const DILabel>(Node)) { |
| 1246 | ConcreteEntities.push_back( |
| 1247 | llvm::make_unique<DbgLabel>(cast<const DILabel>(Node), |
| 1248 | Location, Sym)); |
| 1249 | InfoHolder.addScopeLabel(&Scope, |
| 1250 | cast<DbgLabel>(ConcreteEntities.back().get())); |
| 1251 | } |
| 1252 | return ConcreteEntities.back().get(); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1253 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1254 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1255 | /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its |
| 1256 | /// enclosing lexical scope. The check ensures there are no other instructions |
| 1257 | /// in the same lexical scope preceding the DBG_VALUE and that its range is |
| 1258 | /// either open or otherwise rolls off the end of the scope. |
| 1259 | static bool validThroughout(LexicalScopes &LScopes, |
| 1260 | const MachineInstr *DbgValue, |
| 1261 | const MachineInstr *RangeEnd) { |
| 1262 | assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); |
| 1263 | auto MBB = DbgValue->getParent(); |
| 1264 | auto DL = DbgValue->getDebugLoc(); |
| 1265 | auto *LScope = LScopes.findLexicalScope(DL); |
| 1266 | // Scope doesn't exist; this is a dead DBG_VALUE. |
| 1267 | if (!LScope) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1268 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1269 | auto &LSRange = LScope->getRanges(); |
| 1270 | if (LSRange.size() == 0) |
| 1271 | return false; |
| 1272 | |
| 1273 | // Determine if the DBG_VALUE is valid at the beginning of its lexical block. |
| 1274 | const MachineInstr *LScopeBegin = LSRange.front().first; |
| 1275 | // Early exit if the lexical scope begins outside of the current block. |
| 1276 | if (LScopeBegin->getParent() != MBB) |
| 1277 | return false; |
| 1278 | MachineBasicBlock::const_reverse_iterator Pred(DbgValue); |
| 1279 | for (++Pred; Pred != MBB->rend(); ++Pred) { |
| 1280 | if (Pred->getFlag(MachineInstr::FrameSetup)) |
| 1281 | break; |
| 1282 | auto PredDL = Pred->getDebugLoc(); |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1283 | if (!PredDL || Pred->isMetaInstruction()) |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1284 | continue; |
| 1285 | // Check whether the instruction preceding the DBG_VALUE is in the same |
| 1286 | // (sub)scope as the DBG_VALUE. |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1287 | if (DL->getScope() == PredDL->getScope()) |
| 1288 | return false; |
| 1289 | auto *PredScope = LScopes.findLexicalScope(PredDL); |
| 1290 | if (!PredScope || LScope->dominates(PredScope)) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1291 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | // If the range of the DBG_VALUE is open-ended, report success. |
| 1295 | if (!RangeEnd) |
| 1296 | return true; |
| 1297 | |
| 1298 | // Fail if there are instructions belonging to our scope in another block. |
| 1299 | const MachineInstr *LScopeEnd = LSRange.back().second; |
| 1300 | if (LScopeEnd->getParent() != MBB) |
| 1301 | return false; |
| 1302 | |
| 1303 | // Single, constant DBG_VALUEs in the prologue are promoted to be live |
| 1304 | // throughout the function. This is a hack, presumably for DWARF v2 and not |
| 1305 | // necessarily correct. It would be much better to use a dbg.declare instead |
| 1306 | // if we know the constant is live throughout the scope. |
| 1307 | if (DbgValue->getOperand(0).isImm() && MBB->pred_empty()) |
| 1308 | return true; |
| 1309 | |
| 1310 | return false; |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1313 | // Find variables for each lexical scope. |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1314 | void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU, |
| 1315 | const DISubprogram *SP, |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1316 | DenseSet<InlinedEntity> &Processed) { |
Eric Christopher | 270a12c | 2013-07-03 21:37:03 +0000 | [diff] [blame] | 1317 | // 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] | 1318 | collectVariableInfoFromMFTable(TheCU, Processed); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1319 | |
Alexey Samsonov | 0436caa | 2014-04-30 23:02:40 +0000 | [diff] [blame] | 1320 | for (const auto &I : DbgValues) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1321 | InlinedEntity IV = I.first; |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1322 | if (Processed.count(IV)) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1323 | continue; |
| 1324 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1325 | // Instruction ranges, specifying where IV is accessible. |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1326 | const auto &Ranges = I.second; |
| 1327 | if (Ranges.empty()) |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1328 | continue; |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1329 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1330 | LexicalScope *Scope = nullptr; |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1331 | const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1332 | if (const DILocation *IA = IV.second) |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1333 | Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA); |
Duncan P. N. Exon Smith | b474937 | 2015-02-17 00:02:27 +0000 | [diff] [blame] | 1334 | else |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1335 | Scope = LScopes.findLexicalScope(LocalVar->getScope()); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1336 | // If variable scope is not found then skip this variable. |
Devang Patel | fbd6c45 | 2010-05-21 00:10:20 +0000 | [diff] [blame] | 1337 | if (!Scope) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1338 | continue; |
| 1339 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1340 | Processed.insert(IV); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1341 | DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU, |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1342 | *Scope, LocalVar, IV.second)); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1343 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1344 | const MachineInstr *MInsn = Ranges.front().first; |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1345 | assert(MInsn->isDebugValue() && "History must begin with debug value"); |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1346 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1347 | // 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] | 1348 | if (Ranges.size() == 1 && |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1349 | validThroughout(LScopes, MInsn, Ranges.front().second)) { |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1350 | RegVar->initializeDbgValue(MInsn); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1351 | continue; |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1352 | } |
Alexey Bataev | 2a03d42 | 2018-06-29 14:23:28 +0000 | [diff] [blame] | 1353 | // Do not emit location lists if .debug_loc secton is disabled. |
| 1354 | if (!useLocSection()) |
| 1355 | continue; |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1356 | |
Eric Christopher | 59cc071 | 2013-01-28 17:33:26 +0000 | [diff] [blame] | 1357 | // Handle multiple DBG_VALUE instructions describing one variable. |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1358 | DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); |
Jakob Stoklund Olesen | 9c057ee | 2011-03-22 00:21:41 +0000 | [diff] [blame] | 1359 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1360 | // Build the location list for this variable. |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1361 | SmallVector<DebugLocEntry, 8> Entries; |
| 1362 | buildLocationList(Entries, Ranges); |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1363 | |
Adrian Prantl | 693e8de | 2016-02-29 17:06:46 +0000 | [diff] [blame] | 1364 | // 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] | 1365 | // unique identifiers, so don't bother resolving the type with the |
| 1366 | // identifier map. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1367 | const DIBasicType *BT = dyn_cast<DIBasicType>( |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1368 | static_cast<const Metadata *>(LocalVar->getType())); |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1369 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1370 | // 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] | 1371 | for (auto &Entry : Entries) |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1372 | Entry.finalize(*Asm, List, BT); |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1373 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1374 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1375 | // For each InlinedEntity collected from DBG_LABEL instructions, convert to |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1376 | // DWARF-related DbgLabel. |
| 1377 | for (const auto &I : DbgLabels) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1378 | InlinedEntity IL = I.first; |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1379 | const MachineInstr *MI = I.second; |
| 1380 | if (MI == nullptr) |
| 1381 | continue; |
| 1382 | |
| 1383 | LexicalScope *Scope = nullptr; |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1384 | const DILabel *Label = cast<DILabel>(IL.first); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1385 | // Get inlined DILocation if it is inlined label. |
| 1386 | if (const DILocation *IA = IL.second) |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1387 | Scope = LScopes.findInlinedScope(Label->getScope(), IA); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1388 | else |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1389 | Scope = LScopes.findLexicalScope(Label->getScope()); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1390 | // If label scope is not found then skip this label. |
| 1391 | if (!Scope) |
| 1392 | continue; |
| 1393 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1394 | Processed.insert(IL); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1395 | /// At this point, the temporary label is created. |
| 1396 | /// Save the temporary label to DbgLabel entity to get the |
| 1397 | /// actually address when generating Dwarf DIE. |
| 1398 | MCSymbol *Sym = getLabelBeforeInsn(MI); |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1399 | createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | // Collect info for variables/labels that were optimized out. |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 1403 | for (const DINode *DN : SP->getRetainedNodes()) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1404 | if (!Processed.insert(InlinedEntity(DN, nullptr)).second) |
| 1405 | continue; |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1406 | LexicalScope *Scope = nullptr; |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 1407 | if (auto *DV = dyn_cast<DILocalVariable>(DN)) { |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1408 | Scope = LScopes.findLexicalScope(DV->getScope()); |
| 1409 | } else if (auto *DL = dyn_cast<DILabel>(DN)) { |
| 1410 | Scope = LScopes.findLexicalScope(DL->getScope()); |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 1411 | } |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (Scope) |
| 1414 | createConcreteEntity(TheCU, *Scope, DN, nullptr); |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1415 | } |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1416 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1417 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1418 | // Process beginning of an instruction. |
Devang Patel | b5694e7 | 2010-10-26 17:49:02 +0000 | [diff] [blame] | 1419 | void DwarfDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1420 | DebugHandlerBase::beginInstruction(MI); |
| 1421 | assert(CurMI); |
| 1422 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1423 | const auto *SP = MI->getMF()->getFunction().getSubprogram(); |
David Blaikie | 7f2b717 | 2017-05-26 17:05:15 +0000 | [diff] [blame] | 1424 | if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
| 1425 | return; |
| 1426 | |
Paul Robinson | 4fa7b57a | 2016-12-09 19:15:32 +0000 | [diff] [blame] | 1427 | // Check if source location changes, but ignore DBG_VALUE and CFI locations. |
Paul Robinson | ee88ed6 | 2018-02-14 17:35:52 +0000 | [diff] [blame] | 1428 | // If the instruction is part of the function frame setup code, do not emit |
| 1429 | // any line record, as there is no correspondence with any user code. |
| 1430 | if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup)) |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1431 | return; |
| 1432 | const DebugLoc &DL = MI->getDebugLoc(); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1433 | // When we emit a line-0 record, we don't update PrevInstLoc; so look at |
| 1434 | // the last line number actually emitted, to see if it was line 0. |
| 1435 | unsigned LastAsmLine = |
| 1436 | Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); |
Paul Robinson | 96de8c7 | 2016-11-29 22:41:16 +0000 | [diff] [blame] | 1437 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 1438 | // Request a label after the call in order to emit AT_return_pc information |
| 1439 | // in call site entries. TODO: Add support for targets with delay slots. |
| 1440 | if (SP->areAllCallsDescribed() && MI->isCall() && !MI->hasDelaySlot()) |
| 1441 | requestLabelAfterInsn(MI); |
| 1442 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1443 | if (DL == PrevInstLoc) { |
| 1444 | // If we have an ongoing unspecified location, nothing to do here. |
| 1445 | if (!DL) |
| 1446 | return; |
| 1447 | // We have an explicit location, same as the previous location. |
| 1448 | // But we might be coming back to it after a line 0 record. |
| 1449 | if (LastAsmLine == 0 && DL.getLine() != 0) { |
| 1450 | // Reinstate the source location but not marked as a statement. |
| 1451 | const MDNode *Scope = DL.getScope(); |
| 1452 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); |
Paul Robinson | 37a13dd | 2016-11-30 22:49:55 +0000 | [diff] [blame] | 1453 | } |
| 1454 | return; |
| 1455 | } |
| 1456 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1457 | if (!DL) { |
| 1458 | // We have an unspecified location, which might want to be line 0. |
| 1459 | // If we have already emitted a line-0 record, don't repeat it. |
| 1460 | if (LastAsmLine == 0) |
| 1461 | return; |
| 1462 | // If user said Don't Do That, don't do that. |
| 1463 | if (UnknownLocations == Disable) |
| 1464 | return; |
Matthias Braun | c7efb6f | 2018-10-11 23:37:58 +0000 | [diff] [blame] | 1465 | // See if we have a reason to emit a line-0 record now. |
| 1466 | // Reasons to emit a line-0 record include: |
| 1467 | // - User asked for it (UnknownLocations). |
| 1468 | // - Instruction has a label, so it's referenced from somewhere else, |
| 1469 | // possibly debug information; we want it to have a source location. |
| 1470 | // - Instruction is at the top of a block; we don't want to inherit the |
| 1471 | // location from the physically previous (maybe unrelated) block. |
| 1472 | if (UnknownLocations == Enable || PrevLabel || |
| 1473 | (PrevInstBB && PrevInstBB != MI->getParent())) { |
| 1474 | // Preserve the file and column numbers, if we can, to save space in |
| 1475 | // the encoded line table. |
| 1476 | // Do not update PrevInstLoc, it remembers the last non-0 line. |
| 1477 | const MDNode *Scope = nullptr; |
| 1478 | unsigned Column = 0; |
| 1479 | if (PrevInstLoc) { |
| 1480 | Scope = PrevInstLoc.getScope(); |
| 1481 | Column = PrevInstLoc.getCol(); |
| 1482 | } |
| 1483 | recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1484 | } |
| 1485 | return; |
| 1486 | } |
| 1487 | |
Matthias Braun | c7efb6f | 2018-10-11 23:37:58 +0000 | [diff] [blame] | 1488 | // We have an explicit location, different from the previous location. |
| 1489 | // Don't repeat a line-0 record, but otherwise emit the new location. |
| 1490 | // (The new location might be an explicit line 0, which we do emit.) |
Jonas Devlieghere | 669edb5 | 2019-01-16 23:26:29 +0000 | [diff] [blame] | 1491 | if (DL.getLine() == 0 && LastAsmLine == 0) |
Matthias Braun | c7efb6f | 2018-10-11 23:37:58 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | unsigned Flags = 0; |
| 1494 | if (DL == PrologEndLoc) { |
| 1495 | Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; |
| 1496 | PrologEndLoc = DebugLoc(); |
| 1497 | } |
| 1498 | // If the line changed, we call that a new statement; unless we went to |
| 1499 | // line 0 and came back, in which case it is not a new statement. |
| 1500 | unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; |
| 1501 | if (DL.getLine() && DL.getLine() != OldLine) |
| 1502 | Flags |= DWARF2_FLAG_IS_STMT; |
| 1503 | |
| 1504 | const MDNode *Scope = DL.getScope(); |
| 1505 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); |
| 1506 | |
| 1507 | // If we're not at line 0, remember this location. |
| 1508 | if (DL.getLine()) |
| 1509 | PrevInstLoc = DL; |
Devang Patel | 75cc16c | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1512 | static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { |
| 1513 | // First known non-DBG_VALUE and non-frame setup location marks |
| 1514 | // the beginning of the function body. |
| 1515 | for (const auto &MBB : *MF) |
| 1516 | for (const auto &MI : MBB) |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 1517 | if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && |
Michael Kuperstein | 2b3c16c | 2015-10-08 07:48:49 +0000 | [diff] [blame] | 1518 | MI.getDebugLoc()) |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1519 | return MI.getDebugLoc(); |
| 1520 | return DebugLoc(); |
| 1521 | } |
| 1522 | |
Alexey Bataev | 9d5974a | 2019-01-22 17:24:16 +0000 | [diff] [blame] | 1523 | /// Register a source line with debug info. Returns the unique label that was |
| 1524 | /// emitted and which provides correspondence to the source line list. |
| 1525 | static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col, |
| 1526 | const MDNode *S, unsigned Flags, unsigned CUID, |
| 1527 | uint16_t DwarfVersion, |
| 1528 | ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) { |
| 1529 | StringRef Fn; |
| 1530 | unsigned FileNo = 1; |
| 1531 | unsigned Discriminator = 0; |
| 1532 | if (auto *Scope = cast_or_null<DIScope>(S)) { |
| 1533 | Fn = Scope->getFilename(); |
| 1534 | if (Line != 0 && DwarfVersion >= 4) |
| 1535 | if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) |
| 1536 | Discriminator = LBF->getDiscriminator(); |
| 1537 | |
| 1538 | FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID]) |
| 1539 | .getOrCreateSourceID(Scope->getFile()); |
| 1540 | } |
| 1541 | Asm.OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0, |
| 1542 | Discriminator, Fn); |
| 1543 | } |
| 1544 | |
| 1545 | DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, |
| 1546 | unsigned CUID) { |
| 1547 | // Get beginning of function. |
| 1548 | if (DebugLoc PrologEndLoc = findPrologueEndLoc(&MF)) { |
| 1549 | // Ensure the compile unit is created if the function is called before |
| 1550 | // beginFunction(). |
| 1551 | (void)getOrCreateDwarfCompileUnit( |
| 1552 | MF.getFunction().getSubprogram()->getUnit()); |
| 1553 | // We'd like to list the prologue as "not statements" but GDB behaves |
| 1554 | // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. |
| 1555 | const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); |
| 1556 | ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT, |
| 1557 | CUID, getDwarfVersion(), getUnits()); |
| 1558 | return PrologEndLoc; |
| 1559 | } |
| 1560 | return DebugLoc(); |
| 1561 | } |
| 1562 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1563 | // Gather pre-function debug information. Assumes being called immediately |
| 1564 | // after the function entry point has been emitted. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1565 | void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1566 | CurFn = MF; |
Eric Christopher | fedfa44 | 2013-11-01 23:14:17 +0000 | [diff] [blame] | 1567 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1568 | auto *SP = MF->getFunction().getSubprogram(); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1569 | assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1570 | if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 1571 | return; |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1572 | |
| 1573 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1574 | |
| 1575 | // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function |
| 1576 | // belongs to so that we add to the correct per-cu line table in the |
| 1577 | // non-asm case. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1578 | if (Asm->OutStreamer->hasRawTextSupport()) |
Rafael Espindola | b4eec1d | 2014-02-05 18:00:21 +0000 | [diff] [blame] | 1579 | // Use a single line table if we are generating assembly. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1580 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Manman Ren | 9d4c735 | 2013-05-21 00:57:22 +0000 | [diff] [blame] | 1581 | else |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1582 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); |
Manman Ren | 4e042a6 | 2013-02-05 21:52:47 +0000 | [diff] [blame] | 1583 | |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1584 | // Record beginning of function. |
Alexey Bataev | 9d5974a | 2019-01-22 17:24:16 +0000 | [diff] [blame] | 1585 | PrologEndLoc = emitInitialLocDirective( |
| 1586 | *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID()); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1589 | void DwarfDebug::skippedNonDebugFunction() { |
| 1590 | // If we don't have a subprogram for this function then there will be a hole |
| 1591 | // in the range information. Keep note of this by setting the previously used |
| 1592 | // section to nullptr. |
| 1593 | PrevCU = nullptr; |
| 1594 | CurFn = nullptr; |
| 1595 | } |
| 1596 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1597 | // Gather and emit post-function debug information. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1598 | void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1599 | const DISubprogram *SP = MF->getFunction().getSubprogram(); |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1600 | |
David Blaikie | e75f963 | 2014-10-14 17:12:02 +0000 | [diff] [blame] | 1601 | assert(CurFn == MF && |
| 1602 | "endFunction should be called with the same function as beginFunction"); |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1603 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1604 | // Set DwarfDwarfCompileUnitID in MCContext to default value. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1605 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1606 | |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1607 | LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1608 | assert(!FnScope || SP == FnScope->getScopeNode()); |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 1609 | DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 1610 | if (TheCU.getCUNode()->isDebugDirectivesOnly()) { |
| 1611 | PrevLabel = nullptr; |
| 1612 | CurFn = nullptr; |
| 1613 | return; |
| 1614 | } |
David Blaikie | 263a008 | 2014-10-23 00:06:27 +0000 | [diff] [blame] | 1615 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1616 | DenseSet<InlinedEntity> Processed; |
| 1617 | collectEntityInfo(TheCU, SP, Processed); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1618 | |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1619 | // 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] | 1620 | TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Under -gmlt, skip building the subprogram if there are no inlined |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1623 | // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram |
| 1624 | // is still needed as we need its source location. |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 1625 | if (!TheCU.getCUNode()->getDebugInfoForProfiling() && |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1626 | TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && |
David Blaikie | e1c7974 | 2014-09-30 21:28:32 +0000 | [diff] [blame] | 1627 | LScopes.getAbstractScopesList().empty() && !IsDarwin) { |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1628 | assert(InfoHolder.getScopeVariables().empty()); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1629 | PrevLabel = nullptr; |
| 1630 | CurFn = nullptr; |
| 1631 | return; |
| 1632 | } |
| 1633 | |
Adrian Prantl | 049d21c | 2014-10-13 20:44:58 +0000 | [diff] [blame] | 1634 | #ifndef NDEBUG |
| 1635 | size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); |
| 1636 | #endif |
Devang Patel | 7e62302 | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1637 | // Construct abstract scopes. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1638 | for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1639 | auto *SP = cast<DISubprogram>(AScope->getScopeNode()); |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 1640 | for (const DINode *DN : SP->getRetainedNodes()) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1641 | if (!Processed.insert(InlinedEntity(DN, nullptr)).second) |
| 1642 | continue; |
| 1643 | |
| 1644 | const MDNode *Scope = nullptr; |
| 1645 | if (auto *DV = dyn_cast<DILocalVariable>(DN)) |
| 1646 | Scope = DV->getScope(); |
| 1647 | else if (auto *DL = dyn_cast<DILabel>(DN)) |
| 1648 | Scope = DL->getScope(); |
| 1649 | else |
| 1650 | llvm_unreachable("Unexpected DI type!"); |
| 1651 | |
| 1652 | // Collect info for variables/labels that were optimized out. |
| 1653 | ensureAbstractEntityIsCreated(TheCU, DN, Scope); |
| 1654 | assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes |
| 1655 | && "ensureAbstractEntityIsCreated inserted abstract scopes"); |
Devang Patel | 5c0f85c | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 1656 | } |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1657 | constructAbstractSubprogramScopeDIE(TheCU, AScope); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1658 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1659 | |
David Blaikie | 38b74bf | 2016-12-15 23:37:38 +0000 | [diff] [blame] | 1660 | ProcessedSPNodes.insert(SP); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 1661 | DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1662 | if (auto *SkelCU = TheCU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 1663 | if (!LScopes.getAbstractScopesList().empty() && |
| 1664 | TheCU.getCUNode()->getSplitDebugInlining()) |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1665 | SkelCU->constructSubprogramScopeDIE(SP, FnScope); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1666 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 1667 | // Construct call site entries. |
| 1668 | constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF); |
| 1669 | |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1670 | // Clear debug info |
David Blaikie | 825bdd2 | 2014-05-21 22:41:17 +0000 | [diff] [blame] | 1671 | // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the |
| 1672 | // DbgVariables except those that are also in AbstractVariables (since they |
| 1673 | // can be used cross-function) |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1674 | InfoHolder.getScopeVariables().clear(); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1675 | InfoHolder.getScopeLabels().clear(); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1676 | PrevLabel = nullptr; |
| 1677 | CurFn = nullptr; |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1680 | // Register a source line with debug info. Returns the unique label that was |
| 1681 | // emitted and which provides correspondence to the source line list. |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1682 | void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, |
| 1683 | unsigned Flags) { |
Alexey Bataev | 9d5974a | 2019-01-22 17:24:16 +0000 | [diff] [blame] | 1684 | ::recordSourceLine(*Asm, Line, Col, S, Flags, |
| 1685 | Asm->OutStreamer->getContext().getDwarfCompileUnitID(), |
| 1686 | getDwarfVersion(), getUnits()); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Bill Wendling | 806535f | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 1689 | //===----------------------------------------------------------------------===// |
| 1690 | // Emit Methods |
| 1691 | //===----------------------------------------------------------------------===// |
| 1692 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1693 | // Emit the debug info section. |
| 1694 | void DwarfDebug::emitDebugInfo() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1695 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1696 | Holder.emitUnits(/* UseOffsets */ false); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1699 | // Emit the abbreviation section. |
Eric Christopher | 3837195 | 2012-11-20 23:30:11 +0000 | [diff] [blame] | 1700 | void DwarfDebug::emitAbbreviations() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1701 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 1702 | |
| 1703 | Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 1704 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1705 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1706 | void DwarfDebug::emitStringOffsetsTableHeader() { |
| 1707 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Pavel Labath | 7bfa5d6 | 2018-07-26 14:36:07 +0000 | [diff] [blame] | 1708 | Holder.getStringPool().emitStringOffsetsTableHeader( |
| 1709 | *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(), |
| 1710 | Holder.getStringOffsetsStartSym()); |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 1713 | template <typename AccelTableT> |
| 1714 | void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section, |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1715 | StringRef TableName) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1716 | Asm->OutStreamer->SwitchSection(Section); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Emit the full data. |
Pavel Labath | a7c457d | 2018-02-19 16:12:20 +0000 | [diff] [blame] | 1719 | emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol()); |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 1722 | void DwarfDebug::emitAccelDebugNames() { |
Pavel Labath | eadfac8 | 2018-04-09 14:38:53 +0000 | [diff] [blame] | 1723 | // Don't emit anything if we have no compilation units to index. |
| 1724 | if (getUnits().empty()) |
| 1725 | return; |
| 1726 | |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 1727 | emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits()); |
| 1728 | } |
| 1729 | |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1730 | // Emit visible names into a hashed accelerator table section. |
| 1731 | void DwarfDebug::emitAccelNames() { |
| 1732 | emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1733 | "Names"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 1736 | // Emit objective C classes and categories into a hashed accelerator table |
| 1737 | // section. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1738 | void DwarfDebug::emitAccelObjC() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1739 | emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1740 | "ObjC"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1743 | // Emit namespace dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1744 | void DwarfDebug::emitAccelNamespaces() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1745 | emitAccel(AccelNamespace, |
| 1746 | Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1747 | "namespac"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1750 | // Emit type dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1751 | void DwarfDebug::emitAccelTypes() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1752 | emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1753 | "types"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1756 | // Public name handling. |
| 1757 | // The format for the various pubnames: |
| 1758 | // |
| 1759 | // dwarf pubnames - offset/name pairs where the offset is the offset into the CU |
| 1760 | // for the DIE that is named. |
| 1761 | // |
| 1762 | // gnu pubnames - offset/index value/name tuples where the offset is the offset |
| 1763 | // into the CU and the index value is computed according to the type of value |
| 1764 | // for the DIE that is named. |
| 1765 | // |
| 1766 | // For type units the offset is the offset of the skeleton DIE. For split dwarf |
| 1767 | // it's the offset within the debug_info/debug_types dwo section, however, the |
| 1768 | // reference in the pubname header doesn't change. |
| 1769 | |
| 1770 | /// computeIndexValue - Compute the gdb index value for the DIE and CU. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1771 | static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, |
Eric Christopher | 0fe676a | 2013-11-21 00:48:22 +0000 | [diff] [blame] | 1772 | const DIE *Die) { |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1773 | // Entities that ended up only in a Type Unit reference the CU instead (since |
| 1774 | // the pub entry has offsets within the CU there's no real offset that can be |
| 1775 | // provided anyway). As it happens all such entities (namespaces and types, |
| 1776 | // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out |
| 1777 | // not to be true it would be necessary to persist this information from the |
| 1778 | // point at which the entry is added to the index data structure - since by |
| 1779 | // the time the index is built from that, the original type/namespace DIE in a |
| 1780 | // type unit has already been destroyed so it can't be queried for properties |
| 1781 | // like tag, etc. |
| 1782 | if (Die->getTag() == dwarf::DW_TAG_compile_unit) |
| 1783 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, |
| 1784 | dwarf::GIEL_EXTERNAL); |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1785 | dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; |
| 1786 | |
| 1787 | // We could have a specification DIE that has our most of our knowledge, |
| 1788 | // look for that now. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1789 | if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { |
| 1790 | DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); |
David Blaikie | 8dbcc3f | 2014-04-25 19:33:43 +0000 | [diff] [blame] | 1791 | if (SpecDIE.findAttribute(dwarf::DW_AT_external)) |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1792 | Linkage = dwarf::GIEL_EXTERNAL; |
| 1793 | } else if (Die->findAttribute(dwarf::DW_AT_external)) |
| 1794 | Linkage = dwarf::GIEL_EXTERNAL; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1795 | |
| 1796 | switch (Die->getTag()) { |
| 1797 | case dwarf::DW_TAG_class_type: |
| 1798 | case dwarf::DW_TAG_structure_type: |
| 1799 | case dwarf::DW_TAG_union_type: |
| 1800 | case dwarf::DW_TAG_enumeration_type: |
Eric Christopher | 261d234 | 2013-09-23 20:55:35 +0000 | [diff] [blame] | 1801 | return dwarf::PubIndexEntryDescriptor( |
| 1802 | dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus |
| 1803 | ? dwarf::GIEL_STATIC |
| 1804 | : dwarf::GIEL_EXTERNAL); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1805 | case dwarf::DW_TAG_typedef: |
| 1806 | case dwarf::DW_TAG_base_type: |
| 1807 | case dwarf::DW_TAG_subrange_type: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1808 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1809 | case dwarf::DW_TAG_namespace: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1810 | return dwarf::GIEK_TYPE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1811 | case dwarf::DW_TAG_subprogram: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1812 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1813 | case dwarf::DW_TAG_variable: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1814 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1815 | case dwarf::DW_TAG_enumerator: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1816 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, |
| 1817 | dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1818 | default: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1819 | return dwarf::GIEK_NONE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1820 | } |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1823 | /// emitDebugPubSections - Emit visible names and types into debug pubnames and |
| 1824 | /// pubtypes sections. |
| 1825 | void DwarfDebug::emitDebugPubSections() { |
David Blaikie | c3d9e9e | 2014-03-06 01:42:00 +0000 | [diff] [blame] | 1826 | for (const auto &NU : CUMap) { |
| 1827 | DwarfCompileUnit *TheU = NU.second; |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1828 | if (!TheU->hasDwarfPubSections()) |
David Blaikie | ce2f1cb | 2014-03-11 23:35:06 +0000 | [diff] [blame] | 1829 | continue; |
| 1830 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1831 | bool GnuStyle = TheU->getCUNode()->getNameTableKind() == |
| 1832 | DICompileUnit::DebugNameTableKind::GNU; |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1833 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1834 | Asm->OutStreamer->SwitchSection( |
| 1835 | GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() |
| 1836 | : Asm->getObjFileLowering().getDwarfPubNamesSection()); |
| 1837 | emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1838 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1839 | Asm->OutStreamer->SwitchSection( |
| 1840 | GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() |
| 1841 | : Asm->getObjFileLowering().getDwarfPubTypesSection()); |
| 1842 | emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1846 | void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) { |
| 1847 | if (useSectionsAsReferences()) |
| 1848 | Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(), |
| 1849 | CU.getDebugSectionOffset()); |
| 1850 | else |
| 1851 | Asm->emitDwarfSymbolReference(CU.getLabelBegin()); |
| 1852 | } |
| 1853 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1854 | void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name, |
| 1855 | DwarfCompileUnit *TheU, |
| 1856 | const StringMap<const DIE *> &Globals) { |
| 1857 | if (auto *Skeleton = TheU->getSkeleton()) |
| 1858 | TheU = Skeleton; |
Eric Christopher | 8b3737f | 2013-09-13 00:34:58 +0000 | [diff] [blame] | 1859 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1860 | // Emit the header. |
| 1861 | Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); |
| 1862 | MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); |
| 1863 | MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); |
| 1864 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); |
| 1865 | |
| 1866 | Asm->OutStreamer->EmitLabel(BeginLabel); |
| 1867 | |
| 1868 | Asm->OutStreamer->AddComment("DWARF Version"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 1869 | Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1870 | |
| 1871 | Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1872 | emitSectionReference(*TheU); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1873 | |
| 1874 | Asm->OutStreamer->AddComment("Compilation Unit Length"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 1875 | Asm->emitInt32(TheU->getLength()); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Emit the pubnames for this compilation unit. |
| 1878 | for (const auto &GI : Globals) { |
| 1879 | const char *Name = GI.getKeyData(); |
| 1880 | const DIE *Entity = GI.second; |
| 1881 | |
| 1882 | Asm->OutStreamer->AddComment("DIE offset"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 1883 | Asm->emitInt32(Entity->getOffset()); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (GnuStyle) { |
| 1886 | dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); |
| 1887 | Asm->OutStreamer->AddComment( |
Fangrui Song | d8fd0ec | 2018-11-13 20:18:08 +0000 | [diff] [blame] | 1888 | Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + |
| 1889 | ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 1890 | Asm->emitInt8(Desc.toBits()); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | Asm->OutStreamer->AddComment("External Name"); |
| 1894 | Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); |
| 1895 | } |
| 1896 | |
| 1897 | Asm->OutStreamer->AddComment("End Mark"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 1898 | Asm->emitInt32(0); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1899 | Asm->OutStreamer->EmitLabel(EndLabel); |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1902 | /// Emit null-terminated strings into a debug str section. |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1903 | void DwarfDebug::emitDebugStr() { |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1904 | MCSection *StringOffsetsSection = nullptr; |
| 1905 | if (useSegmentedStringOffsetsTable()) { |
| 1906 | emitStringOffsetsTableHeader(); |
| 1907 | StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection(); |
| 1908 | } |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1909 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1910 | Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(), |
| 1911 | StringOffsetsSection, /* UseRelativeOffsets = */ true); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1914 | void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1915 | const DebugLocStream::Entry &Entry) { |
| 1916 | auto &&Comments = DebugLocs.getComments(Entry); |
| 1917 | auto Comment = Comments.begin(); |
| 1918 | auto End = Comments.end(); |
| 1919 | for (uint8_t Byte : DebugLocs.getBytes(Entry)) |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1920 | Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : ""); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1923 | static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1924 | const DebugLocEntry::Value &Value, |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1925 | DwarfExpression &DwarfExpr) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1926 | auto *DIExpr = Value.getExpression(); |
| 1927 | DIExpressionCursor ExprCursor(DIExpr); |
| 1928 | DwarfExpr.addFragmentOffset(DIExpr); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1929 | // Regular entry. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1930 | if (Value.isInt()) { |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1931 | if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || |
| 1932 | BT->getEncoding() == dwarf::DW_ATE_signed_char)) |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1933 | DwarfExpr.addSignedConstant(Value.getInt()); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 1934 | else |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1935 | DwarfExpr.addUnsignedConstant(Value.getInt()); |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1936 | } else if (Value.isLocation()) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1937 | MachineLocation Location = Value.getLoc(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1938 | if (Location.isIndirect()) |
| 1939 | DwarfExpr.setMemoryLocationKind(); |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1940 | DIExpressionCursor Cursor(DIExpr); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1941 | const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1942 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1943 | return; |
| 1944 | return DwarfExpr.addExpression(std::move(Cursor)); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 1945 | } else if (Value.isConstantFP()) { |
| 1946 | APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1947 | DwarfExpr.addUnsignedConstant(RawBytes); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1948 | } |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1949 | DwarfExpr.addExpression(std::move(ExprCursor)); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1952 | void DebugLocEntry::finalize(const AsmPrinter &AP, |
| 1953 | DebugLocStream::ListBuilder &List, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1954 | const DIBasicType *BT) { |
David Stenberg | 33b192d | 2019-01-09 09:58:59 +0000 | [diff] [blame] | 1955 | assert(Begin != End && "unexpected location list entry with empty range"); |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1956 | DebugLocStream::EntryBuilder Entry(List, Begin, End); |
| 1957 | BufferByteStreamer Streamer = Entry.getStreamer(); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1958 | DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1959 | const DebugLocEntry::Value &Value = Values[0]; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1960 | if (Value.isFragment()) { |
| 1961 | // Emit all fragments that belong to the same variable and range. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1962 | assert(llvm::all_of(Values, [](DebugLocEntry::Value P) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1963 | return P.isFragment(); |
| 1964 | }) && "all values are expected to be fragments"); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1965 | assert(std::is_sorted(Values.begin(), Values.end()) && |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1966 | "fragments are expected to be sorted"); |
Duncan P. N. Exon Smith | c82570b | 2015-04-13 18:53:11 +0000 | [diff] [blame] | 1967 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1968 | for (auto Fragment : Values) |
Petar Jovanovic | d175aeb | 2018-06-28 04:50:40 +0000 | [diff] [blame] | 1969 | emitDebugLocValue(AP, BT, Fragment, DwarfExpr); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1970 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1971 | } else { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1972 | assert(Values.size() == 1 && "only fragments may have >1 value"); |
Petar Jovanovic | d175aeb | 2018-06-28 04:50:40 +0000 | [diff] [blame] | 1973 | emitDebugLocValue(AP, BT, Value, DwarfExpr); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1974 | } |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1975 | DwarfExpr.finalize(); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1978 | void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) { |
Duncan P. N. Exon Smith | 653c109 | 2015-05-06 19:11:20 +0000 | [diff] [blame] | 1979 | // Emit the size. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1980 | Asm->OutStreamer->AddComment("Loc expr size"); |
Wolfgang Pieb | 58513b7 | 2019-02-01 17:11:58 +0000 | [diff] [blame] | 1981 | if (getDwarfVersion() >= 5) |
| 1982 | Asm->EmitULEB128(DebugLocs.getBytes(Entry).size()); |
| 1983 | else |
| 1984 | Asm->emitInt16(DebugLocs.getBytes(Entry).size()); |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1985 | // Emit the entry. |
| 1986 | APByteStreamer Streamer(*Asm); |
| 1987 | emitDebugLocEntry(Streamer, Entry); |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 1990 | // Emit the common part of the DWARF 5 range/locations list tables header. |
| 1991 | static void emitListsTableHeaderStart(AsmPrinter *Asm, const DwarfFile &Holder, |
| 1992 | MCSymbol *TableStart, |
| 1993 | MCSymbol *TableEnd) { |
| 1994 | // Build the table header, which starts with the length field. |
| 1995 | Asm->OutStreamer->AddComment("Length"); |
| 1996 | Asm->EmitLabelDifference(TableEnd, TableStart, 4); |
| 1997 | Asm->OutStreamer->EmitLabel(TableStart); |
| 1998 | // Version number (DWARF v5 and later). |
| 1999 | Asm->OutStreamer->AddComment("Version"); |
| 2000 | Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion()); |
| 2001 | // Address size. |
| 2002 | Asm->OutStreamer->AddComment("Address size"); |
| 2003 | Asm->emitInt8(Asm->MAI->getCodePointerSize()); |
| 2004 | // Segment selector size. |
| 2005 | Asm->OutStreamer->AddComment("Segment selector size"); |
| 2006 | Asm->emitInt8(0); |
| 2007 | } |
| 2008 | |
| 2009 | // Emit the header of a DWARF 5 range list table list table. Returns the symbol |
| 2010 | // that designates the end of the table for the caller to emit when the table is |
| 2011 | // complete. |
| 2012 | static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, |
| 2013 | const DwarfFile &Holder) { |
| 2014 | MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start"); |
| 2015 | MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end"); |
| 2016 | emitListsTableHeaderStart(Asm, Holder, TableStart, TableEnd); |
| 2017 | |
| 2018 | Asm->OutStreamer->AddComment("Offset entry count"); |
| 2019 | Asm->emitInt32(Holder.getRangeLists().size()); |
| 2020 | Asm->OutStreamer->EmitLabel(Holder.getRnglistsTableBaseSym()); |
| 2021 | |
| 2022 | for (const RangeSpanList &List : Holder.getRangeLists()) |
| 2023 | Asm->EmitLabelDifference(List.getSym(), Holder.getRnglistsTableBaseSym(), |
| 2024 | 4); |
| 2025 | |
| 2026 | return TableEnd; |
| 2027 | } |
| 2028 | |
| 2029 | // Emit the header of a DWARF 5 locations list table. Returns the symbol that |
| 2030 | // designates the end of the table for the caller to emit when the table is |
| 2031 | // complete. |
| 2032 | static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm, |
| 2033 | const DwarfFile &Holder) { |
| 2034 | MCSymbol *TableStart = Asm->createTempSymbol("debug_loclist_table_start"); |
| 2035 | MCSymbol *TableEnd = Asm->createTempSymbol("debug_loclist_table_end"); |
| 2036 | emitListsTableHeaderStart(Asm, Holder, TableStart, TableEnd); |
| 2037 | |
| 2038 | // FIXME: Generate the offsets table and use DW_FORM_loclistx with the |
| 2039 | // DW_AT_loclists_base attribute. Until then set the number of offsets to 0. |
| 2040 | Asm->OutStreamer->AddComment("Offset entry count"); |
| 2041 | Asm->emitInt32(0); |
| 2042 | Asm->OutStreamer->EmitLabel(Holder.getLoclistsTableBaseSym()); |
| 2043 | |
| 2044 | return TableEnd; |
| 2045 | } |
| 2046 | |
| 2047 | // Emit locations into the .debug_loc/.debug_rnglists section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2048 | void DwarfDebug::emitDebugLoc() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 2049 | if (DebugLocs.getLists().empty()) |
| 2050 | return; |
| 2051 | |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2052 | bool IsLocLists = getDwarfVersion() >= 5; |
| 2053 | MCSymbol *TableEnd = nullptr; |
| 2054 | if (IsLocLists) { |
| 2055 | Asm->OutStreamer->SwitchSection( |
| 2056 | Asm->getObjFileLowering().getDwarfLoclistsSection()); |
| 2057 | TableEnd = emitLoclistsTableHeader(Asm, useSplitDwarf() ? SkeletonHolder |
| 2058 | : InfoHolder); |
| 2059 | } else { |
| 2060 | Asm->OutStreamer->SwitchSection( |
| 2061 | Asm->getObjFileLowering().getDwarfLocSection()); |
| 2062 | } |
| 2063 | |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 2064 | unsigned char Size = Asm->MAI->getCodePointerSize(); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2065 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2066 | Asm->OutStreamer->EmitLabel(List.Label); |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2067 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2068 | const DwarfCompileUnit *CU = List.CU; |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2069 | const MCSymbol *Base = CU->getBaseAddress(); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2070 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2071 | if (Base) { |
| 2072 | // Set up the range. This range is relative to the entry point of the |
| 2073 | // compile unit. This is a hard coded 0 for low_pc when we're emitting |
| 2074 | // ranges, or the DW_AT_low_pc on the compile unit otherwise. |
| 2075 | if (IsLocLists) { |
| 2076 | Asm->OutStreamer->AddComment("DW_LLE_offset_pair"); |
| 2077 | Asm->OutStreamer->EmitIntValue(dwarf::DW_LLE_offset_pair, 1); |
| 2078 | Asm->OutStreamer->AddComment(" starting offset"); |
| 2079 | Asm->EmitLabelDifferenceAsULEB128(Entry.BeginSym, Base); |
| 2080 | Asm->OutStreamer->AddComment(" ending offset"); |
| 2081 | Asm->EmitLabelDifferenceAsULEB128(Entry.EndSym, Base); |
| 2082 | } else { |
| 2083 | Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); |
| 2084 | Asm->EmitLabelDifference(Entry.EndSym, Base, Size); |
| 2085 | } |
| 2086 | |
| 2087 | emitDebugLocEntryLocation(Entry); |
| 2088 | continue; |
| 2089 | } |
| 2090 | |
| 2091 | // We have no base address. |
| 2092 | if (IsLocLists) { |
| 2093 | // TODO: Use DW_LLE_base_addressx + DW_LLE_offset_pair, or |
| 2094 | // DW_LLE_startx_length in case if there is only a single range. |
| 2095 | // That should reduce the size of the debug data emited. |
| 2096 | // For now just use the DW_LLE_startx_length for all cases. |
| 2097 | Asm->OutStreamer->AddComment("DW_LLE_startx_length"); |
| 2098 | Asm->emitInt8(dwarf::DW_LLE_startx_length); |
| 2099 | Asm->OutStreamer->AddComment(" start idx"); |
| 2100 | Asm->EmitULEB128(AddrPool.getIndex(Entry.BeginSym)); |
| 2101 | Asm->OutStreamer->AddComment(" length"); |
| 2102 | Asm->EmitLabelDifferenceAsULEB128(Entry.EndSym, Entry.BeginSym); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 2103 | } else { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2104 | Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); |
| 2105 | Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 2106 | } |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2107 | |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 2108 | emitDebugLocEntryLocation(Entry); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 2109 | } |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2110 | |
| 2111 | if (IsLocLists) { |
| 2112 | // .debug_loclists section ends with DW_LLE_end_of_list. |
| 2113 | Asm->OutStreamer->AddComment("DW_LLE_end_of_list"); |
| 2114 | Asm->OutStreamer->EmitIntValue(dwarf::DW_LLE_end_of_list, 1); |
| 2115 | } else { |
| 2116 | // Terminate the .debug_loc list with two 0 values. |
| 2117 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2118 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2119 | } |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2120 | } |
George Rimar | 088d96b | 2018-10-26 11:25:12 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (TableEnd) |
| 2123 | Asm->OutStreamer->EmitLabel(TableEnd); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | void DwarfDebug::emitDebugLocDWO() { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2127 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2128 | Asm->getObjFileLowering().getDwarfLocDWOSection()); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2129 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2130 | Asm->OutStreamer->EmitLabel(List.Label); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2131 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
David Blaikie | 73c2f19 | 2018-10-25 22:26:25 +0000 | [diff] [blame] | 2132 | // GDB only supports startx_length in pre-standard split-DWARF. |
| 2133 | // (in v5 standard loclists, it currently* /only/ supports base_address + |
| 2134 | // offset_pair, so the implementations can't really share much since they |
| 2135 | // need to use different representations) |
| 2136 | // * as of October 2018, at least |
| 2137 | // Ideally/in v5, this could use SectionLabels to reuse existing addresses |
| 2138 | // in the address pool to minimize object size/relocations. |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2139 | Asm->emitInt8(dwarf::DW_LLE_startx_length); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2140 | unsigned idx = AddrPool.getIndex(Entry.BeginSym); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2141 | Asm->EmitULEB128(idx); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 2142 | Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 2143 | |
| 2144 | emitDebugLocEntryLocation(Entry); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 2145 | } |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2146 | Asm->emitInt8(dwarf::DW_LLE_end_of_list); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 2147 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2150 | struct ArangeSpan { |
| 2151 | const MCSymbol *Start, *End; |
| 2152 | }; |
| 2153 | |
| 2154 | // Emit a debug aranges section, containing a CU lookup for any |
| 2155 | // address we can tie back to a CU. |
Eric Christopher | 7b30f2e4 | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 2156 | void DwarfDebug::emitDebugARanges() { |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2157 | // Provides a unique id per text section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 2158 | MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2159 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2160 | // Filter labels by section. |
| 2161 | for (const SymbolCU &SCU : ArangeLabels) { |
| 2162 | if (SCU.Sym->isInSection()) { |
| 2163 | // Make a note of this symbol and it's section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 2164 | MCSection *Section = &SCU.Sym->getSection(); |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2165 | if (!Section->getKind().isMetadata()) |
| 2166 | SectionMap[Section].push_back(SCU); |
| 2167 | } else { |
| 2168 | // Some symbols (e.g. common/bss on mach-o) can have no section but still |
| 2169 | // appear in the output. This sucks as we rely on sections to build |
| 2170 | // arange spans. We can do it without, but it's icky. |
| 2171 | SectionMap[nullptr].push_back(SCU); |
| 2172 | } |
| 2173 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2174 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2175 | DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; |
| 2176 | |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 2177 | for (auto &I : SectionMap) { |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 2178 | MCSection *Section = I.first; |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 2179 | SmallVector<SymbolCU, 8> &List = I.second; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 2180 | if (List.size() < 1) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2181 | continue; |
| 2182 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 2183 | // If we have no section (e.g. common), just write out |
| 2184 | // individual spans for each symbol. |
| 2185 | if (!Section) { |
| 2186 | for (const SymbolCU &Cur : List) { |
| 2187 | ArangeSpan Span; |
| 2188 | Span.Start = Cur.Sym; |
| 2189 | Span.End = nullptr; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 2190 | assert(Cur.CU); |
| 2191 | Spans[Cur.CU].push_back(Span); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 2192 | } |
| 2193 | continue; |
| 2194 | } |
| 2195 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2196 | // Sort the symbols by offset within the section. |
Mandeep Singh Grang | 46d02de | 2018-02-25 19:52:34 +0000 | [diff] [blame] | 2197 | std::stable_sort( |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 2198 | List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) { |
| 2199 | unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; |
| 2200 | unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; |
Benjamin Kramer | 571e2fe | 2014-03-07 19:41:22 +0000 | [diff] [blame] | 2201 | |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 2202 | // Symbols with no order assigned should be placed at the end. |
| 2203 | // (e.g. section end labels) |
| 2204 | if (IA == 0) |
| 2205 | return false; |
| 2206 | if (IB == 0) |
| 2207 | return true; |
| 2208 | return IA < IB; |
| 2209 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2210 | |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 2211 | // Insert a final terminator. |
| 2212 | List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); |
| 2213 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 2214 | // Build spans between each label. |
| 2215 | const MCSymbol *StartSym = List[0].Sym; |
| 2216 | for (size_t n = 1, e = List.size(); n < e; n++) { |
| 2217 | const SymbolCU &Prev = List[n - 1]; |
| 2218 | const SymbolCU &Cur = List[n]; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2219 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 2220 | // Try and build the longest span we can within the same CU. |
| 2221 | if (Cur.CU != Prev.CU) { |
| 2222 | ArangeSpan Span; |
| 2223 | Span.Start = StartSym; |
| 2224 | Span.End = Cur.Sym; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 2225 | assert(Prev.CU); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 2226 | Spans[Prev.CU].push_back(Span); |
| 2227 | StartSym = Cur.Sym; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2232 | // Start the dwarf aranges section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2233 | Asm->OutStreamer->SwitchSection( |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 2234 | Asm->getObjFileLowering().getDwarfARangesSection()); |
| 2235 | |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 2236 | unsigned PtrSize = Asm->MAI->getCodePointerSize(); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2237 | |
| 2238 | // Build a list of CUs used. |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 2239 | std::vector<DwarfCompileUnit *> CUs; |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 2240 | for (const auto &it : Spans) { |
| 2241 | DwarfCompileUnit *CU = it.first; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2242 | CUs.push_back(CU); |
| 2243 | } |
| 2244 | |
| 2245 | // Sort the CU list (again, to ensure consistent output order). |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 2246 | llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { |
| 2247 | return A->getUniqueID() < B->getUniqueID(); |
| 2248 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2249 | |
| 2250 | // Emit an arange table for each CU we used. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 2251 | for (DwarfCompileUnit *CU : CUs) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2252 | std::vector<ArangeSpan> &List = Spans[CU]; |
| 2253 | |
David Blaikie | 27e35f2 | 2014-11-02 01:21:43 +0000 | [diff] [blame] | 2254 | // Describe the skeleton CU's offset and length, not the dwo file's. |
| 2255 | if (auto *Skel = CU->getSkeleton()) |
| 2256 | CU = Skel; |
| 2257 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2258 | // Emit size of content not including length itself. |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 2259 | unsigned ContentSize = |
| 2260 | sizeof(int16_t) + // DWARF ARange version number |
| 2261 | sizeof(int32_t) + // Offset of CU in the .debug_info section |
| 2262 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 2263 | sizeof(int8_t); // Segment Size (in bytes) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2264 | |
| 2265 | unsigned TupleSize = PtrSize * 2; |
| 2266 | |
| 2267 | // 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] | 2268 | unsigned Padding = |
| 2269 | OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2270 | |
| 2271 | ContentSize += Padding; |
| 2272 | ContentSize += (List.size() + 1) * TupleSize; |
| 2273 | |
| 2274 | // For each compile unit, write the list of spans it covers. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2275 | Asm->OutStreamer->AddComment("Length of ARange Set"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2276 | Asm->emitInt32(ContentSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2277 | Asm->OutStreamer->AddComment("DWARF Arange version number"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2278 | Asm->emitInt16(dwarf::DW_ARANGES_VERSION); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2279 | Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 2280 | emitSectionReference(*CU); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2281 | Asm->OutStreamer->AddComment("Address Size (in bytes)"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2282 | Asm->emitInt8(PtrSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2283 | Asm->OutStreamer->AddComment("Segment Size (in bytes)"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2284 | Asm->emitInt8(0); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2285 | |
Petr Hosek | faef320 | 2016-06-01 01:59:58 +0000 | [diff] [blame] | 2286 | Asm->OutStreamer->emitFill(Padding, 0xff); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2287 | |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 2288 | for (const ArangeSpan &Span : List) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2289 | Asm->EmitLabelReference(Span.Start, PtrSize); |
| 2290 | |
| 2291 | // 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] | 2292 | if (Span.End) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2293 | Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 2294 | } else { |
| 2295 | // For symbols without an end marker (e.g. common), we |
| 2296 | // write a single arange entry containing just that one symbol. |
| 2297 | uint64_t Size = SymSize[Span.Start]; |
| 2298 | if (Size == 0) |
| 2299 | Size = 1; |
| 2300 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2301 | Asm->OutStreamer->EmitIntValue(Size, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 2302 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2305 | Asm->OutStreamer->AddComment("ARange terminator"); |
| 2306 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
| 2307 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 2308 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2311 | /// Emit a single range list. We handle both DWARF v5 and earlier. |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2312 | static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2313 | const RangeSpanList &List) { |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2314 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2315 | auto DwarfVersion = DD.getDwarfVersion(); |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2316 | // Emit our symbol so we can find the beginning of the range. |
| 2317 | Asm->OutStreamer->EmitLabel(List.getSym()); |
| 2318 | // Gather all the ranges that apply to the same section so they can share |
| 2319 | // a base address entry. |
| 2320 | MapVector<const MCSection *, std::vector<const RangeSpan *>> SectionRanges; |
| 2321 | // Size for our labels. |
| 2322 | auto Size = Asm->MAI->getCodePointerSize(); |
| 2323 | |
| 2324 | for (const RangeSpan &Range : List.getRanges()) |
| 2325 | SectionRanges[&Range.getStart()->getSection()].push_back(&Range); |
| 2326 | |
David Blaikie | c8f7e6c | 2018-11-08 00:35:54 +0000 | [diff] [blame] | 2327 | const DwarfCompileUnit &CU = List.getCU(); |
| 2328 | const MCSymbol *CUBase = CU.getBaseAddress(); |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2329 | bool BaseIsSet = false; |
| 2330 | for (const auto &P : SectionRanges) { |
| 2331 | // Don't bother with a base address entry if there's only one range in |
| 2332 | // this section in this range list - for example ranges for a CU will |
| 2333 | // usually consist of single regions from each of many sections |
| 2334 | // (-ffunction-sections, or just C++ inline functions) except under LTO |
| 2335 | // or optnone where there may be holes in a single CU's section |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2336 | // contributions. |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2337 | auto *Base = CUBase; |
David Blaikie | 14cfa0d | 2018-10-20 09:16:49 +0000 | [diff] [blame] | 2338 | if (!Base && (P.second.size() > 1 || DwarfVersion < 5) && |
David Blaikie | bb27911 | 2018-11-13 20:08:10 +0000 | [diff] [blame] | 2339 | (CU.getCUNode()->getRangesBaseAddress() || DwarfVersion >= 5)) { |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2340 | BaseIsSet = true; |
| 2341 | // FIXME/use care: This may not be a useful base address if it's not |
| 2342 | // the lowest address/range in this object. |
| 2343 | Base = P.second.front()->getStart(); |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2344 | if (DwarfVersion >= 5) { |
David Blaikie | 60fddac | 2018-10-24 23:36:29 +0000 | [diff] [blame] | 2345 | Base = DD.getSectionLabel(&Base->getSection()); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2346 | Asm->OutStreamer->AddComment("DW_RLE_base_addressx"); |
| 2347 | Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_addressx, 1); |
| 2348 | Asm->OutStreamer->AddComment(" base address index"); |
| 2349 | Asm->EmitULEB128(DD.getAddressPool().getIndex(Base)); |
| 2350 | } else { |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2351 | Asm->OutStreamer->EmitIntValue(-1, Size); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2352 | Asm->OutStreamer->AddComment(" base address"); |
| 2353 | Asm->OutStreamer->EmitSymbolValue(Base, Size); |
| 2354 | } |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2355 | } else if (BaseIsSet && DwarfVersion < 5) { |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2356 | BaseIsSet = false; |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2357 | assert(!Base); |
| 2358 | Asm->OutStreamer->EmitIntValue(-1, Size); |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2359 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2360 | } |
| 2361 | |
| 2362 | for (const auto *RS : P.second) { |
| 2363 | const MCSymbol *Begin = RS->getStart(); |
| 2364 | const MCSymbol *End = RS->getEnd(); |
| 2365 | assert(Begin && "Range without a begin symbol?"); |
| 2366 | assert(End && "Range without an end symbol?"); |
| 2367 | if (Base) { |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2368 | if (DwarfVersion >= 5) { |
| 2369 | // Emit DW_RLE_offset_pair when we have a base. |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2370 | Asm->OutStreamer->AddComment("DW_RLE_offset_pair"); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2371 | Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1); |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2372 | Asm->OutStreamer->AddComment(" starting offset"); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2373 | Asm->EmitLabelDifferenceAsULEB128(Begin, Base); |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2374 | Asm->OutStreamer->AddComment(" ending offset"); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2375 | Asm->EmitLabelDifferenceAsULEB128(End, Base); |
| 2376 | } else { |
| 2377 | Asm->EmitLabelDifference(Begin, Base, Size); |
| 2378 | Asm->EmitLabelDifference(End, Base, Size); |
| 2379 | } |
| 2380 | } else if (DwarfVersion >= 5) { |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2381 | Asm->OutStreamer->AddComment("DW_RLE_startx_length"); |
| 2382 | Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_startx_length, 1); |
| 2383 | Asm->OutStreamer->AddComment(" start index"); |
| 2384 | Asm->EmitULEB128(DD.getAddressPool().getIndex(Begin)); |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2385 | Asm->OutStreamer->AddComment(" length"); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2386 | Asm->EmitLabelDifferenceAsULEB128(End, Begin); |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2387 | } else { |
| 2388 | Asm->OutStreamer->EmitSymbolValue(Begin, Size); |
| 2389 | Asm->OutStreamer->EmitSymbolValue(End, Size); |
| 2390 | } |
| 2391 | } |
| 2392 | } |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2393 | if (DwarfVersion >= 5) { |
| 2394 | Asm->OutStreamer->AddComment("DW_RLE_end_of_list"); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2395 | Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1); |
David Blaikie | d661405 | 2018-07-18 18:04:42 +0000 | [diff] [blame] | 2396 | } else { |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2397 | // Terminate the list with two 0 values. |
| 2398 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2399 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2400 | } |
Wolfgang Pieb | e194f73 | 2018-07-10 00:10:11 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame] | 2403 | static void emitDebugRangesImpl(DwarfDebug &DD, AsmPrinter *Asm, |
| 2404 | const DwarfFile &Holder, MCSymbol *TableEnd) { |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2405 | for (const RangeSpanList &List : Holder.getRangeLists()) |
| 2406 | emitRangeList(DD, Asm, List); |
| 2407 | |
| 2408 | if (TableEnd) |
| 2409 | Asm->OutStreamer->EmitLabel(TableEnd); |
| 2410 | } |
| 2411 | |
Wolfgang Pieb | 9ea6508 | 2018-07-26 22:48:52 +0000 | [diff] [blame] | 2412 | /// Emit address ranges into the .debug_ranges section or into the DWARF v5 |
| 2413 | /// .debug_rnglists section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2414 | void DwarfDebug::emitDebugRanges() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 2415 | if (CUMap.empty()) |
| 2416 | return; |
| 2417 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2418 | const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Wolfgang Pieb | 9ea6508 | 2018-07-26 22:48:52 +0000 | [diff] [blame] | 2419 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2420 | if (Holder.getRangeLists().empty()) |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 2421 | return; |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 2422 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2423 | assert(useRangesSection()); |
| 2424 | assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { |
| 2425 | return Pair.second->getCUNode()->isDebugDirectivesOnly(); |
| 2426 | })); |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 2427 | |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2428 | // Start the dwarf ranges section. |
Wolfgang Pieb | 9ea6508 | 2018-07-26 22:48:52 +0000 | [diff] [blame] | 2429 | MCSymbol *TableEnd = nullptr; |
| 2430 | if (getDwarfVersion() >= 5) { |
| 2431 | Asm->OutStreamer->SwitchSection( |
| 2432 | Asm->getObjFileLowering().getDwarfRnglistsSection()); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2433 | TableEnd = emitRnglistsTableHeader(Asm, Holder); |
Wolfgang Pieb | 9ea6508 | 2018-07-26 22:48:52 +0000 | [diff] [blame] | 2434 | } else |
| 2435 | Asm->OutStreamer->SwitchSection( |
| 2436 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Eric Christopher | 4751d70 | 2013-11-23 00:05:29 +0000 | [diff] [blame] | 2437 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 2438 | emitDebugRangesImpl(*this, Asm, Holder, TableEnd); |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
David Blaikie | 32e09de | 2018-10-20 08:12:36 +0000 | [diff] [blame] | 2441 | void DwarfDebug::emitDebugRangesDWO() { |
| 2442 | assert(useSplitDwarf()); |
| 2443 | |
| 2444 | if (CUMap.empty()) |
| 2445 | return; |
| 2446 | |
| 2447 | const auto &Holder = InfoHolder; |
| 2448 | |
| 2449 | if (Holder.getRangeLists().empty()) |
| 2450 | return; |
| 2451 | |
| 2452 | assert(getDwarfVersion() >= 5); |
| 2453 | assert(useRangesSection()); |
| 2454 | assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { |
| 2455 | return Pair.second->getCUNode()->isDebugDirectivesOnly(); |
| 2456 | })); |
| 2457 | |
| 2458 | // Start the dwarf ranges section. |
| 2459 | Asm->OutStreamer->SwitchSection( |
| 2460 | Asm->getObjFileLowering().getDwarfRnglistsDWOSection()); |
| 2461 | MCSymbol *TableEnd = emitRnglistsTableHeader(Asm, Holder); |
| 2462 | |
| 2463 | emitDebugRangesImpl(*this, Asm, Holder, TableEnd); |
| 2464 | } |
| 2465 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2466 | void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2467 | for (auto *MN : Nodes) { |
| 2468 | if (auto *M = dyn_cast<DIMacro>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2469 | emitMacro(*M); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2470 | else if (auto *F = dyn_cast<DIMacroFile>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2471 | emitMacroFile(*F, U); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2472 | else |
| 2473 | llvm_unreachable("Unexpected DI type!"); |
| 2474 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2477 | void DwarfDebug::emitMacro(DIMacro &M) { |
| 2478 | Asm->EmitULEB128(M.getMacinfoType()); |
| 2479 | Asm->EmitULEB128(M.getLine()); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2480 | StringRef Name = M.getName(); |
| 2481 | StringRef Value = M.getValue(); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2482 | Asm->OutStreamer->EmitBytes(Name); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2483 | if (!Value.empty()) { |
| 2484 | // There should be one space between macro name and macro value. |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2485 | Asm->emitInt8(' '); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2486 | Asm->OutStreamer->EmitBytes(Value); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2487 | } |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2488 | Asm->emitInt8('\0'); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2491 | void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2492 | assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2493 | Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); |
| 2494 | Asm->EmitULEB128(F.getLine()); |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 2495 | Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile())); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2496 | handleMacroNodes(F.getElements(), U); |
| 2497 | Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 2500 | /// Emit macros into a debug macinfo section. |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2501 | void DwarfDebug::emitDebugMacinfo() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 2502 | if (CUMap.empty()) |
| 2503 | return; |
| 2504 | |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 2505 | if (llvm::all_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { |
| 2506 | return Pair.second->getCUNode()->isDebugDirectivesOnly(); |
| 2507 | })) |
| 2508 | return; |
| 2509 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2510 | // Start the dwarf macinfo section. |
| 2511 | Asm->OutStreamer->SwitchSection( |
| 2512 | Asm->getObjFileLowering().getDwarfMacinfoSection()); |
| 2513 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2514 | for (const auto &P : CUMap) { |
| 2515 | auto &TheCU = *P.second; |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 2516 | if (TheCU.getCUNode()->isDebugDirectivesOnly()) |
| 2517 | continue; |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2518 | auto *SkCU = TheCU.getSkeleton(); |
| 2519 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
| 2520 | auto *CUNode = cast<DICompileUnit>(P.first); |
Alexey Bataev | bd786944 | 2018-02-22 16:20:30 +0000 | [diff] [blame] | 2521 | DIMacroNodeArray Macros = CUNode->getMacros(); |
| 2522 | if (!Macros.empty()) { |
| 2523 | Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); |
| 2524 | handleMacroNodes(Macros, U); |
| 2525 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2526 | } |
| 2527 | Asm->OutStreamer->AddComment("End Of Macro List Mark"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 2528 | Asm->emitInt8(0); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2531 | // DWARF5 Experimental Separate Dwarf emitters. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2532 | |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2533 | void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2534 | std::unique_ptr<DwarfCompileUnit> NewU) { |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2535 | |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2536 | if (!CompilationDir.empty()) |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 2537 | NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2538 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2539 | addGnuPubAttributes(*NewU, Die); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2540 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2541 | SkeletonHolder.addUnit(std::move(NewU)); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2544 | DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2545 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2546 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
David Blaikie | bd57905 | 2014-04-28 21:14:27 +0000 | [diff] [blame] | 2547 | CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2548 | DwarfCompileUnit &NewCU = *OwnedUnit; |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2549 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
Eric Christopher | 4c7765f | 2013-01-17 03:00:04 +0000 | [diff] [blame] | 2550 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2551 | NewCU.initStmtList(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2552 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2553 | if (useSegmentedStringOffsetsTable()) |
| 2554 | NewCU.addStringOffsetsStart(); |
| 2555 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2556 | initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 2557 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2558 | return NewCU; |
| 2559 | } |
| 2560 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2561 | // Emit the .debug_info.dwo section for separated dwarf. This contains the |
| 2562 | // compile units that would normally be in debug_info. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2563 | void DwarfDebug::emitDebugInfoDWO() { |
Eric Christopher | cdf218d | 2012-12-10 19:51:21 +0000 | [diff] [blame] | 2564 | assert(useSplitDwarf() && "No split dwarf debug info?"); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2565 | // Don't emit relocations into the dwo file. |
| 2566 | InfoHolder.emitUnits(/* UseOffsets */ true); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the |
| 2570 | // abbreviations for the .debug_info.dwo section. |
| 2571 | void DwarfDebug::emitDebugAbbrevDWO() { |
| 2572 | assert(useSplitDwarf() && "No split dwarf?"); |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 2573 | InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2574 | } |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2575 | |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2576 | void DwarfDebug::emitDebugLineDWO() { |
| 2577 | assert(useSplitDwarf() && "No split dwarf?"); |
Paul Robinson | 7cb26ad | 2018-03-27 21:28:59 +0000 | [diff] [blame] | 2578 | SplitTypeUnitFileTable.Emit( |
| 2579 | *Asm->OutStreamer, MCDwarfLineTableParams(), |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2580 | Asm->getObjFileLowering().getDwarfLineDWOSection()); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2583 | void DwarfDebug::emitStringOffsetsTableHeaderDWO() { |
| 2584 | assert(useSplitDwarf() && "No split dwarf?"); |
Pavel Labath | 7bfa5d6 | 2018-07-26 14:36:07 +0000 | [diff] [blame] | 2585 | InfoHolder.getStringPool().emitStringOffsetsTableHeader( |
| 2586 | *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(), |
| 2587 | InfoHolder.getStringOffsetsStartSym()); |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2590 | // Emit the .debug_str.dwo section for separated dwarf. This contains the |
| 2591 | // string section and is identical in format to traditional .debug_str |
| 2592 | // sections. |
| 2593 | void DwarfDebug::emitDebugStrDWO() { |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2594 | if (useSegmentedStringOffsetsTable()) |
| 2595 | emitStringOffsetsTableHeaderDWO(); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2596 | assert(useSplitDwarf() && "No split dwarf?"); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 2597 | MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); |
Eric Christopher | 2cbd576 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 2598 | InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2599 | OffSec, /* UseRelativeOffsets = */ false); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2600 | } |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2601 | |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 2602 | // Emit address pool. |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 2603 | void DwarfDebug::emitDebugAddr() { |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 2604 | AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); |
| 2605 | } |
| 2606 | |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2607 | MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { |
| 2608 | if (!useSplitDwarf()) |
| 2609 | return nullptr; |
Paul Robinson | b271f31 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 2610 | const DICompileUnit *DIUnit = CU.getCUNode(); |
| 2611 | SplitTypeUnitFileTable.maybeSetRootFile( |
| 2612 | DIUnit->getDirectory(), DIUnit->getFilename(), |
| 2613 | CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource()); |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2614 | return &SplitTypeUnitFileTable; |
| 2615 | } |
| 2616 | |
Adrian Prantl | ee5feaf | 2015-07-15 17:01:41 +0000 | [diff] [blame] | 2617 | uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2618 | MD5 Hash; |
| 2619 | Hash.update(Identifier); |
| 2620 | // ... take the least significant 8 bytes and return those. Our MD5 |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2621 | // implementation always returns its results in little endian, so we actually |
| 2622 | // need the "high" word. |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2623 | MD5::MD5Result Result; |
| 2624 | Hash.final(Result); |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2625 | return Result.high(); |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
David Blaikie | 15632ae | 2014-02-12 00:31:30 +0000 | [diff] [blame] | 2628 | void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2629 | StringRef Identifier, DIE &RefDie, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2630 | const DICompositeType *CTy) { |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2631 | // Fast path if we're building some type units and one has already used the |
| 2632 | // address pool we know we're going to throw away all this work anyway, so |
| 2633 | // don't bother building dependent types. |
| 2634 | if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) |
| 2635 | return; |
| 2636 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2637 | auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); |
| 2638 | if (!Ins.second) { |
| 2639 | CU.addDIETypeSignature(RefDie, Ins.first->second); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2640 | return; |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2643 | bool TopLevelType = TypeUnitsUnderConstruction.empty(); |
| 2644 | AddrPool.resetUsedFlag(); |
| 2645 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2646 | auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, |
| 2647 | getDwoLineTable(CU)); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2648 | DwarfTypeUnit &NewTU = *OwnedUnit; |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2649 | DIE &UnitDie = NewTU.getUnitDie(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2650 | TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2651 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2652 | NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
David Blaikie | 637cac4 | 2014-04-22 23:09:36 +0000 | [diff] [blame] | 2653 | CU.getLanguage()); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2654 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2655 | uint64_t Signature = makeTypeSignature(Identifier); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2656 | NewTU.setTypeSignature(Signature); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2657 | Ins.first->second = Signature; |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2658 | |
Paul Robinson | 5b302bf | 2018-11-12 16:55:11 +0000 | [diff] [blame] | 2659 | if (useSplitDwarf()) { |
| 2660 | MCSection *Section = |
| 2661 | getDwarfVersion() <= 4 |
| 2662 | ? Asm->getObjFileLowering().getDwarfTypesDWOSection() |
| 2663 | : Asm->getObjFileLowering().getDwarfInfoDWOSection(); |
| 2664 | NewTU.setSection(Section); |
| 2665 | } else { |
Paul Robinson | ddbde9a | 2018-11-09 19:06:09 +0000 | [diff] [blame] | 2666 | MCSection *Section = |
| 2667 | getDwarfVersion() <= 4 |
| 2668 | ? Asm->getObjFileLowering().getDwarfTypesSection(Signature) |
| 2669 | : Asm->getObjFileLowering().getDwarfInfoSection(Signature); |
| 2670 | NewTU.setSection(Section); |
Paul Robinson | 7cb26ad | 2018-03-27 21:28:59 +0000 | [diff] [blame] | 2671 | // Non-split type units reuse the compile unit's line table. |
| 2672 | CU.applyStmtList(UnitDie); |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2673 | } |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2674 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2675 | // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type |
| 2676 | // units. |
| 2677 | if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) |
| 2678 | NewTU.addStringOffsetsStart(); |
| 2679 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2680 | NewTU.setType(NewTU.createTypeDIE(CTy)); |
| 2681 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2682 | if (TopLevelType) { |
| 2683 | auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); |
| 2684 | TypeUnitsUnderConstruction.clear(); |
| 2685 | |
| 2686 | // Types referencing entries in the address table cannot be placed in type |
| 2687 | // units. |
| 2688 | if (AddrPool.hasBeenUsed()) { |
| 2689 | |
| 2690 | // Remove all the types built while building this type. |
| 2691 | // This is pessimistic as some of these types might not be dependent on |
| 2692 | // the type that used an address. |
| 2693 | for (const auto &TU : TypeUnitsToAdd) |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2694 | TypeSignatures.erase(TU.second); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2695 | |
| 2696 | // Construct this type in the CU directly. |
| 2697 | // This is inefficient because all the dependent types will be rebuilt |
| 2698 | // from scratch, including building them in type units, discovering that |
| 2699 | // 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] | 2700 | CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | } |
| 2703 | |
| 2704 | // If the type wasn't dependent on fission addresses, finish adding the type |
| 2705 | // and all its dependent types. |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2706 | for (auto &TU : TypeUnitsToAdd) { |
| 2707 | InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); |
| 2708 | InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); |
| 2709 | } |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2710 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2711 | CU.addDIETypeSignature(RefDie, Signature); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2712 | } |
David Blaikie | 4bd13b7 | 2014-03-07 18:49:45 +0000 | [diff] [blame] | 2713 | |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2714 | // Add the Name along with its companion DIE to the appropriate accelerator |
| 2715 | // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for |
| 2716 | // AccelTableKind::Apple, we use the table we got as an argument). If |
| 2717 | // accelerator tables are disabled, this function does nothing. |
| 2718 | template <typename DataT> |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2719 | void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU, |
| 2720 | AccelTable<DataT> &AppleAccel, StringRef Name, |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2721 | const DIE &Die) { |
| 2722 | if (getAccelTableKind() == AccelTableKind::None) |
| 2723 | return; |
Pavel Labath | 3fb39c7 | 2018-04-18 12:11:59 +0000 | [diff] [blame] | 2724 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2725 | if (getAccelTableKind() != AccelTableKind::Apple && |
| 2726 | CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None) |
| 2727 | return; |
| 2728 | |
Pavel Labath | 3fb39c7 | 2018-04-18 12:11:59 +0000 | [diff] [blame] | 2729 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Pavel Labath | 2f08811 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 2730 | DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name); |
Pavel Labath | 3fb39c7 | 2018-04-18 12:11:59 +0000 | [diff] [blame] | 2731 | |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 2732 | switch (getAccelTableKind()) { |
| 2733 | case AccelTableKind::Apple: |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2734 | AppleAccel.addName(Ref, Die); |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 2735 | break; |
| 2736 | case AccelTableKind::Dwarf: |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2737 | AccelDebugNames.addName(Ref, Die); |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 2738 | break; |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 2739 | case AccelTableKind::Default: |
| 2740 | llvm_unreachable("Default should have already been resolved."); |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2741 | case AccelTableKind::None: |
| 2742 | llvm_unreachable("None handled above"); |
Pavel Labath | 6088c23 | 2018-04-04 14:42:14 +0000 | [diff] [blame] | 2743 | } |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2744 | } |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2745 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2746 | void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name, |
| 2747 | const DIE &Die) { |
| 2748 | addAccelNameImpl(CU, AccelNames, Name, Die); |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2751 | void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name, |
| 2752 | const DIE &Die) { |
Pavel Labath | 7f7e606 | 2018-07-20 15:24:13 +0000 | [diff] [blame] | 2753 | // ObjC names go only into the Apple accelerator tables. |
| 2754 | if (getAccelTableKind() == AccelTableKind::Apple) |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2755 | addAccelNameImpl(CU, AccelObjC, Name, Die); |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2756 | } |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2757 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2758 | void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name, |
| 2759 | const DIE &Die) { |
| 2760 | addAccelNameImpl(CU, AccelNamespace, Name, Die); |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2761 | } |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2762 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 2763 | void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name, |
| 2764 | const DIE &Die, char Flags) { |
| 2765 | addAccelNameImpl(CU, AccelTypes, Name, Die); |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2766 | } |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 2767 | |
| 2768 | uint16_t DwarfDebug::getDwarfVersion() const { |
| 2769 | return Asm->OutStreamer->getContext().getDwarfVersion(); |
| 2770 | } |
David Blaikie | 60fddac | 2018-10-24 23:36:29 +0000 | [diff] [blame] | 2771 | |
| 2772 | void DwarfDebug::addSectionLabel(const MCSymbol *Sym) { |
| 2773 | SectionLabels.insert(std::make_pair(&Sym->getSection(), Sym)); |
| 2774 | } |
| 2775 | |
| 2776 | const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) { |
| 2777 | return SectionLabels.find(S)->second; |
| 2778 | } |