Blob Blame History Raw
#! /bin/bash

# Paranoid check to make sure we don't reinvoke ourselves, effectively
# making a fork()bomb
if [ -n "$LD_HUGETLBFS_RECURSION" ]; then
    exit 99
fi
export LD_HUGETLBFS_RECURSION=1

### SET DEFAULT LDSCRIPT PATH HERE ###
if [ -z "$HUGETLB_LDSCRIPT_PATH" ]; then
    # Assume this script is running from the libhugetlbfs source tree,
    # and look for the ldscripts accordingly
    HUGETLB_LDSCRIPT_PATH=$(dirname $(readlink $0))/ldscripts
fi

### SET CUSTOM_LDSCRIPTS HERE ###
if [ -z "$CUSTOM_LDSCRIPTS" ]; then
    # Assume this script is running from the libhugetlbfs source tree,
    # and set CUSTOM_LDSCRIPTS to default "yes"
    CUSTOM_LDSCRIPTS="yes"
fi

# Try to figure out what's the underlying linker to invoke
if [ -z "$LD" ]; then
    for x in $(which -a ld); do
	if [ "$x" != "$0" ]; then
	    LD="$x"
	    break
	fi
    done
fi

i=0
while [ -n "$1" ]; do
    arg="$1"
    case "$arg" in
	-m*)
	    EMU="${arg#-m}"
	    args[$i]="$arg"
	    i=$[i+1]
	    if [ -z "$EMU" ]; then
		shift
		EMU="$1"
		args[$i]="$1"
		i=$[i+1]
	    fi
	    ;;
	--hugetlbfs-link=*)
	    if [ -z "$HUGETLB_DEPRECATED_LINK" ]; then
		echo -n "ld.hugetlbfs: --hugetlbfs-link is deprecated. " 1>&2
		echo "Migrate to --hugetlbfs-align." 1>&2
	    fi
	    HTLB_LINK="${arg#--hugetlbfs-link=}"
	    ;;
	--hugetlbfs-script-path=*)
	    HUGETLB_LDSCRIPT_PATH="${arg#--hugetlbfs-script-path=}"
	    ;;
	--hugetlbfs-align)
	    HTLB_ALIGN="slice"
	    ;;
	--)
	    args=("${args[@]}" "$@")
	    break
	    ;;
	*)
	    args[$i]="$arg"
	    i=$[i+1]
	    ;;
    esac
    shift
done

if [ -n "$HTLB_LINK" ]; then
    if [ "$CUSTOM_LDSCRIPTS" == "no" ]; then
        echo -n "ld.hugetlbfs: --hugetlbfs-link is not supported on this " 1>&2
        echo "platform. Use --hugetlbfs-align instead." 1>&2
    fi
    HTLB_ALIGN="" # --hugetlbfs-link overrides --hugetlbfs-align
    LDSCRIPT="$EMU.x$HTLB_LINK"
    HTLBOPTS="-T${HUGETLB_LDSCRIPT_PATH}/${LDSCRIPT}"
fi

# if -m is not present on command line
if [ -z "$EMU" ]; then
	if [ -n "$LDEMULATION" ]; then
		# try env. variable
		EMU="$LDEMULATION"
	else
		# pick first supported
		EMU="$(ld -V | sed -n '/Supported emulations/{n;p}' | tr -d ' ')"
	fi
fi

# if -m is not present on command line
if [ -z "$EMU" ]; then
	if [ -n "$LDEMULATION" ]; then
		# try env. variable
		EMU="$LDEMULATION"
	else
		# pick first supported
		EMU="$(ld -V | sed -n '/Supported emulations/{n;p}' | tr -d ' ')"
	fi
fi

MB=$((1024*1024))
case "$EMU" in
elf32ppclinux)		HPAGE_SIZE=$((16*$MB)) SLICE_SIZE=$((256*$MB)) ;;
elf64ppc|elf64lppc)
	hpage_kb=$(cat /proc/meminfo  | grep Hugepagesize: | awk '{print $2}')
	MMU_TYPE=$(cat /proc/cpuinfo  | grep MMU | awk '{ print $3}')
	HPAGE_SIZE=$((hpage_kb * 1024))
	if [ "$MMU_TYPE" == "Hash" ] ; then
		SLICE_SIZE=$((256*$MB))
	else
		SLICE_SIZE=$HPAGE_SIZE
	fi ;;
elf_i386|elf_x86_64)	HPAGE_SIZE=$((4*$MB)) SLICE_SIZE=$HPAGE_SIZE ;;
elf_s390|elf64_s390)	HPAGE_SIZE=$((1*$MB)) SLICE_SIZE=$HPAGE_SIZE ;;
armelf*_linux_eabi|aarch64elf*|aarch64linux*)
	hpage_kb=$(cat /proc/meminfo  | grep Hugepagesize: | awk '{print $2}')
	HPAGE_SIZE=$((hpage_kb * 1024))
	SLICE_SIZE=$HPAGE_SIZE ;;
esac

if [ "$HTLB_ALIGN" == "slice" ]; then
	HTLBOPTS="-zcommon-page-size=$SLICE_SIZE -zmax-page-size=$SLICE_SIZE"
	HTLBOPTS="$HTLBOPTS -lhugetlbfs"

	# targeting the ARM platform one needs to explicitly set the text segment offset
	# otherwise it will be NULL.
	case "$EMU" in
	armelf*_linux_eabi|aarch64elf*|aarch64linux*)	HTLBOPTS="$HTLBOPTS -Ttext-segment=$SLICE_SIZE" ;;
	elf_i386)		HTLBOPTS="$HTLBOPTS -Ttext-segment=0x08000000" ;;
	elf64ppc|elf64lppc)
		if [ "$MMU_TYPE" == "Hash" ] ; then
			printf -v TEXTADDR "%x" "$SLICE_SIZE"
			HTLBOPTS="$HTLBOPTS -Ttext-segment=$TEXTADDR"
		fi ;;
	esac
fi

${LD} "${args[@]}" ${HTLBOPTS}