executing init and fini

后端 未结 2 572
星月不相逢
星月不相逢 2021-02-01 04:45

I just read about init and fini sections in ELF files and gave it a try:

#include 
int main(){
  puts(\"main\");
  return 0;
}

void init(){
  put         


        
2条回答
  •  你的背包
    2021-02-01 05:37

    Don't do that; let your compiler and linker fill in the sections as they see fit.

    Instead, mark your functions with the appropriate function attributes, so that the compiler and linker will put them in the correct sections.

    For example,

    static void before_main(void) __attribute__((constructor));
    static void after_main(void) __attribute__((destructor));
    
    static void before_main(void)
    {
        /* This is run before main() */
    }
    
    static void after_main(void)
    {
        /* This is run after main() returns (or exit() is called) */
    }
    

    You can also assign a priority (say, __attribute__((constructor (300)))), an integer between 101 and 65535, inclusive, with functions having a smaller priority number run first.

    Note that for illustration, I marked the functions static. That is, the functions won't be visible outside the file scope. The functions do not need to be exported symbols to be automatically called.


    For testing, I suggest saving the following in a separate file, say tructor.c:

    #include 
    #include 
    #include 
    
    static int outfd = -1;
    
    static void wrout(const char *const string)
    {
        if (string && *string && outfd != -1) {
            const char       *p = string;
            const char *const q = string + strlen(string);
    
            while (p < q) {
                ssize_t n = write(outfd, p, (size_t)(q - p));
                if (n > (ssize_t)0)
                    p += n;
                else
                if (n != (ssize_t)-1 || errno != EINTR)
                    break;
            }
        }
    }
    
    void before_main(void) __attribute__((constructor (101)));
    void before_main(void)
    {
        int saved_errno = errno;
    
        /* This is run before main() */
        outfd = dup(STDERR_FILENO);
        wrout("Before main()\n");
    
        errno = saved_errno;
    }
    
    static void after_main(void) __attribute__((destructor (65535)));
    static void after_main(void)
    {
        int saved_errno = errno;
    
        /* This is run after main() returns (or exit() is called) */
        wrout("After main()\n");
    
        errno = saved_errno;
    }
    

    so you can compile and link it as part of any program or library. To compile it as a shared library, use e.g.

    gcc -Wall -Wextra -fPIC -shared tructor.c -Wl,-soname,libtructor.so -o libtructor.so
    

    and you can interpose it into any dynamically linked command or binary using

    LD_PRELOAD=./libtructor.so some-command-or-binary
    

    The functions keep errno unchanged, although it should not matter in practice, and use the low-level write() syscall to output the messages to standard error. The initial standard error is duplicated to a new descriptor, because in many instances, the standard error itself gets closed before the last global destructor -- our destructor here -- gets run.

    (Some paranoid binaries, typically security sensitive ones, close all descriptors they don't know about, so you might not see the After main() message in all cases.)

提交回复
热议问题