elf.sc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 none)
  9. # NO_SMALL_DATA - no .sbss/.sbss2/.sdata/.sdata2 sections if not
  10. # empty.
  11. # SMALL_DATA_CTOR - .ctors contains small data.
  12. # SMALL_DATA_DTOR - .dtors contains small data.
  13. # DATA_ADDR - if end-of-text-plus-one-page isn't right for data start
  14. # INITIAL_READONLY_SECTIONS - at start of text segment
  15. # OTHER_READONLY_SECTIONS - other than .text .init .rodata ...
  16. # (e.g., .PARISC.milli)
  17. # OTHER_TEXT_SECTIONS - these get put in .text when relocating
  18. # INITIAL_READWRITE_SECTIONS - at start of data segment (after relro)
  19. # OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
  20. # (e.g., .PARISC.global)
  21. # OTHER_RELRO_SECTIONS - other than .data.rel.ro ...
  22. # (e.g. PPC32 .fixup, .got[12])
  23. # OTHER_RELRO_SECTIONS_2 - as above, but after .dynamic in text segment
  24. # OTHER_BSS_SECTIONS - other than .bss .sbss ...
  25. # ATTRS_SECTIONS - at the end
  26. # OTHER_SECTIONS - at the end
  27. # EXECUTABLE_SYMBOLS - symbols that must be defined for an
  28. # executable (e.g., _DYNAMIC_LINK)
  29. # TEXT_START_ADDR - the first byte of the text segment, after any
  30. # headers.
  31. # TEXT_BASE_ADDRESS - the first byte of the text segment.
  32. # TEXT_START_SYMBOLS - symbols that appear at the start of the
  33. # .text section.
  34. # DATA_START_SYMBOLS - symbols that appear at the start of the
  35. # .data section.
  36. # DATA_END_SYMBOLS - symbols that appear at the end of the
  37. # writeable data sections.
  38. # OTHER_GOT_SYMBOLS - symbols defined just before .got.
  39. # OTHER_GOT_SECTIONS - sections just after .got.
  40. # OTHER_SDATA_SECTIONS - sections just after .sdata.
  41. # OTHER_BSS_SYMBOLS - symbols that appear at the start of the
  42. # .bss section besides __bss_start.
  43. # PLT_NEXT_DATA - .plt next to data segment when .plt is in text segment.
  44. # DATA_PLT - .plt should be in data segment, not text segment.
  45. # PLT_BEFORE_GOT - .plt just before .got when .plt is in data segement.
  46. # BSS_PLT - .plt should be in bss segment
  47. # NO_REL_RELOCS - Don't include .rel.* sections in script
  48. # NO_RELA_RELOCS - Don't include .rela.* sections in script
  49. # NON_ALLOC_DYN - Place dynamic sections after data segment.
  50. # TEXT_DYNAMIC - .dynamic in text segment, not data segment.
  51. # EMBEDDED - whether this is for an embedded system.
  52. # SHLIB_TEXT_START_ADDR - if set, add to SIZEOF_HEADERS to set
  53. # start address of shared library.
  54. # INPUT_FILES - INPUT command of files to always include
  55. # WRITABLE_RODATA - if set, the .rodata section should be writable
  56. # INIT_START, INIT_END - statements just before and just after
  57. # combination of .init sections.
  58. # FINI_START, FINI_END - statements just before and just after
  59. # combination of .fini sections.
  60. # STACK_ADDR - start of a .stack section.
  61. # OTHER_SYMBOLS - symbols to place right at the end of the script.
  62. # ETEXT_NAME - name of a symbol for the end of the text section,
  63. # normally etext.
  64. # SEPARATE_CODE - if set, .text and similar sections containing
  65. # actual machine instructions must be in wholly disjoint
  66. # pages from any other data, including headers
  67. # SEPARATE_GOTPLT - if set, .got.plt should be separate output section,
  68. # so that .got can be in the RELRO area. It should be set to
  69. # the number of bytes in the beginning of .got.plt which can be
  70. # in the RELRO area as well.
  71. # USER_LABEL_PREFIX - prefix to add to user-visible symbols.
  72. # RODATA_NAME, SDATA_NAME, SBSS_NAME, BSS_NAME - base parts of names
  73. # for standard sections, without initial "." or suffixes.
  74. #
  75. # When adding sections, do note that the names of some sections are used
  76. # when specifying the start address of the next.
  77. #
  78. # Many sections come in three flavours. There is the 'real' section,
  79. # like ".data". Then there are the per-procedure or per-variable
  80. # sections, generated by -ffunction-sections and -fdata-sections in GCC,
  81. # and useful for --gc-sections, which for a variable "foo" might be
  82. # ".data.foo". Then there are the linkonce sections, for which the linker
  83. # eliminates duplicates, which are named like ".gnu.linkonce.d.foo".
  84. # The exact correspondences are:
  85. #
  86. # Section Linkonce section
  87. # .text .gnu.linkonce.t.foo
  88. # .rodata .gnu.linkonce.r.foo
  89. # .data .gnu.linkonce.d.foo
  90. # .bss .gnu.linkonce.b.foo
  91. # .sdata .gnu.linkonce.s.foo
  92. # .sbss .gnu.linkonce.sb.foo
  93. # .sdata2 .gnu.linkonce.s2.foo
  94. # .sbss2 .gnu.linkonce.sb2.foo
  95. # .debug_info .gnu.linkonce.wi.foo
  96. # .tdata .gnu.linkonce.td.foo
  97. # .tbss .gnu.linkonce.tb.foo
  98. # .lrodata .gnu.linkonce.lr.foo
  99. # .ldata .gnu.linkonce.l.foo
  100. # .lbss .gnu.linkonce.lb.foo
  101. #
  102. # Each of these can also have corresponding .rel.* and .rela.* sections.
  103. if test -n "$NOP"; then
  104. FILL="=$NOP"
  105. else
  106. FILL=
  107. fi
  108. test -z "$RODATA_NAME" && RODATA_NAME=rodata
  109. test -z "$SDATA_NAME" && SDATA_NAME=sdata
  110. test -z "$SBSS_NAME" && SBSS_NAME=sbss
  111. test -z "$BSS_NAME" && BSS_NAME=bss
  112. test -z "$ENTRY" && ENTRY=${USER_LABEL_PREFIX}_start
  113. test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
  114. test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
  115. if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
  116. test -z "${ELFSIZE}" && ELFSIZE=32
  117. test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
  118. test "$LD_FLAG" = "N" && DATA_ADDR=.
  119. test -z "${ETEXT_NAME}" && ETEXT_NAME=${USER_LABEL_PREFIX}etext
  120. test -n "$CREATE_SHLIB$CREATE_PIE" && test -n "$SHLIB_DATA_ADDR" && COMMONPAGESIZE=""
  121. test -z "$CREATE_SHLIB$CREATE_PIE" && test -n "$DATA_ADDR" && COMMONPAGESIZE=""
  122. test -n "$RELRO_NOW" && unset SEPARATE_GOTPLT
  123. test -z "$ATTRS_SECTIONS" && ATTRS_SECTIONS=".gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
  124. DATA_SEGMENT_ALIGN="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
  125. DATA_SEGMENT_RELRO_END=""
  126. DATA_SEGMENT_END=""
  127. if test -n "${COMMONPAGESIZE}"; then
  128. if test "${SEGMENT_SIZE}" != "${MAXPAGESIZE}"; then
  129. DATA_SEGMENT_ALIGN="ALIGN (${SEGMENT_SIZE}) - ((${MAXPAGESIZE} - .) & (${MAXPAGESIZE} - 1)); . = DATA_SEGMENT_ALIGN (${MAXPAGESIZE}, ${COMMONPAGESIZE})"
  130. else
  131. DATA_SEGMENT_ALIGN="DATA_SEGMENT_ALIGN (${MAXPAGESIZE}, ${COMMONPAGESIZE})"
  132. fi
  133. DATA_SEGMENT_END=". = DATA_SEGMENT_END (.);"
  134. DATA_SEGMENT_RELRO_END=". = DATA_SEGMENT_RELRO_END (${SEPARATE_GOTPLT-0}, .);"
  135. fi
  136. if test -z "${INITIAL_READONLY_SECTIONS}${CREATE_SHLIB}"; then
  137. INITIAL_READONLY_SECTIONS=".interp ${RELOCATING-0} : { *(.interp) }"
  138. fi
  139. if test -z "$PLT"; then
  140. IPLT=".iplt ${RELOCATING-0} : { *(.iplt) }"
  141. PLT=".plt ${RELOCATING-0} : { *(.plt)${IREL_IN_PLT+ *(.iplt)} }
  142. ${IREL_IN_PLT-$IPLT}"
  143. fi
  144. test -n "${DATA_PLT-${BSS_PLT-text}}" && TEXT_PLT=
  145. if test -z "$GOT"; then
  146. if test -z "$SEPARATE_GOTPLT"; then
  147. GOT=".got ${RELOCATING-0} : { *(.got.plt) *(.igot.plt) *(.got) *(.igot) }"
  148. else
  149. GOT=".got ${RELOCATING-0} : { *(.got) *(.igot) }"
  150. GOTPLT=".got.plt ${RELOCATING-0} : { *(.got.plt) *(.igot.plt) }"
  151. fi
  152. fi
  153. REL_IFUNC=".rel.ifunc ${RELOCATING-0} : { *(.rel.ifunc) }"
  154. RELA_IFUNC=".rela.ifunc ${RELOCATING-0} : { *(.rela.ifunc) }"
  155. REL_IPLT=".rel.iplt ${RELOCATING-0} :
  156. {
  157. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_start = .);}}
  158. *(.rel.iplt)
  159. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_end = .);}}
  160. }"
  161. RELA_IPLT=".rela.iplt ${RELOCATING-0} :
  162. {
  163. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_start = .);}}
  164. *(.rela.iplt)
  165. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_end = .);}}
  166. }"
  167. DYNAMIC=".dynamic ${RELOCATING-0} : { *(.dynamic) }"
  168. RODATA=".${RODATA_NAME} ${RELOCATING-0} : { *(.${RODATA_NAME}${RELOCATING+ .${RODATA_NAME}.* .gnu.linkonce.r.*}) }"
  169. DATARELRO=".data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }"
  170. DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }"
  171. if test -z "${NO_SMALL_DATA}"; then
  172. SBSS=".${SBSS_NAME} ${RELOCATING-0} :
  173. {
  174. ${RELOCATING+${SBSS_START_SYMBOLS}}
  175. ${CREATE_SHLIB+*(.${SBSS_NAME}2 .${SBSS_NAME}2.* .gnu.linkonce.sb2.*)}
  176. *(.dyn${SBSS_NAME})
  177. *(.${SBSS_NAME}${RELOCATING+ .${SBSS_NAME}.* .gnu.linkonce.sb.*})
  178. *(.scommon)
  179. ${RELOCATING+${SBSS_END_SYMBOLS}}
  180. }"
  181. SBSS2=".${SBSS_NAME}2 ${RELOCATING-0} : { *(.${SBSS_NAME}2${RELOCATING+ .${SBSS_NAME}2.* .gnu.linkonce.sb2.*}) }"
  182. SDATA="/* We want the small data sections together, so single-instruction offsets
  183. can access them all, and initialized data all before uninitialized, so
  184. we can shorten the on-disk segment size. */
  185. .${SDATA_NAME} ${RELOCATING-0} :
  186. {
  187. ${RELOCATING+${SDATA_START_SYMBOLS}}
  188. ${CREATE_SHLIB+*(.${SDATA_NAME}2 .${SDATA_NAME}2.* .gnu.linkonce.s2.*)}
  189. *(.${SDATA_NAME}${RELOCATING+ .${SDATA_NAME}.* .gnu.linkonce.s.*})
  190. }"
  191. SDATA2=".${SDATA_NAME}2 ${RELOCATING-0} :
  192. {
  193. ${RELOCATING+${SDATA2_START_SYMBOLS}}
  194. *(.${SDATA_NAME}2${RELOCATING+ .${SDATA_NAME}2.* .gnu.linkonce.s2.*})
  195. }"
  196. REL_SDATA=".rel.${SDATA_NAME} ${RELOCATING-0} : { *(.rel.${SDATA_NAME}${RELOCATING+ .rel.${SDATA_NAME}.* .rel.gnu.linkonce.s.*}) }
  197. .rela.${SDATA_NAME} ${RELOCATING-0} : { *(.rela.${SDATA_NAME}${RELOCATING+ .rela.${SDATA_NAME}.* .rela.gnu.linkonce.s.*}) }"
  198. REL_SBSS=".rel.${SBSS_NAME} ${RELOCATING-0} : { *(.rel.${SBSS_NAME}${RELOCATING+ .rel.${SBSS_NAME}.* .rel.gnu.linkonce.sb.*}) }
  199. .rela.${SBSS_NAME} ${RELOCATING-0} : { *(.rela.${SBSS_NAME}${RELOCATING+ .rela.${SBSS_NAME}.* .rela.gnu.linkonce.sb.*}) }"
  200. REL_SDATA2=".rel.${SDATA_NAME}2 ${RELOCATING-0} : { *(.rel.${SDATA_NAME}2${RELOCATING+ .rel.${SDATA_NAME}2.* .rel.gnu.linkonce.s2.*}) }
  201. .rela.${SDATA_NAME}2 ${RELOCATING-0} : { *(.rela.${SDATA_NAME}2${RELOCATING+ .rela.${SDATA_NAME}2.* .rela.gnu.linkonce.s2.*}) }"
  202. REL_SBSS2=".rel.${SBSS_NAME}2 ${RELOCATING-0} : { *(.rel.${SBSS_NAME}2${RELOCATING+ .rel.${SBSS_NAME}2.* .rel.gnu.linkonce.sb2.*}) }
  203. .rela.${SBSS_NAME}2 ${RELOCATING-0} : { *(.rela.${SBSS_NAME}2${RELOCATING+ .rela.${SBSS_NAME}2.* .rela.gnu.linkonce.sb2.*}) }"
  204. else
  205. NO_SMALL_DATA=" "
  206. fi
  207. if test -z "${SDATA_GOT}${DATA_GOT}"; then
  208. if test -n "${NO_SMALL_DATA}"; then
  209. DATA_GOT=" "
  210. fi
  211. fi
  212. if test -z "${SDATA_GOT}${DATA_GOT}"; then
  213. if test -z "${NO_SMALL_DATA}"; then
  214. SDATA_GOT=" "
  215. fi
  216. fi
  217. test -n "$SEPARATE_GOTPLT" && SEPARATE_GOTPLT=" "
  218. test "${LARGE_SECTIONS}" = "yes" && REL_LARGE="
  219. .rel.ldata ${RELOCATING-0} : { *(.rel.ldata${RELOCATING+ .rel.ldata.* .rel.gnu.linkonce.l.*}) }
  220. .rela.ldata ${RELOCATING-0} : { *(.rela.ldata${RELOCATING+ .rela.ldata.* .rela.gnu.linkonce.l.*}) }
  221. .rel.lbss ${RELOCATING-0} : { *(.rel.lbss${RELOCATING+ .rel.lbss.* .rel.gnu.linkonce.lb.*}) }
  222. .rela.lbss ${RELOCATING-0} : { *(.rela.lbss${RELOCATING+ .rela.lbss.* .rela.gnu.linkonce.lb.*}) }
  223. .rel.lrodata ${RELOCATING-0} : { *(.rel.lrodata${RELOCATING+ .rel.lrodata.* .rel.gnu.linkonce.lr.*}) }
  224. .rela.lrodata ${RELOCATING-0} : { *(.rela.lrodata${RELOCATING+ .rela.lrodata.* .rela.gnu.linkonce.lr.*}) }"
  225. test "${LARGE_SECTIONS}" = "yes" && LARGE_BSS="
  226. .lbss ${RELOCATING-0} :
  227. {
  228. *(.dynlbss)
  229. *(.lbss${RELOCATING+ .lbss.* .gnu.linkonce.lb.*})
  230. *(LARGE_COMMON)
  231. }"
  232. test "${LARGE_SECTIONS}" = "yes" && LARGE_SECTIONS="
  233. .lrodata ${RELOCATING-0} ${RELOCATING+ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))} :
  234. {
  235. *(.lrodata${RELOCATING+ .lrodata.* .gnu.linkonce.lr.*})
  236. }
  237. .ldata ${RELOCATING-0} ${RELOCATING+ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))} :
  238. {
  239. *(.ldata${RELOCATING+ .ldata.* .gnu.linkonce.l.*})
  240. ${RELOCATING+. = ALIGN(. != 0 ? ${ALIGNMENT} : 1);}
  241. }"
  242. if test "${ENABLE_INITFINI_ARRAY}" = "yes"; then
  243. SORT_INIT_ARRAY="KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))"
  244. SORT_FINI_ARRAY="KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))"
  245. CTORS_IN_INIT_ARRAY="EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors"
  246. DTORS_IN_FINI_ARRAY="EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors"
  247. else
  248. SORT_INIT_ARRAY="KEEP (*(SORT(.init_array.*)))"
  249. SORT_FINI_ARRAY="KEEP (*(SORT(.fini_array.*)))"
  250. CTORS_IN_INIT_ARRAY=
  251. DTORS_IN_FINI_ARRAY=
  252. fi
  253. INIT_ARRAY=".init_array ${RELOCATING-0} :
  254. {
  255. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__init_array_start = .);}}
  256. ${SORT_INIT_ARRAY}
  257. KEEP (*(.init_array ${CTORS_IN_INIT_ARRAY}))
  258. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__init_array_end = .);}}
  259. }"
  260. FINI_ARRAY=".fini_array ${RELOCATING-0} :
  261. {
  262. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__fini_array_start = .);}}
  263. ${SORT_FINI_ARRAY}
  264. KEEP (*(.fini_array ${DTORS_IN_FINI_ARRAY}))
  265. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__fini_array_end = .);}}
  266. }"
  267. CTOR=".ctors ${CONSTRUCTING-0} :
  268. {
  269. ${CONSTRUCTING+${CTOR_START}}
  270. /* gcc uses crtbegin.o to find the start of
  271. the constructors, so we make sure it is
  272. first. Because this is a wildcard, it
  273. doesn't matter if the user does not
  274. actually link against crtbegin.o; the
  275. linker won't look for a file to match a
  276. wildcard. The wildcard also means that it
  277. doesn't matter which directory crtbegin.o
  278. is in. */
  279. KEEP (*crtbegin.o(.ctors))
  280. KEEP (*crtbegin?.o(.ctors))
  281. /* We don't want to include the .ctor section from
  282. the crtend.o file until after the sorted ctors.
  283. The .ctor section from the crtend file contains the
  284. end of ctors marker and it must be last */
  285. KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
  286. KEEP (*(SORT(.ctors.*)))
  287. KEEP (*(.ctors))
  288. ${CONSTRUCTING+${CTOR_END}}
  289. }"
  290. DTOR=".dtors ${CONSTRUCTING-0} :
  291. {
  292. ${CONSTRUCTING+${DTOR_START}}
  293. KEEP (*crtbegin.o(.dtors))
  294. KEEP (*crtbegin?.o(.dtors))
  295. KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
  296. KEEP (*(SORT(.dtors.*)))
  297. KEEP (*(.dtors))
  298. ${CONSTRUCTING+${DTOR_END}}
  299. }"
  300. STACK=" .stack ${RELOCATING-0}${RELOCATING+${STACK_ADDR}} :
  301. {
  302. ${RELOCATING+${USER_LABEL_PREFIX}_stack = .;}
  303. *(.stack)
  304. }"
  305. TEXT_START_ADDR="SEGMENT_START(\"text-segment\", ${TEXT_START_ADDR})"
  306. SHLIB_TEXT_START_ADDR="SEGMENT_START(\"text-segment\", ${SHLIB_TEXT_START_ADDR:-0})"
  307. if [ -z "$SEPARATE_CODE" ]; then
  308. SIZEOF_HEADERS_CODE=" + SIZEOF_HEADERS"
  309. else
  310. SIZEOF_HEADERS_CODE=
  311. fi
  312. # If this is for an embedded system, don't add SIZEOF_HEADERS.
  313. if [ -z "$EMBEDDED" ]; then
  314. test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}${SIZEOF_HEADERS_CODE}"
  315. else
  316. test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
  317. fi
  318. cat <<EOF
  319. /* Copyright (C) 2014-2015 Free Software Foundation, Inc.
  320. Copying and distribution of this script, with or without modification,
  321. are permitted in any medium without royalty provided the copyright
  322. notice and this notice are preserved. */
  323. OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  324. "${LITTLE_OUTPUT_FORMAT}")
  325. OUTPUT_ARCH(${OUTPUT_ARCH})
  326. ${RELOCATING+ENTRY(${ENTRY})}
  327. ${RELOCATING+${LIB_SEARCH_DIRS}}
  328. ${RELOCATING+${EXECUTABLE_SYMBOLS}}
  329. ${RELOCATING+${INPUT_FILES}}
  330. ${RELOCATING- /* For some reason, the Solaris linker makes bad executables
  331. if gld -r is used and the intermediate file has sections starting
  332. at non-zero addresses. Could be a Solaris ld bug, could be a GNU ld
  333. bug. But for now assigning the zero vmas works. */}
  334. SECTIONS
  335. {
  336. /* Read-only sections, merged into text segment: */
  337. ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+PROVIDE (__executable_start = ${TEXT_START_ADDR}); . = ${TEXT_BASE_ADDRESS};}}}
  338. ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR}${SIZEOF_HEADERS_CODE};}}
  339. ${CREATE_PIE+${RELOCATING+PROVIDE (__executable_start = ${SHLIB_TEXT_START_ADDR}); . = ${SHLIB_TEXT_START_ADDR}${SIZEOF_HEADERS_CODE};}}
  340. EOF
  341. emit_early_ro()
  342. {
  343. cat <<EOF
  344. ${INITIAL_READONLY_SECTIONS}
  345. .note.gnu.build-id : { *(.note.gnu.build-id) }
  346. EOF
  347. }
  348. test -n "${SEPARATE_CODE}" || emit_early_ro
  349. test -n "${RELOCATING+0}" || unset NON_ALLOC_DYN
  350. test -z "${NON_ALLOC_DYN}" || TEXT_DYNAMIC=
  351. cat > ldscripts/dyntmp.$$ <<EOF
  352. ${TEXT_DYNAMIC+${DYNAMIC}}
  353. .hash ${RELOCATING-0} : { *(.hash) }
  354. .gnu.hash ${RELOCATING-0} : { *(.gnu.hash) }
  355. .dynsym ${RELOCATING-0} : { *(.dynsym) }
  356. .dynstr ${RELOCATING-0} : { *(.dynstr) }
  357. .gnu.version ${RELOCATING-0} : { *(.gnu.version) }
  358. .gnu.version_d ${RELOCATING-0}: { *(.gnu.version_d) }
  359. .gnu.version_r ${RELOCATING-0}: { *(.gnu.version_r) }
  360. EOF
  361. if [ "x$COMBRELOC" = x ]; then
  362. COMBRELOCCAT="cat >> ldscripts/dyntmp.$$"
  363. else
  364. COMBRELOCCAT="cat > $COMBRELOC"
  365. fi
  366. eval $COMBRELOCCAT <<EOF
  367. ${INITIAL_RELOC_SECTIONS}
  368. .rel.init ${RELOCATING-0} : { *(.rel.init) }
  369. .rela.init ${RELOCATING-0} : { *(.rela.init) }
  370. .rel.text ${RELOCATING-0} : { *(.rel.text${RELOCATING+ .rel.text.* .rel.gnu.linkonce.t.*}) }
  371. .rela.text ${RELOCATING-0} : { *(.rela.text${RELOCATING+ .rela.text.* .rela.gnu.linkonce.t.*}) }
  372. .rel.fini ${RELOCATING-0} : { *(.rel.fini) }
  373. .rela.fini ${RELOCATING-0} : { *(.rela.fini) }
  374. .rel.${RODATA_NAME} ${RELOCATING-0} : { *(.rel.${RODATA_NAME}${RELOCATING+ .rel.${RODATA_NAME}.* .rel.gnu.linkonce.r.*}) }
  375. .rela.${RODATA_NAME} ${RELOCATING-0} : { *(.rela.${RODATA_NAME}${RELOCATING+ .rela.${RODATA_NAME}.* .rela.gnu.linkonce.r.*}) }
  376. ${OTHER_READONLY_RELOC_SECTIONS}
  377. .rel.data.rel.ro ${RELOCATING-0} : { *(.rel.data.rel.ro${RELOCATING+ .rel.data.rel.ro.* .rel.gnu.linkonce.d.rel.ro.*}) }
  378. .rela.data.rel.ro ${RELOCATING-0} : { *(.rela.data.rel.ro${RELOCATING+ .rela.data.rel.ro.* .rela.gnu.linkonce.d.rel.ro.*}) }
  379. .rel.data ${RELOCATING-0} : { *(.rel.data${RELOCATING+ .rel.data.* .rel.gnu.linkonce.d.*}) }
  380. .rela.data ${RELOCATING-0} : { *(.rela.data${RELOCATING+ .rela.data.* .rela.gnu.linkonce.d.*}) }
  381. ${OTHER_READWRITE_RELOC_SECTIONS}
  382. .rel.tdata ${RELOCATING-0} : { *(.rel.tdata${RELOCATING+ .rel.tdata.* .rel.gnu.linkonce.td.*}) }
  383. .rela.tdata ${RELOCATING-0} : { *(.rela.tdata${RELOCATING+ .rela.tdata.* .rela.gnu.linkonce.td.*}) }
  384. .rel.tbss ${RELOCATING-0} : { *(.rel.tbss${RELOCATING+ .rel.tbss.* .rel.gnu.linkonce.tb.*}) }
  385. .rela.tbss ${RELOCATING-0} : { *(.rela.tbss${RELOCATING+ .rela.tbss.* .rela.gnu.linkonce.tb.*}) }
  386. .rel.ctors ${RELOCATING-0} : { *(.rel.ctors) }
  387. .rela.ctors ${RELOCATING-0} : { *(.rela.ctors) }
  388. .rel.dtors ${RELOCATING-0} : { *(.rel.dtors) }
  389. .rela.dtors ${RELOCATING-0} : { *(.rela.dtors) }
  390. .rel.got ${RELOCATING-0} : { *(.rel.got) }
  391. .rela.got ${RELOCATING-0} : { *(.rela.got) }
  392. ${OTHER_GOT_RELOC_SECTIONS}
  393. ${REL_SDATA}
  394. ${REL_SBSS}
  395. ${REL_SDATA2}
  396. ${REL_SBSS2}
  397. .rel.${BSS_NAME} ${RELOCATING-0} : { *(.rel.${BSS_NAME}${RELOCATING+ .rel.${BSS_NAME}.* .rel.gnu.linkonce.b.*}) }
  398. .rela.${BSS_NAME} ${RELOCATING-0} : { *(.rela.${BSS_NAME}${RELOCATING+ .rela.${BSS_NAME}.* .rela.gnu.linkonce.b.*}) }
  399. ${REL_LARGE}
  400. ${IREL_IN_PLT+$REL_IFUNC}
  401. ${IREL_IN_PLT+$RELA_IFUNC}
  402. ${IREL_IN_PLT-$REL_IPLT}
  403. ${IREL_IN_PLT-$RELA_IPLT}
  404. EOF
  405. if [ -n "$COMBRELOC" ]; then
  406. cat >> ldscripts/dyntmp.$$ <<EOF
  407. .rel.dyn ${RELOCATING-0} :
  408. {
  409. EOF
  410. sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rela\./d;/__rela_iplt_/d;s/^.*: { *\(.*\)}$/ \1/' $COMBRELOC >> ldscripts/dyntmp.$$
  411. cat >> ldscripts/dyntmp.$$ <<EOF
  412. }
  413. .rela.dyn ${RELOCATING-0} :
  414. {
  415. EOF
  416. sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rel\./d;/__rel_iplt_/d;s/^.*: { *\(.*\)}/ \1/' $COMBRELOC >> ldscripts/dyntmp.$$
  417. cat >> ldscripts/dyntmp.$$ <<EOF
  418. }
  419. EOF
  420. fi
  421. cat >> ldscripts/dyntmp.$$ <<EOF
  422. .rel.plt ${RELOCATING-0} :
  423. {
  424. *(.rel.plt)
  425. ${IREL_IN_PLT+${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_start = .);}}}
  426. ${IREL_IN_PLT+${RELOCATING+*(.rel.iplt)}}
  427. ${IREL_IN_PLT+${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_end = .);}}}
  428. }
  429. .rela.plt ${RELOCATING-0} :
  430. {
  431. *(.rela.plt)
  432. ${IREL_IN_PLT+${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_start = .);}}}
  433. ${IREL_IN_PLT+${RELOCATING+*(.rela.iplt)}}
  434. ${IREL_IN_PLT+${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_end = .);}}}
  435. }
  436. ${OTHER_PLT_RELOC_SECTIONS}
  437. EOF
  438. emit_dyn()
  439. {
  440. if test -z "${NO_REL_RELOCS}${NO_RELA_RELOCS}"; then
  441. cat ldscripts/dyntmp.$$
  442. else
  443. if test -z "${NO_REL_RELOCS}"; then
  444. sed -e '/^[ ]*\.rela\.[^}]*$/,/}/d;/^[ ]*\.rela\./d;/__rela_iplt_/d' ldscripts/dyntmp.$$
  445. fi
  446. if test -z "${NO_RELA_RELOCS}"; then
  447. sed -e '/^[ ]*\.rel\.[^}]*$/,/}/d;/^[ ]*\.rel\./d;/__rel_iplt_/d' ldscripts/dyntmp.$$
  448. fi
  449. fi
  450. rm -f ldscripts/dyntmp.$$
  451. }
  452. test -n "${NON_ALLOC_DYN}${SEPARATE_CODE}" || emit_dyn
  453. cat <<EOF
  454. .init ${RELOCATING-0}${RELOCATING+${INIT_ADDR}} :
  455. {
  456. ${RELOCATING+${INIT_START}}
  457. KEEP (*(SORT_NONE(.init)))
  458. ${RELOCATING+${INIT_END}}
  459. } ${FILL}
  460. ${TEXT_PLT+${PLT_NEXT_DATA-${PLT}}}
  461. ${TINY_READONLY_SECTION}
  462. .text ${RELOCATING-0} :
  463. {
  464. ${RELOCATING+${TEXT_START_SYMBOLS}}
  465. ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
  466. ${RELOCATING+*(.text.exit .text.exit.*)}
  467. ${RELOCATING+*(.text.startup .text.startup.*)}
  468. ${RELOCATING+*(.text.hot .text.hot.*)}
  469. *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
  470. /* .gnu.warning sections are handled specially by elf32.em. */
  471. *(.gnu.warning)
  472. ${RELOCATING+${OTHER_TEXT_SECTIONS}}
  473. } ${FILL}
  474. .fini ${RELOCATING-0}${RELOCATING+${FINI_ADDR}} :
  475. {
  476. ${RELOCATING+${FINI_START}}
  477. KEEP (*(SORT_NONE(.fini)))
  478. ${RELOCATING+${FINI_END}}
  479. } ${FILL}
  480. ${RELOCATING+PROVIDE (__${ETEXT_NAME} = .);}
  481. ${RELOCATING+PROVIDE (_${ETEXT_NAME} = .);}
  482. ${RELOCATING+PROVIDE (${ETEXT_NAME} = .);}
  483. EOF
  484. if test -n "${SEPARATE_CODE}"; then
  485. if test -n "${RODATA_ADDR}"; then
  486. RODATA_ADDR="\
  487. SEGMENT_START(\"rodata-segment\", ${RODATA_ADDR}) + SIZEOF_HEADERS"
  488. else
  489. RODATA_ADDR="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
  490. RODATA_ADDR="SEGMENT_START(\"rodata-segment\", ${RODATA_ADDR})"
  491. fi
  492. if test -n "${SHLIB_RODATA_ADDR}"; then
  493. SHLIB_RODATA_ADDR="\
  494. SEGMENT_START(\"rodata-segment\", ${SHLIB_RODATA_ADDR}) + SIZEOF_HEADERS"
  495. else
  496. SHLIB_RODATA_ADDR="SEGMENT_START(\"rodata-segment\", ${SHLIB_RODATA_ADDR})"
  497. SHLIB_RODATA_ADDR="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
  498. fi
  499. cat <<EOF
  500. /* Adjust the address for the rodata segment. We want to adjust up to
  501. the same address within the page on the next page up. */
  502. ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+. = ${RODATA_ADDR};}}}
  503. ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_RODATA_ADDR};}}
  504. ${CREATE_PIE+${RELOCATING+. = ${SHLIB_RODATA_ADDR};}}
  505. EOF
  506. emit_early_ro
  507. emit_dyn
  508. fi
  509. cat <<EOF
  510. ${WRITABLE_RODATA-${RODATA}}
  511. .${RODATA_NAME}1 ${RELOCATING-0} : { *(.${RODATA_NAME}1) }
  512. ${CREATE_SHLIB-${SDATA2}}
  513. ${CREATE_SHLIB-${SBSS2}}
  514. ${OTHER_READONLY_SECTIONS}
  515. .eh_frame_hdr : { *(.eh_frame_hdr) ${RELOCATING+*(.eh_frame_entry .eh_frame_entry.*)} }
  516. .eh_frame ${RELOCATING-0} : ONLY_IF_RO { KEEP (*(.eh_frame)) ${RELOCATING+*(.eh_frame.*)} }
  517. .gcc_except_table ${RELOCATING-0} : ONLY_IF_RO { *(.gcc_except_table
  518. .gcc_except_table.*) }
  519. .gnu_extab ${RELOCATING-0} : ONLY_IF_RO { *(.gnu_extab*) }
  520. /* These sections are generated by the Sun/Oracle C++ compiler. */
  521. .exception_ranges ${RELOCATING-0} : ONLY_IF_RO { *(.exception_ranges
  522. .exception_ranges*) }
  523. ${TEXT_PLT+${PLT_NEXT_DATA+${PLT}}}
  524. /* Adjust the address for the data segment. We want to adjust up to
  525. the same address within the page on the next page up. */
  526. ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+. = ${DATA_ADDR-${DATA_SEGMENT_ALIGN}};}}}
  527. ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_DATA_ADDR-${DATA_SEGMENT_ALIGN}};}}
  528. ${CREATE_PIE+${RELOCATING+. = ${SHLIB_DATA_ADDR-${DATA_SEGMENT_ALIGN}};}}
  529. /* Exception handling */
  530. .eh_frame ${RELOCATING-0} : ONLY_IF_RW { KEEP (*(.eh_frame)) ${RELOCATING+*(.eh_frame.*)} }
  531. .gnu_extab ${RELOCATING-0} : ONLY_IF_RW { *(.gnu_extab) }
  532. .gcc_except_table ${RELOCATING-0} : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }
  533. .exception_ranges ${RELOCATING-0} : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) }
  534. /* Thread Local Storage sections */
  535. .tdata ${RELOCATING-0} : { *(.tdata${RELOCATING+ .tdata.* .gnu.linkonce.td.*}) }
  536. .tbss ${RELOCATING-0} : { *(.tbss${RELOCATING+ .tbss.* .gnu.linkonce.tb.*})${RELOCATING+ *(.tcommon)} }
  537. .preinit_array ${RELOCATING-0} :
  538. {
  539. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__preinit_array_start = .);}}
  540. KEEP (*(.preinit_array))
  541. ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__preinit_array_end = .);}}
  542. }
  543. ${RELOCATING+${INIT_ARRAY}}
  544. ${RELOCATING+${FINI_ARRAY}}
  545. ${SMALL_DATA_CTOR-${RELOCATING+${CTOR}}}
  546. ${SMALL_DATA_DTOR-${RELOCATING+${DTOR}}}
  547. .jcr ${RELOCATING-0} : { KEEP (*(.jcr)) }
  548. ${RELOCATING+${DATARELRO}}
  549. ${OTHER_RELRO_SECTIONS}
  550. ${TEXT_DYNAMIC-${DYNAMIC}}
  551. ${OTHER_RELRO_SECTIONS_2}
  552. ${DATA_GOT+${RELRO_NOW+${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}}
  553. ${DATA_GOT+${RELRO_NOW+${GOT}}}
  554. ${DATA_GOT+${RELRO_NOW+${GOTPLT}}}
  555. ${DATA_GOT+${RELRO_NOW-${SEPARATE_GOTPLT+${GOT}}}}
  556. ${RELOCATING+${DATA_SEGMENT_RELRO_END}}
  557. ${INITIAL_READWRITE_SECTIONS}
  558. ${DATA_SDATA+${SDATA}}
  559. ${DATA_SDATA+${OTHER_SDATA_SECTIONS}}
  560. ${DATA_SDATA+${SBSS}}
  561. ${DATA_GOT+${RELRO_NOW-${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}}
  562. ${DATA_GOT+${RELRO_NOW-${SEPARATE_GOTPLT-${GOT}}}}
  563. ${DATA_GOT+${RELRO_NOW-${GOTPLT}}}
  564. ${DATA_PLT+${PLT_BEFORE_GOT-${PLT}}}
  565. .data ${RELOCATING-0} :
  566. {
  567. ${RELOCATING+${DATA_START_SYMBOLS}}
  568. *(.data${RELOCATING+ .data.* .gnu.linkonce.d.*})
  569. ${CONSTRUCTING+SORT(CONSTRUCTORS)}
  570. }
  571. .data1 ${RELOCATING-0} : { *(.data1) }
  572. ${WRITABLE_RODATA+${RODATA}}
  573. ${OTHER_READWRITE_SECTIONS}
  574. ${SMALL_DATA_CTOR+${RELOCATING+${CTOR}}}
  575. ${SMALL_DATA_DTOR+${RELOCATING+${DTOR}}}
  576. ${SDATA_GOT+${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}
  577. ${SDATA_GOT+${RELOCATING+${OTHER_GOT_SYMBOLS+. = .; ${OTHER_GOT_SYMBOLS}}}}
  578. ${SDATA_GOT+${GOT}}
  579. ${SDATA_GOT+${OTHER_GOT_SECTIONS}}
  580. ${DATA_SDATA-${SDATA}}
  581. ${DATA_SDATA-${OTHER_SDATA_SECTIONS}}
  582. ${RELOCATING+${DATA_END_SYMBOLS-${USER_LABEL_PREFIX}_edata = .; PROVIDE (${USER_LABEL_PREFIX}edata = .);}}
  583. ${RELOCATING+. = .;}
  584. ${RELOCATING+${USER_LABEL_PREFIX}__bss_start = .;}
  585. ${RELOCATING+${OTHER_BSS_SYMBOLS}}
  586. ${DATA_SDATA-${SBSS}}
  587. ${BSS_PLT+${PLT}}
  588. .${BSS_NAME} ${RELOCATING-0} :
  589. {
  590. *(.dyn${BSS_NAME})
  591. *(.${BSS_NAME}${RELOCATING+ .${BSS_NAME}.* .gnu.linkonce.b.*})
  592. *(COMMON)
  593. /* Align here to ensure that the .bss section occupies space up to
  594. _end. Align after .bss to ensure correct alignment even if the
  595. .bss section disappears because there are no input sections.
  596. FIXME: Why do we need it? When there is no .bss section, we don't
  597. pad the .data section. */
  598. ${RELOCATING+. = ALIGN(. != 0 ? ${ALIGNMENT} : 1);}
  599. }
  600. ${OTHER_BSS_SECTIONS}
  601. ${LARGE_BSS_AFTER_BSS+${LARGE_BSS}}
  602. ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
  603. ${RELOCATING+. = ALIGN(${ALIGNMENT});}
  604. EOF
  605. LARGE_DATA_ADDR=". = SEGMENT_START(\"ldata-segment\", ${LARGE_DATA_ADDR-.});"
  606. SHLIB_LARGE_DATA_ADDR=". = SEGMENT_START(\"ldata-segment\", ${SHLIB_LARGE_DATA_ADDR-.});"
  607. cat <<EOF
  608. ${RELOCATING+${CREATE_SHLIB-${CREATE_PIE-${LARGE_DATA_ADDR}}}}
  609. ${RELOCATING+${CREATE_SHLIB+${SHLIB_LARGE_DATA_ADDR}}}
  610. ${RELOCATING+${CREATE_PIE+${SHLIB_LARGE_DATA_ADDR}}}
  611. ${LARGE_SECTIONS}
  612. ${LARGE_BSS_AFTER_BSS-${LARGE_BSS}}
  613. ${RELOCATING+. = ALIGN(${ALIGNMENT});}
  614. ${RELOCATING+${OTHER_END_SYMBOLS}}
  615. ${RELOCATING+${END_SYMBOLS-${USER_LABEL_PREFIX}_end = .; PROVIDE (${USER_LABEL_PREFIX}end = .);}}
  616. ${RELOCATING+${DATA_SEGMENT_END}}
  617. EOF
  618. test -z "${NON_ALLOC_DYN}" || emit_dyn
  619. cat <<EOF
  620. /* Stabs debugging sections. */
  621. .stab 0 : { *(.stab) }
  622. .stabstr 0 : { *(.stabstr) }
  623. .stab.excl 0 : { *(.stab.excl) }
  624. .stab.exclstr 0 : { *(.stab.exclstr) }
  625. .stab.index 0 : { *(.stab.index) }
  626. .stab.indexstr 0 : { *(.stab.indexstr) }
  627. .comment 0 : { *(.comment) }
  628. EOF
  629. . $srcdir/scripttempl/DWARF.sc
  630. cat <<EOF
  631. ${TINY_DATA_SECTION}
  632. ${TINY_BSS_SECTION}
  633. ${STACK_ADDR+${STACK}}
  634. ${ATTRS_SECTIONS}
  635. ${OTHER_SECTIONS}
  636. ${RELOCATING+${OTHER_SYMBOLS}}
  637. ${RELOCATING+${DISCARDED}}
  638. }
  639. EOF