[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / CodeGen / AsmPrinter / DebugHandlerBase.cpp
blob18fc46c74eb47783bef835969d3cda1ec6211186
1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common functionality for different debug information format backends.
10 // LLVM currently supports DWARF and CodeView.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DebugHandlerBase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/Support/CommandLine.h"
26 using namespace llvm;
28 #define DEBUG_TYPE "dwarfdebug"
30 /// If true, we drop variable location ranges which exist entirely outside the
31 /// variable's lexical scope instruction ranges.
32 static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
34 Optional<DbgVariableLocation>
35 DbgVariableLocation::extractFromMachineInstruction(
36 const MachineInstr &Instruction) {
37 DbgVariableLocation Location;
38 // Variables calculated from multiple locations can't be represented here.
39 if (Instruction.getNumDebugOperands() != 1)
40 return None;
41 if (!Instruction.getDebugOperand(0).isReg())
42 return None;
43 Location.Register = Instruction.getDebugOperand(0).getReg();
44 Location.FragmentInfo.reset();
45 // We only handle expressions generated by DIExpression::appendOffset,
46 // which doesn't require a full stack machine.
47 int64_t Offset = 0;
48 const DIExpression *DIExpr = Instruction.getDebugExpression();
49 auto Op = DIExpr->expr_op_begin();
50 // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
51 // appears exactly once at the start of the expression.
52 if (Instruction.isDebugValueList()) {
53 if (Instruction.getNumDebugOperands() == 1 &&
54 Op->getOp() == dwarf::DW_OP_LLVM_arg)
55 ++Op;
56 else
57 return None;
59 while (Op != DIExpr->expr_op_end()) {
60 switch (Op->getOp()) {
61 case dwarf::DW_OP_constu: {
62 int Value = Op->getArg(0);
63 ++Op;
64 if (Op != DIExpr->expr_op_end()) {
65 switch (Op->getOp()) {
66 case dwarf::DW_OP_minus:
67 Offset -= Value;
68 break;
69 case dwarf::DW_OP_plus:
70 Offset += Value;
71 break;
72 default:
73 continue;
76 } break;
77 case dwarf::DW_OP_plus_uconst:
78 Offset += Op->getArg(0);
79 break;
80 case dwarf::DW_OP_LLVM_fragment:
81 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
82 break;
83 case dwarf::DW_OP_deref:
84 Location.LoadChain.push_back(Offset);
85 Offset = 0;
86 break;
87 default:
88 return None;
90 ++Op;
93 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
94 // instruction.
95 // FIXME: Replace these with DIExpression.
96 if (Instruction.isIndirectDebugValue())
97 Location.LoadChain.push_back(Offset);
99 return Location;
102 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
104 void DebugHandlerBase::beginModule(Module *M) {
105 if (M->debug_compile_units().empty())
106 Asm = nullptr;
109 // Each LexicalScope has first instruction and last instruction to mark
110 // beginning and end of a scope respectively. Create an inverse map that list
111 // scopes starts (and ends) with an instruction. One instruction may start (or
112 // end) multiple scopes. Ignore scopes that are not reachable.
113 void DebugHandlerBase::identifyScopeMarkers() {
114 SmallVector<LexicalScope *, 4> WorkList;
115 WorkList.push_back(LScopes.getCurrentFunctionScope());
116 while (!WorkList.empty()) {
117 LexicalScope *S = WorkList.pop_back_val();
119 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
120 if (!Children.empty())
121 WorkList.append(Children.begin(), Children.end());
123 if (S->isAbstractScope())
124 continue;
126 for (const InsnRange &R : S->getRanges()) {
127 assert(R.first && "InsnRange does not have first instruction!");
128 assert(R.second && "InsnRange does not have second instruction!");
129 requestLabelBeforeInsn(R.first);
130 requestLabelAfterInsn(R.second);
135 // Return Label preceding the instruction.
136 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
137 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
138 assert(Label && "Didn't insert label before instruction");
139 return Label;
142 // Return Label immediately following the instruction.
143 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
144 return LabelsAfterInsn.lookup(MI);
147 /// If this type is derived from a base type then return base type size.
148 uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
149 assert(Ty);
150 const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
151 if (!DDTy)
152 return Ty->getSizeInBits();
154 unsigned Tag = DDTy->getTag();
156 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
157 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
158 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
159 Tag != dwarf::DW_TAG_immutable_type)
160 return DDTy->getSizeInBits();
162 DIType *BaseType = DDTy->getBaseType();
164 if (!BaseType)
165 return 0;
167 // If this is a derived type, go ahead and get the base type, unless it's a
168 // reference then it's just the size of the field. Pointer types have no need
169 // of this since they're a different type of qualification on the type.
170 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
171 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
172 return Ty->getSizeInBits();
174 return getBaseTypeSize(BaseType);
177 bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
178 if (isa<DIStringType>(Ty)) {
179 // Some transformations (e.g. instcombine) may decide to turn a Fortran
180 // character object into an integer, and later ones (e.g. SROA) may
181 // further inject a constant integer in a llvm.dbg.value call to track
182 // the object's value. Here we trust the transformations are doing the
183 // right thing, and treat the constant as unsigned to preserve that value
184 // (i.e. avoid sign extension).
185 return true;
188 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
189 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
190 if (!(Ty = CTy->getBaseType()))
191 // FIXME: Enums without a fixed underlying type have unknown signedness
192 // here, leading to incorrectly emitted constants.
193 return false;
194 } else
195 // (Pieces of) aggregate types that get hacked apart by SROA may be
196 // represented by a constant. Encode them as unsigned bytes.
197 return true;
200 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
201 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
202 // Encode pointer constants as unsigned bytes. This is used at least for
203 // null pointer constant emission.
204 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
205 // here, but accept them for now due to a bug in SROA producing bogus
206 // dbg.values.
207 if (T == dwarf::DW_TAG_pointer_type ||
208 T == dwarf::DW_TAG_ptr_to_member_type ||
209 T == dwarf::DW_TAG_reference_type ||
210 T == dwarf::DW_TAG_rvalue_reference_type)
211 return true;
212 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
213 T == dwarf::DW_TAG_volatile_type ||
214 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
215 T == dwarf::DW_TAG_immutable_type);
216 assert(DTy->getBaseType() && "Expected valid base type");
217 return isUnsignedDIType(DTy->getBaseType());
220 auto *BTy = cast<DIBasicType>(Ty);
221 unsigned Encoding = BTy->getEncoding();
222 assert((Encoding == dwarf::DW_ATE_unsigned ||
223 Encoding == dwarf::DW_ATE_unsigned_char ||
224 Encoding == dwarf::DW_ATE_signed ||
225 Encoding == dwarf::DW_ATE_signed_char ||
226 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
227 Encoding == dwarf::DW_ATE_boolean ||
228 (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
229 Ty->getName() == "decltype(nullptr)")) &&
230 "Unsupported encoding");
231 return Encoding == dwarf::DW_ATE_unsigned ||
232 Encoding == dwarf::DW_ATE_unsigned_char ||
233 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
234 Ty->getTag() == dwarf::DW_TAG_unspecified_type;
237 static bool hasDebugInfo(const MachineModuleInfo *MMI,
238 const MachineFunction *MF) {
239 if (!MMI->hasDebugInfo())
240 return false;
241 auto *SP = MF->getFunction().getSubprogram();
242 if (!SP)
243 return false;
244 assert(SP->getUnit());
245 auto EK = SP->getUnit()->getEmissionKind();
246 if (EK == DICompileUnit::NoDebug)
247 return false;
248 return true;
251 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
252 PrevInstBB = nullptr;
254 if (!Asm || !hasDebugInfo(MMI, MF)) {
255 skippedNonDebugFunction();
256 return;
259 // Grab the lexical scopes for the function, if we don't have any of those
260 // then we're not going to be able to do anything.
261 LScopes.initialize(*MF);
262 if (LScopes.empty()) {
263 beginFunctionImpl(MF);
264 return;
267 // Make sure that each lexical scope will have a begin/end label.
268 identifyScopeMarkers();
270 // Calculate history for local variables.
271 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
272 assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
273 calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
274 DbgValues, DbgLabels);
275 InstOrdering.initialize(*MF);
276 if (TrimVarLocs)
277 DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
278 LLVM_DEBUG(DbgValues.dump());
280 // Request labels for the full history.
281 for (const auto &I : DbgValues) {
282 const auto &Entries = I.second;
283 if (Entries.empty())
284 continue;
286 auto IsDescribedByReg = [](const MachineInstr *MI) {
287 return any_of(MI->debug_operands(),
288 [](auto &MO) { return MO.isReg() && MO.getReg(); });
291 // The first mention of a function argument gets the CurrentFnBegin label,
292 // so arguments are visible when breaking at function entry.
294 // We do not change the label for values that are described by registers,
295 // as that could place them above their defining instructions. We should
296 // ideally not change the labels for constant debug values either, since
297 // doing that violates the ranges that are calculated in the history map.
298 // However, we currently do not emit debug values for constant arguments
299 // directly at the start of the function, so this code is still useful.
300 const DILocalVariable *DIVar =
301 Entries.front().getInstr()->getDebugVariable();
302 if (DIVar->isParameter() &&
303 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
304 if (!IsDescribedByReg(Entries.front().getInstr()))
305 LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
306 if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
307 // Mark all non-overlapping initial fragments.
308 for (auto I = Entries.begin(); I != Entries.end(); ++I) {
309 if (!I->isDbgValue())
310 continue;
311 const DIExpression *Fragment = I->getInstr()->getDebugExpression();
312 if (std::any_of(Entries.begin(), I,
313 [&](DbgValueHistoryMap::Entry Pred) {
314 return Pred.isDbgValue() &&
315 Fragment->fragmentsOverlap(
316 Pred.getInstr()->getDebugExpression());
318 break;
319 // The code that generates location lists for DWARF assumes that the
320 // entries' start labels are monotonically increasing, and since we
321 // don't change the label for fragments that are described by
322 // registers, we must bail out when encountering such a fragment.
323 if (IsDescribedByReg(I->getInstr()))
324 break;
325 LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
330 for (const auto &Entry : Entries) {
331 if (Entry.isDbgValue())
332 requestLabelBeforeInsn(Entry.getInstr());
333 else
334 requestLabelAfterInsn(Entry.getInstr());
338 // Ensure there is a symbol before DBG_LABEL.
339 for (const auto &I : DbgLabels) {
340 const MachineInstr *MI = I.second;
341 requestLabelBeforeInsn(MI);
344 PrevInstLoc = DebugLoc();
345 PrevLabel = Asm->getFunctionBegin();
346 beginFunctionImpl(MF);
349 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
350 if (!Asm || !MMI->hasDebugInfo())
351 return;
353 assert(CurMI == nullptr);
354 CurMI = MI;
356 // Insert labels where requested.
357 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
358 LabelsBeforeInsn.find(MI);
360 // No label needed.
361 if (I == LabelsBeforeInsn.end())
362 return;
364 // Label already assigned.
365 if (I->second)
366 return;
368 if (!PrevLabel) {
369 PrevLabel = MMI->getContext().createTempSymbol();
370 Asm->OutStreamer->emitLabel(PrevLabel);
372 I->second = PrevLabel;
375 void DebugHandlerBase::endInstruction() {
376 if (!Asm || !MMI->hasDebugInfo())
377 return;
379 assert(CurMI != nullptr);
380 // Don't create a new label after DBG_VALUE and other instructions that don't
381 // generate code.
382 if (!CurMI->isMetaInstruction()) {
383 PrevLabel = nullptr;
384 PrevInstBB = CurMI->getParent();
387 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
388 LabelsAfterInsn.find(CurMI);
390 // No label needed or label already assigned.
391 if (I == LabelsAfterInsn.end() || I->second) {
392 CurMI = nullptr;
393 return;
396 // We need a label after this instruction. With basic block sections, just
397 // use the end symbol of the section if this is the last instruction of the
398 // section. This reduces the need for an additional label and also helps
399 // merging ranges.
400 if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
401 PrevLabel = CurMI->getParent()->getEndSymbol();
402 } else if (!PrevLabel) {
403 PrevLabel = MMI->getContext().createTempSymbol();
404 Asm->OutStreamer->emitLabel(PrevLabel);
406 I->second = PrevLabel;
407 CurMI = nullptr;
410 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
411 if (Asm && hasDebugInfo(MMI, MF))
412 endFunctionImpl(MF);
413 DbgValues.clear();
414 DbgLabels.clear();
415 LabelsBeforeInsn.clear();
416 LabelsAfterInsn.clear();
417 InstOrdering.clear();
420 void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
421 if (!MBB.isBeginSection())
422 return;
424 PrevLabel = MBB.getSymbol();
427 void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
428 if (!MBB.isEndSection())
429 return;
431 PrevLabel = nullptr;