From 31d6eac302412909640f44465f03aab1f5e10afc Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 22 Jan 2023 12:05:14 +0000 Subject: [linux] Centralise the linker script for Linux binaries Reduce duplication between i386 and x86_64 by providing a single shared linker script that both architectures can include. Signed-off-by: Michael Brown --- src/arch/i386/Makefile.linux | 4 ++ src/arch/i386/scripts/linux.lds | 97 +----------------------------------- src/arch/x86_64/Makefile.linux | 4 ++ src/arch/x86_64/scripts/linux.lds | 97 +----------------------------------- src/scripts/linux.lds | 101 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 192 deletions(-) create mode 100644 src/scripts/linux.lds diff --git a/src/arch/i386/Makefile.linux b/src/arch/i386/Makefile.linux index fe4229e..d78ee32 100644 --- a/src/arch/i386/Makefile.linux +++ b/src/arch/i386/Makefile.linux @@ -4,6 +4,10 @@ # LDSCRIPT = arch/i386/scripts/linux.lds +# Starting virtual address +# +LDFLAGS += -Ttext=0x08048000 + # Compiler flags for building host API wrapper # LINUX_CFLAGS += -m32 diff --git a/src/arch/i386/scripts/linux.lds b/src/arch/i386/scripts/linux.lds index 8c3a7b0..ba0b437 100644 --- a/src/arch/i386/scripts/linux.lds +++ b/src/arch/i386/scripts/linux.lds @@ -8,99 +8,4 @@ OUTPUT_FORMAT ( "elf32-i386", "elf32-i386", "elf32-i386" ) OUTPUT_ARCH ( i386 ) -SECTIONS { - _max_align = 32; - - . = 0x08048000; - - /* - * The text section - * - */ - - . = ALIGN ( _max_align ); - .text : { - _text = .; - *(.text) - *(.text.*) - _etext = .; - } - - /* - * The rodata section - * - */ - - . = ALIGN ( _max_align ); - .rodata : { - _rodata = .; - *(.rodata) - *(.rodata.*) - _erodata = .; - } - - /* - * The data section - * - * Adjust the address for the data segment. We want to adjust up to - * the same address within the page on the next page up. - */ - - . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - .data : { - _data = .; - *(.data) - *(.data.*) - KEEP(*(SORT(.tbl.*))) - KEEP(*(.provided)) - KEEP(*(.provided.*)) - _edata = .; - } - - /* - * The bss section - * - */ - - . = ALIGN ( _max_align ); - .bss : { - _bss = .; - *(.bss) - *(.bss.*) - *(COMMON) - _ebss = .; - } - - /* - * Weak symbols that need zero values if not otherwise defined - * - */ - - .weak 0x0 : { - _weak = .; - *(.weak) - *(.weak.*) - _eweak = .; - } - _assert = ASSERT ( ( _weak == _eweak ), ".weak is non-zero length" ); - - /* - * Dispose of the comment and note sections to make the link map - * easier to read - * - */ - - /DISCARD/ : { - *(.comment) - *(.comment.*) - *(.note) - *(.note.*) - *(.rel) - *(.rel.*) - *(.discard) - *(.discard.*) - *(.sbat) - *(.sbat.*) - } -} +INCLUDE scripts/linux.lds diff --git a/src/arch/x86_64/Makefile.linux b/src/arch/x86_64/Makefile.linux index c41ee49..9b57254 100644 --- a/src/arch/x86_64/Makefile.linux +++ b/src/arch/x86_64/Makefile.linux @@ -4,6 +4,10 @@ # LDSCRIPT = arch/x86_64/scripts/linux.lds +# Starting virtual address +# +LDFLAGS += -Ttext=0x400000 + # Include generic Linux Makefile # MAKEDEPS += arch/x86/Makefile.linux diff --git a/src/arch/x86_64/scripts/linux.lds b/src/arch/x86_64/scripts/linux.lds index a093787..a37b067 100644 --- a/src/arch/x86_64/scripts/linux.lds +++ b/src/arch/x86_64/scripts/linux.lds @@ -8,99 +8,4 @@ OUTPUT_FORMAT ( "elf64-x86-64", "elf64-x86-64", "elf64-x86-64" ) OUTPUT_ARCH ( i386:x86-64 ) -SECTIONS { - _max_align = 32; - - . = 0x400000; - - /* - * The text section - * - */ - - . = ALIGN ( _max_align ); - .text : { - _text = .; - *(.text) - *(.text.*) - _etext = .; - } - - /* - * The rodata section - * - */ - - . = ALIGN ( _max_align ); - .rodata : { - _rodata = .; - *(.rodata) - *(.rodata.*) - _erodata = .; - } - - /* - * The data section - * - * Adjust the address for the data segment. We want to adjust up to - * the same address within the page on the next page up. - */ - - . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - .data : { - _data = .; - *(.data) - *(.data.*) - KEEP(*(SORT(.tbl.*))) - KEEP(*(.provided)) - KEEP(*(.provided.*)) - _edata = .; - } - - /* - * The bss section - * - */ - - . = ALIGN ( _max_align ); - .bss : { - _bss = .; - *(.bss) - *(.bss.*) - *(COMMON) - _ebss = .; - } - - /* - * Weak symbols that need zero values if not otherwise defined - * - */ - - .weak 0x0 : { - _weak = .; - *(.weak) - *(.weak.*) - _eweak = .; - } - _assert = ASSERT ( ( _weak == _eweak ), ".weak is non-zero length" ); - - /* - * Dispose of the comment and note sections to make the link map - * easier to read - * - */ - - /DISCARD/ : { - *(.comment) - *(.comment.*) - *(.note) - *(.note.*) - *(.rel) - *(.rel.*) - *(.discard) - *(.discard.*) - *(.sbat) - *(.sbat.*) - } -} +INCLUDE scripts/linux.lds diff --git a/src/scripts/linux.lds b/src/scripts/linux.lds new file mode 100644 index 0000000..afc0128 --- /dev/null +++ b/src/scripts/linux.lds @@ -0,0 +1,101 @@ +/* -*- sh -*- */ + +/* + * Linker script for Linux images + * + */ + +SECTIONS { + _max_align = 32; + + /* + * The text section + * + */ + + . = ALIGN ( _max_align ); + .text : { + _text = .; + *(.text) + *(.text.*) + _etext = .; + } + + /* + * The rodata section + * + */ + + . = ALIGN ( _max_align ); + .rodata : { + _rodata = .; + *(.rodata) + *(.rodata.*) + _erodata = .; + } + + /* + * The data section + * + * Adjust the address for the data segment. We want to adjust up to + * the same address within the page on the next page up. + */ + + . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); + . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); + .data : { + _data = .; + *(.data) + *(.data.*) + KEEP(*(SORT(.tbl.*))) + KEEP(*(.provided)) + KEEP(*(.provided.*)) + _edata = .; + } + + /* + * The bss section + * + */ + + . = ALIGN ( _max_align ); + .bss : { + _bss = .; + *(.bss) + *(.bss.*) + *(COMMON) + _ebss = .; + } + + /* + * Weak symbols that need zero values if not otherwise defined + * + */ + + .weak 0x0 : { + _weak = .; + *(.weak) + *(.weak.*) + _eweak = .; + } + _assert = ASSERT ( ( _weak == _eweak ), ".weak is non-zero length" ); + + /* + * Dispose of the comment and note sections to make the link map + * easier to read + * + */ + + /DISCARD/ : { + *(.comment) + *(.comment.*) + *(.note) + *(.note.*) + *(.rel) + *(.rel.*) + *(.discard) + *(.discard.*) + *(.sbat) + *(.sbat.*) + } +} -- cgit v1.1