Skip to content

Commit 6847cf4

Browse files
committed
[llvm-readobj] Parse .note.gnu.property in RISCV for Zicfiss/Zicfilip(CFI extension)
1 parent ead018e commit 6847cf4

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,7 +5090,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printAddrsig() {
50905090

50915091
template <typename ELFT>
50925092
static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
5093-
ArrayRef<uint8_t> Data) {
5093+
ArrayRef<uint8_t> Data, uint16_t Target) {
50945094
std::string str;
50955095
raw_string_ostream OS(str);
50965096
uint32_t PrData;
@@ -5123,8 +5123,30 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
51235123
return OS.str();
51245124
case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
51255125
case GNU_PROPERTY_X86_FEATURE_1_AND:
5126-
OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
5127-
: "x86 feature: ");
5126+
5127+
if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
5128+
if (Target == ELF::EM_RISCV)
5129+
OS << "riscv feature: ";
5130+
else
5131+
OS << "aarch64 feature: ";
5132+
} else {
5133+
OS << "x86 feature: ";
5134+
}
5135+
// TODO Should we spilt
5136+
// GNU_PROPERTY_AARCH64_FEATURE_1_AND and
5137+
// GNU_PROPERTY_RISCV_FEATURE_1_AND to different label ?
5138+
/*switch (Type) {
5139+
default:
5140+
OS << "x86 feature: ";
5141+
break;
5142+
case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
5143+
OS << "aarch64 feature: ";
5144+
break;
5145+
case GNU_PROPERTY_RISCV_FEATURE_1_AND:
5146+
OS << "riscv feature: ";
5147+
break;
5148+
}*/
5149+
51285150
if (DataSize != 4) {
51295151
OS << format("<corrupt length: 0x%x>", DataSize);
51305152
return OS.str();
@@ -5134,13 +5156,38 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
51345156
OS << "<None>";
51355157
return OS.str();
51365158
}
5159+
51375160
if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
5138-
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
5139-
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
5161+
if (Target == ELF::EM_RISCV) {
5162+
DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP, "ZICFILP");
5163+
DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS, "ZICFISS");
5164+
} else {
5165+
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
5166+
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
5167+
}
51405168
} else {
51415169
DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
51425170
DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
51435171
}
5172+
5173+
// TODO Should we spilt
5174+
// GNU_PROPERTY_AARCH64_FEATURE_1_AND and
5175+
// GNU_PROPERTY_RISCV_FEATURE_1_AND to different label ?
5176+
/*switch (Type) {
5177+
default:
5178+
DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
5179+
DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
5180+
break;
5181+
case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
5182+
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
5183+
DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
5184+
break;
5185+
case GNU_PROPERTY_RISCV_FEATURE_1_AND:
5186+
DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP, "ZICFILP");
5187+
DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS, "ZICFISS");
5188+
break;
5189+
}*/
5190+
51445191
if (PrData)
51455192
OS << format("<unknown flags: 0x%x>", PrData);
51465193
return OS.str();
@@ -5194,7 +5241,7 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
51945241
}
51955242

51965243
template <typename ELFT>
5197-
static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
5244+
static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr, uint16_t Target) {
51985245
using Elf_Word = typename ELFT::Word;
51995246

52005247
SmallVector<std::string, 4> Properties;
@@ -5213,7 +5260,7 @@ static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
52135260
break;
52145261
}
52155262
Properties.push_back(
5216-
getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
5263+
getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize), Target));
52175264
Arr = Arr.drop_front(PaddedSize);
52185265
}
52195266

@@ -5265,7 +5312,7 @@ static StringRef getDescAsStringRef(ArrayRef<uint8_t> Desc) {
52655312

52665313
template <typename ELFT>
52675314
static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
5268-
ArrayRef<uint8_t> Desc) {
5315+
ArrayRef<uint8_t> Desc, uint16_t Target) {
52695316
// Return true if we were able to pretty-print the note, false otherwise.
52705317
switch (NoteType) {
52715318
default:
@@ -5287,7 +5334,7 @@ static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
52875334
break;
52885335
case ELF::NT_GNU_PROPERTY_TYPE_0:
52895336
OS << " Properties:";
5290-
for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
5337+
for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
52915338
OS << " " << Property << "\n";
52925339
break;
52935340
}
@@ -5976,10 +6023,12 @@ template <class ELFT> void GNUELFDumper<ELFT>::printNotes() {
59766023
else
59776024
OS << "Unknown note type: (" << format_hex(Type, 10) << ")\n";
59786025

6026+
uint16_t Target = this->Obj.getHeader().e_machine;
6027+
59796028
// Print the description, or fallback to printing raw bytes for unknown
59806029
// owners/if we fail to pretty-print the contents.
59816030
if (Name == "GNU") {
5982-
if (printGNUNote<ELFT>(OS, Type, Descriptor))
6031+
if (printGNUNote<ELFT>(OS, Type, Descriptor, Target))
59836032
return Error::success();
59846033
} else if (Name == "FreeBSD") {
59856034
if (std::optional<FreeBSDNote> N =
@@ -7535,7 +7584,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printAddrsig() {
75357584

75367585
template <typename ELFT>
75377586
static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
7538-
ScopedPrinter &W) {
7587+
ScopedPrinter &W, uint16_t Target) {
75397588
// Return true if we were able to pretty-print the note, false otherwise.
75407589
switch (NoteType) {
75417590
default:
@@ -7560,7 +7609,7 @@ static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
75607609
break;
75617610
case ELF::NT_GNU_PROPERTY_TYPE_0:
75627611
ListScope D(W, "Property");
7563-
for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
7612+
for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
75647613
W.printString(Property);
75657614
break;
75667615
}
@@ -7673,10 +7722,11 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
76737722
W.printString("Type",
76747723
"Unknown (" + to_string(format_hex(Type, 10)) + ")");
76757724

7725+
uint16_t Target = this->Obj.getHeader().e_machine;
76767726
// Print the description, or fallback to printing raw bytes for unknown
76777727
// owners/if we fail to pretty-print the contents.
76787728
if (Name == "GNU") {
7679-
if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W))
7729+
if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W, Target))
76807730
return Error::success();
76817731
} else if (Name == "FreeBSD") {
76827732
if (std::optional<FreeBSDNote> N =

0 commit comments

Comments
 (0)