为什么当crt1.o crti.o和crtn.o联汇编程序才有效? [英] Why does an assembly program only work when linked with crt1.o crti.o and crtn.o?

查看:543
本文介绍了为什么当crt1.o crti.o和crtn.o联汇编程序才有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道程序如何工作,以便使其尽可能裸露的骨头尽可能我用汇编鬼混。

I like to know how programs work so to make it as bare bones as possible I fool around with assembly.

我刚刚找到了如何组装code使用wprintf函数x86_64的(发现了宽字符是32位)。我所要做的就是链接到libc(-lc)。

I just found out how to assemble code for x86_64 using wprintf function (found out wide chars are 32 bit). all I had to do was link to libc (-lc).

我想组装code 32位做同样的事情,但我无意中发现了不少。最后,我用gcc来进行链接(并改变了_start:主:)。于是我做了使用LD自己的链接和包含crt1.o crti.o和crtn.o.然后我的工作程序(它不会打印出任何东西前),所以我的问题是,我可以做我的code之内的东西,以消除这些其他3目标文件的需要(当然恢复到_start:代替主营:?)

I'm trying to assemble code for 32-bit doing about the same thing but I stumbled quite a bit. Eventually I used gcc to do the linking ( and changed the _start: to main:). So then I did the linking myself using ld and included crt1.o crti.o and crtn.o. Then my program worked ( it wouldn't print out anything before ) So my question is, can I do something within my code to eliminate the need for these other 3 object files (and of course revert back to _start: instead of main:)?

test_lib.S

.section .data
locale:
  .string ""
  .align 4
printformat:
  .long '%','l','c',0

.section .text
.global main
main:

pushl   $locale
pushl   $6
call    setlocale
pushl   $12414
pushl   $printformat
call    wprintf
pushl   $2
call    exit

和运行以下

as --32 test_lib.S -o test_lib.o
ld -m elf_i386 -L/lib/ -L/usr/lib/ -I/lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o -lc /usr/lib/crtn.o test_lib.o -o test_lib
./test_lib

哦,输出是一个简单的日文平假名(MA)ま(注意没有断行,因此前提示打印)

oh and the output is simply a japanese hiragana (ma)ま (notice there is no line break so it prints before the prompt)

推荐答案

下面是什么文件,为你做。他们是C-运行环境和安装链接到操作系统。

Here are what the files do for you. They are the c-runtime environment and setup that link to the OS.

crt1.o
  初始运行时code的新风格。包含建立与ARGC的包膜的_start符号/的argv / libc的_init / _fini的libc跳转到主要的libc之前。 glibc的调用这个文件'start.S中。

crt1.o Newer style of the initial runtime code. Contains the _start symbol which sets up the env with argc/argv/libc _init/libc _fini before jumping to the libc main. glibc calls this file 'start.S'.

crti.o
  定义函数序言; _init在.init节,并在.fini部分_fini。 glibc的调用这个'initfini.c。

crti.o Defines the function prolog; _init in the .init section and _fini in the .fini section. glibc calls this 'initfini.c'.

crtn.o
  定义功能收尾。 glibc的调用这个'initfini.c。

crtn.o Defines the function epilog. glibc calls this 'initfini.c'.

有是一个很好的写起来,例如code以下网站 HTTP被发现://维基.osdev.org / Creating_a_C_Library 每个以上的库。

There is an excellent write up and example code to be found at the following website http://wiki.osdev.org/Creating_a_C_Library for each of the libraries above.

这篇关于为什么当crt1.o crti.o和crtn.o联汇编程序才有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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