1#!/bin/sh
2# genscripts.sh - generate the ld-emulation-target specific files
3#
4# Usage: genscripts.sh srcdir libdir host target target_alias \
5# default_emulation native_lib_dirs this_emulation
6#
7# Sample usage:
8# genscripts.sh /djm/ld-devo/devo/ld /usr/local/lib sparc-sun-sunos4.1.3 \
9# sparc-sun-sunos4.1.3 sparc-sun-sunos4.1.3 sun4 "" sun3 sparc-sun-sunos4.1.3
10# produces sun3.x sun3.xbn sun3.xn sun3.xr sun3.xu em_sun3.c
11#
12# $FreeBSD: releng/11.0/gnu/usr.bin/binutils/ld/genscripts.sh 289410 2015-10-16 05:54:41Z bdrewery $
13#
14# This is a cut-down version of the GNU script. Instead of jumping through
15# hoops for all possible combinations of paths, just use the libdir
16# argument in place of LIB_PATH.
17#
18# The exec_prefix, target_alias, use_sysroot, NATIVE_LIB_DIRS, TOOL_LIB, CUSTOMIZER_SCRIPT
19# arguments are not used in this version.
20#
21
22srcdir=$1
23libdir=$2
24exec_prefix=$3
25host=$4
26target=$5
27target_alias=$6
28EMULATION_LIBPATH=$7
29NATIVE_LIB_DIRS=$8
30use_sysroot=$9
31shift 9
32EMULATION_NAME=$1
33TOOL_LIB=$2
34CUSTOMIZER_SCRIPT=$3
35
36# Create the 'CUSTOMIZER_SCRIPT' knob to better sync this script with
37# FSF BU ver 2.15 which allows for a more generic emulparams processing.
38# To reduce the diff, I also include the ${EMULATION_NAME} parameter in uses
39# of 'CUSTOMIZER_SCRIPT'.
40
41# XXX: arm hack : until those file are merged back into the FSF repo, just
42# use the version in this directory.
43if !(test -f ${CUSTOMIZER_SCRIPT}"";) then
44CUSTOMIZER_SCRIPT="${srcdir}/emulparams/${EMULATION_NAME}.sh"
45fi
46
47# Include the emulation-specific parameters:
48. ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
49
50if test -d ldscripts; then
51  true
52else
53  mkdir -p ldscripts
54fi
55
56# Set some flags for the emultempl scripts.  USE_LIBPATH will
57# be set for any libpath-using emulation; NATIVE will be set for a
58# emulation to enable 'LD_LIBRARY_PATH=/foo:/bar ld -lfooz'
59    if [ "x${host}" = "x${target}" ] ; then
60      NATIVE=yes
61    fi
62  USE_LIBPATH=yes
63
64# Set the library search path, for libraries named by -lfoo.
65# If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used.
66# Otherwise, the default is set here.
67#
68# The format is the usual list of colon-separated directories.
69# To force a logically empty LIB_PATH, do LIBPATH=":".
70
71LIB_SEARCH_DIRS=`echo ${libdir} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\1);/g'`
72
73# Generate 5 or 6 script files from a master script template in
74# ${srcdir}/scripttempl/${SCRIPT_NAME}.sh.  Which one of the 5 or 6
75# script files is actually used depends on command line options given
76# to ld.  (SCRIPT_NAME was set in the emulparams_file.)
77#
78# A .x script file is the default script.
79# A .xr script is for linking without relocation (-r flag).
80# A .xu script is like .xr, but *do* create constructors (-Ur flag).
81# A .xn script is for linking with -n flag (mix text and data on same page).
82# A .xbn script is for linking with -N flag (mix text and data on same page).
83# A .xs script is for generating a shared library with the --shared
84#   flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the
85#   emulation parameters.
86# A .xc script is for linking with -z combreloc; it is only generated if
87#   $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
88#   $SCRIPT_NAME is "elf".
89# A .xsc script is for linking with --shared -z combreloc; it is generated
90#   if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or
91#   $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation
92#   parameters too.
93
94if [ "x$SCRIPT_NAME" = "xelf" ]; then
95  GENERATE_COMBRELOC_SCRIPT=yes
96fi
97
98SEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}}
99
100# Determine DATA_ALIGNMENT for the 5 variants, using
101# values specified in the emulparams/<script_to_run>.sh file or default.
102
103DATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}"
104DATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}"
105DATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}"
106DATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}"
107DATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}"
108
109LD_FLAG=r
110DATA_ALIGNMENT=${DATA_ALIGNMENT_r}
111DEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})"
112( echo "/* Script for ld -r: link without relocation */"
113  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
114  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
115) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xr
116
117LD_FLAG=u
118DATA_ALIGNMENT=${DATA_ALIGNMENT_u}
119CONSTRUCTING=" "
120( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */"
121  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
122  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
123) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xu
124
125LD_FLAG=
126DATA_ALIGNMENT=${DATA_ALIGNMENT_}
127RELOCATING=" "
128( echo "/* Default linker script, for normal executables */"
129  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
130  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
131) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.x
132
133LD_FLAG=n
134DATA_ALIGNMENT=${DATA_ALIGNMENT_n}
135TEXT_START_ADDR=${NONPAGED_TEXT_START_ADDR-${TEXT_START_ADDR}}
136( echo "/* Script for -n: mix text and data on same page */"
137  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
138  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
139) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xn
140
141LD_FLAG=N
142DATA_ALIGNMENT=${DATA_ALIGNMENT_N}
143( echo "/* Script for -N: mix text and data on same page; don't align data */"
144  . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
145  . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
146) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xbn
147
148if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
149  DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}}
150  LD_FLAG=c
151  COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp
152  ( echo "/* Script for -z combreloc: combine and sort reloc sections */"
153    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
154    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
155  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xc
156  rm -f ${COMBRELOC}
157  LD_FLAG=w
158  RELRO_NOW=" "
159  COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp
160  ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */"
161    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
162    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
163  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xw
164  rm -f ${COMBRELOC}
165  COMBRELOC=
166  unset RELRO_NOW
167fi
168
169if test -n "$GENERATE_SHLIB_SCRIPT"; then
170  LD_FLAG=shared
171  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
172  CREATE_SHLIB=" "
173  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
174  (
175    echo "/* Script for ld --shared: link shared library */"
176    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
177    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
178  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xs
179  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
180    LD_FLAG=cshared
181    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
182    COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp
183    ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */"
184      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
185      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
186    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xsc
187    rm -f ${COMBRELOC}
188    LD_FLAG=wshared
189    RELRO_NOW=" "
190    COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp
191    ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */"
192      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
193      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
194    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xsw
195    rm -f ${COMBRELOC}
196    COMBRELOC=
197    unset RELRO_NOW
198  fi
199  unset CREATE_SHLIB
200fi
201
202if test -n "$GENERATE_PIE_SCRIPT"; then
203  LD_FLAG=pie
204  DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}}
205  CREATE_PIE=" "
206  # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR.
207  (
208    echo "/* Script for ld -pie: link position independent executable */"
209    . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
210    . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
211  ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xd
212  if test -n "$GENERATE_COMBRELOC_SCRIPT"; then
213    LD_FLAG=cpie
214    DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}}
215    COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp
216    ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */"
217      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
218      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
219    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdc
220    rm -f ${COMBRELOC}
221    LD_FLAG=wpie
222    RELRO_NOW=" "
223    COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp
224    ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */"
225      . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME}
226      . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc
227    ) | sed -e '/^ *$/d;s/[ 	]*$//' > ldscripts/${EMULATION_NAME}.xdw
228    rm -f ${COMBRELOC}
229    COMBRELOC=
230    unset RELRO_NOW
231  fi
232  unset CREATE_PIE
233fi
234
235case " $EMULATION_LIBPATH " in
236    *" ${EMULATION_NAME} "*) COMPILE_IN=true;;
237esac
238
239# Generate e${EMULATION_NAME}.c.
240. ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em
241