Skip to content

Commit dc96cd3

Browse files
committed
Correctly update .debug_addr DWARF section.
This is a minimal patch to add support for also updating the addresses stored in the .debug_addr DWARF section, which is used to perform address resolution at debug time with debug fission. This is minimal in the sence that it only supports the pre-DWARFv5 .debug_addr format that is emitted by Emscripten currently, and which only consists of a simple list of addresses. Ideally the copy of DWARFYAML in the Binaryen tree should be updated with LLVM ToT at some point, which will provide full support for DWARFv5 index tables, including the new .debug_addr format. Ref: #3460 Ref: emscripten-core/emscripten#13099 Bug: https://crbug.com/1161422
1 parent dc4288c commit dc96cd3

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

src/wasm/wasm-debug.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,19 @@ static void updateLoc(llvm::DWARFYAML::Data& yaml,
10451045
}
10461046
}
10471047

1048+
static void updateAddr(llvm::DWARFYAML::Data& yaml,
1049+
const LocationUpdater& locationUpdater) {
1050+
for (auto& addrTable : yaml.DebugAddr) {
1051+
for (auto& addr : addrTable.Addrs) {
1052+
BinaryLocation start = addr, newStart = 0;
1053+
if (!isTombstone(start)) {
1054+
newStart = locationUpdater.getNewStart(start);
1055+
}
1056+
addr = newStart;
1057+
}
1058+
}
1059+
}
1060+
10481061
void writeDWARFSections(Module& wasm, const BinaryLocations& newLocations) {
10491062
BinaryenDWARFInfo info(wasm);
10501063

@@ -1064,6 +1077,8 @@ void writeDWARFSections(Module& wasm, const BinaryLocations& newLocations) {
10641077

10651078
updateLoc(data, locationUpdater);
10661079

1080+
updateAddr(data, locationUpdater);
1081+
10671082
// Convert to binary sections.
10681083
auto newSections =
10691084
EmitDebugSections(data, false /* EmitFixups for debug_info */);

third_party/llvm-project/DWARFEmitter.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ void DWARFYAML::EmitDebugLoc(raw_ostream &OS, const DWARFYAML::Data &DI) {
148148
}
149149
}
150150

151+
// XXX BINARYEN
152+
void DWARFYAML::EmitDebugAddr(raw_ostream &OS, const DWARFYAML::Data &DI) {
153+
// This is the legacy DWARF extension .debug_addr format, which is
154+
// only a simple list of addresses. This doesn't support DWARF5.
155+
for (auto AddrTable : DI.DebugAddr) {
156+
for (auto Addr : AddrTable.Addrs) {
157+
writeInteger((uint32_t)Addr, OS, DI.IsLittleEndian);
158+
}
159+
}
160+
}
161+
151162
void DWARFYAML::EmitPubSection(raw_ostream &OS,
152163
const DWARFYAML::PubSection &Sect,
153164
bool IsLittleEndian) {
@@ -485,6 +496,8 @@ EmitDebugSections(llvm::DWARFYAML::Data &DI, bool ApplyFixups) {
485496
DebugSections); // XXX BINARYEN
486497
EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLoc, "debug_loc",
487498
DebugSections); // XXX BINARYEN
499+
EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAddr, "debug_addr",
500+
DebugSections); // XXX BINARYEN
488501
return std::move(DebugSections);
489502
}
490503
} // namespace DWARFYAML

third_party/llvm-project/dwarf2yaml.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Error.h"
1010
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
11+
#include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h"
1112
#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
1213
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
1314
#include "llvm/ObjectYAML/DWARFYAML.h"
@@ -89,6 +90,31 @@ void dumpDebugARanges(DWARFContext &DCtx, DWARFYAML::Data &Y) {
8990
}
9091
}
9192

