Skip to content

[llvm-readobj][ELF][RISCV] Dump .note.gnu.property section contents #125642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

mylai-mtk
Copy link
Contributor

RISCV Zicfilp/Zicfiss extensions uses the .note.gnu.property section to store flags indicating the adoption of features based on these extensions. This patch enables the llvm-readobj/llvm-readelf tools to dump these flags with the --note flag.

The patch was modified from PR #77414, which was opened and maintained by my colleague.

@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2025

@llvm/pr-subscribers-backend-risc-v

@llvm/pr-subscribers-llvm-binary-utilities

Author: Ming-Yi Lai (mylai-mtk)

Changes

RISCV Zicfilp/Zicfiss extensions uses the .note.gnu.property section to store flags indicating the adoption of features based on these extensions. This patch enables the llvm-readobj/llvm-readelf tools to dump these flags with the --note flag.

The patch was modified from PR llvm/llvm-project#77414, which was opened and maintained by my colleague.


Full diff: https://github.com/llvm/llvm-project/pull/125642.diff

8 Files Affected:

  • (modified) llvm/include/llvm/BinaryFormat/ELF.h (+8)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-func-sig.s (+37)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-unlabeled.s (+37)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfiss.s (+37)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-func-sig.s (+37)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-unlabeled.s (+37)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfiss.s (+37)
  • (modified) llvm/tools/llvm-readobj/ELFDumper.cpp (+39-13)
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 8853c4a88b0b59..37fb982bbef432 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1798,6 +1798,7 @@ enum : unsigned {
   GNU_PROPERTY_AARCH64_FEATURE_1_AND = 0xc0000000,
   GNU_PROPERTY_AARCH64_FEATURE_PAUTH = 0xc0000001,
   GNU_PROPERTY_X86_FEATURE_1_AND = 0xc0000002,
+  GNU_PROPERTY_RISCV_FEATURE_1_AND = 0xc0000000,
 
   GNU_PROPERTY_X86_UINT32_OR_LO = 0xc0008000,
   GNU_PROPERTY_X86_FEATURE_2_NEEDED = GNU_PROPERTY_X86_UINT32_OR_LO + 1,
@@ -1862,6 +1863,13 @@ enum : unsigned {
   GNU_PROPERTY_X86_ISA_1_V4 = 1 << 3,
 };
 
+// riscv processor feature bits.
+enum : unsigned {
+  GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED = 1 << 0,
+  GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS = 1 << 1,
+  GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG = 1 << 2,
+};
+
 // FreeBSD note types.
 enum {
   NT_FREEBSD_ABI_TAG = 1,
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-func-sig.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-func-sig.s
new file mode 100644
index 00000000000000..1bd9856398dda6
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-func-sig.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFILP-func-sig
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFILP-func-sig
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 4           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG */
+  .p2align 2        /* pr_padding */
+end:
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-unlabeled.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-unlabeled.s
new file mode 100644
index 00000000000000..adf893c5dba9d9
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfilp-unlabeled.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFILP-unlabeled
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFILP-unlabeled
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 1           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED */
+  .p2align 2        /* pr_padding */
+end:
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfiss.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfiss.s
new file mode 100644
index 00000000000000..7ce06ba70daafd
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv32-note-gnu-property-zicfiss.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfiss %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv32-unknown-linux-gnu -mattr=+experimental-zicfiss %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFISS
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFISS
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 2           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS */
+  .p2align 2        /* pr_padding */
+end:
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-func-sig.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-func-sig.s
new file mode 100644
index 00000000000000..b1a39fe35630e0
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-func-sig.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFILP-func-sig
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFILP-func-sig
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 4           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG */
+  .p2align 3        /* pr_padding */
+end:
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-unlabeled.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-unlabeled.s
new file mode 100644
index 00000000000000..175eb08a152ff6
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfilp-unlabeled.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFILP-unlabeled
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFILP-unlabeled
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 1           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED */
+  .p2align 3        /* pr_padding */
+end:
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfiss.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfiss.s
new file mode 100644
index 00000000000000..dc6d8d83e1ce00
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv64-note-gnu-property-zicfiss.s
@@ -0,0 +1,37 @@
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readelf --notes - | FileCheck %s --check-prefix=GNU
+// RUN: llvm-mc -filetype=obj -triple riscv64-unknown-linux-gnu -mattr=+experimental-zicfilp %s -o - | llvm-readobj --notes - | FileCheck %s --check-prefix=LLVM
+
+// GNU: Displaying notes found in: .note.gnu.property
+// GNU-NEXT:   Owner                 Data size	Description
+// GNU-NEXT:   GNU                   0x{{([0-9a-z]{8})}}	NT_GNU_PROPERTY_TYPE_0 (property note)
+// GNU-NEXT:     Properties:    riscv feature: ZICFISS
+
+// LLVM:      NoteSections [
+// LLVM-NEXT:   NoteSection {
+// LLVM-NEXT:     Name: .note.gnu.property
+// LLVM-NEXT:     Offset:
+// LLVM-NEXT:     Size:
+// LLVM-NEXT:     Notes [
+// LLVM-NEXT:       {
+// LLVM-NEXT:         Owner: GNU
+// LLVM-NEXT:         Data size:
+// LLVM-NEXT:         Type: NT_GNU_PROPERTY_TYPE_0 (property note)
+// LLVM-NEXT:         Property [
+// LLVM-NEXT:           riscv feature: ZICFISS
+// LLVM-NEXT:         ]
+// LLVM-NEXT:       }
+// LLVM-NEXT:	    ]
+// LLVM-NEXT:   }
+// LLVM-NEXT: ]
+
+.section ".note.gnu.property", "a"
+  .long 4           /* n_namsz: always 4 (sizeof("GNU")) */
+  .long end - begin /* n_descsz */
+  .long 5           /* n_type: NT_GNU_PROPERTY_TYPE_0 */
+  .asciz "GNU"      /* n_name */
+begin:
+  .long 0xc0000000  /* pr_type: GNU_PROPERTY_RISCV_FEATURE_1_AND */
+  .long 4           /* pr_datasz */
+  .long 2           /* pr_data: GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS */
+  .p2align 3        /* pr_padding */
+end:
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index bfca65aad52b44..c2e74c7c5e7797 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -5333,7 +5333,7 @@ static bool printAArch64PAuthABICoreInfo(raw_ostream &OS, uint32_t DataSize,
 
 template <typename ELFT>
 static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
-                                  ArrayRef<uint8_t> Data) {
+                                  ArrayRef<uint8_t> Data, uint16_t Target) {
   std::string str;
   raw_string_ostream OS(str);
   uint32_t PrData;
@@ -5366,8 +5366,24 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
     return str;
   case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
   case GNU_PROPERTY_X86_FEATURE_1_AND:
-    OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
-                                                        : "x86 feature: ");
+    static_assert(GNU_PROPERTY_AARCH64_FEATURE_1_AND ==
+                  GNU_PROPERTY_RISCV_FEATURE_1_AND,
+                  "GNU_PROPERTY_RISCV_FEATURE_1_AND should equal "
+                  "GNU_PROPERTY_AARCH64_FEATURE_1_AND, otherwise "
+                  "GNU_PROPERTY_RISCV_FEATURE_1_AND would be skipped!");
+
+    if (Target == EM_AARCH64 && Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
+      OS << "aarch64 feature: ";
+    } else if (Target == EM_RISCV && Type == GNU_PROPERTY_RISCV_FEATURE_1_AND) {
+      OS << "riscv feature: ";
+    } else if ((Target == EM_386 || Target == EM_X86_64) &&
+               Type == GNU_PROPERTY_X86_FEATURE_1_AND) {
+      OS << "x86 feature: ";
+    } else {
+      OS << format("<application-specific type 0x%x>", Type);
+      return str;
+    }
+
     if (DataSize != 4) {
       OS << format("<corrupt length: 0x%x>", DataSize);
       return str;
@@ -5377,14 +5393,20 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
       OS << "<None>";
       return str;
     }
-    if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
+
+    if (Target == EM_AARCH64) {
       DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
       DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
       DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS");
+    } else if (Target == EM_RISCV) {
+      DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED, "ZICFILP-unlabeled");
+      DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS, "ZICFISS");
+      DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_FUNC_SIG, "ZICFILP-func-sig");
     } else {
       DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
       DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
     }
