Skip to content

Commit 6600c64

Browse files
dcpleungnashif
authored andcommitted
linker: warn about orphan sections
(Previous patch set was reverted due to issue with priv_stack. Resubmitting after fixing the faults caused by priv_stack.noinit not at the end of RAM.) This adds a linker flag and necessary changes to linker scripts so that linker will warn about orphan sections. Relates to #5534. Fixes #10473, #10474, #10515. Signed-off-by: Daniel Leung <[email protected]>
1 parent 1fa8cf9 commit 6600c64

File tree

24 files changed

+312
-0
lines changed

24 files changed

+312
-0
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,32 @@ zephyr_ld_options(
287287
${LINKERFLAGPREFIX},--build-id=none
288288
)
289289

290+
if(NOT CONFIG_NATIVE_APPLICATION)
291+
# Funny thing is if this is set to =error, some architectures will
292+
# skip this flag even though the compiler flag check passes
293+
# (e.g. ARC and Xtensa). So warning should be the default for now.
294+
#
295+
# Skip this for native application as Zephyr only provides
296+
# additions to the host toolchain linker script. The relocation
297+
# sections (.rel*) requires us to override those provided
298+
# by host toolchain. As we can't account for all possible
299+
# combination of compiler and linker on all machines used
300+
# for development, it is better to turn this off.
301+
#
302+
# CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
303+
# without any warnings or errors, which is the default behavior.
304+
# So there is no need to explicity set a linker flag.
305+
if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
306+
zephyr_ld_options(
307+
${LINKERFLAGPREFIX},--orphan-handling=warn
308+
)
309+
elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
310+
zephyr_ld_options(
311+
${LINKERFLAGPREFIX},--orphan-handling=error
312+
)
313+
endif()
314+
endif()
315+
290316
if(CONFIG_HAVE_CUSTOM_LINKER_SCRIPT)
291317
set(LINKER_SCRIPT ${APPLICATION_SOURCE_DIR}/${CONFIG_CUSTOM_LINKER_SCRIPT})
292318
if(NOT EXISTS ${LINKER_SCRIPT})

Kconfig.zephyr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ source "subsys/Kconfig"
4343
source "ext/Kconfig"
4444

4545
source "tests/Kconfig"
46+
47+
choice
48+
prompt "Linker Orphan Section Handling"
49+
default LINKER_ORPHAN_SECTION_WARN
50+
51+
config LINKER_ORPHAN_SECTION_PLACE
52+
bool "Place"
53+
help
54+
Linker puts orphan sections in place without warnings
55+
or errors.
56+
57+
config LINKER_ORPHAN_SECTION_WARN
58+
bool "Warn"
59+
help
60+
Linker places the orphan sections in ouput and issues
61+
warning about those sections.
62+
63+
config LINKER_ORPHAN_SECTION_ERROR
64+
bool "Error"
65+
help
66+
Linker exits with error when an orphan section is found.
67+
68+
endchoice

include/arch/arc/v2/linker.ld

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ MEMORY {
7373
}
7474

