From 7a6b2c4012309b1c525f51da5908a0c7b0290dd7 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Sun, 17 Mar 2019 05:21:24 -0400 Subject: tools/shld.sh: Add a basic RTLD --- (limited to 'tools') diff --git a/tools/shld.sh b/tools/shld.sh index 41751d3..3117ccf 100755 --- a/tools/shld.sh +++ b/tools/shld.sh @@ -2,7 +2,7 @@ # # Shell command language linker # -# Copyright (C) 2015 Patrick "P. J." McDermott +# Copyright (C) 2015, 2019 Patrick "P. J." McDermott # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ set -u -VERSION='0.2.0' +VERSION='0.3.0' die() { @@ -35,8 +35,9 @@ link() local make_executable="${1}" local interpreter="${2}" local entry_point="${3}" - local output="${4}" - shift 4 + local libs="${4}" + local output="${5}" + shift 5 local input= # Open output file. @@ -58,6 +59,33 @@ link() } EOF + # Write __DT_NEEDED list and basic RTLD. + printf "__DT_NEEDED='%s'\n" "${libs}" >&3 + cat >&3 <<-'EOF' + : ${SHLD_LIBRARY_PATH:=${LIBDATADIR}} + for __lib in ${__DT_NEEDED}; do + __found=false + IFS=':' + for __el in ${SHLD_LIBRARY_PATH}; do + if __f="$(cat "${__el}/${__lib}" 2>/dev/null)" + then + eval "${__f}" + __found=true + break + fi + done + unset IFS + if ! ${__found}; then + printf "%s: error while loading shell $(: \ + )shared libraries: %s: cannot open $(: \ + )shell shared object file\n" \ + "${0}" "${__lib}" >&2 + exit 127 + fi + done + unset __lib __el __found + EOF + # Read input files. for input in "${@}"; do if ! cat "${input}" >&3; then @@ -101,6 +129,7 @@ help() Options: -h Display this information -V Display linker version information + -l Add to the list of libraries to load at run time -I Make an executable and use as the interpreter for your program, instead of the default of "/bin/sh" -e Make an executable and use as the function for beginning @@ -124,12 +153,13 @@ EOF main() { local opt= + local libs='' local make_executable=false local interpreter='/bin/sh' local entry_point='main' local output='out.sh' - while getopts 'hVI:e:o:' opt; do + while getopts 'hVl:I:e:o:' opt; do case "${opt}" in 'h') help @@ -139,6 +169,9 @@ main() version exit ;; + 'l') + libs="${libs}${OPTARG} " + ;; 'I') interpreter="${OPTARG}" make_executable=true @@ -159,7 +192,7 @@ main() exit 1 fi - link "${make_executable}" "${interpreter}" "${entry_point}" \ + link "${make_executable}" "${interpreter}" "${entry_point}" "${libs}" \ "${output}" "${@}" } -- cgit v0.9.1