133965Sjdp#!/bin/sh 233965Sjdp# genscripts.sh - generate the ld-emulation-target specific files 333965Sjdp# 4130561Sobrien# Usage: genscripts_extra.sh \ 5130561Sobrien# srcdir \ 6130561Sobrien# libdir \ 7218822Sdim# prefix \ 8130561Sobrien# exec_prefix \ 9130561Sobrien# host \ 10130561Sobrien# target \ 11130561Sobrien# target_alias \ 12130561Sobrien# default_emulation \ 13130561Sobrien# native_lib_dirs \ 14130561Sobrien# use_sysroot \ 15130561Sobrien# this_emulation \ 16130561Sobrien# optional: 17130561Sobrien# tool_dir \ 18130561Sobrien# customizer_script 1933965Sjdp# 2033965Sjdp# Sample usage: 21130561Sobrien# 22130561Sobrien# genscripts_extra.sh \ 23130561Sobrien# /sources/ld \ 24130561Sobrien# /usr/local/lib \ 25130561Sobrien# /usr/local \ 26218822Sdim# /usr/local \ 27130561Sobrien# sparc-sun-sunos4.1.3 \ 28130561Sobrien# sparc-sun-sunos4.1.3 \ 29130561Sobrien# sparc-sun-sunos4.1.3 \ 30130561Sobrien# sun4 \ 31130561Sobrien# "" \ 32130561Sobrien# no \ 33130561Sobrien# sun3 \ 34130561Sobrien# sparc-sun-sunos4.1.3 \ 35130561Sobrien# sparc.sh 36130561Sobrien# 37130561Sobrien# produces the linker scripts: 38130561Sobrien# 39130561Sobrien# sun3.x [default linker script] 40130561Sobrien# sun3.xbn [used when the linker is invoked with "-N"] 41130561Sobrien# sun3.xn [used when the linker is invoked with "-n"] 42130561Sobrien# sun3.xr [used when the linker is invoked with "-r"] 43130561Sobrien# sun3.xu [used when the linker is invoked with "-Ur"] 44130561Sobrien# and maybe: 45130561Sobrien# sun3.xc [used when the linker is invoked with "-z combreloc"] 46130561Sobrien# sun3.xsc [used when the linker is invoked with "--shared"] 47130561Sobrien# sun3.xdc [used when the linker is invoked with "-pie"] 48130561Sobrien# 49130561Sobrien# It also produced the C source file: 50130561Sobrien# 51130561Sobrien# em_sun3.c 52130561Sobrien# 53130561Sobrien# which is then compiled into the linker. 54130561Sobrien# 55130561Sobrien# The linker scripts are created by running the shell script 56130561Sobrien# /sources/ld/emulparams/sparc.sh to set the value of ${SCRIPT_NAME} 57130561Sobrien# (and any other variables it wants to). ${SCRIPT_NAME} is then 58130561Sobrien# invoked with a variable called ${LD_FLAG} to tell it which version 59130561Sobrien# of the linker script to create. 6033965Sjdp 61130561Sobrien 6233965Sjdpsrcdir=$1 6333965Sjdplibdir=$2 64218822Sdimprefix=$3 65218822Sdimexec_prefix=$4 66218822Sdimhost=$5 67218822Sdimtarget=$6 68218822Sdimtarget_alias=$7 69218822SdimEMULATION_LIBPATH=$8 70218822SdimNATIVE_LIB_DIRS=$9 7177298Sobrienshift 9 72218822Sdimuse_sysroot=$1 73218822SdimEMULATION_NAME=$2 74218822SdimTOOL_LIB=$3 75218822SdimCUSTOMIZER_SCRIPT=$4 76130561Sobrien 77130561Sobrien# Can't use ${TOOL_LIB:-$target_alias} here due to an Ultrix shell bug. 78130561Sobrienif [ "x${TOOL_LIB}" = "x" ] ; then 7977298Sobrien tool_lib=${exec_prefix}/${target_alias}/lib 8077298Sobrienelse 81130561Sobrien tool_lib=${exec_prefix}/${TOOL_LIB}/lib 8277298Sobrienfi 8333965Sjdp 84130561Sobrienif [ "x${CUSTOMIZER_SCRIPT}" = "x" ] ; then 85130561Sobrien CUSTOMIZER_SCRIPT=${EMULATION_NAME} 86130561Sobrienfi 87130561SobrienCUSTOMIZER_SCRIPT="${srcdir}/emulparams/${CUSTOMIZER_SCRIPT}.sh" 88130561Sobrien 8933965Sjdp# Include the emulation-specific parameters: 90130561Sobrien. ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 9133965Sjdp 9233965Sjdpif test -d ldscripts; then 9333965Sjdp true 9433965Sjdpelse 9533965Sjdp mkdir ldscripts 9633965Sjdpfi 9733965Sjdp 98130561Sobrien# Set some flags for the emultempl scripts. USE_LIBPATH will 99130561Sobrien# be set for any libpath-using emulation; NATIVE will be set for a 100130561Sobrien# libpath-using emulation where ${host} = ${target}. NATIVE 101130561Sobrien# may already have been set by the emulparams file, but that's OK 102130561Sobrien# (it'll just get set to "yes" twice). 103130561Sobrien 104130561Sobriencase " $EMULATION_LIBPATH " in 105130561Sobrien *" ${EMULATION_NAME} "*) 106130561Sobrien if [ "x${host}" = "x${target}" ] ; then 107130561Sobrien NATIVE=yes 108130561Sobrien USE_LIBPATH=yes 109130561Sobrien elif [ "x${use_sysroot}" = "xyes" ] ; then 110130561Sobrien USE_LIBPATH=yes 111130561Sobrien fi 112130561Sobrien ;; 113130561Sobrienesac 114130561Sobrien 115130561Sobrien# If the emulparams file sets NATIVE, make sure USE_LIBPATH is set also. 116130561Sobrienif test "x$NATIVE" = "xyes" ; then 117130561Sobrien USE_LIBPATH=yes 118130561Sobrienfi 119130561Sobrien 12033965Sjdp# Set the library search path, for libraries named by -lfoo. 12133965Sjdp# If LIB_PATH is defined (e.g., by Makefile) and non-empty, it is used. 12233965Sjdp# Otherwise, the default is set here. 12333965Sjdp# 12433965Sjdp# The format is the usual list of colon-separated directories. 12533965Sjdp# To force a logically empty LIB_PATH, do LIBPATH=":". 126130561Sobrien# 127130561Sobrien# If we are using a sysroot, prefix library paths with "=" to indicate this. 128130561Sobrien# 129130561Sobrien# If the emulparams file set LIBPATH_SUFFIX, prepend an extra copy of 130130561Sobrien# the library path with the suffix applied. 13133965Sjdp 132130561Sobrienif [ "x${LIB_PATH}" = "x" ] && [ "x${USE_LIBPATH}" = xyes ] ; then 133130561Sobrien LIB_PATH2= 134130561Sobrien 135130561Sobrien libs=${NATIVE_LIB_DIRS} 136130561Sobrien if [ "x${use_sysroot}" != "xyes" ] ; then 137130561Sobrien case " ${libs} " in 138130561Sobrien *" ${libdir} "*) ;; 139130561Sobrien *) libs="${libdir} ${libs}" ;; 14060484Sobrien esac 141130561Sobrien case " ${libs} " in 142130561Sobrien *" ${tool_lib} "*) ;; 143130561Sobrien *) libs="${tool_lib} ${libs}" ;; 144130561Sobrien esac 14533965Sjdp fi 146130561Sobrien 147130561Sobrien for lib in ${libs}; do 148130561Sobrien # The "=" is harmless if we aren't using a sysroot, but also needless. 149130561Sobrien if [ "x${use_sysroot}" = "xyes" ] ; then 150130561Sobrien lib="=${lib}" 151130561Sobrien fi 152130561Sobrien addsuffix= 153130561Sobrien case "${LIBPATH_SUFFIX}:${lib}" in 154130561Sobrien :*) ;; 155130561Sobrien *:*${LIBPATH_SUFFIX}) ;; 156130561Sobrien *) addsuffix=yes ;; 157130561Sobrien esac 158130561Sobrien if test -n "$addsuffix"; then 159130561Sobrien case :${LIB_PATH}: in 160130561Sobrien *:${lib}${LIBPATH_SUFFIX}:*) ;; 161130561Sobrien ::) LIB_PATH=${lib}${LIBPATH_SUFFIX} ;; 162130561Sobrien *) LIB_PATH=${LIB_PATH}:${lib}${LIBPATH_SUFFIX} ;; 163130561Sobrien esac 164130561Sobrien case :${LIB_PATH}:${LIB_PATH2}: in 165130561Sobrien *:${lib}:*) ;; 166130561Sobrien *::) LIB_PATH2=${lib} ;; 167130561Sobrien *) LIB_PATH2=${LIB_PATH2}:${lib} ;; 168130561Sobrien esac 169130561Sobrien else 170130561Sobrien case :${LIB_PATH2}: in 171130561Sobrien *:${lib}:*) ;; 172130561Sobrien ::) LIB_PATH2=${lib} ;; 173130561Sobrien *) LIB_PATH2=${LIB_PATH2}:${lib} ;; 174130561Sobrien esac 175130561Sobrien fi 176130561Sobrien done 177130561Sobrien 178130561Sobrien case :${LIB_PATH}:${LIB_PATH2}: in 179130561Sobrien *:: | ::*) LIB_PATH=${LIB_PATH}${LIB_PATH2} ;; 180130561Sobrien *) LIB_PATH=${LIB_PATH}:${LIB_PATH2} ;; 181130561Sobrien esac 18233965Sjdpfi 18333965Sjdp 184130561Sobrien# Always search $(tooldir)/lib, aka /usr/local/TARGET/lib, except for 185130561Sobrien# sysrooted configurations and when LIBPATH=":". 186130561Sobrienif [ "x${use_sysroot}" != "xyes" ] ; then 187130561Sobrien case :${LIB_PATH}: in 188130561Sobrien ::: | *:${tool_lib}:*) ;; 189130561Sobrien ::) LIB_PATH=${tool_lib} ;; 190130561Sobrien *) LIB_PATH=${tool_lib}:${LIB_PATH} ;; 191130561Sobrien esac 192218822Sdim # For multilib targets, search both $tool_lib dirs 193218822Sdim if [ "x${LIBPATH_SUFFIX}" != "x" ] ; then 194218822Sdim case :${LIB_PATH}: in 195218822Sdim ::: | *:${tool_lib}${LIBPATH_SUFFIX}:*) ;; 196218822Sdim ::) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX} ;; 197218822Sdim *) LIB_PATH=${tool_lib}${LIBPATH_SUFFIX}:${LIB_PATH} ;; 198218822Sdim esac 199218822Sdim fi 200130561Sobrienfi 20133965Sjdp 202104834SobrienLIB_SEARCH_DIRS=`echo ${LIB_PATH} | sed -e 's/:/ /g' -e 's/\([^ ][^ ]*\)/SEARCH_DIR(\\"\1\\");/g'` 20333965Sjdp 204130561Sobrien# We need it for testsuite. 205218822Sdimset $EMULATION_LIBPATH 206218822Sdimif [ "x$1" = "x$EMULATION_NAME" ]; then 207130561Sobrien test -d tmpdir || mkdir tmpdir 208218822Sdim rm -f tmpdir/libpath.exp 209130561Sobrien echo "set libpath \"${LIB_PATH}\"" | sed -e 's/:/ /g' > tmpdir/libpath.exp 210218822Sdimfi 211130561Sobrien 21233965Sjdp# Generate 5 or 6 script files from a master script template in 21333965Sjdp# ${srcdir}/scripttempl/${SCRIPT_NAME}.sh. Which one of the 5 or 6 21433965Sjdp# script files is actually used depends on command line options given 21533965Sjdp# to ld. (SCRIPT_NAME was set in the emulparams_file.) 21633965Sjdp# 21733965Sjdp# A .x script file is the default script. 21833965Sjdp# A .xr script is for linking without relocation (-r flag). 21933965Sjdp# A .xu script is like .xr, but *do* create constructors (-Ur flag). 22033965Sjdp# A .xn script is for linking with -n flag (mix text and data on same page). 22133965Sjdp# A .xbn script is for linking with -N flag (mix text and data on same page). 22233965Sjdp# A .xs script is for generating a shared library with the --shared 22333965Sjdp# flag; it is only generated if $GENERATE_SHLIB_SCRIPT is set by the 22433965Sjdp# emulation parameters. 22589857Sobrien# A .xc script is for linking with -z combreloc; it is only generated if 22689857Sobrien# $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or 22789857Sobrien# $SCRIPT_NAME is "elf". 22889857Sobrien# A .xsc script is for linking with --shared -z combreloc; it is generated 22989857Sobrien# if $GENERATE_COMBRELOC_SCRIPT is set by the emulation parameters or 23089857Sobrien# $SCRIPT_NAME is "elf" and $GENERATE_SHLIB_SCRIPT is set by the emulation 23189857Sobrien# parameters too. 23233965Sjdp 23389857Sobrienif [ "x$SCRIPT_NAME" = "xelf" ]; then 23489857Sobrien GENERATE_COMBRELOC_SCRIPT=yes 23589857Sobrienfi 23689857Sobrien 237130561SobrienSEGMENT_SIZE=${SEGMENT_SIZE-${MAXPAGESIZE-${TARGET_PAGE_SIZE}}} 23833965Sjdp 23933965Sjdp# Determine DATA_ALIGNMENT for the 5 variants, using 240130561Sobrien# values specified in the emulparams/<script_to_run>.sh file or default. 24133965Sjdp 24233965SjdpDATA_ALIGNMENT_="${DATA_ALIGNMENT_-${DATA_ALIGNMENT-ALIGN(${SEGMENT_SIZE})}}" 24333965SjdpDATA_ALIGNMENT_n="${DATA_ALIGNMENT_n-${DATA_ALIGNMENT_}}" 24433965SjdpDATA_ALIGNMENT_N="${DATA_ALIGNMENT_N-${DATA_ALIGNMENT-.}}" 24533965SjdpDATA_ALIGNMENT_r="${DATA_ALIGNMENT_r-${DATA_ALIGNMENT-}}" 24633965SjdpDATA_ALIGNMENT_u="${DATA_ALIGNMENT_u-${DATA_ALIGNMENT_r}}" 24733965Sjdp 24833965SjdpLD_FLAG=r 24933965SjdpDATA_ALIGNMENT=${DATA_ALIGNMENT_r} 25033965SjdpDEFAULT_DATA_ALIGNMENT="ALIGN(${SEGMENT_SIZE})" 25189857Sobrien( echo "/* Script for ld -r: link without relocation */" 252130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 25389857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 25489857Sobrien) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xr 25533965Sjdp 25633965SjdpLD_FLAG=u 25733965SjdpDATA_ALIGNMENT=${DATA_ALIGNMENT_u} 25833965SjdpCONSTRUCTING=" " 25989857Sobrien( echo "/* Script for ld -Ur: link w/out relocation, do create constructors */" 260130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 26189857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 26289857Sobrien) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xu 26333965Sjdp 26433965SjdpLD_FLAG= 26533965SjdpDATA_ALIGNMENT=${DATA_ALIGNMENT_} 26633965SjdpRELOCATING=" " 26789857Sobrien( echo "/* Default linker script, for normal executables */" 268130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 26989857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 27089857Sobrien) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.x 27133965Sjdp 27233965SjdpLD_FLAG=n 27333965SjdpDATA_ALIGNMENT=${DATA_ALIGNMENT_n} 27433965SjdpTEXT_START_ADDR=${NONPAGED_TEXT_START_ADDR-${TEXT_START_ADDR}} 27589857Sobrien( echo "/* Script for -n: mix text and data on same page */" 276130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 27789857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 27889857Sobrien) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xn 27933965Sjdp 28033965SjdpLD_FLAG=N 28133965SjdpDATA_ALIGNMENT=${DATA_ALIGNMENT_N} 28289857Sobrien( echo "/* Script for -N: mix text and data on same page; don't align data */" 283130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 28489857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 28589857Sobrien) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xbn 28633965Sjdp 28789857Sobrienif test -n "$GENERATE_COMBRELOC_SCRIPT"; then 28889857Sobrien DATA_ALIGNMENT=${DATA_ALIGNMENT_c-${DATA_ALIGNMENT_}} 28989857Sobrien LD_FLAG=c 29089857Sobrien COMBRELOC=ldscripts/${EMULATION_NAME}.xc.tmp 29189857Sobrien ( echo "/* Script for -z combreloc: combine and sort reloc sections */" 292130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 29389857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 29489857Sobrien ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xc 29589857Sobrien rm -f ${COMBRELOC} 296218822Sdim LD_FLAG=w 297218822Sdim RELRO_NOW=" " 298218822Sdim COMBRELOC=ldscripts/${EMULATION_NAME}.xw.tmp 299218822Sdim ( echo "/* Script for -z combreloc -z now -z relro: combine and sort reloc sections */" 300218822Sdim . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 301218822Sdim . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 302218822Sdim ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xw 303218822Sdim rm -f ${COMBRELOC} 30489857Sobrien COMBRELOC= 305218822Sdim unset RELRO_NOW 30689857Sobrienfi 30789857Sobrien 30833965Sjdpif test -n "$GENERATE_SHLIB_SCRIPT"; then 30933965Sjdp LD_FLAG=shared 31033965Sjdp DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}} 31133965Sjdp CREATE_SHLIB=" " 31233965Sjdp # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR. 31389857Sobrien ( 31489857Sobrien echo "/* Script for ld --shared: link shared library */" 315130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 31689857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 31789857Sobrien ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xs 31889857Sobrien if test -n "$GENERATE_COMBRELOC_SCRIPT"; then 31989857Sobrien LD_FLAG=cshared 32089857Sobrien DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}} 321218822Sdim COMBRELOC=ldscripts/${EMULATION_NAME}.xsc.tmp 32289857Sobrien ( echo "/* Script for --shared -z combreloc: shared library, combine & sort relocs */" 323130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 32489857Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 32589857Sobrien ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsc 32689857Sobrien rm -f ${COMBRELOC} 327218822Sdim LD_FLAG=wshared 328218822Sdim RELRO_NOW=" " 329218822Sdim COMBRELOC=ldscripts/${EMULATION_NAME}.xsw.tmp 330218822Sdim ( echo "/* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */" 331218822Sdim . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 332218822Sdim . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 333218822Sdim ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xsw 334218822Sdim rm -f ${COMBRELOC} 33589857Sobrien COMBRELOC= 336218822Sdim unset RELRO_NOW 33789857Sobrien fi 338130561Sobrien unset CREATE_SHLIB 33933965Sjdpfi 34033965Sjdp 341130561Sobrienif test -n "$GENERATE_PIE_SCRIPT"; then 342130561Sobrien LD_FLAG=pie 343130561Sobrien DATA_ALIGNMENT=${DATA_ALIGNMENT_s-${DATA_ALIGNMENT_}} 344130561Sobrien CREATE_PIE=" " 345130561Sobrien # Note that TEXT_START_ADDR is set to NONPAGED_TEXT_START_ADDR. 346130561Sobrien ( 347130561Sobrien echo "/* Script for ld -pie: link position independent executable */" 348130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 349130561Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 350130561Sobrien ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xd 351130561Sobrien if test -n "$GENERATE_COMBRELOC_SCRIPT"; then 352130561Sobrien LD_FLAG=cpie 353130561Sobrien DATA_ALIGNMENT=${DATA_ALIGNMENT_sc-${DATA_ALIGNMENT}} 354218822Sdim COMBRELOC=ldscripts/${EMULATION_NAME}.xdc.tmp 355130561Sobrien ( echo "/* Script for -pie -z combreloc: position independent executable, combine & sort relocs */" 356130561Sobrien . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 357130561Sobrien . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 358130561Sobrien ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdc 359130561Sobrien rm -f ${COMBRELOC} 360218822Sdim LD_FLAG=wpie 361218822Sdim RELRO_NOW=" " 362218822Sdim COMBRELOC=ldscripts/${EMULATION_NAME}.xdw.tmp 363218822Sdim ( echo "/* Script for -pie -z combreloc -z now -z relro: position independent executable, combine & sort relocs */" 364218822Sdim . ${CUSTOMIZER_SCRIPT} ${EMULATION_NAME} 365218822Sdim . ${srcdir}/scripttempl/${SCRIPT_NAME}.sc 366218822Sdim ) | sed -e '/^ *$/d;s/[ ]*$//' > ldscripts/${EMULATION_NAME}.xdw 367218822Sdim rm -f ${COMBRELOC} 368130561Sobrien COMBRELOC= 369218822Sdim unset RELRO_NOW 370130561Sobrien fi 371130561Sobrien unset CREATE_PIE 372130561Sobrienfi 373130561Sobrien 374104834Sobriencase " $EMULATION_LIBPATH " in 375104834Sobrien *" ${EMULATION_NAME} "*) COMPILE_IN=true;; 376104834Sobrienesac 37733965Sjdp 37833965Sjdp# Generate e${EMULATION_NAME}.c. 37933965Sjdp. ${srcdir}/emultempl/${TEMPLATE_NAME-generic}.em 380