Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Under the linker-the actual combat of the linker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report