是否可以链接可执行文件中的.symtab表符号? [英] Is it possible to link against .symtab table symbols in an executable file?

查看:94
本文介绍了是否可以链接可执行文件中的.symtab表符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在main.c下面给出:

Given below main.c:

#include <stdio.h>

void test()
{
  printf("test()\n");
}

int main() {
  test();
  return 0;
}

执行以下命令:

clang-10 main.c -o main
readelf -s main

插入输出复制到此处:

Symbol table '.dynsym' contains 4 entries:
... ignore ...

Symbol table '.symtab' contains 62 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
   61: 00000000004004f0    23 FUNC    GLOBAL DEFAULT   13 test

问题:

  1. 其他可重定位文件(目标文件或静态库)或共享库或可执行文件是否可以使用/链接/访问.symtab表中的 test 符号?

注意:感谢您对问题的复查,这个问题仅用于教育目的,我并没有面对这个真正的问题.

Note : Thanks for reviewing the question, this question is only for education purpose, I am not facing a real problem of this.

要在可执行动态表中导出 test 符号,请执行以下操作:

Edit : To export test symbol in executable dynamic table:

clang-10 main.c -Wl,--dynamic-list=symbols.txt -fPIC -o main

symbols.txt:
{
    test;
};

test 符号出现在.dymsym表中.

The test symbol shows up in the .dymsym table.

根据上面的可执行文件 main :从下面的源文件(shared.c)构建另一个共享库:

Build another shared lib from below source file (shared.c) dependent on above executable main:

extern void test();

void share() {
  test();
}

构建命令:

clang-10 shared.c main -fPIC -shared -o libShared.so

但是,构建失败,并显示以下错误消息:

However, the build failed and give below error message:

main: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
main: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtbeginS.o:(.data.rel.local+0x0): first defined here
main: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtendS.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
main:(.data+0x10): first defined here
/usr/bin/ld: error in main(.eh_frame); no .eh_frame_hdr table will be created.

推荐答案

有可能

否.

用于其他可重定位(目标文件或静态库)

for other relocatable (object file or static library)

这部分问题毫无意义:将 .o 文件与 .symtab 链接以产生什么?

This part of the question is meaningless: link .o file against .symtab to produce what?

如果您的意思是,将一个额外的 foo.o 链接到现有的可执行文件中,答案是否定的: ELF 链接程序考虑该可执行文件 final .链接后,重建可执行文件所需的大多数信息都将被丢弃,而没有该信息,将可执行文件分解并添加新代码再次进行重建几乎是不可能的.

If you mean, link an extra foo.o into existing executable, the answer is no: ELF linkers consider the executable final. Most of the information needed to rebuild the executable is discarded after the link, and without that info, breaking the executable apart and rebuilding it again with new code added is near impossible.

(在AIX上)有一个链接器允许这种分离和重建,但它不使用 ELF 格式.

There is one linker (on AIX) which allows such break apart and rebuild, but it doesn't use ELF format.

或共享库

否:动态链接器不能使用 .symtab .它是要在动态符号表中导出的符号.

No: dynamic linker can not use .symtab. It the symbol to be exported in the dynamic symbol table.

可以使用 -Wl,-export-dynamic 标志构建可执行文件,然后该函数将出现在 .dynsym 中并可供其他共享库使用.

You could build your executable with -Wl,--export-dynamic flag, and then the function will be present in .dynsym and usable by other shared libraries.

或可执行文件以使用/链接/访问.symtab表中的测试符号?

or executable to use/link against/access to the test symbol in .symtab table?

将一个可执行文件链接到另一个可执行文件意味着什么?它们不能同时在同一进程中运行(除非其中一个是位置无关的可执行文件,这实际上是共享库的一种特殊形式).

What would it mean to have one executable link against another executable? They can't be both running in the same process (unless one of them is a position-independent executable, which is really a special form of shared library).

更新:

clang-10 shared.c main -fPIC -shared -o libShared.so

就像我之前说过的那样,您不要链接到 main 可执行文件.

Like I said before, you don't link against the main executable.

您要查找的命令是:

clang-10 shared.c -fPIC -shared -o libShared.so

之所以可行,是因为(默认情况下)允许共享库包含未解析的符号.

This works because shared libraries are (by default) allowed to have unresolved symbols.

这篇关于是否可以链接可执行文件中的.symtab表符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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