mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-10-27 11:58:55 +00:00 
			
		
		
		
	 019dc93ae0
			
		
	
	019dc93ae0
	
	
	
		
			
			Initially, ESP-IDF used the do_global_ctors() function to run global
constructors. This was done to accommodate Xtensa targets that emit
.ctors.* sections, which are ordered in descending order.
For RISC-V, compilation used .init_array.* sections, which are designed
to have ascending order. Priority constructors in .init_array.* sections
were correctly processed in ascending order. However, non-priority
.init_array section was processed in descending order, as it was done
for Xtensa .ctors.
Starting with ESP-IDF v6.0, the implementation switched to the standard
LibC behavior (__libc_init_array()), which processes both priority and
non-priority constructors in ascending order.
To achieve this, a breaking changes were introduced:
  - Xtensa .ctors.* priority entries converted to .init_array.* format
    (ascending), to be passed to __libc_init_array().
  - Processing order of non-priority .init_array and .ctors sections was
    changed from descending to ascending.
Also, this change introduces .preinit_array for linking. This may be
needed for some C++ or sanitizer features.
Related to https://github.com/espressif/esp-idf/issues/15529
		
	
		
			
				
	
	
		
			370 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			370 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| /*
 | |
|  * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
 | |
|  *
 | |
|  * SPDX-License-Identifier: Apache-2.0
 | |
|  */
 | |
| 
 | |
| #include "ld.common"
 | |
| 
 | |
| /* Default entry point */
 | |
| ENTRY(call_start_cpu0);
 | |
| 
 | |
| SECTIONS
 | |
| {
 | |
|   .iram0.text :
 | |
|   {
 | |
|     _iram_start = ABSOLUTE(.);
 | |
|     /* Vectors go to start of IRAM */
 | |
|     ASSERT(ABSOLUTE(.) % 0x100 == 0, "vector address must be 256 byte aligned");
 | |
|     KEEP(*(.exception_vectors_table.text));
 | |
|     KEEP(*(.exception_vectors.text));
 | |
| 
 | |
|     /* Code marked as running out of IRAM */
 | |
|     _iram_text_start = ABSOLUTE(.);
 | |
| 
 | |
|     mapping[iram0_text]
 | |
| 
 | |
|   } > sram_seg
 | |
| 
 | |
|   /* Marks the end of IRAM code segment */
 | |
|   .iram0.text_end (NOLOAD) :
 | |
|   {
 | |
|     /* Align the end of code region as per PMP region granularity */
 | |
|     . = ALIGN(_esp_pmp_align_size);
 | |
| 
 | |
|     ALIGNED_SYMBOL(4, _iram_text_end)
 | |
|   } > sram_seg
 | |
| 
 | |
|   .iram0.data :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(16, _iram_data_start)
 | |
| 
 | |
|     mapping[iram0_data]
 | |
| 
 | |
|     _iram_data_end = ABSOLUTE(.);
 | |
|   } > sram_seg
 | |
| 
 | |
|   .iram0.bss (NOLOAD) :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(16, _iram_bss_start)
 | |
| 
 | |
|     mapping[iram0_bss]
 | |
| 
 | |
|     _iram_bss_end = ABSOLUTE(.);
 | |
|     ALIGNED_SYMBOL(16, _iram_end)
 | |
|   } > sram_seg
 | |
| 
 | |
|   /**
 | |
|    * This section is required to skip .iram0.text area because sram_seg and
 | |
|    * sram_seg reflect the same address space on different buses.
 | |
|    */
 | |
|   .dram0.dummy (NOLOAD):
 | |
|   {
 | |
|     . = ORIGIN(sram_seg) + _iram_end - _iram_start;
 | |
|   } > sram_seg
 | |
| 
 | |
|   .dram0.data :
 | |
|   {
 | |
|     _data_start = ABSOLUTE(.);
 | |
|     *(.gnu.linkonce.d.*)
 | |
|     *(.data1)
 | |
|     __global_pointer$ = . + 0x800;
 | |
|     *(.sdata)
 | |
|     *(.sdata.*)
 | |
|     *(.gnu.linkonce.s.*)
 | |
|     *(.gnu.linkonce.s2.*)
 | |
|     *(.jcr)
 | |
| 
 | |
|     mapping[dram0_data]
 | |
| 
 | |
|     _data_end = ABSOLUTE(.);
 | |
|   } > sram_seg
 | |
| 
 | |
|   /**
 | |
|    * This section holds data that should not be initialized at power up.
 | |
|    * The section located in Internal SRAM memory region. The macro _NOINIT
 | |
|    * can be used as attribute to place data into this section.
 | |
|    * See the "esp_attr.h" file for more information.
 | |
|    */
 | |
