Skip to content

Commit fb61ec6

Browse files
committed
linker generator: Handle .last_section
Add cmake linker generator things for .last_section. Follow up to #88970. Fixes bug #89719, for gcc/ld & iar Signed-off-by: Björn Bergman <bjorn.bergman@iar.com>
1 parent 9a409d6 commit fb61ec6

File tree

4 files changed

+110
-80
lines changed

4 files changed

+110
-80
lines changed

cmake/linker/iar/config_file_script.cmake

Lines changed: 90 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ function(process_region)
9797
EXPR "@ADDR(${symbol_start})@"
9898
)
9999
endif()
100+
# Treat BSS to be noinit
101+
if(type STREQUAL BSS)
102+
set_property(GLOBAL PROPERTY ${section}_NOINIT TRUE)
103+
endif()
104+
endforeach() # all sections
100105

101-
endforeach()
102-
106+
#Add houseeeping symbols for sektion start, end, size, load start.
103107
get_property(groups GLOBAL PROPERTY ${REGION_OBJECT}_GROUP_LIST_ORDERED)
104108
foreach(group ${groups})
105109
get_property(name GLOBAL PROPERTY ${group}_NAME)
@@ -146,6 +150,7 @@ function(process_region)
146150

147151
endforeach()
148152

153+
# create_symbol() for region-symbols that dont have an expression ?
149154
get_property(symbols GLOBAL PROPERTY ${REGION_OBJECT}_SYMBOLS)
150155
foreach(symbol ${symbols})
151156
get_property(name GLOBAL PROPERTY ${symbol}_NAME)
@@ -173,6 +178,7 @@ function(process_region)
173178
endif()
174179
endif()
175180

181+
#Short circuit our vma and lma to the parent's vma and lma
176182
if(${parent_type} STREQUAL GROUP)
177183
get_property(vma GLOBAL PROPERTY ${parent}_VMA)
178184
get_property(lma GLOBAL PROPERTY ${parent}_LMA)
@@ -244,7 +250,7 @@ function(system_to_string)
244250
set(${STRING_STRING} "${${STRING_STRING}}\n\n")
245251
set_property(GLOBAL PROPERTY ILINK_SYMBOL_ICF)
246252

247-
set(${STRING_STRING} "${${STRING_STRING}}\n")
253+
#Generate all regions
248254
foreach(region ${regions})
249255
get_property(empty GLOBAL PROPERTY ${region}_EMPTY)
250256
if(NOT empty)
@@ -254,8 +260,29 @@ function(system_to_string)
254260
set(ILINK_CURRENT_NAME)
255261
endif()
256262
endforeach()
257-
set(${STRING_STRING} "${${STRING_STRING}}\n")
263+
set(${STRING_STRING} "${${STRING_STRING}}\n/*SYSTEM_SECTIONS*/\n")
264+
265+
# Sections that sit directly under the system are fishy characters.
266+
# Currently there are two classes of them:
267+
# 1 - .rel.iplt & friends - these are not used by iar tools currently.
268+
# These do not have any parents, so get no placement. Ignore them for
269+
# now, since the get Error[Lc041]: "foo" defined but not referenced
270+
# 2 - TYPE LINKER_SCRIPT_FOOTER - these have vma and lma settings, and so
271+
# are easy to handle
272+
get_property(sections GLOBAL PROPERTY ${STRING_OBJECT}_SECTIONS)
273+
foreach(section ${sections})
274+
message("system_to_string: doing section ${section}")
275+
get_property(vma GLOBAL PROPERTY ${section}_VMA)
276+
get_property(lma GLOBAL PROPERTY ${section}_LMA)
277+
message("system_to_string: doing section ${section} vma:${vma} lma:${lma}")
278+
if(DEFINED lma OR DEFINED vma)
279+
to_string(OBJECT ${section} STRING ${STRING_STRING})
280+
place_in_region(STRING place OBJECT ${section})
281+
string(APPEND ${STRING_STRING} "${place}")
282+
endif()
283+
endforeach()
258284

