3

I wonder if LD_PRELOAD is now supported with the newer Android-versions?

At the time of 4.0 ICS it wasn't, and in the documentation (NDK docs/SYSTEM-ISSUES.html) there's still:

No support for LD_LIBRARY_PATH, LD_PRELOAD, RTLD_LOCAL and many other options.

But some days ago I have used LD_LIBRARY_PATH on my Android 4.2 Galaxy Nexus and it worked (!).

Thanks in advance!

Martin L.
  • 3,006
  • 6
  • 36
  • 60
  • I can recall getting one of those to work, but one wonders what you plan to do with it - during normal Android operation, exec() is not really used, so there isn't much opportunity for such a setting to have an effect. If you want to know definitively, check the sources, run strings on the linker binary, or test it. – Chris Stratton Jan 20 '13 at 15:58
  • I have never used LD_PRELOAD before, but it wonder if I could do something like this in android: http://www.linuxjournal.com/article/7795 – Martin L. Jan 20 '13 at 16:00
  • But use it on what? If you are dealing with proper Android code, you'd be loading a jni library into an existing process, in which case you can simply load the library with the replacement functions first - though beware that most of the system libraries are pre-loaded long before the first line of code you wrote runs. The case where linker preload variables would actually apply is if you are actually executing binaries - operating more in the unix tradition than the android one. – Chris Stratton Jan 20 '13 at 16:03

2 Answers2

6

Although the offical NDK-r9d documentation still says the same, but it turns out that LD_PRELOAD does work on rooted devices. Run

adb shell
setprop wrap.com.xyz.yourapp LD_PRELOAD=/path/to/your/library.so
stop
start

after pushing the library to the device. I tried it successfully on ANDROID-19 emulator to provide alternate definition of a function.

Check out this link - http://cedricvb.be/post/intercepting-android-native-library-calls/

narayan
  • 1,119
  • 13
  • 20
  • Too late for me, but definitely a good hint and very interesting :) – Martin L. Jun 06 '14 at 15:07
  • 1
    Awesome answer. There's one gotcha though that a property value must be no bigger than 91 characters...so you need to make sure the path/to/your/library.so is short (especially if you want multiple libraries). – JonnyBoy Sep 12 '14 at 22:05
0

Generally speaking, LD_LIBRARY_PATH has worked on engineering builds (a.k.a. rooted devices) for quite a while. The problem is that this does not help for production builds - not because the loader is changed, but because the environment for your app is secured.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Hm yes, my device is a rooted one. Does this also mean, that I can assume that LD_LIBRARY_PATH (or better LD_PRELOAD) exists on "all" routed devices? I have already accepted, that my app will require are rooted device (sadly). As I already sad - I don't even know if LD_PRELOAD would work, I'm new at this. – Martin L. Jan 20 '13 at 17:26
  • Looking at AOSP bionic/linker/linker.c, `LD_PRELOAD` is not parsed. The same proves that you can assume that changes to `LD_LIBRARY_PATH` will be respected by the loader on all rooted devices. See https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/m6OddFQINxs and https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/2iy4zeyoVKU. Essentially, LD_LIBRARY_PATH is parsed when the loader (or the process, if you care) starts up, which can be controlled only on rooted devices. – Alex Cohn Jan 20 '13 at 18:21
  • Thank you for your response, that helped. Now, I have to look if I can do something with this :) – Martin L. Jan 20 '13 at 20:14
  • What do you need to achieve? – Alex Cohn Jan 20 '13 at 21:08
  • I would like to capture outgoing audio and stream it to remote speakers. But in general, I would like to learn how android works. This evening, I found something about libaudio.so: http://stackoverflow.com/questions/14430045/is-android-always-using-tinyalsa – Martin L. Jan 20 '13 at 22:29
  • It would be kind of you, if you could look into this again. :) – erbdex Jan 23 '14 at 05:59
  • @erbdex: Not that something really happened in the last 12 months about it, except that all of us got 1 year older and wiser ;-) What do you expect from reopening this discussion? You can stream all audio output elsewhere (e.g. to external speakers) with a custom ROM, or on a rooted device. And anyways, tweaking `LD_LIBRARY_PATH` is unlikely to help. – Alex Cohn Jan 23 '14 at 08:23
  • What [eriksen](https://github.com/mariusaeriksen/trickle) did was to exploit the unix's dynamic loader's pre-loading functionality (`LD_PRELOAD`) to place Trickle's code as a middle-ware between the socket calls and `libc` implementation to achieve rate-limiting. I want to do the same thing on android.So i am looking for an authoritative take on `LD_PRELOAD`'s support on android. – erbdex Jan 23 '14 at 10:00