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