93+
void dumpDebugAddr(DWARFContext &DCtx, DWARFYAML::Data &Y) { // XXX BINARYEN
94+
DWARFDebugAddrTable AddrTable;
95+
DWARFDataExtractor AddrData(DCtx.getDWARFObj(),
96+
DCtx.getDWARFObj().getAddrSection(),
97+
DCtx.isLittleEndian(), /*AddrSize=*/0);
98+
std::vector<DWARFYAML::AddrTable> AddrTables;
99+
uint64_t Offset = 0;
100+
while (AddrData.isValidOffset(Offset)) {
101+
// We ignore any errors that don't prevent parsing the section, since we can
102+
// still represent such sections.
103+
if (Error E = AddrTable.extract(AddrData, &Offset, DCtx.getMaxVersion(), DCtx.getDWARFObj().getAddressSize(), consumeError)) {
104+
errs() << toString(std::move(E)) << '\n';
105+
break;
106+
}
107+
AddrTables.emplace_back();
108+
for (uint64_t Addr : AddrTable.getAddressEntries()) {
109+
// Currently, the parser doesn't support parsing an address table with non
110+
// linear addresses (segment_selector_size != 0). The segment selectors
111+
// are specified to be zero.
112+
AddrTables.back().Addrs.push_back(Addr);
113+
}
114+
}
115+
Y.DebugAddr = std::move(AddrTables);
116+
}
117+
92118
void dumpDebugRanges(DWARFContext &DCtx, DWARFYAML::Data &Y) { // XXX BINARYEN
93119
uint8_t savedAddressByteSize = 4;
94120
DWARFDataExtractor rangesData(DCtx.getDWARFObj(), DCtx.getDWARFObj().getRangesSection(),
@@ -427,6 +453,7 @@ std::error_code dwarf2yaml(DWARFContext &DCtx, DWARFYAML::Data &Y) {
427453
dumpDebugARanges(DCtx, Y);
428454
dumpDebugRanges(DCtx, Y); // XXX BINARYEN
429455
dumpDebugLoc(DCtx, Y); // XXX BINARYEN
456+
dumpDebugAddr(DCtx, Y); // XXX BINARYEN
430457
dumpDebugPubSections(DCtx, Y);
431458
dumpDebugInfo(DCtx, Y);
432459
dumpDebugLines(DCtx, Y);

third_party/llvm-project/include/llvm/DebugInfo/DWARF/DWARFDebugAddr.h

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class DWARFDebugAddrTable {
9090

9191
/// Returns the length of the array of addresses.
9292
uint32_t getDataSize() const;
93+
94+
/// Return the parsed addresses of this table.
95+
ArrayRef<uint64_t> getAddressEntries() const { return Addrs; }
9396
};
9497

9598
} // end namespace llvm

third_party/llvm-project/include/llvm/ObjectYAML/DWARFEmitter.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void EmitDebugStr(raw_ostream &OS, const Data &DI);
3434
void EmitDebugAranges(raw_ostream &OS, const Data &DI);
3535
void EmitDebugRanges(raw_ostream &OS, const Data &DI); // XXX BINARYEN
3636
void EmitDebugLoc(raw_ostream &OS, const Data &DI); // XXX BINARYEN
37+
void EmitDebugAddr(raw_ostream &OS, const Data &DI); // XXX BINARYEN
3738
void EmitPubSection(raw_ostream &OS, const PubSection &Sect,
3839
bool IsLittleEndian);
3940
void EmitDebugInfo(raw_ostream &OS, const Data &DI);

third_party/llvm-project/include/llvm/ObjectYAML/DWARFYAML.h

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ struct Loc {
9494
std::vector<uint8_t> Location;
9595
uint64_t CompileUnitOffset;
9696
};
97+
98+
struct AddrTable {
99+
std::vector<uint64_t> Addrs;
100+
};
97101
// XXX BINARYEN -->
98102

99103
struct PubEntry {
@@ -173,6 +177,7 @@ struct Data {
173177
std::vector<ARange> ARanges;
174178
std::vector<Range> Ranges; // XXX BINARYEN
175179
std::vector<Loc> Locs; // XXX BINARYEN
180+
std::vector<AddrTable> DebugAddr; // XXX BINARYEN
176181
PubSection PubNames;
177182
PubSection PubTypes;
178183

@@ -195,6 +200,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARangeDescriptor)
195200
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARange)
196201
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Range) // XXX BINARYEN
197202
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Loc) // XXX BINARYEN
203+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::AddrTable) // XXX BINARYEN
198204
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::PubEntry)
199205
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Unit)
200206
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::FormValue)
@@ -234,6 +240,10 @@ template <> struct MappingTraits<DWARFYAML::Loc> { // XXX BINARYEN
234240
static void mapping(IO &IO, DWARFYAML::Loc &Loc);
235241
};
236242

243+
template <> struct MappingTraits<DWARFYAML::AddrTable> { // XXX BINARYEN
244+
static void mapping(IO &IO, DWARFYAML::AddrTable &AddrTable);
245+
};
246+
237247
template <> struct MappingTraits<DWARFYAML::PubEntry> {
238248
static void mapping(IO &IO, DWARFYAML::PubEntry &Entry);
239249
};

0 commit comments

Comments
 (0)