From patchwork Tue Dec 3 10:48:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 102299 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A15543858D3C for ; Tue, 3 Dec 2024 10:49:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A15543858D3C Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cozb+/XB 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 7E6C63858D33 for ; Tue, 3 Dec 2024 10:48:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E6C63858D33 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 7E6C63858D33 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=1733222923; cv=none; b=oQyhuna4nLBEev5v6GsH5zaDEKoorANONJ52FhholCrzS6uVrXSje7MxeV5obIUWQ++hF9addb9pLi3Ys6T7Qx/wMaF+T4c5vSu0qofC7zXOi/PeCsWDkwlpwwK5h1OAKLjJjYOLj2jDDjPpyzOehxEm4U63Q98da1AaR2yWvRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733222923; c=relaxed/simple; bh=Dr+fSMRHXHpdnzBTn3yOnateLcPS3+yUUEIn6FsRdKU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=b8bZdH8Yw67daho6uHgFLxZtQErfmPtd9cFY3MaUVD4CQfqFChmF+pMdRo/pnOyM/6gs+Hcwqc9i2MhiT1r0ui3RXDL91JU3mCcB9a0IhZ3DkvW7+pv8ZONxpw2wVG+LCOcpNQWb4QUjwIPfkiqUgaV++/ffYcDwqgpig09OIxg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E6C63858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733222923; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=lVNz0YtBiUywkPzYsqt7K0kos4s0ZEQ8ENHC3/S4s1A=; b=cozb+/XB0y5ooyJEgcxAscj7UNgjrRz9fMYNGWG4EaD7o+eLMJBuptmFBnWxhJ1kJsG5CP wxFr2biKWu25SO2iJN/ObI4zQBhiPal7Yf+F9It++zl5X8Ih91y7IJdwZcgoOjxJXT9mIL hSLopeRJZSaDOczHodtNFSaOhp0rhmg= Received: from mx-prod-mc-04.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-468--h5Ao3HjPdueP8sJST1JkA-1; Tue, 03 Dec 2024 05:48:41 -0500 X-MC-Unique: -h5Ao3HjPdueP8sJST1JkA-1 X-Mimecast-MFC-AGG-ID: -h5Ao3HjPdueP8sJST1JkA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 02A5C19560B7 for ; Tue, 3 Dec 2024 10:48:41 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.6]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 169EC19560A3 for ; Tue, 3 Dec 2024 10:48:39 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] dlfcn: Add RTLD_DI_BASEADDRESS request for dlinfo Date: Tue, 03 Dec 2024 11:48:36 +0100 Message-ID: <87cyi9oxdn.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.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: UU5MHm4-8azidbBFyC5tDMOBnIgxd7B9gfFT7ER0-5U_1733222921 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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~patchwork=sourceware.org@sourceware.org This is what the l_addr link map field looks like at first glance, but it actually works for this purpose. --- dlfcn/Makefile | 8 +++++ dlfcn/dlfcn.h | 6 +++- dlfcn/dlinfo.c | 4 +++ dlfcn/tst-dlinfo-baseaddress-pie.c | 1 + dlfcn/tst-dlinfo-baseaddress.c | 73 ++++++++++++++++++++++++++++++++++++++ manual/dynlink.texi | 21 ++++++----- 6 files changed, 101 insertions(+), 12 deletions(-) base-commit: f43eb2cf30fdff39bda1c2018246d4badabbc576 diff --git a/dlfcn/Makefile b/dlfcn/Makefile index d473773dd4..931a3833e7 100644 --- a/dlfcn/Makefile +++ b/dlfcn/Makefile @@ -67,10 +67,15 @@ tests = \ glrefmain \ tst-dladdr \ tst-dlinfo \ + tst-dlinfo-baseaddress \ tst-rec-dlopen \ tstatexit \ tstcxaatexit \ # tests + +tests-pie = \ + tst-dlinfo-baseaddress-pie \ + # tests-pie endif modules-names = \ bug-atexit1-lib \ @@ -197,3 +202,6 @@ $(objpfx)bug-dl-leaf.out: $(objpfx)bug-dl-leaf-lib-cb.so $(objpfx)bug-dl-leaf-lib-cb.so: $(objpfx)bug-dl-leaf-lib.so $(objpfx)tst-rec-dlopen.out: $(objpfx)moddummy1.so $(objpfx)moddummy2.so + +tst-dlinfo-baseaddress-no-pie = yes +CFLAGS-tst-dlinfo-baseaddress-pie.c += $(pie-ccflag) diff --git a/dlfcn/dlfcn.h b/dlfcn/dlfcn.h index 5747962611..b5faef604b 100644 --- a/dlfcn/dlfcn.h +++ b/dlfcn/dlfcn.h @@ -167,7 +167,11 @@ enum the number of program headers in the array. */ RTLD_DI_PHDR = 11, - RTLD_DI_MAX = 11 + /* Treat ARG as void **, and store the first mapped address of the + object at this location. */ + RTLD_DI_BASEADDRESS = 12, + + RTLD_DI_MAX = 12 }; diff --git a/dlfcn/dlinfo.c b/dlfcn/dlinfo.c index b0feb9362d..cf8434257f 100644 --- a/dlfcn/dlinfo.c +++ b/dlfcn/dlinfo.c @@ -85,6 +85,10 @@ dlinfo_doit (void *argsblock) *(const ElfW(Phdr) **) args->arg = l->l_phdr; args->result = l->l_phnum; break; + + case RTLD_DI_BASEADDRESS: + *(void **) args->arg = (void *) l->l_real->l_map_start; + break; } } diff --git a/dlfcn/tst-dlinfo-baseaddress-pie.c b/dlfcn/tst-dlinfo-baseaddress-pie.c new file mode 100644 index 0000000000..c2af599784 --- /dev/null +++ b/dlfcn/tst-dlinfo-baseaddress-pie.c @@ -0,0 +1 @@ +#include "tst-dlinfo-baseaddress.c" diff --git a/dlfcn/tst-dlinfo-baseaddress.c b/dlfcn/tst-dlinfo-baseaddress.c new file mode 100644 index 0000000000..6e0584487f --- /dev/null +++ b/dlfcn/tst-dlinfo-baseaddress.c @@ -0,0 +1,73 @@ +/* Test for dlinfo (RTLD_DI_BASEADDRESS). + 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 +#include +#include +#include +#include + +/* Defined by the linker. */ +extern char __ehdr_start[]; + +static int +do_test (void) +{ + void *handle, *handle2, *namespace; + void *addr, *addr2; + struct link_map *l; + Lmid_t lmid; + + handle = xdlopen ("", RTLD_LAZY); + TEST_COMPARE (dlinfo (handle, RTLD_DI_BASEADDRESS, &addr), 0); + TEST_COMPARE ((uintptr_t) addr, (uintptr_t) &__ehdr_start); + xdlclose (handle); + + handle = xdlopen (LIBC_SO, RTLD_LAZY); + TEST_COMPARE (dlinfo (handle, RTLD_DI_LINKMAP, &l), 0); + TEST_COMPARE (dlinfo (handle, RTLD_DI_BASEADDRESS, &addr), 0); + TEST_COMPARE ((uintptr_t) addr, l->l_addr); + xdlclose (handle); + + handle2 = xdlopen (LD_SO, RTLD_LAZY); + TEST_COMPARE (dlinfo (handle2, RTLD_DI_LINKMAP, &l), 0); + TEST_COMPARE (dlinfo (handle2, RTLD_DI_BASEADDRESS, &addr2), 0); + TEST_COMPARE ((uintptr_t) addr2, l->l_addr); + TEST_VERIFY (addr2 != addr); + + namespace = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW); + TEST_COMPARE (dlinfo (namespace, RTLD_DI_LINKMAP, &l), 0); + TEST_COMPARE (dlinfo (namespace, RTLD_DI_BASEADDRESS, &addr), 0); + TEST_COMPARE ((uintptr_t) addr, l->l_addr); + TEST_COMPARE (dlinfo (namespace, RTLD_DI_LMID, &lmid), 0); + handle = xdlmopen (lmid, LD_SO, RTLD_NOW); + /* Should have obtained the proxy link map. */ + TEST_VERIFY (handle != handle2); + TEST_COMPARE (dlinfo (handle, RTLD_DI_LINKMAP, &l), 0); + TEST_COMPARE (dlinfo (handle, RTLD_DI_BASEADDRESS, &addr), 0); + TEST_COMPARE ((uintptr_t) addr, l->l_addr); + /* But the mapped address is the same. */ + TEST_COMPARE ((uintptr_t) addr, (uintptr_t) addr2); + xdlclose (handle); + xdlclose (namespace); + + xdlclose (handle2); + return 0; +} + +#include diff --git a/manual/dynlink.texi b/manual/dynlink.texi index 3a4864f146..bf4acfad3a 100644 --- a/manual/dynlink.texi +++ b/manual/dynlink.texi @@ -383,17 +383,9 @@ might use zero as the lowest virtual address. Due to the second variable, values of the @code{l_addr} field do not necessarily uniquely identify a shared object. -On Linux, to obtain the lowest loaded address of the main program, use -@code{getauxval} to obtain the @code{AT_PHDR} and @code{AT_PHNUM} values -for the current process. Alternatively, call -@samp{dlinfo (_r_debug.r_map, &@var{phdr})} -to obtain the number of program headers, and the address of the program -header array will be stored in @var{phdr} -(of type @code{const ElfW(Phdr) *}, as explained below). -These values allow processing the array of program headers and the -address information in the @code{PT_LOAD} entries among them. -This works even when the program was started with an explicit loader -invocation. +To obtain lowest address in memory of an object, independently of object +type, instead of the @code{l_addr} field, use the +@code{RTLD_DI_BASEADDRESS} request for @code{dlinfo}. @item l_name For a shared object, this field contains the file name that the @@ -529,6 +521,13 @@ object of type @code{const ElfW(Phdr) *} (that is, for the current architecture). For this request, the value returned by @code{dlinfo} is the number of program headers in the program header array. + +@item RTLD_DI_BASEADDRESS +This request writes the lowested mapped address of the object associated +with @var{handle} to @code{*@var{arg}}. The @var{arg} argument must be +the address of an object of @code{void *}. The pointer written is never +a null pointer (unlike the @code{l_addr} field of @code{struct +link_map}, which can be zero). @end vtable The @code{dlinfo} function is a GNU extension.