1 #include "DwarfCompileUnit.h"
3 #include "llvm/IR/DataLayout.h"
4 #include "llvm/IR/GlobalValue.h"
5 #include "llvm/IR/GlobalVariable.h"
6 #include "llvm/IR/Instruction.h"
7 #include "llvm/MC/MCAsmInfo.h"
8 #include "llvm/MC/MCStreamer.h"
9 #include "llvm/Target/TargetLoweringObjectFile.h"
13 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
14 AsmPrinter *A, DwarfDebug *DW,
16 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
17 insertDIE(Node, &getUnitDie());
20 /// addLabelAddress - Add a dwarf label attribute data and value using
21 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
23 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
24 const MCSymbol *Label) {
26 // Don't use the address pool in non-fission or in the skeleton unit itself.
27 // FIXME: Once GDB supports this, it's probably worthwhile using the address
28 // pool from the skeleton - maybe even in non-fission (possibly fewer
29 // relocations by sharing them in the pool, but we have other ideas about how
30 // to reduce the number of relocations as well/instead).
31 if (!DD->useSplitDwarf() || !Skeleton)
32 return addLocalLabelAddress(Die, Attribute, Label);
35 DD->addArangeLabel(SymbolCU(this, Label));
37 unsigned idx = DD->getAddressPool().getIndex(Label);
38 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
39 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
42 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
43 dwarf::Attribute Attribute,
44 const MCSymbol *Label) {
46 DD->addArangeLabel(SymbolCU(this, Label));
48 Die.addValue(Attribute, dwarf::DW_FORM_addr,
49 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
50 : new (DIEValueAllocator) DIEInteger(0));
53 unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
54 // If we print assembly, we can't separate .file entries according to
55 // compile units. Thus all files will belong to the default compile unit.
57 // FIXME: add a better feature test than hasRawTextSupport. Even better,
58 // extend .file to support this.
59 return Asm->OutStreamer.EmitDwarfFileDirective(
61 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
64 // Return const expression if value is a GEP to access merged global
66 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
67 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
68 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
69 if (!CE || CE->getNumOperands() != 3 ||
70 CE->getOpcode() != Instruction::GetElementPtr)
73 // First operand points to a global struct.
74 Value *Ptr = CE->getOperand(0);
75 if (!isa<GlobalValue>(Ptr) ||
76 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
79 // Second operand is zero.
80 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
81 if (!CI || !CI->isZero())
84 // Third operand is offset.
85 if (!isa<ConstantInt>(CE->getOperand(2)))
91 /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
92 DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) {
93 // Check for pre-existence.
94 if (DIE *Die = getDIE(GV))
97 assert(GV.isGlobalVariable());
99 DIScope GVContext = GV.getContext();
100 DIType GTy = DD->resolve(GV.getType());
102 // If this is a static data member definition, some attributes belong
103 // to the declaration DIE.
104 DIE *VariableDIE = nullptr;
105 bool IsStaticMember = false;
106 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
107 if (SDMDecl.Verify()) {
108 assert(SDMDecl.isStaticMember() && "Expected static member decl");
109 // We need the declaration DIE that is in the static member's class.
110 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
111 IsStaticMember = true;
114 // If this is not a static data member definition, create the variable
115 // DIE and add the initial set of attributes to it.
117 // Construct the context before querying for the existence of the DIE in
118 // case such construction creates the DIE.
119 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
122 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
124 // Add name and type.
125 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
126 addType(*VariableDIE, GTy);
129 if (!GV.isLocalToUnit())
130 addFlag(*VariableDIE, dwarf::DW_AT_external);
132 // Add line number info.
133 addSourceLine(*VariableDIE, GV);
137 bool addToAccelTable = false;
138 DIE *VariableSpecDIE = nullptr;
139 bool isGlobalVariable = GV.getGlobal() != nullptr;
140 if (isGlobalVariable) {
141 addToAccelTable = true;
142 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
143 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
144 if (GV.getGlobal()->isThreadLocal()) {
145 // FIXME: Make this work with -gsplit-dwarf.
146 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
147 assert((PointerSize == 4 || PointerSize == 8) &&
148 "Add support for other sizes if necessary");
149 // Based on GCC's support for TLS:
150 if (!DD->useSplitDwarf()) {
151 // 1) Start with a constNu of the appropriate pointer size
152 addUInt(*Loc, dwarf::DW_FORM_data1,
153 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
154 // 2) containing the (relocated) offset of the TLS variable
155 // within the module's TLS block.
156 addExpr(*Loc, dwarf::DW_FORM_udata,
157 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
159 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
160 addUInt(*Loc, dwarf::DW_FORM_udata,
161 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
163 // 3) followed by a custom OP to make the debugger do a TLS lookup.
164 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
166 DD->addArangeLabel(SymbolCU(this, Sym));
167 addOpAddress(*Loc, Sym);
169 // A static member's declaration is already flagged as such.
170 if (!SDMDecl.Verify() && !GV.isDefinition())
171 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
172 // Do not create specification DIE if context is either compile unit
174 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
175 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
176 // Create specification DIE.
177 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie);
178 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE);
179 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
180 // A static member's declaration is already flagged as such.
181 if (!SDMDecl.Verify())
182 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
184 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
186 // Add the linkage name.
187 StringRef LinkageName = GV.getLinkageName();
188 if (!LinkageName.empty())
189 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
190 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
192 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
194 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
195 : dwarf::DW_AT_MIPS_linkage_name,
196 GlobalValue::getRealLinkageName(LinkageName));
197 } else if (const ConstantInt *CI =
198 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
199 // AT_const_value was added when the static member was created. To avoid
200 // emitting AT_const_value multiple times, we only add AT_const_value when
201 // it is not a static member.
203 addConstantValue(*VariableDIE, CI, GTy);
204 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) {
205 addToAccelTable = true;
206 // GV is a merged global.
207 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
208 Value *Ptr = CE->getOperand(0);
209 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
210 DD->addArangeLabel(SymbolCU(this, Sym));
211 addOpAddress(*Loc, Sym);
212 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
213 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
214 addUInt(*Loc, dwarf::DW_FORM_udata,
215 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
216 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
217 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
220 DIE *ResultDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
222 if (addToAccelTable) {
223 DD->addAccelName(GV.getName(), *ResultDIE);
225 // If the linkage name is different than the name, go ahead and output
226 // that as well into the name table.
227 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
228 DD->addAccelName(GV.getLinkageName(), *ResultDIE);
231 addGlobalName(GV.getName(), *ResultDIE, GV.getContext());
235 void DwarfCompileUnit::addRange(RangeSpan Range) {
236 bool SameAsPrevCU = this == DD->getPrevCU();
238 // If we have no current ranges just add the range and return, otherwise,
239 // check the current section and CU against the previous section and CU we
240 // emitted into and the subprogram was contained within. If these are the
241 // same then extend our current range, otherwise add this as a new range.
242 if (CURanges.empty() || !SameAsPrevCU ||
243 (&CURanges.back().getEnd()->getSection() !=
244 &Range.getEnd()->getSection())) {
245 CURanges.push_back(Range);
249 CURanges.back().setEnd(Range.getEnd());
252 void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
253 // Define start line table label for each Compile Unit.
254 MCSymbol *LineTableStartSym =
255 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
257 stmtListIndex = UnitDie.getValues().size();
259 // DW_AT_stmt_list is a offset of line number information for this
260 // compile unit in debug_line section. For split dwarf this is
261 // left in the skeleton CU and so not included.
262 // The line table entries are not always emitted in assembly, so it
263 // is not okay to use line_table_start here.
264 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
265 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym);
267 addSectionDelta(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
268 DwarfLineSectionSym);
271 void DwarfCompileUnit::applyStmtList(DIE &D) {
272 D.addValue(dwarf::DW_AT_stmt_list,
273 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
274 UnitDie.getValues()[stmtListIndex]);
277 } // end llvm namespace