Skip to content

Commit b13bd03

Browse files
committed
[llvm-readobj] Fixes malformed json on JSON printed corefiles
This patch fixes an issue where, when printing corefile notes with llvm-readobj as json, the dumper generated llvm formatted output which isn't valid json. This alters the dumper to, in the NT_FILE, note use a JSON particular method and dump properly formatted json data.
1 parent 50be0b1 commit b13bd03

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

llvm/test/tools/llvm-readobj/ELF/note-core-ntfile.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# RUN: yaml2obj %s -o %t.o
44
# RUN: llvm-readelf --notes %t.o | FileCheck %s --check-prefix=GNU
55
# RUN: llvm-readobj --notes %t.o | FileCheck %s --check-prefix=LLVM
6+
# RUN: llvm-readelf --elf-output-style=JSON --pretty-print --notes %t.o | FileCheck %s --check-prefix=JSON
7+
# RUN: llvm-readobj --elf-output-style=JSON --pretty-print --notes %t.o | FileCheck %s --check-prefix=JSON
68

79
## llvm-mc doesn't support generating ET_CORE files; the 'Content' field was
810
## generated with the following steps:
@@ -93,3 +95,39 @@ ProgramHeaders:
9395
# LLVM-NEXT: }
9496
# LLVM-NEXT: }
9597
# LLVM-NEXT: ]
98+
99+
# JSON: "Notes": [
100+
# JSON-NEXT: {
101+
# JSON-NEXT: "NoteSection": {
102+
# JSON-NEXT: "Name": "<?>",
103+
# JSON-NEXT: "Offset": 120,
104+
# JSON-NEXT: "Size": 148,
105+
# JSON-NEXT: "Note": {
106+
# JSON-NEXT: "Owner": "CORE",
107+
# JSON-NEXT: "Data size": 128,
108+
# JSON-NEXT: "Type": "NT_FILE (mapped files)",
109+
# JSON-NEXT: "Page Size": 4096,
110+
# JSON-NEXT: "Mappings": [
111+
# JSON-NEXT: {
112+
# JSON-NEXT: "Start": 4096,
113+
# JSON-NEXT: "End": 8192,
114+
# JSON-NEXT: "Offset": 12288,
115+
# JSON-NEXT: "Filename": "/path/to/a.out"
116+
# JSON-NEXT: },
117+
# JSON-NEXT: {
118+
# JSON-NEXT: "Start": 16384,
119+
# JSON-NEXT: "End": 20480,
120+
# JSON-NEXT: "Offset": 24576,
121+
# JSON-NEXT: "Filename": "/path/to/libc.so"
122+
# JSON-NEXT: },
123+
# JSON-NEXT: {
124+
# JSON-NEXT: "Start": 28672,
125+
# JSON-NEXT: "End": 32768,
126+
# JSON-NEXT: "Offset": 36864,
127+
# JSON-NEXT: "Filename": "[stack]"
128+
# JSON-NEXT: }
129+
# JSON-NEXT: ]
130+
# JSON-NEXT: }
131+
# JSON-NEXT: }
132+
# JSON-NEXT: }
133+
# JSON-NEXT: ]

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ struct GroupSection {
180180
std::vector<GroupMember> Members;
181181
};
182182

183+
struct CoreFileMapping {
184+
uint64_t Start, End, Offset;
185+
StringRef Filename;
186+
};
187+
188+
struct CoreNote {
189+
uint64_t PageSize;
190+
std::vector<CoreFileMapping> Mappings;
191+
};
192+
183193
namespace {
184194

185195
struct NoteType {
@@ -762,6 +772,7 @@ template <typename ELFT> class LLVMELFDumper : public ELFDumper<ELFT> {
762772
const unsigned SecNdx);
763773
virtual void printSectionGroupMembers(StringRef Name, uint64_t Idx) const;
764774
virtual void printEmptyGroupMessage() const;
775+
virtual void printCoreNote(const CoreNote &Note, ScopedPrinter &W) const;
765776

766777
ScopedPrinter &W;
767778
};
@@ -793,6 +804,8 @@ template <typename ELFT> class JSONELFDumper : public LLVMELFDumper<ELFT> {
793804

794805
void printEmptyGroupMessage() const override;
795806

807+
void printCoreNote(const CoreNote &Note, ScopedPrinter &W) const override;
808+
796809
private:
797810
std::unique_ptr<DictScope> FileScope;
798811
};
@@ -5736,16 +5749,6 @@ static AMDGPUNote getAMDGPUNote(uint32_t NoteType, ArrayRef<uint8_t> Desc) {
57365749
}
57375750
}
57385751

5739-
struct CoreFileMapping {
5740-
uint64_t Start, End, Offset;
5741-
StringRef Filename;
5742-
};
5743-
5744-
struct CoreNote {
5745-
uint64_t PageSize;
5746-
std::vector<CoreFileMapping> Mappings;
5747-
};
5748-
57495752
static Expected<CoreNote> readCoreNote(DataExtractor Desc) {
57505753
// Expected format of the NT_FILE note description:
57515754
// 1. # of file mappings (call it N)
@@ -7838,7 +7841,9 @@ static bool printLLVMOMPOFFLOADNoteLLVMStyle(uint32_t NoteType,
78387841
return true;
78397842
}
78407843

7841-
static void printCoreNoteLLVMStyle(const CoreNote &Note, ScopedPrinter &W) {
7844+
template <class ELFT>
7845+
void LLVMELFDumper<ELFT>::printCoreNote(const CoreNote &Note,
7846+
ScopedPrinter &W) const {
78427847
W.printNumber("Page Size", Note.PageSize);
78437848
for (const CoreFileMapping &Mapping : Note.Mappings) {
78447849
ListScope D(W, "Mapping");
@@ -7849,6 +7854,20 @@ static void printCoreNoteLLVMStyle(const CoreNote &Note, ScopedPrinter &W) {
78497854
}
78507855
}
78517856

7857+
template <class ELFT>
7858+
void JSONELFDumper<ELFT>::printCoreNote(const CoreNote &Note,
7859+
ScopedPrinter &W) const {
7860+
W.printNumber("Page Size", Note.PageSize);
7861+
ListScope D(W, "Mappings");
7862+
for (const CoreFileMapping &Mapping : Note.Mappings) {
7863+
auto MappingDict = std::make_unique<DictScope>(W);
7864+
W.printHex("Start", Mapping.Start);
7865+
W.printHex("End", Mapping.End);
7866+
W.printHex("Offset", Mapping.Offset);
7867+
W.printString("Filename", Mapping.Filename);
7868+
}
7869+
}
7870+
78527871
template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
78537872
ListScope L(W, "Notes");
78547873

@@ -7916,7 +7935,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
79167935
Descriptor, ELFT::Endianness == llvm::endianness::little,
79177936
sizeof(Elf_Addr));
79187937
if (Expected<CoreNote> N = readCoreNote(DescExtractor)) {
7919-
printCoreNoteLLVMStyle(*N, W);
7938+
printCoreNote(*N, W);
79207939
return Error::success();
79217940
} else {
79227941
return N.takeError();

0 commit comments

Comments
 (0)