Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing rpath causes Exec format error on FreeBSD #36

Open
dmilith opened this issue Aug 31, 2014 · 7 comments
Open

Changing rpath causes Exec format error on FreeBSD #36

dmilith opened this issue Aug 31, 2014 · 7 comments

Comments

@dmilith
Copy link

dmilith commented Aug 31, 2014

vks01# cp which git /tmp
vks01# ./patchelf --set-rpath /Software/Git /tmp/git
vks01# ldd /tmp/git
/tmp/git:
libz.so.1 => not found (0)
libiconv.so.3 => not found (0)
libcrypto.so.1.0.0 => not found (0)
libthr.so.3 => /lib/libthr.so.3 (0x800948000)
libc.so.7 => /lib/libc.so.7 (0x800b6b000)
vks01# ./patchelf --set-rpath /Software/Git/lib /tmp/git
vks01# ldd /tmp/git
/tmp/git:
ldd: /tmp/git: Exec format error
/tmp/git: exit status 1

@dmilith
Copy link
Author

dmilith commented Sep 2, 2014

I tried changing "elf.h" to sys/elf.h, but same issue happens. There's no way to set back rpath which is longer than current one.

ng0# cp /Software/Git/bin/git /tmp/git
ng0# ldd /tmp/git
/tmp/git:
    libz.so.1 => /Software/Git/lib/libz.so.1 (0x800948000)
    libiconv.so.3 => /Software/Git/lib/libiconv.so.3 (0x800b5c000)
    libcrypto.so.1.0.0 => /Software/Git/lib/libcrypto.so.1.0.0 (0x800e57000)
    libthr.so.3 => /lib/libthr.so.3 (0x801233000)
    libc.so.7 => /lib/libc.so.7 (0x801456000)
