mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Harmen Stoppels <me@harmenstoppels.nl>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: [musl] [PATCH] Improve locating ld-musl-[arch].path file
Date: Mon, 03 Jan 2022 17:00:19 +0000	[thread overview]
Message-ID: <KORRcazC3jZihuTlLVKOeASexiV0a6ddk52DXfkFH0ZY5cloL3_9jUOPmUMb9dKq4Vy04kiR8qERpRT3Kf1-xdYyiTHg2qHrV1fhHWAosYU=@harmenstoppels.nl> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1774 bytes --]

musl locates the config file with search paths relative to the
interpreter/loader by taking the path up to the second to last /, that
is, it tries to drop the filename and dir. This logic is flawed when the
interpreter contains multiple /'s between dir and filename. This commit
fixes that.

Example:

```
$ ./configure "--prefix=$PWD/build" "--syslibdir=$PWD/build/loader/lib"
$ make install -j
$ mkdir -p build/loader/etc example
$ echo "$PWD/example" > "./build/loader/etc/ld-musl-$(arch).path"
$ echo 'int f() {return 0;}' | ./build/bin/musl-gcc -shared -x c - -Wl,-soname,libf.so -o example/libf.so
$ echo 'int f(); int main(){return f();}' | ./build/bin/musl-gcc -x c - -Lexample -lf -o example/a.out
$ ./example/a.out && echo "works"
works
$ patchelf --set-interpreter "$PWD/build/loader/lib//ld-musl-$(arch).so.1" example/a.out
$ ./example/a.out || echo "does not work"
Error loading shared library libf.so: No such file or directory (needed by ./example/a.out)
Error relocating ./example/a.out: f: symbol not found
does not work
```
---
 ldso/dynlink.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index 5b9c8be4..3e7766ac 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1065,8 +1065,12 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
 				size_t prefix_len;
 				if (ldso.name[0]=='/') {
 					char *s, *t, *z;
-					for (s=t=z=ldso.name; *s; s++)
-						if (*s=='/') z=t, t=s;
+					s=t=z=ldso.name;
+					do {
+						if (*s++ != '/') continue;
+						z=t, t=s-1;
+						while (*s == '/') ++s;
+					} while (*s);
 					prefix_len = z-ldso.name;
 					if (prefix_len < PATH_MAX)
 						prefix = ldso.name;
-- 

2.25.1

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 509 bytes --]

                 reply	other threads:[~2022-01-03 17:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='KORRcazC3jZihuTlLVKOeASexiV0a6ddk52DXfkFH0ZY5cloL3_9jUOPmUMb9dKq4Vy04kiR8qERpRT3Kf1-xdYyiTHg2qHrV1fhHWAosYU=@harmenstoppels.nl' \
    --to=me@harmenstoppels.nl \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).