+
     if (PrData)
       OS << format("<unknown flags: 0x%x>", PrData);
     return str;
@@ -5441,7 +5463,8 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
 }
 
 template <typename ELFT>
-static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
+static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr,
+                                                      uint16_t Target) {
   using Elf_Word = typename ELFT::Word;
 
   SmallVector<std::string, 4> Properties;
@@ -5459,8 +5482,8 @@ static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
       Properties.push_back(str);
       break;
     }
-    Properties.push_back(
-        getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
+    Properties.push_back(getGNUProperty<ELFT>(
+        Type, DataSize, Arr.take_front(PaddedSize), Target));
     Arr = Arr.drop_front(PaddedSize);
   }
 
@@ -5512,7 +5535,7 @@ static StringRef getDescAsStringRef(ArrayRef<uint8_t> Desc) {
 
 template <typename ELFT>
 static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
-                         ArrayRef<uint8_t> Desc) {
+                         ArrayRef<uint8_t> Desc, uint16_t Target) {
   // Return true if we were able to pretty-print the note, false otherwise.
   switch (NoteType) {
   default:
@@ -5534,7 +5557,7 @@ static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
     break;
   case ELF::NT_GNU_PROPERTY_TYPE_0:
     OS << "    Properties:";
-    for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
+    for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
       OS << "    " << Property << "\n";
     break;
   }
@@ -6223,10 +6246,12 @@ template <class ELFT> void GNUELFDumper<ELFT>::printNotes() {
     else
       OS << "Unknown note type: (" << format_hex(Type, 10) << ")\n";
 
+    const uint16_t Target = this->Obj.getHeader().e_machine;
+
     // Print the description, or fallback to printing raw bytes for unknown
     // owners/if we fail to pretty-print the contents.
     if (Name == "GNU") {
-      if (printGNUNote<ELFT>(OS, Type, Descriptor))
+      if (printGNUNote<ELFT>(OS, Type, Descriptor, Target))
         return Error::success();
     } else if (Name == "FreeBSD") {
       if (std::optional<FreeBSDNote> N =
@@ -7913,7 +7938,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printAddrsig() {
 
 template <typename ELFT>
 static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
-                                  ScopedPrinter &W) {
+                                  ScopedPrinter &W, uint16_t Target) {
   // Return true if we were able to pretty-print the note, false otherwise.
   switch (NoteType) {
   default:
@@ -7938,7 +7963,7 @@ static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
     break;
   case ELF::NT_GNU_PROPERTY_TYPE_0:
     ListScope D(W, "Property");
-    for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
+    for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
       W.printString(Property);
     break;
   }
@@ -8057,10 +8082,11 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
       W.printString("Type",
                     "Unknown (" + to_string(format_hex(Type, 10)) + ")");
 
+    const uint16_t Target = this->Obj.getHeader().e_machine;
     // Print the description, or fallback to printing raw bytes for unknown
     // owners/if we fail to pretty-print the contents.
     if (Name == "GNU") {
-      if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W))
+      if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W, Target))
         return Error::success();
     } else if (Name == "FreeBSD") {
       if (std::optional<FreeBSDNote> N =

Copy link

github-actions bot commented Feb 4, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please combine the tests into a single test file. I don't think you need a separate object for each property value: you can easily have multiple notes in the same input file. For the 32/64 bit variation, take a look how yaml2obj's -D option is used to parameterise values in the YAML. In this case, you could parameterise the endianness value.

@mylai-mtk mylai-mtk force-pushed the rv-readobj-dump-gnu-note branch from 9c935ab to 0866bf1 Compare February 6, 2025 08:37
@mylai-mtk
Copy link
Contributor Author

Update: Address comments

Copy link
Member

@kito-cheng kito-cheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one minor comment

@mylai-mtk
Copy link
Contributor Author

Update: Address comment

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit. Otherwise, LGTM too.

@mylai-mtk mylai-mtk force-pushed the rv-readobj-dump-gnu-note branch from 4b97c08 to 65cc148 Compare February 6, 2025 09:50
@mylai-mtk
Copy link
Contributor Author

Update: Address comment, rebase and squash

RISCV Zicfilp/Zicfiss extensions uses the `.note.gnu.property` section to store
flags indicating the adoption of features based on these extensions. This
patch enables the llvm-readobj/llvm-readelf tools to dump these flags with the
`--notes` flag
@mylai-mtk mylai-mtk force-pushed the rv-readobj-dump-gnu-note branch from 65cc148 to 5e850a2 Compare February 7, 2025 03:40
@mylai-mtk
Copy link
Contributor Author

Update: Address comment and rebase

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mylai-mtk mylai-mtk merged commit a1984ec into llvm:main Feb 7, 2025
8 checks passed
@mylai-mtk mylai-mtk deleted the rv-readobj-dump-gnu-note branch February 7, 2025 05:55
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…lvm#125642)

RISCV Zicfilp/Zicfiss extensions uses the `.note.gnu.property` section
to store flags indicating the adoption of features based on these
extensions. This patch enables the llvm-readobj/llvm-readelf tools to
dump these flags with the `--note` flag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants