Under the linker-the actual combat of the linker
Linker Combat 1 Target
Simulate embedded development, write a "volume limited" executable program, complete compilation through makefile, print "D.T.Software" after running
Traditional writing: test.c
#include void main(){ print("D.T.Software\n"); return 0;}
Today we are taking a completely new approach to achieving the goal of minimal volume.
2 Analytical process
3 Solutions
1. Custom print and exit functions via built-in assembly (INT 80H)
2. Custom entry functions via link scripts (independent of any libraries and gcc built-in features)
3. Delete useless information (useless segment information, debugging information, etc.) from executable programs
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" );}6 Link Script Design ENTRY(program)SECTIONS{ .text 0x08048000 + SIZEOF_HEADERS : { *(.text) *(.rodata) } /DISCARD/ : { *(*) }}7 makefieCC := gccLD := ldRM := rm -frTARGET := program.outSRC := $(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 allall : $(TARGET)clean : $(RM) $(TARGET) $(OBJ)
Final design:
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" );}
Comparison of compiled results: (no comparison, no damage)
You can also use the strip command to further remove unwanted debugging information:
8 Specify linking option-ld command// GNU linker, links object files into executable programs, member of GCC compiler integration, important behind-the-scenes worker-ld -static //Specifies that static linking is used to produce final programs instead of the default dynamic linking method. - gcc -fno-builtin //Used to disable GCC built-in functions (GCC provides many built-in functions that replace some common C library functions with compiler built-in functions for optimization purposes). 9 summarizes
For embedded devices with limited resources, the size of executable programs needs to be considered. The use of related libraries can be split by directly using system services through embedded assembly. The size of executable programs can be controlled by the following methods.
1. Minimize library usage (consider implementing correlation functions yourself if necessary)
2. Custom link script, delete useless segment information
The article is organized and referenced from the Dee Tai course.