条件1.我有没有myassembly.s
的main
。
条件2.相反,myassembly.s
有全球符号_start
。
条件3.我想将_IO_stdin_used
链接到输出二进制。
......这是有问题的部分。
你可以看到_IO_stdin_used
在crt1.o
上声明:
jiwon@jiwon:/tmp$ readelf -s /usr/lib/i386-linux-gnu/crt1.o
Symbol table '.symtab' contains 17 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
...
8: 00000000 4 OBJECT GLOBAL DEFAULT 4 _fp_hw
9: 00000000 0 NOTYPE GLOBAL DEFAULT UND __libc_csu_fini
10: 00000000 0 FUNC GLOBAL DEFAULT 2 _start
11: 00000000 0 NOTYPE GLOBAL DEFAULT UND __libc_csu_init
12: 00000000 0 NOTYPE GLOBAL DEFAULT UND main
13: 00000000 0 NOTYPE WEAK DEFAULT 6 data_start
14: 00000000 4 OBJECT GLOBAL DEFAULT 5 _IO_stdin_used
15: 00000000 0 NOTYPE GLOBAL DEFAULT UND __libc_start_main
16: 00000000 0 NOTYPE GLOBAL DEFAULT 6 __data_start
题。我可以在没有crt1.o
的情况下链接_start
吗?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
...
我更具体地说明了我的情况,因为它可能是分析所需的信息。
这是myassembly.s
。
如上所述,它使用_start
而没有main
。
.global _start
_start:
push $_STRING1
push $_STRING2
call printf
push $0
call exit
_STRING1:
.string "gogo\n"
_STRING2:
.string "%s"
我汇编并链接它:
jiwon@jiwon:/tmp$ as -o stackoverflow.o stackoverflow.s
jiwon@jiwon:/tmp$ ld -o stackoverflow -dynamic-linker /lib/ld-linux.so.2 /usr/lib/i386-linux-gnu/crti.o -lc stackoverflow.o /usr/lib/i386-linux-gnu/crtn.o
jiwon@jiwon:/tmp$ ./stackoverflow
gogo
如您所见,输出二进制文件运行良好。但是,当我尝试连接crt1.o
时,会发生错误。
jiwon@jiwon:/tmp$ ld -o stackoverflow -dynamic-linker /lib/ld-linux.so.2 /usr/lib/i386-linux-gnu/crti.o -lc stackoverflow.o /usr/lib/i386-linux-gnu/crtn.o /usr/lib/i386-linux-gnu/crt1.o
/usr/lib/i386-linux-gnu/crt1.o: In function `_start':
(.text+0x0): multiple definition of `_start'
stackoverflow.o:(.text+0x0): first defined here
/usr/lib/i386-linux-gnu/crt1.o: In function `_start':
(.text+0xc): undefined reference to `__libc_csu_fini'
/usr/lib/i386-linux-gnu/crt1.o: In function `_start':
(.text+0x11): undefined reference to `__libc_csu_init'
/usr/lib/i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
问题(相同)我可以在没有crt1.o
的情况下链接_start
吗?
而不是拉动整个老鼠的crt1.o
的尾巴,你怎么简单地自己定义_IO_stdin_used
?只需将此代码放在一个程序集文件中:
.section .rodata
.globl _IO_stdin_used
.type _IO_stdin_used, @object
.align 4
_IO_stdin_used:
.int 0x20001
.size _IO_stdin_used, 4
否则,如果您只是想要自己定义_IO_stdin_used
(例如,因为libc中的某个文件定义了它),只需将此行放入一个程序集文件中:
.globl _IO_stdin_used
这会导致汇编程序将_IO_stdin_used
标记为未定义的全局符号,链接器必须从某处将其拉入。