Fix comment.
[deliverable/binutils-gdb.git] / ld / genscripts.sh
CommitLineData
252b5132
RH
1#!/bin/sh
2# genscripts.sh - generate the ld-emulation-target specific files
f96b4a7b
NC
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.
252b5132 21#
8fbdf3ab
AM
22# Usage: genscripts_extra.sh \
23# srcdir \
24# libdir \
dfcffada 25# prefix \
8fbdf3ab
AM
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
252b5132
RH
37#
38# Sample usage:
8fbdf3ab
AM
39#
40# genscripts_extra.sh \
41# /sources/ld \
42# /usr/local/lib \
43# /usr/local \
dfcffada 44# /usr/local \
8fbdf3ab
AM
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#
67# It also produced the C source file:
68#
69# em_sun3.c
70#
71# which is then compiled into the linker.
72#
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.
78
252b5132
RH
79
80srcdir=$1
81libdir=$2
dfcffada
AM
82prefix=$3
83exec_prefix=$4
84host=$5
85target=$6
86target_alias=$7
87EMULATION_LIBPATH=$8
88NATIVE_LIB_DIRS=$9
d9fc3714 89shift 9
dfcffada
AM
90use_sysroot=$1
91EMULATION_NAME=$2
92TOOL_LIB=$3
93CUSTOMIZER_SCRIPT=$4
8fbdf3ab
AM
94
95# Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
96if [ "x${TOOL_LIB}" = "x" ] ; then
d9fc3714
AM
97 tool_lib=${exec_prefix}/${target_alias}/lib
98else
8fbdf3ab
AM
99 tool_lib=${exec_prefix}/${TOOL_LIB}/lib
100fi
101
102if [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
103 CUSTOMIZER_SCRIPT=${EMULATION_NAME}
d9fc3714 104fi
8fbdf3ab 105CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
252b5132
RH
106
107# Include the emulation-specific parameters:
8fbdf3ab 108. ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
252b5132
RH
109
110if test -d ldscripts; then
111 true
112else
113 mkdir ldscripts
114fi
115
9c8ebd6a
DJ
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).
121
122case " $EMULATION_LIBPATH " in
123 *" ${EMULATION_NAME} "*)
124 if [ "x${host}" = "x${target}" ] ; then
125 NATIVE=yes
126 USE_LIBPATH=yes
127 elif [ "x${use_sysroot}" = "xyes" ] ; then
128 USE_LIBPATH=yes
129 fi
130 ;;
131esac
132
133# If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
134if test "x$NATIVE" = "xyes" ; then
135 USE_LIBPATH=yes
136fi
137
252b5132
RH
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.
141#
142# The format is the usual list of colon-separated directories.
143# To force a logically empty LIB_PATH, do LIBPATH=":".
9c8ebd6a
DJ
144#
145# If we are using a sysroot, prefix library paths with "=" to indicate this.
146#
147# If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
148# the library path with the suffix applied.
252b5132 149
9c8ebd6a 150if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
f22f5fcd 151 LIB_PATH2=
1dd3bf68
AM
152
153 libs=${NATIVE_LIB_DIRS}
154 if [ "x${use_sysroot}" != "xyes" ] ; then
155 case " ${libs} " in
156 *" ${libdir} "*) ;;
586196d5 157 *) libs="${libdir} ${libs}" ;;
1dd3bf68
AM
158 esac
159 case " ${libs} " in
160 *" ${tool_lib} "*) ;;
586196d5 161 *) libs="${tool_lib} ${libs}" ;;
1dd3bf68 162 esac
9c8ebd6a 163 fi
1dd3bf68
AM
164
165 for lib in ${libs}; do
9c8ebd6a
DJ
166 # The "=" is harmless if we aren't using a sysroot, but also needless.
167 if [ "x${use_sysroot}" = "xyes" ] ; then
168 lib="=${lib}"
169 fi
d460ab58
JJ
170 addsuffix=
171 case "${LIBPATH_SUFFIX}:${lib}" in
172 :*) ;;
173 *:*${LIBPATH_SUFFIX}) ;;
174 *) addsuffix=yes ;;
3336653a 175 esac
d460ab58
JJ
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} ;;
181 esac
f22f5fcd 182 case :${LIB_PATH}:${LIB_PATH2}: in
d460ab58 183 *:${lib}:*) ;;
f22f5fcd
AM
184 *::) LIB_PATH2=${lib} ;;
185 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
d460ab58
JJ
186 esac
187 else
f22f5fcd
AM
188 case :${LIB_PATH2}: in
189 *:${lib}:*) ;;
190 ::) LIB_PATH2=${lib} ;;
191 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
d460ab58
JJ
192 esac
193 fi
9c8ebd6a 194 done
1dd3bf68 195
f22f5fcd
AM
196 case :${LIB_PATH}:${LIB_PATH2}: in
197 *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
198 *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
199 esac
252b5132
RH
200fi
201
9c8ebd6a 202# Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for
f22f5fcd 203# sysrooted configurations and when LIBPATH=":".
9c8ebd6a 204if [ "x${use_sysroot}" != "xyes" ] ; then
f22f5fcd
AM
205 case :${LIB_PATH}: in
206 ::: | *:${tool_lib}:*) ;;
207 ::) LIB_PATH=${tool_lib} ;;
208 *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
209 esac
33423b7f
MF
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} ;;
216 esac
217 fi
9c8ebd6a 218fi
252b5132 219
eb3d6bb8 220LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
252b5132 221
cdaeef2e 222# We need it for testsuite.
dc4f76f8
L
223set $EMULATION_LIBPATH
224if [ "x$1" = "x$EMULATION_NAME" ]; then
cdaeef2e 225 test -d tmpdir || mkdir tmpdir
dc4f76f8 226 rm -f tmpdir/libpath.exp
cdaeef2e 227 echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
dc4f76f8 228fi
cdaeef2e 229
252b5132
RH
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.)
234#
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.
db6751f2
JJ
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
249# parameters too.
250
251if [ "x$SCRIPT_NAME" = "xelf" ]; then
252 GENERATE_COMBRELOC_SCRIPT=yes
253fi
252b5132 254
1ddd7b13 255SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
252b5132
RH
256
257# Determine DATA_ALIGNMENT for the 5 variants, using
8fbdf3ab 258# values specified in the emulparams/<script_to_run>.sh file or default.
252b5132
RH
259
260DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
261DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
262DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
263DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
264DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
265
266LD_FLAG=r
267DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
268DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
cedd6b0d 269( echo "/* Script for ld -r: link without relocation */"
8fbdf3ab 270 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 271 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 272) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr
252b5132
RH
273
274LD_FLAG=u
275DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
276CONSTRUCTING=" "
cedd6b0d 277( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
8fbdf3ab 278 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 279 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 280) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu
252b5132
RH
281
282LD_FLAG=
283DATA_ALIGNMENT=${DATA_ALIGNMENT_}
284RELOCATING=" "
cedd6b0d 285( echo "/* Default linker script, for normal executables */"
8fbdf3ab 286 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 287 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 288) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x
252b5132
RH
289
290LD_FLAG=n
291DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
cedd6b0d 292( echo "/* Script for -n: mix text and data on same page */"
8fbdf3ab 293 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 294 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 295) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn
252b5132
RH
296
297LD_FLAG=N
298DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
cedd6b0d 299( echo "/* Script for -N: mix text and data on same page; don't align data */"
8fbdf3ab 300 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 301 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2
JJ
302) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn
303
304if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
305 DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
306 LD_FLAG=c
307 COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
e24d7c12 308 ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
8fbdf3ab 309 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
db6751f2
JJ
310 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
311 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc
312 rm -f ${COMBRELOC}
8c37241b
JJ
313 LD_FLAG=w
314 RELRO_NOW=" "
315 COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
316 ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
317 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
318 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
319 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw
320 rm -f ${COMBRELOC}
db6751f2 321 COMBRELOC=
8c37241b 322 unset RELRO_NOW
db6751f2 323fi
252b5132
RH
324
325if test -n "$GENERATE_SHLIB_SCRIPT"; then
326 LD_FLAG=shared
327 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
328 CREATE_SHLIB=" "
cedd6b0d
JB
329 (
330 echo "/* Script for ld --shared: link shared library */"
8fbdf3ab 331 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
179c732c 332 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2
JJ
333 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs
334 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
335 LD_FLAG=cshared
336 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
8c37241b 337 COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
cedd6b0d 338 ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
8fbdf3ab 339 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
db6751f2
JJ
340 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
341 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc
342 rm -f ${COMBRELOC}
8c37241b
JJ
343 LD_FLAG=wshared
344 RELRO_NOW=" "
345 COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
346 ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
347 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
348 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
349 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw
350 rm -f ${COMBRELOC}
db6751f2 351 COMBRELOC=
8c37241b 352 unset RELRO_NOW
db6751f2 353 fi
36af4a4e
JJ
354 unset CREATE_SHLIB
355fi
356
357if test -n "$GENERATE_PIE_SCRIPT"; then
358 LD_FLAG=pie
359 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
360 CREATE_PIE=" "
36af4a4e
JJ
361 (
362 echo "/* Script for ld -pie: link position independent executable */"
8fbdf3ab 363 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
36af4a4e
JJ
364 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
365 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd
366 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
367 LD_FLAG=cpie
368 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
8c37241b 369 COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
36af4a4e 370 ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
8fbdf3ab 371 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
36af4a4e
JJ
372 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
373 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc
374 rm -f ${COMBRELOC}
8c37241b
JJ
375 LD_FLAG=wpie
376 RELRO_NOW=" "
377 COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
378 ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
379 . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
380 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
381 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw
382 rm -f ${COMBRELOC}
36af4a4e 383 COMBRELOC=
8c37241b 384 unset RELRO_NOW
36af4a4e
JJ
385 fi
386 unset CREATE_PIE
252b5132
RH
387fi
388
eb3d6bb8
AM
389case " $EMULATION_LIBPATH " in
390 *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
391esac
252b5132 392
92b93329
RM
393if test -n "${BASH+set}"; then
394 source_em()
395 {
396 local current_script="$em_script"
397 em_script=$1
398 . $em_script
399 em_script=$current_script
400 }
401 fragment()
402 {
403 local lineno=$[${BASH_LINENO[0]} + 1]
404 echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\""
405 cat >> e${EMULATION_NAME}.c
406 }
407else
408 source_em()
409 {
410 . $1
411 }
412 fragment()
413 {
414 cat >> e${EMULATION_NAME}.c
415 }
416fi
417
252b5132 418# Generate e${EMULATION_NAME}.c.
92b93329
RM
419# Start with an empty file, then the sourced .em script
420# can use the "fragment" function to append.
421> e${EMULATION_NAME}.c
422source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em
This page took 0.335795 seconds and 4 git commands to generate.