2014-04-24 02:54:00 +08:00
|
|
|
//===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DwarfFile.h"
|
|
|
|
#include "DwarfDebug.h"
|
|
|
|
#include "DwarfUnit.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-04-24 02:54:00 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Support/LEB128.h"
|
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
2015-03-04 10:30:17 +08:00
|
|
|
DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
|
|
|
|
: Asm(AP), StrPool(DA, *Asm, Pref) {}
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2015-05-28 20:55:43 +08:00
|
|
|
DwarfFile::~DwarfFile() {
|
|
|
|
for (DIEAbbrev *Abbrev : Abbreviations)
|
|
|
|
Abbrev->~DIEAbbrev();
|
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
// Define a unique number for the abbreviation.
|
|
|
|
//
|
2015-05-28 06:31:41 +08:00
|
|
|
DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
DIEAbbrev Abbrev = Die.generateAbbrev();
|
|
|
|
Abbrev.Profile(ID);
|
|
|
|
|
|
|
|
void *InsertPos;
|
|
|
|
if (DIEAbbrev *Existing =
|
|
|
|
AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
|
|
|
|
Die.setAbbrevNumber(Existing->getNumber());
|
|
|
|
return *Existing;
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|
2015-05-28 06:31:41 +08:00
|
|
|
|
|
|
|
// Move the abbreviation to the heap and assign a number.
|
|
|
|
DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
|
|
|
|
Abbreviations.push_back(New);
|
|
|
|
New->setNumber(Abbreviations.size());
|
|
|
|
Die.setAbbrevNumber(Abbreviations.size());
|
|
|
|
|
|
|
|
// Store it for lookup.
|
|
|
|
AbbreviationsSet.InsertNode(New, InsertPos);
|
|
|
|
return *New;
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
|
|
|
|
CUs.push_back(std::move(U));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the various dwarf units to the unit section USection with
|
|
|
|
// the abbreviations going into ASection.
|
2015-03-11 00:58:10 +08:00
|
|
|
void DwarfFile::emitUnits(bool UseOffsets) {
|
2014-04-24 02:54:00 +08:00
|
|
|
for (const auto &TheU : CUs) {
|
2014-04-26 02:35:57 +08:00
|
|
|
DIE &Die = TheU->getUnitDie();
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSection *USection = TheU->getSection();
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->SwitchSection(USection);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2015-03-11 00:58:10 +08:00
|
|
|
TheU->emitHeader(UseOffsets);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2015-03-04 10:30:17 +08:00
|
|
|
Asm->emitDwarfDIE(Die);
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-23 08:16:05 +08:00
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
// Compute the size and offset for each DIE.
|
|
|
|
void DwarfFile::computeSizeAndOffsets() {
|
|
|
|
// Offset from the first CU in the debug info section is 0 initially.
|
|
|
|
unsigned SecOffset = 0;
|
|
|
|
|
|
|
|
// Iterate over each compile unit and set the size and offsets for each
|
|
|
|
// DIE within each compile unit. All offsets are CU relative.
|
|
|
|
for (const auto &TheU : CUs) {
|
|
|
|
TheU->setDebugInfoOffset(SecOffset);
|
|
|
|
|
|
|
|
// CU-relative offset is reset to 0 here.
|
|
|
|
unsigned Offset = sizeof(int32_t) + // Length of Unit Info
|
|
|
|
TheU->getHeaderSize(); // Unit-specific headers
|
|
|
|
|
|
|
|
// EndOffset here is CU-relative, after laying out
|
|
|
|
// all of the CU DIE.
|
2014-04-26 02:35:57 +08:00
|
|
|
unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
|
2014-04-24 02:54:00 +08:00
|
|
|
SecOffset += EndOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compute the size and offset of a DIE. The offset is relative to start of the
|
|
|
|
// CU. It returns the offset after laying out the DIE.
|
|
|
|
unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
|
|
|
|
// Record the abbreviation.
|
2015-05-28 06:31:41 +08:00
|
|
|
const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
// Set DIE offset
|
|
|
|
Die.setOffset(Offset);
|
|
|
|
|
|
|
|
// Start the size with the size of abbreviation code.
|
|
|
|
Offset += getULEB128Size(Die.getAbbrevNumber());
|
|
|
|
|
|
|
|
// Size the DIE attribute values.
|
2015-05-28 06:44:06 +08:00
|
|
|
for (const auto &V : Die.values())
|
2014-04-24 02:54:00 +08:00
|
|
|
// Size attribute value.
|
2015-05-28 06:44:06 +08:00
|
|
|
Offset += V.SizeOf(Asm, V.getForm());
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
// Size the DIE children if any.
|
2015-05-29 03:56:34 +08:00
|
|
|
if (Die.hasChildren()) {
|
2015-05-28 07:02:36 +08:00
|
|
|
(void)Abbrev;
|
2014-04-24 02:54:00 +08:00
|
|
|
assert(Abbrev.hasChildren() && "Children flag not set");
|
|
|
|
|
2015-05-29 03:56:34 +08:00
|
|
|
for (auto &Child : Die.children())
|
2014-04-24 02:54:00 +08:00
|
|
|
Offset = computeSizeAndOffset(*Child, Offset);
|
|
|
|
|
|
|
|
// End of children marker.
|
|
|
|
Offset += sizeof(int8_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
Die.setSize(Offset - Die.getOffset());
|
|
|
|
return Offset;
|
|
|
|
}
|
2015-03-04 10:30:17 +08:00
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
void DwarfFile::emitAbbrevs(MCSection *Section) {
|
2014-04-24 02:54:00 +08:00
|
|
|
// Check to see if it is worth the effort.
|
|
|
|
if (!Abbreviations.empty()) {
|
|
|
|
// Start the debug abbrev section.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->SwitchSection(Section);
|
2015-03-04 10:30:17 +08:00
|
|
|
Asm->emitDwarfAbbrevs(Abbreviations);
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit strings into a string section.
|
2015-05-22 03:20:38 +08:00
|
|
|
void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
|
2014-09-12 05:12:48 +08:00
|
|
|
StrPool.emit(*Asm, StrSection, OffsetSection);
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|
2014-10-23 08:16:05 +08:00
|
|
|
|
2015-02-11 07:18:28 +08:00
|
|
|
bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
|
2014-10-25 01:57:34 +08:00
|
|
|
SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
|
2015-04-30 00:38:44 +08:00
|
|
|
const DILocalVariable *DV = Var->getVariable();
|
2014-10-24 06:04:30 +08:00
|
|
|
// Variables with positive arg numbers are parameters.
|
2015-04-14 10:22:36 +08:00
|
|
|
if (unsigned ArgNum = DV->getArg()) {
|
2014-10-24 06:04:30 +08:00
|
|
|
// Keep all parameters in order at the start of the variable list to ensure
|
|
|
|
// function types are correct (no out-of-order parameters)
|
|
|
|
//
|
|
|
|
// This could be improved by only doing it for optimized builds (unoptimized
|
|
|
|
// builds have the right order to begin with), searching from the back (this
|
|
|
|
// would catch the unoptimized case quickly), or doing a binary search
|
|
|
|
// rather than linear search.
|
|
|
|
auto I = Vars.begin();
|
|
|
|
while (I != Vars.end()) {
|
2015-04-14 10:22:36 +08:00
|
|
|
unsigned CurNum = (*I)->getVariable()->getArg();
|
2014-10-24 06:04:30 +08:00
|
|
|
// A local (non-parameter) variable has been found, insert immediately
|
|
|
|
// before it.
|
|
|
|
if (CurNum == 0)
|
|
|
|
break;
|
|
|
|
// A later indexed parameter has been found, insert immediately before it.
|
|
|
|
if (CurNum > ArgNum)
|
|
|
|
break;
|
2015-02-11 07:18:28 +08:00
|
|
|
if (CurNum == ArgNum) {
|
|
|
|
(*I)->addMMIEntry(*Var);
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-24 06:04:30 +08:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
Vars.insert(I, Var);
|
2015-02-11 07:18:28 +08:00
|
|
|
return true;
|
2014-10-24 06:04:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vars.push_back(Var);
|
2015-02-11 07:18:28 +08:00
|
|
|
return true;
|
2014-10-24 06:04:30 +08:00
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
}
|