From patchwork Fri Jun 10 16:35:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 55023 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 7FAA5385AE7D for ; Fri, 10 Jun 2022 16:37:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FAA5385AE7D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654879063; bh=sBtvDfPL2GyBY6Ot9Jds/K/8+rkxeeZ3UL+Y8bCTEbc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=yosYkXmVR9k9nhbTaOEnr7RsVPFfn65z3VPJAoqYMkmXfdrpUDwWTrZsiM9CRuzIx GEJz9j6sIsKIfMBl/f57+VSeO6ftZ3IcTKbEKsr2IOlwAlEORazaOh/4I5fJcUObDM pbk0c6EUqPns5BZwGmW4SZ3xHogemz0fbMbXDPmE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 6166938376E9 for ; Fri, 10 Jun 2022 16:35:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6166938376E9 Received: by mail-oi1-x234.google.com with SMTP id w16so26330191oie.5 for ; Fri, 10 Jun 2022 09:35:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sBtvDfPL2GyBY6Ot9Jds/K/8+rkxeeZ3UL+Y8bCTEbc=; b=wiRObC/+zOO6+XLU7IJTPBRCatCWF+XRQL4NPgDe8OZ3MnlXHJltm5gcaP35Nkg4Da C8IxYdGTkqCss9BV9m8RcbmLbnC+uEmGrGUL2LK30U1Zwxv4AuG2MqgEnia++BS9XBgm 9XqA1kzRpl9XnhMoqfcSEcPvDwHrPsxgZRNoJlFwRv3Wd3bNBfmApVt4+/sX/ROuOc21 J5HZYFXBbq0gulnT5HX2JLw7zVJivf6k2SR1thzdIXwnJfdCR3AeOncti+cD7vv+VVai waf00OiPjqz7yQuUQMlzQXNbMbaECjUnAJXQhlVf6Syp8TFCJ790AF08uCodMoimXXrw At9Q== X-Gm-Message-State: AOAM533ywNr9SG+2QgiMlX21I6r7fIW5bZA0vSzJAww70356Fjz/rAFk 5gWpc7v0SODucXyyB8T8Lbb5wiL+HYgK9Q== X-Google-Smtp-Source: ABdhPJzz+R3KYu3RJnrQagOdZq7+xxpZgtiBEc8zjN5uvDzSpcvwEvLpRobkBxm/RKEnXkohg7uIHQ== X-Received: by 2002:a05:6808:171a:b0:2f9:ac95:fc5b with SMTP id bc26-20020a056808171a00b002f9ac95fc5bmr350400oib.191.1654878958409; Fri, 10 Jun 2022 09:35:58 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:a613:818b:b86c:a3f8:d455]) by smtp.gmail.com with ESMTPSA id p203-20020aca42d4000000b0032efe5871b0sm1262424oia.45.2022.06.10.09.35.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jun 2022 09:35:58 -0700 (PDT) To: libc-alpha@sourceware.org, Wilco Dijkstra Subject: [PATCH v2 1/4] misc: Optimize internal usage of __libc_single_threaded Date: Fri, 10 Jun 2022 13:35:49 -0300 Message-Id: <20220610163552.3587064-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220610163552.3587064-1-adhemerval.zanella@linaro.org> References: <20220610163552.3587064-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" By adding an internal hidden_def alias to avoid the GOT indirection. On some architecture, __libc_single_thread may be accessed through copy relocations and thus it requires to update both copies. To obtain the correct address of the __libc_single_thread, __libc_dlsym is extended to support RTLD_DEFAULT. It searches through all scope instead of default local ones. Checked on x86_64-linux-gnu and i686-linux-gnu. --- elf/dl-libc.c | 20 ++++++++++++++++++-- elf/libc_early_init.c | 9 +++++++++ include/sys/single_threaded.h | 11 +++++++++++ misc/single_threaded.c | 2 ++ nptl/pthread_create.c | 6 +++++- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/elf/dl-libc.c b/elf/dl-libc.c index 266e068da6..e64f4b9910 100644 --- a/elf/dl-libc.c +++ b/elf/dl-libc.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -72,6 +73,7 @@ struct do_dlsym_args /* Arguments to do_dlsym. */ struct link_map *map; const char *name; + const void *caller_dlsym; /* Return values of do_dlsym. */ lookup_t loadbase; @@ -102,8 +104,21 @@ do_dlsym (void *ptr) { struct do_dlsym_args *args = (struct do_dlsym_args *) ptr; args->ref = NULL; - args->loadbase = GLRO(dl_lookup_symbol_x) (args->name, args->map, &args->ref, - args->map->l_local_scope, NULL, 0, + struct link_map *match = args->map; + struct r_scope_elem **scope; + if (args->map == RTLD_DEFAULT) + { + ElfW(Addr) caller = (ElfW(Addr)) args->caller_dlsym; + match = _dl_find_dso_for_object (caller); + /* It is only used internally, so caller should be always recognized. */ + assert (match != NULL); + scope = match->l_scope; + } + else + scope = args->map->l_local_scope; + + args->loadbase = GLRO(dl_lookup_symbol_x) (args->name, match, &args->ref, + scope, NULL, 0, DL_LOOKUP_RETURN_NEWEST, NULL); } @@ -182,6 +197,7 @@ __libc_dlsym (void *map, const char *name) struct do_dlsym_args args; args.map = map; args.name = name; + args.caller_dlsym = RETURN_ADDRESS (0); #ifdef SHARED if (GLRO (dl_dlfcn_hook) != NULL) diff --git a/elf/libc_early_init.c b/elf/libc_early_init.c index 3c4a19cf6b..7cc2997122 100644 --- a/elf/libc_early_init.c +++ b/elf/libc_early_init.c @@ -16,7 +16,9 @@ License along with the GNU C Library; if not, see . */ +#include #include +#include #include #include #include @@ -38,6 +40,13 @@ __libc_early_init (_Bool initial) __libc_single_threaded = initial; #ifdef SHARED + /* __libc_single_threaded can be accessed through copy relocations, so it + requires to update the external copy. */ + __libc_external_single_threaded = __libc_dlsym (RTLD_DEFAULT, + "__libc_single_threaded"); + assert (__libc_external_single_threaded != NULL); + *__libc_external_single_threaded = initial; + __libc_initial = initial; #endif diff --git a/include/sys/single_threaded.h b/include/sys/single_threaded.h index 18f6972482..258b01e0b2 100644 --- a/include/sys/single_threaded.h +++ b/include/sys/single_threaded.h @@ -1 +1,12 @@ #include + +#ifndef _ISOMAC + +libc_hidden_proto (__libc_single_threaded); + +# ifdef SHARED +extern __typeof (__libc_single_threaded) *__libc_external_single_threaded + attribute_hidden; +# endif + +#endif diff --git a/misc/single_threaded.c b/misc/single_threaded.c index 96ada9137b..201d86a273 100644 --- a/misc/single_threaded.c +++ b/misc/single_threaded.c @@ -22,6 +22,8 @@ __libc_early_init (as false for inner libcs). */ #ifdef SHARED char __libc_single_threaded; +__typeof (__libc_single_threaded) *__libc_external_single_threaded; #else char __libc_single_threaded = 1; #endif +libc_hidden_data_def (__libc_single_threaded) diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index e7a099acb7..5633d01c62 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -627,7 +627,11 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, if (__libc_single_threaded) { late_init (); - __libc_single_threaded = 0; + __libc_single_threaded = +#ifdef SHARED + *__libc_external_single_threaded = +#endif + 0; } const struct pthread_attr *iattr = (struct pthread_attr *) attr;