#!/bin/sh
# Copyright (c) 2015-2021 Contributors as noted in the AUTHORS file
#
# This file is part of Solo5, a sandboxed execution environment.
#
# Permission to use, copy, modify, and/or distribute this software
# for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear
# in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# x86_64-solo5-none-static-ld: Solo5 wrapper for 'ld'.
# Generated by configure.sh at build time.
#
# Passes through to @@CONFIG_TARGET_LD@ for actual work. Defines all flags
# required for correct operation when linking code intended for inclusion in a
# Solo5 unikernel.
#
# Provides a '-z solo5-abi=ABI' option to select the Solo5 bindings to use if
# linking a unikernel. No default for ABI is provided, as it is expected that a
# caller directly using 'ld' knows what they are doing.

L="$(realpath $0 | xargs dirname)/../lib/x86_64-solo5-none-static"
[ ! -d "${L}" ] && echo "$0: Could not determine library path" 1>&2 && exit 1
# ld accepts -z solo5-abi=ABI, but does not provide a default ABI
# this is intentional
B=
Z=
for arg do
    shift
    if [ -n "${Z}" ]; then
        # handle -z linker-arg
        Z=
        case "$arg" in
            solo5-abi=*)
                B="${arg##*=}"
                continue
                ;;
            *)
                set -- "$@" "-z" "$arg"
                continue
                ;;
        esac
    fi

    case "$arg" in
        -z)
            if [ -z "${Z}" ]; then
                Z=1
                continue
            fi
            ;;
    esac
    set -- "$@" "$arg"
done
[ -n "${B}" ] && B="-T solo5_${B}.lds -l :solo5_${B}.o"
[ -n "${__V}" ] && set -x
exec ld \
     \
    -nostdlib \
    -L ${L} \
    -z max-page-size=0x1000 \
    -static \
    ${B} \
    "$@"