285+
#Generate all image symbols we have collected
259286
get_property(symbols_icf GLOBAL PROPERTY ILINK_SYMBOL_ICF)
260287
foreach(image_symbol ${symbols_icf})
261288
set(${STRING_STRING} "${${STRING_STRING}}define image symbol ${image_symbol};\n")
@@ -281,6 +308,38 @@ function(system_to_string)
281308
set(${STRING_STRING} ${${STRING_STRING}} PARENT_SCOPE)
282309
endfunction()
283310

311+
#A helper to output "place in <Region>"
312+
function(place_in_region)
313+
cmake_parse_arguments(PLACE "" "OBJECT;STRING" "" ${ARGN})
314+
set(section ${PLACE_OBJECT})
315+
get_property(name GLOBAL PROPERTY ${section}_NAME)
316+
317+
get_property(name_clean GLOBAL PROPERTY ${section}_NAME_CLEAN)
318+
319+
get_property(parent GLOBAL PROPERTY ${section}_PARENT)
320+
get_property(noinit GLOBAL PROPERTY ${section}_NOINIT)
321+
# This is only a trick to get the memories
322+
get_property(parent_type GLOBAL PROPERTY ${parent}_OBJ_TYPE)
323+
if(${parent_type} STREQUAL GROUP)
324+
get_property(vma GLOBAL PROPERTY ${parent}_VMA)
325+
get_property(lma GLOBAL PROPERTY ${parent}_LMA)
326+
endif()
327+
328+
if(DEFINED vma)
329+
set(ILINK_CURRENT_NAME ${vma})
330+
elseif(DEFINED lma)
331+
set(ILINK_CURRENT_NAME ${lma})
332+
else()
333+
# message(FATAL_ERROR "Need either vma or lma")
334+
endif()
335+
336+
set(result "\"${name}\": place in ${ILINK_CURRENT_NAME} { block ${name_clean} };\n")
337+
if(DEFINED vma AND DEFINED lma AND (NOT ${noinit}) AND NOT ("${vma}" STREQUAL "${lma}") )
338+
string(APPEND result "\"${name}_init\": place in ${lma} { block ${name_clean}_init };\n")
339+
endif()
340+
set(${PLACE_STRING} "${result}" PARENT_SCOPE)
341+
endfunction()
342+
284343
function(group_to_string)
285344
cmake_parse_arguments(STRING "" "OBJECT;STRING" "" ${ARGN})
286345

@@ -302,55 +361,36 @@ function(group_to_string)
302361
set(${STRING_STRING} "${${STRING_STRING}}\"${name}\": place at address mem:${section_address} { block ${name_clean} };\n")
303362
endforeach()
304363

364+
#Generate sub-groups
305365
get_property(groups GLOBAL PROPERTY ${STRING_OBJECT}_GROUPS)
306366
foreach(group ${groups})
307367
to_string(OBJECT ${group} STRING ${STRING_STRING})
308368
endforeach()
309369

370+
#Generate sections
310371
get_property(sections GLOBAL PROPERTY ${STRING_OBJECT}_SECTIONS)
311372
foreach(section ${sections})
312373
to_string(OBJECT ${section} STRING ${STRING_STRING})
313374

