Skip to content

Commit ea7db62

Browse files
committed
[ObjectYAML][DWARF] Make the PubSection optional.
This patch helps make the `PubSection` optional in the DWARF structure. Reviewed By: jhenderson, aprantl Differential Revision: https://reviews.llvm.org/D80722
1 parent 20b2af3 commit ea7db62

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-28
lines changed

llvm/include/llvm/ObjectYAML/DWARFYAML.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ struct PubSection {
9999
uint32_t UnitSize;
100100
bool IsGNUStyle = false;
101101
std::vector<PubEntry> Entries;
102+
103+
PubSection() = default;
104+
PubSection(bool IsGNUStyle) : IsGNUStyle(IsGNUStyle) {}
102105
};
103106

104107
struct FormValue {
@@ -161,11 +164,11 @@ struct Data {
161164
std::vector<StringRef> DebugStrings;
162165
std::vector<ARange> ARanges;
163166
std::vector<Ranges> DebugRanges;
164-
PubSection PubNames;
165-
PubSection PubTypes;
167+
Optional<PubSection> PubNames;
168+
Optional<PubSection> PubTypes;
166169

167-
PubSection GNUPubNames;
168-
PubSection GNUPubTypes;
170+
Optional<PubSection> GNUPubNames;
171+
Optional<PubSection> GNUPubTypes;
169172

170173
std::vector<Unit> CompileUnits;
171174

llvm/lib/ObjectYAML/DWARFYAML.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ namespace llvm {
1717

1818
bool DWARFYAML::Data::isEmpty() const {
1919
return DebugStrings.empty() && AbbrevDecls.empty() && ARanges.empty() &&
20-
DebugRanges.empty() && PubNames.Entries.empty() &&
21-
PubTypes.Entries.empty() && GNUPubNames.Entries.empty() &&
22-
GNUPubTypes.Entries.empty() && CompileUnits.empty() &&
23-
DebugLines.empty();
20+
DebugRanges.empty() && !PubNames && !PubTypes && !GNUPubNames &&
21+
!GNUPubTypes && CompileUnits.empty() && DebugLines.empty();
2422
}
2523

2624
SetVector<StringRef> DWARFYAML::Data::getUsedSectionNames() const {
@@ -41,14 +39,10 @@ void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
4139
IO.mapOptional("debug_aranges", DWARF.ARanges);
4240
if (!DWARF.DebugRanges.empty() || !IO.outputting())
4341
IO.mapOptional("debug_ranges", DWARF.DebugRanges);
44-
if (!DWARF.PubNames.Entries.empty() || !IO.outputting())
45-
IO.mapOptional("debug_pubnames", DWARF.PubNames);
46-
if (!DWARF.PubTypes.Entries.empty() || !IO.outputting())
47-
IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
48-
if (!DWARF.GNUPubNames.Entries.empty() || !IO.outputting())
49-
IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
50-
if (!DWARF.GNUPubTypes.Entries.empty() || !IO.outputting())
51-
IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
42+
IO.mapOptional("debug_pubnames", DWARF.PubNames);
43+
IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
44+
IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
45+
IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
5246
IO.mapOptional("debug_info", DWARF.CompileUnits);
5347
IO.mapOptional("debug_line", DWARF.DebugLines);
5448
IO.setContext(&oldContext);

llvm/lib/ObjectYAML/MachOEmitter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,13 @@ Error MachOWriter::writeSectionData(raw_ostream &OS) {
295295
} else if (0 == strncmp(&Sec.sectname[0], "__debug_ranges", 16)) {
296296
DWARFYAML::EmitDebugRanges(OS, Obj.DWARF);
297297
} else if (0 == strncmp(&Sec.sectname[0], "__debug_pubnames", 16)) {
298-
DWARFYAML::EmitPubSection(OS, Obj.DWARF.PubNames,
299-
Obj.IsLittleEndian);
298+
if (Obj.DWARF.PubNames)
299+
DWARFYAML::EmitPubSection(OS, *Obj.DWARF.PubNames,
300+
Obj.IsLittleEndian);
300301
} else if (0 == strncmp(&Sec.sectname[0], "__debug_pubtypes", 16)) {
301-
DWARFYAML::EmitPubSection(OS, Obj.DWARF.PubTypes,
302-
Obj.IsLittleEndian);
302+
if (Obj.DWARF.PubTypes)
303+
DWARFYAML::EmitPubSection(OS, *Obj.DWARF.PubTypes,
304+
Obj.IsLittleEndian);
303305
} else if (0 == strncmp(&Sec.sectname[0], "__debug_info", 16)) {
304306
DWARFYAML::EmitDebugInfo(OS, Obj.DWARF);
305307
} else if (0 == strncmp(&Sec.sectname[0], "__debug_line", 16)) {

llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## This file contains test cases for generating .debug_pubnames/.debug_pubtypes
2+
## section in object files from the DWARF entry of Mach-O YAML inputs
3+
4+
## a) Test that yaml2obj emits the .debug_pubnames and .debug_pubtypes sections and
5+
## obj2yaml converts them back.
6+
17
# RUN: yaml2obj %s | obj2yaml | FileCheck %s
28

39
--- !mach-o
@@ -345,3 +351,87 @@ DWARF:
345351
#CHECK: - DieOffset: 0x00000071
346352
#CHECK: Name: char
347353
#CHECK: ...
354+
355+
## b) Test that yaml2obj will not emit the .debug_pubnames/.debug_pubtypes section's
356+
## contents, if the "debug_pubnames"/"debug_pubtypes" entry doesn't exist in the
357+
## "DWARF" entry.
358+
359+
# RUN: yaml2obj --docnum=2 %s | obj2yaml | FileCheck %s --check-prefix=EMPTY
360+
361+
# EMPTY: Sections:
362+
# EMPTY-NEXT: - sectname: __debug_pubnames
363+
# EMPTY-NEXT: segname: __DWARF
364+
# EMPTY-NEXT: addr: 0x0000000000000000
365+
# EMPTY-NEXT: size: 0
366+
# EMPTY-NEXT: offset: 0x00000000
367+
# EMPTY-NEXT: align: 0
368+
# EMPTY-NEXT: reloff: 0x00000000
369+
# EMPTY-NEXT: nreloc: 0
370+
# EMPTY-NEXT: flags: 0x00000000
371+
# EMPTY-NEXT: reserved1: 0x00000000
372+
# EMPTY-NEXT: reserved2: 0x00000000
373+
# EMPTY-NEXT: reserved3: 0x00000000
374+
# EMPTY-NEXT: content: ''
375+
# EMPTY-NEXT: - sectname: __debug_pubtypes
376+
# EMPTY-NEXT: segname: __DWARF
377+
# EMPTY-NEXT: addr: 0x0000000000000000
378+
# EMPTY-NEXT: size: 0
379+
# EMPTY-NEXT: offset: 0x00000720
380+
# EMPTY-NEXT: align: 0
381+
# EMPTY-NEXT: reloff: 0x00000000
382+
# EMPTY-NEXT: nreloc: 0
383+
# EMPTY-NEXT: flags: 0x00000000
384+
# EMPTY-NEXT: reserved1: 0x00000000
385+
# EMPTY-NEXT: reserved2: 0x00000000
386+
# EMPTY-NEXT: reserved3: 0x00000000
387+
# EMPTY-NEXT: content: ''
388+
# EMPTY-NEXT: ...
389+
390+
--- !mach-o
391+
FileHeader:
392+
magic: 0xFEEDFACF
393+
cputype: 0x01000007
394+
cpusubtype: 0x00000003
395+
filetype: 0x0000000A
396+
ncmds: 1
397+
sizeofcmds: 1800
398+
flags: 0x00000000
399+
reserved: 0x00000000
400+
LoadCommands:
401+
- cmd: LC_SEGMENT_64
402+
cmdsize: 232
403+
segname: __DWARF
404+
vmaddr: 0x00000000
405+
vmsize: 0x00000000
406+
fileoff: 0
407+
filesize: 0
408+
maxprot: 0
409+
initprot: 0
410+
nsects: 2
411+
flags: 0
412+
Sections:
413+
- sectname: __debug_pubnames
414+
segname: __DWARF
415+
addr: 0x0000000000000000
416+
size: 0
417+
offset: 0x00000000
418+
align: 0
419+
reloff: 0x00000000
420+
nreloc: 0
421+
flags: 0x00000000
422+
reserved1: 0x00000000
423+
reserved2: 0x00000000
424+
reserved3: 0x00000000
425+
- sectname: __debug_pubtypes
426+
segname: __DWARF
427+
addr: 0x0000000000000000
428+
size: 0
429+
offset: 0x00000720
430+
align: 0
431+
reloff: 0x00000000
432+
nreloc: 0
433+
flags: 0x00000000
434+
reserved1: 0x00000000
435+
reserved2: 0x00000000
436+
reserved3: 0x00000000
437+
DWARF:

llvm/tools/obj2yaml/dwarf2yaml.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
1212
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1313
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
14+
#include "llvm/DebugInfo/DWARF/DWARFSection.h"
1415
#include "llvm/ObjectYAML/DWARFYAML.h"
1516

1617
#include <algorithm>
@@ -135,17 +136,32 @@ void dumpPubSection(DWARFContext &DCtx, DWARFYAML::PubSection &Y,
135136

136137
void dumpDebugPubSections(DWARFContext &DCtx, DWARFYAML::Data &Y) {
137138
const DWARFObject &D = DCtx.getDWARFObj();
138-
Y.PubNames.IsGNUStyle = false;
139-
dumpPubSection(DCtx, Y.PubNames, D.getPubnamesSection());
140139

141-
Y.PubTypes.IsGNUStyle = false;
142-
dumpPubSection(DCtx, Y.PubTypes, D.getPubtypesSection());
140+
const DWARFSection PubNames = D.getPubnamesSection();
141+
if (!PubNames.Data.empty()) {
142+
Y.PubNames.emplace(/*IsGNUStyle=*/false);
143+
dumpPubSection(DCtx, *Y.PubNames, PubNames);
144+
}
143145

144-
Y.GNUPubNames.IsGNUStyle = true;
145-
dumpPubSection(DCtx, Y.GNUPubNames, D.getGnuPubnamesSection());
146+
const DWARFSection PubTypes = D.getPubtypesSection();
147+
if (!PubTypes.Data.empty()) {
148+
Y.PubTypes.emplace(/*IsGNUStyle=*/false);
149+
dumpPubSection(DCtx, *Y.PubTypes, PubTypes);
150+
}
146151

147-
Y.GNUPubTypes.IsGNUStyle = true;
148-
dumpPubSection(DCtx, Y.GNUPubTypes, D.getGnuPubtypesSection());
152+
const DWARFSection GNUPubNames = D.getGnuPubnamesSection();
153+
if (!GNUPubNames.Data.empty()) {
154+
// TODO: Test dumping .debug_gnu_pubnames section.
155+
Y.GNUPubNames.emplace(/*IsGNUStyle=*/true);
156+
dumpPubSection(DCtx, *Y.GNUPubNames, GNUPubNames);
157+
}
158+
159+
const DWARFSection GNUPubTypes = D.getGnuPubtypesSection();
160+
if (!GNUPubTypes.Data.empty()) {
161+
// TODO: Test dumping .debug_gnu_pubtypes section.
162+
Y.GNUPubTypes.emplace(/*IsGNUStyle=*/true);
163+
dumpPubSection(DCtx, *Y.GNUPubTypes, GNUPubTypes);
164+
}
149165
}
150166

151167
void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {

0 commit comments

Comments
 (0)