C++ 如何触发c';可执行共享库(.so)中全局变量的TOR?

C++ 如何触发c';可执行共享库(.so)中全局变量的TOR?,c++,linker,shared-libraries,factory,elf,C++,Linker,Shared Libraries,Factory,Elf,我有一个共享库,我想使其可执行,类似于libc。当库执行时,我希望它转储在特定抽象工厂中注册的类的名称列表(这是C++)。我使用标准技术,通过初始化/构造全局变量向工厂注册类 有几个教程介绍如何使共享库可执行(例如,和)。这是相对直接的。然而,当我尝试它时,我发现入口点在调用任何全局构造函数之前执行。在我的例子中,这意味着没有类在我的工厂注册,所以我没有信息可以打印出来 我想在调用构造函数之后执行入口点,或者从入口点函数学习如何自己触发构造。这可能吗?有人知道怎么做吗?也许有一个内部libc函数

我有一个共享库,我想使其可执行,类似于libc。当库执行时,我希望它转储在特定抽象工厂中注册的类的名称列表(这是C++)。我使用标准技术,通过初始化/构造全局变量向工厂注册类

有几个教程介绍如何使共享库可执行(例如,和)。这是相对直接的。然而,当我尝试它时,我发现入口点在调用任何全局构造函数之前执行。在我的例子中,这意味着没有类在我的工厂注册,所以我没有信息可以打印出来


我想在调用构造函数之后执行入口点,或者从入口点函数学习如何自己触发构造。这可能吗?有人知道怎么做吗?也许有一个内部libc函数我可以外部调用?

我相信我已经找到了一个可行的解决方案。它基于创建
-nostlib
可执行文件(如操作系统内核)的技术。但是,在这种情况下,我们的共享库仍然链接标准库。我发现RaspberryPi论坛的帖子特别有用

解决方案是手动执行存储在共享库的嵌入式
init_数组
中的函数指针。诀窍是使用链接器脚本定义用于访问此数组的指针。然后,我们将
extern
这些指针放入程序代码中。我们也可以重复执行析构函数的过程

test.cpp
中,我们有以下内容:

#include <cstdio>
#include <unistd.h>

class Test
{
public:
    Test() { printf("Hello world!\n"); }
    ~Test() { printf("Goodbye world!\n"); }
};

Test myGlobal;  // a global class instance

extern "C"
{
// ensures linker generates executable .so (assuming x86_64)
extern const char test_interp[] __attribute__((section(".interp"))) =
    "/lib64/ld-linux-x86-64.so.2";

// function walks the init_array and executes constructors
void manual_init(void)
{
    typedef void (*constructor_t)(void);
    // _manual_init_array_start and _manual_init_array_end
    // are created by linker script.
    extern constructor_t _manual_init_array_start[];
    extern constructor_t _manual_init_array_end[];

    constructor_t *fn = _manual_init_array_start;
    while(fn < _manual_init_array_end)
    {
        (*fn++)();
    }
}

// function walks the fini_array and executes destructors
void manual_fini(void)
{
    typedef void (*destructor_t)(void);
    // _manual_init_array_start and _manual_init_array_end
    // are created by linker script.
    extern destructor_t _manual_fini_array_start[];
    extern destructor_t _manual_fini_array_end[];

    destructor_t *fn = _manual_fini_array_start;
    while(fn < _manual_fini_array_end)
    {
        (*fn++)();
    }
}

// entry point for libtest.so when it is executed
void lib_entry(void)
{
    manual_init();  // call ctors
    printf("Entry point of the library!\n");
    manual_fini();  // call dtors

    _exit(0);
}
 g++ test.cpp -fPIC -Wl,-T,test.ld -Wl,-e,lib_entry -shared -o libtest.so
我们必须为链接器提供两个参数:(1)链接器脚本和(2)库的入口点。要进行编译,我们将执行以下操作:

#include <cstdio>
#include <unistd.h>

class Test
{
public:
    Test() { printf("Hello world!\n"); }
    ~Test() { printf("Goodbye world!\n"); }
};

Test myGlobal;  // a global class instance

extern "C"
{
// ensures linker generates executable .so (assuming x86_64)
extern const char test_interp[] __attribute__((section(".interp"))) =
    "/lib64/ld-linux-x86-64.so.2";

// function walks the init_array and executes constructors
void manual_init(void)
{
    typedef void (*constructor_t)(void);
    // _manual_init_array_start and _manual_init_array_end
    // are created by linker script.
    extern constructor_t _manual_init_array_start[];
    extern constructor_t _manual_init_array_end[];

    constructor_t *fn = _manual_init_array_start;
    while(fn < _manual_init_array_end)
    {
        (*fn++)();
    }
}

// function walks the fini_array and executes destructors
void manual_fini(void)
{
    typedef void (*destructor_t)(void);
    // _manual_init_array_start and _manual_init_array_end
    // are created by linker script.
    extern destructor_t _manual_fini_array_start[];
    extern destructor_t _manual_fini_array_end[];

    destructor_t *fn = _manual_fini_array_start;
    while(fn < _manual_fini_array_end)
    {
        (*fn++)();
    }
}

// entry point for libtest.so when it is executed
void lib_entry(void)
{
    manual_init();  // call ctors
    printf("Entry point of the library!\n");
    manual_fini();  // call dtors

    _exit(0);
}
 g++ test.cpp -fPIC -Wl,-T,test.ld -Wl,-e,lib_entry -shared -o libtest.so
libtest时,我们得到以下输出。因此在命令行执行

$ ./libtest.so 
Hello world!
Entry point of the library!
Goodbye world!

我还尝试在.cpp文件中定义全局变量,而不是
test.cpp
,这些文件也编译到共享库中。这些全局变量的ctor调用包含在
init\u数组中,因此它们由
manual\u init()
调用。当库作为常规共享库加载时,它的工作方式“正常”。

您是否控制入口点函数代码?到底是什么阻止您调用其中全局对象的构造函数?我可以控制入口点代码,但我相信我需要一种机制来检索要调用的任务列表。这种机制必须存在,因为正常的程序总是这样做,但我自己不知道怎么做。也许我需要深入了解ELF文档。一个更简单的解决方案是将DSO与
-pie
标志链接。哇!gcc手册页中没有任何内容表明-pie会给我这种行为。