aboutsummaryrefslogtreecommitdiff
path: root/elf/Makefile
diff options
context:
space:
mode:
authorGeoffrey Thomas <geofft@ldpreload.com>2025-02-18 15:58:16 -0500
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2025-03-03 17:05:21 -0300
commit372c632ce7c78471dbda69ca33625d1ecb6fb2f7 (patch)
tree77a0d5c2ae9aeca4d195e170771234bffe473cbf /elf/Makefile
parent9e51ae3cd0c7f65bdeba93b7f1d780cdb21fc269 (diff)
downloadglibc-azanella/bz25263-loader-realpath.tar.xz
glibc-azanella/bz25263-loader-realpath.zip
elf: Canonicalize $ORIGIN in an explicit ld.so invocation [BZ 25263]azanella/bz25263-loader-realpath
When an executable is invoked directly, we calculate $ORIGIN by calling readlink on /proc/self/exe, which the Linux kernel resolves to the target of any symlinks. However, if an executable is run through ld.so, we cannot use /proc/self/exe and instead use the path given as an argument. This leads to a different calculation of $ORIGIN, which is most notable in that it causes ldd to behave differently (e.g., by not finding a library) from directly running the program. To make the behavior consistent, take advantage of the fact that the kernel also resolves /proc/self/fd/ symlinks to the target of any symlinks in the same manner, so once we have opened the main executable in order to load it, replace the user-provided path with the result of calling readlink("/proc/self/fd/N"). (On non-Linux platforms this resolution does not happen and so no behavior change is needed.) Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'elf/Makefile')
-rw-r--r--elf/Makefile23
1 files changed, 23 insertions, 0 deletions
diff --git a/elf/Makefile b/elf/Makefile
index 1ea0e7037e..3267f28a62 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -455,6 +455,7 @@ tests += \
tst-noload \
tst-non-directory-path \
tst-null-argv \
+ tst-origin \
tst-p_align1 \
tst-p_align2 \
tst-p_align3 \
@@ -761,6 +762,7 @@ modules-names += \
libmarkermod5-3 \
libmarkermod5-4 \
libmarkermod5-5 \
+ liborigin-mod \
libtracemod1-1 \
libtracemod2-1 \
libtracemod3-1 \
@@ -3397,3 +3399,24 @@ $(objpfx)tst-nolink-libc-2: $(objpfx)tst-nolink-libc.o
-Wl,--dynamic-linker=$(objpfx)ld.so
$(objpfx)tst-nolink-libc-2.out: $(objpfx)tst-nolink-libc-2 $(objpfx)ld.so
$< > $@ 2>&1; $(evaluate-test)
+
+
+$(objpfx)tst-origin: $(objpfx)tst-origin.o $(objpfx)liborigin-mod.so
+ $(LINK.o) -o $@ -B$(csu-objpfx) $(LDFLAGS.so) $< \
+ -Wl,-rpath,\$$ORIGIN \
+ -L$(subst :, -L,$(rpath-link)) -Wl,--no-as-needed -lorigin-mod
+$(objpfx)liborigin-mod.so: $(objpfx)liborigin-mod.os
+ $(LINK.o) -shared -o $@ -B$(csu-objpfx) $(LDFLAGS.so) \
+ $(LDFLAGS-soname-fname) \
+ $<
+$(objpfx)tst-origin.out: tst-origin.sh $(objpfx)tst-origin
+ $(SHELL) \
+ $< \
+ '$(common-objpfx)' \
+ '$(test-wrapper-env)' \
+ '$(run-program-env)' \
+ '$(rpath-link)' \
+ tst-origin \
+ liborigin-mod.so \
+ > $@; \
+ $(evaluate-test)