Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,22 @@
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_error_implicit_int>>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_error_implicit_int>>)

if (NOT DEFINED CONFIG_PIC_OPTIONS)

Check failure on line 533 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (paren-space)

CMakeLists.txt:533 remove the space before '(' in 'if' See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.

Check failure on line 533 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (paren-space)

CMakeLists.txt:533 remove the space before '(' in 'if' See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.
# @Intent: Do not make position independent code / executable
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
endif()


if (CONFIG_PIC_OPTIONS)

Check failure on line 540 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (paren-space)

CMakeLists.txt:540 remove the space before '(' in 'if' See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.

Check failure on line 540 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (paren-space)

CMakeLists.txt:540 remove the space before '(' in 'if' See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.
zephyr_link_libraries($<TARGET_PROPERTY:linker,position_independent_elf>)

Check failure on line 541 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (indent)

CMakeLists.txt:541 expected 2 spaces of indentation, found 0 See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.

Check failure on line 541 in CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (indent)

CMakeLists.txt:541 expected 2 spaces of indentation, found 0 See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.
else()
# In case of CONFIG_NATIVE_LIBRARY we also don't want position independent code,
# but we cannot tell that to the linker yet as we are first only doing a
# relocatable link into a static library
zephyr_link_libraries_ifndef(CONFIG_NATIVE_LIBRARY
$<TARGET_PROPERTY:linker,no_position_independent>)
endif()

if(CONFIG_INSTRUMENTATION)
# @Intent: Enable function instrumentation injection at compile time
Expand Down
7 changes: 7 additions & 0 deletions Kconfig.zephyr
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ config LTO_SINGLE_THREADED
LTO. See the following issue for more info:
https://github.com/zephyrproject-rtos/sdk-ng/issues/1038

config PIC_OPTIONS
bool "Generate Position Independent ELF"
help
This option enables Position Independent ELF. Requires a CPULLVM toolchain
(github.com/qualcomm/cpullvm-toolchain) due to position independent
executable compilation ("-pie") not being supported.

config COMPILER_WARNINGS_AS_ERRORS
bool "Treat warnings as errors"
help
Expand Down
3 changes: 3 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ config ISR_TABLES_LOCAL_DECLARATION
The most important advantage of such approach is that the generated interrupt tables
are LTO compatible.
The drawback is that the support on the architecture port is required.
If position independent executable ("-pie") is enabled this is not supported.

config DYNAMIC_INTERRUPTS
bool "Installation of IRQs at runtime"
Expand Down Expand Up @@ -719,11 +720,13 @@ config GEN_SW_ISR_TABLE_ARRAY

config GEN_SW_ISR_TABLE_SWITCH
bool "Generate a function with a switch-case"
depends on !PIC_OPTIONS
help
Use a switch-case instead of an array.
This helps to limit binary size when most of the IRQs are not used.
In such case common, duplicated entries are grouped in a single default block
while few used interrupts have their own case blocks.
If position independent executable ("-pie") is enabled this is not supported.

endchoice

Expand Down
2 changes: 1 addition & 1 deletion arch/common/isr_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const uintptr_t __irq_vector_table _irq_vector_table[IRQ_TABLE_SIZE] = {
* type and bypass the _sw_isr_table, then do not generate one.
*/
#if defined(CONFIG_GEN_SW_ISR_TABLE_ARRAY)
#ifndef CONFIG_DYNAMIC_INTERRUPTS
#if !defined(CONFIG_DYNAMIC_INTERRUPTS) && !defined(CONFIG_PIC_OPTIONS)
const
#endif /* CONFIG_DYNAMIC_INTERRUPTS */
struct _isr_table_entry __sw_isr_table _sw_isr_table[IRQ_TABLE_SIZE] = {
Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/core/isr.S
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ SECTION_FUNC(exception.entry, _isr_wrapper)
#endif /* CONFIG_RISCV_ISA_EXT_C */

is_fp: /* Process the FP trap and quickly return from exception */
la ra, fp_trap_exit
lla ra, fp_trap_exit
mv a0, sp
tail z_riscv_fpu_trap
2:
Expand Down Expand Up @@ -489,7 +489,7 @@ no_fp: /* increment _current->arch.exception_depth */

j no_reschedule
#else
la ra, no_reschedule
lla ra, no_reschedule
tail z_riscv_fault
#endif /* CONFIG_EXCEPTION_DEBUG */

Expand Down
6 changes: 3 additions & 3 deletions arch/riscv/core/pmp.S
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
GTEXT(z_riscv_write_pmp_entries)
SECTION_FUNC(TEXT, z_riscv_write_pmp_entries)

la t0, pmpaddr_store
lla t0, pmpaddr_store
slli t1, a0, 4 /* 16-byte instruction blocks */
add t0, t0, t1
jr t0
Expand All @@ -59,7 +59,7 @@ pmpaddr_done:
* a0 = a0 / RV_REGSIZE
* a1 = (a1 + RV_REGSIZE - 1) / RV_REGSIZE
*/
la t0, pmpcfg_store
lla t0, pmpcfg_store
srli a0, a0, RV_REGSHIFT
slli t1, a0, 4 /* 16-byte instruction blocks */
add t0, t0, t1
Expand All @@ -85,7 +85,7 @@ pmpcfg_done:

beqz a2, done

la t0, pmpcfg_zerotail
lla t0, pmpcfg_zerotail
slli a0, a0, 2 /* 4-byte instruction blocks */
add t0, t0, a0
jr t0
Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/core/pmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@
(COND_CODE_1(CONFIG_PMP_UNLOCK_ROM_FOR_DEBUG, (0x0),
(PMP_L)))),
(uintptr_t)__rom_region_start,
(size_t)__rom_region_size,
(size_t)((uintptr_t)__rom_region_end - (uintptr_t)__rom_region_start),
pmp_addr, pmp_cfg, ARRAY_SIZE(pmp_addr));

