[FreeBSD-users-jp 95919] Re: LD_PRELOAD がうまくいかない

岡部 勝幸 HGC02147 @ nifty.com
2016年 8月 10日 (水) 00:56:59 UTC


岡部です。

9.3-RELEASEではなく10.3-STABLEですが、LD_PRELOADの設定反映されますよ。
ソースの方に綴り間違え等ありませんか?

以下、試した内容です。

> % cat random_num.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int
> main(int argc, char **argv)
> {
>   srand(time(NULL));
>   int i = 10;
>   while (i--)
>     printf("%d\n", rand());
>   return 0;
> }
>
> % cc -o random_num random_num.c
> % ./random_num
> 2016130939
> 2115726213
> 976251671
> 1086788223
> 1301263032
> 354334582
> 329183349
> 666688777
> 1616105446
> 511080472
> % cat unrandom.c
> #include <stdlib.h>
>
> int
> rand(void)
> {
>   return 42;
> }
>
> % cc -shared -fPIC -o unrandom.so unrandom.c
> % LD_PRELOAD=./unrandom.so ./random_num
> 42
> 42
> 42
> 42
> 42
> 42
> 42
> 42
> 42
> 42
> % LD_PRELOAD=./unrandom.so ldd ./random_num
> ./random_num:
>         libc.so.7 => /lib/libc.so.7 (0x800a21000)
> %

----
岡部 勝幸

On 2016/08/07 12:09, User Ribbon wrote:
> 実行時に動的ライブラリを差し替える手段として、LD_PRELOAD 環境変数
> があります。使用例としては、
>
> https://siguniang.wordpress.com/2015/05/15/override-functions-with-ld_preload/
>
> などがあります。
> FreeBSDでも動くと思っててストしてみたのですが動きませんでした。
>
> 上記URLにあるサンプルプログラムを使い、
>
> setenv LD_PRELOAD ./unrandom.so
>
> をしたあと、
> %ldd random_num
> としても、
> random_num:
>         libc.so.7 => /lib/libc.so.7 (0x800a1b000)
>
> となり、LD_PRELOAD した結果が反映されません。
> 動作環境は
> FreeBSD bsd09-64 9.3-RELEASE-p43 FreeBSD 9.3-RELEASE-p43 #0: Sat May 28 00:19:32 UTC 2016
> です。
>
> 何かほかに設定しないと動かないのでしょうか。
>
> ribbon



freebsd-users-jp メーリングリストの案内