Skip to content

Commit bc9806e

Browse files
authored
Use include linker scripts (#2841)
* Separate linker scripts out into include files Allows for much simpler custom linker scripts * Add customisable heap location, with pico_set_linker_script_var function * Add kitchen sink test of custom linker scripts * Add pico_add_linker_script_override_path to make overriding individual files easier * Add simple overlay demo * Move linker scripts out of crt0 Breaking change for Bazel builds using different binary types, instead of setting PICO_DEFAULT_LINKER_SCRIPT to eg `//src/rp2_common/pico_crt0:no_flash_linker_script` it is now `//src/rp2_common/pico_standard_link:no_flash_linker_script` * Add excludes.ld files for default memmap * Put mem functions in SRAM * Add section_extra files to make adding extra sections simpler * Add generated override files - currently unused, but can be overridden in the future by CMake/bazel functions * Add PICO_DEFAULT_BINARY_TYPE to Bazel * Add bazel pico_set_binary_type transition, to allow setting binary type for individual binaries * Add kitchen_sink_ram_section and kitchen_sink_simple_overlay tests to bazel
1 parent 34bb4a4 commit bc9806e

File tree

105 files changed

+1702
-2149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1702
-2149
lines changed

bazel/config/BUILD.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,22 @@ string_flag(
217217
build_setting_default = "Debug",
218218
)
219219

220-
# PICO_BAZEL_CONFIG: PICO_DEFAULT_LINKER_SCRIPT, [Bazel only] The library that provides a linker script to link into all binaries, default=//src/rp2_common/pico_crt0:default_linker_script, group=pico_standard_link
220+
# PICO_BAZEL_CONFIG: PICO_DEFAULT_BINARY_TYPE, The default binary type to use, type=string, default=default, group=build
221+
string_flag(
222+
name = "PICO_DEFAULT_BINARY_TYPE",
223+
build_setting_default = "default",
224+
values = [
225+
"default",
226+
"no_flash",
227+
"copy_to_ram",
228+
"blocked_ram",
229+
],
230+
)
231+
232+
# PICO_BAZEL_CONFIG: PICO_DEFAULT_LINKER_SCRIPT, [Bazel only] The library that provides a linker script to link into all binaries, default=//src/rp2_common/pico_standard_link:default_linker_script, group=pico_standard_link
221233
label_flag(
222234
name = "PICO_DEFAULT_LINKER_SCRIPT",
223-
build_setting_default = "//src/rp2_common/pico_crt0:default_linker_script",
235+
build_setting_default = "//src/rp2_common/pico_standard_link:default_linker_script",
224236
)
225237

226238
# PICO_BAZEL_CONFIG: PICO_NO_TARGET_NAME, Don't define PICO_TARGET_NAME, type=bool, default=0, group=build

bazel/constraint/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,18 @@ config_setting(
273273
name = "pico_compilation_no_fastbuild_args_set",
274274
flag_values = {"//bazel/config:PICO_COMPILATION_NO_FASTBUILD_ARGS": "True"},
275275
)
276+
277+
config_setting(
278+
name = "pico_binary_type_no_flash",
279+
flag_values = {"//bazel/config:PICO_DEFAULT_BINARY_TYPE": "no_flash"},
280+
)
281+
282+
config_setting(
283+
name = "pico_binary_type_copy_to_ram",
284+
flag_values = {"//bazel/config:PICO_DEFAULT_BINARY_TYPE": "copy_to_ram"},
285+
)
286+
287+
config_setting(
288+
name = "pico_binary_type_blocked_ram",
289+
flag_values = {"//bazel/config:PICO_DEFAULT_BINARY_TYPE": "blocked_ram"},
290+
)

bazel/util/pico_linker_scripts.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "use_cpp_toolchain")
2+
3+
def _linker_scripts_impl(ctx):
4+
link_flags = []
5+
for script in ctx.attr.include_scripts:
6+
link_include_dir = script.label.package
7+
if ctx.label.workspace_root:
8+
link_include_dir = "/".join((ctx.label.workspace_root, link_include_dir))
9+
link_flag = "-L" + str(link_include_dir)
10+
if not (link_flag in link_flags):
11+
link_flags.append(link_flag)
12+
13+
for script in ctx.files.link_scripts:
14+
link_flags.append("-T" + str(script.path))
15+
16+
all_scripts = ctx.files.link_scripts + ctx.files.include_scripts
17+
18+
linking_inputs = cc_common.create_linker_input(
19+
owner = ctx.label,
20+
user_link_flags = depset(
21+
direct = link_flags,
22+
),
23+
additional_inputs = depset(direct = all_scripts),
24+
)
25+
return [
26+
DefaultInfo(files = depset(direct = all_scripts)),
27+
CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset(direct = [linking_inputs]))),
28+
]
29+
30+
linker_scripts = rule(
31+
implementation = _linker_scripts_impl,
32+
attrs = {
33+
"link_scripts": attr.label_list(allow_files = [".ld"], doc = "List of scripts to explicitly link"),
34+
"include_scripts": attr.label_list(allow_files = [".incl"], doc = "List of scripts to include"),
35+
},
36+
toolchains = use_cpp_toolchain(),
37+
fragments = ["cpp"],
38+
)

