link.x 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. PROVIDE(_stext = ORIGIN(REGION_TEXT));
  2. PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK));
  3. PROVIDE(_max_hart_id = 0);
  4. PROVIDE(_hart_stack_size = 2K);
  5. PROVIDE(_heap_size = 0);
  6. PROVIDE(trap_handler = default_trap_handler);
  7. /* # Pre-initialization function */
  8. /* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function,
  9. then the function this points to will be called before the RAM is initialized. */
  10. PROVIDE(__pre_init = default_pre_init);
  11. /* # Multi-processing hook function
  12. fn _mp_hook() -> bool;
  13. This function is called from all the harts and must return true only for one hart,
  14. which will perform memory initialization. For other harts it must return false
  15. and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt).
  16. */
  17. PROVIDE(_mp_hook = default_mp_hook);
  18. PHDRS
  19. {
  20. load PT_LOAD;
  21. ram_load PT_LOAD;
  22. virtual PT_NULL;
  23. }
  24. SECTIONS
  25. {
  26. .text.dummy ORIGIN(REGION_TEXT) :
  27. {
  28. /* This section is intended to make _stext address work */
  29. } > REGION_TEXT :virtual
  30. .text _stext :
  31. {
  32. /* Put reset handler first in .text section so it ends up as the entry */
  33. /* point of the program. */
  34. KEEP(*(.init));
  35. KEEP(*(.init.rust));
  36. . = ALIGN(4);
  37. KEEP(*(.trap));
  38. KEEP(*(.trap.rust));
  39. *(.text .text.*);
  40. } > REGION_TEXT :load
  41. .rodata : ALIGN(4)
  42. {
  43. *(.rodata .rodata.*);
  44. /* 4-byte align the end (VMA) of this section.
  45. This is required by LLD to ensure the LMA of the following .data
  46. section will have the correct alignment. */
  47. . = ALIGN(4);
  48. } > REGION_RODATA :load
  49. .data : ALIGN(4)
  50. {
  51. _sidata = LOADADDR(.data);
  52. _sdata = .;
  53. /* Must be called __global_pointer$ for linker relaxations to work. */
  54. PROVIDE(__global_pointer$ = . + 0x800);
  55. *(.sdata .sdata.*);
  56. *(.data .data.*);
  57. . = ALIGN(4);
  58. _edata = .;
  59. } > REGION_DATA AT > REGION_RODATA :ram_load
  60. .bss :
  61. {
  62. _sbss = .;
  63. *(.sbss .sbss.* .bss .bss.*);
  64. . = ALIGN(4);
  65. _ebss = .;
  66. } > REGION_BSS :virtual
  67. /* fictitious region that represents the memory available for the heap */
  68. .heap (INFO) :
  69. {
  70. _sheap = .;
  71. . += _heap_size;
  72. . = ALIGN(4);
  73. _eheap = .;
  74. } > REGION_HEAP :virtual
  75. /* fictitious region that represents the memory available for the stack */
  76. .stack (INFO) :
  77. {
  78. _estack = .;
  79. . = _stack_start;
  80. _sstack = .;
  81. } > REGION_STACK :virtual
  82. /* fake output .got section */
  83. /* Dynamic relocations are unsupported. This section is only used to detect
  84. relocatable code in the input files and raise an error if relocatable code
  85. is found */
  86. .got (INFO) :
  87. {
  88. KEEP(*(.got .got.*));
  89. }
  90. /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */
  91. /DISCARD/ :
  92. {
  93. *(.eh_frame);
  94. }
  95. }
  96. /* Do not exceed this mark in the error messages above | */
  97. ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, "
  98. ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned");
  99. ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, "
  100. ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned");
  101. ASSERT(ORIGIN(REGION_DATA) % 4 == 0, "
  102. ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned");
  103. ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, "
  104. ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned");
  105. ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, "
  106. ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned");
  107. ASSERT(ORIGIN(REGION_STACK) % 4 == 0, "
  108. ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned");
  109. ASSERT(_stext % 4 == 0, "
  110. ERROR(riscv-rt): `_stext` must be 4-byte aligned");
  111. ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, "
  112. BUG(riscv-rt): .data is not 4-byte aligned");
  113. ASSERT(_sidata % 4 == 0, "
  114. BUG(riscv-rt): the LMA of .data is not 4-byte aligned");
  115. ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, "
  116. BUG(riscv-rt): .bss is not 4-byte aligned");
  117. ASSERT(_sheap % 4 == 0, "
  118. BUG(riscv-rt): start of .heap is not 4-byte aligned");
  119. ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), "
  120. ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region.
  121. Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'");
  122. ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, "
  123. ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts.
  124. Consider changing `_max_hart_id` or `_hart_stack_size`.");
  125. ASSERT(SIZEOF(.got) == 0, "
  126. .got section detected in the input files. Dynamic relocations are not
  127. supported. If you are linking to C code compiled using the `gcc` crate
  128. then modify your build script to compile the C code _without_ the
  129. -fPIC flag. See the documentation of the `gcc::Config.fpic` method for
  130. details.");
  131. /* Do not exceed this mark in the error messages above | */