linker.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. set(COMMON_ZEPHYR_LINKER_DIR ${ZEPHYR_BASE}/cmake/linker_script/common)
  2. set_ifndef(region_min_align CONFIG_CUSTOM_SECTION_MIN_ALIGN_SIZE)
  3. # Set alignment to CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE if not set above
  4. # to make linker section alignment comply with MPU granularity.
  5. set_ifndef(region_min_align CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE)
  6. # If building without MPU support, use default 4-byte alignment.. if not set abve.
  7. set_ifndef(region_min_align 4)
  8. # Note, the `+ 0` in formulas below avoids errors in cases where a Kconfig
  9. # variable is undefined and thus expands to nothing.
  10. math(EXPR FLASH_ADDR
  11. "${CONFIG_FLASH_BASE_ADDRESS} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
  12. OUTPUT_FORMAT HEXADECIMAL
  13. )
  14. math(EXPR FLASH_SIZE
  15. "(${CONFIG_FLASH_SIZE} + 0) * 1024 - (${CONFIG_FLASH_LOAD_OFFSET} + 0)"
  16. OUTPUT_FORMAT HEXADECIMAL
  17. )
  18. set(RAM_ADDR ${CONFIG_SRAM_BASE_ADDRESS})
  19. math(EXPR RAM_SIZE "(${CONFIG_SRAM_SIZE} + 0) * 1024" OUTPUT_FORMAT HEXADECIMAL)
  20. math(EXPR IDT_ADDR "${RAM_ADDR} + ${RAM_SIZE}" OUTPUT_FORMAT HEXADECIMAL)
  21. # ToDo: decide on the optimal location for this.
  22. # linker/ld/target.cmake based on arch, or directly in arch and scatter_script.cmake can ignore
  23. zephyr_linker(FORMAT "elf32-littlearm")
  24. zephyr_linker(ENTRY ${CONFIG_KERNEL_ENTRY})
  25. zephyr_linker_memory(NAME FLASH FLAGS rx START ${FLASH_ADDR} SIZE ${FLASH_SIZE})
  26. zephyr_linker_memory(NAME RAM FLAGS wx START ${RAM_ADDR} SIZE ${RAM_SIZE})
  27. zephyr_linker_memory(NAME IDT_LIST FLAGS wx START ${IDT_ADDR} SIZE 2K)
  28. # TI CCFG Registers
  29. #zephyr_linker_dts_memory(NAME FLASH_CCFG FLAGS rwx NODELABEL ti_ccfg_partition)
  30. # Data & Instruction Tightly Coupled Memory
  31. #zephyr_linker_dts_memory(NAME ITCM FLAGS rw CHOSEN "zephyr,itcm")
  32. #zephyr_linker_dts_memory(NAME DTCM FLAGS rw CHOSEN "zephyr,dtcm")
  33. #zephyr_linker_dts_memory(NAME SRAM1 FLAGS rw NODELABEL sram1)
  34. #zephyr_linker_dts_memory(NAME SRAM2 FLAGS rw NODELABEL sram2)
  35. #zephyr_linker_dts_memory(NAME SRAM3 FLAGS rw NODELABEL sram3)
  36. #zephyr_linker_dts_memory(NAME SRAM4 FLAGS rw NODELABEL sram4)
  37. #zephyr_linker_dts_memory(NAME SDRAM1 FLAGS rw NODELABEL sdram1)
  38. #zephyr_linker_dts_memory(NAME SDRAM2 FLAGS rw NODELABEL sdram2)
  39. #zephyr_linker_dts_memory(NAME BACKUP_SRAM FLAGS rw NODELABEL backup_sram)
  40. if(CONFIG_XIP)
  41. zephyr_linker_group(NAME ROM_REGION LMA FLASH)
  42. set(rom_start ${FLASH_ADDR})
  43. set(XIP_ALIGN_WITH_INPUT ALIGN_WITH_INPUT)
  44. else()
  45. zephyr_linker_group(NAME ROM_REGION LMA RAM)
  46. set(rom_start ${RAM_ADDR})
  47. endif()
  48. zephyr_linker_group(NAME RAM_REGION VMA RAM LMA ROM_REGION)
  49. zephyr_linker_group(NAME TEXT_REGION GROUP ROM_REGION SYMBOL SECTION)
  50. zephyr_linker_group(NAME RODATA_REGION GROUP ROM_REGION)
  51. zephyr_linker_group(NAME DATA_REGION GROUP RAM_REGION SYMBOL SECTION)
  52. # should go to a relocation.cmake - from include/linker/rel-sections.ld - start
  53. zephyr_linker_section(NAME .rel.plt HIDDEN)
  54. zephyr_linker_section(NAME .rela.plt HIDDEN)
  55. zephyr_linker_section(NAME .rel.dyn)
  56. zephyr_linker_section(NAME .rela.dyn)
  57. # should go to a relocation.cmake - from include/linker/rel-sections.ld - end
  58. # Discard sections for GNU ld.
  59. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".plt")
  60. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".iplt")
  61. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".got.plt")
  62. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".igot.plt")
  63. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".got")
  64. zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".igot")
  65. zephyr_linker_section(NAME .rom_start ADDRESS ${rom_start} GROUP ROM_REGION NOINPUT)
  66. zephyr_linker_section(NAME .text GROUP TEXT_REGION)
  67. zephyr_linker_section_configure(SECTION .rel.plt INPUT ".rel.iplt")
  68. zephyr_linker_section_configure(SECTION .rela.plt INPUT ".rela.iplt")
  69. zephyr_linker_section_configure(SECTION .text INPUT ".TEXT.*")
  70. zephyr_linker_section_configure(SECTION .text INPUT ".gnu.linkonce.t.*")
  71. zephyr_linker_section_configure(SECTION .text INPUT ".glue_7t")
  72. zephyr_linker_section_configure(SECTION .text INPUT ".glue_7")
  73. zephyr_linker_section_configure(SECTION .text INPUT ".vfp11_veneer")
  74. zephyr_linker_section_configure(SECTION .text INPUT ".v4_bx")
  75. if(CONFIG_CPLUSPLUS)
  76. zephyr_linker_section(NAME .ARM.extab GROUP ROM_REGION)
  77. zephyr_linker_section_configure(SECTION .ARM.extab INPUT ".gnu.linkonce.armextab.*")
  78. endif()
  79. zephyr_linker_section(NAME .ARM.exidx GROUP ROM_REGION)
  80. # Here the original linker would check for __GCC_LINKER_CMD__, need to check toolchain linker ?
  81. #if(__GCC_LINKER_CMD__)
  82. zephyr_linker_section_configure(SECTION .ARM.exidx INPUT ".gnu.linkonce.armexidx.*")
  83. #endif()
  84. include(${COMMON_ZEPHYR_LINKER_DIR}/common-rom.cmake)
  85. include(${COMMON_ZEPHYR_LINKER_DIR}/thread-local-storage.cmake)
  86. zephyr_linker_section(NAME .rodata GROUP RODATA_REGION)
  87. zephyr_linker_section_configure(SECTION .rodata INPUT ".gnu.linkonce.r.*")
  88. if(CONFIG_USERSPACE AND CONFIG_XIP)
  89. zephyr_linker_section_configure(SECTION .rodata INPUT ".kobject_data.rodata*")
  90. endif()
  91. zephyr_linker_section_configure(SECTION .rodata ALIGN 4)
  92. # ToDo - . = ALIGN(_region_min_align);
  93. # Symbol to add _image_ram_start = .;
  94. # This comes from ramfunc.ls, via snippets-ram-sections.ld
  95. zephyr_linker_section(NAME .ramfunc GROUP RAM_REGION SUBALIGN 8)
  96. # Todo: handle MPU_ALIGN(_ramfunc_size);
  97. # ToDo - handle if(CONFIG_USERSPACE)
  98. zephyr_linker_section(NAME .data GROUP DATA_REGION)
  99. zephyr_linker_section_configure(SECTION .data INPUT ".kernel.*")
  100. include(${COMMON_ZEPHYR_LINKER_DIR}/common-ram.cmake)
  101. #include(kobject.ld)
  102. if(NOT CONFIG_USERSPACE)
  103. zephyr_linker_section(NAME .bss VMA RAM LMA FLASH TYPE BSS)
  104. zephyr_linker_section_configure(SECTION .bss INPUT COMMON)
  105. zephyr_linker_section_configure(SECTION .bss INPUT ".kernel_bss.*")
  106. # As memory is cleared in words only, it is simpler to ensure the BSS
  107. # section ends on a 4 byte boundary. This wastes a maximum of 3 bytes.
  108. zephyr_linker_section_configure(SECTION .bss ALIGN 4)
  109. zephyr_linker_section(NAME .noinit GROUP RAM_REGION TYPE NOLOAD NOINIT)
  110. # This section is used for non-initialized objects that
  111. # will not be cleared during the boot process.
  112. zephyr_linker_section_configure(SECTION .noinit INPUT ".kernel_noinit.*")
  113. endif()
  114. zephyr_linker_symbol(SYMBOL __kernel_ram_start EXPR "(%__bss_start%)")
  115. zephyr_linker_symbol(SYMBOL __kernel_ram_end EXPR "(${RAM_ADDR} + ${RAM_SIZE})")
  116. zephyr_linker_symbol(SYMBOL __kernel_ram_size EXPR "(%__kernel_ram_end% - %__bss_start%)")
  117. zephyr_linker_symbol(SYMBOL _image_ram_start EXPR "(${RAM_ADDR})" SUBALIGN 32) # ToDo calculate 32 correctly
  118. zephyr_linker_symbol(SYMBOL ARM_LIB_STACKHEAP EXPR "(${RAM_ADDR} + ${RAM_SIZE})" SIZE -0x1000)
  119. set(VECTOR_ALIGN 4)
  120. if(CONFIG_CPU_CORTEX_M_HAS_VTOR)
  121. math(EXPR VECTOR_ALIGN "4 * (16 + ${CONFIG_NUM_IRQS})")
  122. if(${VECTOR_ALIGN} LESS 128)
  123. set(VECTOR_ALIGN 128)
  124. else()
  125. pow2round(VECTOR_ALIGN)
  126. endif()
  127. endif()
  128. zephyr_linker_section_configure(
  129. SECTION .rom_start
  130. INPUT ".exc_vector_table*"
  131. ".gnu.linkonce.irq_vector_table*"
  132. ".vectors"
  133. OFFSET ${CONFIG_ROM_START_OFFSET}
  134. KEEP FIRST
  135. SYMBOLS _vector_start _vector_end
  136. ALIGN ${VECTOR_ALIGN}
  137. PRIO 50
  138. )
  139. #dt_chosen(chosen_itcm PROPERTY "zephyr,itcm")
  140. #if(DEFINED chosen_itcm)
  141. # dt_node_has_status(status_result PATH ${chosen_itcm} STATUS okay)
  142. # if(${status_result})
  143. # zephyr_linker_group(NAME ITCM_REGION VMA ITCM LMA ROM_REGION)
  144. # zephyr_linker_section(NAME .itcm GROUP ITCM_REGION SUBALIGN 4)
  145. # endif()
  146. #endif()
  147. #dt_chosen(chosen_dtcm PROPERTY "zephyr,dtcm")
  148. #if(DEFINED chosen_dtcm)
  149. # dt_node_has_status(status_result PATH ${chosen_dtcm} STATUS okay)
  150. # if(${status_result})
  151. # zephyr_linker_group(NAME DTCM_REGION VMA DTCM LMA ROM_REGION)
  152. # zephyr_linker_section(NAME .dtcm_bss GROUP DTCM_REGION SUBALIGN 4 TYPE BSS)
  153. # zephyr_linker_section(NAME .dtcm_noinit GROUP DTCM_REGION SUBALIGN 4 TYPE NOLOAD NOINIT)
  154. # zephyr_linker_section(NAME .dtcm_data GROUP DTCM_REGION SUBALIGN 4)
  155. # endif()
  156. #endif()
  157. zephyr_linker_section(NAME .ARM.attributes ADDRESS 0 NOINPUT)
  158. zephyr_linker_section_configure(SECTION .ARM.attributes INPUT ".ARM.attributes" KEEP)
  159. zephyr_linker_section_configure(SECTION .ARM.attributes INPUT ".gnu.attributes" KEEP)
  160. # armlink specific flags
  161. zephyr_linker_section_configure(SECTION .text ANY FLAGS "+RO" "+XO")
  162. zephyr_linker_section_configure(SECTION .data ANY FLAGS "+RW")
  163. zephyr_linker_section_configure(SECTION .bss ANY FLAGS "+ZI")
  164. include(${COMMON_ZEPHYR_LINKER_DIR}/debug-sections.cmake)