bazel/util/transition.bzl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,33 @@ extra_copts_for_all_deps = declare_transtion(
167167
"//command_line_option:copt": "extra_copts",
168168
},
169169
)
170+
171+
# This transition sets the binary type
172+
pico_set_binary_type = declare_transtion(
173+
attrs = {
174+
"binary_type": attr.string(),
175+
# This could be shared, but we don't in order to make it clearer that
176+
# a transition is in use.
177+
"_allowlist_function_transition": attr.label(
178+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
179+
),
180+
},
181+
flag_overrides = {
182+
"@pico-sdk//bazel/config:PICO_DEFAULT_BINARY_TYPE": "binary_type",
183+
},
184+
)
185+
186+
# This transition sets the linker script
187+
pico_set_linker_script = declare_transtion(
188+
attrs = {
189+
"linker_script": attr.string(),
190+
# This could be shared, but we don't in order to make it clearer that
191+
# a transition is in use.
192+
"_allowlist_function_transition": attr.label(
193+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
194+
),
195+
},
196+
flag_overrides = {
197+
"@pico-sdk//bazel/config:PICO_DEFAULT_LINKER_SCRIPT": "linker_script",
198+
},
199+
)

src/common/pico_base_headers/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ cc_library(
7474
"//bazel/constraint:rp2040": ["PICO_RP2040=1"],
7575
"//bazel/constraint:rp2350": ["PICO_RP2350=1"],
7676
"//conditions:default": [],
77+
}) + select({
78+
"//bazel/constraint:pico_binary_type_no_flash": ["PICO_NO_FLASH=1"],
79+
"//bazel/constraint:pico_binary_type_copy_to_ram": ["PICO_COPY_TO_RAM=1"],
80+
"//bazel/constraint:pico_binary_type_blocked_ram": ["PICO_USE_BLOCKED_RAM=1"],
81+
"//conditions:default": [],
7782
}),
7883
)
7984

