diff --git a/arch/common/CMakeLists.txt b/arch/common/CMakeLists.txt index 401cc82148c68b..21ddbd24ea4926 100644 --- a/arch/common/CMakeLists.txt +++ b/arch/common/CMakeLists.txt @@ -52,6 +52,9 @@ zephyr_linker_sources_ifdef(CONFIG_NOCACHE_MEMORY # Only ARM, X86 and OPENISA_RV32M1_RISCV32 use ROM_START_OFFSET. if (DEFINED CONFIG_ARM OR DEFINED CONFIG_X86 OR DEFINED CONFIG_ARM64 OR DEFINED CONFIG_SOC_OPENISA_RV32M1_RISCV32) + # Exclamation mark is printable character with lowest number in ASCII table. + # We are sure that this file will be included as a first. + zephyr_linker_sources(ROM_START SORT_KEY ! rom_start_address.ld) zephyr_linker_sources(ROM_START SORT_KEY 0x0 rom_start_offset.ld) # Handled in ld.cmake endif() diff --git a/arch/common/rom_start_address.ld b/arch/common/rom_start_address.ld new file mode 100644 index 00000000000000..afebcb606fecba --- /dev/null +++ b/arch/common/rom_start_address.ld @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2023, Google, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * To provide correct value, this file must be the first file included in + * snippets-rom-start.ld. This variable is used in rom_start_offset.ld + */ +HIDDEN(__rom_start_address = .); diff --git a/arch/common/rom_start_offset.ld b/arch/common/rom_start_offset.ld index 2e82f30d71886e..e141a162b295ce 100644 --- a/arch/common/rom_start_offset.ld +++ b/arch/common/rom_start_offset.ld @@ -4,5 +4,18 @@ * SPDX-License-Identifier: Apache-2.0 */ -. = CONFIG_ROM_START_OFFSET; +/* + * The line below this comment is equivalent to '. = CONFIG_ROM_START_OFFSET' + * as interpreted by GNU LD, but also compatible with LLVM LLD. + * + * Simple assignment doesn't work for LLVM LLD, because the dot inside section + * is absolute, so assigning offset here results in moving location counter + * backwards. + * + * We can't use '. += CONFIG_ROM_START_OFFSET' here because there might be some + * other files included before this file. + * + * Symbol __rom_start_address is defined in rom_start_address.ld + */ +. += CONFIG_ROM_START_OFFSET - (. - __rom_start_address); . = ALIGN(4);