PR linker/4844
[external/binutils.git] / ld / genscripts.sh
1 #!/bin/sh
2 # genscripts.sh - generate the ld-emulation-target specific files
3 # Copyright 2007 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 #          this_emulation \
34 # optional:
35 #          tool_dir \
36 #          customizer_script
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
80
81 srcdir=$1
82 libdir=$2
83 prefix=$3
84 exec_prefix=$4
85 host=$5
86 target=$6
87 target_alias=$7
88 EMULATION_LIBPATH=$8
89 NATIVE_LIB_DIRS=$9
90 shift 9
91 use_sysroot=$1
92 EMULATION_NAME=$2
93 TOOL_LIB=$3
94 CUSTOMIZER_SCRIPT=$4
95
96 # Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
97 if [ "x${TOOL_LIB}" = "x" ] ; then
98   tool_lib=${exec_prefix}/${target_alias}/lib
99 else
100   tool_lib=${exec_prefix}/${TOOL_LIB}/lib
101 fi
102
103 if [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
104   CUSTOMIZER_SCRIPT=${EMULATION_NAME}
105 fi
106 CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
107
108 # Include the emulation-specific parameters:
109 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
110
111 if test -d ldscripts; then
112   true
113 else
114   mkdir ldscripts
115 fi
116
117 # Set some flags for the emultempl scripts.  USE_LIBPATH will
118 # be set for any libpath-using emulation; NATIVE will be set for a
119 # libpath-using emulation where ${host} = ${target}.  NATIVE
120 # may already have been set by the emulparams file, but that's OK
121 # (it'll just get set to "yes" twice).
122
123 case " $EMULATION_LIBPATH " in
124   *" ${EMULATION_NAME} "*)
125     if [ "x${host}" = "x${target}" ] ; then
126       NATIVE=yes
127       USE_LIBPATH=yes
128     elif [ "x${use_sysroot}" = "xyes" ] ; then
129       USE_LIBPATH=yes
130     fi
131     ;;
132 esac
133
134 # If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
135 if test "x$NATIVE" = "xyes" ; then
136   USE_LIBPATH=yes
137 fi
138
139 # Set the library search path, for libraries named by -lfoo.
140 # If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
141 # Otherwise, the default is set here.
142 #
143 # The format is the usual list of colon-separated directories.
144 # To force a logically empty LIB_PATH, do LIBPATH=":".
145 #
146 # If we are using a sysroot, prefix library paths with "=" to indicate this.
147 #
148 # If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
149 # the library path with the suffix applied.
150
151 if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
152   LIB_PATH2=
153
154   libs=${NATIVE_LIB_DIRS}
155   if [ "x${use_sysroot}" != "xyes" ] ; then
156     case " ${libs} " in
157       *" ${libdir} "*) ;;
158       *) libs="${libdir} ${libs}" ;;
159     esac
160     case " ${libs} " in
161       *" ${tool_lib} "*) ;;
162       *) libs="${tool_lib} ${libs}" ;;
163     esac
164   fi
165
166   for lib in ${libs}; do
167     # The "=" is harmless if we aren't using a sysroot, but also needless.
168     if [ "x${use_sysroot}" = "xyes" ] ; then
169       lib="=${lib}"
170     fi
171     addsuffix=
172     case "${LIBPATH_SUFFIX}:${lib}" in
173       :*) ;;
174       *:*${LIBPATH_SUFFIX}) ;;
175       *) addsuffix=yes ;;
176     esac
177     if test -n "$addsuffix"; then
178       case :${LIB_PATH}: in
179         *:${lib}${LIBPATH_SUFFIX}:*) ;;
180         ::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;;
181         *) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;;
182       esac
183       case :${LIB_PATH}:${LIB_PATH2}: in
184         *:${lib}:*) ;;
185         *::) LIB_PATH2=${lib} ;;
186         *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
187       esac
188     else
189       case :${LIB_PATH2}: in
190         *:${lib}:*) ;;
191         ::) LIB_PATH2=${lib} ;;
192         *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
193       esac
194     fi
195   done
196
197   case :${LIB_PATH}:${LIB_PATH2}: in
198     *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
199     *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
200   esac
201 fi
202
203 # Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for
204 # sysrooted configurations and when LIBPATH=":".
205 if [ "x${use_sysroot}" != "xyes" ] ; then
206   case :${LIB_PATH}: in
207   ::: | *:${tool_lib}:*) ;;
208   ::) LIB_PATH=${tool_lib} ;;
209   *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
210   esac
211   # For multilib targets, search both $tool_lib dirs
212   if [ "x${LIBPATH_SUFFIX}" != "x" ] ; then
213     case :${LIB_PATH}: in
214       ::: | *:${tool_lib}${LIBPATH_SUFFIX}:*) ;;
215       ::) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX} ;;
216       *) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX}:${LIB_PATH} ;;
217     esac
218   fi
219 fi
220
221 LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
222
223 # We need it for testsuite.
224 set $EMULATION_LIBPATH
225 if [ "x$1" = "x$EMULATION_NAME" ]; then
226     test -d tmpdir || mkdir tmpdir
227     rm -f tmpdir/libpath.exp
228     echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
229 fi
230
231 # Generate 5 or 6 script files from a master script template in
232 # ${srcdir}/scripttempl/${SCRIPT_NAME}.sh.  Which one of the 5 or 6
233 # script files is actually used depends on command line options given
234 # to ld.  (SCRIPT_NAME was set in the emulparams_file.)
235 #
236 # A .x script file is the default script.
237 # A .xr script is for linking without relocation (-r flag).
238 # A .xu script is like .xr, but *do* create constructors (-Ur flag).
239 # A .xn script is for linking with -n flag (mix text and data on same page).
240 # A .xbn script is for linking with -N flag (mix text and data on same page).
241 # A .xs script is for generating a shared library with the --shared
242 #   flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
243 #   emulation parameters.
244 # A .xc script is for linking with -z combreloc; it is only generated if
245 #   $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
246 #   $SCRIPT_NAME is "elf".
247 # A .xsc script is for linking with --shared -z combreloc; it is generated
248 #   if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
249 #   $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
250 #   parameters too.
251
252 if [ "x$SCRIPT_NAME" = "xelf" ]; then
253   GENERATE_COMBRELOC_SCRIPT=yes
254 fi
255
256 SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
257
258 # Determine DATA_ALIGNMENT for the 5 variants, using
259 # values specified in the emulparams/<script_to_run>.sh file or default.
260
261 DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
262 DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
263 DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
264 DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
265 DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
266
267 LD_FLAG=r
268 DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
269 DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
270 ( echo "/* Script for ld -r: link without relocation */"
271   . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
272   . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
273 ) | sed -e '/^ *$/d;s/[         ]*$//' > ldscripts/${EMULATION_NAME}.xr
274
275 LD_FLAG=u
276 DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
277 CONSTRUCTING=" "
278 ( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
279   . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
280   . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
281 ) | sed -e '/^ *$/d;s/[         ]*$//' > ldscripts/${EMULATION_NAME}.xu
282
283 LD_FLAG=
284 DATA_ALIGNMENT=${DATA_ALIGNMENT_}
285 RELOCATING=" "
286 ( echo "/* Default linker script, for normal executables */"
287   . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
288   . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
289 ) | sed -e '/^ *$/d;s/[         ]*$//' > ldscripts/${EMULATION_NAME}.x
290
291 LD_FLAG=n
292 DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
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
297
298 LD_FLAG=N
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
304
305 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
306   DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
307   LD_FLAG=c
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
313   rm -f ${COMBRELOC}
314   LD_FLAG=w
315   RELRO_NOW=" "
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
321   rm -f ${COMBRELOC}
322   COMBRELOC=
323   unset RELRO_NOW
324 fi
325
326 if test -n "$GENERATE_SHLIB_SCRIPT"; then
327   LD_FLAG=shared
328   DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
329   CREATE_SHLIB=" "
330   (
331     echo "/* Script for ld --shared: link shared library */"
332     . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
333     . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
334   ) | sed -e '/^ *$/d;s/[       ]*$//' > ldscripts/${EMULATION_NAME}.xs
335   if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
336     LD_FLAG=cshared
337     DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
338     COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
339     ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
340       . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
341       . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
342     ) | sed -e '/^ *$/d;s/[     ]*$//' > ldscripts/${EMULATION_NAME}.xsc
343     rm -f ${COMBRELOC}
344     LD_FLAG=wshared
345     RELRO_NOW=" "
346     COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
347     ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
348       . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
349       . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
350     ) | sed -e '/^ *$/d;s/[     ]*$//' > ldscripts/${EMULATION_NAME}.xsw
351     rm -f ${COMBRELOC}
352     COMBRELOC=
353     unset RELRO_NOW
354   fi
355   unset CREATE_SHLIB
356 fi
357
358 if test -n "$GENERATE_PIE_SCRIPT"; then
359   LD_FLAG=pie
360   DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
361   CREATE_PIE=" "
362   (
363     echo "/* Script for ld -pie: link position independent executable */"
364     . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
365     . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
366   ) | sed -e '/^ *$/d;s/[       ]*$//' > ldscripts/${EMULATION_NAME}.xd
367   if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
368     LD_FLAG=cpie
369     DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
370     COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
371     ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
372       . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
373       . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
374     ) | sed -e '/^ *$/d;s/[     ]*$//' > ldscripts/${EMULATION_NAME}.xdc
375     rm -f ${COMBRELOC}
376     LD_FLAG=wpie
377     RELRO_NOW=" "
378     COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
379     ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
380       . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
381       . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
382     ) | sed -e '/^ *$/d;s/[     ]*$//' > ldscripts/${EMULATION_NAME}.xdw
383     rm -f ${COMBRELOC}
384     COMBRELOC=
385     unset RELRO_NOW
386   fi
387   unset CREATE_PIE
388 fi
389
390 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT"; then
391   LD_FLAG=auto_import
392   DATA_ALIGNMENT=${DATA_ALIGNMENT_}
393   (
394     echo "/* Script for ld --enable-auto-import: Like the default script except read only data is placed into .data  */"
395     . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
396     . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
397   ) | sed -e '/^ *$/d;s/[       ]*$//' > ldscripts/${EMULATION_NAME}.xa
398 fi
399
400 case " $EMULATION_LIBPATH " in
401     *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
402 esac
403
404 if test -n "${BASH+set}"; then
405   source_em()
406   {
407     local current_script="$em_script"
408     em_script=$1
409     . $em_script
410     em_script=$current_script
411   }
412   fragment()
413   {
414     local lineno=$[${BASH_LINENO[0]} + 1]
415     echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\""
416     cat >> e${EMULATION_NAME}.c
417   }
418 else
419   source_em()
420   {
421     . $1
422   }
423   fragment()
424   {
425     cat >> e${EMULATION_NAME}.c
426   }
427 fi
428
429 # Generate e${EMULATION_NAME}.c.
430 # Start with an empty file, then the sourced .em script
431 # can use the "fragment" function to append.
432 > e${EMULATION_NAME}.c
433 source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em