|   .noinit (NOLOAD):
 | |
|   {
 | |
|     ALIGNED_SYMBOL(4, _noinit_start)
 | |
| 
 | |
|     *(.noinit .noinit.*)
 | |
| 
 | |
|     ALIGNED_SYMBOL(4, _noinit_end)
 | |
|   } > sram_seg
 | |
| 
 | |
|   /* Shared RAM */
 | |
|   .dram0.bss (NOLOAD) :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(8, _bss_start)
 | |
| 
 | |
|     /**
 | |
|      * ldgen places all bss-related data to mapping[dram0_bss]
 | |
|      * (See components/esp_system/app.lf).
 | |
|      */
 | |
|     mapping[dram0_bss]
 | |
| 
 | |
|     ALIGNED_SYMBOL(8, _bss_end)
 | |
|   } > sram_seg
 | |
| 
 | |
|   ASSERT(((_bss_end - ORIGIN(sram_seg)) <= LENGTH(sram_seg)), "DRAM segment data does not fit.")
 | |
| 
 | |
|   .flash.text :
 | |
|   {
 | |
|     _stext = .;
 | |
|     /**
 | |
|      * Mark the start of flash.text.
 | |
|      * This can be used by the MMU driver to maintain the virtual address.
 | |
|      */
 | |
|     _instruction_reserved_start = ABSOLUTE(.);
 | |
|     _text_start = ABSOLUTE(.);
 | |
| 
 | |
|     mapping[flash_text]
 | |
| 
 | |
|     *(.stub)
 | |
|     *(.gnu.linkonce.t.*)
 | |
|     *(.gnu.warning)
 | |
|     *(.irom0.text) /* catch stray ICACHE_RODATA_ATTR */
 | |
| 
 | |
|     /**
 | |
|      * CPU will try to prefetch up to 16 bytes of of instructions.
 | |
|      * This means that any configuration (e.g. MMU, PMS) must allow
 | |
|      * safe access to up to 16 bytes after the last real instruction, add
 | |
|      * dummy bytes to ensure this
 | |
|      */
 | |
|     . += _esp_flash_mmap_prefetch_pad_size;
 | |
| 
 | |
| #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
 | |
|     /* Align the end of flash text region as per PMP granularity to allow using the
 | |
|      * page alignment gap created while mapping the flash region into the PSRAM memory.
 | |
|      */
 | |
|     . = ALIGN(_esp_pmp_align_size);
 | |
| #endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
 | |
| 
 | |
|     _text_end = ABSOLUTE(.);
 | |
|     /**
 | |
|      * Mark the flash.text end.
 | |
|      * This can be used for MMU driver to maintain virtual address.
 | |
|      */
 | |
|     _instruction_reserved_end = ABSOLUTE(.);
 | |
|     _etext = .;
 | |
| 
 | |
|     /**
 | |
|      * Similar to _iram_start, this symbol goes here so it is
 | |
|      * resolved by addr2line in preference to the first symbol in
 | |
|      * the flash.text segment.
 | |
|      */
 | |
|     _flash_cache_start = ABSOLUTE(0);
 | |
|   } > default_code_seg
 | |
| 
 | |
|   /**
 | |
|    * Dummy section represents the .flash.text section but in default_rodata_seg.
 | |
|    * Thus, it must have its alignment and (at least) its size.
 | |
|    */
 | |
|   .flash_rodata_dummy (NOLOAD):
 | |
|   {
 | |
|     _flash_rodata_dummy_start = .;
 | |
| 
 | |
|     . = ALIGN(ALIGNOF(.flash.text)) + SIZEOF(.flash.text);
 | |
| 
 | |
|     /* Add alignment of MMU page size + 0x20 bytes for the mapping header. */
 | |
|     . = ALIGN(_esp_mmu_page_size) + 0x20;
 | |
|   } > default_rodata_seg
 | |
| 
 | |
|   .flash.appdesc : ALIGN(0x10)
 | |
