genscripts.sh revision 130561
1114402Sru#!/bin/sh
2114402Sru# genscripts.sh - generate the ld-emulation-target specific files
3114402Sru#
4114402Sru# Usage: genscripts_extra.sh \
5114402Sru#          srcdir \
6114402Sru#          libdir \
7114402Sru#          exec_prefix \
8114402Sru#          host \
9114402Sru#          target \
10114402Sru#          target_alias \
11114402Sru#          default_emulation \
12114402Sru#          native_lib_dirs \
13114402Sru#          use_sysroot \
14114402Sru#          this_emulation \
15114402Sru# optional:
16114402Sru#          tool_dir \
17114402Sru#          customizer_script
18114402Sru#
19114402Sru# Sample usage:
20114402Sru#
21114402Sru#   genscripts_extra.sh \
22114402Sru#    /sources/ld \
23114402Sru#    /usr/local/lib \
24114402Sru#    /usr/local \
25114402Sru#    sparc-sun-sunos4.1.3 \
26114402Sru#    sparc-sun-sunos4.1.3 \
27114402Sru#    sparc-sun-sunos4.1.3 \
28114402Sru#    sun4 \
29114402Sru#    "" \
30114402Sru#    no \
31114402Sru#    sun3 \
32114402Sru#    sparc-sun-sunos4.1.3 \
33114402Sru#    sparc.sh
34114402Sru#
35114402Sru# produces the linker scripts:
36114402Sru#
37114402Sru#   sun3.x       [default linker script]
38114402Sru#   sun3.xbn     [used when the linker is invoked with "-N"]
39114402Sru#   sun3.xn      [used when the linker is invoked with "-n"]
40114402Sru#   sun3.xr      [used when the linker is invoked with "-r"]
41114402Sru#   sun3.xu      [used when the linker is invoked with "-Ur"]
42114402Sru# and maybe:
43114402Sru#   sun3.xc      [used when the linker is invoked with "-z combreloc"]
44114402Sru#   sun3.xsc     [used when the linker is invoked with "--shared"]
45114402Sru#   sun3.xdc     [used when the linker is invoked with "-pie"]
46114402Sru#
47114402Sru# It also produced the C source file:
48114402Sru#
49114402Sru#   em_sun3.c
50114402Sru#
51114402Sru# which is then compiled into the linker.
52114402Sru#
53114402Sru# The linker scripts are created by running the shell script
54114402Sru# /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME}
55114402Sru# (and any other variables it wants to).  ${SCRIPT_NAME} is then
56114402Sru# invoked with a variable called ${LD_FLAG} to tell it which version
57114402Sru# of the linker script to create.
58114402Sru
59114402Sru
60114402Srusrcdir=$1
61114402Srulibdir=$2
62114402Sruexec_prefix=$3
63114402Sruhost=$4
64114402Srutarget=$5
65114402Srutarget_alias=$6
66114402SruEMULATION_LIBPATH=$7
67114402SruNATIVE_LIB_DIRS=$8
68114402Sruuse_sysroot=$9
69114402Srushift 9
70114402SruEMULATION_NAME=$1
71114402SruTOOL_LIB=$2
72114402SruCUSTOMIZER_SCRIPT=$3
73114402Sru
74114402Sru# Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
75114402Sruif [ "x${TOOL_LIB}" = "x" ] ; then
76114402Sru  tool_lib=${exec_prefix}/${target_alias}/lib
77114402Sruelse
78114402Sru  tool_lib=${exec_prefix}/${TOOL_LIB}/lib
79114402Srufi
80114402Sru
81114402Sruif [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
82114402Sru  CUSTOMIZER_SCRIPT=${EMULATION_NAME}
83114402Srufi
84114402SruCUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
85114402Sru
86114402Sru# Include the emulation-specific parameters:
87114402Sru. ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
88114402Sru
89114402Sruif test -d ldscripts; then
90114402Sru  true
91114402Sruelse
92114402Sru  mkdir ldscripts
93114402Srufi
94114402Sru
95114402Sru# Set some flags for the emultempl scripts.  USE_LIBPATH will
96114402Sru# be set for any libpath-using emulation; NATIVE will be set for a
97114402Sru# libpath-using emulation where ${host} = ${target}.  NATIVE
98114402Sru# may already have been set by the emulparams file, but that's OK
99114402Sru# (it'll just get set to "yes" twice).
100114402Sru
101114402Srucase " $EMULATION_LIBPATH " in
102114402Sru  *" ${EMULATION_NAME} "*)
103114402Sru    if [ "x${host}" = "x${target}" ] ; then
104114402Sru      NATIVE=yes
105114402Sru      USE_LIBPATH=yes
106114402Sru    elif [ "x${use_sysroot}" = "xyes" ] ; then
107114402Sru      USE_LIBPATH=yes
108114402Sru    fi
109114402Sru    ;;
110114402Sruesac
111114402Sru
112114402Sru# If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
113114402Sruif test "x$NATIVE" = "xyes" ; then
114114402Sru  USE_LIBPATH=yes
115114402Srufi
116114402Sru
117114402Sru# Set the library search path, for libraries named by -lfoo.
118114402Sru# If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
119114402Sru# Otherwise, the default is set here.
120114402Sru#
121114402Sru# The format is the usual list of colon-separated directories.
122114402Sru# To force a logically empty LIB_PATH, do LIBPATH=":".
123114402Sru#
124114402Sru# If we are using a sysroot, prefix library paths with "=" to indicate this.
125114402Sru#
126114402Sru# If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
127114402Sru# the library path with the suffix applied.
128114402Sru
129114402Sruif [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
130114402Sru  LIB_PATH2=
131114402Sru
132114402Sru  libs=${NATIVE_LIB_DIRS}
133114402Sru  if [ "x${use_sysroot}" != "xyes" ] ; then
134114402Sru    case " ${libs} " in
135114402Sru      *" ${libdir} "*) ;;
136114402Sru      *) libs="${libdir} ${libs}" ;;
137114402Sru    esac
138114402Sru    case " ${libs} " in
139114402Sru      *" ${tool_lib} "*) ;;
140114402Sru      *) libs="${tool_lib} ${libs}" ;;
141114402Sru    esac
142114402Sru  fi
143114402Sru
144114402Sru  for lib in ${libs}; do
145114402Sru    # The "=" is harmless if we aren't using a sysroot, but also needless.
146114402Sru    if [ "x${use_sysroot}" = "xyes" ] ; then
147114402Sru      lib="=${lib}"
148114402Sru    fi
149114402Sru    addsuffix=
150114402Sru    case "${LIBPATH_SUFFIX}:${lib}" in
151114402Sru      :*) ;;
152114402Sru      *:*${LIBPATH_SUFFIX}) ;;
153114402Sru      *) addsuffix=yes ;;
154114402Sru    esac
155114402Sru    if test -n "$addsuffix"; then
156114402Sru      case :${LIB_PATH}: in
157114402Sru	*:${lib}${LIBPATH_SUFFIX}:*) ;;
158114402Sru	::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;;
159114402Sru	*) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;;
160114402Sru      esac
161114402Sru      case :${LIB_PATH}:${LIB_PATH2}: in
162114402Sru	*:${lib}:*) ;;
163114402Sru	*::) LIB_PATH2=${lib} ;;
164114402Sru	*) LIB_PATH2=${LIB_PATH2}:${lib} ;;
165114402Sru      esac
166114402Sru    else
167114402Sru      case :${LIB_PATH2}: in
168114402Sru	*:${lib}:*) ;;
169114402Sru	::) LIB_PATH2=${lib} ;;
170114402Sru	*) LIB_PATH2=${LIB_PATH2}:${lib} ;;
171114402Sru      esac
172114402Sru    fi
173114402Sru  done
174114402Sru
175114402Sru  case :${LIB_PATH}:${LIB_PATH2}: in
176114402Sru    *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
177114402Sru    *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
178114402Sru  esac
179114402Srufi
180114402Sru
181114402Sru# Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for
182114402Sru# sysrooted configurations and when LIBPATH=":".
183114402Sruif [ "x${use_sysroot}" != "xyes" ] ; then
184114402Sru  case :${LIB_PATH}: in
185114402Sru  ::: | *:${tool_lib}:*) ;;
186114402Sru  ::) LIB_PATH=${tool_lib} ;;
187114402Sru  *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
188114402Sru  esac
189114402Srufi
190114402Sru
191114402SruLIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
192114402Sru
193114402Sru# We need it for testsuite.
194114402Srucase " $EMULATION_LIBPATH " in
195114402Sru  *" ${EMULATION_NAME} "*)
196114402Sru    test -d tmpdir || mkdir tmpdir
197114402Sru    test -f tmpdir/libpath.exp || \
198114402Sru    echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
199114402Sru    ;;
200114402Sruesac
201114402Sru
202114402Sru# Generate 5 or 6 script files from a master script template in
203114402Sru# ${srcdir}/scripttempl/${SCRIPT_NAME}.sh.  Which one of the 5 or 6
204114402Sru# script files is actually used depends on command line options given
205114402Sru# to ld.  (SCRIPT_NAME was set in the emulparams_file.)
206114402Sru#
207114402Sru# A .x script file is the default script.
208114402Sru# A .xr script is for linking without relocation (-r flag).
209114402Sru# A .xu script is like .xr, but *do* create constructors (-Ur flag).
210114402Sru# A .xn script is for linking with -n flag (mix text and data on same page).
211114402Sru# A .xbn script is for linking with -N flag (mix text and data on same page).
212114402Sru# A .xs script is for generating a shared library with the --shared
213114402Sru#   flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
214114402Sru#   emulation parameters.
215114402Sru# A .xc script is for linking with -z combreloc; it is only generated if
216114402Sru#   $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
217114402Sru#   $SCRIPT_NAME is "elf".
218114402Sru# A .xsc script is for linking with --shared -z combreloc; it is generated
219114402Sru#   if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
220114402Sru#   $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
221114402Sru#   parameters too.
222114402Sru
223114402Sruif [ "x$SCRIPT_NAME" = "xelf" ]; then
224114402Sru  GENERATE_COMBRELOC_SCRIPT=yes
225114402Srufi
226114402Sru
227114402SruSEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
228114402Sru
229114402Sru# Determine DATA_ALIGNMENT for the 5 variants, using
230114402Sru# values specified in the emulparams/<script_to_run>.sh file or default.
231114402Sru
232114402SruDATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
233114402SruDATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
234114402SruDATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
235114402SruDATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
236114402SruDATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
237114402Sru
238114402SruLD_FLAG=r
239114402SruDATA_ALIGNMENT=${DATA_ALIGNMENT_r}
240114402SruDEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
241114402Sru( echo "/* Script for ld -r: link without relocation */"
242114402Sru  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
243114402Sru  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
244114402Sru) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xr
245114402Sru
246114402SruLD_FLAG=u
247114402SruDATA_ALIGNMENT=${DATA_ALIGNMENT_u}
248114402SruCONSTRUCTING=" "
249114402Sru( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
250114402Sru  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
251114402Sru  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
252114402Sru) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xu
253114402Sru
254114402SruLD_FLAG=
255114402SruDATA_ALIGNMENT=${DATA_ALIGNMENT_}
256114402SruRELOCATING=" "
257114402Sru( echo "/* Default linker script, for normal executables */"
258114402Sru  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
259114402Sru  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
260114402Sru) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.x
261114402Sru
262114402SruLD_FLAG=n
263114402SruDATA_ALIGNMENT=${DATA_ALIGNMENT_n}
264114402SruTEXT_START_ADDR=${NONPAGED_TEXT_START_ADDR-${TEXT_START_ADDR}}
265114402Sru( echo "/* Script for -n: mix text and data on same page */"
266114402Sru  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
267114402Sru  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
268114402Sru) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xn
269114402Sru
270114402SruLD_FLAG=N
271114402SruDATA_ALIGNMENT=${DATA_ALIGNMENT_N}
272114402Sru( echo "/* Script for -N: mix text and data on same page; don't align data */"
273114402Sru  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
274114402Sru  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
275114402Sru) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xbn
276114402Sru
277114402Sruif test -n "$GENERATE_COMBRELOC_SCRIPT"; then
278114402Sru  DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
279114402Sru  LD_FLAG=c
280114402Sru  COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
281114402Sru  ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
282114402Sru    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
283114402Sru    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
284114402Sru  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xc
285114402Sru  rm -f ${COMBRELOC}
286114402Sru  COMBRELOC=
287114402Srufi
288114402Sru
289114402Sruif test -n "$GENERATE_SHLIB_SCRIPT"; then
290114402Sru  LD_FLAG=shared
291114402Sru  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
292114402Sru  CREATE_SHLIB=" "
293114402Sru  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
294114402Sru  (
295114402Sru    echo "/* Script for ld --shared: link shared library */"
296114402Sru    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
297114402Sru    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
298114402Sru  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xs
299114402Sru  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
300114402Sru    LD_FLAG=cshared
301114402Sru    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
302114402Sru    COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
303114402Sru    ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
304114402Sru      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
305114402Sru      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
306114402Sru    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xsc
307114402Sru    rm -f ${COMBRELOC}
308114402Sru    COMBRELOC=
309114402Sru  fi
310114402Sru  unset CREATE_SHLIB
311114402Srufi
312114402Sru
313114402Sruif test -n "$GENERATE_PIE_SCRIPT"; then
314114402Sru  LD_FLAG=pie
315114402Sru  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
316114402Sru  CREATE_PIE=" "
317114402Sru  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
318114402Sru  (
319114402Sru    echo "/* Script for ld -pie: link position independent executable */"
320114402Sru    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
321114402Sru    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
322114402Sru  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xd
323114402Sru  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
324114402Sru    LD_FLAG=cpie
325114402Sru    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
326114402Sru    COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
327114402Sru    ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
328114402Sru      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
329114402Sru      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
330114402Sru    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdc
331114402Sru    rm -f ${COMBRELOC}
332114402Sru    COMBRELOC=
333114402Sru  fi
334114402Sru  unset CREATE_PIE
335114402Srufi
336114402Sru
337114402Srucase " $EMULATION_LIBPATH " in
338114402Sru    *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
339114402Sruesac
340114402Sru
341114402Sru# Generate e${EMULATION_NAME}.c.
342114402Sru. ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em
343114402Sru