From patchwork Thu May 18 08:28:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: stsp X-Patchwork-Id: 1783062 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=yandex.ru header.i=@yandex.ru header.a=rsa-sha256 header.s=mail header.b=cMTAXWiz; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QMNWV4Ffqz20dn for ; Thu, 18 May 2023 18:32:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A3F2383803A for ; Thu, 18 May 2023 08:32:20 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from forward102b.mail.yandex.net (forward102b.mail.yandex.net [IPv6:2a02:6b8:c02:900:1:45:d181:d102]) by sourceware.org (Postfix) with ESMTPS id 383DC3857724 for ; Thu, 18 May 2023 08:29:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 383DC3857724 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=yandex.ru Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=yandex.ru Received: from mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net [IPv6:2a02:6b8:c0c:1186:0:640:38cb:0]) by forward102b.mail.yandex.net (Yandex) with ESMTP id 1F32460114 for ; Thu, 18 May 2023 11:29:41 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id XTYe39MDZ0U0-bmLxqVgZ; Thu, 18 May 2023 11:29:40 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1684398580; bh=3e+njtjSZzf0T0qO1mowm2r+GG7fOKEeaGRZLBdMsBg=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=cMTAXWizK9ktq4jOxsrmK6RI0btSe0VOoFtwD6sqJHu0/swq5GCf4obV9WCNT36bq X/7JKsvS+rdAUIzoYrpj8PiqQPTqr+B7ygnyDzh4GY+JaIPi/haoA8pQjQCBOYhCkf aLfNfC9Jbsl8OvO18/sgvLe+5il+rlUriT/G/llw= Authentication-Results: mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Stas Sergeev To: libc-alpha@sourceware.org Cc: Stas Sergeev Subject: [PATCH 06/14] move relocation into _dl_object_reloc() func Date: Thu, 18 May 2023 13:28:46 +0500 Message-Id: <20230518082854.3903342-7-stsp2@yandex.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru> References: <20230518082854.3903342-1-stsp2@yandex.ru> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 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 Sender: "Libc-alpha" This is a small refactoring to consolidate the relocation code in a single place. The test-suite was run on x86_64/64 and showed no regressions. Signed-off-by: Stas Sergeev --- elf/dl-close.c | 2 ++ elf/dl-open.c | 55 +++++++++++++++++++++++++++++++++++++++++++++----- include/link.h | 2 ++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/elf/dl-close.c b/elf/dl-close.c index b887a44888..48ac3663ec 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -690,6 +690,8 @@ _dl_close_worker (struct link_map *map, bool force) if (imap == GL(dl_initfirst)) GL(dl_initfirst) = NULL; + free (imap->l_dlopen_args); + free (imap); } } diff --git a/elf/dl-open.c b/elf/dl-open.c index 687a393e0d..e553403e8b 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -666,6 +666,31 @@ do_reloc_2 (struct link_map *new, int mode, struct dl_open_args *args) add_to_global_update (new); } +static void +dl_reloc_worker_begin (void *a) +{ + struct dl_open_args *args = a; + do_reloc_1 (args->map, args->mode, args->nsid, !args->libc_already_loaded); +} + +static void +_dl_object_reloc (struct link_map *l) +{ + struct dl_exception ex; + int err; + struct dl_open_args *args = l->l_dlopen_args; + int mode = args->mode; + + /* Protects global and module specific TLS state. */ + __rtld_lock_lock_recursive (GL(dl_load_tls_lock)); + err = _dl_catch_exception (&ex, dl_reloc_worker_begin, args); + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); + if (__glibc_unlikely (ex.errstring != NULL)) + /* Reraise the error. */ + _dl_signal_exception (err, &ex, NULL); + do_reloc_2 (l, mode, args); +} + static void dl_open_worker_begin (void *a) { @@ -775,6 +800,27 @@ dl_open_worker_begin (void *a) _dl_map_object_deps (new, NULL, 0, 0, mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT)); + args->worker_continue = true; + if (!new->l_dlopen_args) + { + if (new->l_type == lt_loaded) + { + new->l_dlopen_args = (struct dl_open_args *) + malloc (sizeof (struct dl_open_args)); + if (new->l_dlopen_args == NULL) + _dl_signal_error (ENOMEM, new->l_name, NULL, + N_("cannot allocate memory buffer")); + memcpy (new->l_dlopen_args, args, sizeof (*args)); + } + else + new->l_dlopen_args = args; + } + else + { + assert (new->l_type == lt_loaded); + memcpy (new->l_dlopen_args, args, sizeof (*args)); + } + #ifdef SHARED /* Auditing checkpoint: we have added all objects. */ _dl_audit_activity_nsid (new->l_ns, LA_ACT_CONSISTENT); @@ -791,9 +837,6 @@ dl_open_worker_begin (void *a) /* Print scope information. */ if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES)) _dl_show_scope (new, 0); - - args->worker_continue = true; - do_reloc_1 (new, mode, args->nsid, !args->libc_already_loaded); } static void @@ -820,10 +863,12 @@ dl_open_worker (void *a) if (!args->worker_continue) return; - int mode = args->mode; struct link_map *new = args->map; - do_reloc_2 (new, mode, args); + _dl_object_reloc (new); + /* For !lt_loaded we do not malloc(), so needs to null out here. */ + if (new->l_type != lt_loaded) + new->l_dlopen_args = NULL; /* Let the user know about the opencount. */ if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) diff --git a/include/link.h b/include/link.h index 1d74feb2bd..03b9f82715 100644 --- a/include/link.h +++ b/include/link.h @@ -347,6 +347,8 @@ struct link_map size_t l_relro_size; unsigned long long int l_serial; + + void *l_dlopen_args; }; #include