|   {
 | |
|     /**
 | |
|      * Mark flash.rodata start.
 | |
|      * This can be used for mmu driver to maintain virtual address
 | |
|      */
 | |
|     _rodata_reserved_start = ABSOLUTE(.);
 | |
|     _rodata_start = ABSOLUTE(.);
 | |
| 
 | |
|     /* !DO NOT PUT ANYTHING BEFORE THIS! */
 | |
| 
 | |
|     /* Should be the first.  App version info. */
 | |
|     *(.rodata_desc .rodata_desc.*)
 | |
|     /* Should be the second. Custom app version info. */
 | |
|     *(.rodata_custom_desc .rodata_custom_desc.*)
 | |
| 
 | |
|     /**
 | |
|      * Create an empty gap within this section. Thanks to this, the end of this
 | |
|      * section will match .flash.rodata's begin address. Thus, both sections
 | |
|      * will be merged when creating the final bin image.
 | |
|      */
 | |
|     . = ALIGN(ALIGNOF(.flash.rodata));
 | |
|   } > default_rodata_seg
 | |
|   ASSERT_SECTIONS_GAP(.flash.appdesc, .flash.rodata)
 | |
| 
 | |
|   .flash.rodata : ALIGN(0x10)
 | |
|   {
 | |
|     _flash_rodata_start = ABSOLUTE(.);
 | |
| 
 | |
|     mapping[flash_rodata]
 | |
| 
 | |
|     *(.irom1.text) /* catch stray ICACHE_RODATA_ATTR */
 | |
|     *(.gnu.linkonce.r.*)
 | |
|     *(.rodata1)
 | |
|     *(.gcc_except_table .gcc_except_table.*)
 | |
|     *(.gnu.linkonce.e.*)
 | |
|     /**
 | |
|      * C++ constructor tables.
 | |
|      *
 | |
|      * Excluding crtbegin.o/crtend.o since IDF doesn't use the toolchain crt.
 | |
|      */
 | |
|     ALIGNED_SYMBOL(4, __preinit_array_start)
 | |
|     KEEP (*(.preinit_array))
 | |
|     __preinit_array_end = ABSOLUTE(.);
 | |
| 
 | |
|     ALIGNED_SYMBOL(4, __init_array_start)
 | |
|     KEEP (*(SORT_BY_INIT_PRIORITY(EXCLUDE_FILE (*crtend.* *crtbegin.*) .init_array.*)))
 | |
|     KEEP (*(EXCLUDE_FILE (*crtend.* *crtbegin.*) .init_array))
 | |
|     __init_array_end = ABSOLUTE(.);
 | |
| 
 | |
|     /* Addresses of memory regions reserved via SOC_RESERVE_MEMORY_REGION() */
 | |
|     ALIGNED_SYMBOL(4, soc_reserved_memory_region_start)
 | |
|     KEEP (*(.reserved_memory_address))
 | |
|     soc_reserved_memory_region_end = ABSOLUTE(.);
 | |
| 
 | |
|     /* System init functions registered via ESP_SYSTEM_INIT_FN */
 | |
|     ALIGNED_SYMBOL(4, _esp_system_init_fn_array_start)
 | |
|     KEEP (*(SORT_BY_INIT_PRIORITY(.esp_system_init_fn.*)))
 | |
|     _esp_system_init_fn_array_end = ABSOLUTE(.);
 | |
| 
 | |
|     _rodata_end = ABSOLUTE(.);
 | |
|     . = ALIGN(ALIGNOF(SECTION_AFTER_FLASH_RODATA));
 | |
|   } > default_rodata_seg
 | |
|   ASSERT_SECTIONS_GAP(.flash.rodata, SECTION_AFTER_FLASH_RODATA)
 | |
| 
 | |
| #if EH_FRAME_LINKING_ENABLED
 | |
|   .eh_frame_hdr :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(4, __eh_frame_hdr)
 | |
| 
 | |
|     KEEP (*(.eh_frame_hdr))
 | |
| 
 | |
|     __eh_frame_hdr_end = ABSOLUTE(.);
 | |
| 
 | |
|     . = ALIGN(ALIGNOF(.eh_frame));
 | |
|   } > default_rodata_seg
 | |
|   ASSERT_SECTIONS_GAP(.eh_frame_hdr, .eh_frame)
 | |
| 
 | |
|   .eh_frame :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(4, __eh_frame)
 | |
| 
 | |
|     KEEP (*(.eh_frame))
 | |
|     /**
 | |
|      * As we are not linking with crtend.o, which includes the CIE terminator
 | |
|      * (see __FRAME_END__ in libgcc sources), it is manually provided here.
 | |
|      */
 | |
|     LONG(0);
 | |
| 
 | |
|     __eh_frame_end = ABSOLUTE(.);
 | |
| 
 | |
|     . = ALIGN(ALIGNOF(.flash.tdata));
 | |
|   } > default_rodata_seg
 | |
|   ASSERT_SECTIONS_GAP(.eh_frame, .flash.tdata)
 | |
| #endif // EH_FRAME_LINKING_ENABLED
 | |