314-
get_property(name GLOBAL PROPERTY ${section}_NAME)
315-
316-
get_property(name_clean GLOBAL PROPERTY ${section}_NAME_CLEAN)
317-
318-
get_property(parent GLOBAL PROPERTY ${section}_PARENT)
319-
get_property(noinit GLOBAL PROPERTY ${section}_NOINIT)
320-
# This is only a trick to get the memories
321-
get_property(parent_type GLOBAL PROPERTY ${parent}_OBJ_TYPE)
322-
if(${parent_type} STREQUAL GROUP)
323-
get_property(vma GLOBAL PROPERTY ${parent}_VMA)
324-
get_property(lma GLOBAL PROPERTY ${parent}_LMA)
325-
endif()
326-
327-
if(DEFINED vma)
328-
set(ILINK_CURRENT_NAME ${vma})
329-
elseif(DEFINED lma)
330-
set(ILINK_CURRENT_NAME ${lma})
331-
else()
332-
# message(FATAL_ERROR "Need either vma or lma")
333-
endif()
334-
335-
set(${STRING_STRING} "${${STRING_STRING}}\"${name}\": place in ${ILINK_CURRENT_NAME} { block ${name_clean} };\n")
336-
if(DEFINED vma AND DEFINED lma AND NOT ${noinit})
337-
set(${STRING_STRING} "${${STRING_STRING}}\"${name}_init\": place in ${lma} { block ${name_clean}_init };\n")
338-
endif()
339-
375+
place_in_region(STRING place OBJECT ${section})
376+
string(APPEND ${STRING_STRING} "${place}")
340377
endforeach()
341378

342379
get_parent(OBJECT ${STRING_OBJECT} PARENT parent TYPE SYSTEM)
343380
get_property(regions GLOBAL PROPERTY ${parent}_REGIONS)
344381
list(REMOVE_ITEM regions ${STRING_OBJECT})
345382

383+
#Go over REGIONS
346384
foreach(region ${regions})
347385
get_property(vma GLOBAL PROPERTY ${region}_NAME)
348386
get_property(sections GLOBAL PROPERTY ${STRING_OBJECT}_${vma}_SECTIONS_FIXED)
349387

388+
#Generate our fixed-sections that has vma in this region
350389
foreach(section ${sections})
351390
to_string(OBJECT ${section} STRING ${STRING_STRING})
352391
endforeach()
353392

393+
#generate our groups with vma in region
354394
get_property(groups GLOBAL PROPERTY ${STRING_OBJECT}_${vma}_GROUPS)
355395
foreach(group ${groups})
356396
to_string(OBJECT ${group} STRING ${STRING_STRING})
@@ -379,20 +419,20 @@ function(group_to_string)
379419
endif()
380420
endif()
381421

382-
if(${name_clean} STREQUAL last_ram_section)
383-
get_property(group_name_lma GLOBAL PROPERTY ILINK_ROM_REGION_NAME)
384-
set(${STRING_STRING} "${${STRING_STRING}}\n")
385-
if(${CONFIG_LINKER_LAST_SECTION_ID})
386-
set(${STRING_STRING} "${${STRING_STRING}}define section last_section_id { udata32 ${CONFIG_LINKER_LAST_SECTION_ID_PATTERN}; };\n")
387-
set(${STRING_STRING} "${${STRING_STRING}}define block last_section with fixed order { section last_section_id };\n")
388-
else()
389-
set(${STRING_STRING} "${${STRING_STRING}}define block last_section with fixed order { };\n")
390-
endif()
391-
# Not really the right place, we want the last used flash bytes not end of the world!
392-
# set(${STRING_STRING} "${${STRING_STRING}}\".last_section\": place at end of ${group_name_lma} { block last_section };\n")
393-
set(${STRING_STRING} "${${STRING_STRING}}\".last_section\": place in ${group_name_lma} { block last_section };\n")
394-
set(${STRING_STRING} "${${STRING_STRING}}keep { block last_section };\n")
395-
endif()
422+
# if(${name_clean} STREQUAL last_ram_section)
423+
# get_property(group_name_lma GLOBAL PROPERTY ILINK_ROM_REGION_NAME)
424+
# set(${STRING_STRING} "${${STRING_STRING}}\n")
425+
# if(${CONFIG_LINKER_LAST_SECTION_ID})
426+
# set(${STRING_STRING} "${${STRING_STRING}}define section last_section_id { udata32 ${CONFIG_LINKER_LAST_SECTION_ID_PATTERN}; };\n")
427+
# set(${STRING_STRING} "${${STRING_STRING}}define block last_section with fixed order { section last_section_id };\n")
428+
# else()
429+
# set(${STRING_STRING} "${${STRING_STRING}}define block last_section with fixed order { };\n")
430+
# endif()
431+
# # Not really the right place, we want the last used flash bytes not end of the world!
432+
# # set(${STRING_STRING} "${${STRING_STRING}}\".last_section\": place at end of ${group_name_lma} { block last_section };\n")
433+
# set(${STRING_STRING} "${${STRING_STRING}}\".last_section\": place in ${group_name_lma} { block last_section };\n")
434+
# set(${STRING_STRING} "${${STRING_STRING}}keep { block last_section };\n")
435+
# endif()
396436