ng0# ./patchelf --debug --set-rpath /Software /tmp/git
patching ELF file `/tmp/git'
new rpath is `/Software'
ng0# ./patchelf --debug --set-rpath /Software/Git/lib /tmp/git
patching ELF file `/tmp/git'
new rpath is `/Software/Git/lib'
rpath is too long, resizing...
replacing section `.dynstr' with size 4410
this is an executable
using replaced section `.dynstr'
last replaced is 6
looking at section `.interp'
replacing section `.interp' which is in the way
looking at section `.note.ABI-tag'
replacing section `.note.ABI-tag' which is in the way
looking at section `.hash'
replacing section `.hash' which is in the way
looking at section `.gnu.hash'
replacing section `.gnu.hash' which is in the way
looking at section `.dynsym'
replacing section `.dynsym' which is in the way
looking at section `.dynstr'
first reserved offset/addr is 0x4200/0x404200
first page is 0x400000
needed space is 16920
needed space is 16976
needed pages is 1
changing alignment of program header 2 from 2097152 to 4096
changing alignment of program header 3 from 2097152 to 4096
clearing first 20424 bytes
rewriting section `.dynstr' from offset 0x40d8 (size 4392) to offset 0x238 (size 4410)
rewriting section `.dynsym' from offset 0x2080 (size 8280) to offset 0x1378 (size 8280)
rewriting section `.gnu.hash' from offset 0x1bb8 (size 1220) to offset 0x33d0 (size 1220)
rewriting section `.hash' from offset 0x1230 (size 2440) to offset 0x3898 (size 2440)
rewriting section `.interp' from offset 0x1200 (size 21) to offset 0x4220 (size 21)
rewriting section `.note.ABI-tag' from offset 0x1218 (size 24) to offset 0x4238 (size 24)
rewriting symbol table section 2
ng0# ldd /tmp/git
/tmp/git:
ldd: /tmp/git: Exec format error
/tmp/git: exit status 1
ng0#

@dmilith
Copy link
Author

dmilith commented Sep 2, 2014

I also tried to patch unstripped binary. Still shows zsh: exec format error: /tmp/tig, although nm shows full list of symbols.

@dmilith
Copy link
Author

dmilith commented Sep 2, 2014

After patch (shorter rpath). Readelf info:

ng0#  readelf -d /tmp/tig

Dynamic section at offset 0x1ff90 contains 25 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libiconv.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libncursesw.so.8]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.7]
 0x000000000000000f (RPATH)              Library rpath: [/Software/Tig/lib]
 0x000000000000001d (RUNPATH)            Library runpath: [/Software/Tig/lib]
 0x000000000000000c (INIT)               0x402438
 0x000000000000000d (FINI)               0x4171c4
 0x0000000000000004 (HASH)               0x400370
 0x000000006ffffef5 (GNU_HASH)           0x400328
 0x0000000000000005 (STRTAB)             0x3ff238
 0x0000000000000006 (SYMTAB)             0x3ff6c8
 0x000000000000000a (STRSZ)              1162 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x61f1d8
 0x0000000000000002 (PLTRELSZ)           2640 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4019e8
 0x0000000000000007 (RELA)               0x4018b0
 0x0000000000000008 (RELASZ)             312 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x401880
 0x000000006fffffff (VERNEEDNUM)         1
 0x000000006ffffff0 (VERSYM)             0x401778
 0x0000000000000000 (NULL)               0x0

So the RPATH was correctly set back to /Software/Tig/lib .. so that worked. Interesting..

@dmilith
Copy link
Author

dmilith commented Sep 2, 2014

What's even more interesting.. If I also set interpreter, tig is working:

ng0# patchelf --debug --set-rpath /Software/Tig/lib --set-interpreter /libexec/ld-elf.so.1 /tmp/tig
patching ELF file `/tmp/tig'
new rpath is `/Software/Tig/lib'
rpath is too long, resizing...
replacing section `.dynstr' with size 1180
replacing section `.interp' with size 21
this is an executable
using replaced section `.dynstr'
using replaced section `.interp'
last replaced is 2
looking at section `.dynstr'
looking at section `.interp'
first reserved offset/addr is 0x738/0x3fd738
first page is 0x3fd000
needed space is 1888
needed space is 1944
needed pages is 1
clearing first 5208 bytes
rewriting section `.dynstr' from offset 0x12a8 (size 1162) to offset 0x2e0 (size 1180)
rewriting section `.interp' from offset 0x1738 (size 21) to offset 0x780 (size 21)
rewriting symbol table section 3
rewriting symbol table section 29
ng0# /tmp/tig
tig: Not a git repository

@darealshinji
Copy link
Contributor

Does the version from the ports work?

@dmilith
Copy link
Author

dmilith commented Sep 2, 2014

I have no idea, didn't use ports.

@Peter2121
Copy link

Peter2121 commented Jun 8, 2016

The same problem with 0.9 version and ports version (both tested).
BTW, is FreeBSD binaries patching supported?
Trying to patch mc built from ports:

patchelf --set-rpath /usr/ports/
misc/mc/work/stage/usr/local/lib --set-interpreter /libexec/ld-elf.so.1 --debug mc
patching ELF file mc' new rpath is/usr/ports/misc/mc/work/stage/usr/local/lib'
rpath is too long, resizing...
DT_NULL index is 27
replacing section .dynamic' with size 544 replacing section.dynstr' with size 12186
replacing section .interp' with size 21 this is an executable using replaced section.interp'
using replaced section .dynstr' using replaced section.dynamic'
last replaced is 22
looking at section .interp' looking at section.note.tag'
replacing section .note.tag' which is in the way looking at section.hash'
replacing section .hash' which is in the way looking at section.gnu.hash'
replacing section .gnu.hash' which is in the way looking at section.dynsym'
replacing section .dynsym' which is in the way looking at section.dynstr'
looking at section .gnu.version' first reserved offset/addr is 0x9a4e/0x409a4e first page is 0x400000 needed space is 40096 needed space is 40152 needed pages is 1 changing alignment of program header 2 from 2097152 to 4096 changing alignment of program header 3 from 2097152 to 4096 clearing first 43030 bytes rewriting section.dynamic' from offset 0x10f880 (size 528) to offset 0x238 (size 544)
rewriting section .dynstr' from offset 0x7ae0 (size 12142) to offset 0x458 (size 12186) rewriting section.dynsym' from offset 0x3070 (size 19056) to offset 0x33f8 (size 19056)
rewriting section .gnu.hash' from offset 0x26e0 (size 2444) to offset 0x7e68 (size 2444) rewriting section.hash' from offset 0x1248 (size 5268) to offset 0x87f8 (size 5268)
rewriting section .interp' from offset 0x1200 (size 21) to offset 0x9c90 (size 21) rewriting section.note.tag' from offset 0x1218 (size 48) to offset 0x9ca8 (size 48)
./mc
./mc: Exec format error. Binary file not executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants