unexpected reloc type問題分析

sciapex發表於2018-09-17

 

1.現象,程式在啟動的時候報如下錯誤
error while loading shared libraries: /home/test/lib/libtest.so: unexpected reloc type 0x03

2.分析,可以通過readelf進行檢視依賴的目標檔案是否存在non-pic情況,對比新舊版本
readelf -d libtest.so
PIC:Position Independent Code.
有TEXTREL說明該目標檔案是non-PIC, 沒有則是PIC的
non-PIC在使用-fPIC選項會出現問題
libtest.so.new
Dynamic section at offset 0x19d928 contains 37 entries:
Tag Type Name/Value
0x00000003 (PLTGOT) 0x1a2280
0x00000002 (PLTRELSZ) 11936 (bytes)
0x00000017 (JMPREL) 0x211b8
0x00000014 (PLTREL) REL
0x00000011 (REL) 0x1e020
0x00000012 (RELSZ) 12696 (bytes)
0x00000013 (RELENT) 8 (bytes)
0x6ffffffa (RELCOUNT) 341
0x00000006 (SYMTAB) 0x158
0x0000000b (SYMENT) 16 (bytes)
0x00000005 (STRTAB) 0xa3a8
0x0000000a (STRSZ) 55275 (bytes)
0x6ffffef5 (GNU_HASH) 0x17b94
0x00000001 (NEEDED) Shared library: [libpthread.so.0]
0x00000001 (NEEDED) Shared library: [libglib-2.0.so.0]
0x00000001 (NEEDED) Shared library: [librt.so.1]
0x00000001 (NEEDED) Shared library: [libstdc++.so.6]
0x00000001 (NEEDED) Shared library: [libm.so.6]
0x00000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x00000001 (NEEDED) Shared library: [libc.so.6]

0x0000000c (INIT) 0x24058
0x0000000d (FINI) 0xe2138
0x00000019 (INIT_ARRAY) 0x19f328
0x0000001b (INIT_ARRAYSZ) 16 (bytes)
0x0000001a (FINI_ARRAY) 0x19f338
0x0000001c (FINI_ARRAYSZ) 4 (bytes)
0x00000016 (TEXTREL) 0x0
0x0000001e (FLAGS) TEXTREL
0x6ffffff0 (VERSYM) 0x1caf8
0x6ffffffc (VERDEF) 0x1df44
0x6ffffffd (VERDEFNUM) 1
0x6ffffffe (VERNEED) 0x1df60
0x6fffffff (VERNEEDNUM) 5
0x00000000 (NULL) 0x0

libtest.so.old
Dynamic section at offset 0x1915c0 contains 35 entries:
Tag Type Name/Value
0x00000003 (PLTGOT) 0x195f18
0x00000002 (PLTRELSZ) 11968 (bytes)
0x00000017 (JMPREL) 0x20630
0x00000014 (PLTREL) REL
0x00000011 (REL) 0x1d4b0
0x00000012 (RELSZ) 12672 (bytes)
0x00000013 (RELENT) 8 (bytes)
0x6ffffffa (RELCOUNT) 341
0x00000006 (SYMTAB) 0x158
0x0000000b (SYMENT) 16 (bytes)
0x00000005 (STRTAB) 0x9e38
0x0000000a (STRSZ) 54259 (bytes)
0x6ffffef5 (GNU_HASH) 0x1722c

0x0000000c (INIT) 0x234f0
0x0000000d (FINI) 0xd482c
0x00000019 (INIT_ARRAY) 0x192fb0
0x0000001b (INIT_ARRAYSZ) 16 (bytes)
0x0000001a (FINI_ARRAY) 0x192fc0
0x0000001c (FINI_ARRAYSZ) 4 (bytes)
0x6ffffff0 (VERSYM) 0x1c038
0x6ffffffc (VERDEF) 0x1d3d4
0x6ffffffd (VERDEFNUM) 1
0x6ffffffe (VERNEED) 0x1d3f0
0x6fffffff (VERNEEDNUM) 5
0x00000000 (NULL) 0x0

執行如下命令檢視目標檔案的重定向型別,存在R_ARM_REL32說明定向存在偏移,
如果存在該重定向型別,還可以看到涉及的function,從這個function判斷該功能出自哪個庫
readelf -r libtest.so|grep R_ARM_REL32

如果使用-fPIC,這個函式的重定向型別是R_ARM_GLOB_DAT

3.解決,用新的編譯工具使用-fPIC(-fpic -pie)選項重新編譯相關依賴庫檔案,依賴庫目標檔案避免出現TEXTREL標識。

 

相關文章