From 3a48e2bfd774d88e51f5bb32e5dfc68d748458db Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sat, 23 Dec 2023 22:03:15 +0800 Subject: [PATCH] improve linker scripts Previously, due to the ordering of .text.p2ram and .bss, the linker was forced to allocate .bss in the output file, increasing the size of the final binary unnecessarily. The linker scripts now preserve debugging symbols during linking. --- alt.lds | 30 ++++++++++++------------------ debug.lds | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ main.lds | 36 ++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 debug.lds diff --git a/alt.lds b/alt.lds index bce99eb..86a8dc7 100644 --- a/alt.lds +++ b/alt.lds @@ -33,35 +33,29 @@ SECTIONS KEEP(*(.ctors.*)) } > p1ram - .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) - { - *(.bss) - *(.bss.*) - } > p1ram - .text.p2ram ALIGN(4) : SUBALIGN(4) { *(.p2ram) *(.p2ram.*) } > p1ram - __p1ram_end = .; - - /DISCARD/ : + .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) { - *(.debug*) - *(.comment*) - *(.rela*) - } + *(.bss) + *(.bss.*) + *(COMMON) + } > p1ram - __bss_link_start = ADDR(.bss); - __bss_link_end = ADDR(.bss) + SIZEOF(.bss); - - __ctors_link_start = ADDR(.ctors); - __ctors_link_end = ADDR(.ctors) + SIZEOF(.ctors); + INCLUDE "debug.lds" } __p1ram_start = ORIGIN(p1ram); __p1ram_end = ORIGIN(p1ram) + LENGTH(p1ram); +__bss_link_start = ADDR(.bss); +__bss_link_end = ADDR(.bss) + SIZEOF(.bss); + +__ctors_link_start = ADDR(.ctors); +__ctors_link_end = ADDR(.ctors) + SIZEOF(.ctors); + INCLUDE "addresses.lds" diff --git a/debug.lds b/debug.lds new file mode 100644 index 0000000..71b5eb2 --- /dev/null +++ b/debug.lds @@ -0,0 +1,48 @@ + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1. */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions. */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2. */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2. */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions. */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + /* DWARF 3. */ + .debug_pubtypes 0 : { *(.debug_pubtypes) } + .debug_ranges 0 : { *(.debug_ranges) } + /* DWARF 5. */ + .debug_addr 0 : { *(.debug_addr) } + .debug_line_str 0 : { *(.debug_line_str) } + .debug_loclists 0 : { *(.debug_loclists) } + .debug_macro 0 : { *(.debug_macro) } + .debug_names 0 : { *(.debug_names) } + .debug_rnglists 0 : { *(.debug_rnglists) } + .debug_str_offsets 0 : { *(.debug_str_offsets) } + .debug_sup 0 : { *(.debug_sup) } + .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } + /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.rela.*) } diff --git a/main.lds b/main.lds index c744c77..91159a2 100644 --- a/main.lds +++ b/main.lds @@ -6,7 +6,7 @@ MEMORY } SECTIONS { - . = 0x8c010000; + . = ORIGIN(p1ram); .text ALIGN(4) : SUBALIGN(4) { @@ -34,15 +34,7 @@ SECTIONS KEEP(*(.ctors.*)) } > p1ram - .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) - { - *(.bss) - *(.bss.*) - } > p1ram - - __p1ram_end = .; - - . = 0xac010000 + __p1ram_end - __p1ram_start; + . = ORIGIN(p2ram) + (. - ORIGIN(p1ram)); .text.p2ram ALIGN(4) : SUBALIGN(4) { @@ -50,21 +42,25 @@ SECTIONS *(.p2ram.*) } > p2ram AT>p1ram - /DISCARD/ : + . = ORIGIN(p1ram) + (. - ORIGIN(p2ram)); + + .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) { - *(.debug*) - *(.comment*) - *(.rela*) - } + *(.bss) + *(.bss.*) + *(COMMON) + } > p1ram - __bss_link_start = ADDR(.bss); - __bss_link_end = ADDR(.bss) + SIZEOF(.bss); - - __ctors_link_start = ADDR(.ctors); - __ctors_link_end = ADDR(.ctors) + SIZEOF(.ctors); + INCLUDE "debug.ld" } __p1ram_start = ORIGIN(p1ram); __p1ram_end = ORIGIN(p1ram) + LENGTH(p1ram); +__bss_link_start = ADDR(.bss); +__bss_link_end = ADDR(.bss) + SIZEOF(.bss); + +__ctors_link_start = ADDR(.ctors); +__ctors_link_end = ADDR(.ctors) + SIZEOF(.ctors); + INCLUDE "addresses.lds"