2 # genscripts.sh - generate the ld-emulation-target specific files
3 # Copyright (C) 2004-2018 Free Software Foundation, Inc.
5 # This file is part of the Gnu Linker.
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)
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.
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
22 # Usage: genscripts_extra.sh \
33 # enable_initfini_array \
40 # genscripts_extra.sh \
45 # sparc-sun-sunos4.1.3 \
46 # sparc-sun-sunos4.1.3 \
47 # sparc-sun-sunos4.1.3 \
52 # sparc-sun-sunos4.1.3 \
55 # produces the linker scripts:
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"]
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"]
68 # It also produced the C source file:
72 # which is then compiled into the linker.
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.
92 ENABLE_INITFINI_ARRAY=$2
96 # Include the emulation-specific parameters:
97 CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${EMULATION_NAME}.sh"
98 . ${CUSTOMIZER_SCRIPT}
100 if test -d ldscripts; then
106 # Set some flags for the emultempl scripts. USE_LIBPATH will
107 # be set for any libpath-using emulation; NATIVE will be set for a
108 # libpath-using emulation where ${host} = ${target}. NATIVE
109 # may already have been set by the emulparams file, but that's OK
110 # (it'll just get set to "yes" twice).
112 case " $EMULATION_LIBPATH " in
113 *" ${EMULATION_NAME} "*)
114 if [ "x${host}" = "x${target}" ] ; then
117 elif [ "x${use_sysroot}" = "xyes" ] ; then
123 # If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
124 if test "x$NATIVE" = "xyes" ; then
128 # Set the library search path, for libraries named by -lfoo.
129 # If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
130 # Otherwise, the default is set here.
132 # The format is the usual list of colon-separated directories.
133 # To force a logically empty LIB_PATH, do LIBPATH=":".
135 # If we are using a sysroot, prefix library paths with "=" to indicate this.
137 # If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
138 # the library path with the suffix applied.
140 # Paths with LIBPATH_SUFFIX
142 # Paths without LIBPATH_SUFFIX
144 if [ "${LIB_PATH}" != ":" ] ; then
145 lib_path2=${LIB_PATH}
148 # Add args to lib_path1 and lib_path2, discarding any duplicates
153 # The "=" is harmless if we aren't using a sysroot, but also needless.
154 if [ "x${use_sysroot}" = "xyes" ] ; then
158 if test -n "${LIBPATH_SUFFIX}"; then
161 case :${lib_path1}: in
163 ::) lib_path1=${lib} ;;
164 *) lib_path1=${lib_path1}:${lib} ;;
167 if test -n "${LIBPATH_SUFFIX_SKIP}"; then
169 *${LIBPATH_SUFFIX_SKIP}) skip_lib=yes ;;
172 if test "${skip_lib}" = "no"; then
173 case :${lib_path1}: in
174 *:${lib}${LIBPATH_SUFFIX}:*) ;;
175 ::) lib_path1=${lib}${LIBPATH_SUFFIX} ;;
176 *) lib_path1=${lib_path1}:${lib}${LIBPATH_SUFFIX} ;;
181 if test "${skip_lib}" = "no"; then
182 case :${lib_path1}:${lib_path2}: in
184 *::) lib_path2=${lib} ;;
185 *) lib_path2=${lib_path2}:${lib} ;;
192 # Always search $(tooldir)/lib, aka /usr/local/TARGET/lib when native
193 # except when LIBPATH=":".
194 if [ "${LIB_PATH}" != ":" ] ; then
196 if [ "x${TOOL_LIB}" = "x" ] ; then
197 if [ "x${NATIVE}" = "xyes" ] ; then
198 libs="${exec_prefix}/${target_alias}/lib"
201 # For multilib'ed targets, ensure both ${target_alias}/lib${LIBPATH_SUFFIX}
202 # and ${TOOL_LIB}/lib${LIBPATH_SUFFIX} are in the default search path,
203 # because 64bit libraries may be in both places, depending on
204 # cross-development setup method (e.g.: /usr/s390x-linux/lib64
205 # vs. /usr/s390-linux/lib64)
206 case "${NATIVE}:${LIBPATH_SUFFIX}:${TOOL_LIB}" in
207 :* | *::* | *:*:*${LIBPATH_SUFFIX}) ;;
208 *) libs="${exec_prefix}/${target_alias}/lib${LIBPATH_SUFFIX}" ;;
210 libs="${exec_prefix}/${TOOL_LIB}/lib ${libs}"
212 append_to_lib_path ${libs}
215 if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
216 libs=${NATIVE_LIB_DIRS}
217 if [ "x${NATIVE}" = "xyes" ] ; then
220 *) libs="${libdir} ${libs}" ;;
223 append_to_lib_path ${libs}
226 case :${lib_path1}:${lib_path2}: in
227 *:: | ::*) LIB_PATH=${lib_path1}${lib_path2} ;;
228 *) LIB_PATH=${lib_path1}:${lib_path2} ;;
231 LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
233 # We need it for testsuite.
234 set $EMULATION_LIBPATH
235 if [ "x$1" = "x$EMULATION_NAME" ]; then
236 test -d tmpdir || mkdir tmpdir
237 rm -f tmpdir/libpath.exp
238 echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
241 # Generate 5 or 6 script files from a master script template in
242 # ${srcdir}/scripttempl/${SCRIPT_NAME}.sh. Which one of the 5 or 6
243 # script files is actually used depends on command line options given
244 # to ld. (SCRIPT_NAME was set in the emulparams_file.)
246 # A .x script file is the default script.
247 # A .xr script is for linking without relocation (-r flag).
248 # A .xu script is like .xr, but *do* create constructors (-Ur flag).
249 # A .xn script is for linking with -n flag (mix text and data on same page).
250 # A .xbn script is for linking with -N flag (mix text and data on same page).
251 # A .xs script is for generating a shared library with the --shared
252 # flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
253 # emulation parameters.
254 # A .xc script is for linking with -z combreloc; it is only generated if
255 # $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
256 # $SCRIPT_NAME is "elf".
257 # A .xsc script is for linking with --shared -z combreloc; it is generated
258 # if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
259 # $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
262 if [ "x$SCRIPT_NAME" = "xelf" ]; then
263 GENERATE_COMBRELOC_SCRIPT=yes
266 SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
268 # Determine DATA_ALIGNMENT for the 5 variants, using
269 # values specified in the emulparams/<script_to_run>.sh file or default.
271 DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
272 DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
273 DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
274 DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
275 DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
278 DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
279 DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
280 ( echo "/* Script for ld -r: link without relocation */"
281 . ${CUSTOMIZER_SCRIPT}
282 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
283 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr
286 DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
288 ( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
289 . ${CUSTOMIZER_SCRIPT}
290 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
291 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu
293 DATA_ALIGNMENT=${DATA_ALIGNMENT_}
296 ( echo "/* Default linker script, for normal executables */"
297 . ${CUSTOMIZER_SCRIPT}
298 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
299 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x
302 ( echo "/* Script for -z separate-code: generate normal executables with separate code segment */"
303 . ${CUSTOMIZER_SCRIPT}
304 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
305 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xe
308 DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
309 ( echo "/* Script for -n: mix text and data on same page */"
310 . ${CUSTOMIZER_SCRIPT}
311 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
312 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn
315 DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
316 ( echo "/* Script for -N: mix text and data on same page; don't align data */"
317 . ${CUSTOMIZER_SCRIPT}
318 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
319 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn
321 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
322 DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
324 COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
325 ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
326 . ${CUSTOMIZER_SCRIPT}
327 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
328 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc
331 COMBRELOC=ldscripts/${EMULATION_NAME}.xce.tmp
332 ( echo "/* Script for -z combreloc -z separate-code: combine and sort reloc sections with separate code segment */"
333 . ${CUSTOMIZER_SCRIPT}
334 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
335 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xce
339 COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
340 ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
341 . ${CUSTOMIZER_SCRIPT}
342 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
343 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw
346 COMBRELOC=ldscripts/${EMULATION_NAME}.xwe.tmp
347 ( echo "/* Script for -z combreloc -z now -z relro -z separate-code: combine and sort reloc sections with separate code segment */"
348 . ${CUSTOMIZER_SCRIPT}
349 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
350 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xwe
356 if test -n "$GENERATE_SHLIB_SCRIPT"; then
357 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
361 echo "/* Script for ld --shared: link shared library */"
362 . ${CUSTOMIZER_SCRIPT}
363 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
364 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs
365 LD_FLAG=sharedtextonly
367 echo "/* Script for ld --shared -z separate-code: link shared library with separate code segment */"
368 . ${CUSTOMIZER_SCRIPT}
369 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
370 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xse
371 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
372 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
374 COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
375 ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
376 . ${CUSTOMIZER_SCRIPT}
377 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
378 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc
380 LD_FLAG=csharedtextonly
381 COMBRELOC=ldscripts/${EMULATION_NAME}.xsce.tmp
382 ( echo "/* Script for --shared -z combreloc -z separate-code: shared library, combine & sort relocs with separate code segment */"
383 . ${CUSTOMIZER_SCRIPT}
384 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
385 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsce
389 COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
390 ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
391 . ${CUSTOMIZER_SCRIPT}
392 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
393 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw
395 LD_FLAG=wsharedtextonly
396 COMBRELOC=ldscripts/${EMULATION_NAME}.xswe.tmp
397 ( echo "/* Script for --shared -z combreloc -z now -z relro -z separate-code: shared library, combine & sort relocs with separate code segment */"
398 . ${CUSTOMIZER_SCRIPT}
399 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
400 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xswe
408 if test -n "$GENERATE_PIE_SCRIPT"; then
409 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
413 echo "/* Script for ld -pie: link position independent executable */"
414 . ${CUSTOMIZER_SCRIPT}
415 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
416 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd
419 echo "/* Script for ld -pie -z separate-code: link position independent executable with separate code segment */"
420 . ${CUSTOMIZER_SCRIPT}
421 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
422 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xde
423 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
424 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
425 COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
427 ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
428 . ${CUSTOMIZER_SCRIPT}
429 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
430 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc
433 COMBRELOC=ldscripts/${EMULATION_NAME}.xdce.tmp
434 ( echo "/* Script for -pie -z combreloc -z separate-code: position independent executable, combine & sort relocs with separate code segment */"
435 . ${CUSTOMIZER_SCRIPT}
436 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
437 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdce
441 COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
442 ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
443 . ${CUSTOMIZER_SCRIPT}
444 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
445 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw
448 COMBRELOC=ldscripts/${EMULATION_NAME}.xdwe.tmp
449 ( echo "/* Script for -pie -z combreloc -z now -z relro -z separate-code: position independent executable, combine & sort relocs with separate code segment */"
450 . ${CUSTOMIZER_SCRIPT}
451 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
452 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdwe
460 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT"; then
462 DATA_ALIGNMENT=${DATA_ALIGNMENT_}
464 echo "/* Script for ld --enable-auto-import: Like the default script except read only data is placed into .data */"
465 . ${CUSTOMIZER_SCRIPT}
466 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
467 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xa
470 case "$COMPILE_IN: $EMULATION_LIBPATH " in
471 :*" ${EMULATION_NAME} "*) COMPILE_IN=yes;;
475 # Determine if the shell has support for the variable BASH_LINENO.
476 # When it is the case, it is only available inside functions.
479 test "x$BASH_LINENO" != "x"
482 # Enable accruate error source in the compiler error messages, if possible.
484 . ${srcdir}/genscrba.sh
492 cat >> e${EMULATION_NAME}.c
496 # Generate e${EMULATION_NAME}.c.
497 # Start with an empty file, then the sourced .em script
498 # can use the "fragment" function to append.
499 > e${EMULATION_NAME}.c
500 source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em