参数来构造函数样式函数 [英] Arguments to attribute constructor style functions

查看:146
本文介绍了参数来构造函数样式函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这样的代码:(基于Apple的代码)

$ p $ __属性__((构造函数))
void do_action(int argc,const char ** argv,const char ** envp,const char **,struct ProgramVars *)
{
//
}

这很奇怪,因为我读到构造函数样式函数应该是无效的。这些论据来自哪里,我可以选择这些参数是什么?这是gcc / clang的苹果电脑吗?

此代码应该与DYLD_INSERT_LIBRARIES(Linux的LD_PRELOAD)一起使用。
这是获取参数的一个特殊原因吗?

解决方案

具有构造函数可以与 main 具有相同的参数。他们无法区别。 LD_PRELOAD与它没有任何共同之处。此功能主要基于glibc库(不知道其他人),并且对于所有使用glibc和支持构造函数属性的编译器都是可接受的。在Linux上它工作正常。



看一段代码,你的函数 do_action 被调用:(* __ init_array_start [i])(argc, argv,envp);

它放在路径* csu / elf-init.c中的glibc库中。

我也认为,你的参数 struct ProgramVars * 不能被赋值,只能是 nullptr


I've seen code like this: (Apple based code)

__attribute__((constructor))
void do_action(int argc, const char **argv, const char **envp, const char **things, struct ProgramVars *)
{
   //
}

Which is odd to be because I read that constructors style functions are supposed to be void. Where are those arguments coming from, can I choose what those parameters can be? Is this an Apple only thing for gcc/clang?

This code is supposed to be used with DYLD_INSERT_LIBRARIES, (Linux's LD_PRELOAD). Is that a special reason why it gets arguments?

解决方案

Functions with attribute constructor can have the same arguments as main has. They can't differ. LD_PRELOAD has nothing common with it. This feature is heavily based on glibc library (don't know about others) and is acceptable for all compilers, that use glibc and support constructor attribute. On Linux it works fine.

Look at the piece of code, where your function do_action is called:

for (size_t i = 0; i < size; i++)
  (*__init_array_start [i]) (argc, argv, envp);

It is placed in glibc library within path *csu/elf-init.c

I also deem, that your argument struct ProgramVars * can't be assigned and can be only nullptr.

这篇关于参数来构造函数样式函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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