链接器实战
1目标
模拟嵌入式开发,编写一个“体积受限”的可执行程序,通过makefile 完成编译, 运行后打印“D.T.Software”
传统的写法:test.c
#include <stdio.h>
void main()
{
print("D.T.Software\n");
return 0;
}
今天我们采用全新的方法,达到体积最小的 目标。
2分析过程

3解决方案
1.通过内嵌汇编自定义打印函数和退出函数(INT 80H)
2.通过链接脚本自定入口函数(不依赖任何库和gcc内置功能)
3.删除可执行程序中的无用信息(无用段信息、调试信息、等)
4打印函数设计
void print(const char* s, int l)
{
asm volatile (
"movl $4, %%eax\n"
"movl $1, %%ebx\n"
"movl %0, %%ecx\n"
"movl %1, %%edx\n"
"int $0x80 \n"
:
: "r"(s), "r"(l)
: "eax", "ebx", "ecx", "edx"
);
}
5退出函数设计
void exit(int code)
{
asm volatile (
"movl $1, %%eax\n"
"movl %0, %%ebx\n"
"int $0x80 \n"
:
: "r"(code)
: "eax", "ebx"
);
}
6链接脚本设计
ENTRY(program)
SECTIONS
{
.text 0x08048000 + SIZEOF_HEADERS :
{
*(.text)
*(.rodata)
}
/DISCARD/ :
{
*(*)
}
}
7 makefie
CC := gcc
LD := ld
RM := rm -fr
TARGET := program.out
SRC := $(TARGET:.out=.c)
OBJ := $(TARGET:.out=.o)
LDS := $(TARGET:.out=.lds)
.PHONY : rebuild clean all
$(TARGET) : $(OBJ) $(LDS)
$(LD) -static -T $(LDS) -o $@ $<
@echo "Target File ==> $@"
$(OBJ) : $(SRC)
$(CC) -fno-builtin -o $@ -c $^
rebuild : clean all
all : $(TARGET)
clean :
$(RM) $(TARGET) $(OBJ)
最终的设计:
void print(const char* s, int l);
void exit(int code);
void program()
{
print("D.T.Software\n", 13);
exit(0);
}
void print(const char* s, int l)
{
asm volatile (
"movl $4, %%eax\n"
"movl $1, %%ebx\n"
"movl %0, %%ecx\n"
"movl %1, %%edx\n"
"int $0x80 \n"
:
: "r"(s), "r"(l)
: "eax", "ebx", "ecx", "edx"
);
}
void exit(int code)
{
asm volatile (
"movl $1, %%eax\n"
"movl %0, %%ebx\n"
"int $0x80 \n"
:
: "r"(code)
: "eax", "ebx"
);
}
编译后结果对比:(没有对比就没有伤害)

还可以使用strip命令进一步去除无用调试信息:

8指定链接选项
文章整理参考自狄泰课程。