7575
SECTIONS {
76+
77+
#include <linker/rel-sections.ld>
78+
7679
GROUP_START(ROMABLE_REGION)
7780

7881
SECTION_PROLOGUE(_TEXT_SECTION_NAME,,ALIGN(1024)) {
@@ -283,4 +286,7 @@ SECTIONS {
283286
#ifdef CONFIG_GEN_ISR_TABLES
284287
#include <linker/intlist.ld>
285288
#endif
289+
290+
#include <linker/debug-sections.ld>
291+
286292
}

include/arch/arm/cortex_m/scripts/linker.ld

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ ENTRY(CONFIG_KERNEL_ENTRY)
8787

8888
SECTIONS
8989
{
90+
91+
#include <linker/rel-sections.ld>
92+
93+
/*
94+
* .plt and .iplt are here according to 'arm-zephyr-elf-ld --verbose',
95+
* before text section.
96+
*/
97+
SECTION_PROLOGUE(.plt,,)
98+
{
99+
*(.plt)
100+
}
101+
102+
SECTION_PROLOGUE(.iplt,,)
103+
{
104+
*(.iplt)
105+
}
106+
90107
GROUP_START(ROMABLE_REGION)
91108

92109
_image_rom_start = ROM_ADDR;
@@ -133,6 +150,12 @@ SECTIONS
133150
*(".text.*")
134151
*(.gnu.linkonce.t.*)
135152

153+
/*
154+
* These are here according to 'arm-zephyr-elf-ld --verbose',
155+
* after .gnu.linkonce.t.*
156+
*/
157+
*(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx)
158+
136159
#include <linker/priv_stacks-text.ld>
137160
#include <linker/kobject-text.ld>
138161

@@ -219,6 +242,18 @@ SECTIONS
219242
} > FLASH_CCFG
220243
#endif
221244

245+
/*
246+
* These are here according to 'arm-zephyr-elf-ld --verbose',
247+
* before data section.
248+
*/
249+
SECTION_PROLOGUE(.got,,)
250+
{
251+
*(.got.plt)
252+
*(.igot.plt)
253+
*(.got)
254+
*(.igot)
255+
}
256+
222257
GROUP_START(RAMABLE_REGION)
223258

224259

@@ -429,4 +464,12 @@ SECTIONS
429464
#include <linker/intlist.ld>
430465
#endif
431466

467+
#include <linker/debug-sections.ld>
468+
469+
SECTION_PROLOGUE(.ARM.attributes, 0,)
470+
{
471+
KEEP(*(.ARM.attributes))
472+
KEEP(*(.gnu.attributes))
473+
}
474+
432475
}

include/arch/nios2/linker.ld

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ ENTRY(CONFIG_KERNEL_ENTRY)
8282

8383
SECTIONS
8484
{
85+
86+
#include <linker/rel-sections.ld>
87+
88+
/*
89+
* .plt and .iplt are here according to
90+
* 'nios2-zephyr-elf-ld --verbose', before text section.
91+
*/
92+
SECTION_PROLOGUE(.plt,,)
93+
{
94+
*(.plt)
95+
}
96+
97+
SECTION_PROLOGUE(.iplt,,)
98+
{
99+
*(.iplt)
100+
}
101+
85102
GROUP_START(ROMABLE_REGION)
86103
_image_rom_start = _ROM_ADDR;
87104

@@ -261,5 +278,7 @@ SECTIONS
261278
#include <linker/intlist.ld>
262279
#endif
263280

281+
#include <linker/debug-sections.ld>
282+
264283
}
265284

include/arch/riscv32/common/linker.ld

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ ENTRY(CONFIG_KERNEL_ENTRY)
4646
SECTIONS
4747
{
4848

49+
#include <linker/rel-sections.ld>
50+
51+
/*
52+
* The .plt and .iplt are here according to
53+
* 'riscv32-zephyr-elf-ld --verbose', before text section.
54+
*/
55+
SECTION_PROLOGUE(.plt,,)
56+
{
57+
*(.plt)
58+
}
59+
60+
SECTION_PROLOGUE(.iplt,,)
61+
{
62+
*(.iplt)
63+
}
64+
4965
GROUP_START(ROM)
5066
_image_rom_start = .;
5167

@@ -175,4 +191,7 @@ SECTIONS
175191
#endif
176192

177193
GROUP_END(RAMABLE_REGION)
194+
195+
#include <linker/debug-sections.ld>
196+
178197
}

include/arch/riscv32/pulpino/linker.ld

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ ENTRY(CONFIG_KERNEL_ENTRY)
4141

4242
SECTIONS
4343
{
44+
45+
#include <linker/rel-sections.ld>
46+
47+
/*
48+
* .plt and .iplt are here according to
49+
* 'riscv32-zephyr-elf-ld --verbose', before text section.
50+
*/
51+
SECTION_PROLOGUE(.plt,,)
52+
{
53+
*(.plt)
54+
}
55+
56+
SECTION_PROLOGUE(.iplt,,)
57+
{
58+
*(.iplt)
59+
}
60+
4461
GROUP_START(INSTRRAM)
4562

4663
SECTION_PROLOGUE(_VECTOR_SECTION_NAME,,)
@@ -71,6 +88,7 @@ SECTIONS
7188
*(.text)
7289
*(".text.*")
7390
*(.gnu.linkonce.t.*)
91+
*(.eh_frame)
7492
} GROUP_LINK_IN(INSTRRAM)
7593

7694
_image_text_end = .;
@@ -160,4 +178,7 @@ SECTIONS
160178
#endif
161179

162180
GROUP_END(RAMABLE_REGION)
181+
182+
#include <linker/debug-sections.ld>
183+
163184
}

