1#!/bin/sh
2# genscripts.sh - generate the ld-emulation-target specific files
3#
4# Usage: genscripts_extra.sh \
5#          srcdir \
6#          libdir \
7#          prefix \
8#          exec_prefix \
9#          host \
10#          target \
11#          target_alias \
12#          default_emulation \
13#          native_lib_dirs \
14#          use_sysroot \
15#          this_emulation \
16# optional:
17#          tool_dir \
18#          customizer_script
19#
20# Sample usage:
21#
22#   genscripts_extra.sh \
23#    /sources/ld \
24#    /usr/local/lib \
25#    /usr/local \
26#    /usr/local \
27#    sparc-sun-sunos4.1.3 \
28#    sparc-sun-sunos4.1.3 \
29#    sparc-sun-sunos4.1.3 \
30#    sun4 \
31#    "" \
32#    no \
33#    sun3 \
34#    sparc-sun-sunos4.1.3 \
35#    sparc.sh
36#
37# produces the linker scripts:
38#
39#   sun3.x       [default linker script]
40#   sun3.xbn     [used when the linker is invoked with "-N"]
41#   sun3.xn      [used when the linker is invoked with "-n"]
42#   sun3.xr      [used when the linker is invoked with "-r"]
43#   sun3.xu      [used when the linker is invoked with "-Ur"]
44# and maybe:
45#   sun3.xc      [used when the linker is invoked with "-z combreloc"]
46#   sun3.xsc     [used when the linker is invoked with "--shared"]
47#   sun3.xdc     [used when the linker is invoked with "-pie"]
48#
49# It also produced the C source file:
50#
51#   em_sun3.c
52#
53# which is then compiled into the linker.
54#
55# The linker scripts are created by running the shell script
56# /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME}
57# (and any other variables it wants to).  ${SCRIPT_NAME} is then
58# invoked with a variable called ${LD_FLAG} to tell it which version
59# of the linker script to create.
60
61
62srcdir=$1
63libdir=$2
64prefix=$3
65exec_prefix=$4
66host=$5
67target=$6
68target_alias=$7
69EMULATION_LIBPATH=$8
70NATIVE_LIB_DIRS=$9
71shift 9
72use_sysroot=$1
73EMULATION_NAME=$2
74TOOL_LIB=$3
75CUSTOMIZER_SCRIPT=$4
76
77# Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
78if [ "x${TOOL_LIB}" = "x" ] ; then
79  tool_lib=${exec_prefix}/${target_alias}/lib
80else
81  tool_lib=${exec_prefix}/${TOOL_LIB}/lib
82fi
83
84if [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
85  CUSTOMIZER_SCRIPT=${EMULATION_NAME}
86fi
87CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
88
89# Include the emulation-specific parameters:
90. ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
91
92if test -d ldscripts; then
93  true
94else
95  mkdir ldscripts
96fi
97
98# Set some flags for the emultempl scripts.  USE_LIBPATH will
99# be set for any libpath-using emulation; NATIVE will be set for a
100# libpath-using emulation where ${host} = ${target}.  NATIVE
101# may already have been set by the emulparams file, but that's OK
102# (it'll just get set to "yes" twice).
103
104case " $EMULATION_LIBPATH " in
105  *" ${EMULATION_NAME} "*)
106    if [ "x${host}" = "x${target}" ] ; then
107      NATIVE=yes
108      USE_LIBPATH=yes
109    elif [ "x${use_sysroot}" = "xyes" ] ; then
110      USE_LIBPATH=yes
111    fi
112    ;;
113esac
114
115# If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
116if test "x$NATIVE" = "xyes" ; then
117  USE_LIBPATH=yes
118fi
119
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
132if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
133  LIB_PATH2=
134
135  libs=${NATIVE_LIB_DIRS}
136  if [ "x${use_sysroot}" != "xyes" ] ; then
137    case " ${libs} " in
138      *" ${libdir} "*) ;;
139      *) libs="${libdir} ${libs}" ;;
140    esac
141    case " ${libs} " in
142      *" ${tool_lib} "*) ;;
143      *) libs="${tool_lib} ${libs}" ;;
144    esac
145  fi
146
147  for lib in ${libs}; do
148    # The "=" is harmless if we aren't using a sysroot, but also needless.
149    if [ "x${use_sysroot}" = "xyes" ] ; then
150      lib="=${lib}"
151    fi
152    addsuffix=
153    case "${LIBPATH_SUFFIX}:${lib}" in
154      :*) ;;
155      *:*${LIBPATH_SUFFIX}) ;;
156      *) addsuffix=yes ;;
157    esac
158    if test -n "$addsuffix"; then
159      case :${LIB_PATH}: in
160	*:${lib}${LIBPATH_SUFFIX}:*) ;;
161	::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;;
162	*) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;;
163      esac
164      case :${LIB_PATH}:${LIB_PATH2}: in
165	*:${lib}:*) ;;
166	*::) LIB_PATH2=${lib} ;;
167	*) LIB_PATH2=${LIB_PATH2}:${lib} ;;
168      esac
169    else
170      case :${LIB_PATH2}: in
171	*:${lib}:*) ;;
172	::) LIB_PATH2=${lib} ;;
173	*) LIB_PATH2=${LIB_PATH2}:${lib} ;;
174      esac
175    fi
176  done
177
178  case :${LIB_PATH}:${LIB_PATH2}: in
179    *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
180    *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
181  esac
182fi
183
184LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
185
186# We need it for testsuite.
187set $EMULATION_LIBPATH
188if [ "x$1" = "x$EMULATION_NAME" ]; then
189    test -d tmpdir || mkdir tmpdir
190    rm -f tmpdir/libpath.exp
191    echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
192fi
193
194# Generate 5 or 6 script files from a master script template in
195# ${srcdir}/scripttempl/${SCRIPT_NAME}.sh.  Which one of the 5 or 6
196# script files is actually used depends on command line options given
197# to ld.  (SCRIPT_NAME was set in the emulparams_file.)
198#
199# A .x script file is the default script.
200# A .xr script is for linking without relocation (-r flag).
201# A .xu script is like .xr, but *do* create constructors (-Ur flag).
202# A .xn script is for linking with -n flag (mix text and data on same page).
203# A .xbn script is for linking with -N flag (mix text and data on same page).
204# A .xs script is for generating a shared library with the --shared
205#   flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
206#   emulation parameters.
207# A .xc script is for linking with -z combreloc; it is only generated if
208#   $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
209#   $SCRIPT_NAME is "elf".
210# A .xsc script is for linking with --shared -z combreloc; it is generated
211#   if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
212#   $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
213#   parameters too.
214
215if [ "x$SCRIPT_NAME" = "xelf" ]; then
216  GENERATE_COMBRELOC_SCRIPT=yes
217fi
218
219SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
220
221# Determine DATA_ALIGNMENT for the 5 variants, using
222# values specified in the emulparams/<script_to_run>.sh file or default.
223
224DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
225DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
226DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
227DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
228DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
229
230LD_FLAG=r
231DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
232DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
233( echo "/* Script for ld -r: link without relocation */"
234  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
235  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
236) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xr
237
238LD_FLAG=u
239DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
240CONSTRUCTING=" "
241( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
242  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
243  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
244) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xu
245
246LD_FLAG=
247DATA_ALIGNMENT=${DATA_ALIGNMENT_}
248RELOCATING=" "
249( echo "/* Default linker script, for normal executables */"
250  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
251  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
252) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.x
253
254LD_FLAG=n
255DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
256TEXT_START_ADDR=${NONPAGED_TEXT_START_ADDR-${TEXT_START_ADDR}}
257( echo "/* Script for -n: mix text and data on same page */"
258  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
259  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
260) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xn
261
262LD_FLAG=N
263DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
264( echo "/* Script for -N: mix text and data on same page; don't align data */"
265  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
266  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
267) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xbn
268
269if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
270  DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
271  LD_FLAG=c
272  COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
273  ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
274    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
275    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
276  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xc
277  rm -f ${COMBRELOC}
278  LD_FLAG=w
279  RELRO_NOW=" "
280  COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
281  ( echo "/* Script for -z combreloc -z relro: combine and sort reloc sections */"
282    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
283    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
284  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xw
285  rm -f ${COMBRELOC}
286  COMBRELOC=
287  unset RELRO_NOW
288fi
289
290if test -n "$GENERATE_SHLIB_SCRIPT"; then
291  LD_FLAG=shared
292  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
293  CREATE_SHLIB=" "
294  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
295  (
296    echo "/* Script for ld --shared: link shared library */"
297    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
298    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
299  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xs
300  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
301    LD_FLAG=cshared
302    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
303    COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
304    ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
305      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
306      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
307    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xsc
308    rm -f ${COMBRELOC}
309    LD_FLAG=wshared
310    RELRO_NOW=" "
311    COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
312    ( echo "/* Script for --shared -z combreloc -z relro: shared library, combine & sort relocs */"
313      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
314      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
315    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xsw
316    rm -f ${COMBRELOC}
317    COMBRELOC=
318    unset RELRO_NOW
319  fi
320  unset CREATE_SHLIB
321fi
322
323if test -n "$GENERATE_PIE_SCRIPT"; then
324  LD_FLAG=pie
325  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
326  CREATE_PIE=" "
327  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
328  (
329    echo "/* Script for ld -pie: link position independent executable */"
330    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
331    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
332  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xd
333  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
334    LD_FLAG=cpie
335    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
336    COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
337    ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
338      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
339      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
340    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdc
341    rm -f ${COMBRELOC}
342    LD_FLAG=wpie
343    RELRO_NOW=" "
344    COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
345    ( echo "/* Script for -pie -z combreloc -z relro: position independent executable, combine & sort relocs */"
346      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
347      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
348    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdw
349    rm -f ${COMBRELOC}
350    LD_FLAG=Zcpie
351    ( echo "/* Script for -pie -z combreloc, -Z: position independent executable, combine & sort relocs, no PLT/GOT padding */"
352      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
353      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
354    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdcz
355    rm -f ${COMBRELOC}
356    COMBRELOC=
357    unset RELRO_NOW
358  fi
359  LD_FLAG=Zpie
360  DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
361  (
362    echo "/* Script for ld -pie -Z: link position independent executable, no PLT/GOT padding */"
363    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
364    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
365  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdz
366  unset CREATE_PIE
367fi
368
369LD_FLAG=Z
370DATA_ALIGNMENT=${DATA_ALIGNMENT_}
371RELOCATING=" "
372( echo "/* Script for -Z: traditional binaries with no PLT/GOT padding */"
373  . ${srcdir}/emulparams/${EMULATION_NAME}.sh
374  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
375) | sed -e '/^ *$/d;s/[        ]*$//' > ldscripts/${EMULATION_NAME}.xz
376
377case " $EMULATION_LIBPATH " in
378    *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
379esac
380
381# Generate e${EMULATION_NAME}.c.
382. ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em
383