397437
endforeach()
398438
endforeach()
@@ -602,7 +642,7 @@ function(section_to_string)
602642

603643
if(DEFINED first_index AND first_index EQUAL ${idx})
604644
# Create the offset
605-
set(TEMP "${TEMP}\n block ${first_index_section_name}")
645+
set(TEMP "${TEMP}\n /*first_index*/\n block ${first_index_section_name}")
606646
list(APPEND block_attr "size = ${first_index_offset}")
607647
if(sort)
608648
if(${sort} STREQUAL NAME)
@@ -677,11 +717,6 @@ function(section_to_string)
677717
set(block_attr)
678718
set(block_attr_str)
679719

680-
if(empty)
681-
set(TEMP "${TEMP}\n {")
682-
set(empty FALSE)
683-
endif()
684-
685720
list(GET input -1 last_input)
686721

687722
set(TEMP "${TEMP} {")
@@ -697,21 +732,6 @@ function(section_to_string)
697732

698733
set(section_type "")
699734

700-
# build for ram, no section_type
701-
# if("${lma}" STREQUAL "${vma}")
702-
# # if("${vma}" STREQUAL "")
703-
# set(section_type "")
704-
# # else()
705-
# # set(section_type " readwrite")
706-
# # endif()
707-
# elseif(NOT "${vma}" STREQUAL "")
708-
# set(section_type " readwrite")
709-
# elseif(NOT "${lma}" STREQUAL "")
710-
# set(section_type " readonly")
711-
# else()
712-
# message(FATAL_ERROR "How to handle this? lma=${lma} vma=${vma}")
713-
# endif()
714-
715735
set(TEMP "${TEMP}${section_type} ${part}section ${setting}")
716736
set_property(GLOBAL APPEND PROPERTY ILINK_CURRENT_SECTIONS "section ${setting}")
717737
set(section_type "")
@@ -777,21 +797,13 @@ function(section_to_string)
777797

778798
set(TEMP "${TEMP}\n};")
779799

780-
get_property(type GLOBAL PROPERTY ${parent}_OBJ_TYPE)
781-
if(${type} STREQUAL REGION)
782-
get_property(name GLOBAL PROPERTY ${parent}_NAME)
783-
get_property(address GLOBAL PROPERTY ${parent}_ADDRESS)
784-
get_property(size GLOBAL PROPERTY ${parent}_SIZE)
785-
786-
endif()
787-
788800
get_property(current_sections GLOBAL PROPERTY ILINK_CURRENT_SECTIONS)
789-
790-
if(DEFINED group_parent_vma AND DEFINED group_parent_lma)
791-
if(${noinit})
801+
if(${noinit})
792802
list(JOIN current_sections ", " SELECTORS)
793803
set(TEMP "${TEMP}\ndo not initialize {\n${SELECTORS}\n};")
794-
else()
804+
endif()
805+
if(DEFINED group_parent_vma AND DEFINED group_parent_lma)
806+
if(NOT ${noinit})
795807
# Generate the _init block and the initialize manually statement.
796808
# Note that we need to have the X_init block defined even if we have
797809
# no sections, since there will come a "place in XXX" statement later.
@@ -815,7 +827,7 @@ function(section_to_string)
815827
foreach(section ${current_sections})
816828
set(TEMP "${TEMP} ${section},\n")
817829
endforeach()
818-
set(TEMP "${TEMP}};")
830+
set(TEMP "${TEMP}};\n")
819831
set(current_sections)
820832
endif()
821833
endif()