include/arch/x86/linker.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ ENTRY(CONFIG_KERNEL_ENTRY)
6666
/* SECTIONS definitions */
6767
SECTIONS
6868
{
69+
70+
#include <linker/rel-sections.ld>
71+
6972
GROUP_START(ROMABLE_REGION)
7073
#ifdef CONFIG_REALMODE
7174
/* 16-bit sections */
@@ -378,6 +381,8 @@ SECTIONS
378381
#include <custom-sections.ld>
379382
#endif
380383

384+
#include <linker/debug-sections.ld>
385+
381386
}
382387

383388
#ifdef CONFIG_XIP

include/linker/debug-sections.ld

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* following sections are obtained via 'ld --verbose' */
2+
3+
/* Stabs debugging sections. */
4+
SECTION_PROLOGUE(.stab, 0,) { *(.stab) }
5+
SECTION_PROLOGUE(.stabstr, 0,) { *(.stabstr) }
6+
SECTION_PROLOGUE(.stab.excl, 0,) { *(.stab.excl) }
7+
SECTION_PROLOGUE(.stab.exclstr, 0,) { *(.stab.exclstr) }
8+
SECTION_PROLOGUE(.stab.index, 0,) { *(.stab.index) }
9+
SECTION_PROLOGUE(.stab.indexstr, 0,) { *(.stab.indexstr) }
10+
SECTION_PROLOGUE(.comment, 0,) { *(.comment) }
11+
/* DWARF debug sections.
12+
Symbols in the DWARF debugging sections are relative to the beginning
13+
of the section so we begin them at 0. */
14+
/* DWARF 1 */
15+
SECTION_PROLOGUE(.debug, 0,) { *(.debug) }
16+
SECTION_PROLOGUE(.line, 0,) { *(.line) }
17+
/* GNU DWARF 1 extensions */
18+
SECTION_PROLOGUE(.debug_srcinfo, 0,) { *(.debug_srcinfo) }
19+
SECTION_PROLOGUE(.debug_sfnames, 0,) { *(.debug_sfnames) }
20+
/* DWARF 1.1 and DWARF 2 */
21+
SECTION_PROLOGUE(.debug_aranges, 0,) { *(.debug_aranges) }
22+
SECTION_PROLOGUE(.debug_pubnames, 0,) { *(.debug_pubnames) }
23+
/* DWARF 2 */
24+
SECTION_PROLOGUE(.debug_info, 0,) { *(.debug_info .gnu.linkonce.wi.*) }
25+
SECTION_PROLOGUE(.debug_abbrev, 0,) { *(.debug_abbrev) }
26+
SECTION_PROLOGUE(.debug_line, 0,) { *(.debug_line .debug_line.* .debug_line_end ) }
27+
SECTION_PROLOGUE(.debug_frame, 0,) { *(.debug_frame) }
28+
SECTION_PROLOGUE(.debug_str, 0,) { *(.debug_str) }
29+
SECTION_PROLOGUE(.debug_loc, 0,) { *(.debug_loc) }
30+
SECTION_PROLOGUE(.debug_macinfo, 0,) { *(.debug_macinfo) }
31+
/* SGI/MIPS DWARF 2 extensions */
32+
SECTION_PROLOGUE(.debug_weaknames, 0,) { *(.debug_weaknames) }
33+
SECTION_PROLOGUE(.debug_funcnames, 0,) { *(.debug_funcnames) }
34+
SECTION_PROLOGUE(.debug_typenames, 0,) { *(.debug_typenames) }
35+
SECTION_PROLOGUE(.debug_varnames, 0,) { *(.debug_varnames) }
36+
/* DWARF 3 */
37+
SECTION_PROLOGUE(.debug_pubtypes, 0,) { *(.debug_pubtypes) }
38+
SECTION_PROLOGUE(.debug_ranges, 0,) { *(.debug_ranges) }
39+
/* DWARF Extension. */
40+
SECTION_PROLOGUE(.debug_macro, 0,) { *(.debug_macro) }

