link.x 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. PHDRS
  12. {
  13. load PT_LOAD;
  14. ram_load PT_LOAD;
  15. virtual PT_NULL;
  16. }
  17. SECTIONS
  18. {
  19. .text.dummy ORIGIN(REGION_TEXT) :
  20. {
  21. /* This section is intended to make _stext address work */
  22. } > REGION_TEXT :virtual
  23. .text ALIGN(_stext,4) :
  24. {
  25. /* Put reset handler first in .text section so it ends up as the entry */
  26. /* point of the program. */
  27. KEEP(*(.init));
  28. KEEP(*(.init.rust));
  29. . = ALIGN(4);
  30. KEEP(*(.trap));
  31. KEEP(*(.trap.rust));
  32. *(.text .text.*);
  33. } > REGION_TEXT :load
  34. .rodata ALIGN(4) :
  35. {
  36. *(.rodata .rodata.*);
  37. } > REGION_RODATA :load
  38. .data ALIGN(4) :
  39. {
  40. _sidata = LOADADDR(.data);
  41. _sdata = .;
  42. /* Must be called __global_pointer$ for linker relaxations to work. */
  43. PROVIDE(__global_pointer$ = . + 0x800);
  44. *(.data .data.*);
  45. . = ALIGN(4);
  46. _edata = .;
  47. } > REGION_DATA AT > REGION_RODATA :ram_load
  48. .bss :
  49. {
  50. _sbss = .;
  51. *(.bss .bss.*);
  52. . = ALIGN(4);
  53. _ebss = .;
  54. } > REGION_BSS :virtual
  55. /* fictitious region that represents the memory available for the heap */
  56. .heap (INFO) :
  57. {
  58. _sheap = .;
  59. . += _heap_size;
  60. . = ALIGN(4);
  61. _eheap = .;
  62. } > REGION_HEAP :virtual
  63. /* fictitious region that represents the memory available for the stack */
  64. .stack (INFO) :
  65. {
  66. _estack = .;
  67. . = _stack_start;
  68. _sstack = .;
  69. } > REGION_STACK :virtual
  70. /* fake output .got section */
  71. /* Dynamic relocations are unsupported. This section is only used to detect
  72. relocatable code in the input files and raise an error if relocatable code
  73. is found */
  74. .got (INFO) :
  75. {
  76. KEEP(*(.got .got.*));
  77. }
  78. /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */
  79. /DISCARD/ :
  80. {
  81. *(.eh_frame);
  82. }
  83. }
  84. /* Do not exceed this mark in the error messages above | */
  85. ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, "
  86. ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned");
  87. ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, "
  88. ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned");
  89. ASSERT(ORIGIN(REGION_DATA) % 4 == 0, "
  90. ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned");
  91. ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, "
  92. ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned");
  93. ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, "
  94. ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned");
  95. ASSERT(ORIGIN(REGION_STACK) % 4 == 0, "
  96. ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned");
  97. ASSERT(_stext % 4 == 0, "
  98. ERROR(riscv-rt): `_stext` must be 4-byte aligned");
  99. ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, "
  100. BUG(riscv-rt): .data is not 4-byte aligned");
  101. ASSERT(_sidata % 4 == 0, "
  102. BUG(riscv-rt): the LMA of .data is not 4-byte aligned");
  103. ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, "
  104. BUG(riscv-rt): .bss is not 4-byte aligned");
  105. ASSERT(_sheap % 4 == 0, "
  106. BUG(riscv-rt): start of .heap is not 4-byte aligned");
  107. ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), "
  108. ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region.
  109. Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'");
  110. ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, "
  111. ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts.
  112. Consider changing `_max_hart_id` or `_hart_stack_size`.");
  113. ASSERT(SIZEOF(.got) == 0, "
  114. .got section detected in the input files. Dynamic relocations are not
  115. supported. If you are linking to C code compiled using the `gcc` crate
  116. then modify your build script to compile the C code _without_ the
  117. -fPIC flag. See the documentation of the `gcc::Config.fpic` method for
  118. details.");
  119. /* Do not exceed this mark in the error messages above | */