PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/linker-scripts.rkt

https://bitbucket.org/loee/lpc1xxx-hw-git
Racket | 140 lines | 108 code | 29 blank | 3 comment | 7 complexity | 5760130b54f18cbe6c5df280c92f45c5 MD5 | raw file
  1. #lang at-exp racket
  2. (require scribble/text
  3. "common.rkt"
  4. "devices.rkt")
  5. (define output-dir "../linker-scripts")
  6. (define (main)
  7. (make-directory/remove output-dir)
  8. (parameterize ([current-directory output-dir])
  9. (for ([d (in-list devices)])
  10. (define fname (format "~a.ld" (device-model d)))
  11. (unless (file-exists? fname)
  12. (with-output-to-file fname
  13. (λ () (output (device-linker-script d))))))
  14. (with-output-to-file "flash.ld" (λ () (output (flash-linker-script))))
  15. (with-output-to-file "bootloader.ld" (λ () (output (bootloader-linker-script))))))
  16. (define (TODO)
  17. @list{TODO:
  18. - handle the exotic input sections (e.g. glue and veneer, C++ sections)
  19. - add boot ROM memory regions})
  20. ;;
  21. ;; Formatting
  22. ;;
  23. (define (flash-linker-script)
  24. @list{
  25. @header[url]{
  26. This is the linker file fragment for code running from flash
  27. on NXP LPC17xx family of microprocessors. It lacks MEMORY declarations
  28. so it must be included from an apropriate model-specific file.
  29. @(TODO)}
  30. OUTPUT_FORMAT("elf32-littlearm")
  31. OUTPUT_ARCH(arm)
  32. ENTRY(Reset_Handler)
  33. SECTIONS {
  34. . = 0;
  35. .text : {
  36. _stext = .;
  37. KEEP(*(.cs3.interrupt_vector))
  38. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.text*)))
  39. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*)))
  40. . = ALIGN(4);
  41. _etext = .;
  42. } > FLASH
  43. .data : {
  44. _sdata = .;
  45. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.data*)))
  46. . = ALIGN(4);
  47. _edata = .;
  48. } > SRAM AT > FLASH
  49. .bss : {
  50. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
  51. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(COMMON)))
  52. . = ALIGN(4);
  53. _ebss = .;
  54. } > SRAM
  55. _sstack = ORIGIN(SRAM) + LENGTH(SRAM) - 32; /* 32 bytes for IAP */
  56. }})
  57. (define (bootloader-linker-script)
  58. @list{
  59. @header[url]{
  60. This is the linker file fragment for code running from flash
  61. on NXP LPC17xx family of microprocessors. It lacks MEMORY declarations
  62. so it must be included from an apropriate model-specific file.
  63. @(TODO)}
  64. OUTPUT_FORMAT("elf32-littlearm")
  65. OUTPUT_ARCH(arm)
  66. ENTRY(Reset_Handler)
  67. SECTIONS {
  68. . = 0;
  69. .text 0x1000 : {
  70. _stext = .;
  71. KEEP(*(.cs3.interrupt_vector))
  72. KEEP(*(SORT_BY_NAME(.before*)))
  73. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.text*)))
  74. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*)))
  75. KEEP(*(SORT_BY_NAME(.after*)))
  76. . = ALIGN(4);
  77. _etext = .;
  78. } > FLASH
  79. .data : {
  80. _sdata = .;
  81. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.data*)))
  82. . = ALIGN(4);
  83. _edata = .;
  84. } > SRAM AT > FLASH
  85. .bss : {
  86. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
  87. *(SORT_BY_ALIGNMENT(SORT_BY_NAME(COMMON)))
  88. . = ALIGN(4);
  89. _ebss = .;
  90. } > SRAM
  91. _sstack = ORIGIN(SRAM) + LENGTH(SRAM) - 32; /* 32 bytes for IAP */
  92. }})
  93. (define (device-linker-script d)
  94. @list{
  95. @header[url]{
  96. This is the NXP @(device-model d) linker file for code running from flash.
  97. @(TODO)}
  98. MEMORY {
  99. FLASH (rx) : ORIGIN = 0x00000000, LENGTH = @format-size[(device-total-flash d)]
  100. @(add-newlines
  101. (for/list ([b (device-ram-map d)])
  102. @list{@(memory-block-name b) (rwx) : ORIGIN = 0x@(number->string (memory-block-addr b) 16), @;
  103. LENGTH = @(format-size (memory-block-size b))}))
  104. }
  105. INCLUDE flash.ld
  106. })
  107. (define (format-size n)
  108. (cond
  109. [(zero? (remainder n 1024)) @list{@[/ n 1024]K}]
  110. [else n]))
  111. (main)