将ELF二进制文件与c程序链接

问题描述 投票:0回答:2

仅允许访问独立的ELF程序,我希望能够从自己的程序中调用该程序中的函数。假设下面的代码是main.c

#include <stdio.h>

extern int mystery(int a,int b);

int main() {
        int a = 0;
        int b = 1;
        printf("mystery(a,b) = %d\n",mystery(a,b));
        return 0;
}

功能mystery存在于某些elf文件not_my_program中。我正在尝试做的事情类似于

gcc main.c not_my_program

但是,这给了我mystery的不确定参考错误。我在寻找方法在论坛上发现,无法将此elf文件转换为共享对象文件。我也考虑使用[]将main.c编译成可重定位的目标文件

gcc -c main.c

然后使用ld将小精灵与main.o链接起来,但是我不知道该怎么做。 elf是32位,但我省略了-m32标志。如果ld的标志不同,请告诉我。任何帮助将不胜感激。

编辑:readelf -h not_my_program的输出

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x10e0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          15116 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         11
  Size of section headers:           40 (bytes)
  Number of section headers:         30
  Section header string table index: 29

仅允许访问独立的ELF程序,我希望能够从自己的程序中调用该程序中的函数。假设下面的代码是main.c #include extern int ...

c gcc linker ld elf
2个回答
0
投票

仅允许访问独立的ELF程序,我希望能够从自己的程序中调用该程序中的函数


0
投票

这种骇人听闻的方式适用于非常简单的情况。

© www.soinside.com 2019 - 2024. All rights reserved.