OSX上的LD_PRELOAD的确切等效参数是什么? [英] What is the exact equivalent to LD_PRELOAD on OSX?

查看:109
本文介绍了OSX上的LD_PRELOAD的确切等效参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LD_PRELOAD 来挂钩库函数,并且在Linux中它完美地工作。但我无法弄清楚如何在OSX中做同样的事情。



我在Linux上的设置如下:

代码是:

  #include< stdio.h> 
#include< dlfcn.h>
#include< stdlib.h>
#include< unistd.h>
#include< ruby​​.h>

void
rb_raise(unsigned long exc,const char * fmt,...)
{
static void(* libruby_rb_raise)
(unsigned long exc,const char * fmt,...)= NULL;

void * handle;
char *错误;

if(!libruby_rb_raise){
handle = dlopen(/ path / to / libruby.so,
RTLD_LAZY);
if(!handle){
fputs(dlerror(),stderr);
exit(1);
}
libruby_rb_raise = dlsym(句柄,rb_raise); ((error = dlerror())!= NULL){
fprintf(stderr,%s \ n,error);
exit(1);
}
}

// ...代码...

return Qnil;
}

然后我可以编译它:

  gcc -Wall -O2 -fpic -shared -ldl -g -I / path / to / includes / -o raise_shim.so raise_shim.c 

然后执行:

  LD_PRELOAD = ./ raise_shim.so ruby​​ 

以上所有内容在Linux上运行良好,什么是每个步骤使OSX上的这个工作相当于什么?我已经搜索了这一点,并没有能够得到它所提供的信息,因为一些步骤的信息丢失。



提前致谢!

p>

解决方案

请看 DYLD_INSERT_LIBRARIES 。这是你正在寻找的变量。



另见这个答案


I am using LD_PRELOAD to hook a library function, and in Linux it works perfectly. But I cannot figure out how to do the equivalent in OSX.

The setup I have on Linux is as follows:

The code is:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>

void
rb_raise(unsigned long exc, const char *fmt, ...)
{
  static void (*libruby_rb_raise)
    (unsigned long exc, const char *fmt, ...) = NULL;

  void * handle;
  char * error;

  if (!libruby_rb_raise) {
    handle = dlopen("/path/to/libruby.so",
                    RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libruby_rb_raise = dlsym(handle, "rb_raise");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }

  // ...code... 

  return Qnil;
}

Which I then compile with:

gcc -Wall -O2 -fpic -shared -ldl -g -I/path/to/includes/ -o raise_shim.so raise_shim.c

I then execute with:

LD_PRELOAD=./raise_shim.so ruby

All of the above works well on Linux, what is the equivalent for each step to get this working on OSX? I have googled this and have not been able to get it to work with the information provided as the info for some of the steps are missing.

Thanks in advance!

解决方案

Take a look at DYLD_INSERT_LIBRARIES. That's the variable you're looking for.

See also this answer.

这篇关于OSX上的LD_PRELOAD的确切等效参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