[PATCH] D152065: [ELF] Add PT_RISCV_ATTRIBUTES

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 3 07:29:03 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: asb, jrtc27, kito-cheng, peter.smith, psykose.
Herald added subscribers: luke, VincentWu, vkmr, frasercrmck, jdoerfert, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, arichardson, emaste.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD.
Herald added a project: LLVM.

Close https://github.com/llvm/llvm-project/issues/63084

Unlike AArch32, RISC-V defines PT_RISCV_ATTRIBUTES to include the
SHT_RISCV_ATTRIBUTES section. There is no real-world use case yet.

Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/71


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152065

Files:
  lld/ELF/Writer.cpp
  lld/docs/ReleaseNotes.rst
  lld/test/ELF/riscv-attributes.s


Index: lld/test/ELF/riscv-attributes.s
===================================================================
--- lld/test/ELF/riscv-attributes.s
+++ lld/test/ELF/riscv-attributes.s
@@ -3,7 +3,7 @@
 # RUN: rm -rf %t && split-file %s %t && cd %t
 # RUN: llvm-mc -filetype=obj -triple=riscv64 a.s -o a.o
 # RUN: ld.lld -e 0 a.o -o out 2>&1 | count 0
-# RUN: llvm-readobj --arch-specific out | FileCheck %s
+# RUN: llvm-readelf -S -l --arch-specific out | FileCheck %s --check-prefixes=PHDR,CHECK
 # RUN: ld.lld -e 0 a.o a.o -o out1 2>&1 | count 0
 # RUN: llvm-readobj --arch-specific out1 | FileCheck %s
 # RUN: ld.lld -r a.o a.o -o out1 2>&1 | count 0
@@ -63,6 +63,14 @@
 # UNKNOWN22-COUNT-2: warning: unknown22.o:(.riscv.attributes): invalid tag 0x16 at offset 0x10
 # UNKNOWN22:         warning: unknown22a.o:(.riscv.attributes): invalid tag 0x16 at offset 0x10
 
+# HDR:      Name              Type             Address          Off    Size   ES Flg Lk Inf Al
+# HDR:      .riscv.attributes RISCV_ATTRIBUTES 0000000000000000 000158 00003e 00      0   0  1{{$}}
+
+# HDR:      Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
+# HDR:      LOAD           0x000000 0x0000000000010000 0x0000000000010000 0x000158 0x000158 R   0x1000
+# HDR-NEXT: GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0
+# HDR-NEXT: ATTRIBUTES     0x000158 0x0000000000000000 0x0000000000000000 0x00003e 0x00003e R   0x1{{$}}
+
 # CHECK:      BuildAttributes {
 # CHECK-NEXT:   FormatVersion: 0x41
 # CHECK-NEXT:   Section 1 {
Index: lld/docs/ReleaseNotes.rst
===================================================================
--- lld/docs/ReleaseNotes.rst
+++ lld/docs/ReleaseNotes.rst
@@ -28,6 +28,7 @@
 
 * ``--remap-inputs=`` and ``--remap-inputs-file=`` are added to remap input files.
   (`D148859 <https://reviews.llvm.org/D148859>`_)
+* ``PT_RISCV_ATTRIBUTES`` is added to include the SHT_RISCV_ATTRIBUTES section.
 
 Breaking changes
 ----------------
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -251,6 +251,14 @@
   ElfSym::edata2 = add("_edata", -1);
 }
 
+static OutputSection *findSection(uint32_t type, unsigned partition = 1) {
+  for (SectionCommand *cmd : script->sectionCommands)
+    if (auto *osd = dyn_cast<OutputDesc>(cmd))
+      if (osd->osec.type == type && osd->osec.partition == partition)
+        return &osd->osec;
+  return nullptr;
+}
+
 static OutputSection *findSection(StringRef name, unsigned partition = 1) {
   for (SectionCommand *cmd : script->sectionCommands)
     if (auto *osd = dyn_cast<OutputDesc>(cmd))
@@ -2418,6 +2426,10 @@
       note = nullptr;
     }
   }
+
+  if (config->emachine == EM_RISCV)
+    if (OutputSection *cmd = findSection(PT_RISCV_ATTRIBUTES, partNo))
+      addHdr(PT_RISCV_ATTRIBUTES, PF_R)->add(cmd);
   return ret;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152065.528105.patch
Type: text/x-patch
Size: 2937 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230603/185e385f/attachment.bin>


More information about the llvm-commits mailing list