Skip to content

Commit

Permalink
build: x86: add some flexibility in custom linker scripts
Browse files Browse the repository at this point in the history
If a particular project needs to add additional data to the
binary image, in most cases the entire linker script needs to
forked into the project space, causing maintenance issues if
the main linker script is changed.

Now we add some Kconfig options to allow a project to specify
some additional linker scripts which get included by the main
one in a few key areas:

1) In the definition to the 'rodata' section, which can allow
additional data to be included in this ROM section.

2) In the definition to the 'datas' section, which allows
additional data to be included in this RAM section.

3) Arbitrary additional sections to be included at the end of
the binary.

For 1 and 2, this is useful to include data generated outside of
the normal C compilation, such as data structures that are created
by special build tools.

3 is useful for including arbitrary binary blobs inside the final
image, such as for peripheral or co-processor firmware.

Change-Id: I5738d3d6da25f5bc96cda8ae806bf1a3fb34bd5d
Signed-off-by: Andrew Boie <[email protected]>
  • Loading branch information
Andrew Boie authored and Anas Nashif committed Nov 30, 2016
1 parent e868a80 commit 82f11bf
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ $(KERNEL_NAME).lnk: $(zephyr-deps)
linker.cmd: $(zephyr-deps)
$(Q)$(CC) -x assembler-with-cpp -nostdinc -undef -E -P \
$(LDFLAG_LINKERCMD) $(LD_TOOLCHAIN) -I$(srctree)/include \
-I$(SOURCE_DIR) \
-I$(objtree)/include/generated $(EXTRA_LINKER_CMD_OPT) $(KBUILD_LDS) -o $@

PREBUILT_KERNEL = $(KERNEL_NAME)_prebuilt.elf
Expand Down
16 changes: 16 additions & 0 deletions include/arch/arc/v2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ SECTIONS {
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)

#ifdef CONFIG_CUSTOM_RODATA_LD
/* Located in project source directory */
#include <custom-rodata.ld>
#endif

} GROUP_LINK_IN(ROMABLE_REGION)

_image_rom_end = .;
Expand All @@ -135,6 +141,11 @@ SECTIONS {
*(.data)
*(".data.*")

#ifdef CONFIG_CUSTOM_RWDATA_LD
/* Located in project source directory */
#include <custom-rwdata.ld>
#endif

} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

#include <linker/common-ram.ld>
Expand Down Expand Up @@ -184,4 +195,9 @@ SECTIONS {
}
ASSERT(SIZEOF(initlevel_error) == 0, "Undefined initialization levels used.")

#ifdef CONFIG_CUSTOM_SECTIONS_LD
/* Located in project source directory */
#include <custom-sections.ld>
#endif

}
16 changes: 16 additions & 0 deletions include/arch/arm/cortex_m/scripts/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ SECTIONS
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)

#ifdef CONFIG_CUSTOM_RODATA_LD
/* Located in project source directory */
#include <custom-rodata.ld>
#endif
/*
* For XIP images, in order to avoid the situation when __data_rom_start
* is 32-bit aligned, but the actual data is placed right after rodata
Expand Down Expand Up @@ -167,6 +172,12 @@ SECTIONS
*(.top_of_image_ram.*)
*(.data)
*(".data.*")

#ifdef CONFIG_CUSTOM_RWDATA_LD
/* Located in project source directory */
#include <custom-rwdata.ld>
#endif

} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

#include <linker/common-ram.ld>
Expand Down Expand Up @@ -236,4 +247,9 @@ SECTIONS
} GROUP_LINK_IN(SYSTEM_CONTROL_SPACE)
GROUP_END(SYSTEM_CONTROL_SPACE)

#ifdef CONFIG_CUSTOM_SECTIONS_LD
/* Located in project source directory */
#include <custom-sections.ld>
#endif

}
16 changes: 16 additions & 0 deletions include/arch/nios2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ SECTIONS
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)

#ifdef CONFIG_CUSTOM_RODATA_LD
/* Located in project source directory */
#include <custom-rodata.ld>
#endif

} GROUP_LINK_IN(ROMABLE_REGION)

_image_rom_end = .;
Expand Down Expand Up @@ -177,6 +183,11 @@ SECTIONS
*(.data)
*(".data.*")

#ifdef CONFIG_CUSTOM_RWDATA_LD
/* Located in project source directory */
#include <custom-rwdata.ld>
#endif

/* the Nios2 architecture only has 16-bit signed immediate offsets in
* the instructions, so accessing a general address requires typically
* three instructions – basically, two for the two halves of the 32-bit
Expand Down Expand Up @@ -237,5 +248,10 @@ SECTIONS

GROUP_END(RAMABLE_REGION)

#ifdef CONFIG_CUSTOM_SECTIONS_LD
/* Located in project source directory */
#include <custom-sections.ld>
#endif

}

18 changes: 18 additions & 0 deletions include/arch/x86/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ SECTIONS
KEEP(*(irq_int_vector_map))
#endif

#ifdef CONFIG_CUSTOM_RODATA_LD
/* Located in project source directory */
#include <custom-rodata.ld>
#endif

KEXEC_PGALIGN_PAD(MMU_PAGE_SIZE)
} GROUP_LINK_IN(ROMABLE_REGION)

Expand All @@ -129,6 +134,12 @@ SECTIONS
*(.top_of_image_ram.*)
*(.data)
*(".data.*")

#ifdef CONFIG_CUSTOM_RWDATA_LD
/* Located in project source directory */
#include <custom-rwdata.ld>
#endif

. = ALIGN(4);
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

Expand Down Expand Up @@ -180,6 +191,7 @@ SECTIONS
*(.bottom_of_image_ram.*)
} GROUP_LINK_IN(RAMABLE_REGION)


/* Define linker symbols */
_image_ram_end = .;
_end = .; /* end of image */
Expand All @@ -201,6 +213,12 @@ SECTIONS
KEEP(*(.gnu.linkonce.intList.*))
__INT_LIST_END__ = .;
} > IDT_LIST

#ifdef CONFIG_CUSTOM_SECTIONS_LD
/* Located in project source directory */
#include <custom-sections.ld>
#endif

}

#ifdef CONFIG_XIP
Expand Down
21 changes: 21 additions & 0 deletions misc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ config CUSTOM_LINKER_SCRIPT
linker script and avoid having to change the script provided by
Zephyr.

config CUSTOM_RODATA_LD
bool "Include custom-rodata.ld"
default n
help
Include a customized linker script fragment for inserting additional
data and linker directives into the rodata section.

config CUSTOM_RWDATA_LD
bool "Include custom-rwdata.ld"
default n
help
Include a customized linker script fragment for inserting additional
data and linker directives into the data section.

config CUSTOM_SECTIONS_LD
bool "Include custom-sections.ld"
default n
help
Include a customized linker script fragment for inserting additional
arbitrary sections.

config CROSS_COMPILE
string "Cross-compiler tool prefix"
help
Expand Down

0 comments on commit 82f11bf

Please sign in to comment.