Add SORT_BY_INIT_PRIORITY.
[deliverable/binutils-gdb.git] / ld / genscripts.sh
CommitLineData
252b5132
RH
1#!/bin/sh
2# genscripts.sh - generate the ld-emulation-target specific files
aa820537 3# Copyright 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
f96b4a7b
NC
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 \
02ecc8e9 33# enable_initfini_array \
8fbdf3ab
AM
34# this_emulation \
35# optional:
36# tool_dir \
37# customizer_script
252b5132
RH
38#
39# Sample usage:
8fbdf3ab
AM
40#
41# genscripts_extra.sh \
42# /sources/ld \
43# /usr/local/lib \
44# /usr/local \
dfcffada 45# /usr/local \
8fbdf3ab
AM
46# sparc-sun-sunos4.1.3 \
47# sparc-sun-sunos4.1.3 \
48# sparc-sun-sunos4.1.3 \
49# sun4 \
50# "" \
51# no \
52# sun3 \
53# sparc-sun-sunos4.1.3 \
54# sparc.sh
55#
56# produces the linker scripts:
57#
58# sun3.x [default linker script]
59# sun3.xbn [used when the linker is invoked with "-N"]
60# sun3.xn [used when the linker is invoked with "-n"]
61# sun3.xr [used when the linker is invoked with "-r"]
62# sun3.xu [used when the linker is invoked with "-Ur"]
63# and maybe:
64# sun3.xc [used when the linker is invoked with "-z combreloc"]
65# sun3.xsc [used when the linker is invoked with "--shared"]
66# sun3.xdc [used when the linker is invoked with "-pie"]
e2a83dd0 67# sun3.xa [used when the linker is invoked with "--enable-auto-import"]
8fbdf3ab
AM
68#
69# It also produced the C source file:
70#
71# em_sun3.c
72#
73# which is then compiled into the linker.
74#
75# The linker scripts are created by running the shell script
76# /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME}
77# (and any other variables it wants to). ${SCRIPT_NAME} is then
78# invoked with a variable called ${LD_FLAG} to tell it which version
79# of the linker script to create.
80
252b5132
RH
81
82srcdir=$1
83libdir=$2
dfcffada
AM
84prefix=$3
85exec_prefix=$4
86host=$5
87target=$6
88target_alias=$7
89EMULATION_LIBPATH=$8
90NATIVE_LIB_DIRS=$9
d9fc3714 91shift 9
dfcffada 92use_sysroot=$1
02ecc8e9
L
93ENABLE_INITFINI_ARRAY=$2
94EMULATION_NAME=$3
95TOOL_LIB=$4
96CUSTOMIZER_SCRIPT=$5
8fbdf3ab
AM
97
98# Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug.
99if [ "x${TOOL_LIB}" = "x" ] ; then
d9fc3714
AM
100 tool_lib=${exec_prefix}/${target_alias}/lib
101else
8fbdf3ab
AM
102 tool_lib=${exec_prefix}/${TOOL_LIB}/lib
103fi
104
105if [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then
106 CUSTOMIZER_SCRIPT=${EMULATION_NAME}
d9fc3714 107fi
8fbdf3ab 108CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh"
252b5132
RH
109
110# Include the emulation-specific parameters:
f50230ae 111. ${CUSTOMIZER_SCRIPT}
252b5132
RH
112
113if test -d ldscripts; then
114 true
115else
116 mkdir ldscripts
117fi
118
9c8ebd6a
DJ
119# Set some flags for the emultempl scripts. USE_LIBPATH will
120# be set for any libpath-using emulation; NATIVE will be set for a
121# libpath-using emulation where ${host} = ${target}. NATIVE
122# may already have been set by the emulparams file, but that's OK
123# (it'll just get set to "yes" twice).
124
125case " $EMULATION_LIBPATH " in
126 *" ${EMULATION_NAME} "*)
127 if [ "x${host}" = "x${target}" ] ; then
128 NATIVE=yes
129 USE_LIBPATH=yes
130 elif [ "x${use_sysroot}" = "xyes" ] ; then
131 USE_LIBPATH=yes
132 fi
133 ;;
134esac
135
136# If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also.
137if test "x$NATIVE" = "xyes" ; then
138 USE_LIBPATH=yes
139fi
140
252b5132
RH
141# Set the library search path, for libraries named by -lfoo.
142# If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
143# Otherwise, the default is set here.
144#
145# The format is the usual list of colon-separated directories.
146# To force a logically empty LIB_PATH, do LIBPATH=":".
9c8ebd6a
DJ
147#
148# If we are using a sysroot, prefix library paths with "=" to indicate this.
149#
150# If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of
151# the library path with the suffix applied.
252b5132 152
9c8ebd6a 153if [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then
f22f5fcd 154 LIB_PATH2=
1dd3bf68
AM
155
156 libs=${NATIVE_LIB_DIRS}
157 if [ "x${use_sysroot}" != "xyes" ] ; then
158 case " ${libs} " in
159 *" ${libdir} "*) ;;
586196d5 160 *) libs="${libdir} ${libs}" ;;
1dd3bf68
AM
161 esac
162 case " ${libs} " in
163 *" ${tool_lib} "*) ;;
586196d5 164 *) libs="${tool_lib} ${libs}" ;;
1dd3bf68 165 esac
9c8ebd6a 166 fi
1dd3bf68
AM
167
168 for lib in ${libs}; do
9c8ebd6a
DJ
169 # The "=" is harmless if we aren't using a sysroot, but also needless.
170 if [ "x${use_sysroot}" = "xyes" ] ; then
171 lib="=${lib}"
172 fi
d460ab58
JJ
173 addsuffix=
174 case "${LIBPATH_SUFFIX}:${lib}" in
175 :*) ;;
176 *:*${LIBPATH_SUFFIX}) ;;
177 *) addsuffix=yes ;;
3336653a 178 esac
d460ab58
JJ
179 if test -n "$addsuffix"; then
180 case :${LIB_PATH}: in
181 *:${lib}${LIBPATH_SUFFIX}:*) ;;
182 ::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;;
183 *) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;;
184 esac
f22f5fcd 185 case :${LIB_PATH}:${LIB_PATH2}: in
d460ab58 186 *:${lib}:*) ;;
f22f5fcd
AM
187 *::) LIB_PATH2=${lib} ;;
188 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
d460ab58
JJ
189 esac
190 else
f22f5fcd
AM
191 case :${LIB_PATH2}: in
192 *:${lib}:*) ;;
193 ::) LIB_PATH2=${lib} ;;
194 *) LIB_PATH2=${LIB_PATH2}:${lib} ;;
d460ab58
JJ
195 esac
196 fi
9c8ebd6a 197 done
1dd3bf68 198
f22f5fcd
AM
199 case :${LIB_PATH}:${LIB_PATH2}: in
200 *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;;
201 *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;;
202 esac
9b168ece
NC
203
204 # For multilib'ed targets, ensure both ${target_alias}/lib${LIBPATH_SUFFIX}
205 # and ${TOOL_LIB}/lib${LIBPATH_SUFFIX} are in the default search path, because
206 # 64bit libraries may be in both places, depending on cross-development
207 # setup method (e.g.: /usr/s390x-linux/lib64 vs /usr/s390-linux/lib64)
208 case "${LIBPATH_SUFFIX}:${tool_lib}" in
209 :*) ;;
210 *:*${LIBPATH_SUFFIX}) ;;
211 *)
212 paths="${exec_prefix}/${target_alias}/lib${LIBPATH_SUFFIX}"
213 if [ x"${TOOL_LIB}" != x ]; then
214 paths="${paths} ${exec_prefix}/${TOOL_LIB}/lib${LIBPATH_SUFFIX}"
215 fi
216 for path in $paths; do
217 case :${LIB_PATH}: in
218 ::: | *:${path}:*) ;;
219 *) LIB_PATH=${path}:${LIB_PATH} ;;
220 esac
221 done
222 ;;
223 esac
252b5132
RH
224fi
225
9c8ebd6a 226# Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for
f22f5fcd 227# sysrooted configurations and when LIBPATH=":".
9c8ebd6a 228if [ "x${use_sysroot}" != "xyes" ] ; then
f22f5fcd
AM
229 case :${LIB_PATH}: in
230 ::: | *:${tool_lib}:*) ;;
231 ::) LIB_PATH=${tool_lib} ;;
232 *) LIB_PATH=${tool_lib}:${LIB_PATH} ;;
233 esac
33423b7f
MF
234 # For multilib targets, search both $tool_lib dirs
235 if [ "x${LIBPATH_SUFFIX}" != "x" ] ; then
236 case :${LIB_PATH}: in
237 ::: | *:${tool_lib}${LIBPATH_SUFFIX}:*) ;;
238 ::) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX} ;;
239 *) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX}:${LIB_PATH} ;;
240 esac
241 fi
9c8ebd6a 242fi
252b5132 243
eb3d6bb8 244LIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'`
252b5132 245
cdaeef2e 246# We need it for testsuite.
dc4f76f8
L
247set $EMULATION_LIBPATH
248if [ "x$1" = "x$EMULATION_NAME" ]; then
cdaeef2e 249 test -d tmpdir || mkdir tmpdir
dc4f76f8 250 rm -f tmpdir/libpath.exp
cdaeef2e 251 echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp
dc4f76f8 252fi
cdaeef2e 253
252b5132
RH
254# Generate 5 or 6 script files from a master script template in
255# ${srcdir}/scripttempl/${SCRIPT_NAME}.sh. Which one of the 5 or 6
256# script files is actually used depends on command line options given
257# to ld. (SCRIPT_NAME was set in the emulparams_file.)
258#
259# A .x script file is the default script.
260# A .xr script is for linking without relocation (-r flag).
261# A .xu script is like .xr, but *do* create constructors (-Ur flag).
262# A .xn script is for linking with -n flag (mix text and data on same page).
263# A .xbn script is for linking with -N flag (mix text and data on same page).
264# A .xs script is for generating a shared library with the --shared
265# flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
266# emulation parameters.
db6751f2
JJ
267# A .xc script is for linking with -z combreloc; it is only generated if
268# $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
269# $SCRIPT_NAME is "elf".
270# A .xsc script is for linking with --shared -z combreloc; it is generated
271# if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
272# $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
273# parameters too.
274
275if [ "x$SCRIPT_NAME" = "xelf" ]; then
276 GENERATE_COMBRELOC_SCRIPT=yes
277fi
252b5132 278
1ddd7b13 279SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
252b5132
RH
280
281# Determine DATA_ALIGNMENT for the 5 variants, using
8fbdf3ab 282# values specified in the emulparams/<script_to_run>.sh file or default.
252b5132
RH
283
284DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
285DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
286DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
287DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
288DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
289
290LD_FLAG=r
291DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
292DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
cedd6b0d 293( echo "/* Script for ld -r: link without relocation */"
f50230ae 294 . ${CUSTOMIZER_SCRIPT}
179c732c 295 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 296) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr
252b5132
RH
297
298LD_FLAG=u
299DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
300CONSTRUCTING=" "
cedd6b0d 301( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
f50230ae 302 . ${CUSTOMIZER_SCRIPT}
179c732c 303 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 304) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu
252b5132
RH
305
306LD_FLAG=
307DATA_ALIGNMENT=${DATA_ALIGNMENT_}
308RELOCATING=" "
cedd6b0d 309( echo "/* Default linker script, for normal executables */"
f50230ae 310 . ${CUSTOMIZER_SCRIPT}
179c732c 311 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 312) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x
252b5132
RH
313
314LD_FLAG=n
315DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
cedd6b0d 316( echo "/* Script for -n: mix text and data on same page */"
f50230ae 317 . ${CUSTOMIZER_SCRIPT}
179c732c 318 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2 319) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn
252b5132
RH
320
321LD_FLAG=N
322DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
cedd6b0d 323( echo "/* Script for -N: mix text and data on same page; don't align data */"
f50230ae 324 . ${CUSTOMIZER_SCRIPT}
179c732c 325 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2
JJ
326) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn
327
328if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
329 DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
330 LD_FLAG=c
331 COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
e24d7c12 332 ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
f50230ae 333 . ${CUSTOMIZER_SCRIPT}
db6751f2
JJ
334 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
335 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc
336 rm -f ${COMBRELOC}
8c37241b
JJ
337 LD_FLAG=w
338 RELRO_NOW=" "
339 COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
340 ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
f50230ae 341 . ${CUSTOMIZER_SCRIPT}
8c37241b
JJ
342 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
343 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw
344 rm -f ${COMBRELOC}
db6751f2 345 COMBRELOC=
8c37241b 346 unset RELRO_NOW
db6751f2 347fi
252b5132
RH
348
349if test -n "$GENERATE_SHLIB_SCRIPT"; then
350 LD_FLAG=shared
351 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
352 CREATE_SHLIB=" "
cedd6b0d
JB
353 (
354 echo "/* Script for ld --shared: link shared library */"
f50230ae 355 . ${CUSTOMIZER_SCRIPT}
179c732c 356 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
db6751f2
JJ
357 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs
358 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
359 LD_FLAG=cshared
360 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
8c37241b 361 COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
cedd6b0d 362 ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
f50230ae 363 . ${CUSTOMIZER_SCRIPT}
db6751f2
JJ
364 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
365 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc
366 rm -f ${COMBRELOC}
8c37241b
JJ
367 LD_FLAG=wshared
368 RELRO_NOW=" "
369 COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
370 ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
f50230ae 371 . ${CUSTOMIZER_SCRIPT}
8c37241b
JJ
372 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
373 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw
374 rm -f ${COMBRELOC}
db6751f2 375 COMBRELOC=
8c37241b 376 unset RELRO_NOW
db6751f2 377 fi
36af4a4e
JJ
378 unset CREATE_SHLIB
379fi
380
381if test -n "$GENERATE_PIE_SCRIPT"; then
382 LD_FLAG=pie
383 DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
384 CREATE_PIE=" "
36af4a4e
JJ
385 (
386 echo "/* Script for ld -pie: link position independent executable */"
f50230ae 387 . ${CUSTOMIZER_SCRIPT}
36af4a4e
JJ
388 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
389 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd
390 if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
391 LD_FLAG=cpie
392 DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
8c37241b 393 COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
36af4a4e 394 ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
f50230ae 395 . ${CUSTOMIZER_SCRIPT}
36af4a4e
JJ
396 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
397 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc
398 rm -f ${COMBRELOC}
8c37241b
JJ
399 LD_FLAG=wpie
400 RELRO_NOW=" "
401 COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
402 ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
f50230ae 403 . ${CUSTOMIZER_SCRIPT}
8c37241b
JJ
404 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
405 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw
406 rm -f ${COMBRELOC}
36af4a4e 407 COMBRELOC=
8c37241b 408 unset RELRO_NOW
36af4a4e
JJ
409 fi
410 unset CREATE_PIE
252b5132
RH
411fi
412
e2a83dd0
NC
413if test -n "$GENERATE_AUTO_IMPORT_SCRIPT"; then
414 LD_FLAG=auto_import
415 DATA_ALIGNMENT=${DATA_ALIGNMENT_}
416 (
417 echo "/* Script for ld --enable-auto-import: Like the default script except read only data is placed into .data */"
f50230ae 418 . ${CUSTOMIZER_SCRIPT}
e2a83dd0
NC
419 . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
420 ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xa
421fi
422
eb3d6bb8
AM
423case " $EMULATION_LIBPATH " in
424 *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
425esac
252b5132 426
6e73d595
NC
427# PR ld/5652:
428# Determine if the shell has support for the variable BASH_LINENO.
429# When it is the case, it is only available inside functions.
430has_lineno()
431{
432 test "x$BASH_LINENO" != "x"
433}
434
435# Enable accruate error source in the compiler error messages, if possible.
436if has_lineno; then
c43cc9ff 437 . ${srcdir}/genscrba.sh
92b93329
RM
438else
439 source_em()
440 {
441 . $1
442 }
443 fragment()
444 {
445 cat >> e${EMULATION_NAME}.c
446 }
447fi
448
252b5132 449# Generate e${EMULATION_NAME}.c.
92b93329
RM
450# Start with an empty file, then the sourced .em script
451# can use the "fragment" function to append.
452> e${EMULATION_NAME}.c
453source_em ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em
This page took 0.497779 seconds and 4 git commands to generate.