2 # genscripts.sh - generate the ld-emulation-target specific files
3 # Copyright 2007 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 \
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"]
67 # It also produced the C source file:
71 # which is then compiled into the linker.
73 # The linker scripts are created by running the shell script
74 # /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME}
75 # (and any other variables it wants to). ${SCRIPT_NAME} is then
76 # invoked with a variable called ${LD_FLAG} to tell it which version
77 # of the linker script to create.
95 # Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
96 if [ "x${TOOL_LIB}" = "x" ] ; then
97 tool_lib=${exec_prefix}/${target_alias}/lib
99 tool_lib=${exec_prefix}/${TOOL_LIB}/lib
102 if [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
103 CUSTOMIZER_SCRIPT=${EMULATION_NAME}
105 CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
107 # Include the emulation-specific parameters:
108 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
110 if test -d ldscripts; then
116 # Set some flags for the emultempl scripts. USE_LIBPATH will
117 # be set for any libpath-using emulation; NATIVE will be set for a
118 # libpath-using emulation where ${host} = ${target}. NATIVE
119 # may already have been set by the emulparams file, but that's OK
120 # (it'll just get set to "yes" twice).
122 case " $EMULATION_LIBPATH " in
123 *" ${EMULATION_NAME} "*)
124 if [ "x${host}" = "x${target}" ] ; then
127 elif [ "x${use_sysroot}" = "xyes" ] ; then
133 # If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
134 if test "x$NATIVE" = "xyes" ; then
138 # Set the library search path, for libraries named by -lfoo.
139 # If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
140 # Otherwise, the default is set here.
142 # The format is the usual list of colon-separated directories.
143 # To force a logically empty LIB_PATH, do LIBPATH=":".
145 # If we are using a sysroot, prefix library paths with "=" to indicate this.
147 # If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
148 # the library path with the suffix applied.
150 if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
153 libs=${NATIVE_LIB_DIRS}
154 if [ "x${use_sysroot}" != "xyes" ] ; then
157 *) libs="${libdir} ${libs}" ;;
160 *" ${tool_lib} "*) ;;
161 *) libs="${tool_lib} ${libs}" ;;
165 for lib in ${libs}; do
166 # The "=" is harmless if we aren't using a sysroot, but also needless.
167 if [ "x${use_sysroot}" = "xyes" ] ; then
171 case "${LIBPATH_SUFFIX}:${lib}" in
173 *:*${LIBPATH_SUFFIX}) ;;
176 if test -n "$addsuffix"; then
177 case :${LIB_PATH}: in
178 *:${lib}${LIBPATH_SUFFIX}:*) ;;
179 ::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;;
180 *) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;;
182 case :${LIB_PATH}:${LIB_PATH2}: in
184 *::) LIB_PATH2=${lib} ;;
185 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
188 case :${LIB_PATH2}: in
190 ::) LIB_PATH2=${lib} ;;
191 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
196 case :${LIB_PATH}:${LIB_PATH2}: in
197 *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
198 *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
202 # Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for
203 # sysrooted configurations and when LIBPATH=":".
204 if [ "x${use_sysroot}" != "xyes" ] ; then
205 case :${LIB_PATH}: in
206 ::: | *:${tool_lib}:*) ;;
207 ::) LIB_PATH=${tool_lib} ;;
208 *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
210 # For multilib targets, search both $tool_lib dirs
211 if [ "x${LIBPATH_SUFFIX}" != "x" ] ; then
212 case :${LIB_PATH}: in
213 ::: | *:${tool_lib}${LIBPATH_SUFFIX}:*) ;;
214 ::) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX} ;;
215 *) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX}:${LIB_PATH} ;;
220 LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
222 # We need it for testsuite.
223 set $EMULATION_LIBPATH
224 if [ "x$1" = "x$EMULATION_NAME" ]; then
225 test -d tmpdir || mkdir tmpdir
226 rm -f tmpdir/libpath.exp
227 echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
230 # Generate 5 or 6 script files from a master script template in
231 # ${srcdir}/scripttempl/${SCRIPT_NAME}.sh. Which one of the 5 or 6
232 # script files is actually used depends on command line options given
233 # to ld. (SCRIPT_NAME was set in the emulparams_file.)
235 # A .x script file is the default script.
236 # A .xr script is for linking without relocation (-r flag).
237 # A .xu script is like .xr, but *do* create constructors (-Ur flag).
238 # A .xn script is for linking with -n flag (mix text and data on same page).
239 # A .xbn script is for linking with -N flag (mix text and data on same page).
240 # A .xs script is for generating a shared library with the --shared
241 # flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
242 # emulation parameters.
243 # A .xc script is for linking with -z combreloc; it is only generated if
244 # $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
245 # $SCRIPT_NAME is "elf".
246 # A .xsc script is for linking with --shared -z combreloc; it is generated
247 # if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
248 # $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
251 if [ "x$SCRIPT_NAME" = "xelf" ]; then
252 GENERATE_COMBRELOC_SCRIPT=yes
255 SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
257 # Determine DATA_ALIGNMENT for the 5 variants, using
258 # values specified in the emulparams/<script_to_run>.sh file or default.
260 DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
261 DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
262 DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
263 DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
264 DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
267 DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
268 DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
269 ( echo "/* Script for ld -r: link without relocation */"
270 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
271 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
272 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr
275 DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
277 ( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
278 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
279 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
280 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu
283 DATA_ALIGNMENT=${DATA_ALIGNMENT_}
285 ( echo "/* Default linker script, for normal executables */"
286 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
287 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
288 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x
291 DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
292 TEXT_START_ADDR=${NONPAGED_TEXT_START_ADDR-${TEXT_START_ADDR}}
293 ( echo "/* Script for -n: mix text and data on same page */"
294 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
295 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
296 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn
299 DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
300 ( echo "/* Script for -N: mix text and data on same page; don't align data */"
301 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
302 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
303 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn
305 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
306 DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
308 COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
309 ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
310 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
311 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
312 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc
316 COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
317 ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
318 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
319 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
320 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw
326 if test -n "$GENERATE_SHLIB_SCRIPT"; then
328 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
330 # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
332 echo "/* Script for ld --shared: link shared library */"
333 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
334 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
335 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs
336 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
338 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
339 COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
340 ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
341 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
342 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
343 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc
347 COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
348 ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
349 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
350 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
351 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw
359 if test -n "$GENERATE_PIE_SCRIPT"; then
361 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
363 # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
365 echo "/* Script for ld -pie: link position independent executable */"
366 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
367 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
368 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd
369 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
371 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
372 COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
373 ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
374 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
375 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
376 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc
380 COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
381 ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
382 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
383 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
384 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw
392 case " $EMULATION_LIBPATH " in
393 *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
396 if test -n "${BASH+set}"; then
399 local current_script="$em_script"
402 em_script=$current_script
406 local lineno=$[${BASH_LINENO[0]} + 1]
407 echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\""
408 cat >> e${EMULATION_NAME}.c
417 cat >> e${EMULATION_NAME}.c
421 # Generate e${EMULATION_NAME}.c.
422 # Start with an empty file, then the sourced .em script
423 # can use the "fragment" function to append.
424 > e${EMULATION_NAME}.c
425 source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em