From patchwork Wed Jul 31 20:02:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 815666 Delivered-To: patch@linaro.org Received: by 2002:a5d:4acf:0:b0:367:895a:4699 with SMTP id y15csp900767wrs; Wed, 31 Jul 2024 13:03:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV+n8byBRk9h18kRQESyghdYddviGh6iJ1DA+prT+jvW2qtnvJDAqENaq3HkUptWV/zlcp6y/45D0dx8NulnpVB X-Google-Smtp-Source: AGHT+IFVLivKiEpBGUhNQvhlLgdrEihXCNBh5KwZYDGCfY1aW3fXwE9XprPG0MI01/QJ6HX3mMvy X-Received: by 2002:a05:6214:3d8b:b0:6b5:936d:e5e9 with SMTP id 6a1803df08f44-6bb8d6cccc6mr4126396d6.26.1722456213108; Wed, 31 Jul 2024 13:03:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1722456213; cv=pass; d=google.com; s=arc-20160816; b=iUsMDzL0RZn40SQYthnmlK04idcuGhR2cGpzcnHouDSvj3HMWFY8ohVkcC8CTcDTR3 M010jTvanVY0NStrd310/wYW3heba1cgrjNB1fZkYGNxlm6Zt9n51Ld+8bbmc/rABlh7 PIUJ+L0X6yHEwQN0EWO2qBaKg3WKhFD7B75tO77ZHCLjITWlIILjQeDnL48chMeWUCrK lbULOtx7b9FmUR2cnvLdjzdzz/DyPsTkkFQFKZ2FEnJSDgvuHA/T/pvOO7HO5Q9StPeO hpNJZSd+OdHms7FFa76ph4AyBl75ltYVMpgFhu4XNSr5eYzi1VbS5yhmx1htvAg7Zzzf pdpg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=RHvyD6Fsry74hIiTf1TwSqxwHjliffo16SLcTmTnzGQ=; fh=zKuAMb1nXEL7zl6xmPyPzNI9yuHu1VXmLp48x4QMXiI=; b=YpkIhdGFx81cGN+kCIkfEry/0ElQHkLYY6f4aD/AJIEKXn5ZgU58S0C8a0WH3fpsw/ Jm3KlCF3193Fp1awob10CbmlL4mMA6X3KLJtxg4b4Qjp0iktT8f36VFp0WKfYYvHysq2 QnUnO+e+uCTEJLREySyizLKe7UvwUwP1H3MbOwUd1r04GnHqVE4bDFziZiN8wy1n4C6/ iwOQ8Tn3Ht1Znci1IHmx+P48r2g5OsWkGnp7lJ5ZkvCTEXFOXtFc8LSDDJ2IC7hD+oOp CgV3P8C2vpRSVnnSjR9yX3kAeyoPuXIeBOoemu9q4VCFUbvbLM/djeNECoLf5Sj5yb4B rFvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=qar0Ei83; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 6a1803df08f44-6bb3fb1de2dsi161221756d6.591.2024.07.31.13.03.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 13:03:33 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=qar0Ei83; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 777AE3858C35 for ; Wed, 31 Jul 2024 20:03:32 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id E352A3858C33 for ; Wed, 31 Jul 2024 20:03:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E352A3858C33 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 E352A3858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456199; cv=none; b=Xa1kwJW6hnrECUTC/ONGQzTruaI4DT/bLFEUNQuToJKepcmfBDvY3ZrEyR0ohSThNtYpsu1sf+OhVHnYUGCSyH5zy25QlTnaYPmQpghFJN5lRNhrandIZXvu0L0zhLo5TsDloNi87dbcKymgcBYWSuMtrSLrBlYqPz/wzs13efo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722456199; c=relaxed/simple; bh=0ysnPjRdIheuSDpDvLtlabkeqckUw4zQQ+KkPfVO7bA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EA3TMVItULIFNUqJCV3CoKmQ6qNj+ajp1WxnP/d9l4cleeTpjVZdAY4tdT9byatAMjdHacVeGONWosDJQ8466LZ9RRy6fkIPCmstjd7PVpz7o5MYIsP2uxn84BBmcWcGLfzwX4KbCgAfRZZZnwPXpvkrfr+AVe9rSsJrD5YEm9g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1fc5239faebso42705425ad.1 for ; Wed, 31 Jul 2024 13:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722456196; x=1723060996; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RHvyD6Fsry74hIiTf1TwSqxwHjliffo16SLcTmTnzGQ=; b=qar0Ei83fz5pufylWmlnXcE4gT3oErSnadetGk8KW35BBCH88JN/yBIfoCu1acCaWR qtFqhmUT/6eeLm9bHhApKnS4HHKbdD6f8MQuwiu93Jz2AfqWMXTh+twHKjcux+FalAb0 Pg4bRMRRjvm847R8CVrLR0z+JYpbfL9+peFzKPbTPmrc2/uxwZXOFKZuayvxiPSNgf0g XoCxykYaIId80DS4NYDgplG5upSk5iNaXeeriCMmYLI/ZSa5XKHeGrqHq0JxDhRppJRW 43JbK0fwgXGOqFizvFYmcsE92IxJ31hAwhkqs3bsTh6CEH3+sni1Be881Xy7m0UtRXPP C8sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722456196; x=1723060996; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RHvyD6Fsry74hIiTf1TwSqxwHjliffo16SLcTmTnzGQ=; b=Vei9G5vj32ONMamzEKrl6lWcuMeDyl1a0dimsShcC6lmK6ZIqFp8ENWbyFKzBEUCP0 CDmt/ihSknaUv1FROfk++gwJhjRHzMqm5zVqlFFoG0Nq0pEOjfQ+y6MqZ4ek0jnPzofm 6r8FTpvlJyaFdSLIXHTt/Uarh9hqSxJWFhKwkQO449k5+GItwjKDyGqXdjmHVgij6jUC gAG4czVRTx+iYXF30pAQbkkToOSjB6Dddb8N1C4BMiyuqm5OW7NCacOhrui1PGQrgq+y FCGge6J4Lqlm2vInbKEBOSMNSbR5t0VonDYmxlAGQzlZ8/fzYV7TdQ1rl+AijgnqF/Cq YbjA== X-Gm-Message-State: AOJu0YzmJ1Jk8JBl3TEfxd7Nqk0FwsyOHKUh7KKf4PeTJ/osEwjO3egr hxoFJO4o+lP46jg+9cMFFl3OO4qEt7pDWqSQjggCLZ51Hu8cCd+TynbtbRT/FNhvF+/h/Q4wFcM 1 X-Received: by 2002:a17:902:f707:b0:1f9:f906:9088 with SMTP id d9443c01a7336-1ff4ce7ca63mr4809105ad.22.1722456196109; Wed, 31 Jul 2024 13:03:16 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:1944:b913:6070:fef0:3852]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7fbd8d4sm124106725ad.271.2024.07.31.13.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 13:03:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Stephen Roettger , Jeff Xu , Florian Weimer , Mike Hommey Subject: [PATCH v2 1/5] elf: Use RTLD_NODELETE is more places Date: Wed, 31 Jul 2024 17:02:04 -0300 Message-ID: <20240731200307.2269811-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240731200307.2269811-1-adhemerval.zanella@linaro.org> References: <20240731200307.2269811-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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~patch=linaro.org@sourceware.org * On dlopen dependencies for objects opened with RTLD_NODELETE (since it does not make sense to unload a dependency of a RTLD_NODELETE object); * For the main map executable. * For the main map DT_NEEDED dependencies. * On __libc_unwind_link_get for libgcc_s.so (used for backtrace and unwind). Checked on x86_64-linux-gnu. --- elf/dl-open.c | 3 ++- elf/rtld.c | 6 +++--- include/dlfcn.h | 2 ++ misc/unwind-link.c | 5 +++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/elf/dl-open.c b/elf/dl-open.c index c378da16c0..076b32b977 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -636,7 +636,8 @@ dl_open_worker_begin (void *a) /* Load that object's dependencies. */ _dl_map_object_deps (new, NULL, 0, 0, - mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT)); + mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT + | RTLD_NODELETE)); /* So far, so good. Now check the versions. */ for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i) diff --git a/elf/rtld.c b/elf/rtld.c index bfdf632e77..1090d89bfe 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -811,7 +811,7 @@ do_preload (const char *fname, struct link_map *main_map, const char *where) args.str = fname; args.loader = main_map; - args.mode = __RTLD_SECURE; + args.mode = __RTLD_SECURE | RTLD_NODELETE; unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded; @@ -1638,7 +1638,7 @@ dl_main (const ElfW(Phdr) *phdr, /* Create a link_map for the executable itself. This will be what dlopen on "" returns. */ main_map = _dl_new_object ((char *) "", "", lt_executable, NULL, - __RTLD_OPENEXEC, LM_ID_BASE); + __RTLD_OPENEXEC | RTLD_NODELETE, LM_ID_BASE); assert (main_map != NULL); main_map->l_phdr = phdr; main_map->l_phnum = phnum; @@ -1966,7 +1966,7 @@ dl_main (const ElfW(Phdr) *phdr, RTLD_TIMING_VAR (start); rtld_timer_start (&start); _dl_map_object_deps (main_map, preloads, npreloads, - state.mode == rtld_mode_trace, 0); + state.mode == rtld_mode_trace, RTLD_NODELETE); rtld_timer_accum (&load_time, start); } diff --git a/include/dlfcn.h b/include/dlfcn.h index f49ee1b0c9..06e2ecbdd2 100644 --- a/include/dlfcn.h +++ b/include/dlfcn.h @@ -50,6 +50,8 @@ extern char **__libc_argv attribute_hidden; better error handling semantics for the library. */ #define __libc_dlopen(name) \ __libc_dlopen_mode (name, RTLD_NOW | __RTLD_DLOPEN) +#define __libc_dlopen_nodelete(name) \ + __libc_dlopen_mode (name, RTLD_NODELETE | RTLD_NOW | __RTLD_DLOPEN) extern void *__libc_dlopen_mode (const char *__name, int __mode) attribute_hidden; extern void *__libc_dlsym (void *__map, const char *__name) diff --git a/misc/unwind-link.c b/misc/unwind-link.c index 213a0162a4..7267ecbec3 100644 --- a/misc/unwind-link.c +++ b/misc/unwind-link.c @@ -48,7 +48,7 @@ __libc_unwind_link_get (void) /* Initialize a copy of the data, so that we do not need about unlocking in case the dynamic loader somehow triggers unwinding. */ - void *local_libgcc_handle = __libc_dlopen (LIBGCC_S_SO); + void *local_libgcc_handle = __libc_dlopen_nodelete (LIBGCC_S_SO); if (local_libgcc_handle == NULL) { __libc_lock_unlock (lock); @@ -100,7 +100,8 @@ __libc_unwind_link_get (void) __libc_lock_lock (lock); if (atomic_load_relaxed (&global_libgcc_handle) != NULL) - /* This thread lost the race. Clean up. */ + /* This thread lost the race. Drop the l_direct_opencount and issue + the debug log. */ __libc_dlclose (local_libgcc_handle); else {