-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use include linker scripts #2841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
will-v-pi
merged 36 commits into
raspberrypi:develop
from
will-v-pi:include-linker-scripts
Mar 26, 2026
Merged
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b8f699f
Separate linker scripts out into include files
will-v-pi 6dc06e6
Add customisable heap location, with pico_set_linker_script_var function
will-v-pi 727cabd
Add kitchen sink test of custom linker scripts
will-v-pi 3a83dc7
Separate out rp2_common and platform-specific linker script sections
will-v-pi 4b1a3ec
More de-duplication
will-v-pi ae97a22
Make overriding ram locations simpler
will-v-pi 3811e67
Make it possible to reference default locations in pico_set_linker_sc…
will-v-pi a20f86a
Add pico_add_linker_script_override_path to make overriding individua…
will-v-pi 6bd9e5b
Add simple overlay demo
will-v-pi 2bf8555
Add scripts to bazel build
will-v-pi fe2d56b
Move linker scripts out of crt0
will-v-pi 1bd7089
Make rp2350 text sections the default
will-v-pi 72adb67
Rename scripts include directories to standard_scripts and platform_s…
will-v-pi e47fe09
Move and add example to Bazel docs for changing linker scripts
will-v-pi 9c0ed0c
Add cc_library load to new bazel files
will-v-pi e44bdea
Add excludes.ld files for default memmap
will-v-pi dc3a2e8
Put mem functions in SRAM
will-v-pi 8b9d9ec
Rename standard_scripts and platform_scripts to script_include
will-v-pi 91c3a9a
Move pico_platform_link stuff into pico_plaftorm
will-v-pi 614f7f0
Remove rigidity from linker include paths
will-v-pi da23cf4
Rename all linker scripts intended to be included to .incl
will-v-pi 780c05e
Separate into section_... files which do only contain one section, an…
will-v-pi 2e7ca67
Add section_extra files to make adding extra sections simpler
will-v-pi 4180427
Fix comments, and add some more extra files
will-v-pi c122bf7
Add generated override files - currently unused, but can be overridde…
will-v-pi ea8320b
pico_add_linker_script_override_path can now be called after target_l…
will-v-pi ad35ad8
Add extra post_platform_end sections
will-v-pi 8e3aca5
review fixups
will-v-pi e9e9a09
Fix bazel PICO_DEFAULT_LINKER_SCRIPT comment
will-v-pi 519544e
Add PICO_DEFAULT_BINARY_TYPE to Bazel
will-v-pi 3f0615d
Fix PICO_DEFAULT_BINARY_TYPE descriptions
will-v-pi a26a42b
Add azel pico_set_binary_type transition, to allow setting binary typ…
will-v-pi df68b1e
Refactor bazel functionality following review
will-v-pi 41fd36e
Add kitchen_sink_ram_section and kitchen_sink_simple_overlay tests to…
will-v-pi ea9b114
Some tidyups
will-v-pi c483e66
Update bazel/util/transition.bzl
will-v-pi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain") | ||
|
|
||
| def _linker_scripts_impl(ctx): | ||
| link_flags = [] | ||
| for script in ctx.attr.include_scripts: | ||
| link_include_dir = script.label.package | ||
| if ctx.label.workspace_root: | ||
| link_include_dir = "/".join((ctx.label.workspace_root, link_include_dir)) | ||
| link_flag = "-L" + str(link_include_dir) | ||
| if not (link_flag in link_flags): | ||
| link_flags.append(link_flag) | ||
|
|
||
| for script in ctx.files.link_scripts: | ||
| link_flags.append("-T" + str(script.path)) | ||
|
|
||
| all_scripts = ctx.files.link_scripts + ctx.files.include_scripts | ||
|
|
||
| linking_inputs = cc_common.create_linker_input( | ||
| owner = ctx.label, | ||
| user_link_flags = depset( | ||
| direct = link_flags, | ||
| ), | ||
| additional_inputs = depset(direct = all_scripts), | ||
| ) | ||
| return [ | ||
| DefaultInfo(files = depset(direct = all_scripts)), | ||
| CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset(direct = [linking_inputs]))), | ||
| ] | ||
|
|
||
| linker_scripts = rule( | ||
| implementation = _linker_scripts_impl, | ||
| attrs = { | ||
| "link_scripts": attr.label_list(allow_files = [".ld"], doc = "List of scripts to explicitly link"), | ||
| "include_scripts": attr.label_list(allow_files = [".incl"], doc = "List of scripts to include"), | ||
| }, | ||
| toolchains = use_cpp_toolchain(), | ||
| fragments = ["cpp"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /* Use blocked ram */ | ||
| RAM_ORIGIN = 0x21000000; | ||
|
|
||
| INCLUDE "memmap_default.incl" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| INCLUDE "memmap_copy_to_ram.incl" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| INCLUDE "memmap_default.incl" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| INCLUDE "memmap_no_flash.incl" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| load("//bazel/util:pico_linker_scripts.bzl", "linker_scripts") | ||
|
|
||
| linker_scripts( | ||
| name = "rp2040_linker_script_includes", | ||
| link_scripts = ["default_locations.ld"], | ||
| include_scripts = glob(["*.incl"]), | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "rp2040_linker_scripts", | ||
| target_compatible_with = ["//bazel/constraint:rp2040"], | ||
| deps = [ | ||
| "rp2040_linker_script_includes", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| RAM_ORIGIN_DEFAULT = 0x20000000; | ||
| RAM_LENGTH_DEFAULT = 256k; | ||
| SCRATCH_X_ORIGIN_DEFAULT = 0x20040000; | ||
| SCRATCH_X_LENGTH_DEFAULT = 4k; | ||
| SCRATCH_Y_ORIGIN_DEFAULT = 0x20041000; | ||
| SCRATCH_Y_LENGTH_DEFAULT = 4k; | ||
| XIP_RAM_ORIGIN_DEFAULT = 0x15000000; | ||
| XIP_RAM_LENGTH_DEFAULT = 16k; |
16 changes: 16 additions & 0 deletions
16
src/rp2040/pico_platform/script_include/section_boot2.incl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| SECTIONS | ||
| { | ||
| /* Second stage bootloader is prepended to the image. It must be 256 bytes big | ||
| and checksummed. It is usually built by the boot_stage2 target | ||
| in the Raspberry Pi Pico SDK | ||
| */ | ||
|
|
||
| .boot2 : { | ||
| __boot2_start__ = .; | ||
| KEEP (*(.boot2)) | ||
| __boot2_end__ = .; | ||
| } > TEXT_STORE | ||
|
|
||
| ASSERT(__boot2_end__ - __boot2_start__ == 256, | ||
| "ERROR: Pico second stage bootloader must be exactly 256 bytes in size for RP2040") | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/rp2040/pico_platform/script_include/section_no_flash_text.incl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* Defines the following symbols for use by code: | ||
| __logical_binary_start | ||
| __binary_info_header_end | ||
| __embedded_block_end | ||
| __reset_start, __reset_end | ||
| */ | ||
|
|
||
| SECTIONS | ||
| { | ||
| /* Note in NO_FLASH builds the entry point for both the bootrom, and debugger | ||
| entry (ELF entry point), are *first* in the image, and the vector table | ||
| follows immediately afterward. This is because the bootrom enters RAM | ||
| binaries directly at their lowest address (preferring main RAM over XIP | ||
| cache-as-SRAM if both are used). | ||
| */ | ||
|
|
||
| .text : { | ||
| __logical_binary_start = .; | ||
| __reset_start = .; | ||
| KEEP (*(.reset)) | ||
| __reset_end = .; | ||
| KEEP (*(.binary_info_header)) | ||
| __binary_info_header_end = .; | ||
| KEEP (*(.embedded_block)) | ||
| __embedded_block_end = .; | ||
| . = ALIGN(256); | ||
| KEEP (*(.vectors)) | ||
| *(.text*) | ||
| . = ALIGN(4); | ||
| *(.init) | ||
| *(.fini) | ||
| /* Pull all c'tors into .text */ | ||
| *crtbegin.o(.ctors) | ||
| *crtbegin?.o(.ctors) | ||
| *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) | ||
| *(SORT(.ctors.*)) | ||
| *(.ctors) | ||
| /* Followed by destructors */ | ||
| *crtbegin.o(.dtors) | ||
| *crtbegin?.o(.dtors) | ||
| *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) | ||
| *(SORT(.dtors.*)) | ||
| *(.dtors) | ||
|
|
||
| *(.eh_frame*) | ||
| } > RAM | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/rp2040/pico_platform/script_include/section_platform_end.incl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| SECTIONS | ||
| { | ||
| ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary") | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/rp2040/pico_platform/script_include/sections_copy_to_ram_text.incl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* This swaps section_boot2 and section_default_text, because RP2040 binaries must begin with boot2 */ | ||
|
|
||
| INCLUDE "section_flash_begin.incl" | ||
| INCLUDE "section_boot2.incl" | ||
| INCLUDE "section_copy_to_ram_flashtext.incl" | ||
| INCLUDE "section_copy_to_ram_rodata.incl" | ||
| INCLUDE "sections_arm_ex.incl" | ||
| INCLUDE "section_binary_info.incl" | ||
| INCLUDE "section_ram_vector_table.incl" | ||
| INCLUDE "section_uninitialized_data.incl" | ||
| INCLUDE "section_copy_to_ram_text.incl" |
8 changes: 8 additions & 0 deletions
8
src/rp2040/pico_platform/script_include/sections_default_text.incl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* This swaps section_boot2 and section_default_text, because RP2040 binaries must begin with boot2 */ | ||
|
|
||
| INCLUDE "section_flash_begin.incl" | ||
| INCLUDE "section_boot2.incl" | ||
| INCLUDE "section_default_text.incl" | ||
| INCLUDE "section_default_rodata.incl" | ||
| INCLUDE "sections_arm_ex.incl" | ||
| INCLUDE "section_binary_info.incl" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.