diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -4490,6 +4490,14 @@ std::vector Symbols; auto getNewSectionIndex = [&](uint32_t OldIndex) { + // For dynamic symbol table, the section index could be wrong on the input, + // and its value is ignored by the runtime if it's different from + // SHN_UNDEF and SHN_ABS. + // However, we still need to update dynamic symbol table, so return a + // section index, even though the index is broken. + if (IsDynSym && OldIndex >= NewSectionIndex.size()) + return OldIndex; + assert(OldIndex < NewSectionIndex.size() && "section index out of bounds"); const uint32_t NewIndex = NewSectionIndex[OldIndex]; diff --git a/bolt/test/X86/broken_dynsym.s b/bolt/test/X86/broken_dynsym.s new file mode 100644 --- /dev/null +++ b/bolt/test/X86/broken_dynsym.s @@ -0,0 +1,173 @@ +# This test checks if BOLT can process stripped binaries, where symbol's section +# header index is corrupted due to strip tool. + +# RUN: %clang++ %s -o %t -Wl,-q +# RUN: llvm-strip -s %t +# RUN: llvm-bolt %t -o %t.bolt + + .text + .section .text.startup,"ax",@progbits + .p2align 4, 0x90 # -- Begin function __cxx_global_var_init + .type __cxx_global_var_init,@function +__cxx_global_var_init: # @__cxx_global_var_init + .cfi_startproc +# %bb.0: # %entry + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + movabsq $_ZStL8__ioinit, %rdi + callq _ZNSt8ios_base4InitC1Ev + movabsq $_ZNSt8ios_base4InitD1Ev, %rdi + movabsq $_ZStL8__ioinit, %rsi + movabsq $__dso_handle, %rdx + callq __cxa_atexit + popq %rbp + .cfi_def_cfa %rsp, 8 + retq +.Lfunc_end0: + .size __cxx_global_var_init, .Lfunc_end0-__cxx_global_var_init + .cfi_endproc + # -- End function + .text + .globl main # -- Begin function main + .p2align 4, 0x90 + .type main,@function +main: # @main + .cfi_startproc +# %bb.0: # %entry + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + subq $32, %rsp + movl $0, -4(%rbp) + movabsq $_ZSt4cout, %rdi + movabsq $.L.str, %rsi + callq _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc + movabsq $_ZSt3cin, %rdi + leaq -8(%rbp), %rsi + callq _ZNSirsERi + movq %rax, %rdi + leaq -12(%rbp), %rsi + callq _ZNSirsERi + movl -12(%rbp), %eax + cmpl -8(%rbp), %eax + jle .LBB1_2 +# %bb.1: # %if.then + movl -12(%rbp), %eax + movl %eax, -20(%rbp) + movl -8(%rbp), %eax + movl %eax, -12(%rbp) + movl -20(%rbp), %eax + movl %eax, -8(%rbp) +.LBB1_2: # %if.end + movl $1, -24(%rbp) +.LBB1_3: # %for.cond + # =>This Inner Loop Header: Depth=1 + movl -24(%rbp), %eax + cmpl -12(%rbp), %eax + jg .LBB1_9 +# %bb.4: # %for.body + # in Loop: Header=BB1_3 Depth=1 + movl -8(%rbp), %eax + cltd + idivl -24(%rbp) + cmpl $0, %edx + jne .LBB1_7 +# %bb.5: # %land.lhs.true + # in Loop: Header=BB1_3 Depth=1 + movl -12(%rbp), %eax + cltd + idivl -24(%rbp) + cmpl $0, %edx + jne .LBB1_7 +# %bb.6: # %if.then7 + # in Loop: Header=BB1_3 Depth=1 + movl -24(%rbp), %eax + movl %eax, -16(%rbp) +.LBB1_7: # %if.end8 + # in Loop: Header=BB1_3 Depth=1 + jmp .LBB1_8 +.LBB1_8: # %for.inc + # in Loop: Header=BB1_3 Depth=1 + movl -24(%rbp), %eax + addl $1, %eax + movl %eax, -24(%rbp) + jmp .LBB1_3 +.LBB1_9: # %for.end + movabsq $_ZSt4cout, %rdi + movabsq $.L.str.1, %rsi + callq _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc + movq %rax, %rdi + movl -16(%rbp), %esi + callq _ZNSolsEi + movq %rax, %rdi + movabsq $.L.str.2, %rsi + callq _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc + xorl %eax, %eax + addq $32, %rsp + popq %rbp + .cfi_def_cfa %rsp, 8 + retq +.Lfunc_end1: + .size main, .Lfunc_end1-main + .cfi_endproc + # -- End function + .section .text.startup,"ax",@progbits + .p2align 4, 0x90 # -- Begin function _GLOBAL__sub_I_gcd.cpp + .type _GLOBAL__sub_I_gcd.cpp,@function +_GLOBAL__sub_I_gcd.cpp: # @_GLOBAL__sub_I_gcd.cpp + .cfi_startproc +# %bb.0: # %entry + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + callq __cxx_global_var_init + popq %rbp + .cfi_def_cfa %rsp, 8 + retq +.Lfunc_end2: + .size _GLOBAL__sub_I_gcd.cpp, .Lfunc_end2-_GLOBAL__sub_I_gcd.cpp + .cfi_endproc + # -- End function + .type _ZStL8__ioinit,@object # @_ZStL8__ioinit + .local _ZStL8__ioinit + .comm _ZStL8__ioinit,1,1 + .hidden __dso_handle + .type .L.str,@object # @.str + .section .rodata.str1.1,"aMS",@progbits,1 +.L.str: + .asciz "Enter two numbers: " + .size .L.str, 20 + + .type .L.str.1,@object # @.str.1 +.L.str.1: + .asciz "Greatest common divisor = " + .size .L.str.1, 27 + + .type .L.str.2,@object # @.str.2 +.L.str.2: + .asciz "\n" + .size .L.str.2, 2 + + .section .init_array,"aw",@init_array + .p2align 3 + .quad _GLOBAL__sub_I_gcd.cpp + .ident "clang version 15.0.0 (ssh://git.vip.facebook.com/data/gitrepos/osmeta/external/llvm-project 91ed9bd853620e163fda21b2f65a8ec2a3d8c867)" + .section ".note.GNU-stack","",@progbits + .addrsig + .addrsig_sym __cxx_global_var_init + .addrsig_sym __cxa_atexit + .addrsig_sym _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc + .addrsig_sym _ZNSirsERi + .addrsig_sym _ZNSolsEi + .addrsig_sym _GLOBAL__sub_I_gcd.cpp + .addrsig_sym _ZStL8__ioinit + .addrsig_sym __dso_handle + .addrsig_sym _ZSt4cout + .addrsig_sym _ZSt3cin