1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
//===- DWARFLinkerUnit.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DWARFLinkerUnit.h"
#include "DWARFEmitterImpl.h"
#include "DebugLineSectionEmitter.h"
using namespace llvm;
using namespace dwarf_linker;
using namespace dwarf_linker::parallel;
void DwarfUnit::assignAbbrev(DIEAbbrev &Abbrev) {
// Check the set for priors.
FoldingSetNodeID ID;
Abbrev.Profile(ID);
void *InsertToken;
DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
// If it's newly added.
if (InSet) {
// Assign existing abbreviation number.
Abbrev.setNumber(InSet->getNumber());
} else {
// Add to abbreviation list.
Abbreviations.push_back(
std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
for (const auto &Attr : Abbrev.getData())
Abbreviations.back()->AddAttribute(Attr);
AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
// Assign the unique abbreviation number.
Abbrev.setNumber(Abbreviations.size());
Abbreviations.back()->setNumber(Abbreviations.size());
}
}
Error DwarfUnit::emitAbbreviations() {
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs = getAbbreviations();
if (Abbrevs.empty())
return Error::success();
SectionDescriptor &AbbrevSection =
getOrCreateSectionDescriptor(DebugSectionKind::DebugAbbrev);
// For each abbreviation.
for (const auto &Abbrev : Abbrevs)
emitDwarfAbbrevEntry(*Abbrev, AbbrevSection);
// Mark end of abbreviations.
encodeULEB128(0, AbbrevSection.OS);
return Error::success();
}
void DwarfUnit::emitDwarfAbbrevEntry(const DIEAbbrev &Abbrev,
SectionDescriptor &AbbrevSection) {
// Emit the abbreviations code (base 1 index.)
encodeULEB128(Abbrev.getNumber(), AbbrevSection.OS);
// Emit the abbreviations data.
// Emit its Dwarf tag type.
encodeULEB128(Abbrev.getTag(), AbbrevSection.OS);
// Emit whether it has children DIEs.
encodeULEB128((unsigned)Abbrev.hasChildren(), AbbrevSection.OS);
// For each attribute description.
const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
for (unsigned i = 0, N = Data.size(); i < N; ++i) {
const DIEAbbrevData &AttrData = Data[i];
// Emit attribute type.
encodeULEB128(AttrData.getAttribute(), AbbrevSection.OS);
// Emit form type.
encodeULEB128(AttrData.getForm(), AbbrevSection.OS);
// Emit value for DW_FORM_implicit_const.
if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
encodeSLEB128(AttrData.getValue(), AbbrevSection.OS);
}
// Mark end of abbreviation.
encodeULEB128(0, AbbrevSection.OS);
encodeULEB128(0, AbbrevSection.OS);
}
Error DwarfUnit::emitDebugInfo(const Triple &TargetTriple) {
DIE *OutUnitDIE = getOutUnitDIE();
if (OutUnitDIE == nullptr)
return Error::success();
// FIXME: Remove dependence on DwarfEmitterImpl/AsmPrinter and emit DIEs
// directly.
SectionDescriptor &OutSection =
getOrCreateSectionDescriptor(DebugSectionKind::DebugInfo);
DwarfEmitterImpl Emitter(DWARFLinker::OutputFileType::Object, OutSection.OS);
if (Error Err = Emitter.init(TargetTriple, "__DWARF"))
return Err;
// Emit compile unit header.
Emitter.emitCompileUnitHeader(*this);
size_t OffsetToAbbreviationTableOffset =
(getFormParams().Version >= 5) ? 8 : 6;
OutSection.notePatch(DebugOffsetPatch{
OffsetToAbbreviationTableOffset,
&getOrCreateSectionDescriptor(DebugSectionKind::DebugAbbrev)});
// Emit DIEs.
Emitter.emitDIE(*OutUnitDIE);
Emitter.finish();
// Set start offset ans size for .debug_info section.
OutSection.setSizesForSectionCreatedByAsmPrinter();
return Error::success();
}
Error DwarfUnit::emitDebugLine(const Triple &TargetTriple,
const DWARFDebugLine::LineTable &OutLineTable) {
DebugLineSectionEmitter DebugLineEmitter(TargetTriple, *this);
return DebugLineEmitter.emit(OutLineTable);
}
Error DwarfUnit::emitDebugStringOffsetSection() {
if (getVersion() < 5)
return Error::success();
if (DebugStringIndexMap.empty())
return Error::success();
SectionDescriptor &OutDebugStrOffsetsSection =
getOrCreateSectionDescriptor(DebugSectionKind::DebugStrOffsets);
// Emit section header.
// Emit length.
OutDebugStrOffsetsSection.emitUnitLength(0xBADDEF);
uint64_t OffsetAfterSectionLength = OutDebugStrOffsetsSection.OS.tell();
// Emit version.
OutDebugStrOffsetsSection.emitIntVal(5, 2);
// Emit padding.
OutDebugStrOffsetsSection.emitIntVal(0, 2);
// Emit index to offset map.
for (const StringEntry *String : DebugStringIndexMap.getValues()) {
// Note patch for string offset value.
OutDebugStrOffsetsSection.notePatch(
DebugStrPatch{{OutDebugStrOffsetsSection.OS.tell()}, String});
// Emit placeholder for offset value.
OutDebugStrOffsetsSection.emitOffset(0xBADDEF);
}
// Patch section length.
OutDebugStrOffsetsSection.apply(
OffsetAfterSectionLength -
OutDebugStrOffsetsSection.getFormParams().getDwarfOffsetByteSize(),
dwarf::DW_FORM_sec_offset,
OutDebugStrOffsetsSection.OS.tell() - OffsetAfterSectionLength);
return Error::success();
}
/// Emit the pubnames or pubtypes section contribution for \p
/// Unit into \p Sec. The data is provided in \p Info.
std::optional<uint64_t>
DwarfUnit::emitPubAcceleratorEntry(SectionDescriptor &OutSection,
const DwarfUnit::AccelInfo &Info,
std::optional<uint64_t> LengthOffset) {
if (!LengthOffset) {
// Emit the header.
OutSection.emitIntVal(0xBADDEF,
getFormParams().getDwarfOffsetByteSize()); // Length
LengthOffset = OutSection.OS.tell();
OutSection.emitIntVal(dwarf::DW_PUBNAMES_VERSION, 2); // Version
OutSection.notePatch(DebugOffsetPatch{
OutSection.OS.tell(),
&getOrCreateSectionDescriptor(DebugSectionKind::DebugInfo)});
OutSection.emitOffset(0xBADDEF); // Unit offset
OutSection.emitIntVal(getUnitSize(), 4); // Size
}
OutSection.emitOffset(Info.OutOffset);
// Emit the string itself.
OutSection.emitInplaceString(Info.String->first());
return LengthOffset;
}
/// Emit .debug_pubnames and .debug_pubtypes for \p Unit.
void DwarfUnit::emitPubAccelerators() {
std::optional<uint64_t> NamesLengthOffset;
std::optional<uint64_t> TypesLengthOffset;
forEachAcceleratorRecord([&](const DwarfUnit::AccelInfo &Info) {
if (Info.AvoidForPubSections)
return;
switch (Info.Type) {
case DwarfUnit::AccelType::Name: {
NamesLengthOffset = emitPubAcceleratorEntry(
getOrCreateSectionDescriptor(DebugSectionKind::DebugPubNames), Info,
NamesLengthOffset);
} break;
case DwarfUnit::AccelType::Type: {
TypesLengthOffset = emitPubAcceleratorEntry(
getOrCreateSectionDescriptor(DebugSectionKind::DebugPubTypes), Info,
TypesLengthOffset);
} break;
default: {
// Nothing to do.
} break;
}
});
if (NamesLengthOffset) {
SectionDescriptor &OutSection =
getOrCreateSectionDescriptor(DebugSectionKind::DebugPubNames);
OutSection.emitIntVal(0, 4); // End marker.
OutSection.apply(*NamesLengthOffset -
OutSection.getFormParams().getDwarfOffsetByteSize(),
dwarf::DW_FORM_sec_offset,
OutSection.OS.tell() - *NamesLengthOffset);
}
if (TypesLengthOffset) {
SectionDescriptor &OutSection =
getOrCreateSectionDescriptor(DebugSectionKind::DebugPubTypes);
OutSection.emitIntVal(0, 4); // End marker.
OutSection.apply(*TypesLengthOffset -
OutSection.getFormParams().getDwarfOffsetByteSize(),
dwarf::DW_FORM_sec_offset,
OutSection.OS.tell() - *TypesLengthOffset);
}
}
|