genscripts.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/bin/sh
  2. # genscripts.sh - generate the ld-emulation-target specific files
  3. # Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. #
  5. # This file is part of the Gnu Linker.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3, or (at your option)
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with GLD; see the file COPYING. If not, write to the Free
  19. # Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  20. # 02110-1301, USA.
  21. #
  22. # Usage: genscripts_extra.sh \
  23. # srcdir \
  24. # libdir \
  25. # prefix \
  26. # exec_prefix \
  27. # host \
  28. # target \
  29. # target_alias \
  30. # default_emulation \
  31. # native_lib_dirs \
  32. # use_sysroot \
  33. # enable_initfini_array \
  34. # this_emulation \
  35. # optional:
  36. # tool_dir
  37. #
  38. # Sample usage:
  39. #
  40. # genscripts_extra.sh \
  41. # /sources/ld \
  42. # /usr/local/lib \
  43. # /usr/local \
  44. # /usr/local \
  45. # sparc-sun-sunos4.1.3 \
  46. # sparc-sun-sunos4.1.3 \
  47. # sparc-sun-sunos4.1.3 \
  48. # sun4 \
  49. # "" \
  50. # no \
  51. # sun3 \
  52. # sparc-sun-sunos4.1.3 \
  53. # sparc.sh
  54. #
  55. # produces the linker scripts:
  56. #
  57. # sun3.x [default linker script]
  58. # sun3.xbn [used when the linker is invoked with "-N"]
  59. # sun3.xn [used when the linker is invoked with "-n"]
  60. # sun3.xr [used when the linker is invoked with "-r"]
  61. # sun3.xu [used when the linker is invoked with "-Ur"]
  62. # and maybe:
  63. # sun3.xc [used when the linker is invoked with "-z combreloc"]
  64. # sun3.xsc [used when the linker is invoked with "--shared"]
  65. # sun3.xdc [used when the linker is invoked with "-pie"]
  66. # sun3.xa [used when the linker is invoked with "--enable-auto-import"]
  67. #
  68. # It also produced the C source file:
  69. #
  70. # em_sun3.c
  71. #
  72. # which is then compiled into the linker.
  73. #
  74. # The linker scripts are created by running the shell script
  75. # /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME}
  76. # (and any other variables it wants to). ${SCRIPT_NAME} is then
  77. # invoked with a variable called ${LD_FLAG} to tell it which version
  78. # of the linker script to create.
  79. srcdir=$1
  80. libdir=$2
  81. prefix=$3
  82. exec_prefix=$4
  83. host=$5
  84. target=$6
  85. target_alias=$7
  86. EMULATION_LIBPATH=$8
  87. NATIVE_LIB_DIRS=$9
  88. shift 9
  89. use_sysroot=$1
  90. ENABLE_INITFINI_ARRAY=$2
  91. EMULATION_NAME=$3
  92. TOOL_LIB=$4
  93. # Include the emulation-specific parameters:
  94. CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${EMULATION_NAME}.sh"
  95. . ${CUSTOMIZER_SCRIPT}
  96. if test -d ldscripts; then
  97. true
  98. else
  99. mkdir ldscripts
  100. fi
  101. # Set some flags for the emultempl scripts. USE_LIBPATH will
  102. # be set for any libpath-using emulation; NATIVE will be set for a
  103. # libpath-using emulation where ${host} = ${target}. NATIVE
  104. # may already have been set by the emulparams file, but that's OK
  105. # (it'll just get set to "yes" twice).
  106. case " $EMULATION_LIBPATH " in
  107. *" ${EMULATION_NAME} "*)
  108. if [ "x${host}" = "x${target}" ] ; then
  109. NATIVE=yes
  110. USE_LIBPATH=yes
  111. elif [ "x${use_sysroot}" = "xyes" ] ; then
  112. USE_LIBPATH=yes
  113. fi
  114. ;;
  115. esac
  116. # If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
  117. if test "x$NATIVE" = "xyes" ; then
  118. USE_LIBPATH=yes
  119. fi
  120. # Set the library search path, for libraries named by -lfoo.
  121. # If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
  122. # Otherwise, the default is set here.
  123. #
  124. # The format is the usual list of colon-separated directories.
  125. # To force a logically empty LIB_PATH, do LIBPATH=":".
  126. #
  127. # If we are using a sysroot, prefix library paths with "=" to indicate this.
  128. #
  129. # If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
  130. # the library path with the suffix applied.
  131. # Paths with LIBPATH_SUFFIX
  132. lib_path1=
  133. # Paths without LIBPATH_SUFFIX
  134. lib_path2=
  135. if [ "${LIB_PATH}" != ":" ] ; then
  136. lib_path2=${LIB_PATH}
  137. fi
  138. # Add args to lib_path1 and lib_path2, discarding any duplicates
  139. append_to_lib_path()
  140. {
  141. if [ $# != 0 ]; then
  142. for lib in "$@"; do
  143. # The "=" is harmless if we aren't using a sysroot, but also needless.
  144. if [ "x${use_sysroot}" = "xyes" ] ; then
  145. lib="=${lib}"
  146. fi
  147. skip_lib=no
  148. if test -n "${LIBPATH_SUFFIX}"; then
  149. case "${lib}" in
  150. *${LIBPATH_SUFFIX})
  151. case :${lib_path1}: in
  152. *:${lib}:*) ;;
  153. ::) lib_path1=${lib} ;;
  154. *) lib_path1=${lib_path1}:${lib} ;;
  155. esac ;;
  156. *)
  157. if test -n "${LIBPATH_SUFFIX_SKIP}"; then
  158. case "${lib}" in
  159. *${LIBPATH_SUFFIX_SKIP}) skip_lib=yes ;;
  160. esac
  161. fi
  162. if test "${skip_lib}" = "no"; then
  163. case :${lib_path1}: in
  164. *:${lib}${LIBPATH_SUFFIX}:*) ;;
  165. ::) lib_path1=${lib}${LIBPATH_SUFFIX} ;;
  166. *) lib_path1=${lib_path1}:${lib}${LIBPATH_SUFFIX} ;;
  167. esac
  168. fi ;;
  169. esac
  170. fi
  171. if test "${skip_lib}" = "no"; then
  172. case :${lib_path1}:${lib_path2}: in
  173. *:${lib}:*) ;;
  174. *::) lib_path2=${lib} ;;
  175. *) lib_path2=${lib_path2}:${lib} ;;
  176. esac
  177. fi
  178. done
  179. fi
  180. }
  181. # Always search $(tooldir)/lib, aka /usr/local/TARGET/lib when native
  182. # except when LIBPATH=":".
  183. if [ "${LIB_PATH}" != ":" ] ; then
  184. libs=
  185. if [ "x${TOOL_LIB}" = "x" ] ; then
  186. if [ "x${NATIVE}" = "xyes" ] ; then
  187. libs="${exec_prefix}/${target_alias}/lib"
  188. fi
  189. else
  190. # For multilib'ed targets, ensure both ${target_alias}/lib${LIBPATH_SUFFIX}
  191. # and ${TOOL_LIB}/lib${LIBPATH_SUFFIX} are in the default search path,
  192. # because 64bit libraries may be in both places, depending on
  193. # cross-development setup method (e.g.: /usr/s390x-linux/lib64
  194. # vs. /usr/s390-linux/lib64)
  195. case "${NATIVE}:${LIBPATH_SUFFIX}:${TOOL_LIB}" in
  196. :* | *::* | *:*:*${LIBPATH_SUFFIX}) ;;
  197. *) libs="${exec_prefix}/${target_alias}/lib${LIBPATH_SUFFIX}" ;;
  198. esac
  199. libs="${exec_prefix}/${TOOL_LIB}/lib ${libs}"
  200. fi
  201. append_to_lib_path ${libs}
  202. fi
  203. if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
  204. libs=${NATIVE_LIB_DIRS}
  205. if [ "x${NATIVE}" = "xyes" ] ; then
  206. case " ${libs} " in
  207. *" ${libdir} "*) ;;
  208. *) libs="${libdir} ${libs}" ;;
  209. esac
  210. fi
  211. append_to_lib_path ${libs}
  212. fi
  213. case :${lib_path1}:${lib_path2}: in
  214. *:: | ::*) LIB_PATH=${lib_path1}${lib_path2} ;;
  215. *) LIB_PATH=${lib_path1}:${lib_path2} ;;
  216. esac
  217. LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
  218. # We need it for testsuite.
  219. set $EMULATION_LIBPATH
  220. if [ "x$1" = "x$EMULATION_NAME" ]; then
  221. test -d tmpdir || mkdir tmpdir
  222. rm -f tmpdir/libpath.exp
  223. echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
  224. fi
  225. # Generate 5 or 6 script files from a master script template in
  226. # ${srcdir}/scripttempl/${SCRIPT_NAME}.sh. Which one of the 5 or 6
  227. # script files is actually used depends on command line options given
  228. # to ld. (SCRIPT_NAME was set in the emulparams_file.)
  229. #
  230. # A .x script file is the default script.
  231. # A .xr script is for linking without relocation (-r flag).
  232. # A .xu script is like .xr, but *do* create constructors (-Ur flag).
  233. # A .xn script is for linking with -n flag (mix text and data on same page).
  234. # A .xbn script is for linking with -N flag (mix text and data on same page).
  235. # A .xs script is for generating a shared library with the --shared
  236. # flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
  237. # emulation parameters.
  238. # A .xc script is for linking with -z combreloc; it is only generated if
  239. # $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
  240. # $SCRIPT_NAME is "elf".
  241. # A .xsc script is for linking with --shared -z combreloc; it is generated
  242. # if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
  243. # $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
  244. # parameters too.
  245. if [ "x$SCRIPT_NAME" = "xelf" ]; then
  246. GENERATE_COMBRELOC_SCRIPT=yes
  247. fi
  248. SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
  249. # Determine DATA_ALIGNMENT for the 5 variants, using
  250. # values specified in the emulparams/<script_to_run>.sh file or default.
  251. DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
  252. DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
  253. DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
  254. DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
  255. DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
  256. LD_FLAG=r
  257. DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
  258. DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
  259. ( echo "/* Script for ld -r: link without relocation */"
  260. . ${CUSTOMIZER_SCRIPT}
  261. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  262. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr
  263. LD_FLAG=u
  264. DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
  265. CONSTRUCTING=" "
  266. ( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
  267. . ${CUSTOMIZER_SCRIPT}
  268. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  269. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu
  270. LD_FLAG=
  271. DATA_ALIGNMENT=${DATA_ALIGNMENT_}
  272. RELOCATING=" "
  273. ( echo "/* Default linker script, for normal executables */"
  274. . ${CUSTOMIZER_SCRIPT}
  275. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  276. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x
  277. LD_FLAG=n
  278. DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
  279. ( echo "/* Script for -n: mix text and data on same page */"
  280. . ${CUSTOMIZER_SCRIPT}
  281. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  282. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn
  283. LD_FLAG=N
  284. DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
  285. ( echo "/* Script for -N: mix text and data on same page; don't align data */"
  286. . ${CUSTOMIZER_SCRIPT}
  287. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  288. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn
  289. if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
  290. DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
  291. LD_FLAG=c
  292. COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
  293. ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
  294. . ${CUSTOMIZER_SCRIPT}
  295. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  296. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc
  297. rm -f ${COMBRELOC}
  298. LD_FLAG=w
  299. RELRO_NOW=" "
  300. COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
  301. ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
  302. . ${CUSTOMIZER_SCRIPT}
  303. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  304. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw
  305. rm -f ${COMBRELOC}
  306. COMBRELOC=
  307. unset RELRO_NOW
  308. fi
  309. if test -n "$GENERATE_SHLIB_SCRIPT"; then
  310. LD_FLAG=shared
  311. DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
  312. CREATE_SHLIB=" "
  313. (
  314. echo "/* Script for ld --shared: link shared library */"
  315. . ${CUSTOMIZER_SCRIPT}
  316. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  317. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs
  318. if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
  319. LD_FLAG=cshared
  320. DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
  321. COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
  322. ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
  323. . ${CUSTOMIZER_SCRIPT}
  324. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  325. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc
  326. rm -f ${COMBRELOC}
  327. LD_FLAG=wshared
  328. RELRO_NOW=" "
  329. COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
  330. ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
  331. . ${CUSTOMIZER_SCRIPT}
  332. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  333. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw
  334. rm -f ${COMBRELOC}
  335. COMBRELOC=
  336. unset RELRO_NOW
  337. fi
  338. unset CREATE_SHLIB
  339. fi
  340. if test -n "$GENERATE_PIE_SCRIPT"; then
  341. LD_FLAG=pie
  342. DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
  343. CREATE_PIE=" "
  344. (
  345. echo "/* Script for ld -pie: link position independent executable */"
  346. . ${CUSTOMIZER_SCRIPT}
  347. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  348. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd
  349. if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
  350. LD_FLAG=cpie
  351. DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
  352. COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
  353. ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
  354. . ${CUSTOMIZER_SCRIPT}
  355. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  356. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc
  357. rm -f ${COMBRELOC}
  358. LD_FLAG=wpie
  359. RELRO_NOW=" "
  360. COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
  361. ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
  362. . ${CUSTOMIZER_SCRIPT}
  363. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  364. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw
  365. rm -f ${COMBRELOC}
  366. COMBRELOC=
  367. unset RELRO_NOW
  368. fi
  369. unset CREATE_PIE
  370. fi
  371. if test -n "$GENERATE_AUTO_IMPORT_SCRIPT"; then
  372. LD_FLAG=auto_import
  373. DATA_ALIGNMENT=${DATA_ALIGNMENT_}
  374. (
  375. echo "/* Script for ld --enable-auto-import: Like the default script except read only data is placed into .data */"
  376. . ${CUSTOMIZER_SCRIPT}
  377. . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
  378. ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xa
  379. fi
  380. case "$COMPILE_IN: $EMULATION_LIBPATH " in
  381. :*" ${EMULATION_NAME} "*) COMPILE_IN=yes;;
  382. esac
  383. # PR ld/5652:
  384. # Determine if the shell has support for the variable BASH_LINENO.
  385. # When it is the case, it is only available inside functions.
  386. has_lineno()
  387. {
  388. test "x$BASH_LINENO" != "x"
  389. }
  390. # Enable accruate error source in the compiler error messages, if possible.
  391. if has_lineno; then
  392. . ${srcdir}/genscrba.sh
  393. else
  394. source_em()
  395. {
  396. . $1
  397. }
  398. fragment()
  399. {
  400. cat >> e${EMULATION_NAME}.c
  401. }
  402. fi
  403. # Generate e${EMULATION_NAME}.c.
  404. # Start with an empty file, then the sourced .em script
  405. # can use the "fragment" function to append.
  406. > e${EMULATION_NAME}.c
  407. source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em