Skip to content

Commit b786e01

Browse files
committed
[DWARF][BOLT] Handle getBinaryFunctionContainingAddress returning nullptr for DW_TAG_call_site
DW_TAG_call_site/DW_AT_call_return_pc can contain address that is not in any function. In this case getBinaryFunctionContainingAddress returns nullptr. For this case preserving original address. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D132057
1 parent f1b5d30 commit b786e01

File tree

4 files changed

+614
-4
lines changed

4 files changed

+614
-4
lines changed

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,16 @@ void DWARFRewriter::updateUnitDebugInfo(
485485
Optional<uint64_t> Address = AttrVal.V.getAsAddress();
486486
const BinaryFunction *Function =
487487
BC.getBinaryFunctionContainingAddress(*Address);
488-
const uint64_t UpdatedAddress =
489-
Function->translateInputToOutputAddress(*Address);
490-
const uint32_t Index =
491-
AddrWriter->getIndexFromAddress(UpdatedAddress, Unit);
488+
uint32_t Index = 0;
489+
// Preserving original address instead of using whatever ends up at this
490+
// index.
491+
if (!Function) {
492+
Index = AddrWriter->getIndexFromAddress(*Address, Unit);
493+
} else {
494+
const uint64_t UpdatedAddress =
495+
Function->translateInputToOutputAddress(*Address);
496+
Index = AddrWriter->getIndexFromAddress(UpdatedAddress, Unit);
497+
}
492498
if (AttrVal.V.getForm() == dwarf::DW_FORM_addrx)
493499
DebugInfoPatcher.addUDataPatch(AttrVal.Offset, Index, AttrVal.Size);
494500
else
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# clang++ main.cpp -g2 -O2 -S
2+
# int helper(int z_, int d_) {
3+
# return z_ + d_;
4+
# }
5+
6+
7+
.text
8+
.file "helper.cpp"
9+
.globl _Z6helperii # -- Begin function _Z6helperii
10+
.p2align 4, 0x90
11+
.type _Z6helperii,@function
12+
_Z6helperii: # @_Z6helperii
13+
.Lfunc_begin0:
14+
.file 0 "." "helper.cpp" md5 0x8020e02b87876b529416442978378ed0
15+
.loc 0 1 0 # helper.cpp:1:0
16+
.cfi_startproc
17+
# %bb.0: # %entry
18+
#DEBUG_VALUE: helper:z_ <- $edi
19+
#DEBUG_VALUE: helper:d_ <- $esi
20+
# kill: def $esi killed $esi def $rsi
21+
# kill: def $edi killed $edi def $rdi
22+
.loc 0 2 13 prologue_end # helper.cpp:2:13
23+
leal (%rdi,%rsi), %eax
24+
.loc 0 2 3 is_stmt 0 # helper.cpp:2:3
25+
retq
26+
.Ltmp0:
27+
.Lfunc_end0:
28+
.size _Z6helperii, .Lfunc_end0-_Z6helperii
29+
.cfi_endproc
30+
# -- End function
31+
.section .debug_abbrev,"",@progbits
32+
.byte 1 # Abbreviation Code
33+
.byte 17 # DW_TAG_compile_unit
34+
.byte 1 # DW_CHILDREN_yes
35+
.byte 37 # DW_AT_producer
36+
.byte 37 # DW_FORM_strx1
37+
.byte 19 # DW_AT_language
38+
.byte 5 # DW_FORM_data2
39+
.byte 3 # DW_AT_name
40+
.byte 37 # DW_FORM_strx1
41+
.byte 114 # DW_AT_str_offsets_base
42+
.byte 23 # DW_FORM_sec_offset
43+
.byte 16 # DW_AT_stmt_list
44+
.byte 23 # DW_FORM_sec_offset
45+
.byte 27 # DW_AT_comp_dir
46+
.byte 37 # DW_FORM_strx1
47+
.byte 17 # DW_AT_low_pc
48+
.byte 27 # DW_FORM_addrx
49+
.byte 18 # DW_AT_high_pc
50+
.byte 6 # DW_FORM_data4
51+
.byte 115 # DW_AT_addr_base
52+
.byte 23 # DW_FORM_sec_offset
53+
.byte 0 # EOM(1)
54+
.byte 0 # EOM(2)
55+
.byte 2 # Abbreviation Code
56+
.byte 46 # DW_TAG_subprogram
57+
.byte 1 # DW_CHILDREN_yes
58+
.byte 17 # DW_AT_low_pc
59+
.byte 27 # DW_FORM_addrx
60+
.byte 18 # DW_AT_high_pc
61+
.byte 6 # DW_FORM_data4
62+
.byte 64 # DW_AT_frame_base
63+
.byte 24 # DW_FORM_exprloc
64+
.byte 122 # DW_AT_call_all_calls
65+
.byte 25 # DW_FORM_flag_present
66+
.byte 110 # DW_AT_linkage_name
67+
.byte 37 # DW_FORM_strx1
68+
.byte 3 # DW_AT_name
69+
.byte 37 # DW_FORM_strx1
70+
.byte 58 # DW_AT_decl_file
71+
.byte 11 # DW_FORM_data1
72+
.byte 59 # DW_AT_decl_line
73+
.byte 11 # DW_FORM_data1
74+
.byte 73 # DW_AT_type
75+
.byte 19 # DW_FORM_ref4
76+
.byte 63 # DW_AT_external
77+
.byte 25 # DW_FORM_flag_present
78+
.byte 0 # EOM(1)
79+
.byte 0 # EOM(2)
80+
.byte 3 # Abbreviation Code
81+
.byte 5 # DW_TAG_formal_parameter
82+
.byte 0 # DW_CHILDREN_no
83+
.byte 2 # DW_AT_location
84+
.byte 24 # DW_FORM_exprloc
85+
.byte 3 # DW_AT_name
86+
.byte 37 # DW_FORM_strx1
87+
.byte 58 # DW_AT_decl_file
88+
.byte 11 # DW_FORM_data1
89+
.byte 59 # DW_AT_decl_line
90+
.byte 11 # DW_FORM_data1
91+
.byte 73 # DW_AT_type
92+
.byte 19 # DW_FORM_ref4
93+
.byte 0 # EOM(1)
94+
.byte 0 # EOM(2)
95+
.byte 4 # Abbreviation Code
96+
.byte 36 # DW_TAG_base_type
97+
.byte 0 # DW_CHILDREN_no
98+
.byte 3 # DW_AT_name
99+
.byte 37 # DW_FORM_strx1
100+
.byte 62 # DW_AT_encoding
101+
.byte 11 # DW_FORM_data1
102+
.byte 11 # DW_AT_byte_size
103+
.byte 11 # DW_FORM_data1
104+
.byte 0 # EOM(1)
105+
.byte 0 # EOM(2)
106+
.byte 0 # EOM(3)
107+
.section .debug_info,"",@progbits
108+
.Lcu_begin0:
109+
.long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
110+
.Ldebug_info_start0:
111+
.short 5 # DWARF version number
112+
.byte 1 # DWARF Unit Type
113+
.byte 8 # Address Size (in bytes)
114+
.long .debug_abbrev # Offset Into Abbrev. Section
115+
.byte 1 # Abbrev [1] 0xc:0x41 DW_TAG_compile_unit
116+
.byte 0 # DW_AT_producer
117+
.short 33 # DW_AT_language
118+
.byte 1 # DW_AT_name
119+
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
120+
.long .Lline_table_start0 # DW_AT_stmt_list
121+
.byte 2 # DW_AT_comp_dir
122+
.byte 0 # DW_AT_low_pc
123+
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
124+
.long .Laddr_table_base0 # DW_AT_addr_base
125+
.byte 2 # Abbrev [2] 0x23:0x25 DW_TAG_subprogram
126+
.byte 0 # DW_AT_low_pc
127+
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
128+
.byte 1 # DW_AT_frame_base
129+
.byte 87
130+
# DW_AT_call_all_calls
131+
.byte 3 # DW_AT_linkage_name
132+
.byte 4 # DW_AT_name
133+
.byte 0 # DW_AT_decl_file
134+
.byte 1 # DW_AT_decl_line
135+
.long 72 # DW_AT_type
136+
# DW_AT_external
137+
.byte 3 # Abbrev [3] 0x33:0xa DW_TAG_formal_parameter
138+
.byte 1 # DW_AT_location
139+
.byte 85
140+
.byte 6 # DW_AT_name
141+
.byte 0 # DW_AT_decl_file
142+
.byte 1 # DW_AT_decl_line
143+
.long 72 # DW_AT_type
144+
.byte 3 # Abbrev [3] 0x3d:0xa DW_TAG_formal_parameter
145+
.byte 1 # DW_AT_location
146+
.byte 84
147+
.byte 7 # DW_AT_name
148+
.byte 0 # DW_AT_decl_file
149+
.byte 1 # DW_AT_decl_line
150+
.long 72 # DW_AT_type
151+
.byte 0 # End Of Children Mark
152+
.byte 4 # Abbrev [4] 0x48:0x4 DW_TAG_base_type
153+
.byte 5 # DW_AT_name
154+
.byte 5 # DW_AT_encoding
155+
.byte 4 # DW_AT_byte_size
156+
.byte 0 # End Of Children Mark
157+
.Ldebug_info_end0:
158+
.section .debug_str_offsets,"",@progbits
159+
.long 36 # Length of String Offsets Set
160+
.short 5
161+
.short 0
162+
.Lstr_offsets_base0:
163+
.section .debug_str,"MS",@progbits,1
164+
.Linfo_string0:
165+
.asciz "clang version 16.0.0" # string offset=0
166+
.Linfo_string1:
167+
.asciz "helper.cpp" # string offset=134
168+
.Linfo_string2:
169+
.asciz "." # string offset=145
170+
.Linfo_string3:
171+
.asciz "_Z6helperii" # string offset=198
172+
.Linfo_string4:
173+
.asciz "helper" # string offset=210
174+
.Linfo_string5:
175+
.asciz "int" # string offset=217
176+
.Linfo_string6:
177+
.asciz "z_" # string offset=221
178+
.Linfo_string7:
179+
.asciz "d_" # string offset=224
180+
.section .debug_str_offsets,"",@progbits
181+
.long .Linfo_string0
182+
.long .Linfo_string1
183+
.long .Linfo_string2
184+
.long .Linfo_string3
185+
.long .Linfo_string4
186+
.long .Linfo_string5
187+
.long .Linfo_string6
188+
.long .Linfo_string7
189+
.section .debug_addr,"",@progbits
190+
.long .Ldebug_addr_end0-.Ldebug_addr_start0 # Length of contribution
191+
.Ldebug_addr_start0:
192+
.short 5 # DWARF version number
193+
.byte 8 # Address size
194+
.byte 0 # Segment selector size
195+
.Laddr_table_base0:
196+
.quad .Lfunc_begin0
197+
.Ldebug_addr_end0:
198+
.ident "clang version 16.0.0"
199+
.section ".note.GNU-stack","",@progbits
200+
.addrsig
201+
.section .debug_line,"",@progbits
202+
.Lline_table_start0:

0 commit comments

Comments
 (0)