我需要一个共享对象上无节头链接C程序
-
22-09-2019 - |
题
我已经写入到代码发生器,让我产生共享对象的接口。虽然我不想Ø实现对节头表支持,因为那是ELF文件格式依然是广大的复杂性。
GNU ld使用节标题针对共享对象链接。这意味着,当我试图把gcc的链接对我的共享对象,没有节标题,它会失败,因为LD没有找到符号,即使在库中存在他们。
确实存在一些技巧,我可以用它来骗过编译器,使连接成功,即使它不会发现某些符号?
下面是一些澄清的麻烦:
cheery@ruttunen:~/Documents/g386$ gcc dev/shared_test.c -L. -lshared -m32
/tmp/cc6qBViY.o: In function `main':
shared_test.c:(.text+0xa): undefined reference to `example_function'
collect2: ld returned 1 exit status
cheery@ruttunen:~/Documents/g386$ cat dev/shared_test.c
// gcc shared_test.c -L. -lshared -m32
// LD_LIBRARY_PATH=. ./a.out
#include <stdio.h>
extern int example_function();
int main(){
printf("hello %d\n", example_function());
}
cheery@ruttunen:~/Documents/g386$ readelf -D -s libshared.so
Symbol table for image:
Num Buc: Value Size Type Bind Vis Ndx Name
2 0: 00800164 0 FUNC GLOBAL DEFAULT ABS example_function
1 0: 008000ac 0 OBJECT GLOBAL DEFAULT ABS _DYNAMIC
解决方案 2
这里最好的方法是增加与gcc所需的部分表。如果你有一个工作的动态链接在你的发电机机制,它要求所有的信息与你插入的部分表的内容。
有关懒惰我写在其上的共享文件装配和使用的条命令获取的参考点。 'readelf --sections'演出的几节,但你并不需要它们。我通过这个实施部分工作,从而直到它开始正常工作。这是我不得不补充:
cheery@ruttunen:~/Documents/g386$ readelf --sections dynamic_hello.app
There are 5 section headers, starting at offset 0x1b9:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .shstrndx STRTAB 00000000 000281 000024 00 0 0 1
[ 2] .dynamic DYNAMIC 00000000 0000b0 000050 08 WA 3 0 4
[ 3] .dynstr STRTAB 00000000 000158 000020 00 A 0 0 1
[ 4] .dynsym DYNSYM 00000000 000100 000040 10 A 3 0 4
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
这不,如果你把在不止这些部分伤害,但这足以让动态链接工作。
其他提示
GCC(即,后面的gcc LD)具有命令行选项来忽略解析的外部。这会压制你从GCC收到错误消息。我不知道,那将会使你快乐。
不隶属于 StackOverflow