简体   繁体   English

为什么这个简单的 Linux C 程序在运行时加载 .so 会崩溃?

[英]Why this simple Linux C program loading .so at runtime crashes?

I am trying to write the tiniest program loading a shared object (.so) of mine too at runtime.我正在尝试编写在运行时加载我的共享对象 (.so) 的最小程序。

Unfortunately, it hangs at runtime despite doing error checking :-(不幸的是,尽管进行了错误检查,它还是在运行时挂起 :-(

I am very interested in what I overlooked on source code level.我对我在源代码级别忽略的内容非常感兴趣。

The source code and my shell session running my program follows.源代码和运行我的程序的 shell 会话如下。

File "libsample.c" :文件“libsample.c”:

#include <stdio.h>

void sample_check(void)
{
    printf("INFO: Function sample_check() called.\n");
}

File "test.c" :文件“test.c”:

#include <stdio.h>
#include <dlfcn.h>

typedef void (*sample_func_t) (void);

int main(void)
{
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);
    void* h_lib = dlopen("./libsample.so.1", RTLD_LAZY); // RTLD_LAZY || RTLD_NOW
    if (! h_lib)
    {
        fprintf(stderr, "ERROR(%d): %s\n", __LINE__, dlerror());
        return 1;
    }

    sample_func_t* symver = NULL;
    dlerror();
    symver = dlsym(h_lib, "sample_check");
    char* reter = dlerror();
    if (reter)
    {
        fprintf(stderr, "ERROR(%d): %s\n", __LINE__, reter);
        return 1;
    }

    printf("INFO(%d): Resolved library sample_check() symbol at %p\n", __LINE__, symver);
    printf("INFO(%d): About to call library sample_check() ...\n", __LINE__);
    (*symver)();
    printf("INFO(%d): sample_check() called !\n", __LINE__);

    int retcl = dlclose(h_lib);
    if (retcl)
    {
        fprintf(stderr, "ERROR(%d): %s\n", __LINE__, dlerror());
        return 1;
    }

    return 0;
}

File "build" :文件“构建”:

#! /bin/bash

echo "Begin of compilation ..."

rm test test.o libsample.so.1 libsample.so.1.0.1 libsample.o 2>/dev/null

gcc -fpic -c -o libsample.o libsample.c || exit 1

gcc -shared -Wl,-soname,libsample.so.1 -o libsample.so.1.0.1 libsample.o || exit 1

ln -s libsample.so.1.0.1 libsample.so.1 || exit 1

gcc -c -o test.o test.c || exit 1

gcc -o test test.o -ldl || exit 1

echo "Compilation successful !"

My shell session log :我的 shell 会话日志:

 valentin@valentin-SATELLITE-L875-10G:~/PROGRAMMING/C/Libraries/libsample$ valentin@valentin-SATELLITE-L875-10G:~/PROGRAMMING/C/Libraries/libsample$ ./build Begin of compilation ... Compilation successful ! valentin@valentin-SATELLITE-L875-10G:~/PROGRAMMING/C/Libraries/libsample$ ./test INFO(27): Resolved library sample_check() symbol at 0x7f5e96df86f0 INFO(28): About to call library sample_check() ... Erreur de segmentation valentin@valentin-SATELLITE-L875-10G:~/PROGRAMMING/C/Libraries/libsample$

Any idea ?任何的想法 ?

Here这里

 (*symver)();

the code dereferences what had been received as entry point for library function to be run.代码取消引用作为要运行的库函数的入口点接收到的内容。 This resolves to a random address, which, when being called, typically crashes the program.这会解析为一个随机地址,当被调用时,通常会导致程序崩溃。

To fix this define为了解决这个定义

sample_func_t symver = NULL;

where samle_func_t already is a pointer type because of其中samle_func_t已经是一个指针类型,因为

typedef void (*sample_func_t) (void);

(Mind the * .) (注意* 。)

Then there are two possibilities to assign symver :然后有两种分配symver可能性:

  1. The "dirty" one “脏”的

     symver = dlsym(h_lib, "sample_check");

    "dirty" as the compiler might issue a warning like this: “脏”,因为编译器可能会发出这样的警告:

     ISO C forbids assignment between function pointer and 'void *'
  2. The "cleaner" one “清洁工”之一

     *(void**)(&symver) = dlsym(h_lib, "sample_check");

And finally call the function like this:最后像这样调用函数:

symver(); /* No need to dereference here. */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM