From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 435B0385500E for ; Thu, 17 Jun 2021 17:16:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 435B0385500E Received: by mail-pj1-x1033.google.com with SMTP id bb10-20020a17090b008ab029016eef083425so6109337pjb.5 for ; Thu, 17 Jun 2021 10:16:21 -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:subject:date:message-id:mime-version :content-transfer-encoding; bh=YG6hIKgIVrR17MVlybzSfFknqddSScnDhQDvzwpy540=; b=ZINnEH21JH9XG6rMJtUGSYsnehEbaIG4p3EGyUXqUCEzpXLTt6kCe1/tOlhfBOaei8 wnU3EMTMQkiNESdIuf+YOC/8oZzFp4w5yaa6f7tVZrT8cfeIaQ8/9/aiMmRyuJ7IUmUI c/Il4P0OdQq/tu8SEgWaFNSmN//HbMy9u1QKvjtcEoRThISOvdeTAGTUQG27+BjMBfmM d/70jvvKvP0oNXgwgcAC9W19qGPefjd5APZ5mX3DTN12tldJM7d5dX75fPYf6wPXWtJC PC6PbMV/LlGf4bG9nBGv7cNZzfgRtwaMvGKyZx7NLPacjLFnasPAl82VUmY+ZhZbx4T4 oqQg== X-Gm-Message-State: AOAM532I3nweT8C4lCbjVZR7QNVnwiFiXPlZiFfCRugd8LUe9ZzXJ8+O m4KGDKDRf/uMU/0OVkFHFy+GgsS58sQ= X-Google-Smtp-Source: ABdhPJwYJ4tWcUd8TtLK3YNs4HZtkRjCREZwHvTVUIfGfR7bgWT2uI27WCOJ1xTPg/y0Rx5SKEjBWg== X-Received: by 2002:a17:90b:17c2:: with SMTP id me2mr6624545pjb.176.1623950180207; Thu, 17 Jun 2021 10:16:20 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id y39sm5492842pfa.119.2021.06.17.10.16.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 10:16:19 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 5EDF9C02D6 for ; Thu, 17 Jun 2021 10:16:18 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] x86-64: Test protected function pointers Date: Thu, 17 Jun 2021 10:16:18 -0700 Message-Id: <20210617171618.491471-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3032.9 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Jun 2021 17:16:22 -0000 On x86-64, function pointers in executable for external funtions may be resolved to their PLT entries in executable. If it happens, function pointers of protected funtions in shared libraries must be resolved to the PLT entries in executable, not addresses of protected funtions in shared libraries. * testsuite/ld-x86-64/x86-64.exp: Run protected function tests. * testsuite/ld-x86-64/protected-func-1.h: New file. * testsuite/ld-x86-64/protected-func-1a.s: Likewise. * testsuite/ld-x86-64/protected-func-1b.c: Likewise. --- ld/testsuite/ld-x86-64/protected-func-1.h | 6 +++ ld/testsuite/ld-x86-64/protected-func-1a.s | 48 ++++++++++++++++++++++ ld/testsuite/ld-x86-64/protected-func-1b.c | 35 ++++++++++++++++ ld/testsuite/ld-x86-64/x86-64.exp | 25 +++++++++++ 4 files changed, 114 insertions(+) create mode 100644 ld/testsuite/ld-x86-64/protected-func-1.h create mode 100644 ld/testsuite/ld-x86-64/protected-func-1a.s create mode 100644 ld/testsuite/ld-x86-64/protected-func-1b.c diff --git a/ld/testsuite/ld-x86-64/protected-func-1.h b/ld/testsuite/ld-x86-64/protected-func-1.h new file mode 100644 index 00000000000..b5ff4e8f822 --- /dev/null +++ b/ld/testsuite/ld-x86-64/protected-func-1.h @@ -0,0 +1,6 @@ +extern int protected_func_1a (void); +extern int protected_func_1b (void); + +typedef int (*protected_func_type) (void); +extern protected_func_type protected_func_1a_p (void); +extern protected_func_type protected_func_1b_p (void); diff --git a/ld/testsuite/ld-x86-64/protected-func-1a.s b/ld/testsuite/ld-x86-64/protected-func-1a.s new file mode 100644 index 00000000000..eb2ed91fb52 --- /dev/null +++ b/ld/testsuite/ld-x86-64/protected-func-1a.s @@ -0,0 +1,48 @@ + .text + .p2align 4 + .protected protected_func_1a + .globl protected_func_1a + .type protected_func_1a, @function +protected_func_1a: +.LFB0: + .cfi_startproc + movl $1, %eax + ret + .cfi_endproc +.LFE0: + .size protected_func_1a, .-protected_func_1a + .p2align 4 + .protected protected_func_1b + .globl protected_func_1b + .type protected_func_1b, @function +protected_func_1b: +.LFB1: + .cfi_startproc + movl $2, %eax + ret + .cfi_endproc +.LFE1: + .size protected_func_1b, .-protected_func_1b + .p2align 4 + .globl protected_func_1a_p + .type protected_func_1a_p, @function +protected_func_1a_p: +.LFB2: + .cfi_startproc + movq protected_func_1a@GOTPCREL(%rip), %rax + ret + .cfi_endproc +.LFE2: + .size protected_func_1a_p, .-protected_func_1a_p + .p2align 4 + .globl protected_func_1b_p + .type protected_func_1b_p, @function +protected_func_1b_p: +.LFB3: + .cfi_startproc + movq protected_func_1b@GOTPCREL(%rip), %rax + ret + .cfi_endproc +.LFE3: + .size protected_func_1b_p, .-protected_func_1b_p + .section .note.GNU-stack,"",@progbits diff --git a/ld/testsuite/ld-x86-64/protected-func-1b.c b/ld/testsuite/ld-x86-64/protected-func-1b.c new file mode 100644 index 00000000000..a9d728afe24 --- /dev/null +++ b/ld/testsuite/ld-x86-64/protected-func-1b.c @@ -0,0 +1,35 @@ +#include + +#include "protected-func-1.h" + +int +protected_func_1b (void) +{ + return 3; +} + +int +main (void) +{ + int res = 0; + + /* Check if we get the same address for the protected function symbol. */ + if (protected_func_1a != protected_func_1a_p ()) + { + puts ("'protected_func_1a' in main and shared library doesn't have same address"); + res = 1; + } + + /* Check if we get the different addresses for the protected function + symbol. */ + if (protected_func_1b == protected_func_1b_p ()) + { + puts ("'protected_func_1b' in main and shared library has same address"); + res = 1; + } + + if (!res) + puts ("PASS"); + + return res; +} diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index 80716668df6..5dc9a1c218e 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -1321,6 +1321,14 @@ if { [isnative] && [check_compiler_available] } { {readelf -rW pr22842b.rd}} \ "pr22842" \ ] \ + [list \ + "Build libprotected-func-1.so" \ + "-shared" \ + "-fPIC -Wa,-mx86-used-note=yes" \ + { protected-func-1a.s } \ + {} \ + "libprotected-func-1.so" \ + ] \ ] if {[istarget "x86_64-*-linux*-gnux32"]} { @@ -1730,6 +1738,23 @@ if { [isnative] && [check_compiler_available] } { "pr23997" \ "pass.out" \ ] \ + [list \ + "Run protected-func-1 without PIE" \ + "-Wl,--no-as-needed tmpdir/libprotected-func-1.so" \ + "-Wa,-mx86-used-note=yes" \ + { protected-func-1b.c } \ + "protected-func-1a" \ + "pass.out" \ + ] \ + [list \ + "Run protected-func-1 with PIE" \ + "-Wl,--no-as-needed -pie tmpdir/libprotected-func-1.so" \ + "-Wa,-mx86-used-note=yes" \ + { protected-func-1b.c } \ + "protected-func-1b" \ + "pass.out" \ + "-fPIE" \ + ] \ ] # Run-time tests which require working ifunc attribute support. -- 2.31.1