| 
 | |
|   .flash.tdata :
 | |
|   {
 | |
|     _thread_local_data_start = ABSOLUTE(.);
 | |
| 
 | |
|     *(.tdata .tdata.* .gnu.linkonce.td.*)
 | |
| 
 | |
|     . = ALIGN(ALIGNOF(.flash.tbss));
 | |
| 
 | |
| #if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
 | |
|     /* Align the end of flash rodata region as per PMP granularity to allow using the
 | |
|      * page alignment gap created while mapping the flash region into the PSRAM memory.
 | |
|      */
 | |
|     . = ALIGN(_esp_pmp_align_size);
 | |
| #endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
 | |
| 
 | |
|     _thread_local_data_end = ABSOLUTE(.);
 | |
|   } > default_rodata_seg
 | |
|   ASSERT_SECTIONS_GAP(.flash.tdata, .flash.tbss)
 | |
| 
 | |
|   .flash.tbss (NOLOAD) :
 | |
|   {
 | |
|     _thread_local_bss_start = ABSOLUTE(.);
 | |
| 
 | |
|     *(.tbss .tbss.* .gnu.linkonce.tb.*)
 | |
|     *(.tcommon .tcommon.*)
 | |
| 
 | |
|     _thread_local_bss_end = ABSOLUTE(.);
 | |
|   } > default_rodata_seg
 | |
| 
 | |
|   /**
 | |
|    * This section contains all the rodata that is not used
 | |
|    * at runtime, helping to avoid an increase in binary size.
 | |
|    */
 | |
|   .flash.rodata_noload (NOLOAD) :
 | |
|   {
 | |
|     /**
 | |
|      * This symbol marks the end of flash.rodata. It can be utilized by the MMU
 | |
|      * driver to maintain the virtual address.
 | |
|      * NOLOAD rodata may not be included in this section.
 | |
|      */
 | |
|     _rodata_reserved_end = ADDR(.flash.tbss);
 | |
| 
 | |
|     mapping[rodata_noload]
 | |
|   } > default_rodata_seg
 | |
| 
 | |
|   /* Marks the end of data, bss and possibly rodata */
 | |
|   .dram0.heap_start (NOLOAD) :
 | |
|   {
 | |
|     ALIGNED_SYMBOL(16, _heap_start)
 | |
|   } > sram_seg
 | |
| 
 | |
|   /* External RAM */
 | |
|   /**
 | |
|    * This section is required to skip flash sections, because `extern_ram_seg`
 | |
|    * and `drom_seg` / `irom_seg` are on the same bus when app build use flash sections
 | |
|    */
 | |
|   .ext_ram.dummy (NOLOAD):
 | |
|   {
 | |
|     . = ORIGIN(extern_ram_seg);
 | |
|     . = . + (_rodata_reserved_end - _flash_rodata_dummy_start);
 | |
|     . = ALIGN (_esp_mmu_page_size);
 | |
|   } > extern_ram_seg
 | |
| 
 | |
| #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
 | |
|   /* This section holds .ext_ram.bss data, and will be put in PSRAM */
 | |
|   .ext_ram.bss (NOLOAD) :
 | |
|   {
 | |
|     _ext_ram_bss_start = ABSOLUTE(.);
 | |
| 
 | |
|     mapping[extern_ram]
 | |
| 
 | |
|     ALIGNED_SYMBOL(4, _ext_ram_bss_end)
 | |
|   } > extern_ram_seg
 | |
| #endif  //CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
 | |
| 
 | |
| #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
 | |
|   /**
 | |
|    * This section holds data that won't be initialized when startup.
 | |
|    * This section locates in External RAM region.
 | |
|    */
 | |
|   .ext_ram_noinit (NOLOAD) :
 | |
|   {
 | |
|     _ext_ram_noinit_start = ABSOLUTE(.);
 | |
| 
 | |
|     *(.ext_ram_noinit*)
 | |
| 
 | |
|     ALIGNED_SYMBOL(4, _ext_ram_noinit_end)
 | |
|   } > extern_ram_seg
 | |
| #endif //CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
 | |
| 
 | |
| #include "elf_misc.ld.in"
 | |
| }
 | |
| 
 | |
| ASSERT(((_iram_end - ORIGIN(sram_seg)) <= LENGTH(sram_seg)),
 | |
|           "IRAM0 segment data does not fit.")
 | |
| 
 | |
| ASSERT(((_heap_start - ORIGIN(sram_seg)) <= LENGTH(sram_seg)),
 | |
|           "DRAM segment data does not fit.")
 |