[riscv][linker] general cleanup of the linker scripts

Start using PHDRS and MAXPAGESIZE, remove some extraneous stuff.

Possible we can combine a lot of these linker scripts for various arches
if we're careful.
This commit is contained in:
Travis Geiselbrecht
2020-05-15 01:32:18 -07:00
parent 7544ff39b5
commit 225bef5a4b
3 changed files with 41 additions and 73 deletions

View File

@@ -1,5 +1,16 @@
OUTPUT_FORMAT("elf%BITS%-littleriscv", "elf%BITS%-bigriscv", "elf%BITS%-littleriscv")
OUTPUT_ARCH(riscv)
/*
* LK linker script for multi segment binaries.
* In this case the read only portion of the binary lives in read only
* memory, usually in ROM at a lower address.
* Data and BSS live in a higher memory address and initial data is
* copied from rom during initialization.
*/
PHDRS
{
code PT_LOAD FLAGS(5); /* PF_R|PF_X */
rodata PT_LOAD FLAGS(4); /* PF_R */
data PT_LOAD FLAGS(6); /* PF_R|PF_W */
}
ENTRY(_start)
SECTIONS
@@ -14,40 +25,13 @@ SECTIONS
KEEP(*(.text.boot))
*(.text .text*)
*(.gnu.linkonce.t.*)
}
} :code
.interp : { *(.interp) }
.hash : { *(.hash) }
.dynsym : { *(.dynsym) }
.dynstr : { *(.dynstr) }
.rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) }
.rela.text : { *(.rela.text) *(.rela.gnu.linkonce.t*) }
.rel.data : { *(.rel.data) *(.rel.gnu.linkonce.d*) }
.rela.data : { *(.rela.data) *(.rela.gnu.linkonce.d*) }
.rel.rodata : { *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
.rela.rodata : { *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
.rel.got : { *(.rel.got) }
.rela.got : { *(.rela.got) }
.rel.ctors : { *(.rel.ctors) }
.rela.ctors : { *(.rela.ctors) }
.rel.dtors : { *(.rel.dtors) }
.rela.dtors : { *(.rela.dtors) }
.rel.init : { *(.rel.init) }
.rela.init : { *(.rela.init) }
.rel.fini : { *(.rel.fini) }
.rela.fini : { *(.rela.fini) }
.rel.bss : { *(.rel.bss) }
.rela.bss : { *(.rela.bss) }
.rel.plt : { *(.rel.plt) }
.rela.plt : { *(.rela.plt) }
.init : { *(.init) }
.plt : { *(.plt) }
.rodata : ALIGN(4) {
.rodata : ALIGN(CONSTANT(MAXPAGESIZE)) {
__rodata_start = .;
*(.rodata .rodata.* .gnu.linkonce.r.*)
*(.srodata*)
}
} :rodata
/*
* extra linker scripts tend to insert sections just after .rodata,
@@ -58,7 +42,7 @@ SECTIONS
__rodata_end = .;
}
.data : ALIGN(4) {
.data : ALIGN(CONSTANT(MAXPAGESIZE)) {
__data_start_rom = .;
/* in one segment binaries, the rom data address is on top of the ram data address */
__data_start = .;
@@ -73,7 +57,7 @@ SECTIONS
__dtor_end = .;
*(.got*)
*(.dynamic)
}
} :data
/*
* extra linker scripts tend to insert sections just after .data,
@@ -81,23 +65,24 @@ SECTIONS
* but not aligned to the next section necessarily.
*/
.dummy_post_data : {
. = ALIGN(%BITS%/8);
__data_end = .;
}
/* uninitialized data (in same segment as writable data) */
.bss : ALIGN(4) {
.bss : ALIGN(%BITS%/8) {
__bss_start = .;
/* regular bss */
*(.bss .bss.*)
*(.sbss .sbss.*)
*(.gnu.linkonce.b.*)
. = ALIGN(4);
. = ALIGN(%BITS%/8);
__bss_end = .;
}
/* Align the end to ensure anything after the kernel ends up on its own pages */
. = ALIGN(4096);
. = ALIGN(CONSTANT(MAXPAGESIZE));
_end = .;
. = %KERNEL_BASE% + %MEMSIZE%;

View File

@@ -1,5 +1,12 @@
OUTPUT_FORMAT("elf%BITS%-littleriscv", "elf%BITS%-bigriscv", "elf%BITS%-littleriscv")
OUTPUT_ARCH(riscv)
/*
* LK linker script for single segment binaries.
*/
PHDRS
{
code PT_LOAD FLAGS(5); /* PF_R|PF_X */
rodata PT_LOAD FLAGS(4); /* PF_R */
data PT_LOAD FLAGS(6); /* PF_R|PF_W */
}
ENTRY(_start)
SECTIONS
@@ -13,39 +20,13 @@ SECTIONS
KEEP(*(.text.boot))
*(.text .text*)
*(.gnu.linkonce.t.*)
}
} :code
.interp : { *(.interp) }
.hash : { *(.hash) }
.dynsym : { *(.dynsym) }
.dynstr : { *(.dynstr) }
.rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) }
.rela.text : { *(.rela.text) *(.rela.gnu.linkonce.t*) }
.rel.data : { *(.rel.data) *(.rel.gnu.linkonce.d*) }
.rela.data : { *(.rela.data) *(.rela.gnu.linkonce.d*) }
.rel.rodata : { *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
.rela.rodata : { *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
.rel.got : { *(.rel.got) }
.rela.got : { *(.rela.got) }
.rel.ctors : { *(.rel.ctors) }
.rela.ctors : { *(.rela.ctors) }
.rel.dtors : { *(.rel.dtors) }
.rela.dtors : { *(.rela.dtors) }
.rel.init : { *(.rel.init) }
.rela.init : { *(.rela.init) }
.rel.fini : { *(.rel.fini) }
.rela.fini : { *(.rela.fini) }
.rel.bss : { *(.rel.bss) }
.rela.bss : { *(.rela.bss) }
.rel.plt : { *(.rel.plt) }
.rela.plt : { *(.rela.plt) }
.init : { *(.init) }
.plt : { *(.plt) }
.rodata : ALIGN(4) {
.rodata : ALIGN(CONSTANT(MAXPAGESIZE)) {
__rodata_start = .;
*(.rodata .rodata.* .gnu.linkonce.r.*)
*(.srodata*)
}
} :rodata
/*
* extra linker scripts tend to insert sections just after .rodata,
@@ -63,7 +44,7 @@ SECTIONS
*/
. = %MEMBASE%;
.data : AT ( ADDR (.dummy_post_rodata) + SIZEOF (.dummy_post_rodata) ) ALIGN(4) {
.data : AT ( ADDR (.dummy_post_rodata) + SIZEOF (.dummy_post_rodata) ) ALIGN(CONSTANT(MAXPAGESIZE)) {
/* in one segment binaries, the rom data address is on top of the ram data address */
__data_start = .;
*(.data .data.* .gnu.linkonce.d.*)
@@ -77,7 +58,7 @@ SECTIONS
__dtor_end = .;
*(.got*)
*(.dynamic)
}
} :data
/*
* extra linker scripts tend to insert sections just after .data,
@@ -85,18 +66,19 @@ SECTIONS
* but not aligned to the next section necessarily.
*/
.dummy_post_data : {
. = ALIGN(%BITS%/8);
__data_end = .;
}
/* uninitialized data (in same segment as writable data) */
.bss : ALIGN(4) {
.bss : ALIGN(%BITS%/8) {
__bss_start = .;
/* regular bss */
*(.bss .bss.*)
*(.sbss .sbss.*)
*(.gnu.linkonce.b.*)
. = ALIGN(4);
. = ALIGN(%BITS%/8);
__bss_end = .;
}

View File

@@ -170,6 +170,7 @@ linkerscript.phony:
ifeq (true,$(call TOBOOL,$(ARCH_RISCV_TWOSEGMENT)))
GLOBAL_DEFINES += ARCH_RISCV_TWOSEGMENT=1
LINKER_SCRIPT += $(BUILDDIR)/linker-twosegment.ld
ARCH_LDFLAGS += -z max-page-size=4
else
GLOBAL_DEFINES += ARCH_RISCV_TWOSEGMENT=0
LINKER_SCRIPT += $(BUILDDIR)/linker-onesegment.ld