link.x 4.0 KB

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