/* SoC-specific PMP regions defined via iterable sections */
Expand Down Expand Up @@ -1396,7 +1396,7 @@
/* Check if this is within the global read-only area */
if (!write) {
uintptr_t ro_start = (uintptr_t)__rom_region_start;
size_t ro_size = (size_t)__rom_region_size;
size_t ro_size = (size_t)((uintptr_t)__rom_region_end - (uintptr_t)__rom_region_start);

Check warning on line 1399 in arch/riscv/core/pmp.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

arch/riscv/core/pmp.c:1399 line length of 103 exceeds 100 columns See https://docs.zephyrproject.org/latest/contribute/guidelines.html#coding-style for more details.

Check warning on line 1399 in arch/riscv/core/pmp.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

arch/riscv/core/pmp.c:1399 line length of 103 exceeds 100 columns See https://docs.zephyrproject.org/latest/contribute/guidelines.html#coding-style for more details.

if (IS_WITHIN(start, size, ro_start, ro_size)) {
return 0;
Expand Down
6 changes: 5 additions & 1 deletion cmake/compiler/clang/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@
set(target_flag "--target=${CMAKE_C_COMPILER_TARGET}")
endif()

if(DEFINED CONFIG_PIC_OPTIONS)
set(pic_flag "-fPIC")

Check failure on line 149 in cmake/compiler/clang/target.cmake

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (indent)

cmake/compiler/clang/target.cmake:149 expected 4 spaces of indentation, found 6 See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.

Check failure on line 149 in cmake/compiler/clang/target.cmake

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CMakeStyle (indent)

cmake/compiler/clang/target.cmake:149 expected 4 spaces of indentation, found 6 See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details.
endif()

execute_process(
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${COMPILER_OPTIMIZATION_FLAG} ${simple_options}
${target_flag}
${target_flag} ${pic_flag}
--print-libgcc-file-name
OUTPUT_VARIABLE library_path
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand Down
2 changes: 2 additions & 0 deletions cmake/linker/eld/linker_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ list(APPEND LLEXT_APPEND_FLAGS -fuse-ld=eld)

set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},-no-pie")

set_property(TARGET linker PROPERTY position_independent_elf "${LINKERFLAGPREFIX},-pie ${LINKERFLAGPREFIX},--no-dynamic-linker")

# ld sets gcc-specific flags, so clear this out.
set_property(TARGET linker PROPERTY lto_arguments)
set_property(TARGET linker PROPERTY lto_arguments_st)
3 changes: 3 additions & 0 deletions cmake/linker/ld/clang/linker_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ elseif(CONFIG_COVERAGE_NATIVE_SOURCE)
set_property(TARGET linker PROPERTY coverage -fprofile-instr-generate -fcoverage-mapping)
endif()

# Position Independent Executable (PIE) and no dynamic linker
set_property(TARGET linker PROPERTY position_independent_elf "${LINKERFLAGPREFIX},-pie ${LINKERFLAGPREFIX},--no-dynamic-linker")

# Extra warnings options for twister run
set_property(TARGET linker PROPERTY ld_extra_warning_options ${LINKERFLAGPREFIX},--fatal-warnings)

Expand Down
2 changes: 2 additions & 0 deletions cmake/linker/ld/linker_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ else()
set_property(TARGET linker PROPERTY no_position_independent)
endif()

set_property(TARGET linker PROPERTY position_independent_elf "${LINKERFLAGPREFIX},-pie ${LINKERFLAGPREFIX},--no-dynamic-linker")

set_property(TARGET linker PROPERTY partial_linking "-r")

set_property(TARGET linker PROPERTY lto_arguments -flto=auto -fno-ipa-sra -ffunction-sections -fdata-sections)
Expand Down
2 changes: 2 additions & 0 deletions cmake/linker/lld/linker_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ endif()

set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},--no-pie")

set_property(TARGET linker PROPERTY position_independent_elf "${LINKERFLAGPREFIX},-pie ${LINKERFLAGPREFIX},--no-dynamic-linker")

set_property(TARGET linker PROPERTY lto_arguments)
set_property(TARGET linker PROPERTY lto_arguments_st)

Expand Down
32 changes: 31 additions & 1 deletion include/zephyr/arch/riscv/common/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,52 @@ ENTRY(CONFIG_KERNEL_ENTRY)
SECTIONS
{

#ifndef CONFIG_PIC_OPTIONS
#include <zephyr/linker/rel-sections.ld>
#endif
#include <zephyr/linker/llext-sections.ld>

/*
* The .plt and .iplt are here according to
* 'riscv32-zephyr-elf-ld --verbose', before text section.
*/
SECTION_PROLOGUE(.plt,,)
#ifndef CONFIG_PIC_OPTIONS
SECTION_PROLOGUE(.plt,,ALIGN(0x1000))
{
*(.plt)
*(.got)
*(.got.plt)
}

SECTION_PROLOGUE(.iplt,,)
{
*(.iplt)
}
#else
SECTION_PROLOGUE(.got,, ALIGN(0x1000))
{
__got_start = .;
*(.got.plt) *(.got)
__got_end = .;
}

SECTION_PROLOGUE(.rela.plt,, ALIGN(0x1000))
{
__rel_plt_start = .;
*(.rela.plt*)
__rel_plt_end = .;
*(.plt)
}

SECTION_PROLOGUE(.rela.dyn,, ALIGN(0x1000))
{
__rel_dyn_start = .;
KEEP(*(.rela*))
}
__rel_dyn_end = .;
Comment on lines +170 to +175

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For whatever reason moving __rel_dyn_end inside the .rela.dyn section results in errors.

In our downstream it was left commented out:

Image


/DISCARD/ : { *(.dynamic*) }
#endif

GROUP_START(ROMABLE_REGION)
__rom_region_start = ROM_BASE;
Expand Down
7 changes: 6 additions & 1 deletion include/zephyr/sw_isr_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
/* Spurious interrupt handler. Throws an error if called */
void z_irq_spurious(const void *unused);

struct _isr_reloc_param_info {
int table_index;
int shared_index;
};

/*
* Note the order: arg first, then ISR. This allows a table entry to be
* loaded arg -> r0, isr -> r3 in _isr_wrapper with one ldmia instruction,
Expand All @@ -46,7 +51,7 @@
* irq line
*/
extern
#ifndef CONFIG_DYNAMIC_INTERRUPTS
#if !defined(CONFIG_DYNAMIC_INTERRUPTS) && !defined(CONFIG_PIC_OPTIONS)

Check failure on line 54 in include/zephyr/sw_isr_table.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

include/zephyr/sw_isr_table.h:54 trailing whitespace See https://docs.zephyrproject.org/latest/contribute/guidelines.html#coding-style for more details.

Check failure on line 54 in include/zephyr/sw_isr_table.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

include/zephyr/sw_isr_table.h:54 trailing whitespace See https://docs.zephyrproject.org/latest/contribute/guidelines.html#coding-style for more details.
const
#endif /* CONFIG_DYNAMIC_INTERRUPTS */
struct _isr_table_entry _sw_isr_table[];
Expand Down
18 changes: 12 additions & 6 deletions kernel/include/kernel_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*/
static inline FUNC_NO_STACK_PROTECTOR size_t z_tls_data_size(void)
{
size_t size = (size_t)(uintptr_t)__tdata_size + (size_t)(uintptr_t)__tbss_size;
const uintptr_t tdata_size = (uintptr_t)__tdata_end - (uintptr_t)__tdata_start;
const uintptr_t tbss_size = (uintptr_t)__tbss_end - (uintptr_t)__tbss_start;

size_t size = (size_t)tdata_size + (size_t)tbss_size;

#if defined(CONFIG_STACK_CANARIES_TLS_PREPEND)
size += (size_t)(uintptr_t)__stack_chk_size;
Expand All @@ -46,22 +49,25 @@ static inline FUNC_NO_STACK_PROTECTOR size_t z_tls_data_size(void)
*/
static inline FUNC_NO_STACK_PROTECTOR void z_tls_copy(char *dest)
{
const uintptr_t tdata_size = (uintptr_t)__tdata_end - (uintptr_t)__tdata_start;
const uintptr_t tbss_size = (uintptr_t)__tbss_end - (uintptr_t)__tbss_start;

#if defined(CONFIG_STACK_CANARIES_TLS_PREPEND)
/* Copy .stack_chk.guard and .tdata separately since the linker may insert
* padding between sections.
*/
memcpy(dest, __stack_chk_start, (size_t)(uintptr_t)__stack_chk_size);
dest += (size_t)(uintptr_t)__stack_chk_size;

memcpy(dest, __tdata_start, (size_t)(uintptr_t)__tdata_size);
dest += (size_t)(uintptr_t)__tdata_size;
memcpy(dest, __tdata_start, (size_t)tdata_size);
dest += (size_t)tdata_size;
#else
/* Copy initialized data (tdata) */
memcpy(dest, __tdata_start, (size_t)(uintptr_t)__tdata_size);
dest += (size_t)(uintptr_t)__tdata_size;
memcpy(dest, __tdata_start, (size_t)tdata_size);
dest += (size_t)tdata_size;
#endif
/* Clear BSS data (tbss) */
memset(dest, 0, (size_t)(uintptr_t)__tbss_size);
memset(dest, 0, (size_t)tbss_size);
}

#endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_TLS_H_ */
Loading
Loading