Skip to content

[ELF] Add support for CREL to getSectionAndRelocations #126445

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

Conversation

boomanaiden154
Copy link
Contributor

This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

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

Author: Aiden Grossman (boomanaiden154)

Changes

This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.


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

3 Files Affected:

  • (modified) llvm/lib/Object/ELF.cpp (+9-1)
  • (modified) llvm/tools/llvm-readobj/ELFDumper.cpp (+10)
  • (modified) llvm/unittests/Object/ELFObjectFileTest.cpp (+16-1)
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index b6d0699ee4fe080..8cb3d7eb141766d 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -747,6 +747,13 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
     assert(RelaSec &&
            "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
            "object file without providing a relocation section.");
+    // We might end up with relocations in CREL here. If we do, return an
+    // error since we do not currently support them.
+    if (RelaSec->sh_type == ELF::SHT_CREL)
+      return createError("unable to read relocations for section " +
+                         describe(EF, Sec) +
+                         " as the corresponding relocation section format is "
+                         "CREL, which is not currently supported.");
     Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
     if (!Relas)
       return createError("unable to read relocations for section " +
@@ -958,7 +965,8 @@ ELFFile<ELFT>::getSectionAndRelocations(
         continue;
     }
 
-    if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
+    if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL &&
+        Sec.sh_type != ELF::SHT_CREL)
       continue;
 
     Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 7806eea6a0c52df..faad89710daa978 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -6833,6 +6833,16 @@ void ELFDumper<ELFT>::printRelocatableStackSizes(
       continue;
     }
 
+    // We might end up with relocations in CREL here. If we do, report a
+    // warning since we do not currently support them.
+    if (RelocSec->sh_type == ELF::SHT_CREL) {
+      reportWarning(createError(".stack_sizes (" + describe(*StackSizesELFSec) +
+                                ") has a corresponding CREL relocation "
+                                "section, which is not currently supported."),
+                    FileName);
+      continue;
+    }
+
     // A .stack_sizes section header's sh_link field is supposed to point
     // to the section that contains the functions whose stack sizes are
     // described in it.
diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp
index 2a0921690914b4a..493e673d6a07db7 100644
--- a/llvm/unittests/Object/ELFObjectFileTest.cpp
+++ b/llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/Object/ELFObjectFile.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -1433,7 +1434,8 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
     // Basic verification to make sure we have the correct section types.
     for (auto const &[Sec, RelaSec] : *SecToRelocMapOrErr) {
       ASSERT_EQ(Sec->sh_type, ELF::SHT_PROGBITS);
-      ASSERT_EQ(RelaSec->sh_type, ELF::SHT_RELA);
+      ASSERT_TRUE(RelaSec->sh_type == ELF::SHT_RELA ||
+                  RelaSec->sh_type == ELF::SHT_CREL);
     }
   };
 
@@ -1503,6 +1505,19 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
   DoCheckFails(MissingRelocatableContent, DefaultMatcher,
                "SHT_RELA section with index 1: failed to get a "
                "relocated section: invalid section index: 255");
+
+  StringRef OneTextSectionCREL = R"(
+Sections:
+  - Name: .text
+    Type: SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+  - Name: .crel.tex
+    Type: SHT_CREL
+    Flags: [ SHF_INFO_LINK ]
+    Info: .text
+)";
+
+  DoCheckSucceeds(OneTextSectionCREL, DefaultMatcher);
 }
 
 TEST(ELFObjectFileTest, ELFSymbolRefLess) {

@boomanaiden154
Copy link
Contributor Author

This is a stacked PR, serving as the base of #126446. Eventually I will put up another PR to support decoding stack size sections in the presence of CREL.

boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Feb 10, 2025
This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Pull Request: llvm#126445
Created using spr 1.3.4
@boomanaiden154 boomanaiden154 merged commit 808b1c1 into main Feb 10, 2025
5 of 7 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/elf-add-support-for-crel-to-getsectionandrelocations branch February 10, 2025 18:57
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 10, 2025
This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Reviewers: red1bluelost, MaskRay, rlavaee

Reviewed By: MaskRay

Pull Request: llvm/llvm-project#126445
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Reviewers: red1bluelost, MaskRay, rlavaee

Reviewed By: MaskRay

Pull Request: llvm#126445
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Reviewers: red1bluelost, MaskRay, rlavaee

Reviewed By: MaskRay

Pull Request: llvm#126445
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
This patch updates the getSectionAndRelocations function to also support
CREL relocation sections. Unit tests have been added. This patch also
updates consumers to say they explicitly do not support CREL format
relocations. Subsequent patches will make the consumers work with CREL
format relocations and also add in testing support.

Reviewers: red1bluelost, MaskRay, rlavaee

Reviewed By: MaskRay

Pull Request: llvm#126445
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.

4 participants