Home | History | Annotate | Download | only in AsmPrinter
      1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "DwarfFile.h"
     11 
     12 #include "DwarfDebug.h"
     13 #include "DwarfUnit.h"
     14 #include "llvm/MC/MCStreamer.h"
     15 #include "llvm/Support/LEB128.h"
     16 #include "llvm/IR/DataLayout.h"
     17 #include "llvm/ADT/STLExtras.h"
     18 #include "llvm/Target/TargetLoweringObjectFile.h"
     19 
     20 namespace llvm {
     21 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
     22     : Asm(AP), StrPool(DA, *Asm, Pref) {}
     23 
     24 DwarfFile::~DwarfFile() {}
     25 
     26 // Define a unique number for the abbreviation.
     27 //
     28 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
     29   // Check the set for priors.
     30   DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
     31 
     32   // If it's newly added.
     33   if (InSet == &Abbrev) {
     34     // Add to abbreviation list.
     35     Abbreviations.push_back(&Abbrev);
     36 
     37     // Assign the vector position + 1 as its number.
     38     Abbrev.setNumber(Abbreviations.size());
     39   } else {
     40     // Assign existing abbreviation number.
     41     Abbrev.setNumber(InSet->getNumber());
     42   }
     43 }
     44 
     45 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
     46   CUs.push_back(std::move(U));
     47 }
     48 
     49 // Emit the various dwarf units to the unit section USection with
     50 // the abbreviations going into ASection.
     51 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
     52   for (const auto &TheU : CUs) {
     53     DIE &Die = TheU->getUnitDie();
     54     const MCSection *USection = TheU->getSection();
     55     Asm->OutStreamer.SwitchSection(USection);
     56 
     57     // Emit the compile units header.
     58     Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
     59 
     60     // Emit size of content not including length itself
     61     Asm->OutStreamer.AddComment("Length of Unit");
     62     Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
     63 
     64     TheU->emitHeader(ASectionSym);
     65 
     66     DD->emitDIE(Die);
     67     Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
     68   }
     69 }
     70 // Compute the size and offset for each DIE.
     71 void DwarfFile::computeSizeAndOffsets() {
     72   // Offset from the first CU in the debug info section is 0 initially.
     73   unsigned SecOffset = 0;
     74 
     75   // Iterate over each compile unit and set the size and offsets for each
     76   // DIE within each compile unit. All offsets are CU relative.
     77   for (const auto &TheU : CUs) {
     78     TheU->setDebugInfoOffset(SecOffset);
     79 
     80     // CU-relative offset is reset to 0 here.
     81     unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
     82                       TheU->getHeaderSize(); // Unit-specific headers
     83 
     84     // EndOffset here is CU-relative, after laying out
     85     // all of the CU DIE.
     86     unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
     87     SecOffset += EndOffset;
     88   }
     89 }
     90 // Compute the size and offset of a DIE. The offset is relative to start of the
     91 // CU. It returns the offset after laying out the DIE.
     92 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
     93   // Record the abbreviation.
     94   assignAbbrevNumber(Die.getAbbrev());
     95 
     96   // Get the abbreviation for this DIE.
     97   const DIEAbbrev &Abbrev = Die.getAbbrev();
     98 
     99   // Set DIE offset
    100   Die.setOffset(Offset);
    101 
    102   // Start the size with the size of abbreviation code.
    103   Offset += getULEB128Size(Die.getAbbrevNumber());
    104 
    105   const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
    106   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
    107 
    108   // Size the DIE attribute values.
    109   for (unsigned i = 0, N = Values.size(); i < N; ++i)
    110     // Size attribute value.
    111     Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
    112 
    113   // Get the children.
    114   const auto &Children = Die.getChildren();
    115 
    116   // Size the DIE children if any.
    117   if (!Children.empty()) {
    118     assert(Abbrev.hasChildren() && "Children flag not set");
    119 
    120     for (auto &Child : Children)
    121       Offset = computeSizeAndOffset(*Child, Offset);
    122 
    123     // End of children marker.
    124     Offset += sizeof(int8_t);
    125   }
    126 
    127   Die.setSize(Offset - Die.getOffset());
    128   return Offset;
    129 }
    130 void DwarfFile::emitAbbrevs(const MCSection *Section) {
    131   // Check to see if it is worth the effort.
    132   if (!Abbreviations.empty()) {
    133     // Start the debug abbrev section.
    134     Asm->OutStreamer.SwitchSection(Section);
    135 
    136     // For each abbrevation.
    137     for (const DIEAbbrev *Abbrev : Abbreviations) {
    138       // Emit the abbrevations code (base 1 index.)
    139       Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
    140 
    141       // Emit the abbreviations data.
    142       Abbrev->Emit(Asm);
    143     }
    144 
    145     // Mark end of abbreviations.
    146     Asm->EmitULEB128(0, "EOM(3)");
    147   }
    148 }
    149 
    150 // Emit strings into a string section.
    151 void DwarfFile::emitStrings(const MCSection *StrSection,
    152                             const MCSection *OffsetSection,
    153                             const MCSymbol *StrSecSym) {
    154   StrPool.emit(*Asm, StrSection, OffsetSection, StrSecSym);
    155 }
    156 }
    157