elf_chaos.sc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # Copyright (C) 2014-2015 Free Software Foundation, Inc.
  2. #
  3. # Copying and distribution of this file, with or without modification,
  4. # are permitted in any medium without royalty provided the copyright
  5. # notice and this notice are preserved.
  6. #
  7. # Unusual variables checked by this code:
  8. # NOP - four byte opcode for no-op (defaults to 0)
  9. # NO_SMALL_DATA - no .sbss/.sbss2/.sdata/.sdata2 sections if not
  10. # empty.
  11. # DATA_ADDR - if end-of-text-plus-one-page isn't right for data start
  12. # INITIAL_READONLY_SECTIONS - at start of text segment
  13. # OTHER_READONLY_SECTIONS - other than .text .init .rodata ...
  14. # (e.g., .PARISC.milli)
  15. # OTHER_TEXT_SECTIONS - these get put in .text when relocating
  16. # OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
  17. # (e.g., .PARISC.global)
  18. # ATTRS_SECTIONS - at the end
  19. # OTHER_SECTIONS - at the end
  20. # EXECUTABLE_SYMBOLS - symbols that must be defined for an
  21. # executable (e.g., _DYNAMIC_LINK)
  22. # TEXT_START_SYMBOLS - symbols that appear at the start of the
  23. # .text section.
  24. # DATA_START_SYMBOLS - symbols that appear at the start of the
  25. # .data section.
  26. # OTHER_GOT_SYMBOLS - symbols defined just before .got.
  27. # OTHER_GOT_SECTIONS - sections just after .got.
  28. # OTHER_SDATA_SECTIONS - sections just after .sdata.
  29. # OTHER_BSS_SYMBOLS - symbols that appear at the start of the
  30. # .bss section besides __bss_start.
  31. # DATA_PLT - .plt should be in data segment, not text segment.
  32. # BSS_PLT - .plt should be in bss segment
  33. # TEXT_DYNAMIC - .dynamic in text segment, not data segment.
  34. # EMBEDDED - whether this is for an embedded system.
  35. # SHLIB_TEXT_START_ADDR - if set, add to SIZEOF_HEADERS to set
  36. # start address of shared library.
  37. # INPUT_FILES - INPUT command of files to always include
  38. # WRITABLE_RODATA - if set, the .rodata section should be writable
  39. # INIT_START, INIT_END - statements just before and just after
  40. # combination of .init sections.
  41. # FINI_START, FINI_END - statements just before and just after
  42. # combination of .fini sections.
  43. # STACK_ADDR - start of a .stack section.
  44. # OTHER_SYMBOLS - symbols to place right at the end of the script.
  45. #
  46. # When adding sections, do note that the names of some sections are used
  47. # when specifying the start address of the next.
  48. #
  49. # Many sections come in three flavours. There is the 'real' section,
  50. # like ".data". Then there are the per-procedure or per-variable
  51. # sections, generated by -ffunction-sections and -fdata-sections in GCC,
  52. # and useful for --gc-sections, which for a variable "foo" might be
  53. # ".data.foo". Then there are the linkonce sections, for which the linker
  54. # eliminates duplicates, which are named like ".gnu.linkonce.d.foo".
  55. # The exact correspondences are:
  56. #
  57. # Section Linkonce section
  58. # .text .gnu.linkonce.t.foo
  59. # .rodata .gnu.linkonce.r.foo
  60. # .data .gnu.linkonce.d.foo
  61. # .bss .gnu.linkonce.b.foo
  62. # .sdata .gnu.linkonce.s.foo
  63. # .sbss .gnu.linkonce.sb.foo
  64. # .sdata2 .gnu.linkonce.s2.foo
  65. # .sbss2 .gnu.linkonce.sb2.foo
  66. # .debug_info .gnu.linkonce.wi.foo
  67. #
  68. # Each of these can also have corresponding .rel.* and .rela.* sections.
  69. test -z "$ENTRY" && ENTRY=_start
  70. test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
  71. test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
  72. if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
  73. test -z "${ELFSIZE}" && ELFSIZE=32
  74. test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
  75. test -z "$ATTRS_SECTIONS" && ATTRS_SECTIONS=".gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
  76. test "$LD_FLAG" = "N" && DATA_ADDR=.
  77. INTERP=".interp ${RELOCATING-0} : { *(.interp) }"
  78. PLT=".plt ${RELOCATING-0} : { *(.plt) }"
  79. DYNAMIC=".dynamic ${RELOCATING-0} : { *(.dynamic) }"
  80. RODATA=".rodata ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
  81. if test -z "${NO_SMALL_DATA}"; then
  82. SBSS=".sbss ${RELOCATING-0} :
  83. {
  84. ${RELOCATING+PROVIDE (__sbss_start = .);}
  85. ${RELOCATING+PROVIDE (___sbss_start = .);}
  86. *(.dynsbss)
  87. *(.sbss${RELOCATING+ .sbss.* .gnu.linkonce.sb.*})
  88. *(.scommon)
  89. ${RELOCATING+PROVIDE (__sbss_end = .);}
  90. ${RELOCATING+PROVIDE (___sbss_end = .);}
  91. }"
  92. SBSS2=".sbss2 ${RELOCATING-0} : { *(.sbss2${RELOCATING+ .sbss2.* .gnu.linkonce.sb2.*}) }"
  93. SDATA="/* We want the small data sections together, so single-instruction offsets
  94. can access them all, and initialized data all before uninitialized, so
  95. we can shorten the on-disk segment size. */
  96. .sdata ${RELOCATING-0} :
  97. {
  98. ${RELOCATING+${SDATA_START_SYMBOLS}}
  99. *(.sdata${RELOCATING+ .sdata.* .gnu.linkonce.s.*})
  100. }"
  101. SDATA2=".sdata2 ${RELOCATING-0} : { *(.sdata2${RELOCATING+ .sdata2.* .gnu.linkonce.s2.*}) }"
  102. REL_SDATA=".rel.sdata ${RELOCATING-0} : { *(.rel.sdata${RELOCATING+ .rel.sdata.* .rel.gnu.linkonce.s.*}) }
  103. .rela.sdata ${RELOCATING-0} : { *(.rela.sdata${RELOCATING+ .rela.sdata.* .rela.gnu.linkonce.s.*}) }"
  104. REL_SBSS=".rel.sbss ${RELOCATING-0} : { *(.rel.sbss${RELOCATING+ .rel.sbss.* .rel.gnu.linkonce.sb.*}) }
  105. .rela.sbss ${RELOCATING-0} : { *(.rela.sbss${RELOCATING+ .rela.sbss.* .rela.gnu.linkonce.sb.*}) }"
  106. REL_SDATA2=".rel.sdata2 ${RELOCATING-0} : { *(.rel.sdata2${RELOCATING+ .rel.sdata2.* .rel.gnu.linkonce.s2.*}) }
  107. .rela.sdata2 ${RELOCATING-0} : { *(.rela.sdata2${RELOCATING+ .rela.sdata2.* .rela.gnu.linkonce.s2.*}) }"
  108. REL_SBSS2=".rel.sbss2 ${RELOCATING-0} : { *(.rel.sbss2${RELOCATING+ .rel.sbss2.* .rel.gnu.linkonce.sb2.*}) }
  109. .rela.sbss2 ${RELOCATING-0} : { *(.rela.sbss2${RELOCATING+ .rela.sbss2.* .rela.gnu.linkonce.sb2.*}) }"
  110. fi
  111. CTOR="
  112. ${CONSTRUCTING+${CTOR_START}}
  113. /* gcc uses crtbegin.o to find the start of
  114. the constructors, so we make sure it is
  115. first. Because this is a wildcard, it
  116. doesn't matter if the user does not
  117. actually link against crtbegin.o; the
  118. linker won't look for a file to match a
  119. wildcard. The wildcard also means that it
  120. doesn't matter which directory crtbegin.o
  121. is in. */
  122. KEEP (*crtbegin.o(.ctors))
  123. KEEP (*crtbegin?.o(.ctors))
  124. /* We don't want to include the .ctor section from
  125. the crtend.o file until after the sorted ctors.
  126. The .ctor section from the crtend file contains the
  127. end of ctors marker and it must be last */
  128. KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
  129. KEEP (*(SORT(.ctors.*)))
  130. KEEP (*(.ctors))
  131. ${CONSTRUCTING+${CTOR_END}}
  132. "
  133. DTOR="
  134. ${CONSTRUCTING+${DTOR_START}}
  135. KEEP (*crtbegin.o(.dtors))
  136. KEEP (*crtbegin?.o(.dtors))
  137. KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
  138. KEEP (*(SORT(.dtors.*)))
  139. KEEP (*(.dtors))
  140. ${CONSTRUCTING+${DTOR_END}}
  141. "
  142. STACK=" .stack ${RELOCATING-0}${RELOCATING+${STACK_ADDR}} :
  143. {
  144. ${RELOCATING+_stack = .;}
  145. *(.stack)
  146. }"
  147. test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
  148. cat <<EOF
  149. /* Copyright (C) 2014-2015 Free Software Foundation, Inc.
  150. Copying and distribution of this script, with or without modification,
  151. are permitted in any medium without royalty provided the copyright
  152. notice and this notice are preserved. */
  153. OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  154. "${LITTLE_OUTPUT_FORMAT}")
  155. OUTPUT_ARCH(${OUTPUT_ARCH})
  156. ${RELOCATING+ENTRY(${ENTRY})}
  157. ${RELOCATING+${LIB_SEARCH_DIRS}}
  158. ${RELOCATING+/* Do we need any of these for elf?
  159. __DYNAMIC = 0; ${STACKZERO+${STACKZERO}} ${SHLIB_PATH+${SHLIB_PATH}} */}
  160. ${RELOCATING+${EXECUTABLE_SYMBOLS}}
  161. ${RELOCATING+${INPUT_FILES}}
  162. ${RELOCATING- /* For some reason, the Solaris linker makes bad executables
  163. if gld -r is used and the intermediate file has sections starting
  164. at non-zero addresses. Could be a Solaris ld bug, could be a GNU ld
  165. bug. But for now assigning the zero vmas works. */}
  166. SECTIONS
  167. {
  168. /* Read-only sections, merged into text segment: */
  169. ${CREATE_SHLIB-${RELOCATING+. = ${TEXT_BASE_ADDRESS};}}
  170. ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0};}}
  171. ${CREATE_SHLIB-${INTERP}}
  172. ${INITIAL_READONLY_SECTIONS}
  173. ${TEXT_DYNAMIC+${DYNAMIC}}
  174. .hash ${RELOCATING-0} : { *(.hash) }
  175. .dynsym ${RELOCATING-0} : { *(.dynsym) }
  176. .dynstr ${RELOCATING-0} : { *(.dynstr) }
  177. .gnu.version ${RELOCATING-0} : { *(.gnu.version) }
  178. .gnu.version_d ${RELOCATING-0}: { *(.gnu.version_d) }
  179. .gnu.version_r ${RELOCATING-0}: { *(.gnu.version_r) }
  180. EOF
  181. if [ "x$COMBRELOC" = x ]; then
  182. COMBRELOCCAT=cat
  183. else
  184. COMBRELOCCAT="cat > $COMBRELOC"
  185. fi
  186. eval $COMBRELOCCAT <<EOF
  187. .rel.init ${RELOCATING-0} : { *(.rel.init) }
  188. .rela.init ${RELOCATING-0} : { *(.rela.init) }
  189. .rel.text ${RELOCATING-0} : { *(.rel.text${RELOCATING+ .rel.text.* .rel.gnu.linkonce.t.*}) }
  190. .rela.text ${RELOCATING-0} : { *(.rela.text${RELOCATING+ .rela.text.* .rela.gnu.linkonce.t.*}) }
  191. .rel.fini ${RELOCATING-0} : { *(.rel.fini) }
  192. .rela.fini ${RELOCATING-0} : { *(.rela.fini) }
  193. .rel.rodata ${RELOCATING-0} : { *(.rel.rodata${RELOCATING+ .rel.rodata.* .rel.gnu.linkonce.r.*}) }
  194. .rela.rodata ${RELOCATING-0} : { *(.rela.rodata${RELOCATING+ .rela.rodata.* .rela.gnu.linkonce.r.*}) }
  195. ${OTHER_READONLY_RELOC_SECTIONS}
  196. .rel.data ${RELOCATING-0} : { *(.rel.data${RELOCATING+ .rel.data.* .rel.gnu.linkonce.d.*}) }
  197. .rela.data ${RELOCATING-0} : { *(.rela.data${RELOCATING+ .rela.data.* .rela.gnu.linkonce.d.*}) }
  198. .rel.ctors ${RELOCATING-0} : { *(.rel.ctors) }
  199. .rela.ctors ${RELOCATING-0} : { *(.rela.ctors) }
  200. .rel.dtors ${RELOCATING-0} : { *(.rel.dtors) }
  201. .rela.dtors ${RELOCATING-0} : { *(.rela.dtors) }
  202. .rel.got ${RELOCATING-0} : { *(.rel.got) }
  203. .rela.got ${RELOCATING-0} : { *(.rela.got) }
  204. ${OTHER_GOT_RELOC_SECTIONS}
  205. ${REL_SDATA}
  206. ${REL_SBSS}
  207. ${REL_SDATA2}
  208. ${REL_SBSS2}
  209. .rel.bss ${RELOCATING-0} : { *(.rel.bss${RELOCATING+ .rel.bss.* .rel.gnu.linkonce.b.*}) }
  210. .rela.bss ${RELOCATING-0} : { *(.rela.bss${RELOCATING+ .rela.bss.* .rela.gnu.linkonce.b.*}) }
  211. EOF
  212. if [ -n "$COMBRELOC" ]; then
  213. cat <<EOF
  214. .rel.dyn ${RELOCATING-0} :
  215. {
  216. EOF
  217. sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rela\./d;s/^.*: { *\(.*\)}$/ \1/' $COMBRELOC
  218. cat <<EOF
  219. }
  220. .rela.dyn ${RELOCATING-0} :
  221. {
  222. EOF
  223. sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rel\./d;s/^.*: { *\(.*\)}/ \1/' $COMBRELOC
  224. cat <<EOF
  225. }
  226. EOF
  227. fi
  228. cat <<EOF
  229. . = ALIGN(0x1000);
  230. .rel.plt ${RELOCATING-0} : { *(.rel.plt) }
  231. .rela.plt ${RELOCATING-0} : { *(.rela.plt) }
  232. ${OTHER_PLT_RELOC_SECTIONS}
  233. ${DATA_PLT-${BSS_PLT-${PLT}}}
  234. .text ${RELOCATING-0} :
  235. {
  236. ${RELOCATING+${TEXT_START_SYMBOLS}}
  237. *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
  238. /* .gnu.warning sections are handled specially by elf32.em. */
  239. *(.gnu.warning)
  240. ${RELOCATING+${OTHER_TEXT_SECTIONS}}
  241. } =${NOP-0}
  242. .fini ${RELOCATING-0} :
  243. {
  244. ${RELOCATING+${FINI_START}}
  245. KEEP (*(.fini))
  246. ${RELOCATING+${FINI_END}}
  247. } =${NOP-0}
  248. ${RELOCATING+PROVIDE (__etext = .);}
  249. ${RELOCATING+PROVIDE (_etext = .);}
  250. ${RELOCATING+PROVIDE (etext = .);}
  251. . = ALIGN(0x1000);
  252. ${CREATE_SHLIB-${SDATA2}}
  253. ${CREATE_SHLIB-${SBSS2}}
  254. ${OTHER_READONLY_SECTIONS}
  255. .eh_frame_hdr : { *(.eh_frame_hdr) }
  256. . = ALIGN(0x1000);
  257. .data ${RELOCATING-0} :
  258. {
  259. *(.rodata .rodata.*)
  260. *(.rodata1)
  261. *(.gnu.linkonce.r.*)
  262. ${RELOCATING+${DATA_START_SYMBOLS}}
  263. *(.data${RELOCATING+ .data.* .gnu.linkonce.d.*})
  264. ${CONSTRUCTING+SORT(CONSTRUCTORS)}
  265. KEEP (*(.eh_frame))
  266. *(.gcc_except_table)
  267. ${CTOR}
  268. ${DTOR}
  269. KEEP (*(.jcr))
  270. }
  271. .data1 ${RELOCATING-0} : { *(.data1) }
  272. . = ALIGN(0x1000);
  273. .gcc_except_table ${RELOCATING-0} : { *(.gcc_except_table) }
  274. ${WRITABLE_RODATA+${RODATA}}
  275. ${OTHER_READWRITE_SECTIONS}
  276. ${TEXT_DYNAMIC-${DYNAMIC}}
  277. ${DATA_PLT+${PLT}}
  278. ${RELOCATING+${OTHER_GOT_SYMBOLS}}
  279. .got ${RELOCATING-0} : { *(.got.plt) *(.got) }
  280. ${OTHER_GOT_SECTIONS}
  281. ${CREATE_SHLIB+${SDATA2}}
  282. ${CREATE_SHLIB+${SBSS2}}
  283. ${SDATA}
  284. ${OTHER_SDATA_SECTIONS}
  285. ${RELOCATING+_edata = .;}
  286. ${RELOCATING+PROVIDE (edata = .);}
  287. ${RELOCATING+__bss_start = .;}
  288. ${RELOCATING+${OTHER_BSS_SYMBOLS}}
  289. ${SBSS}
  290. ${BSS_PLT+${PLT}}
  291. . = ALIGN(0x1000);
  292. .bss ${RELOCATING-0} :
  293. {
  294. *(.dynbss)
  295. *(.bss${RELOCATING+ .bss.* .gnu.linkonce.b.*})
  296. *(COMMON)
  297. /* Align here to ensure that the .bss section occupies space up to
  298. _end. Align after .bss to ensure correct alignment even if the
  299. .bss section disappears because there are no input sections. */
  300. ${RELOCATING+. = ALIGN(${ALIGNMENT});}
  301. }
  302. ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
  303. ${RELOCATING+. = ALIGN(${ALIGNMENT});}
  304. ${RELOCATING+${OTHER_END_SYMBOLS}}
  305. ${RELOCATING+_end = .;}
  306. ${RELOCATING+PROVIDE (end = .);}
  307. /* Stabs debugging sections. */
  308. . = ALIGN(0x1000);
  309. .stab 0 : { *(.stab) }
  310. .stabstr 0 : { *(.stabstr) }
  311. .stab.excl 0 : { *(.stab.excl) }
  312. .stab.exclstr 0 : { *(.stab.exclstr) }
  313. .stab.index 0 : { *(.stab.index) }
  314. .stab.indexstr 0 : { *(.stab.indexstr) }
  315. . = ALIGN(0x1000);
  316. .comment 0 : { *(.comment) }
  317. EOF
  318. . $srcdir/scripttempl/DWARF.sc
  319. cat <<EOF
  320. ${STACK_ADDR+${STACK}}
  321. ${ATTRS_SECTIONS}
  322. ${OTHER_SECTIONS}
  323. ${RELOCATING+${OTHER_SYMBOLS}}
  324. }
  325. EOF