如何链接使用使用ld C标准库不使用gcc气体汇编程序? [英] How to link a gas assembly program that uses the C standard library with ld without using gcc?

查看:784
本文介绍了如何链接使用使用ld C标准库不使用gcc气体汇编程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为练习来pcisely了解更多$ P $ C程序的工作原理和内容是什么最低水平必须存在一个程序能够使用的libc的,我已经采取它在我试图在x86汇编程序为主使用天然气和ld。

As an exercise to learn more precisely how c programs work and what minimum level of content must exist for a program to be able to use libc, I've taken it upon myself to attempt to program primarily in x86 assembly using gas and ld.

作为一个有趣的小挑战,我已经成功地组装和链接链接到不同的自制动态库几个方案,但我没能能够codeA认证从头使用而不直接使用libc的函数调用海湾合作委员会。

As a fun little challenge, I've successfully assembled and linked several programs linked to different self-made dynamic libraries, but I have failed to be able to code a program from scratch to use libc function calls without directly using gcc.

我理解的个别C库函数的调用约定,并彻底检查,通过objdump的使用和readelf的编出来的gcc的方案,但至今都没有得到任何地方以什么样的信息在燃气汇编文件,并包括的内容参数在LD援引成功链接到libc。任何人有任何见解呢?

I understand the calling conventions of individual c library functions, and have thoroughly inspected programs compiled out of gcc through use of objdump and readelf, but haven't gotten anywhere as far as what information to include in a gas assembly file and what parameters to invoke in ld to successfully link to libc. Anyone have any insight to this?

我在运行Linux的x86机器上。

I'm running Linux, on an x86 machine.

推荐答案

有,你需要做的成功使用的libc动态链接至少有三件事情:

There are at least three things that you need to do to successfully use libc with dynamic linking:


  1. 包含链接 /usr/lib/crt1.o _start ,这将是为切入点在ELF二进制;

  2. 链接 /usr/lib/crti.o (之前的libc)和 /usr/lib/crtn.o (之后),它提供了一些初始化和定稿code;

  3. 推荐的二进制将使用动态连接器的连接器, /lib/ld-linux.so

  1. Link /usr/lib/crt1.o, which contains _start, which will be the entry point for the ELF binary;
  2. Link /usr/lib/crti.o (before libc) and /usr/lib/crtn.o (after), which provide some initialisation and finalisation code;
  3. Tell the linker that the binary will use the dynamic linker, /lib/ld-linux.so.

例如:

$ cat hello.s
 .text
 .globl main
main:
 push %ebp
 mov %esp, %ebp
 pushl $hw_str
 call puts
 add $4, %esp
 xor %eax, %eax
 leave
 ret

 .data
hw_str:
 .asciz "Hello world!"

$ as -o hello.o hello.s
$ ld -o hello -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o -lc hello.o /usr/lib/crtn.o
$ ./hello
Hello world!
$

这篇关于如何链接使用使用ld C标准库不使用gcc气体汇编程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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