LLVM API Documentation

DwarfFile.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DwarfFile.h"
00011 
00012 #include "DwarfDebug.h"
00013 #include "DwarfUnit.h"
00014 #include "llvm/MC/MCStreamer.h"
00015 #include "llvm/Support/LEB128.h"
00016 #include "llvm/IR/DataLayout.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/Target/TargetLoweringObjectFile.h"
00019 
00020 namespace llvm {
00021 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
00022     : Asm(AP), StrPool(DA, *Asm, Pref) {}
00023 
00024 DwarfFile::~DwarfFile() {}
00025 
00026 // Define a unique number for the abbreviation.
00027 //
00028 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
00029   // Check the set for priors.
00030   DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
00031 
00032   // If it's newly added.
00033   if (InSet == &Abbrev) {
00034     // Add to abbreviation list.
00035     Abbreviations.push_back(&Abbrev);
00036 
00037     // Assign the vector position + 1 as its number.
00038     Abbrev.setNumber(Abbreviations.size());
00039   } else {
00040     // Assign existing abbreviation number.
00041     Abbrev.setNumber(InSet->getNumber());
00042   }
00043 }
00044 
00045 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
00046   CUs.push_back(std::move(U));
00047 }
00048 
00049 // Emit the various dwarf units to the unit section USection with
00050 // the abbreviations going into ASection.
00051 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
00052   for (const auto &TheU : CUs) {
00053     DIE &Die = TheU->getUnitDie();
00054     const MCSection *USection = TheU->getSection();
00055     Asm->OutStreamer.SwitchSection(USection);
00056 
00057     // Emit the compile units header.
00058     Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
00059 
00060     // Emit size of content not including length itself
00061     Asm->OutStreamer.AddComment("Length of Unit");
00062     Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
00063 
00064     TheU->emitHeader(ASectionSym);
00065 
00066     DD->emitDIE(Die);
00067     Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
00068   }
00069 }
00070 // Compute the size and offset for each DIE.
00071 void DwarfFile::computeSizeAndOffsets() {
00072   // Offset from the first CU in the debug info section is 0 initially.
00073   unsigned SecOffset = 0;
00074 
00075   // Iterate over each compile unit and set the size and offsets for each
00076   // DIE within each compile unit. All offsets are CU relative.
00077   for (const auto &TheU : CUs) {
00078     TheU->setDebugInfoOffset(SecOffset);
00079 
00080     // CU-relative offset is reset to 0 here.
00081     unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
00082                       TheU->getHeaderSize(); // Unit-specific headers
00083 
00084     // EndOffset here is CU-relative, after laying out
00085     // all of the CU DIE.
00086     unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
00087     SecOffset += EndOffset;
00088   }
00089 }
00090 // Compute the size and offset of a DIE. The offset is relative to start of the
00091 // CU. It returns the offset after laying out the DIE.
00092 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
00093   // Record the abbreviation.
00094   assignAbbrevNumber(Die.getAbbrev());
00095 
00096   // Get the abbreviation for this DIE.
00097   const DIEAbbrev &Abbrev = Die.getAbbrev();
00098 
00099   // Set DIE offset
00100   Die.setOffset(Offset);
00101 
00102   // Start the size with the size of abbreviation code.
00103   Offset += getULEB128Size(Die.getAbbrevNumber());
00104 
00105   const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
00106   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
00107 
00108   // Size the DIE attribute values.
00109   for (unsigned i = 0, N = Values.size(); i < N; ++i)
00110     // Size attribute value.
00111     Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
00112 
00113   // Get the children.
00114   const auto &Children = Die.getChildren();
00115 
00116   // Size the DIE children if any.
00117   if (!Children.empty()) {
00118     assert(Abbrev.hasChildren() && "Children flag not set");
00119 
00120     for (auto &Child : Children)
00121       Offset = computeSizeAndOffset(*Child, Offset);
00122 
00123     // End of children marker.
00124     Offset += sizeof(int8_t);
00125   }
00126 
00127   Die.setSize(Offset - Die.getOffset());
00128   return Offset;
00129 }
00130 void DwarfFile::emitAbbrevs(const MCSection *Section) {
00131   // Check to see if it is worth the effort.
00132   if (!Abbreviations.empty()) {
00133     // Start the debug abbrev section.
00134     Asm->OutStreamer.SwitchSection(Section);
00135 
00136     // For each abbrevation.
00137     for (const DIEAbbrev *Abbrev : Abbreviations) {
00138       // Emit the abbrevations code (base 1 index.)
00139       Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
00140 
00141       // Emit the abbreviations data.
00142       Abbrev->Emit(Asm);
00143     }
00144 
00145     // Mark end of abbreviations.
00146     Asm->EmitULEB128(0, "EOM(3)");
00147   }
00148 }
00149 
00150 // Emit strings into a string section.
00151 void DwarfFile::emitStrings(const MCSection *StrSection,
00152                             const MCSection *OffsetSection) {
00153   StrPool.emit(*Asm, StrSection, OffsetSection);
00154 }
00155 }