src/rp2040/pico_platform/BUILD.bazel

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,96 @@ cc_library(
5454
"//src/rp2_common/pico_standard_link",
5555
],
5656
)
57+
58+
load("//bazel/util:pico_linker_scripts.bzl", "linker_scripts")
59+
60+
exports_files(
61+
[
62+
"memmap_blocked_ram.ld",
63+
"memmap_copy_to_ram.ld",
64+
"memmap_default.ld",
65+
"memmap_no_flash.ld",
66+
],
67+
)
68+
69+
linker_scripts(
70+
name = "memmap_default",
71+
link_scripts = ["memmap_default.ld"],
72+
)
73+
74+
linker_scripts(
75+
name = "memmap_blocked_ram",
76+
link_scripts = ["memmap_blocked_ram.ld"],
77+
)
78+
79+
linker_scripts(
80+
name = "memmap_copy_to_ram",
81+
link_scripts = ["memmap_copy_to_ram.ld"],
82+
)
83+
84+
linker_scripts(
85+
name = "memmap_no_flash",
86+
link_scripts = ["memmap_no_flash.ld"],
87+
)
88+
89+
cc_library(
90+
name = "default_linker_script",
91+
target_compatible_with = ["//bazel/constraint:rp2040"],
92+
visibility = [
93+
"//src/rp2_common/pico_standard_link:__pkg__",
94+
],
95+
deps = [
96+
"//src/rp2_common/pico_crt0:no_warn_rwx_flag",
97+
"//src/rp2040/pico_platform/script_include:rp2040_linker_scripts",
98+
"//src/rp2_common/pico_standard_link/script_include:rp2_linker_scripts",
99+
"//src/rp2_common/pico_standard_link:default_flash_region",
100+
"memmap_default",
101+
],
102+
)
103+
104+
# PICO_BUILD_DEFINE: PICO_USE_BLOCKED_RAM, whether this is a 'blocked_ram' build, type=bool, default=0, but dependent on CMake options, group=pico_standard_link
105+
cc_library(
106+
name = "blocked_ram_linker_script",
107+
target_compatible_with = ["//bazel/constraint:rp2040"],
108+
visibility = [
109+
"//src/rp2_common/pico_standard_link:__pkg__",
110+
],
111+
deps = [
112+
"//src/rp2_common/pico_crt0:no_warn_rwx_flag",
113+
"//src/rp2040/pico_platform/script_include:rp2040_linker_scripts",
114+
"//src/rp2_common/pico_standard_link/script_include:rp2_linker_scripts",
115+
"//src/rp2_common/pico_standard_link:default_flash_region",
116+
"memmap_blocked_ram",
117+
],
118+
)
119+
120+
# PICO_BUILD_DEFINE: PICO_COPY_TO_RAM, whether this is a 'copy_to_ram' build, type=bool, default=0, but dependent on CMake options, group=pico_standard_link
121+
cc_library(
122+
name = "copy_to_ram_linker_script",
123+
target_compatible_with = ["//bazel/constraint:rp2040"],
124+
visibility = [
125+
"//src/rp2_common/pico_standard_link:__pkg__",
126+
],
127+
deps = [
128+
"//src/rp2_common/pico_crt0:no_warn_rwx_flag",
129+
"//src/rp2040/pico_platform/script_include:rp2040_linker_scripts",
130+
"//src/rp2_common/pico_standard_link/script_include:rp2_linker_scripts",
131+
"//src/rp2_common/pico_standard_link:default_flash_region",
132+
"memmap_copy_to_ram",
133+
],
134+
)
135+
136+
# PICO_BUILD_DEFINE: PICO_NO_FLASH, whether this is a 'no_flash' build, type=bool, default=0, but dependent on CMake options, group=pico_standard_link
137+
cc_library(
138+
name = "no_flash_linker_script",
139+
target_compatible_with = ["//bazel/constraint:rp2040"],
140+
visibility = [
141+
"//src/rp2_common/pico_standard_link:__pkg__",
142+
],
143+
deps = [
144+
"//src/rp2_common/pico_crt0:no_warn_rwx_flag",
145+
"//src/rp2040/pico_platform/script_include:rp2040_linker_scripts",
146+
"//src/rp2_common/pico_standard_link/script_include:rp2_linker_scripts",
147+
"memmap_no_flash",
148+
],
149+
)

src/rp2040/pico_platform/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ if (NOT TARGET pico_platform)
2222
pico_platform_panic
2323
pico_platform_sections
2424
)
25+
26+
pico_register_common_scope_var(PICO_LINKER_SCRIPT_PATH)
27+
if (NOT PICO_LINKER_SCRIPT_PATH)
28+
set(PICO_LINKER_SCRIPT_PATH ${CMAKE_CURRENT_LIST_DIR})
29+
endif()
30+
31+
pico_register_common_scope_var(PICO_LINKER_DEFAULT_LOCATIONS_PATH)
32+
if (NOT PICO_LINKER_DEFAULT_LOCATIONS_PATH)
33+
set(PICO_LINKER_DEFAULT_LOCATIONS_PATH ${CMAKE_CURRENT_LIST_DIR}/script_include/default_locations.ld)
34+
endif()
35+
36+
pico_register_common_scope_var(PICO_LINKER_SCRIPT_INCLUDE_DIRS)
37+
list(APPEND PICO_LINKER_SCRIPT_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/script_include)
38+
39+
pico_promote_common_scope_vars()
2540
endif()
2641

2742
function(pico_add_platform_library TARGET)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Use blocked ram */
2+
RAM_ORIGIN = 0x21000000;
3+
4+
INCLUDE "memmap_default.incl"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INCLUDE "memmap_copy_to_ram.incl"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INCLUDE "memmap_default.incl"

0 commit comments

Comments
 (0)