cmake/linker/linker_script_common.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ function(create_section)
221221
else()
222222
set(parent ${SECTION_SYSTEM})
223223
endif()
224+
if(SECTION_TYPE STREQUAL "LINKER_SCRIPT_FOOTER")
225+
set(SECTION_VMA) # pretend that we have no VMA, so the section ends up in
226+
# the general heap of sections directly below the system
227+
set(parent ${SECTION_SYSTEM})
228+
endif()
224229

225230
set_property(GLOBAL PROPERTY SECTION_${SECTION_NAME}_PARENT ${parent})
226231
add_section(OBJECT ${parent} SECTION ${SECTION_NAME} ADDRESS ${SECTION_ADDRESS} VMA ${SECTION_VMA})

cmake/linker_script/arm/linker.cmake

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ if(CONFIG_USERSPACE)
155155
zephyr_linker_symbol(SYMBOL "_app_smem_rom_start" EXPR "@__app_smem_group_load_start@")
156156

157157

158-
zephyr_linker_section(NAME .bss VMA RAM LMA FLASH TYPE BSS)
158+
zephyr_linker_section(NAME .bss GROUP RAM_REGION TYPE BSS)
159159
zephyr_linker_section_configure(SECTION .bss INPUT COMMON)
160160
zephyr_linker_section_configure(SECTION .bss INPUT ".kernel_bss.*")
161161

@@ -175,7 +175,7 @@ include(${COMMON_ZEPHYR_LINKER_DIR}/common-ram.cmake)
175175
include(${COMMON_ZEPHYR_LINKER_DIR}/kobject-data.cmake)
176176

177177
if(NOT CONFIG_USERSPACE)
178-
zephyr_linker_section(NAME .bss VMA RAM LMA FLASH TYPE BSS)
178+
zephyr_linker_section(NAME .bss GROUP RAM_REGION TYPE BSS)
179179
zephyr_linker_section_configure(SECTION .bss INPUT COMMON)
180180
zephyr_linker_section_configure(SECTION .bss INPUT ".kernel_bss.*")
181181
# As memory is cleared in words only, it is simpler to ensure the BSS
@@ -256,3 +256,12 @@ dt_comp_path(paths COMPATIBLE "zephyr,memory-region")
256256
foreach(path IN LISTS paths)
257257
zephyr_linker_dts_section(PATH ${path})
258258
endforeach()
259+
260+
261+
# .last_section must be last in romable region
262+
# .last_section contains a fixed word to ensure location counter and actual
263+
# rom region data usage match when CONFIG_LINKER_LAST_SECTION_ID=y.
264+
zephyr_linker_section(NAME .last_section VMA FLASH LMA FLASH
265+
NOINPUT TYPE LINKER_SCRIPT_FOOTER)
266+
# KEEP can not be passed to zephyr_linker_section, so:
267+
zephyr_linker_section_configure(SECTION .last_section INPUT ".last_section" KEEP)

cmake/modules/extensions.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,6 +5074,10 @@ endfunction()
50745074
# the given passes. Empty list means no passes.
50755075
# PASS NOT [<p1>] [<p2>...] makes the section present in
50765076
# all but the given passes. Empty list means all passes.
5077+
# TYPE <type> : Tag section for special treatment.
5078+
# NOLOAD, BSS - Ensure that the section is NOLOAD
5079+
# LINKER_SCRIPT_FOOTER - One single section to be
5080+
# generated last
50775081
# Note: VMA and LMA are mutual exclusive with GROUP
50785082
#
50795083
function(zephyr_linker_section)

0 commit comments

Comments
 (0)