不依赖glibc库的hello world的学习

本文探讨了如何创建一个不依赖c语言运行库的hello world程序,重点介绍了如何使用gcc编译并链接,使得程序直接以nomain为入口,避免了传统c代码的多段内存布局。通过使用静态链接、指定程序入口等技巧,实现了精简的elf文件。同时,文章也提及了elf文件入口地址的研究,展示了如何通过ld命令和链接脚本来调整程序入口。
摘要由CSDN通过智能技术生成

在研究gcc的编译 链接时,遇到的比较有意思的东西。号称最小的程序。

这个东西来源于书  程序员的自我修养(链接 装载与库) P124.

传统的helloworld

#include<stdio.h>

int main()
{
   printf("hello world\n");
   return 0;
}

对于这个代码的运行需要glibc库等很多库,可以利用gcc -static --verbose  -fno-builtin hello.c将编译链接的中间过程打印出来

所以下面的helloworld几个不同点

1、脱离c语言运行库

2、一般c代码的函数入口在库里面的_start,然后调用main函数,此次直接使用c中nomain作为整个程序的入口

3、一般c代码的函数会生成好多段text 段 data段,此次将所有的段合并到一个我们自己命名的tinytext段

tinyhelloworld.c代码如下

char * str = "hello world from tinyos\n";

void printf()
{
	asm("movl $24 ,%%edx\n\t"
	    "movl %0  ,%%ecx\n\t"
	    "movl $0  ,%%ebx\n\t"
	    "movl $4  ,%%eax\n\t"
	    "int $0x80\n\t"
	    ::"r"(str):"edx","ecx","ebx");
}

void exit()
{
	asm("movl $42,%ebx\n\t"
     	    "movl $1,%eax\n\t"
	    "int $0x80\n\t");
}

int nomain()
{
	printf();
	exit();
}

这段源代码为书中,分析一下

这里的printf 是直接使用linux的write系统调用exit也是如此。

gcc -c -fno-builtin tinyhelloworld.c只编译不链接

-fno-builtin参数为不使用gcc内部函数优化

ld -static -e nomain -o tinyhelloworld tinyhelloworld.o

使用ld链接,

-static是静态链接

-e nomain 是修改程序入口为 nomain

这里说明一下

在链接脚本中也可以设置程序入口,ENTRY(name),会有一个优先级。从高到底为:

1、ld -e参数命令

2、链接脚本ENTRY(name)

3、如果定义了_start则以此为入口

4、如果有text段,则text的第一个字节地址

<span style="color:#FF0000;">readelf -h tinyhelloworld</span>
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  <span style="color:#FF0000;">Entry point address:               0x80480c4</span>
  Start of program headers:          52 (bytes into file)
  Start of section headers:          472 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         3
  Size of section headers:           40 (bytes)
  Number of section headers:         9
  Section header string table index: 6

从上面可知程序的入口地址为0x80480c4

<spa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值