[llvm] 56b760c - [ELF] Add support for CREL locations for SHT_LLVM_BB_ADDR_MAP

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 10 10:59:00 PST 2025


Author: Aiden Grossman
Date: 2025-02-10T10:58:56-08:00
New Revision: 56b760cc472fd2e919e5046575b6c537cb7f3d0b

URL: https://github.com/llvm/llvm-project/commit/56b760cc472fd2e919e5046575b6c537cb7f3d0b
DIFF: https://github.com/llvm/llvm-project/commit/56b760cc472fd2e919e5046575b6c537cb7f3d0b.diff

LOG: [ELF] Add support for CREL locations for SHT_LLVM_BB_ADDR_MAP

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: https://github.com/llvm/llvm-project/pull/126446

Added: 
    

Modified: 
    llvm/lib/Object/ELF.cpp
    llvm/test/tools/llvm-readobj/ELF/bb-addr-map-relocatable.test

Removed: 
    


################################################################################
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: ]


        


More information about the llvm-commits mailing list