# GNU ld scripts that stand in for a shared library (hwjs-cc#121). # # A distribution may install lib.so as a short text script instead of a # symlink, ncurses' `INPUT(libncurses.so.6 AS_NEEDED(-ltinfo))` being the usual # one. GNU ld reads such a file wherever it expects a library and links what the # script names. Sourced by brintcc-glibc and brintld, which both resolve -lNAME. # # Only the directives that name inputs are understood: INPUT, GROUP and # AS_NEEDED, with OUTPUT_FORMAT, OUTPUT_ARCH and TARGET ignored. A script with # any other directive (a version script, SECTIONS, ...) is not one of these, and # ld_script_members refuses it so the caller keeps its own handling. # ld_script_members : one line per input the script names, in order: # lib<0|1> for -l # path<0|1> for a file name or -l:, unresolved # The last field is 1 for an entry inside AS_NEEDED(...). Returns 1, printing # nothing, when the file is not a text script of only those directives or names # no input. ld_script_members() { local f="$1" hex [[ -f "$f" && -r "$f" ]] || return 1 # A script is short text; a host ELF or a wasm module is neither. (( $(head -c 65537 "$f" | wc -c) <= 65536 )) || return 1 hex="$(head -c 65536 "$f" | od -An -tx1)" || return 1 LC_ALL=C grep -qw 00 <<<"$hex" && return 1 LC_ALL=C awk ' BEGIN { RS = "\001" } { s = s $0 } END { while ((i = index(s, "/*")) > 0) { rest = substr(s, i + 2); j = index(rest, "*/") if (j == 0) exit 1 s = substr(s, 1, i - 1) " " substr(rest, j + 2) } gsub(/[(]/, " ( ", s); gsub(/[)]/, " ) ", s); gsub(/,/, " ", s) n = split(s, t, /[ \t\r\n]+/) k = 0 for (x = 1; x <= n; x++) if (t[x] != "") tok[++k] = t[x] out = ""; found = 0; p = 1 while (p <= k) { kw = tok[p] if (tok[p + 1] != "(") exit 1 p += 2 if (kw == "OUTPUT_FORMAT" || kw == "OUTPUT_ARCH" || kw == "TARGET") { while (p <= k && tok[p] != ")") { if (tok[p] == "(") exit 1; p++ } if (p > k) exit 1 p++; continue } if (kw != "INPUT" && kw != "GROUP") exit 1 asn = 0 while (1) { if (p > k) exit 1 w = tok[p] if (w == ")") { if (asn) { asn = 0; p++; continue } p++; break } if (w == "AS_NEEDED") { if (asn || tok[p + 1] != "(") exit 1 asn = 1; p += 2; continue } if (w == "(") exit 1 if (w ~ /^".*"$/) w = substr(w, 2, length(w) - 2) if (w ~ /^-l:./) out = out "path\t" substr(w, 4) "\t" asn "\n" else if (w ~ /^-l./) out = out "lib\t" substr(w, 3) "\t" asn "\n" else if (w ~ /^-/ || w == "") exit 1 else out = out "path\t" w "\t" asn "\n" found = 1; p++ } } if (!found) exit 1 printf "%s", out }' "$f" } # ld_script_member_path