Skip to content

Conversation

@boomanaiden154
Copy link
Contributor

This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

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

Author: Aiden Grossman (boomanaiden154)

Changes

This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.


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

2 Files Affected:

  • (modified) llvm/lib/Object/ELF.cpp (+19-14)
  • (modified) llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test (+56-3)
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 8cb3d7eb141766d..bf42c92a242a160 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -747,20 +747,25 @@ 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 " +
-                         describe(EF, Sec) + ": " +
-                         toString(Relas.takeError()));
-    for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
-      FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
+    if (RelaSec->sh_type == ELF::SHT_CREL) {
+      Expected<typename ELFFile<ELFT>::RelsOrRelas> Relas = EF.crels(*RelaSec);
+      if (!Relas)
+        return createError("unable to read CREL relocations for section " +
+                           describe(EF, Sec) + ": " +
+                           toString(Relas.takeError()));
+      for (typename ELFFile<ELFT>::Elf_Rela Rela : std::get<1>(*Relas)) {
+        FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
+      }
+    } else {
+      Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas =
+          EF.relas(*RelaSec);
+      if (!Relas)
+        return createError("unable to read relocations for section " +
+                           describe(EF, Sec) + ": " +
+                           toString(Relas.takeError()));
+      for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
+        FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
+    }
   }
   auto GetAddressForRelocation =
       [&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {
diff --git a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test
index e7f78491a94737a..325a956e78591bb 100644
--- a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test
+++ b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test
@@ -1,7 +1,8 @@
 ## This test checks how we handle the --bb-addr-map option on relocatable
 ## object files.
 
-# RUN: yaml2obj %s -o %t1.o
+# RUN: yaml2obj -D RELOCATION_SECTION_NAME=.rela.llvm_bb_addr_map \
+# RUN:   -D RELOCATION_SECTION_TYPE=SHT_RELA %s -o %t1.o
 # RUN: llvm-readobj %t1.o --bb-addr-map | FileCheck %s
 
 # CHECK:      BBAddrMap [
@@ -77,8 +78,8 @@ Sections:
                AddressOffset:   0x0
                Size:            0x11
                Metadata:        0x8
-  - Name:  .rela.llvm_bb_addr_map
-    Type:  SHT_RELA
+  - Name:  [[RELOCATION_SECTION_NAME]]
+    Type:  [[RELOCATION_SECTION_TYPE]]
     Flags: [ SHF_INFO_LINK ]
     Link:  .symtab
     Info:  .llvm_bb_addr_map
@@ -228,3 +229,55 @@ Sections:
 # ET-DYN-NO-WARNING:     ]
 # ET-DYN-NO-WARNING:   }
 # ET-DYN-NO-WARNING: ]
+
+## Check that we can correctly decode a BBAddrMap in a reloctable object file
+## with CREL relocations.
+
+# RUN: yaml2obj -D RELOCATION_SECTION_NAME=.crel.llvm_bb_addr_map \
+# RUN:   -D RELOCATION_SECTION_TYPE=SHT_CREL %s -o %t6.o
+# RUN: llvm-readobj %t6.o --bb-addr-map | FileCheck %s --check-prefix=CREL
+
+# CREL:      BBAddrMap [
+# CREL-NEXT:   Function {
+# CREL-NEXT:     At: 0x0
+# CREL-NEXT:     Name: <?>
+# CREL-NEXT:     BB Ranges [
+# CREL-NEXT:       {
+# CREL-NEXT:         Base Address: 0x0
+# CREL-NEXT:         BB Entries [
+# CREL-NEXT:           {
+# CREL-NEXT:             ID: 0
+# CREL-NEXT:             Offset: 0x0
+# CREL-NEXT:             Size: 0xF
+# CREL-NEXT:             HasReturn: Yes
+# CREL-NEXT:             HasTailCall: No
+# CREL-NEXT:             IsEHPad: No
+# CREL-NEXT:             CanFallThrough: No
+# CREL-NEXT:             HasIndirectBranch: No
+# CREL-NEXT:           }
+# CREL-NEXT:         ]
+# CREL-NEXT:       }
+# CREL-NEXT:     ]
+# CREL-NEXT:   }
+# CREL-NEXT:   Function {
+# CREL-NEXT:     At: 0x10
+# CREL-NEXT:     Name: <?>
+# CREL-NEXT:     BB Ranges [
+# CREL-NEXT:       {
+# CREL-NEXT:         Base Address: 0x10
+# CREL-NEXT:         BB Entries [
+# CREL-NEXT:           {
+# CREL-NEXT:             ID: 0
+# CREL-NEXT:             Offset: 0x0
+# CREL-NEXT:             Size: 0x11
+# CREL-NEXT:             HasReturn: No
+# CREL-NEXT:             HasTailCall: No
+# CREL-NEXT:             IsEHPad: No
+# CREL-NEXT:             CanFallThrough: Yes
+# CREL-NEXT:             HasIndirectBranch: No
+# CREL-NEXT:           }
+# CREL-NEXT:         ]
+# CREL-NEXT:       }
+# CREL-NEXT:     ]
+# CREL-NEXT:   }
+# CREL-NEXT: ]

@boomanaiden154
Copy link
Contributor Author

This is a stacked PR on top of #126445.

boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Feb 10, 2025
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

Pull Request: llvm#126446
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@boomanaiden154 boomanaiden154 changed the base branch from users/boomanaiden154/main.elf-add-support-for-crel-locations-for-sht_llvm_bb_addr_map to main February 10, 2025 18:58
@boomanaiden154 boomanaiden154 merged commit 56b760c into main Feb 10, 2025
6 of 9 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/elf-add-support-for-crel-locations-for-sht_llvm_bb_addr_map branch February 10, 2025 18:58
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 10, 2025
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

Reviewers: rlavaee, jh7370, red1bluelost, MaskRay

Reviewed By: MaskRay

Pull Request: llvm/llvm-project#126446
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

Reviewers: rlavaee, jh7370, red1bluelost, MaskRay

Reviewed By: MaskRay

Pull Request: llvm#126446
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

Reviewers: rlavaee, jh7370, red1bluelost, MaskRay

Reviewed By: MaskRay

Pull Request: llvm#126446
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP
sections in relocatable object files when the relocation format is CREL.

Reviewers: rlavaee, jh7370, red1bluelost, MaskRay

Reviewed By: MaskRay

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