include/linker/rel-sections.ld

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* .rel.* are for relocation.
3+
* These are being produced by compiler/linker.
4+
* Specify these here so they are not considered orphan sections.
5+
*/
6+
7+
SECTION_PROLOGUE(.rel.plt,,)
8+
{
9+
*(.rel.plt)
10+
11+
PROVIDE_HIDDEN (__rel_iplt_start = .);
12+
*(.rel.iplt)
13+
PROVIDE_HIDDEN (__rel_iplt_end = .);
14+
}
15+
16+
SECTION_PROLOGUE(.rela.plt,,)
17+
{
18+
*(.rela.plt)
19+
20+
PROVIDE_HIDDEN (__rela_iplt_start = .);
21+
*(.rela.iplt)
22+
PROVIDE_HIDDEN (__rela_iplt_end = .);
23+
}
24+
25+
SECTION_PROLOGUE(.rel.dyn,,)
26+
{
27+
*(.rel.*)
28+
}
29+
30+
SECTION_PROLOGUE(.rela.dyn,,)
31+
{
32+
*(.rela.*)
33+
}

soc/xtensa/D_108mini/linker.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ PROVIDE(_memmap_cacheattr_reset = _memmap_cacheattr_wb_trapnull);
178178
SECTIONS
179179
{
180180

181+
#include <linker/rel-sections.ld>
182+
181183
.dram1.rodata : ALIGN(4)
182184
{
183185
_dram1_rodata_start = ABSOLUTE(.);
@@ -563,6 +565,7 @@ SECTIONS
563565
} >sram0_seg :sram0_bss_phdr
564566
__stack = 0x64000000;
565567
_heap_sentry = 0x64000000;
568+
.comment 0 : { *(.comment) }
566569
.debug 0 : { *(.debug) }
567570
.line 0 : { *(.line) }
568571
.debug_srcinfo 0 : { *(.debug_srcinfo) }
@@ -580,6 +583,8 @@ SECTIONS
580583
.debug_funcnames 0 : { *(.debug_funcnames) }
581584
.debug_typenames 0 : { *(.debug_typenames) }
582585
.debug_varnames 0 : { *(.debug_varnames) }
586+
.debug_ranges 0 : { *(.debug_ranges) }
587+
.xtensa.info 0 : { *(.xtensa.info) }
583588
.xt.insn 0 :
584589
{
585590
KEEP (*(.xt.insn))

soc/xtensa/D_212GP/linker.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ PROVIDE(_memmap_cacheattr_reset = _memmap_cacheattr_wb_trapnull);
178178
SECTIONS
179179
{
180180

181+
#include <linker/rel-sections.ld>
182+
181183
.dport0.rodata : ALIGN(4)
182184
{
183185
_dport0_rodata_start = ABSOLUTE(.);
@@ -569,6 +571,7 @@ SECTIONS
569571
} >sram19_seg :sram19_bss_phdr
570572
__stack = 0x64000000;
571573
_heap_sentry = 0x64000000;
574+
.comment 0 : { *(.comment) }
572575
.debug 0 : { *(.debug) }
573576
.line 0 : { *(.line) }
574577
.debug_srcinfo 0 : { *(.debug_srcinfo) }
@@ -586,6 +589,8 @@ SECTIONS
586589
.debug_funcnames 0 : { *(.debug_funcnames) }
587590
.debug_typenames 0 : { *(.debug_typenames) }
588591
.debug_varnames 0 : { *(.debug_varnames) }
592+
.debug_ranges 0 : { *(.debug_ranges) }
593+
.xtensa.info 0 : { *(.xtensa.info) }
589594
.xt.insn 0 :
590595
{
591596
KEEP (*(.xt.insn))

0 commit comments

Comments
 (0)