[llvm] [BOLT] Fix order of R_*_IRELATIVE in .rela.plt (PR #106515)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 09:22:06 PDT 2024


linsinan1995 wrote:

Reproduce from a real-world case.

1. BUILD
```
# CentOS 7
# GCC 9.2.1
# LD 2.32
git clone https://github.com/intel/hyperscan.git

mkdir build
pushd build
cmake -DCMAKE_C_FLAGS="-fno-reorder-blocks-and-partition"   \
      -DCMAKE_CXX_FLAGS="-fno-reorder-blocks-and-partition" \
      -DCMAKE_EXE_LINKER_FLAGS="-Wl,-q" \
      -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
      -DBUILD_AVX512=off \
      -DBUILD_AVX512VBMI=off ..
make -j32 all
popd
```

2. DO BOLT
```
llvm-bolt build/bin/unit-hyperscan -o build/bin/unit-hyperscan.opt
```

3. CHECK
unit-hyperscan works well, but BOLTed unit-hyperscan.opt SEGV.

```
$./build/bin/unit-hyperscan | head
[==========] Running 4130 tests from 33 test cases.
[----------] Global test environment set-up.
[----------] 9 tests from CustomAllocator
[ RUN      ] CustomAllocator.DatabaseInfoBadAlloc
[       OK ] CustomAllocator.DatabaseInfoBadAlloc (0 ms)
[ RUN      ] CustomAllocator.TwoAlignedCompile
[       OK ] CustomAllocator.TwoAlignedCompile (0 ms)
[ RUN      ] CustomAllocator.TwoAlignedCompileError
[       OK ] CustomAllocator.TwoAlignedCompileError (1 ms)
[ RUN      ] CustomAllocator.TwoAlignedDatabaseInfo
```

```
$./build/bin/unit-hyperscan.opt
Segmentation fault
```

4. Check .rela.plt

The order of jump_slop entries are messed up with R_X86_64_IRELATIVE. So when dl_fixup tries to resolve _ZNSt12out_of_rangeC1E at PLT, then it will find `R_X86_64_IRELATIV 12584c0` (DT_JMPREL + RELASZ * reloc_index => now the entry of .rela.plt changed from _ZNSt12out_of_rangeC1E to an IRELATIVE entry). 

```
$readelf -r ./build/bin/unit-hyperscan | grep ".rela.plt" -A 10
Relocation section '.rela.plt' at offset 0x17b80 contains 215 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000ca8018  000100000007 R_X86_64_JUMP_SLO 0000000000000000 ftell at GLIBC_2.2.5 + 0
000000ca8020  000200000007 R_X86_64_JUMP_SLO 0000000000000000 _Znam at GLIBCXX_3.4 + 0
000000ca8028  000300000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSo3putEc at GLIBCXX_3.4 + 0
000000ca8030  000400000007 R_X86_64_JUMP_SLO 0000000000000000 __errno_location at GLIBC_2.2.5 + 0
000000ca8038  000500000007 R_X86_64_JUMP_SLO 0000000000000000 printf at GLIBC_2.2.5 + 0
000000ca8040  000600000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSt8__detail15_List_ at GLIBCXX_3.4.15 + 0
000000ca8048  000700000007 R_X86_64_JUMP_SLO 0000000000000000 isspace at GLIBC_2.2.5 + 0
000000ca8058  000800000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSt12out_of_rangeC1E at GLIBCXX_3.4.21 + 0
000000ca8068  000900000007 R_X86_64_JUMP_SLO 0000000000000000 _ZSt29_Rb_tree_insert_ at GLIBCXX_3.4 + 0
```

```
$readelf -r ./build/bin/unit-hyperscan.opt | grep ".rela.plt" -A 100
Relocation section '.rela.plt' at offset 0x17b80 contains 215 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000ca8018  000100000007 R_X86_64_JUMP_SLO 0000000000000000 ftell at GLIBC_2.2.5 + 0
000000ca8020  000200000007 R_X86_64_JUMP_SLO 0000000000000000 _Znam at GLIBCXX_3.4 + 0
000000ca8028  000300000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSo3putEc at GLIBCXX_3.4 + 0
000000ca8030  000400000007 R_X86_64_JUMP_SLO 0000000000000000 __errno_location at GLIBC_2.2.5 + 0
000000ca8038  000500000007 R_X86_64_JUMP_SLO 0000000000000000 printf at GLIBC_2.2.5 + 0
000000ca8040  000600000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSt8__detail15_List_ at GLIBCXX_3.4.15 + 0
000000ca8048  000700000007 R_X86_64_JUMP_SLO 0000000000000000 isspace at GLIBC_2.2.5 + 0
000000ca8050  000000000025 R_X86_64_IRELATIV                    12584c0
000000ca8058  000800000007 R_X86_64_JUMP_SLO 0000000000000000 _ZNSt12out_of_rangeC1E at GLIBCXX_3.4.21 + 0
000000ca8060  000000000025 R_X86_64_IRELATIV                    1257e80
```


This issue depends on the Arch and linker you use, and I only found it happened with X86 & gnu-ld so far.

https://github.com/llvm/llvm-project/pull/106515


More information about the llvm-commits mailing list