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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
//===- MC/MCCASDebugV1.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 "llvm/MCCAS/MCCASDebugV1.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
using namespace llvm;
using namespace llvm::mccasformats;
using namespace llvm::mccasformats::v1;
Expected<uint64_t>
mccasformats::v1::getFormSize(dwarf::Form Form, dwarf::FormParams FP,
StringRef CUData, uint64_t CUOffset,
bool IsLittleEndian, uint8_t AddressSize) {
uint64_t FormSize = 0;
bool Indirect = false;
Error Err = Error::success();
do {
Indirect = false;
switch (Form) {
case dwarf::DW_FORM_addr:
case dwarf::DW_FORM_ref_addr: {
FormSize +=
(Form == dwarf::DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize();
break;
}
case dwarf::DW_FORM_exprloc:
case dwarf::DW_FORM_block: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
CUOffset += DWARFExtractor.getULEB128(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_block1: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
CUOffset += DWARFExtractor.getU8(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_block2: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
CUOffset += DWARFExtractor.getU16(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_block4: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
CUOffset += DWARFExtractor.getU32(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_implicit_const:
case dwarf::DW_FORM_flag_present: {
FormSize += 0;
break;
}
case dwarf::DW_FORM_data1:
case dwarf::DW_FORM_ref1:
case dwarf::DW_FORM_flag:
case dwarf::DW_FORM_strx1:
case dwarf::DW_FORM_addrx1: {
FormSize += 1;
break;
}
case dwarf::DW_FORM_data2:
case dwarf::DW_FORM_ref2:
case dwarf::DW_FORM_strx2:
case dwarf::DW_FORM_addrx2: {
FormSize += 2;
break;
}
case dwarf::DW_FORM_strx3: {
FormSize += 3;
break;
}
case dwarf::DW_FORM_data4:
case dwarf::DW_FORM_ref4:
case dwarf::DW_FORM_ref_sup4:
case dwarf::DW_FORM_strx4:
case dwarf::DW_FORM_addrx4: {
FormSize += 4;
break;
}
case dwarf::DW_FORM_ref_sig8:
case dwarf::DW_FORM_data8:
case dwarf::DW_FORM_ref8:
case dwarf::DW_FORM_ref_sup8: {
FormSize += 8;
break;
}
case dwarf::DW_FORM_data16: {
FormSize += 16;
break;
}
case dwarf::DW_FORM_sdata: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
DWARFExtractor.getSLEB128(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_udata:
case dwarf::DW_FORM_ref_udata:
case dwarf::DW_FORM_ref4_cas:
case dwarf::DW_FORM_strp_cas:
case dwarf::DW_FORM_rnglistx:
case dwarf::DW_FORM_loclistx:
case dwarf::DW_FORM_GNU_addr_index:
case dwarf::DW_FORM_GNU_str_index:
case dwarf::DW_FORM_addrx:
case dwarf::DW_FORM_strx: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
DWARFExtractor.getULEB128(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_LLVM_addrx_offset: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
DWARFExtractor.getULEB128(&CUOffset, &Err);
FormSize += CUOffset - PrevOffset + 4;
break;
}
case dwarf::DW_FORM_string: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
auto CurrOffset = CUOffset;
DWARFExtractor.getCStr(&CUOffset, &Err);
FormSize += CUOffset - CurrOffset;
break;
}
case dwarf::DW_FORM_indirect: {
DWARFDataExtractor DWARFExtractor(CUData, IsLittleEndian, AddressSize);
uint64_t PrevOffset = CUOffset;
Form =
static_cast<dwarf::Form>(DWARFExtractor.getULEB128(&CUOffset, &Err));
Indirect = true;
FormSize += CUOffset - PrevOffset;
break;
}
case dwarf::DW_FORM_strp:
case dwarf::DW_FORM_sec_offset:
case dwarf::DW_FORM_GNU_ref_alt:
case dwarf::DW_FORM_GNU_strp_alt:
case dwarf::DW_FORM_line_strp:
case dwarf::DW_FORM_strp_sup: {
FormSize += FP.getDwarfOffsetByteSize();
break;
}
case dwarf::DW_FORM_addrx3:
case dwarf::DW_FORM_lo_user: {
llvm_unreachable("usupported form");
break;
}
}
} while (Indirect && !Err);
if (Err)
return std::move(Err);
return FormSize;
}
template <> struct llvm::DenseMapInfo<llvm::dwarf::Form> {
static llvm::dwarf::Form getEmptyKey() {
return static_cast<llvm::dwarf::Form>(
DenseMapInfo<uint16_t>::getEmptyKey());
}
static llvm::dwarf::Form getTombstoneKey() {
return static_cast<llvm::dwarf::Form>(
DenseMapInfo<uint16_t>::getTombstoneKey());
}
static unsigned getHashValue(const llvm::dwarf::Form &OVal) {
return DenseMapInfo<uint16_t>::getHashValue(OVal);
}
static bool isEqual(const llvm::dwarf::Form &LHS,
const llvm::dwarf::Form &RHS) {
return LHS == RHS;
}
};
bool mccasformats::v1::doesntDedup(dwarf::Form Form, dwarf::Attribute Attr) {
// This is a list of attributes known to have a high impact in the
// deduplication of CAS objects.
// Some of these are dependent on the Attribute in which they are used.
static const DenseMap<dwarf::Form, SmallVector<dwarf::Attribute>>
FormsToPartition{
{dwarf::Form::DW_FORM_ref_addr, {}},
{dwarf::Form::DW_FORM_strp, {}},
{dwarf::Form::DW_FORM_strp_cas, {}},
{dwarf::Form::DW_FORM_ref4, {}},
{dwarf::Form::DW_FORM_ref4_cas, {}},
{dwarf::Form::DW_FORM_data1,
{dwarf::Attribute::DW_AT_call_file,
dwarf::Attribute::DW_AT_decl_file}},
{dwarf::Form::DW_FORM_data2,
{dwarf::Attribute::DW_AT_call_file,
dwarf::Attribute::DW_AT_decl_file}},
{dwarf::Form::DW_FORM_data4,
{dwarf::Attribute::DW_AT_call_file,
dwarf::Attribute::DW_AT_decl_file}},
{dwarf::Form::DW_FORM_data8,
{dwarf::Attribute::DW_AT_decl_file,
dwarf::Attribute::DW_AT_call_file}},
{dwarf::Form::DW_FORM_addrx, {}},
{dwarf::Form::DW_FORM_addr, {}},
{dwarf::Form::DW_FORM_strx, {}},
{dwarf::Form::DW_FORM_strx1, {}},
{dwarf::Form::DW_FORM_strx2, {}},
{dwarf::Form::DW_FORM_strx3, {}},
{dwarf::Form::DW_FORM_strx4, {}},
};
auto it = FormsToPartition.find(Form);
if (it == FormsToPartition.end())
return false;
if (it->second.empty())
return true;
return llvm::is_contained(it->second, Attr);
}
uint64_t mccasformats::v1::convertFourByteFormDataToULEB(
ArrayRef<char> FormData, DataWriter &Writer, bool IsLittleEndian,
uint8_t AddressSize) {
assert(FormData.size() == 4);
StringRef FormDataStringRef = StringRef(FormData.begin(), FormData.size());
DataExtractor Extractor(FormDataStringRef, IsLittleEndian, AddressSize);
DataExtractor::Cursor Cursor(0);
uint32_t IntegerData = Extractor.getU32(Cursor);
if (!Cursor)
handleAllErrors(Cursor.takeError()); // this should never fail
Writer.writeULEB128(IntegerData);
return getULEB128Size(IntegerData);
}
void AbbrevEntryWriter::writeAbbrevEntry(DWARFDie DIE) {
// [uleb(Tag), has_children]
// [uleb(Attr), uleb(Form)]*
writeULEB128(DIE.getTag());
writeByte(DIE.hasChildren());
for (const DWARFAttribute &AttrValue : DIE.attributes()) {
writeULEB128(AttrValue.Attr);
dwarf::Form Form = AttrValue.Value.getForm();
if (Form == dwarf::Form::DW_FORM_ref4)
Form = dwarf::Form::DW_FORM_ref4_cas;
if (Form == dwarf::Form::DW_FORM_strp)
Form = dwarf::Form::DW_FORM_strp_cas;
writeULEB128(Form);
// Dwarf 5: Section 7.4:
// The form DW_FORM_implicit_const has to be handled specially. It's
// specification contains a third part, which is a signed LEB128 number.
// This number is used as the value of the attribute with the aformentioned
// form and nothing is stored in the .debug_info section.
if (Form == dwarf::Form::DW_FORM_implicit_const)
writeSLEB128(AttrValue.Value.getRawSValue());
}
}
Expected<dwarf::Tag> AbbrevEntryReader::readTag() {
uint64_t TagAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
return Cursor.takeError();
return static_cast<dwarf::Tag>(TagAsInt);
}
Expected<bool> AbbrevEntryReader::readHasChildren() {
char HasChildren = Extractor.getU8(Cursor);
if (!Cursor)
return Cursor.takeError();
return HasChildren;
}
Expected<dwarf::Attribute> AbbrevEntryReader::readAttr() {
if (Extractor.eof(Cursor))
return static_cast<dwarf::Attribute>(getEndOfAttributesMarker());
uint64_t AttrAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
return Cursor.takeError();
return static_cast<dwarf::Attribute>(AttrAsInt);
}
static Expected<int64_t> handleImplicitConst(DataExtractor &Extractor,
DataExtractor::Cursor &Cursor) {
int64_t ImplicitVal = Extractor.getSLEB128(Cursor);
if (!Cursor)
return Cursor.takeError();
return ImplicitVal;
}
Expected<dwarf::Form> AbbrevEntryReader::readForm() {
uint64_t FormAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
return Cursor.takeError();
auto Form = static_cast<dwarf::Form>(FormAsInt);
// Dwarf 5: Section 7.4:
// The form DW_FORM_implicit_const has to be handled specially. It's
// specification contains a third part, which is a signed LEB128 number. This
// number is used as the value of the attribute with the aformentioned form
// and nothing is stored in the .debug_info section.
// Advance reader to beyond the implicit_const value, to read Forms correctly.
if (Form == dwarf::Form::DW_FORM_implicit_const) {
auto ImplicitVal = handleImplicitConst(Extractor, Cursor);
if (!ImplicitVal)
return ImplicitVal.takeError();
}
return Form;
}
uint64_t mccasformats::v1::reconstructAbbrevSection(
raw_ostream &OS, ArrayRef<StringRef> AbbrevEntries,
uint64_t &MaxDIEAbbrevCount, bool IsLittleEndian, uint8_t AddressSize) {
uint64_t WrittenSize = 0;
for (auto EntryData : AbbrevEntries) {
// Dwarf 5: Section 7.5.3:
// Each declaration begins with an unsigned LEB128 number representing the
// abbreviation code itself. [...] The abbreviation code 0 is reserved for
// null debugging information entries.
WrittenSize += encodeULEB128(MaxDIEAbbrevCount, OS);
DataExtractor Extractor(EntryData, IsLittleEndian, AddressSize);
DataExtractor::Cursor Cursor(0);
// [uleb(Tag), has_children]
uint64_t TagAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
handleAllErrors(Cursor.takeError());
uint8_t HasChildren = Extractor.getU8(Cursor);
if (!Cursor)
handleAllErrors(Cursor.takeError());
WrittenSize += encodeULEB128(TagAsInt, OS);
OS << HasChildren;
WrittenSize += 1;
assert(HasChildren == 0 || HasChildren == 1);
// [uleb(Attr), uleb(Form)]*
while (!Extractor.eof(Cursor)) {
uint64_t AttrAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
handleAllErrors(Cursor.takeError());
uint64_t FormAsInt = Extractor.getULEB128(Cursor);
if (!Cursor)
handleAllErrors(Cursor.takeError());
WrittenSize += encodeULEB128(AttrAsInt, OS);
auto Form = static_cast<dwarf::Form>(FormAsInt);
if (Form == dwarf::Form::DW_FORM_ref4_cas)
Form = dwarf::Form::DW_FORM_ref4;
if (Form == dwarf::Form::DW_FORM_strp_cas)
Form = dwarf::Form::DW_FORM_strp;
WrittenSize += encodeULEB128(Form, OS);
// Dwarf 5: Section 7.4:
// The form DW_FORM_implicit_const has to be handled specially. It's
// specification contains a third part, which is a signed LEB128 number.
// This number is used as the value of the attribute with the
// aformentioned form and nothing is stored in the .debug_info section.
if (Form == dwarf::Form::DW_FORM_implicit_const) {
auto ImplicitVal = handleImplicitConst(Extractor, Cursor);
if (!ImplicitVal)
handleAllErrors(ImplicitVal.takeError());
WrittenSize += encodeSLEB128(*ImplicitVal, OS);
}
}
// Dwarf 5: Section 7.5.3:
// The series of attribute specifications ends with an entry containing 0
// for the name and 0 for the form.
OS.write_zeros(2);
WrittenSize += 2;
MaxDIEAbbrevCount++;
}
return WrittenSize;
}
|