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

patchelf corrupts shared libraries #1706

Closed
bluelightning32 opened this issue Oct 24, 2017 · 7 comments
Closed

patchelf corrupts shared libraries #1706

bluelightning32 opened this issue Oct 24, 2017 · 7 comments

Comments

@bluelightning32
Copy link

The current version of patchelf on termux, 0.9-1, corrupts shared libraries when it edits them on arm and aarch64. I didn't test other platforms.

Here's a repro of the problem, where the libc dependency is replaced with libc (should be a no-op). The example output is from aarch64, but arm output is similar:

$ cat >shared.c <<EOF
#include <stdio.h>

void shared() {
    printf("shared called\n");
}
EOF

$ cat >main.c  <<EOF
#include <stdio.h>

extern void shared();

int main(int argc, const char *argv[]) {
    shared();
        return 0;
}
EOF

$ gcc -shared -o libshared.so shared.c

$ gcc -o main main.c $PWD/libshared.so 

$ ./main
shared called

$ patchelf --replace-needed libc.so libc.so libshared.so 

$ ./main
CANNOT LINK EXECUTABLE "./main": "/data/data/com.termux/files/home/tmp/libshared.so" loaded phdr 0x7571ad5000 not in loadable segment
Aborted

Please upgrade the version of patchelf in the termux package repository to 0.10. Note that 0.10 isn't released yet. Any pre-released git commit should work. I verified 29c085f works.

$ gcc -shared -o libshared.so shared.c

$ patchelf --replace-needed libc.so libc.so libshared.so 

$ ./main 
shared called
@ghost
Copy link

ghost commented Oct 24, 2017

Patchelf at commit 29c085f is not working:

[termux]:~:$ ./patchelf --version
patchelf 0.10
[termux]:~:$ cat main.c
#include <stdio.h>

extern void shared();

int main(int argc, const char *argv[]) {
    shared();
        return 0;
}
[termux]:~:$ cat shared.c
#include <stdio.h>

void shared() {
    printf("shared called\n");
}
[termux]:~:$ clang -shared shared.c -o libshared.so
[termux]:~:$ clang main.c -o main libshared.so
[termux]:~:$ ./main
shared called
[termux]:~:$ ./patchelf --replace-needed libc.so libc.so libshared.so
[termux]:~:$ ./main
CANNOT LINK EXECUTABLE: invalid ELF file "/data/data/com.termux/files/home/libshared.so" load segment[5]: p_offset (0x2000) + p_filesz (0xd8) ( = 0x20d8) past end of file (0x20d8)
page record for 0x7f8031a010 was not found (block_size=16)

@bluelightning32
Copy link
Author

I tried using clang like you did, and 29c085f still works for me. Although I had to do this instead to link main:

bash-4.4$ clang main.c -o main libshared.so
bash-4.4$ ./main
CANNOT LINK EXECUTABLE "./main": library "libshared.so" not found
Aborted
bash-4.4$ clang main.c -o main $PWD/libshared.so
bash-4.4$ ./main
shared called
bash-4.4$ patchelf --version
patchelf 0.10
bash-4.4$ patchelf --replace-needed libc.so libc.so libshared.so 
bash-4.4$ ./main
shared called

Which platform did you test on? Did patchelf 0.9 work on your platform?
Here's what I'm running:

bash-4.4$ termux-info
android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
	at android.os.BinderProxy.transactNative(Native Method)
	at android.os.BinderProxy.transact(Binder.java:748)
	at android.os.BinderProxy.shellCommand(Binder.java:802)
	at com.android.commands.pm.Pm.runShellCommand(Pm.java:334)
	at com.android.commands.pm.Pm.runList(Pm.java:722)
	at com.android.commands.pm.Pm.run(Pm.java:138)
	at com.android.commands.pm.Pm.main(Pm.java:107)
	at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
	at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:284)
Termux version:
<=0.48
Installed Termux apps:
Updatable packages:
All packages up to date
System information:
Linux localhost 3.10.73-ga51b1600b7f8 #1 SMP PREEMPT Thu Aug 17 22:07:37 UTC 2017 aarch64 Android
Termux-packages arch:
aarch64
Android version:
8.0.0
Device manufacturer:
LGE
Device model:
Nexus 5X

@bluelightning32
Copy link
Author

I did a git bisect. This is the commit that fixes it for me:
NixOS/patchelf@c4deb5e

@ghost
Copy link

ghost commented Oct 25, 2017

My platform:

System information:
Linux localhost 3.18.76 #1 SMP Thu Oct 19 10:51:27 UTC 2017 aarch64 Android
Termux-packages arch:
aarch64
Android version:
6.0.1
Device manufacturer:
samsung
Device model:
SM-G930F

Patchelf v0.9 or latest corrupts shared libraries. Each commit from 44b7f95 to 29c085f not working correctly. In chrooted environment based on GNU libc patchelf v0.9 works fine.

I have tested it on ARM (32bit) device with android 7.1.2 (LineageOS) - patchelf v0.9 corrupts libraries but latest (commit 29c085f) is working fine.
Info about ARM device:

System information:
Linux localhost 3.0.101-lineage-g132101c #1 SMP PREEMPT Wed Jul 19 10:11:37 CEST 2017 armv7l Android
Termux-packages arch:
arm
Android version:
7.1.2
Device manufacturer:
samsung
Device model:
GT-I9300

@fornwall
Copy link
Member

Should we updated the patchelf package to build from the latest git master of patchelf?

@ghost
Copy link

ghost commented Oct 25, 2017

@fornwall yes, patchelf can be updated to latest git master, at least this solves problem on android 7.1+.

@fornwall
Copy link
Member

Great! I've updated patchelf to current git master (29c085fd9d3fc972f75b3961905d6b4ecce7eb2b), it's versioned as 0.9.0.1 for now until a proper upstream release is made.

This updated package is now available for installation. Nice work @bluelightning32 and @xeffyr !

@ghost ghost locked and limited conversation to collaborators Oct 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants