[llvm] [Object] Handle SHT_CREL relocation sections when resolving relocation data (PR #141843)
Zequan Wu via llvm-commits
llvm-commits at lists.llvm.org
Wed May 28 12:57:55 PDT 2025
https://github.com/ZequanWu created https://github.com/llvm/llvm-project/pull/141843
Fixes #141680
>From 88a4e6d3205ccf67fd762f5bb589da9cc548511d Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 28 May 2025 12:56:07 -0700
Subject: [PATCH] [Object] Handle SHT_CREL relocation sections when resolving
relocation data
---
llvm/lib/Object/RelocationResolver.cpp | 3 +-
.../llvm-dwarfdump/X86/debug_info_crel.s | 288 ++++++++++++++++++
2 files changed, 290 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/tools/llvm-dwarfdump/X86/debug_info_crel.s
diff --git a/llvm/lib/Object/RelocationResolver.cpp b/llvm/lib/Object/RelocationResolver.cpp
index a28f5943c320d..8cf748aa5681c 100644
--- a/llvm/lib/Object/RelocationResolver.cpp
+++ b/llvm/lib/Object/RelocationResolver.cpp
@@ -888,7 +888,8 @@ uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
};
- if (GetRelSectionType() == ELF::SHT_RELA) {
+ if (GetRelSectionType() == ELF::SHT_RELA ||
+ GetRelSectionType() == ELF::SHT_CREL) {
Addend = getELFAddend(R);
// LoongArch and RISCV relocations use both LocData and Addend.
if (Obj->getArch() != Triple::loongarch32 &&
diff --git a/llvm/test/tools/llvm-dwarfdump/X86/debug_info_crel.s b/llvm/test/tools/llvm-dwarfdump/X86/debug_info_crel.s
new file mode 100644
index 0000000000000..b21018d10361f
--- /dev/null
+++ b/llvm/test/tools/llvm-dwarfdump/X86/debug_info_crel.s
@@ -0,0 +1,288 @@
+# Test llvm-dwarfdump handles CREL relocation sections correctly.
+# Generated from the following code and command:
+# $ clang++ -O1 -Wa,--crel,--allow-experimental-crel -g -S foo.cpp -o foo.s
+# int foo(bool b, int x) {
+# if (b)
+# return x;
+# else
+# return x + 1;
+# }
+
+# RUN: llvm-mc %s -filetype=obj -triple=x86_64-pc-linux -o %t
+# RUN: llvm-dwarfdump --debug-info %t | FileCheck %s
+# CHECK: DW_AT_producer ("clang version
+# CHECK-NEXT: DW_AT_language (DW_LANG_C_plus_plus_14)
+# CHECK-NEXT: DW_AT_name ("foo.cpp")
+# CHECK-NEXT: DW_AT_str_offsets_base (0x00000008)
+# CHECK-NEXT: DW_AT_stmt_list (0x00000000)
+# CHECK-NEXT: DW_AT_comp_dir ("/tmp")
+# CHECK-NEXT: DW_AT_low_pc (0x0000000000000000)
+# CHECK-NEXT: DW_AT_high_pc (0x000000000000000b)
+# CHECK-NEXT: DW_AT_addr_base (0x00000008)
+# CHECK-NEXT: DW_AT_loclists_base (0x0000000c)
+
+ .file "foo.cpp"
+ .text
+ .globl _Z3foobi # -- Begin function _Z3foobi
+ .p2align 4
+ .type _Z3foobi, at function
+_Z3foobi: # @_Z3foobi
+.Lfunc_begin0:
+ .file 0 "/tmp" "foo.cpp" md5 0x33d1d90807761bceb53376a52dc68c38
+ .cfi_startproc
+# %bb.0: # %entry
+ #DEBUG_VALUE: foo:b <- [DW_OP_LLVM_convert 1 7, DW_OP_LLVM_convert 8 7, DW_OP_stack_value] $edi
+ #DEBUG_VALUE: foo:x <- $esi
+ .loc 0 2 7 prologue_end # foo.cpp:2:7
+ xorb $1, %dil
+.Ltmp0:
+ movzbl %dil, %eax
+ addl %esi, %eax
+.Ltmp1:
+ .loc 0 6 1 # foo.cpp:6:1
+ retq
+.Ltmp2:
+.Lfunc_end0:
+ .size _Z3foobi, .Lfunc_end0-_Z3foobi
+ .cfi_endproc
+ # -- End function
+ .section .debug_loclists,"", at progbits
+ .long .Ldebug_list_header_end0-.Ldebug_list_header_start0 # Length
+.Ldebug_list_header_start0:
+ .short 5 # Version
+ .byte 8 # Address size
+ .byte 0 # Segment selector size
+ .long 1 # Offset entry count
+.Lloclists_table_base0:
+ .long .Ldebug_loc0-.Lloclists_table_base0
+.Ldebug_loc0:
+ .byte 4 # DW_LLE_offset_pair
+ .uleb128 .Lfunc_begin0-.Lfunc_begin0 # starting offset
+ .uleb128 .Ltmp0-.Lfunc_begin0 # ending offset
+ .byte 20 # Loc expr size
+ .byte 117 # DW_OP_breg5
+ .byte 0 # 0
+ .byte 16 # DW_OP_constu
+ .byte 255 # 4294967295
+ .byte 255 #
+ .byte 255 #
+ .byte 255 #
+ .byte 15 #
+ .byte 26 # DW_OP_and
+ .byte 168 # DW_OP_convert
+ .asciz "\247\200\200" #
+ .byte 168 # DW_OP_convert
+ .asciz "\253\200\200" #
+ .byte 159 # DW_OP_stack_value
+ .byte 0 # DW_LLE_end_of_list
+.Ldebug_list_header_end0:
+ .section .debug_abbrev,"", at progbits
+ .byte 1 # Abbreviation Code
+ .byte 17 # DW_TAG_compile_unit
+ .byte 1 # DW_CHILDREN_yes
+ .byte 37 # DW_AT_producer
+ .byte 37 # DW_FORM_strx1
+ .byte 19 # DW_AT_language
+ .byte 5 # DW_FORM_data2
+ .byte 3 # DW_AT_name
+ .byte 37 # DW_FORM_strx1
+ .byte 114 # DW_AT_str_offsets_base
+ .byte 23 # DW_FORM_sec_offset
+ .byte 16 # DW_AT_stmt_list
+ .byte 23 # DW_FORM_sec_offset
+ .byte 27 # DW_AT_comp_dir
+ .byte 37 # DW_FORM_strx1
+ .byte 17 # DW_AT_low_pc
+ .byte 27 # DW_FORM_addrx
+ .byte 18 # DW_AT_high_pc
+ .byte 6 # DW_FORM_data4
+ .byte 115 # DW_AT_addr_base
+ .byte 23 # DW_FORM_sec_offset
+ .ascii "\214\001" # DW_AT_loclists_base
+ .byte 23 # DW_FORM_sec_offset
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 2 # Abbreviation Code
+ .byte 36 # DW_TAG_base_type
+ .byte 0 # DW_CHILDREN_no
+ .byte 3 # DW_AT_name
+ .byte 37 # DW_FORM_strx1
+ .byte 62 # DW_AT_encoding
+ .byte 11 # DW_FORM_data1
+ .byte 11 # DW_AT_byte_size
+ .byte 11 # DW_FORM_data1
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 3 # Abbreviation Code
+ .byte 46 # DW_TAG_subprogram
+ .byte 1 # DW_CHILDREN_yes
+ .byte 17 # DW_AT_low_pc
+ .byte 27 # DW_FORM_addrx
+ .byte 18 # DW_AT_high_pc
+ .byte 6 # DW_FORM_data4
+ .byte 64 # DW_AT_frame_base
+ .byte 24 # DW_FORM_exprloc
+ .byte 122 # DW_AT_call_all_calls
+ .byte 25 # DW_FORM_flag_present
+ .byte 110 # DW_AT_linkage_name
+ .byte 37 # DW_FORM_strx1
+ .byte 3 # DW_AT_name
+ .byte 37 # DW_FORM_strx1
+ .byte 58 # DW_AT_decl_file
+ .byte 11 # DW_FORM_data1
+ .byte 59 # DW_AT_decl_line
+ .byte 11 # DW_FORM_data1
+ .byte 73 # DW_AT_type
+ .byte 19 # DW_FORM_ref4
+ .byte 63 # DW_AT_external
+ .byte 25 # DW_FORM_flag_present
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 4 # Abbreviation Code
+ .byte 5 # DW_TAG_formal_parameter
+ .byte 0 # DW_CHILDREN_no
+ .byte 2 # DW_AT_location
+ .byte 34 # DW_FORM_loclistx
+ .byte 3 # DW_AT_name
+ .byte 37 # DW_FORM_strx1
+ .byte 58 # DW_AT_decl_file
+ .byte 11 # DW_FORM_data1
+ .byte 59 # DW_AT_decl_line
+ .byte 11 # DW_FORM_data1
+ .byte 73 # DW_AT_type
+ .byte 19 # DW_FORM_ref4
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 5 # Abbreviation Code
+ .byte 5 # DW_TAG_formal_parameter
+ .byte 0 # DW_CHILDREN_no
+ .byte 2 # DW_AT_location
+ .byte 24 # DW_FORM_exprloc
+ .byte 3 # DW_AT_name
+ .byte 37 # DW_FORM_strx1
+ .byte 58 # DW_AT_decl_file
+ .byte 11 # DW_FORM_data1
+ .byte 59 # DW_AT_decl_line
+ .byte 11 # DW_FORM_data1
+ .byte 73 # DW_AT_type
+ .byte 19 # DW_FORM_ref4
+ .byte 0 # EOM(1)
+ .byte 0 # EOM(2)
+ .byte 0 # EOM(3)
+ .section .debug_info,"", at progbits
+.Lcu_begin0:
+ .long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
+.Ldebug_info_start0:
+ .short 5 # DWARF version number
+ .byte 1 # DWARF Unit Type
+ .byte 8 # Address Size (in bytes)
+ .long .debug_abbrev # Offset Into Abbrev. Section
+ .byte 1 # Abbrev [1] 0xc:0x50 DW_TAG_compile_unit
+ .byte 0 # DW_AT_producer
+ .short 33 # DW_AT_language
+ .byte 1 # DW_AT_name
+ .long .Lstr_offsets_base0 # DW_AT_str_offsets_base
+ .long .Lline_table_start0 # DW_AT_stmt_list
+ .byte 2 # DW_AT_comp_dir
+ .byte 0 # DW_AT_low_pc
+ .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
+ .long .Laddr_table_base0 # DW_AT_addr_base
+ .long .Lloclists_table_base0 # DW_AT_loclists_base
+ .byte 2 # Abbrev [2] 0x27:0x4 DW_TAG_base_type
+ .byte 4 # DW_AT_name
+ .byte 7 # DW_AT_encoding
+ .byte 1 # DW_AT_byte_size
+ .byte 2 # Abbrev [2] 0x2b:0x4 DW_TAG_base_type
+ .byte 3 # DW_AT_name
+ .byte 7 # DW_AT_encoding
+ .byte 1 # DW_AT_byte_size
+ .byte 3 # Abbrev [3] 0x2f:0x24 DW_TAG_subprogram
+ .byte 0 # DW_AT_low_pc
+ .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
+ .byte 1 # DW_AT_frame_base
+ .byte 87
+ # DW_AT_call_all_calls
+ .byte 5 # DW_AT_linkage_name
+ .byte 6 # DW_AT_name
+ .byte 0 # DW_AT_decl_file
+ .byte 1 # DW_AT_decl_line
+ .long 83 # DW_AT_type
+ # DW_AT_external
+ .byte 4 # Abbrev [4] 0x3f:0x9 DW_TAG_formal_parameter
+ .byte 0 # DW_AT_location
+ .byte 8 # DW_AT_name
+ .byte 0 # DW_AT_decl_file
+ .byte 1 # DW_AT_decl_line
+ .long 87 # DW_AT_type
+ .byte 5 # Abbrev [5] 0x48:0xa DW_TAG_formal_parameter
+ .byte 1 # DW_AT_location
+ .byte 84
+ .byte 10 # DW_AT_name
+ .byte 0 # DW_AT_decl_file
+ .byte 1 # DW_AT_decl_line
+ .long 83 # DW_AT_type
+ .byte 0 # End Of Children Mark
+ .byte 2 # Abbrev [2] 0x53:0x4 DW_TAG_base_type
+ .byte 7 # DW_AT_name
+ .byte 5 # DW_AT_encoding
+ .byte 4 # DW_AT_byte_size
+ .byte 2 # Abbrev [2] 0x57:0x4 DW_TAG_base_type
+ .byte 9 # DW_AT_name
+ .byte 2 # DW_AT_encoding
+ .byte 1 # DW_AT_byte_size
+ .byte 0 # End Of Children Mark
+.Ldebug_info_end0:
+ .section .debug_str_offsets,"", at progbits
+ .long 48 # Length of String Offsets Set
+ .short 5
+ .short 0
+.Lstr_offsets_base0:
+ .section .debug_str,"MS", at progbits,1
+.Linfo_string0:
+ .asciz "clang version 21.0.0git (git at github.com:ZequanWu/llvm-project.git 317f3bdcc1f96586489a9e48b494e593592b68cf)" # string offset=0
+.Linfo_string1:
+ .asciz "foo.cpp" # string offset=108
+.Linfo_string2:
+ .asciz "/tmp" # string offset=116
+.Linfo_string3:
+ .asciz "DW_ATE_unsigned_8" # string offset=121
+.Linfo_string4:
+ .asciz "DW_ATE_unsigned_1" # string offset=139
+.Linfo_string5:
+ .asciz "_Z3foobi" # string offset=157
+.Linfo_string6:
+ .asciz "foo" # string offset=166
+.Linfo_string7:
+ .asciz "int" # string offset=170
+.Linfo_string8:
+ .asciz "b" # string offset=174
+.Linfo_string9:
+ .asciz "bool" # string offset=176
+.Linfo_string10:
+ .asciz "x" # string offset=181
+ .section .debug_str_offsets,"", at progbits
+ .long .Linfo_string0
+ .long .Linfo_string1
+ .long .Linfo_string2
+ .long .Linfo_string3
+ .long .Linfo_string4
+ .long .Linfo_string5
+ .long .Linfo_string6
+ .long .Linfo_string7
+ .long .Linfo_string8
+ .long .Linfo_string9
+ .long .Linfo_string10
+ .section .debug_addr,"", at progbits
+ .long .Ldebug_addr_end0-.Ldebug_addr_start0 # Length of contribution
+.Ldebug_addr_start0:
+ .short 5 # DWARF version number
+ .byte 8 # Address size
+ .byte 0 # Segment selector size
+.Laddr_table_base0:
+ .quad .Lfunc_begin0
+.Ldebug_addr_end0:
+ .ident "clang version 21.0.0git (git at github.com:ZequanWu/llvm-project.git 317f3bdcc1f96586489a9e48b494e593592b68cf)"
+ .section ".note.GNU-stack","", at progbits
+ .addrsig
+ .section .debug_line,"", at progbits
+.Lline_table_start0:
More information about the llvm-commits
mailing list