From patchwork Fri Nov 8 13:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 2008464 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fcD0bTXM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XlL2f1Wbrz1xyM for ; Sat, 9 Nov 2024 00:51:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B9BEC3858405 for ; Fri, 8 Nov 2024 13:51:35 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id AB7E33858D3C for ; Fri, 8 Nov 2024 13:51:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB7E33858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AB7E33858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731073876; cv=none; b=nWsRjGw34u/VF/GyprRAnpYlWHy1eCa5xJoEDeWaC2HlYj/b2qm7nM41NL1pusnXmkQNNNdjOA5EW8Q9KHqxzXqyEq+Xo9elNP7/rspVPEXo2RHIXXjRLv8t/qUrsmxTJo6C6ZSaC1b+B17w75UgWueol+keoBGGxw98tvCbwXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731073876; c=relaxed/simple; bh=dtbdR9PUajmvCNczvR7AXRp9CSTd7sXv/CFohZfgaUk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=re/PBKpdGmieyqV04zN1u2OfcimKFtRPox5kbrP6Ot+Lp5Unu6vA1QlZrYSx790rBSat/8gJ29Wi2TpHpIRJVfUSeeIM80Pp2c+x1jQJ4BIBIyGLTF3BhDKDn8KohGX5UXeOMsHy4afyTELel++OORlCUCP/Fdwy0i3AswtHGAI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731073872; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=abOEZmKyhi4VS6p499oap16USzOZI764n3HYnGakKnQ=; b=fcD0bTXM12wxtuMd3QBtVyS/VrZYPBsKujewazRYpEsxs0UFA3nmajuZVmc7tne+GKBziH 0T5PbgH367NnzC86ju6DZU24aZtkBWEiOYg8QeZQcmf/9sGtzdJ3mc/M0fNim9rWL716ST DBZW1FuDymdTeQSYhpJQeX9fyJr54hw= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-76-Nkr9mcF3M-OXeaJJs52FVg-1; Fri, 08 Nov 2024 08:51:09 -0500 X-MC-Unique: Nkr9mcF3M-OXeaJJs52FVg-1 X-Mimecast-MFC-AGG-ID: Nkr9mcF3M-OXeaJJs52FVg Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 694F11955EE7; Fri, 8 Nov 2024 13:51:08 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.32]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 56834195607C; Fri, 8 Nov 2024 13:51:06 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Cc: Cristian =?utf-8?q?Rodr=C3=ADguez?= Subject: [PATCH] elf: Add back support for programs that do not load libc.so Date: Fri, 08 Nov 2024 14:51:03 +0100 Message-ID: <87ttchal6g.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: zxYqpXQuVqk81UaiBejCkeOmWxpVYPT_zHkgEcpp-UU_1731073868 X-Mimecast-Originator: redhat.com 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~incoming=patchwork.ozlabs.org@sourceware.org Commit 8f8dd904c4a2207699bb666f30acceb5209c8d3f (“elf: rtld_multiple_ref is always true”) removed some code that happened to enable compatibility with programs that do not link against libc.so. Such programs cannot call dlopen or any dynamic linker functions (except __tls_get_addr), so this is not really useful. Still ld.so should not crash with a null-pointer dereference or undefined symbol reference in these cases. Tested on i686-linux-gnu, x86_64-linux-gnu, powerpc64le-linux-gnu. Built with build-many-glibcs.py. --- elf/Makefile | 11 +++++++++++ elf/dl-minimal.c | 5 +++++ sysdeps/nptl/dl-mutex.c | 4 ++++ sysdeps/unix/sysv/linux/Makefile | 2 ++ sysdeps/unix/sysv/linux/arm/Makefile | 3 +++ sysdeps/unix/sysv/linux/tst-nolink-libc.c | 25 +++++++++++++++++++++++++ 6 files changed, 50 insertions(+) base-commit: f745d78e2628cd5b13ca119ae0c0e21d08ad1906 diff --git a/elf/Makefile b/elf/Makefile index 3a1cb72955..931332e91d 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -3169,3 +3169,14 @@ tst-rtld-no-malloc-audit-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so # Any shared object should do. tst-rtld-no-malloc-preload-ENV = LD_PRELOAD=$(objpfx)tst-auditmod1.so + +# These rules link (without libc.so) and run the special +# elf/tst-nolink-libc test if a port adds it to the $(tests) or +# $(tests-internals) variables. The test is always run directly, not +# under the dynamic linker. +$(objpfx)tst-nolink-libc: $(objpfx)tst-nolink-libc.o $(objpfx)ld.so + $(LINK.o) -nostdlib -nostartfiles -o $@ $< \ + -Wl,--dynamic-link=$(objpfx)ld.so $(objpfx)ld.so +CFLAGS-tst-nolink-libc.c += $(no-stack-protector) +$(objpfx)tst-nolink-libc.out: $(objpfx)tst-nolink-libc $(objpfx)ld.so + $< > $@ 2>&1; $(evaluate-test) diff --git a/elf/dl-minimal.c b/elf/dl-minimal.c index dfa0764f58..9a66dad361 100644 --- a/elf/dl-minimal.c +++ b/elf/dl-minimal.c @@ -82,6 +82,11 @@ __rtld_malloc_init_real (struct link_map *main_map) rtld relocation (which enables RELRO, after which the pointer variables cannot be written to). */ + if (GL (dl_ns)[LM_ID_BASE].libc_map == NULL) + /* Nothing is against libc.so, which means that there is no malloc + implementation available. */ + return; + struct r_found_version version; version.name = symbol_version_string (libc, GLIBC_2_0); version.hidden = 0; diff --git a/sysdeps/nptl/dl-mutex.c b/sysdeps/nptl/dl-mutex.c index 8d123cbb2f..85c71c1f81 100644 --- a/sysdeps/nptl/dl-mutex.c +++ b/sysdeps/nptl/dl-mutex.c @@ -35,6 +35,10 @@ __rtld_mutex_init (void) constructor while holding loader locks. */ struct link_map *libc_map = GL (dl_ns)[LM_ID_BASE].libc_map; + if (libc_map == NULL) + /* Special case: the program links against ld.so, but not libc.so, + so the full locking implementation is not available. */ + return; const ElfW(Sym) *sym = _dl_lookup_direct (libc_map, "pthread_mutex_lock", diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 527c7a5ae8..3c03e45c9b 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -652,6 +652,8 @@ install-bin += \ # install-bin $(objpfx)pldd: $(objpfx)xmalloc.o + +tests-internal += tst-nolink-libc endif ifeq ($(subdir),rt) diff --git a/sysdeps/unix/sysv/linux/arm/Makefile b/sysdeps/unix/sysv/linux/arm/Makefile index a73c897f43..e73ce4f811 100644 --- a/sysdeps/unix/sysv/linux/arm/Makefile +++ b/sysdeps/unix/sysv/linux/arm/Makefile @@ -1,5 +1,8 @@ ifeq ($(subdir),elf) sysdep-rtld-routines += aeabi_read_tp libc-do-syscall +# The test uses INTERNAL_SYSCALL_CALL. In thumb mode, this uses +# an undefined reference to __libc_do_syscall. +CFLAGS-tst-nolink-libc.c += -marm endif ifeq ($(subdir),misc) diff --git a/sysdeps/unix/sysv/linux/tst-nolink-libc.c b/sysdeps/unix/sysv/linux/tst-nolink-libc.c new file mode 100644 index 0000000000..817f37784b --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-nolink-libc.c @@ -0,0 +1,25 @@ +/* Test program not linked against libc.so and not using any glibc functions. + Copyright (C) 2024 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 + +void +_start (void) +{ + INTERNAL_SYSCALL_CALL (exit_group, 0); +}