From fc3f6268d7d33748e85f41ea89af6b88cbde7ff2 Mon Sep 17 00:00:00 2001 From: Li Jinpei Date: Sun, 25 Jul 2021 01:29:17 +0800 Subject: [PATCH] add linker script to fix binary size --- Makefile | 12 +++++------- fs/Makefile | 1 - kernel/Makefile | 4 ++-- mm/Makefile | 3 +-- x86_32.lds.S | 20 ++++++++++++++++++++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 x86_32.lds.S diff --git a/Makefile b/Makefile index 1b3b33e..7815e99 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,5 @@ AS := as -LD := ld -m elf_x86_64 - -LDFLAG := -Ttext 0x0 -s --oformat binary +LD := ld image : linux.img @@ -15,16 +13,16 @@ kernel/system : kernel/head.S kernel/*.c mm/*.c kernel/blk_drv/*.c fs/*.c cd kernel; make system; cd .. bootsect : bootsect.o - $(LD) $(LDFLAG) -o $@ $< + $(LD) -T x86_32.lds.S -o $@ $< bootsect.o : bootsect.S - $(AS) -o $@ $< + $(AS) --32 -o $@ $< setup : setup.o - $(LD) $(LDFLAG) -e _start_setup -o $@ $< + $(LD) -T x86_32.lds.S -e _start_setup -o $@ $< setup.o : setup.S - $(AS) -o $@ $< + $(AS) --32 -o $@ $< clean: rm *.o rm bootsect diff --git a/fs/Makefile b/fs/Makefile index 193ad9f..149ff4f 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -1,7 +1,6 @@ GCC := gcc LD := ld CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-stack-protector -fno-pic -c -m32 -LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include OBJS := read_write.o buffer.o super.o open.o file_table.o inode.o namei.o fcntl.o char_dev.o bitmap.o truncate.o diff --git a/kernel/Makefile b/kernel/Makefile index 8c976ab..f4ecbca 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,12 +1,12 @@ GCC := gcc CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-pic -fno-stack-protector -c -m32 -LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include OBJS := head.o main.o sys_call.o asm.o sched.o printk.o vsprintf.o mktime.o \ traps.o fork.o panic.o chr_drv/chr_drv.a blk_drv/blk_drv.a ../mm/mm.o ../lib/lib.a ../fs/fs.o + system: $(OBJS) - $(LD) $(LDFLAG) -M -e startup_32 -o $@ $^ > System.map + $(LD) -T ../x86_32.lds.S -M -e startup_32 -o $@ $^ > System.map head.o : head.S $(GCC) -m32 -traditional -c -o $@ $< diff --git a/mm/Makefile b/mm/Makefile index e334d0b..f62260d 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -1,7 +1,6 @@ GCC := gcc -LD := ld +LD := ld CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-pic -fno-stack-protector -c -m32 -LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include OBJS := swap.o memory.o page.o diff --git a/x86_32.lds.S b/x86_32.lds.S new file mode 100644 index 0000000..c19de30 --- /dev/null +++ b/x86_32.lds.S @@ -0,0 +1,20 @@ +OUTPUT_FORMAT("binary") +OUTPUT_ARCH(i386) +SECTIONS +{ + .text 0x00 : + { + *(.text) + /* + *(.text.unlikely .text.*_unlikely .text.unlikely.*) + *(.text.exit .text.exit.*) + *(.text.startup .text.startup.*) + *(.text.hot .text.hot.*) + *(SORT(.text.sorted.*)) + *(.text .stub .text.* .gnu.linkonce.t.*) + */ + } + . = 0x200; + _end = .; PROVIDE (end = .); + /DISCARD/ : {*(*)} +} -- Gitee