2016-04-07 15:06:43 -07:00
|
|
|
//===- DebugData.cpp - Representation and writing of debugging information. ==//
|
|
|
|
|
//
|
2021-03-15 18:04:18 -07:00
|
|
|
// 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
|
2016-04-07 15:06:43 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2021-10-08 11:47:10 -07:00
|
|
|
#include "bolt/Core/DebugData.h"
|
|
|
|
|
#include "bolt/Core/BinaryBasicBlock.h"
|
|
|
|
|
#include "bolt/Core/BinaryFunction.h"
|
|
|
|
|
#include "bolt/Utils/Utils.h"
|
2021-09-01 21:40:54 -07:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
2021-04-01 11:43:00 -07:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2016-09-02 14:15:29 -07:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-12-01 16:29:39 -08:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2017-05-22 17:17:04 -07:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2016-04-07 15:06:43 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cassert>
|
2021-04-01 11:43:00 -07:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <limits>
|
2016-04-07 15:06:43 -07:00
|
|
|
|
2017-01-18 10:09:54 -08:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
#define DEBUG_TYPE "bolt-debug-info"
|
|
|
|
|
|
2016-09-02 14:15:29 -07:00
|
|
|
namespace opts {
|
|
|
|
|
extern llvm::cl::opt<unsigned> Verbosity;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 15:06:43 -07:00
|
|
|
namespace llvm {
|
|
|
|
|
namespace bolt {
|
|
|
|
|
|
|
|
|
|
const DebugLineTableRowRef DebugLineTableRowRef::NULL_ROW{0, 0};
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Writes address ranges to Writer as pairs of 64-bit (address, size).
|
|
|
|
|
// If RelativeRange is true, assumes the address range to be written must be of
|
|
|
|
|
// the form (begin address, range size), otherwise (begin address, end address).
|
|
|
|
|
// Terminates the list by writing a pair of two zeroes.
|
|
|
|
|
// Returns the number of written bytes.
|
2017-05-16 09:27:34 -07:00
|
|
|
uint64_t writeAddressRanges(
|
2020-12-01 16:29:39 -08:00
|
|
|
raw_svector_ostream &Stream,
|
2019-03-29 14:22:54 -07:00
|
|
|
const DebugAddressRangesVector &AddressRanges,
|
2017-05-16 09:27:34 -07:00
|
|
|
const bool WriteRelativeRanges = false) {
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const DebugAddressRange &Range : AddressRanges) {
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(Stream, Range.LowPC, support::little);
|
|
|
|
|
support::endian::write(
|
|
|
|
|
Stream, WriteRelativeRanges ? Range.HighPC - Range.LowPC : Range.HighPC,
|
|
|
|
|
support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
// Finish with 0 entries.
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(Stream, 0ULL, support::little);
|
|
|
|
|
support::endian::write(Stream, 0ULL, support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
return AddressRanges.size() * 16 + 16;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
DebugRangesSectionWriter::DebugRangesSectionWriter() {
|
2021-09-17 14:48:14 -07:00
|
|
|
RangesBuffer = std::make_unique<DebugBufferVector>();
|
2020-12-01 16:29:39 -08:00
|
|
|
RangesStream = std::make_unique<raw_svector_ostream>(*RangesBuffer);
|
2017-05-16 09:27:34 -07:00
|
|
|
|
|
|
|
|
// Add an empty range as the first entry;
|
2020-12-01 16:29:39 -08:00
|
|
|
SectionOffset +=
|
|
|
|
|
writeAddressRanges(*RangesStream.get(), DebugAddressRangesVector{});
|
2017-05-16 09:27:34 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 11:24:49 -07:00
|
|
|
uint64_t DebugRangesSectionWriter::addRanges(
|
2020-10-16 00:11:24 -07:00
|
|
|
DebugAddressRangesVector &&Ranges,
|
2019-07-17 14:58:17 -07:00
|
|
|
std::map<DebugAddressRangesVector, uint64_t> &CachedRanges) {
|
2017-05-16 09:27:34 -07:00
|
|
|
if (Ranges.empty())
|
|
|
|
|
return getEmptyRangesOffset();
|
|
|
|
|
|
2020-10-16 00:11:24 -07:00
|
|
|
const auto RI = CachedRanges.find(Ranges);
|
|
|
|
|
if (RI != CachedRanges.end())
|
|
|
|
|
return RI->second;
|
2016-04-07 15:06:43 -07:00
|
|
|
|
2020-10-16 00:11:24 -07:00
|
|
|
const uint64_t EntryOffset = addRanges(Ranges);
|
2017-05-16 09:27:34 -07:00
|
|
|
CachedRanges.emplace(std::move(Ranges), EntryOffset);
|
|
|
|
|
|
|
|
|
|
return EntryOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t
|
2019-10-25 11:24:49 -07:00
|
|
|
DebugRangesSectionWriter::addRanges(const DebugAddressRangesVector &Ranges) {
|
2017-05-16 09:27:34 -07:00
|
|
|
if (Ranges.empty())
|
|
|
|
|
return getEmptyRangesOffset();
|
|
|
|
|
|
2019-07-17 14:58:17 -07:00
|
|
|
// Reading the SectionOffset and updating it should be atomic to guarantee
|
|
|
|
|
// unique and correct offsets in patches.
|
|
|
|
|
std::lock_guard<std::mutex> Lock(WriterMutex);
|
2021-04-08 00:19:26 -07:00
|
|
|
const uint32_t EntryOffset = SectionOffset;
|
2020-12-01 16:29:39 -08:00
|
|
|
SectionOffset += writeAddressRanges(*RangesStream.get(), Ranges);
|
2017-05-16 09:27:34 -07:00
|
|
|
|
|
|
|
|
return EntryOffset;
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
uint64_t DebugRangesSectionWriter::getSectionOffset() {
|
|
|
|
|
std::lock_guard<std::mutex> Lock(WriterMutex);
|
|
|
|
|
return SectionOffset;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 00:11:24 -07:00
|
|
|
void DebugARangesSectionWriter::addCURanges(uint64_t CUOffset,
|
|
|
|
|
DebugAddressRangesVector &&Ranges) {
|
2019-10-25 11:24:49 -07:00
|
|
|
std::lock_guard<std::mutex> Lock(CUAddressRangesMutex);
|
|
|
|
|
CUAddressRanges.emplace(CUOffset, std::move(Ranges));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
void DebugARangesSectionWriter::writeARangesSection(
|
|
|
|
|
raw_svector_ostream &RangesStream) const {
|
2016-04-07 15:06:43 -07:00
|
|
|
// For reference on the format of the .debug_aranges section, see the DWARF4
|
|
|
|
|
// specification, section 6.1.4 Lookup by Address
|
|
|
|
|
// http://www.dwarfstd.org/doc/DWARF4.pdf
|
|
|
|
|
for (const auto &CUOffsetAddressRangesPair : CUAddressRanges) {
|
2021-04-08 00:19:26 -07:00
|
|
|
const uint64_t Offset = CUOffsetAddressRangesPair.first;
|
|
|
|
|
const DebugAddressRangesVector &AddressRanges =
|
|
|
|
|
CUOffsetAddressRangesPair.second;
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Emit header.
|
|
|
|
|
|
|
|
|
|
// Size of this set: 8 (size of the header) + 4 (padding after header)
|
|
|
|
|
// + 2*sizeof(uint64_t) bytes for each of the ranges, plus an extra
|
|
|
|
|
// pair of uint64_t's for the terminating, zero-length range.
|
|
|
|
|
// Does not include size field itself.
|
2020-12-01 16:29:39 -08:00
|
|
|
uint32_t Size = 8 + 4 + 2*sizeof(uint64_t) * (AddressRanges.size() + 1);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Header field #1: set size.
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(RangesStream, Size, support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Header field #2: version number, 2 as per the specification.
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(RangesStream, static_cast<uint16_t>(2),
|
|
|
|
|
support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Header field #3: debug info offset of the correspondent compile unit.
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(RangesStream, static_cast<uint32_t>(Offset),
|
|
|
|
|
support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Header field #4: address size.
|
|
|
|
|
// 8 since we only write ELF64 binaries for now.
|
2020-12-01 16:29:39 -08:00
|
|
|
RangesStream << char(8);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Header field #5: segment size of target architecture.
|
2020-12-01 16:29:39 -08:00
|
|
|
RangesStream << char(0);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
|
|
|
|
// Padding before address table - 4 bytes in the 64-bit-pointer case.
|
2020-12-01 16:29:39 -08:00
|
|
|
support::endian::write(RangesStream, static_cast<uint32_t>(0),
|
|
|
|
|
support::little);
|
2016-04-07 15:06:43 -07:00
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
writeAddressRanges(RangesStream, AddressRanges, true);
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
DebugAddrWriter::DebugAddrWriter(BinaryContext *Bc) { BC = Bc; }
|
|
|
|
|
|
|
|
|
|
void DebugAddrWriter::AddressForDWOCU::dump() {
|
|
|
|
|
std::vector<IndexAddressPair> SortedMap(indexToAddressBegin(),
|
|
|
|
|
indexToAdddessEnd());
|
|
|
|
|
// Sorting address in increasing order of indices.
|
|
|
|
|
std::sort(SortedMap.begin(), SortedMap.end(),
|
|
|
|
|
[](const IndexAddressPair &A, const IndexAddressPair &B) {
|
|
|
|
|
return A.first < B.first;
|
|
|
|
|
});
|
|
|
|
|
for (auto &Pair : SortedMap)
|
|
|
|
|
dbgs() << Twine::utohexstr(Pair.second) << "\t" << Pair.first << "\n";
|
|
|
|
|
}
|
|
|
|
|
uint32_t DebugAddrWriter::getIndexFromAddress(uint64_t Address,
|
|
|
|
|
uint64_t DWOId) {
|
|
|
|
|
if (!AddressMaps.count(DWOId))
|
|
|
|
|
AddressMaps[DWOId] = AddressForDWOCU();
|
|
|
|
|
|
|
|
|
|
AddressForDWOCU &Map = AddressMaps[DWOId];
|
|
|
|
|
auto Entry = Map.find(Address);
|
|
|
|
|
if (Entry == Map.end()) {
|
|
|
|
|
auto Index = Map.getNextIndex();
|
|
|
|
|
Entry = Map.insert(Address, Index).first;
|
|
|
|
|
}
|
|
|
|
|
return Entry->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Case1) Address is not in map insert in to AddresToIndex and IndexToAddres
|
|
|
|
|
// Case2) Address is in the map but Index is higher or equal. Need to update
|
|
|
|
|
// IndexToAddrss. Case3) Address is in the map but Index is lower. Need to
|
|
|
|
|
// update AddressToIndex and IndexToAddress
|
|
|
|
|
void DebugAddrWriter::addIndexAddress(uint64_t Address, uint32_t Index,
|
|
|
|
|
uint64_t DWOId) {
|
|
|
|
|
AddressForDWOCU &Map = AddressMaps[DWOId];
|
|
|
|
|
auto Entry = Map.find(Address);
|
|
|
|
|
if (Entry != Map.end()) {
|
|
|
|
|
if (Entry->second > Index)
|
|
|
|
|
Map.updateAddressToIndex(Address, Index);
|
|
|
|
|
Map.updateIndexToAddrss(Address, Index);
|
|
|
|
|
} else
|
|
|
|
|
Map.insert(Address, Index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddressSectionBuffer DebugAddrWriter::finalize() {
|
|
|
|
|
// Need to layout all sections within .debug_addr
|
|
|
|
|
// Within each section sort Address by index.
|
|
|
|
|
AddressSectionBuffer Buffer;
|
|
|
|
|
raw_svector_ostream AddressStream(Buffer);
|
|
|
|
|
for (std::unique_ptr<DWARFUnit> &CU : BC->DwCtx->compile_units()) {
|
|
|
|
|
Optional<uint64_t> DWOId = CU->getDWOId();
|
|
|
|
|
// Handling the case wehre debug information is a mix of Debug fission and
|
|
|
|
|
// monolitic.
|
|
|
|
|
if (!DWOId)
|
|
|
|
|
continue;
|
|
|
|
|
auto AM = AddressMaps.find(*DWOId);
|
|
|
|
|
// Adding to map even if it did not contribute to .debug_addr.
|
|
|
|
|
// The Skeleton CU will still have DW_AT_GNU_addr_base.
|
|
|
|
|
DWOIdToOffsetMap[*DWOId] = Buffer.size();
|
|
|
|
|
// If does not exist this CUs DWO section didn't contribute to .debug_addr.
|
|
|
|
|
if (AM == AddressMaps.end())
|
|
|
|
|
continue;
|
|
|
|
|
std::vector<IndexAddressPair> SortedMap(AM->second.indexToAddressBegin(),
|
|
|
|
|
AM->second.indexToAdddessEnd());
|
|
|
|
|
// Sorting address in increasing order of indices.
|
|
|
|
|
std::sort(SortedMap.begin(), SortedMap.end(),
|
|
|
|
|
[](const IndexAddressPair &A, const IndexAddressPair &B) {
|
|
|
|
|
return A.first < B.first;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uint8_t AddrSize = CU->getAddressByteSize();
|
|
|
|
|
uint32_t Counter = 0;
|
|
|
|
|
auto WriteAddress = [&](uint64_t Address) -> void {
|
|
|
|
|
++Counter;
|
|
|
|
|
switch (AddrSize) {
|
|
|
|
|
default:
|
|
|
|
|
assert(false && "Address Size is invalid.");
|
2021-06-29 12:11:56 -07:00
|
|
|
break;
|
2021-04-01 11:43:00 -07:00
|
|
|
case 4:
|
|
|
|
|
support::endian::write(AddressStream, static_cast<uint32_t>(Address),
|
|
|
|
|
support::little);
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
support::endian::write(AddressStream, Address, support::little);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const IndexAddressPair &Val : SortedMap) {
|
|
|
|
|
while (Val.first > Counter)
|
|
|
|
|
WriteAddress(0);
|
|
|
|
|
WriteAddress(Val.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t DebugAddrWriter::getOffset(uint64_t DWOId) {
|
|
|
|
|
auto Iter = DWOIdToOffsetMap.find(DWOId);
|
|
|
|
|
assert(Iter != DWOIdToOffsetMap.end() &&
|
|
|
|
|
"Offset in to.debug_addr was not found for DWO ID.");
|
|
|
|
|
return Iter->second;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 09:27:34 -07:00
|
|
|
DebugLocWriter::DebugLocWriter(BinaryContext *BC) {
|
2021-09-17 14:48:14 -07:00
|
|
|
LocBuffer = std::make_unique<DebugBufferVector>();
|
2020-12-01 16:29:39 -08:00
|
|
|
LocStream = std::make_unique<raw_svector_ostream>(*LocBuffer);
|
2017-05-16 09:27:34 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
void DebugLocWriter::addList(uint64_t AttrOffset,
|
|
|
|
|
DebugLocationsVector &&LocList) {
|
|
|
|
|
if (LocList.empty()) {
|
|
|
|
|
EmptyAttrLists.push_back(AttrOffset);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-25 11:47:51 -07:00
|
|
|
// Since there is a separate DebugLocWriter for each thread,
|
|
|
|
|
// we don't need a lock to read the SectionOffset and update it.
|
2021-04-08 00:19:26 -07:00
|
|
|
const uint32_t EntryOffset = SectionOffset;
|
2019-07-24 17:54:14 -07:00
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
for (const DebugLocationEntry &Entry : LocList) {
|
|
|
|
|
support::endian::write(*LocStream, static_cast<uint64_t>(Entry.LowPC),
|
|
|
|
|
support::little);
|
|
|
|
|
support::endian::write(*LocStream, static_cast<uint64_t>(Entry.HighPC),
|
|
|
|
|
support::little);
|
|
|
|
|
support::endian::write(*LocStream, static_cast<uint16_t>(Entry.Expr.size()),
|
|
|
|
|
support::little);
|
|
|
|
|
*LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()),
|
|
|
|
|
Entry.Expr.size());
|
|
|
|
|
SectionOffset += 2 * 8 + 2 + Entry.Expr.size();
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
2020-12-01 16:29:39 -08:00
|
|
|
LocStream->write_zeros(16);
|
|
|
|
|
SectionOffset += 16;
|
2021-10-11 17:51:05 -07:00
|
|
|
LocListDebugInfoPatches.push_back({AttrOffset, EntryOffset});
|
|
|
|
|
}
|
2017-05-16 09:27:34 -07:00
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
void DebugLoclistWriter::addList(uint64_t AttrOffset,
|
|
|
|
|
DebugLocationsVector &&LocList) {
|
|
|
|
|
Patches.push_back({AttrOffset, std::move(LocList)});
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
std::unique_ptr<DebugBufferVector> DebugLocWriter::getBuffer() {
|
|
|
|
|
return std::move(LocBuffer);
|
2021-04-01 11:43:00 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
// DWARF 4: 2.6.2
|
|
|
|
|
void DebugLocWriter::finalize(uint64_t SectionOffset,
|
|
|
|
|
SimpleBinaryPatcher &DebugInfoPatcher) {
|
|
|
|
|
for (const auto LocListDebugInfoPatchType : LocListDebugInfoPatches) {
|
|
|
|
|
uint64_t Offset = SectionOffset + LocListDebugInfoPatchType.LocListOffset;
|
|
|
|
|
DebugInfoPatcher.addLE32Patch(LocListDebugInfoPatchType.DebugInfoAttrOffset,
|
|
|
|
|
Offset);
|
|
|
|
|
}
|
2021-04-01 11:43:00 -07:00
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
for (uint64_t DebugInfoAttrOffset : EmptyAttrLists)
|
|
|
|
|
DebugInfoPatcher.addLE32Patch(DebugInfoAttrOffset,
|
|
|
|
|
DebugLocWriter::EmptyListOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DebugLoclistWriter::finalize(uint64_t SectionOffset,
|
|
|
|
|
SimpleBinaryPatcher &DebugInfoPatcher) {
|
|
|
|
|
for (LocPatch &Patch : Patches) {
|
|
|
|
|
if (Patch.LocList.empty()) {
|
|
|
|
|
DebugInfoPatcher.addLE32Patch(Patch.AttrOffset,
|
|
|
|
|
DebugLocWriter::EmptyListOffset);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const uint32_t EntryOffset = LocBuffer->size();
|
|
|
|
|
for (const DebugLocationEntry &Entry : Patch.LocList) {
|
|
|
|
|
support::endian::write(*LocStream,
|
|
|
|
|
static_cast<uint8_t>(dwarf::DW_LLE_startx_length),
|
|
|
|
|
support::little);
|
|
|
|
|
uint32_t Index = AddrWriter->getIndexFromAddress(Entry.LowPC, DWOId);
|
|
|
|
|
encodeULEB128(Index, *LocStream);
|
|
|
|
|
|
|
|
|
|
// TODO: Support DWARF5
|
|
|
|
|
support::endian::write(*LocStream,
|
|
|
|
|
static_cast<uint32_t>(Entry.HighPC - Entry.LowPC),
|
|
|
|
|
support::little);
|
|
|
|
|
support::endian::write(*LocStream,
|
|
|
|
|
static_cast<uint16_t>(Entry.Expr.size()),
|
|
|
|
|
support::little);
|
|
|
|
|
*LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()),
|
|
|
|
|
Entry.Expr.size());
|
|
|
|
|
}
|
2021-04-01 11:43:00 -07:00
|
|
|
support::endian::write(*LocStream,
|
2021-10-11 17:51:05 -07:00
|
|
|
static_cast<uint8_t>(dwarf::DW_LLE_end_of_list),
|
2021-04-01 11:43:00 -07:00
|
|
|
support::little);
|
2021-10-11 17:51:05 -07:00
|
|
|
DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, EntryOffset);
|
|
|
|
|
clearList(Patch.LocList);
|
2021-04-01 11:43:00 -07:00
|
|
|
}
|
2021-10-11 17:51:05 -07:00
|
|
|
clearList(Patches);
|
2021-04-01 11:43:00 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 17:51:05 -07:00
|
|
|
DebugAddrWriter *DebugLoclistWriter::AddrWriter = nullptr;
|
|
|
|
|
|
2016-04-07 15:06:43 -07:00
|
|
|
void SimpleBinaryPatcher::addBinaryPatch(uint32_t Offset,
|
|
|
|
|
const std::string &NewValue) {
|
2021-05-07 18:43:25 -07:00
|
|
|
Patches.emplace_back(Offset, NewValue);
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleBinaryPatcher::addBytePatch(uint32_t Offset, uint8_t Value) {
|
2021-05-07 18:43:25 -07:00
|
|
|
Patches.emplace_back(Offset, std::string(1, Value));
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleBinaryPatcher::addLEPatch(uint32_t Offset, uint64_t NewValue,
|
|
|
|
|
size_t ByteSize) {
|
|
|
|
|
std::string LE64(ByteSize, 0);
|
|
|
|
|
for (size_t I = 0; I < ByteSize; ++I) {
|
|
|
|
|
LE64[I] = NewValue & 0xff;
|
|
|
|
|
NewValue >>= 8;
|
|
|
|
|
}
|
2021-05-07 18:43:25 -07:00
|
|
|
Patches.emplace_back(Offset, LE64);
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-22 17:17:04 -07:00
|
|
|
void SimpleBinaryPatcher::addUDataPatch(uint32_t Offset, uint64_t Value, uint64_t Size) {
|
|
|
|
|
std::string Buff;
|
|
|
|
|
raw_string_ostream OS(Buff);
|
[BOLT rebase] Rebase fixes on top of LLVM Feb2018
Summary:
This commit includes all code necessary to make BOLT working again
after the rebase. This includes a redesign of the EHFrame work,
cherry-pick of the 3dnow disassembly work, compilation error fixes,
and port of the debug_info work. The macroop fusion feature is not
ported yet.
The rebased version has minor changes to the "executed instructions"
dynostats counter because REP prefixes are considered a part of the
instruction it applies to. Also, some X86 instructions had the "mayLoad"
tablegen property removed, which BOLT uses to identify and account
for loads, thus reducing the total number of loads reported by
dynostats. This was observed in X86::MOVDQUmr. TRAP instructions are
not terminators anymore, changing our CFG. This commit adds compensation
to preserve this old behavior and minimize tests changes. debug_info
sections are now slightly larger. The discriminator field in the line
table is slightly different due to a change upstream. New profiles
generated with the other bolt are incompatible with this version
because of different hash values calculated for functions, so they will
be considered 100% stale. This commit changes the corresponding test
to XFAIL so it can be updated. The hash function changes because it
relies on raw opcode values, which change according to the opcodes
described in the X86 tablegen files. When processing HHVM, bolt was
observed to be using about 800MB more memory in the rebased version
and being about 5% slower.
(cherry picked from FBD7078072)
2018-02-06 15:00:23 -08:00
|
|
|
encodeULEB128(Value, OS, Size);
|
2017-05-22 17:17:04 -07:00
|
|
|
|
|
|
|
|
Patches.emplace_back(Offset, OS.str());
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 15:06:43 -07:00
|
|
|
void SimpleBinaryPatcher::addLE64Patch(uint32_t Offset, uint64_t NewValue) {
|
|
|
|
|
addLEPatch(Offset, NewValue, 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleBinaryPatcher::addLE32Patch(uint32_t Offset, uint32_t NewValue) {
|
|
|
|
|
addLEPatch(Offset, NewValue, 4);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 15:57:34 -07:00
|
|
|
void SimpleBinaryPatcher::patchBinary(std::string &BinaryContents,
|
|
|
|
|
uint32_t DWPOffset = 0) {
|
2016-04-07 15:06:43 -07:00
|
|
|
for (const auto &Patch : Patches) {
|
2021-06-18 15:57:34 -07:00
|
|
|
uint32_t Offset = Patch.first - DWPOffset;
|
2016-04-07 15:06:43 -07:00
|
|
|
const std::string &ByteSequence = Patch.second;
|
|
|
|
|
assert(Offset + ByteSequence.size() <= BinaryContents.size() &&
|
|
|
|
|
"Applied patch runs over binary size.");
|
|
|
|
|
for (uint64_t I = 0, Size = ByteSequence.size(); I < Size; ++I) {
|
|
|
|
|
BinaryContents[Offset + I] = ByteSequence[I];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
void DebugStrWriter::create() {
|
|
|
|
|
StrBuffer = std::make_unique<DebugStrBufferVector>();
|
|
|
|
|
StrStream = std::make_unique<raw_svector_ostream>(*StrBuffer);
|
2016-04-07 15:06:43 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
void DebugStrWriter::initialize() {
|
|
|
|
|
auto StrSection = BC->DwCtx->getDWARFObj().getStrSection();
|
|
|
|
|
(*StrStream) << StrSection;
|
|
|
|
|
}
|
2016-04-07 15:06:43 -07:00
|
|
|
|
2021-04-01 11:43:00 -07:00
|
|
|
uint32_t DebugStrWriter::addString(StringRef Str) {
|
|
|
|
|
if (StrBuffer->empty())
|
|
|
|
|
initialize();
|
|
|
|
|
auto Offset = StrBuffer->size();
|
|
|
|
|
(*StrStream) << Str;
|
|
|
|
|
StrStream->write_zeros(1);
|
|
|
|
|
return Offset;
|
|
|
|
|
}
|
2016-04-07 15:06:43 -07:00
|
|
|
|
2021-09-17 14:48:14 -07:00
|
|
|
void DebugAbbrevWriter::addUnitAbbreviations(DWARFUnit &Unit) {
|
|
|
|
|
const DWARFAbbreviationDeclarationSet *Abbrevs = Unit.getAbbreviations();
|
|
|
|
|
if (!Abbrevs)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-09-28 23:30:06 -07:00
|
|
|
// Multiple units may share the same abbreviations. Only add abbreviations
|
|
|
|
|
// for the first unit and reuse them.
|
|
|
|
|
const uint64_t AbbrevOffset = Unit.getAbbreviationsOffset();
|
|
|
|
|
if (CUAbbrevData.find(AbbrevOffset) != CUAbbrevData.end())
|
|
|
|
|
return;
|
2021-09-17 14:48:14 -07:00
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(WriterMutex);
|
2021-09-28 23:30:06 -07:00
|
|
|
AbbrevData &UnitData = CUAbbrevData[AbbrevOffset];
|
|
|
|
|
UnitData.Buffer = std::make_unique<DebugBufferVector>();
|
|
|
|
|
UnitData.Stream = std::make_unique<raw_svector_ostream>(*UnitData.Buffer);
|
|
|
|
|
|
2021-09-17 14:48:14 -07:00
|
|
|
const auto &UnitPatches = Patches[&Unit];
|
|
|
|
|
|
2021-09-28 23:30:06 -07:00
|
|
|
raw_svector_ostream &OS = *UnitData.Stream.get();
|
2021-09-17 14:48:14 -07:00
|
|
|
|
|
|
|
|
// Take a fast path if there are no patches to apply. Simply copy the original
|
|
|
|
|
// contents.
|
|
|
|
|
if (UnitPatches.empty()) {
|
|
|
|
|
StringRef AbbrevSectionContents =
|
|
|
|
|
Unit.isDWOUnit() ? Unit.getContext().getDWARFObj().getAbbrevDWOSection()
|
|
|
|
|
: Unit.getContext().getDWARFObj().getAbbrevSection();
|
|
|
|
|
StringRef AbbrevContents;
|
|
|
|
|
|
|
|
|
|
const DWARFUnitIndex &CUIndex = Unit.getContext().getCUIndex();
|
|
|
|
|
if (!CUIndex.getRows().empty()) {
|
|
|
|
|
// Handle DWP section contribution.
|
|
|
|
|
const DWARFUnitIndex::Entry *DWOEntry =
|
|
|
|
|
CUIndex.getFromHash(*Unit.getDWOId());
|
|
|
|
|
if (!DWOEntry)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const DWARFUnitIndex::Entry::SectionContribution *DWOContrubution =
|
|
|
|
|
DWOEntry->getContribution(DWARFSectionKind::DW_SECT_ABBREV);
|
|
|
|
|
AbbrevContents = AbbrevSectionContents.substr(DWOContrubution->Offset,
|
|
|
|
|
DWOContrubution->Length);
|
|
|
|
|
} else {
|
|
|
|
|
DWARFCompileUnit *NextUnit =
|
|
|
|
|
Unit.getContext().getCompileUnitForOffset(Unit.getNextUnitOffset());
|
|
|
|
|
const uint64_t StartOffset = Unit.getAbbreviationsOffset();
|
|
|
|
|
const uint64_t EndOffset = NextUnit ? NextUnit->getAbbreviationsOffset()
|
|
|
|
|
: AbbrevSectionContents.size();
|
|
|
|
|
AbbrevContents = AbbrevSectionContents.slice(StartOffset, EndOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OS.reserveExtraSpace(AbbrevContents.size());
|
|
|
|
|
OS << AbbrevContents;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto I = Abbrevs->begin(), E = Abbrevs->end(); I != E; ++I) {
|
|
|
|
|
const DWARFAbbreviationDeclaration &Abbrev = *I;
|
|
|
|
|
auto Patch = UnitPatches.find(&Abbrev);
|
|
|
|
|
|
|
|
|
|
encodeULEB128(Abbrev.getCode(), OS);
|
|
|
|
|
encodeULEB128(Abbrev.getTag(), OS);
|
|
|
|
|
encodeULEB128(Abbrev.hasChildren(), OS);
|
|
|
|
|
for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec :
|
|
|
|
|
Abbrev.attributes()) {
|
|
|
|
|
if (Patch != UnitPatches.end()) {
|
|
|
|
|
bool Patched = false;
|
|
|
|
|
// Patches added later take a precedence over earlier ones.
|
|
|
|
|
for (auto I = Patch->second.rbegin(), E = Patch->second.rend(); I != E;
|
|
|
|
|
++I) {
|
|
|
|
|
if (I->OldAttr != AttrSpec.Attr)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
encodeULEB128(I->NewAttr, OS);
|
|
|
|
|
encodeULEB128(I->NewAttrForm, OS);
|
|
|
|
|
Patched = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (Patched)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encodeULEB128(AttrSpec.Attr, OS);
|
|
|
|
|
encodeULEB128(AttrSpec.Form, OS);
|
|
|
|
|
if (AttrSpec.isImplicitConst())
|
|
|
|
|
encodeSLEB128(AttrSpec.getImplicitConstValue(), OS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encodeULEB128(0, OS);
|
|
|
|
|
encodeULEB128(0, OS);
|
|
|
|
|
}
|
|
|
|
|
encodeULEB128(0, OS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<DebugBufferVector> DebugAbbrevWriter::finalize() {
|
|
|
|
|
DebugBufferVector ReturnBuffer;
|
|
|
|
|
|
|
|
|
|
// Pre-calculate the total size of abbrev section.
|
|
|
|
|
uint64_t Size = 0;
|
|
|
|
|
for (const auto &KV : CUAbbrevData) {
|
2021-09-28 23:30:06 -07:00
|
|
|
const AbbrevData &UnitData = KV.second;
|
|
|
|
|
Size += UnitData.Buffer->size();
|
2021-09-17 14:48:14 -07:00
|
|
|
}
|
|
|
|
|
ReturnBuffer.reserve(Size);
|
|
|
|
|
|
|
|
|
|
uint64_t Pos = 0;
|
|
|
|
|
for (auto &KV : CUAbbrevData) {
|
2021-09-28 23:30:06 -07:00
|
|
|
AbbrevData &UnitData = KV.second;
|
|
|
|
|
ReturnBuffer.append(*UnitData.Buffer);
|
|
|
|
|
UnitData.Offset = Pos;
|
|
|
|
|
Pos += UnitData.Buffer->size();
|
2021-09-17 14:48:14 -07:00
|
|
|
|
2021-09-28 23:30:06 -07:00
|
|
|
UnitData.Buffer.reset();
|
|
|
|
|
UnitData.Stream.reset();
|
2021-09-17 14:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_unique<DebugBufferVector>(ReturnBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 21:40:54 -07:00
|
|
|
static void emitDwarfSetLineAddrAbs(MCStreamer &OS,
|
|
|
|
|
MCDwarfLineTableParams Params,
|
|
|
|
|
int64_t LineDelta, uint64_t Address,
|
|
|
|
|
int PointerSize) {
|
|
|
|
|
// emit the sequence to set the address
|
|
|
|
|
OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
|
|
|
|
|
OS.emitULEB128IntValue(PointerSize + 1);
|
|
|
|
|
OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
|
|
|
|
|
OS.emitIntValue(Address, PointerSize);
|
|
|
|
|
|
|
|
|
|
// emit the sequence for the LineDelta (from 1) and a zero address delta.
|
|
|
|
|
MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void emitBinaryDwarfLineTable(
|
|
|
|
|
MCStreamer *MCOS, MCDwarfLineTableParams Params,
|
2021-09-08 10:22:19 -07:00
|
|
|
const DWARFDebugLine::LineTable *Table,
|
|
|
|
|
const std::vector<DwarfLineTable::RowSequence> &InputSequences) {
|
|
|
|
|
if (InputSequences.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
constexpr uint64_t InvalidAddress = UINT64_MAX;
|
2021-09-01 21:40:54 -07:00
|
|
|
unsigned FileNum = 1;
|
|
|
|
|
unsigned LastLine = 1;
|
|
|
|
|
unsigned Column = 0;
|
|
|
|
|
unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
|
|
|
|
|
unsigned Isa = 0;
|
|
|
|
|
unsigned Discriminator = 0;
|
2021-09-08 10:22:19 -07:00
|
|
|
uint64_t LastAddress = InvalidAddress;
|
|
|
|
|
uint64_t PrevEndOfSequence = InvalidAddress;
|
2021-09-01 21:40:54 -07:00
|
|
|
const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo();
|
|
|
|
|
|
2021-09-08 10:22:19 -07:00
|
|
|
auto emitEndOfSequence = [&](uint64_t Address) {
|
|
|
|
|
MCDwarfLineAddr::Emit(MCOS, Params, INT64_MAX, Address - LastAddress);
|
|
|
|
|
FileNum = 1;
|
|
|
|
|
LastLine = 1;
|
|
|
|
|
Column = 0;
|
|
|
|
|
Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
|
|
|
|
|
Isa = 0;
|
|
|
|
|
Discriminator = 0;
|
|
|
|
|
LastAddress = InvalidAddress;
|
|
|
|
|
};
|
2021-09-01 21:40:54 -07:00
|
|
|
|
2021-09-08 10:22:19 -07:00
|
|
|
for (const DwarfLineTable::RowSequence &Sequence : InputSequences) {
|
|
|
|
|
const uint64_t SequenceStart =
|
|
|
|
|
Table->Rows[Sequence.FirstIndex].Address.Address;
|
2021-09-01 21:40:54 -07:00
|
|
|
|
2021-09-08 10:22:19 -07:00
|
|
|
// Check if we need to mark the end of the sequence.
|
|
|
|
|
if (PrevEndOfSequence != InvalidAddress && LastAddress != InvalidAddress &&
|
|
|
|
|
PrevEndOfSequence != SequenceStart) {
|
|
|
|
|
emitEndOfSequence(PrevEndOfSequence);
|
2021-09-01 21:40:54 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-08 10:22:19 -07:00
|
|
|
for (uint32_t RowIndex = Sequence.FirstIndex;
|
|
|
|
|
RowIndex <= Sequence.LastIndex; ++RowIndex) {
|
|
|
|
|
const DWARFDebugLine::Row &Row = Table->Rows[RowIndex];
|
|
|
|
|
int64_t LineDelta = static_cast<int64_t>(Row.Line) - LastLine;
|
|
|
|
|
const uint64_t Address = Row.Address.Address;
|
2021-09-01 21:40:54 -07:00
|
|
|
|
2021-09-08 10:22:19 -07:00
|
|
|
if (FileNum != Row.File) {
|
|
|
|
|
FileNum = Row.File;
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_file);
|
|
|
|
|
MCOS->emitULEB128IntValue(FileNum);
|
|
|
|
|
}
|
|
|
|
|
if (Column != Row.Column) {
|
|
|
|
|
Column = Row.Column;
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_column);
|
|
|
|
|
MCOS->emitULEB128IntValue(Column);
|
|
|
|
|
}
|
|
|
|
|
if (Discriminator != Row.Discriminator &&
|
|
|
|
|
MCOS->getContext().getDwarfVersion() >= 4) {
|
|
|
|
|
Discriminator = Row.Discriminator;
|
|
|
|
|
unsigned Size = getULEB128Size(Discriminator);
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_extended_op);
|
|
|
|
|
MCOS->emitULEB128IntValue(Size + 1);
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
|
|
|
|
|
MCOS->emitULEB128IntValue(Discriminator);
|
|
|
|
|
}
|
|
|
|
|
if (Isa != Row.Isa) {
|
|
|
|
|
Isa = Row.Isa;
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_isa);
|
|
|
|
|
MCOS->emitULEB128IntValue(Isa);
|
|
|
|
|
}
|
|
|
|
|
if (Row.IsStmt != Flags) {
|
|
|
|
|
Flags = Row.IsStmt;
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
|
|
|
|
|
}
|
|
|
|
|
if (Row.BasicBlock)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
|
|
|
|
|
if (Row.PrologueEnd)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
|
|
|
|
|
if (Row.EpilogueBegin)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
|
|
|
|
|
|
|
|
|
|
// The end of the sequence is not normal in the middle of the input
|
|
|
|
|
// sequence, but could happen, e.g. for assembly code.
|
|
|
|
|
if (Row.EndSequence) {
|
|
|
|
|
emitEndOfSequence(Address);
|
|
|
|
|
} else {
|
|
|
|
|
if (LastAddress == InvalidAddress)
|
|
|
|
|
emitDwarfSetLineAddrAbs(*MCOS, Params, LineDelta, Address,
|
|
|
|
|
AsmInfo->getCodePointerSize());
|
|
|
|
|
else
|
|
|
|
|
MCDwarfLineAddr::Emit(MCOS, Params, LineDelta, Address - LastAddress);
|
|
|
|
|
|
|
|
|
|
LastAddress = Address;
|
|
|
|
|
LastLine = Row.Line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Discriminator = 0;
|
|
|
|
|
}
|
|
|
|
|
PrevEndOfSequence = Sequence.EndAddress;
|
2021-09-01 21:40:54 -07:00
|
|
|
}
|
2021-09-08 10:22:19 -07:00
|
|
|
|
|
|
|
|
// Finish with the end of the sequence.
|
|
|
|
|
if (LastAddress != InvalidAddress)
|
|
|
|
|
emitEndOfSequence(PrevEndOfSequence);
|
2021-09-01 21:40:54 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-30 17:47:50 -07:00
|
|
|
// This function is similar to the one from MCDwarfLineTable, except it handles
|
|
|
|
|
// end-of-sequence entries differently by utilizing line entries with
|
|
|
|
|
// DWARF2_FLAG_END_SEQUENCE flag.
|
2021-09-01 21:40:54 -07:00
|
|
|
static inline void emitDwarfLineTable(
|
|
|
|
|
MCStreamer *MCOS, MCSection *Section,
|
|
|
|
|
const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
|
|
|
|
|
unsigned FileNum = 1;
|
|
|
|
|
unsigned LastLine = 1;
|
|
|
|
|
unsigned Column = 0;
|
|
|
|
|
unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
|
|
|
|
|
unsigned Isa = 0;
|
|
|
|
|
unsigned Discriminator = 0;
|
|
|
|
|
MCSymbol *LastLabel = nullptr;
|
|
|
|
|
const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo();
|
|
|
|
|
|
|
|
|
|
// Loop through each MCDwarfLineEntry and encode the dwarf line number table.
|
|
|
|
|
for (const MCDwarfLineEntry &LineEntry : LineEntries) {
|
2021-09-30 17:47:50 -07:00
|
|
|
if (LineEntry.getFlags() & DWARF2_FLAG_END_SEQUENCE) {
|
|
|
|
|
MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, LineEntry.getLabel(),
|
|
|
|
|
AsmInfo->getCodePointerSize());
|
|
|
|
|
FileNum = 1;
|
|
|
|
|
LastLine = 1;
|
|
|
|
|
Column = 0;
|
|
|
|
|
Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
|
|
|
|
|
Isa = 0;
|
|
|
|
|
Discriminator = 0;
|
|
|
|
|
LastLabel = nullptr;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 21:40:54 -07:00
|
|
|
int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
|
|
|
|
|
|
|
|
|
|
if (FileNum != LineEntry.getFileNum()) {
|
|
|
|
|
FileNum = LineEntry.getFileNum();
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_file);
|
|
|
|
|
MCOS->emitULEB128IntValue(FileNum);
|
|
|
|
|
}
|
|
|
|
|
if (Column != LineEntry.getColumn()) {
|
|
|
|
|
Column = LineEntry.getColumn();
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_column);
|
|
|
|
|
MCOS->emitULEB128IntValue(Column);
|
|
|
|
|
}
|
|
|
|
|
if (Discriminator != LineEntry.getDiscriminator() &&
|
|
|
|
|
MCOS->getContext().getDwarfVersion() >= 4) {
|
|
|
|
|
Discriminator = LineEntry.getDiscriminator();
|
|
|
|
|
unsigned Size = getULEB128Size(Discriminator);
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_extended_op);
|
|
|
|
|
MCOS->emitULEB128IntValue(Size + 1);
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
|
|
|
|
|
MCOS->emitULEB128IntValue(Discriminator);
|
|
|
|
|
}
|
|
|
|
|
if (Isa != LineEntry.getIsa()) {
|
|
|
|
|
Isa = LineEntry.getIsa();
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_isa);
|
|
|
|
|
MCOS->emitULEB128IntValue(Isa);
|
|
|
|
|
}
|
|
|
|
|
if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
|
|
|
|
|
Flags = LineEntry.getFlags();
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
|
|
|
|
|
}
|
|
|
|
|
if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
|
|
|
|
|
if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
|
|
|
|
|
if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
|
|
|
|
|
MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
|
|
|
|
|
|
|
|
|
|
MCSymbol *Label = LineEntry.getLabel();
|
|
|
|
|
|
|
|
|
|
// At this point we want to emit/create the sequence to encode the delta
|
|
|
|
|
// in line numbers and the increment of the address from the previous
|
|
|
|
|
// Label and the current Label.
|
|
|
|
|
MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
|
|
|
|
|
AsmInfo->getCodePointerSize());
|
|
|
|
|
Discriminator = 0;
|
|
|
|
|
LastLine = LineEntry.getLine();
|
|
|
|
|
LastLabel = Label;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 17:47:50 -07:00
|
|
|
assert(LastLabel == nullptr && "end of sequence expected");
|
2021-09-01 21:40:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DwarfLineTable::emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
|
|
|
|
|
Optional<MCDwarfLineStr> &LineStr) const {
|
2021-10-11 12:05:34 -07:00
|
|
|
if (!RawData.empty()) {
|
|
|
|
|
assert(MCLineSections.getMCLineEntries().empty() &&
|
|
|
|
|
InputSequences.empty() &&
|
|
|
|
|
"cannot combine raw data with new line entries");
|
|
|
|
|
MCOS->emitLabel(getLabel());
|
|
|
|
|
MCOS->emitBytes(RawData);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 21:40:54 -07:00
|
|
|
MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
|
|
|
|
|
|
|
|
|
|
// Put out the line tables.
|
|
|
|
|
for (const auto &LineSec : MCLineSections.getMCLineEntries())
|
|
|
|
|
emitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
|
|
|
|
|
|
|
|
|
|
// Emit line tables for the original code.
|
2021-09-08 10:22:19 -07:00
|
|
|
emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences);
|
2021-09-01 21:40:54 -07:00
|
|
|
|
|
|
|
|
// This is the end of the section, so set the value of the symbol at the end
|
|
|
|
|
// of this section (that was used in a previous expression).
|
|
|
|
|
MCOS->emitLabel(LineEndSym);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DwarfLineTable::emit(BinaryContext &BC, MCStreamer &Streamer) {
|
|
|
|
|
MCAssembler &Assembler =
|
|
|
|
|
static_cast<MCObjectStreamer *>(&Streamer)->getAssembler();
|
|
|
|
|
|
|
|
|
|
MCDwarfLineTableParams Params = Assembler.getDWARFLinetableParams();
|
|
|
|
|
|
|
|
|
|
auto &LineTables = BC.getDwarfLineTables();
|
|
|
|
|
|
|
|
|
|
// Bail out early so we don't switch to the debug_line section needlessly and
|
|
|
|
|
// in doing so create an unnecessary (if empty) section.
|
|
|
|
|
if (LineTables.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// In a v5 non-split line table, put the strings in a separate section.
|
|
|
|
|
Optional<MCDwarfLineStr> LineStr(None);
|
|
|
|
|
if (BC.Ctx->getDwarfVersion() >= 5)
|
|
|
|
|
LineStr = MCDwarfLineStr(*BC.Ctx);
|
|
|
|
|
|
|
|
|
|
// Switch to the section where the table will be emitted into.
|
|
|
|
|
Streamer.SwitchSection(BC.MOFI->getDwarfLineSection());
|
|
|
|
|
|
|
|
|
|
// Handle the rest of the Compile Units.
|
|
|
|
|
for (auto &CUIDTablePair : LineTables) {
|
|
|
|
|
CUIDTablePair.second.emitCU(&Streamer, Params, LineStr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 15:06:43 -07:00
|
|
|
} // namespace bolt
|
|
|
|
|
} // namespace llvm
|