From patchwork Thu Jun 24 13:49:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43995 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 32FDC3888C51 for ; Thu, 24 Jun 2021 13:51:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32FDC3888C51 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624542696; bh=iNZ2J08RTW6liljTviM+v4P9y2QdNYe/+TASlhLSYTI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=m3PmqerHIISYzWmUPJ1Ggg7BACpuc6RQJDjfZYzS7jW1lNDdkn8gtqEpmwtE5Mxsh vmwwNLeuUCVBhPvipRWTfqUwUp9sOi0cOvnMqHZc9ZUK0bEtET/psM73wPOH4sfrWV fTh3+0r77Cw8VW2HmEaF90PZi3ubKXGVJdydJAb8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 469E83888026 for ; Thu, 24 Jun 2021 13:49:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 469E83888026 Received: by mail-pf1-x431.google.com with SMTP id s14so3946550pfg.0 for ; Thu, 24 Jun 2021 06:49:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iNZ2J08RTW6liljTviM+v4P9y2QdNYe/+TASlhLSYTI=; b=H2WDQSam8PYWg1vg9Rh1IH5HoJREcL1lJ8/yYi5DhN5PmqbRXNps+lxllSNDlHuuHX z06uZ0ewjNevzKKLN92xvGVlE8w/Kb86lRJ80gpdAOd3h/w5yqXGdMjQllVLA8rGfS7/ Dtde7JDqFNzUncjPgTqst/k4kA1sZ/WJeZAkpTkTv86yVlYgUGVxjidvm3NsUKz8gMZ2 BnkWtHF5SZTCx/2srlTUFi0+QqXHvZZBKFdxh1A/2Unyp1iuoddP36YrM0FMKubLsJUP j5ZOf/BJolTM3pR06NJgGD4pUI5kJGX7n7vJRp/gdB5GhWBu3IfTcGAABD2KXJwGTYgP d2eA== X-Gm-Message-State: AOAM5311lY7HkIbVJLaijpVqam5Rgn9TGy8Gdhn87eD5b6P3uoQO/AP0 f5iryCDcx7IJz2OLQ1BNWBU= X-Google-Smtp-Source: ABdhPJwP7BYi6DMazJ4rtlhvkrPsOz/fogKGFpb1Nh+THbrZ+B8wdvqkNYRm0Qtxjy6Gr0WF9/SwWA== X-Received: by 2002:a65:44c1:: with SMTP id g1mr4903695pgs.76.1624542580423; Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id q18sm2546155pgj.8.2021.06.24.06.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id D11BCC0337; Thu, 24 Jun 2021 06:49:38 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 1/4] Initial support for GNU_PROPERTY_1_NEEDED Date: Thu, 24 Jun 2021 06:49:35 -0700 Message-Id: <20210624134938.2025098-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210624134938.2025098-1-hjl.tools@gmail.com> References: <20210624134938.2025098-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3032.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 1. Add GNU_PROPERTY_1_NEEDED: #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO to indicate the needed properties by the object file. 2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS: #define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0) to indicate that the object file requires canonical function pointers and cannot be used with copy relocation. 3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed. --- elf/elf.h | 17 +++++++++++++++++ sysdeps/generic/dl-prop.h | 9 ++++++++- sysdeps/generic/link_map.h | 3 ++- sysdeps/x86/dl-prop.h | 19 ++++++++++++++----- sysdeps/x86/link_map.h | 2 ++ 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/elf/elf.h b/elf/elf.h index 2a62b98d4a..dd661a6a64 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -1310,6 +1310,23 @@ typedef struct /* No copy relocation on protected data symbol. */ #define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2 +/* A 4-byte unsigned integer property: A bit is set if it is set in all + relocatable inputs. */ +#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000 +#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff + +/* A 4-byte unsigned integer property: A bit is set if it is set in any + relocatable inputs. */ +#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000 +#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff + +/* The needed properties by the object file. */ +#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO + +/* Set if the object file requires canonical function pointers and + cannot be used with copy relocation. */ +#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0) + /* Processor-specific semantics, lo */ #define GNU_PROPERTY_LOPROC 0xc0000000 /* Processor-specific semantics, hi */ diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h index eaee8052b6..207aadb35d 100644 --- a/sysdeps/generic/dl-prop.h +++ b/sysdeps/generic/dl-prop.h @@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline)) _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, uint32_t datasz, void *data) { - return 0; + /* Continue until GNU_PROPERTY_1_NEEDED is found. */ + if (type == GNU_PROPERTY_1_NEEDED) + { + if (datasz == 4) + l->l_1_needed = *(unsigned int *) data; + return 0; + } + return 1; } #endif /* _DL_PROP_H */ diff --git a/sysdeps/generic/link_map.h b/sysdeps/generic/link_map.h index a056184690..9f482b8c20 100644 --- a/sysdeps/generic/link_map.h +++ b/sysdeps/generic/link_map.h @@ -1 +1,2 @@ -/* No architecture specific definitions. */ +/* GNU_PROPERTY_1_NEEDED of this object. */ +unsigned int l_1_needed; diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index 56bd020b3c..385548fad3 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -97,6 +97,7 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, const ElfW(Addr) start = (ElfW(Addr)) note; + unsigned int needed_1 = 0; unsigned int feature_1_and = 0; unsigned int isa_1_needed = 0; unsigned int last_type = 0; @@ -141,7 +142,8 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, last_type = type; if (type == GNU_PROPERTY_X86_FEATURE_1_AND - || type == GNU_PROPERTY_X86_ISA_1_NEEDED) + || type == GNU_PROPERTY_X86_ISA_1_NEEDED + || type == GNU_PROPERTY_1_NEEDED) { /* The sizes of types which we are searching for are 4 bytes. There is no point to continue if this @@ -151,12 +153,18 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, /* NB: Stop the scan only after seeing all types which we are searching for. */ - _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > - GNU_PROPERTY_X86_FEATURE_1_AND), + _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED + > GNU_PROPERTY_X86_FEATURE_1_AND) + && (GNU_PROPERTY_X86_FEATURE_1_AND + > GNU_PROPERTY_1_NEEDED)), "GNU_PROPERTY_X86_ISA_1_NEEDED > " - "GNU_PROPERTY_X86_FEATURE_1_AND"); + "GNU_PROPERTY_X86_FEATURE_1_AND && " + "GNU_PROPERTY_X86_FEATURE_1_AND > " + "GNU_PROPERTY_1_NEEDED"); if (type == GNU_PROPERTY_X86_FEATURE_1_AND) feature_1_and = *(unsigned int *) ptr; + else if (type == GNU_PROPERTY_1_NEEDED) + needed_1 = *(unsigned int *) ptr; else { isa_1_needed = *(unsigned int *) ptr; @@ -187,9 +195,10 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, } /* We get here only if there is one or no GNU property note. */ - if (isa_1_needed != 0 || feature_1_and != 0) + if (needed_1 != 0 || isa_1_needed != 0 || feature_1_and != 0) { l->l_property = lc_property_valid; + l->l_1_needed = needed_1; l->l_x86_isa_1_needed = isa_1_needed; l->l_x86_feature_1_and = feature_1_and; } diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h index 4c46a25f83..0c7e25dc96 100644 --- a/sysdeps/x86/link_map.h +++ b/sysdeps/x86/link_map.h @@ -29,3 +29,5 @@ unsigned int l_x86_feature_1_and; /* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ unsigned int l_x86_isa_1_needed; + +#include From patchwork Thu Jun 24 13:49:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43999 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 9E4783888C78 for ; Thu, 24 Jun 2021 13:54:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E4783888C78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624542881; bh=mipaf1W5Jt/uuKOr2T+4wihl2swjwa96Jxu97vvaHw8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QAOtLUUKIfZOhGlbaq2JVYPyA7SopzcITTRJXzlIT8nMS/OHy2yOtVdC+EW4Bv2EV +Ad/oTCoB8kmtxDHvXHy2EZZtFye53iRvFjZ/k/WdESG0uFt0PW3fUYm/TPHwgs+Na TpY0CLLNP8ooRp9OFIyDX61vc6rUcHWBsHXdD4LA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 72E343888C58 for ; Thu, 24 Jun 2021 13:49:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 72E343888C58 Received: by mail-pf1-x42f.google.com with SMTP id t32so5231969pfg.2 for ; Thu, 24 Jun 2021 06:49:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mipaf1W5Jt/uuKOr2T+4wihl2swjwa96Jxu97vvaHw8=; b=CJgDdj5wwnZBcG52aWXejG7jGqPEWeydkjFttyyFT/O7czaC+ltlv0y8nvFqWgUgzL OTyJCpTcJSrns1xVeD5kkH8sXOvhic5JzHttSj5iOhfRHLv9yavJ3nDugQXuCV9oey4T 3ZtvgeojcVD9y91gBzy4MkESJ906poY01Xwg9pXRRxzF37Ztor6XoMQnP2582vC9BIQf dfE1MxMSFy2fmgChcUDn6r76sgqhxORenqf8UzU3zipSzam0TEdxUSfJHDpaE0qiLXpj dUCQnTMMfL8awOSWdOqKfWNH8P8/3T43ZkZ5skMlYC3dOsamTBz2UdyMOY1DqWb5bATT IcBA== X-Gm-Message-State: AOAM531dqUWcxgjGLz8YreunS8Qcs5JjOmSSvc857ylrXhsG7RYoTZhh g9lZL/DPEIEhyqdWGJ6MY7QWrq9Jf7E= X-Google-Smtp-Source: ABdhPJyvFjN1G7ohFeom7Jww0LqWiUkwa+ouRHGyzvjkHMG2Mj25d/T32NX3lAE9fRodFDiRR9PpuA== X-Received: by 2002:a65:5189:: with SMTP id h9mr4764335pgq.353.1624542580575; Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id v21sm3193501pfu.77.2021.06.24.06.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id DDE84C035E; Thu, 24 Jun 2021 06:49:38 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/4] Check -z indirect-extern-access and -fno-direct-extern-access Date: Thu, 24 Jun 2021 06:49:36 -0700 Message-Id: <20210624134938.2025098-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210624134938.2025098-1-hjl.tools@gmail.com> References: <20210624134938.2025098-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3031.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 1. Check linker support for -z indirect-extern-access. If GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS is set on any input relocatable files: a. Don't generate copy relocations. b. Turn off extern_protected_data since it implies GNU_PROPERTY_NO_COPY_ON_PROTECTED. c. Treate reference to protected symbols with indirect external access as local. d. Set GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS on output. e. When generating executable, clear this bit when there are non-GOT or non-PLT relocations in input relocatable files without the bit set. f. Add -z [no]indirect-extern-access to control indirect external access. 2. Check compiler support for -fno-direct-extern-access: a. Generate an indirect external access marker in relocatable objects. i. Always use GOT to access undefined data and function symbols, including in PIE and non-PIE. These will avoid copy relocations in executables. ii. This is compatible with existing executables and shared libraries. b. In executable and shared library, bind symbols with the STV_PROTECTED visibility locally: i. The address of data symbol is the address of data body. ii. For systems without function descriptor, the function pointer is the address of function body. iii. The resulting shared libraries may not be incompatible with executables which have copy relocations on protected symbols. Size comparison of non-PIE builds with GCC 12 -O2: 1. On x86-64: text data bss dec hex filename 189958 9304 416 199678 30bfe ld.so (original) 189974 9304 416 199694 30c0e ld.so (-fno-direct-extern-access) 1922458 20240 52432 1995130 1e717a libc.so (original) 1922474 20240 52432 1995146 1e718a libc.so (-fno-direct-extern-access) 49321 1363 192 50876 c6bc iconv_prog (original) 47053 3638 120 50811 c67b iconv_prog (-fno-direct-extern-access) 261978 10339 744 273061 42aa5 localedef (original) 233344 41734 648 275726 4350e localedef (-fno-direct-extern-access) The size difference in localedef mainly comes from .data.rel.ro .data.rel.ro 0x000005 (original) .data.rel.ro 0x007a88 (-fno-direct-extern-access) For example, with -fno-direct-extern-access, localedef.o has 172 relocation entries against section '.rela.data.rel.ro.local' vs none without -fno-direct-extern-access. --- configure | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/configure b/configure index 9619c10991..093c59e25e 100755 --- a/configure +++ b/configure @@ -5746,6 +5746,65 @@ fi $as_echo "$libc_cv_insert" >&6; } +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking -z indirect-extern-access" >&5 +$as_echo_n "checking -z indirect-extern-access... " >&6; } +if ${libc_cv_z_indirect_extern_access+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat > conftest.c <&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; }; then + libc_cv_z_indirect_extern_access=yes + else + libc_cv_z_indirect_extern_access=no + fi + rm -f conftest.* + +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_z_indirect_extern_access" >&5 +$as_echo "$libc_cv_z_indirect_extern_access" >&6; } +config_vars="$config_vars +have-z-indirect-extern-access = $libc_cv_z_indirect_extern_access" + +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -fno-direct-extern-access" >&5 +$as_echo_n "checking for -fno-direct-extern-access... " >&6; } +if ${libc_cv_fno_direct_extern_access+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat > conftest.c <&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; }; then + libc_cv_fno_direct_extern_access=yes + else + libc_cv_fno_direct_extern_access=no + fi + rm -f conftest* +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_fno_direct_extern_access" >&5 +$as_echo "$libc_cv_fno_direct_extern_access" >&6; } +config_vars="$config_vars +have-fno-direct-extern-access = $libc_cv_fno_direct_extern_access" + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for broken __attribute__((alias()))" >&5 $as_echo_n "checking for broken __attribute__((alias()))... " >&6; } if ${libc_cv_broken_alias_attribute+:} false; then : diff --git a/configure.ac b/configure.ac index 34ecbba540..195abe0827 100644 --- a/configure.ac +++ b/configure.ac @@ -1222,6 +1222,43 @@ EOF ]) AC_SUBST(libc_cv_insert) +AC_CACHE_CHECK(-z indirect-extern-access, + libc_cv_z_indirect_extern_access, + [cat > conftest.c <&AS_MESSAGE_LOG_FD]); then + libc_cv_z_indirect_extern_access=yes + else + libc_cv_z_indirect_extern_access=no + fi + rm -f conftest.* + ]) +LIBC_CONFIG_VAR([have-z-indirect-extern-access], + [$libc_cv_z_indirect_extern_access]) + +AC_CACHE_CHECK(for -fno-direct-extern-access, + libc_cv_fno_direct_extern_access, + [cat > conftest.c <&AS_MESSAGE_LOG_FD]); then + libc_cv_fno_direct_extern_access=yes + else + libc_cv_fno_direct_extern_access=no + fi + rm -f conftest*]) +LIBC_CONFIG_VAR([have-fno-direct-extern-access], + [$libc_cv_fno_direct_extern_access]) + AC_CACHE_CHECK(for broken __attribute__((alias())), libc_cv_broken_alias_attribute, [cat > conftest.c < X-Patchwork-Id: 43997 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 F3F9C3888C72 for ; Thu, 24 Jun 2021 13:53:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F3F9C3888C72 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624542785; bh=+HlTecsglP7wvJWjG+sM2DMD/mv4LGSbVS/NlQoAPnE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=e5IKbJSq/jtjdVudfXkYT094atSiVy9SuopJ0JUHUwYRpoW+cR1NEed/NBT03ZSR1 xT/3zYoTzws0brP6uaFzYOQ/oDTdUN+YrDRlDykNgt+WKkHTNrLXEOJ7Z1axzH16i8 1ykHqLqekj7TeQ51VuhUp8mFwD2NF25cnahyQcJA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id A0F083888019 for ; Thu, 24 Jun 2021 13:49:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0F083888019 Received: by mail-pl1-x634.google.com with SMTP id v13so2982368ple.9 for ; Thu, 24 Jun 2021 06:49:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+HlTecsglP7wvJWjG+sM2DMD/mv4LGSbVS/NlQoAPnE=; b=Bk8/v2pueEQ5v4TUU01cVqFyw0HC1YZxePvqQkm5P2mdEJ/JPlEMa4LGzrtXCI3cH9 Gcn6JIObQLChqh6A9FF3h7QcR7ihEp4NrmetFH6v85JVNeU15LFL6W7T7Sw1R/k2MgLz qfTT3B8ia51B1n8ixGQjiqT/25SHCh9xoS6DFlHVOLivuqO4o87+DxhU0bpzsZJtqJNl 7YApt1EI+FRI99auPArZud7irmV1Q4RJQAbOpCda1IGncRPu5waU8tiGGOQoJo+vjGD4 GjLNlUmk2Oc1Np6f2jjZvr9LQ7+4qIA0gkgT7XHpg4OwtpCC3UkpBBhUULfhckSjx8l9 stLQ== X-Gm-Message-State: AOAM533yeZuNoRXz6OclygtkY31p1DKG2rjpZ5XwL32nQ3n5Vt7qee+s bcY+W83sqra5Cui6WQrx2lI= X-Google-Smtp-Source: ABdhPJzkJ4eV2QDkGoEZH8kTWeXSxwMCKoZWcjmnHbAqw9JiJoo7f2keZ4/McccdUSDdvi0feE4AVA== X-Received: by 2002:a17:90a:1c02:: with SMTP id s2mr5523399pjs.172.1624542580842; Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id b18sm8701516pjq.2.2021.06.24.06.49.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id E9FEEC04CF; Thu, 24 Jun 2021 06:49:38 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/4] Add run-time chesk for indirect external access Date: Thu, 24 Jun 2021 06:49:37 -0700 Message-Id: <20210624134938.2025098-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210624134938.2025098-1-hjl.tools@gmail.com> References: <20210624134938.2025098-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3032.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When performing symbol lookup for references in executable without indirect external access: 1. Disallow copy relocations in executable against protected data symbols in a shared object with indirect external access. 2. Disallow non-zero symbol values of undefined function symbols in executable, which are used as the function pointer, against protected function symbols in a shared object with indirect external access. --- elf/dl-lookup.c | 5 ++++ sysdeps/generic/dl-protected.h | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 sysdeps/generic/dl-protected.h diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c index eea217eb28..430359af39 100644 --- a/elf/dl-lookup.c +++ b/elf/dl-lookup.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash, if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym))) goto skip; + if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED) + _dl_check_protected_symbol (undef_name, undef_map, ref, map, + type_class); + switch (ELFW(ST_BIND) (sym->st_info)) { case STB_WEAK: diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h new file mode 100644 index 0000000000..244d020dc4 --- /dev/null +++ b/sysdeps/generic/dl-protected.h @@ -0,0 +1,54 @@ +/* Support for STV_PROTECTED visibility. Generic version. + Copyright (C) 2021 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 + . */ + +#ifndef _DL_PROTECTED_H +#define _DL_PROTECTED_H + +static inline void __attribute__ ((always_inline)) +_dl_check_protected_symbol (const char *undef_name, + const struct link_map *undef_map, + const ElfW(Sym) *ref, + const struct link_map *map, + int type_class) +{ + if (undef_map != NULL + && undef_map->l_type == lt_executable + && !(undef_map->l_1_needed + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS) + && (map->l_1_needed + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)) + { + if ((type_class & ELF_RTYPE_CLASS_COPY)) + /* Disallow copy relocations in executable against protected + data symbols in a shared object which needs indirect external + access. */ + _dl_signal_error (0, map->l_name, undef_name, + N_("copy relocation against non-copyable protected symbol")); + else if (ref->st_value != 0 + && ref->st_shndx == SHN_UNDEF + && (type_class & ELF_RTYPE_CLASS_PLT)) + /* Disallow non-zero symbol values of undefined symbols in + executable, which are used as the function pointer, against + protected function symbols in a shared object with indirect + external access. */ + _dl_signal_error (0, map->l_name, undef_name, + N_("non-canonical reference to canonical protected function")); + } +} + +#endif /* _DL_PROTECTED_H */ From patchwork Thu Jun 24 13:49:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43998 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 C6F193888C5C for ; Thu, 24 Jun 2021 13:53:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C6F193888C5C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624542829; bh=g5vEqHsIKQjnye3/zZZLVTnZoOmddfmANzG0FDwVl08=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=cWqoQ3VCcPfVvFwQ+06+8XeAbjaMIpMZwlrGqifHdZGvcub+t3OHbgCKFAPf3H4mQ AMyijs+DUkigJ61a5bgIsuMT75H/5ET32H+PImTM8GCfEVbdRu2cBZdL0M3Zwd4nB1 A1uE249cBc/QpYYL4Q0lJz4x8pr/h7HhH87RC/40= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 7662A3888C51 for ; Thu, 24 Jun 2021 13:49:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7662A3888C51 Received: by mail-pf1-x432.google.com with SMTP id t32so5232000pfg.2 for ; Thu, 24 Jun 2021 06:49:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g5vEqHsIKQjnye3/zZZLVTnZoOmddfmANzG0FDwVl08=; b=gpdb9np3LErCVpy9BmSRIE6q+fwmL/dOMThRQhvS8EITMAQXjPw6u+xqcRR6/SJBzf Xadbuj9cvz7REav9ulnhaYsLaKiz3PYCXgX4kBlEb3gZeVS/uU6Ua04woS9d+TPpvY1z UbUu546Hd2SLYf4xPZW08xogW/4+4Qy7Sdq7EnPFnoB8c8GmbZOuL2zw4w0H6K2707SX h2/k0gDCUBE3/oqIPK9c7P1Wfm+JuB95qFQ1/Obsua2VAqvJRKGdlhoDYsqVjFg+JoPU VlwpzgLW8mhfoYb0XL0ADuX4yVtUgjsyc81LUJ2W/fr/mQaY40B6LlVTX0v1cVRWBRk6 mnWA== X-Gm-Message-State: AOAM532+/V4mLOMMFkEI1oQOlTAVtACy7xLuCE2fJV6WzOxni+lIrZ2o A3pWWce732Pns7rOA9XuWtc= X-Google-Smtp-Source: ABdhPJwzNjpJInF46cwinPAqctCQ3yqUClREPv+SmNxKVAEbGWw9g3UZDPg0ZtSg45gQNAZu5pKsOA== X-Received: by 2002:a63:3d8f:: with SMTP id k137mr4830058pga.161.1624542581403; Thu, 24 Jun 2021 06:49:41 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id z15sm2775055pgu.71.2021.06.24.06.49.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 06:49:40 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 03D50C05BB; Thu, 24 Jun 2021 06:49:39 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 4/4] Update tests for protected data and function symbols Date: Thu, 24 Jun 2021 06:49:38 -0700 Message-Id: <20210624134938.2025098-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210624134938.2025098-1-hjl.tools@gmail.com> References: <20210624134938.2025098-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3032.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Protected data and function symbols don't work well without -fno-direct-extern-access: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37611 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44166 1. Compile tst-protected1[ab].c and tst-protected1mod[ab].c with -fno-direct-extern-access if possible so that GOT entries are used for undefined data accesses. 2. Add tests for protected function pointers. 3. Build tst-prelink.c with direct external access to keep COPY relocation. --- elf/Makefile | 54 ++++++++++++++++ elf/tst-protected1moda.c | 10 +-- elf/tst-protected1modb.c | 4 +- elf/tst-protected2a.c | 130 ++++++++++++++++++++++++++++++++++++++ elf/tst-protected2apie.c | 1 + elf/tst-protected2b.c | 121 +++++++++++++++++++++++++++++++++++ elf/tst-protected2bpie.c | 1 + elf/tst-protected2mod.h | 35 ++++++++++ elf/tst-protected2moda.c | 52 +++++++++++++++ elf/tst-protected2moda2.c | 41 ++++++++++++ elf/tst-protected2modb.c | 45 +++++++++++++ elf/tst-protected2modb2.c | 28 ++++++++ 12 files changed, 512 insertions(+), 10 deletions(-) create mode 100644 elf/tst-protected2a.c create mode 100644 elf/tst-protected2apie.c create mode 100644 elf/tst-protected2b.c create mode 100644 elf/tst-protected2bpie.c create mode 100644 elf/tst-protected2mod.h create mode 100644 elf/tst-protected2moda.c create mode 100644 elf/tst-protected2moda2.c create mode 100644 elf/tst-protected2modb.c create mode 100644 elf/tst-protected2modb2.c diff --git a/elf/Makefile b/elf/Makefile index 38d08e03b8..dc1a073db8 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -367,15 +367,59 @@ tests += tst-protected1a tst-protected1b $(objpfx)tst-protected1a: $(addprefix $(objpfx),tst-protected1moda.so tst-protected1modb.so) $(objpfx)tst-protected1b: $(addprefix $(objpfx),tst-protected1modb.so tst-protected1moda.so) tst-protected1modb.so-no-z-defs = yes +ifeq (yes,$(have-fno-direct-extern-access)) +CFLAGS-tst-protected1a.c += -fno-direct-extern-access +CFLAGS-tst-protected1b.c += -fno-direct-extern-access +CFLAGS-tst-protected1moda.c += -fno-direct-extern-access +CFLAGS-tst-protected1modb.c += -fno-direct-extern-access +else # These tests fail with GCC versions prior to 5.1 and with some versions # of binutils. See https://sourceware.org/bugzilla/show_bug.cgi?id=17709 # and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65248 for details. # Perhaps in future we can make these XFAILs conditional on some detection # of compiler/linker behavior/version. +# NB: These tests pass with -fno-direct-extern-access when GOT entries +# are used for undefined data accesses. test-xfail-tst-protected1a = yes test-xfail-tst-protected1b = yes endif +ifeq (yes,$(have-z-indirect-extern-access)) +LDFLAGS-tst-protected1moda.so += -Wl,-z,indirect-extern-access +LDFLAGS-tst-protected1modb.so += -Wl,-z,indirect-extern-access +endif +endif ifeq (yesyes,$(have-fpie)$(build-shared)) +ifeq (yes,$(have-z-indirect-extern-access)) +modules-names += tst-protected2moda tst-protected2modb +tests += tst-protected2a tst-protected2b +tests += tst-protected2apie tst-protected2bpie +tests-pie += tst-protected2apie tst-protected2bpie +test-extras += tst-protected2moda2 tst-protected2modb2 +extra-test-objs += tst-protected2moda2.os tst-protected2modb2.os +LDFLAGS-tst-protected2moda.so += -Wl,-z,indirect-extern-access +LDFLAGS-tst-protected2modb.so += -Wl,-z,indirect-extern-access +CFLAGS-tst-protected2apie.c += $(PIE-ccflag) +CFLAGS-tst-protected2bpie.c += $(PIE-ccflag) +ifeq (yes,$(have-fno-direct-extern-access)) +CFLAGS-tst-protected2a.c += -fno-direct-extern-access +CFLAGS-tst-protected2b.c += -fno-direct-extern-access +CFLAGS-tst-protected2moda.c += -fno-direct-extern-access +CFLAGS-tst-protected2moda2.c += -fno-direct-extern-access +CFLAGS-tst-protected2modb.c += -fno-direct-extern-access +CFLAGS-tst-protected2modb2.c += -fno-direct-extern-access +else +# These non-PIE tests fail when GOT entries are not used for undefined +# function pointers. +test-xfail-tst-protected2a = yes +test-xfail-tst-protected2b = yes +endif +$(objpfx)tst-protected2moda.so: $(objpfx)tst-protected2moda2.os +$(objpfx)tst-protected2modb.so: $(objpfx)tst-protected2modb2.os +$(objpfx)tst-protected2a: $(addprefix $(objpfx),tst-protected2moda.so tst-protected2modb.so) +$(objpfx)tst-protected2b: $(addprefix $(objpfx),tst-protected2modb.so tst-protected2moda.so) +$(objpfx)tst-protected2apie: $(addprefix $(objpfx),tst-protected2moda.so tst-protected2modb.so) +$(objpfx)tst-protected2bpie: $(addprefix $(objpfx),tst-protected2modb.so tst-protected2moda.so) +endif modules-names += tst-piemod1 tests += tst-pie1 tst-pie2 tst-dlopen-pie tst-dlopen-tlsmodid-pie \ tst-dlopen-self-pie @@ -469,6 +513,16 @@ tests += tst-prelink tests-internal += tst-prelink-cmp # Don't compile tst-prelink.c with PIE for GLOB_DAT relocation. CFLAGS-tst-prelink.c += -fno-pie +ifeq ($(have-fno-direct-extern-access),yes) +# Compile tst-prelink.c with -fdirect-extern-acces to keepp COPY +# relocation. +CFLAGS-tst-prelink.c += -fdirect-extern-access +endif +ifeq ($(have-z-indirect-extern-access),yes) +# Link tst-prelink with -z noindirect-extern-access to keepp COPY +# relocation. +LDFLAGS-tst-prelink += -Wl,-z,noindirect-extern-access +endif tst-prelink-no-pie = yes endif diff --git a/elf/tst-protected1moda.c b/elf/tst-protected1moda.c index eeb18306bb..3d0eb1e877 100644 --- a/elf/tst-protected1moda.c +++ b/elf/tst-protected1moda.c @@ -17,17 +17,13 @@ #include "tst-protected1mod.h" -int protected1 = 3; +int protected1 __attribute__ ((visibility("protected"))) = 3; static int expected_protected1 = 3; -int protected2 = 4; +int protected2 __attribute__ ((visibility("protected"))) = 4; static int expected_protected2 = 4; -int protected3 = 5; +int protected3 __attribute__ ((visibility("protected"))) = 5; static int expected_protected3 = 5; -asm (".protected protected1"); -asm (".protected protected2"); -asm (".protected protected3"); - void set_protected1a (int i) { diff --git a/elf/tst-protected1modb.c b/elf/tst-protected1modb.c index 2cb1e61b17..ca82c64689 100644 --- a/elf/tst-protected1modb.c +++ b/elf/tst-protected1modb.c @@ -19,11 +19,9 @@ #include "tst-protected1mod.h" int protected1 = -3; -int protected3 = -5; +int protected3 __attribute__ ((visibility("protected"))) = -5; static int expected_protected3 = -5; -asm (".protected protected3"); - void set_protected1b (int i) { diff --git a/elf/tst-protected2a.c b/elf/tst-protected2a.c new file mode 100644 index 0000000000..21b666e12b --- /dev/null +++ b/elf/tst-protected2a.c @@ -0,0 +1,130 @@ +/* Test the protected visibility when main is linked with moda and modb + in that order: + 1. Protected function symbols, protected1, protected2 and protected3, + defined in moda, are used in moda. + 2. Protected function symbol, protected3, defined in modb, are used + in modb. + 3. Symbol, protected1, defined in moda, is also used in main and modb. + 4. Symbol, protected2, defined in main, is used in main. + 5. Symbol, protected3, defined in moda, is also used in main. + + Copyright (C) 2021 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 "tst-protected2mod.h" + +int +protected2 (void) +{ + return -1; +} + +int +__attribute__ ((weak, noclone, noinline)) +call_ptr (protected_func_type ptr) +{ + return ptr (); +} + +int +do_test (void) +{ + int res = 0; + + /* Check if we get the same address for the protected function symbol. */ + protected_func_type ptr = protected1a_p (); + if (&protected1 != ptr) + { + puts ("`protected1' in main and moda doesn't have the same address"); + res = 1; + } + ptr = protected1b_p (); + if (&protected1 != ptr) + { + puts ("`protected1' in main and modb doesn't have the same address"); + res = 1; + } + + /* Check if we get the right protected function symbol. */ + if (call_ptr (ptr) != 3) + { + puts ("`protected1' in main and moda doesn't return the same value"); + res = 1; + } + + /* Check if we get the right function defined in executable. */ + if (protected2 () != -1) + { + puts ("`protected2' in main returns the wrong value"); + res = 1; + } + + /* Check `protected1' in moda. */ + if (!check_protected1 ()) + { + puts ("`protected1' in moda returns the wrong value"); + res = 1; + } + + /* Check `protected2' in moda. */ + if (!check_protected2 ()) + { + puts ("`protected2' in moda returns the wrong value"); + res = 1; + } + + /* Check if we get the same address for the protected function symbol. */ + if (&protected3 != protected3a_p ()) + { + puts ("`protected3' in main and moda doesn't have the same address"); + res = 1; + } + if (&protected3 == protected3b_p ()) + { + puts ("`protected3' in main and modb has the same address"); + res = 1; + } + + /* Check if we get the right value for the protected data symbol. */ + if (protected3 () != 5) + { + puts ("`protected3' in main and moda doesn't return the same value"); + res = 1; + } + + /* Check `protected3' in moda. */ + if (!check_protected3a ()) + { + puts ("`protected3' in moda has the wrong value"); + res = 1; + } + + /* Check `protected3' in modb. */ + if (!check_protected3b ()) + { + puts ("`protected3' in modb has the wrong value"); + res = 1; + } + + return res; +} + +#include diff --git a/elf/tst-protected2apie.c b/elf/tst-protected2apie.c new file mode 100644 index 0000000000..28a7aa3d1a --- /dev/null +++ b/elf/tst-protected2apie.c @@ -0,0 +1 @@ +#include "tst-protected2a.c" diff --git a/elf/tst-protected2b.c b/elf/tst-protected2b.c new file mode 100644 index 0000000000..500323e33f --- /dev/null +++ b/elf/tst-protected2b.c @@ -0,0 +1,121 @@ +/* Test the protected visibility when main is linked with modb and moda + in that order: + 1. Protected function symbols, protected1, protected2 and protected3, + defined in moda, are used in moda. + 2. Protected function symbol, protected3, defined in modb, are used + in modb. + 3. Symbol, protected1, defined in modb, is used in main and modb. + 4. Symbol, protected2, defined in main, is used in main. + 5. Symbol, protected3, defined in modb, is also used in main. + + Copyright (C) 2021 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 "tst-protected2mod.h" + +int +protected2 (void) +{ + return -1; +} + +int +do_test (void) +{ + int res = 0; + + /* Check if we get the same address for the protected data symbol. */ + if (&protected1 == protected1a_p ()) + { + puts ("`protected1' in main and moda has the same address"); + res = 1; + } + if (&protected1 != protected1b_p ()) + { + puts ("`protected1' in main and modb doesn't have the same address"); + res = 1; + } + + /* Check if we get the right protected function symbol. */ + if (protected1 () != -3) + { + puts ("`protected1' in main and modb doesn't return the same value"); + res = 1; + } + + /* Check if we get the right function defined in executable. */ + if (protected2 () != -1) + { + puts ("`protected2' in main returns the wrong value"); + res = 1; + } + + /* Check `protected1' in moda. */ + if (!check_protected1 ()) + { + puts ("`protected1' in moda returns the wrong value"); + res = 1; + } + + /* Check `protected2' in moda. */ + if (!check_protected2 ()) + { + puts ("`protected2' in moda returns the wrong value"); + res = 1; + } + + /* Check if we get the same address for the protected function symbol. */ + if (&protected3 == protected3a_p ()) + { + puts ("`protected3' in main and moda has the same address"); + res = 1; + } + if (&protected3 != protected3b_p ()) + { + puts ("`protected3' in main and modb doesn't have the same address"); + res = 1; + } + + /* Check if we get the right protected function symbol. */ + if (protected3 () != -5) + { + puts ("`protected3' in main and modb doesn't return the same value"); + res = 1; + } + + /* Check `protected3' in moda. */ + if (!check_protected3a ()) + { + puts ("`protected3' in moda returns the wrong value"); + res = 1; + } + + /* Check `protected3' in modb. */ + if (!check_protected3b ()) + { + puts ("`protected3' in modb returns the wrong value"); + res = 1; + } + + return res; +} + +#include diff --git a/elf/tst-protected2bpie.c b/elf/tst-protected2bpie.c new file mode 100644 index 0000000000..8dcfbd04cb --- /dev/null +++ b/elf/tst-protected2bpie.c @@ -0,0 +1 @@ +#include "tst-protected2b.c" diff --git a/elf/tst-protected2mod.h b/elf/tst-protected2mod.h new file mode 100644 index 0000000000..feb28ab0d5 --- /dev/null +++ b/elf/tst-protected2mod.h @@ -0,0 +1,35 @@ +/* Test protected function symbols. + Copyright (C) 2021 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 + . */ + +/* Prototypes for the functions in the DSOs. */ +extern int protected1 (void); +extern int protected2 (void); +extern int protected3 (void); + +typedef int (*protected_func_type) (void); + +extern protected_func_type protected1a_p (void); +extern protected_func_type protected1b_p (void); + +extern int check_protected1 (void); +extern int check_protected2 (void); + +extern int check_protected3a (void); +extern protected_func_type protected3a_p (void); +extern int check_protected3b (void); +extern protected_func_type protected3b_p (void); diff --git a/elf/tst-protected2moda.c b/elf/tst-protected2moda.c new file mode 100644 index 0000000000..db04e8dfb9 --- /dev/null +++ b/elf/tst-protected2moda.c @@ -0,0 +1,52 @@ +/* Test protected function symbols. + Copyright (C) 2021 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 "tst-protected2mod.h" + +__attribute__ ((visibility("protected"))) +int +protected1 (void) +{ + return 3; +} + +__attribute__ ((visibility("protected"))) +int +protected2 (void) +{ + return 4; +} + +__attribute__ ((visibility("protected"))) +int +protected3 (void) +{ + return 5; +} + +protected_func_type +protected1a_p (void) +{ + return &protected1; +} + +protected_func_type +protected3a_p (void) +{ + return &protected3; +} diff --git a/elf/tst-protected2moda2.c b/elf/tst-protected2moda2.c new file mode 100644 index 0000000000..fae72177f9 --- /dev/null +++ b/elf/tst-protected2moda2.c @@ -0,0 +1,41 @@ +/* Test protected function symbols. + Copyright (C) 2021 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 "tst-protected2mod.h" + +extern int protected1 (void) __attribute__ ((visibility("protected"))); +extern int protected2 (void) __attribute__ ((visibility("protected"))); +extern int protected3 (void) __attribute__ ((visibility("protected"))); + +int +check_protected1 (void) +{ + return protected1 () == 3; +} + +int +check_protected2 (void) +{ + return protected2 () == 4; +} + +int +check_protected3a (void) +{ + return protected3 () == 5; +} diff --git a/elf/tst-protected2modb.c b/elf/tst-protected2modb.c new file mode 100644 index 0000000000..3c5063f0c3 --- /dev/null +++ b/elf/tst-protected2modb.c @@ -0,0 +1,45 @@ +/* Test protected function symbols. + Copyright (C) 2021 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 "tst-protected2mod.h" + +int +protected1 (void) +{ + return -3; +} + +__attribute__ ((visibility("protected"))) +int +protected3 (void) +{ + return -5; +} + +protected_func_type +protected1b_p (void) +{ + return &protected1; +} + +protected_func_type +protected3b_p (void) +{ + return &protected3; +} diff --git a/elf/tst-protected2modb2.c b/elf/tst-protected2modb2.c new file mode 100644 index 0000000000..b21b827134 --- /dev/null +++ b/elf/tst-protected2modb2.c @@ -0,0 +1,28 @@ +/* Test protected function symbols. + Copyright (C) 2021 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 "tst-protected2mod.h" + +extern int protected3 (void) __attribute__ ((visibility("protected"))); + +int +check_protected3b (void) +{ + return protected3 () == -5; +}