From patchwork Tue Mar 11 20:17:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 872506 Delivered-To: patch@linaro.org Received: by 2002:a5d:64ce:0:b0:38f:210b:807b with SMTP id f14csp1669114wri; Tue, 11 Mar 2025 13:17:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU2tCR4TFj8EVT8eqkpftZ2h0dPEnyMWe6iXIQiPCSvjVkNgmSbghXhO7gHeTBHFpUg62To8g==@linaro.org X-Google-Smtp-Source: AGHT+IHZQjCcuInqEa8PRt+S4qGOnw35csioQ5Lt3X4dJsY3j2Qkcs969HqPS/X/dM2tp3L7/RKT X-Received: by 2002:a05:620a:2b97:b0:7c5:6299:3f4 with SMTP id af79cd13be357-7c562990591mr635873585a.49.1741724275986; Tue, 11 Mar 2025 13:17:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1741724275; cv=pass; d=google.com; s=arc-20240605; b=afvAYy27kEwikiJoB/w3LMIWqKcN2HwX0B5PccYciLkIALgdKJrbA0fXlRUo9sxUXn WA9iFs/kbDheU4p7dlYHm1MJ+h0SyMXRkszsTX+nuAMxYp+Jwz9ORVJC6WndpGFjRHjN D+x+cWEerWu1xS6+HNiQiUjbNCK6yKMJvJlAdIHZtxM/FeNs9g0T+MPloXQqgMP2oJiv Ypi/gQnC4snkApES4R6b7TWjPqp71Kv+IN5fFLB3iNISbDV/RaWzPHFImJvwOj2dZthB Ldw4G1v1IgNkCbi6A9fyA4cSmgFn7y0RsAnuNH6/q2TAOR8KTyXjJpYTDI4k/Rsy/AQt F0Xg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=lrUhClcMuPdagP5YN3iSuMwqdvFnsHhVvwyRdyVILH0=; fh=ZUYdiu42Otzfm5hZWyt+f/1EKkTvfpBtKLfgfG0qAuk=; b=ZXV3cPFEq8xg03U7nLGi/fdg224CUJjtISeHplmLMfalvT3hVplr8p3Y68mGUf3qCW vkRn/a5KU6G/xqxTMvvJST091QbG+0zV9ePKBtthEI7xtkS6kZCvWlH2ODhp17SnUT0b i7vBfA4ARuvjxIoZFfuV4xA4ATDfTjqB+QfuB2OslfWPLD8lCV8ipFWZIL/LJiyLt9R2 GYtzT1k64ZenV0Wm9reS6UBXimJU7sff4B+lFIVaOsTkAc8eRLMwB9gGYjVN4IYyPF7m 72W3JcXC/vmN+YoMe7iKnho30ZcjjWJg3nLi7PxpS9kbEPHFCHO5PmbQfWHjzv+rWE6J 9cqw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WVkptW1B; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id af79cd13be357-7c54cacdd36si540480085a.314.2025.03.11.13.17.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Mar 2025 13:17:55 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WVkptW1B; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BFD13857731 for ; Tue, 11 Mar 2025 20:17:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BFD13857731 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=WVkptW1B X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id C45CA3857BA5 for ; Tue, 11 Mar 2025 20:17:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C45CA3857BA5 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C45CA3857BA5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741724254; cv=none; b=KZXSMgxrW2CzqZRrKzBiN9V3eQN+H+z2gnPeM3CHKJZ27vProAAwY0LHk5Vl4qsiZpC6nWgQfIbLzlYPNsBQ9Csd5OY5oE7HUhooezx0zclMoEEpHtdftuyUSdFEuwEEAWSKv6DnZfPcWPl/2a+Vjp7i9r+4Auz1dcGmHedimMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741724254; c=relaxed/simple; bh=LMZbHA0HjIlid5tyssAeKr8csgEb/XzjvQ+8gwlalQk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aBkWUYdc4Ip6hO19DKmbgGqYSukjRGRt6Vd7g1JicvGIuJPQA0sNIIqlxBhOcrsQc1N5ocic+lNrDUvbbwmt5Q0OjAy21sqs3uZaOUg1lFxHOrRObk0sJTtJb3UPUmYZKd3Ih2Fh4p6IIeURNB2whoM8h2A1Vw/abG2BjSCSuJE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C45CA3857BA5 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-22359001f1aso140912155ad.3 for ; Tue, 11 Mar 2025 13:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1741724252; x=1742329052; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lrUhClcMuPdagP5YN3iSuMwqdvFnsHhVvwyRdyVILH0=; b=WVkptW1BvWxJa25jDV2TqJpd5eqA/StMXU+5Aiuv7gvxI79GkpVbZzYbWMPoEpHOGj GFJK0W/MmD5K1E1D6xuqYMo2J02jP7yUCxCS3q9QY8+QEs7i/m+o9ZJhHnaaa54M6QJx CDWIY3j6oLf5fD8Nk+0jSPKjv7JDJPvHDOP6iMhqOxGZ4YeMwbMuF++XpqBdiKB9CwJs pBhQFZHVjBWXbBofrYLdOXFtpVsV8LAj0t6A9yejMw4RgXZ2WR6CRE2g2Kn+GPBCa1N8 afcIZmtaggf4kKmJVvfWJ/PQlXl6ACqF4FkuqY61g/XWgQpBF89tfombywqg1fdTLDkD u8JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741724252; x=1742329052; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lrUhClcMuPdagP5YN3iSuMwqdvFnsHhVvwyRdyVILH0=; b=j5yWyeeHJXostNckxExwADuhlFO6eLDfifB3NV7DjxyGjxT+PqemMKHoIWkfQNWK9m niQj0lvXIYTICMT75pK1jje5MgadzOIVJ1LekdPRQn8sxIL3RTz+1KgvCKKfo0dHijm7 Yngwo2Va5c0GlJZT/3cexcUlqYzyBisgqmEN9o5/Q2VBPitQw//wi2KC/mDwxGAiUbCl SyIe7xBd4g1TGLWz8K0olQDVjmULD93LiO8F3UZkgFpFJKjfZ9H08qmNtzas+lB02cv7 kYcFARatpDoZ3kdh6X99rjBzyeyg0JbgYtPZbNxGCNUM0oDGU8vRqbIWIWmH/TdS8gtt pQZQ== X-Gm-Message-State: AOJu0YzRomphlUppELWHIHtRq+HbfMt0puu+N0YQg1pHyUyEeDkZk9rJ b5CzPTRc2vRrNMqexdi7uBg9ha96+aTV4bx3wKcvgHN2O4PDFh/z+AbGYFVxyQ9DpF/qCXGxw/l H X-Gm-Gg: ASbGncu51c7bT2wSDL3dxWNmFoBxrLqWf1Qko3LntobdNVpOuLRFaKNvTMNOvba74OH bkmp5pNT44Z3uHny7Op8MgCe80Rf86g/2hY8HFWL4QRk1BuOjanO9y5259JCgvuJROR4l0HGhtP A4W2Vy/P0n8GgrAii+mxA28CWiajjL88z55jWnlnu2tJebeZF+OtF2Ffa71VKlMf+pnByT3dtp8 dHtVz542AopeUnkM0lpyrVc91ZPqitah/6DvCDLcBBmcGCFoHa/RODKa3saFj66ITdoKKoS56PH Vyq9FkV3kxPbPVChs81g9e/CUV/JWy/UX6KZ2bariIvOqtc3sg8tMlQ= X-Received: by 2002:a17:902:e811:b0:224:7a4:b2a with SMTP id d9443c01a7336-2242887b386mr275014375ad.11.1741724252014; Tue, 11 Mar 2025 13:17:32 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:1ebf:8b5:8f5b:dd39:866]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410aa5c36sm102426535ad.235.2025.03.11.13.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Mar 2025 13:17:31 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Geoffrey Thomas Subject: [PATCH v3] elf: Canonicalize $ORIGIN in an explicit ld.so invocation [BZ 25263] Date: Tue, 11 Mar 2025 17:17:10 -0300 Message-ID: <20250311201720.2794859-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org 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.) The __fd_to_filename usage on loader (through dl-origin.c) pulls _itoa.c, which in turn defines a lot of The __fd_to_filename requires _fitoa_word and _itoa_word, which for 32-bits pulls a lot of definitions from _itoa.c (due _ITOA_NEEDED debing defined). To simplify the build move the required function to a new file, _fitoa_word.c. Checked on x86_64-linux-gnu and i686-linux-gnu. Co-authored-by: Geoffrey Thomas --- elf/Makefile | 23 +++++++++++ elf/dl-load.c | 6 +++ elf/dl-origin.c | 6 +++ elf/liborigin-mod.c | 1 + elf/tst-origin.c | 26 +++++++++++++ elf/tst-origin.sh | 60 +++++++++++++++++++++++++++++ stdio-common/Makefile | 3 ++ stdio-common/_fitoa_word.c | 60 +++++++++++++++++++++++++++++ stdio-common/_itoa.c | 43 --------------------- sysdeps/generic/_itoa.h | 31 --------------- sysdeps/generic/ldsodefs.h | 4 ++ sysdeps/mach/hurd/Makefile | 2 + sysdeps/unix/sysv/linux/dl-origin.c | 23 +++++++++++ 13 files changed, 214 insertions(+), 74 deletions(-) create mode 100644 elf/liborigin-mod.c create mode 100644 elf/tst-origin.c create mode 100755 elf/tst-origin.sh create mode 100644 stdio-common/_fitoa_word.c diff --git a/elf/Makefile b/elf/Makefile index 77a76f2142..8055d9ffbb 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -456,6 +456,7 @@ tests += \ tst-noload \ tst-non-directory-path \ tst-null-argv \ + tst-origin \ tst-p_align1 \ tst-p_align2 \ tst-p_align3 \ @@ -763,6 +764,7 @@ modules-names += \ libmarkermod5-3 \ libmarkermod5-4 \ libmarkermod5-5 \ + liborigin-mod \ libtracemod1-1 \ libtracemod2-1 \ libtracemod3-1 \ @@ -3442,3 +3444,24 @@ $(objpfx)tst-dlopen-constructor-null: \ $(objpfx)tst-dlopen-constructor-null-mod2.so $(objpfx)tst-dlopen-constructor-null-mod2.so: \ $(objpfx)tst-dlopen-constructor-null-mod1.so + +CFLAGS-tst-origin.c += $(no-stack-protector) +$(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) diff --git a/elf/dl-load.c b/elf/dl-load.c index 4998652adf..6b7e9799f3 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -965,6 +965,12 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, { assert (nsid == LM_ID_BASE); memset (&id, 0, sizeof (id)); + char *realname_can = _dl_canonicalize (fd); + if (realname_can != NULL) + { + free (realname); + realname = realname_can; + } } else { diff --git a/elf/dl-origin.c b/elf/dl-origin.c index 9f6b921b01..812f5dbb28 100644 --- a/elf/dl-origin.c +++ b/elf/dl-origin.c @@ -47,3 +47,9 @@ _dl_get_origin (void) return result; } + +char * +_dl_canonicalize (int fd) +{ + return NULL; +} diff --git a/elf/liborigin-mod.c b/elf/liborigin-mod.c new file mode 100644 index 0000000000..aa6d4c27df --- /dev/null +++ b/elf/liborigin-mod.c @@ -0,0 +1 @@ +void foo (void) {} diff --git a/elf/tst-origin.c b/elf/tst-origin.c new file mode 100644 index 0000000000..734b2e81f6 --- /dev/null +++ b/elf/tst-origin.c @@ -0,0 +1,26 @@ +/* Test if $ORIGIN works correctly with symlinks (BZ 25263) + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +extern void foo (void); + +int +main (int argc, char *argv[]) +{ + foo (); + return 0; +} diff --git a/elf/tst-origin.sh b/elf/tst-origin.sh new file mode 100755 index 0000000000..2555d3ed9e --- /dev/null +++ b/elf/tst-origin.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# Test if $ORIGIN works correctly with symlinks (BZ 25263) +# Copyright (C) 2025 Free Software Foundation, Inc. +# This file is part of the GNU C Library. + +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. + +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. + +# You should have received a copy of the GNU Lesser General Public +# License along with the GNU C Library; if not, see +# . + +set -e + +objpfx=$1 +test_wrapper_env=$2 +run_program_env=$3 +library_path=$4 +test_program=$5 +test_library=$6 + +cleanup() +{ + # Move the binary and library back to build directory + mv $tmpdir/sub/$test_program ${objpfx}elf + mv $tmpdir/sub/$test_library ${objpfx}elf + + rm -rf $tmpdir +} + +tmpdir=$(mktemp -d "${objpfx}elf/tst-origin.XXXXXXXXXX") +#trap cleanup 0 + +mkdir ${tmpdir}/sub + +# Remove the dependency from $library_path +mv ${objpfx}elf/$test_program $tmpdir/sub +mv ${objpfx}elf/$test_library $tmpdir/sub + +cd ${tmpdir} +ln -s sub/$test_program $test_program + +${test_wrapper_env} \ +${run_program_env} \ +${objpfx}elf/ld.so --library-path "$library_path" \ + ./$test_program 2>&1 && rc=0 || rc=$? + +# Also check if ldd resolves the dependency +LD_TRACE_LOADED_OBJECTS=1 \ +${objpfx}elf/ld.so --library-path "$library_path" \ + ./$test_program 2>&1 | grep 'not found' && rc=1 || rc=0 + +exit $rc diff --git a/stdio-common/Makefile b/stdio-common/Makefile index b68e9223c6..d3733d0c3d 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -59,6 +59,7 @@ headers := \ # headers routines := \ + _fitoa_word \ _itoa \ _itowa \ asprintf \ @@ -663,6 +664,8 @@ CFLAGS-dprintf.c += $(config-cflags-wno-ignored-attributes) # off for non-shared builds. CFLAGS-_itoa.o = $(no-stack-protector) CFLAGS-_itoa.op = $(no-stack-protector) +CFLAGS-_fitoa_word.o = $(no-stack-protector) +CFLAGS-_fitoa_word.op = $(no-stack-protector) CFLAGS-scanf13.c += $(test-config-cflags-wno-fortify-source) diff --git a/stdio-common/_fitoa_word.c b/stdio-common/_fitoa_word.c new file mode 100644 index 0000000000..f0b2707173 --- /dev/null +++ b/stdio-common/_fitoa_word.c @@ -0,0 +1,60 @@ +/* Internal function for converting integers to ASCII. + Copyright (C) 1994-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include <_itoa.h> + +char * +_itoa_word (_ITOA_WORD_TYPE value, char *buflim, + unsigned int base, int upper_case) +{ + const char *digits = (upper_case + ? _itoa_upper_digits + : _itoa_lower_digits); + + switch (base) + { +#define SPECIAL(Base) \ + case Base: \ + do \ + *--buflim = digits[value % Base]; \ + while ((value /= Base) != 0); \ + break + + SPECIAL (10); + SPECIAL (16); + SPECIAL (8); + default: + do + *--buflim = digits[value % base]; + while ((value /= base) != 0); + } + return buflim; +} +#undef SPECIAL + +char * +_fitoa_word (_ITOA_WORD_TYPE value, char *buf, unsigned int base, + int upper_case) +{ + char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */ + char *cp = _itoa_word (value, tmpbuf + sizeof (value) * 4, base, upper_case); + while (cp < tmpbuf + sizeof (value) * 4) + *buf++ = *cp++; + return buf; +} + diff --git a/stdio-common/_itoa.c b/stdio-common/_itoa.c index 51c3ab9c14..08859f0dd0 100644 --- a/stdio-common/_itoa.c +++ b/stdio-common/_itoa.c @@ -162,38 +162,6 @@ const struct base_table_t _itoa_base_table[] attribute_hidden = }; #endif -#if IS_IN (libc) -char * -_itoa_word (_ITOA_WORD_TYPE value, char *buflim, - unsigned int base, int upper_case) -{ - const char *digits = (upper_case - ? _itoa_upper_digits - : _itoa_lower_digits); - - switch (base) - { -#define SPECIAL(Base) \ - case Base: \ - do \ - *--buflim = digits[value % Base]; \ - while ((value /= Base) != 0); \ - break - - SPECIAL (10); - SPECIAL (16); - SPECIAL (8); - default: - do - *--buflim = digits[value % base]; - while ((value /= base) != 0); - } - return buflim; -} -#undef SPECIAL -#endif /* IS_IN (libc) */ - - #if _ITOA_NEEDED char * _itoa (unsigned long long int value, char *buflim, unsigned int base, @@ -460,17 +428,6 @@ _itoa (unsigned long long int value, char *buflim, unsigned int base, } #endif -char * -_fitoa_word (_ITOA_WORD_TYPE value, char *buf, unsigned int base, - int upper_case) -{ - char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */ - char *cp = _itoa_word (value, tmpbuf + sizeof (value) * 4, base, upper_case); - while (cp < tmpbuf + sizeof (value) * 4) - *buf++ = *cp++; - return buf; -} - #if _ITOA_NEEDED char * _fitoa (unsigned long long value, char *buf, unsigned int base, int upper_case) diff --git a/sysdeps/generic/_itoa.h b/sysdeps/generic/_itoa.h index d7e3007389..2f170d3bf2 100644 --- a/sysdeps/generic/_itoa.h +++ b/sysdeps/generic/_itoa.h @@ -51,40 +51,9 @@ hidden_proto (_itoa_upper_digits) hidden_proto (_itoa_lower_digits) #endif -#if IS_IN (libc) extern char *_itoa_word (_ITOA_WORD_TYPE value, char *buflim, unsigned int base, int upper_case) attribute_hidden; -#else -static inline char * __attribute__ ((unused, always_inline)) -_itoa_word (_ITOA_WORD_TYPE value, char *buflim, - unsigned int base, int upper_case) -{ - const char *digits = (upper_case - ? _itoa_upper_digits - : _itoa_lower_digits); - - switch (base) - { -# define SPECIAL(Base) \ - case Base: \ - do \ - *--buflim = digits[value % Base]; \ - while ((value /= Base) != 0); \ - break - - SPECIAL (10); - SPECIAL (16); - SPECIAL (8); - default: - do - *--buflim = digits[value % base]; - while ((value /= base) != 0); - } - return buflim; -} -# undef SPECIAL -#endif /* Similar to the _itoa functions, but output starts at buf and pointer after the last written character is returned. */ diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 8465cbaa9b..19494b82ee 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1223,6 +1223,10 @@ extern struct link_map * _dl_get_dl_main_map (void) attribute_hidden; /* Find origin of the executable. */ extern const char *_dl_get_origin (void) attribute_hidden; +/* Return the canonalized path name from the opened file descriptor FD, + or NULL otherwise. */ +extern char * _dl_canonicalize (int fd) attribute_hidden; + /* Count DSTs. */ extern size_t _dl_dst_count (const char *name) attribute_hidden; diff --git a/sysdeps/mach/hurd/Makefile b/sysdeps/mach/hurd/Makefile index 13e5cea4c2..4b69b40065 100644 --- a/sysdeps/mach/hurd/Makefile +++ b/sysdeps/mach/hurd/Makefile @@ -300,6 +300,8 @@ ifeq ($(subdir),elf) check-execstack-xfail += ld.so libc.so libpthread.so # We always create a thread for signals test-xfail-tst-single_threaded-pthread-static = yes +# Bug 25263 +test-xfail-tst-origin = yes CFLAGS-tst-execstack.c += -DDEFAULT_RWX_STACK=1 endif diff --git a/sysdeps/unix/sysv/linux/dl-origin.c b/sysdeps/unix/sysv/linux/dl-origin.c index decdd8ae9e..3c52ba51a6 100644 --- a/sysdeps/unix/sysv/linux/dl-origin.c +++ b/sysdeps/unix/sysv/linux/dl-origin.c @@ -21,6 +21,7 @@ #include #include #include +#include /* On Linux >= 2.1 systems which have the dcache implementation we can get the path of the application from the /proc/self/exe symlink. Try this @@ -72,3 +73,25 @@ _dl_get_origin (void) return result; } + +/* On Linux, readlink on the magic symlinks in /proc/self/fd also has + the same behavior of returning the canonical path from the dcache. + If it does not work, we do not bother to canonicalize. */ + +char * +_dl_canonicalize (int fd) +{ + struct fd_to_filename fdfilename; + char canonical[PATH_MAX]; + char *path = __fd_to_filename (fd, &fdfilename); + int size = INTERNAL_SYSCALL_CALL (readlinkat, AT_FDCWD, path, + canonical, PATH_MAX - 1); + + /* Check if the path was truncated. */ + if (size >= 0 && size < PATH_MAX - 1) + { + canonical[size] = '\0'; + return __strdup (canonical); + } + return NULL; +}