From patchwork Sun Jul 17 10:34:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 72173 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp175595qga; Sun, 17 Jul 2016 03:35:12 -0700 (PDT) X-Received: by 10.66.161.132 with SMTP id xs4mr46868183pab.140.1468751711080; Sun, 17 Jul 2016 03:35:11 -0700 (PDT) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id zy8si15829062pab.68.2016.07.17.03.35.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Jul 2016 03:35:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A5B6F1A1E45; Sun, 17 Jul 2016 03:35:52 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x231.google.com (mail-wm0-x231.google.com [IPv6:2a00:1450:400c:c09::231]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A831C1A1E45 for ; Sun, 17 Jul 2016 03:35:51 -0700 (PDT) Received: by mail-wm0-x231.google.com with SMTP id o80so81854494wme.1 for ; Sun, 17 Jul 2016 03:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nq/wNLMQVDfLQm2o/7PEQ45zVyL5osRUy/4gfN77zlQ=; b=HPxYfAOBt2gdcjzPr3PvWJnBKt0EmeKsFDpTnPo5uOTsXYzQUvvN6halMkjN+69OT6 LgvNFz6F9lEtNcP8iFQ3egIKf+37UDZ3RNtIJ0HqsbtWLPWXO9zE4+/PlN1C8ZS+47rL 2MF4v4RKKoDHrmmAHOnbZ8Ym2ZK/WDTcXSM3M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nq/wNLMQVDfLQm2o/7PEQ45zVyL5osRUy/4gfN77zlQ=; b=I3kA4oAe39VkMXRmcLSOF88rsQmO6TcDums4rlz9Da4bKPCqbkV9vGwWGV970E53xL /hX1cktJYy/GEz9t729hcfrTsBgVBFaOpB9j11DKOAbjNrZUfZTithlmVIVekIa7TjIR aAg4WjsJ6TE6KphSAR4g8PpicaTe5pocYM4dAGLPUL8EoQ3v3hBIjo4yyY1tvNLFJ5vc yF8OEofQkq+YkGTQFQoJD6gKBLwv8T40D0pNJEFE8uRfC97N4gbnfoF9pkHCIdcvOGUh ZvkXOO9Jj0f/DP3653hU8obtDmYJrVSRgCTCGQmG/qlqLjBpdtmKV4mjya3n474S4y2K msJg== X-Gm-Message-State: ALyK8tKWRBmhY8EZPXGDbOFbrTCW+v/0bnE2LIheUQ4sgvOT/UUiwnbG9Jxmttr2PeNJiwxO X-Received: by 10.28.39.69 with SMTP id n66mr32173905wmn.3.1468751701914; Sun, 17 Jul 2016 03:35:01 -0700 (PDT) Received: from localhost.localdomain (ip16-2-212-87.adsl2.static.versatel.nl. [87.212.2.16]) by smtp.gmail.com with ESMTPSA id d64sm7075922wmc.22.2016.07.17.03.35.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 17 Jul 2016 03:35:01 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org, lersek@redhat.com, afish@apple.com, liming.gao@intel.com, steven.shi@intel.com, yonghong.zhu@intel.com, michael.d.kinney@intel.com, jordan.l.justen@intel.com Date: Sun, 17 Jul 2016 12:34:45 +0200 Message-Id: <1468751686-28047-9-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1468751686-28047-1-git-send-email-ard.biesheuvel@linaro.org> References: <1468751686-28047-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v3 8/9] MdePkg X64: force 'protected' visibility when building with -fpic X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: bruce@cran.org.uk, pbonzini@redhat.com, Ard Biesheuvel , ting.ye@intel.com, qin.long@intel.com MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" When building position independent (PIC) ELF objects, the GCC compiler assumes that each symbol with external linkage may potentially end up being exported from a shared library, which means that each of those symbols may be subject to symbol preemption, i.e., the executable linking to the shared library at runtime may override symbols exported by the shared library, and every internal reference held by the shared library itself *must* be made to point to the overridden version instead. For this reason, PIC code symbol references always go via the Global Offset Table (GOT), even if the code in question references symbols that are defined in the same compilation unit. The GOT refers to each symbol by absolute address, and so each entry is subject to runtime relocation. Since not every symbol with external linkage is ultimately exported from a shared library, the GCC compiler allows control over symbol visibility using attributes, command line arguments and pragmas, where 'protected' means that the symbol is only referenced by the shared library itself. Due to the poor hygiene in EDK2 regarding the use of the 'static' modifier, many symbols that are local to their compilation unit end up being referenced indirectly via the GOT when building PIC code. In UEFI, there are no shared libraries and so there is no need to deal with symbol preemption, and we can mark every symbol reference protected. The only method that applies to all symbol definitions as well as declarations is the #pragma. So set the visibility 'protected' pragma when building PIC code for X64 using GCC. Note that this affects code generated with the -fpie compiler switch as well as the -fpic compiler switch. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- MdePkg/Include/X64/ProcessorBind.h | 9 +++++++++ 1 file changed, 9 insertions(+) -- 1.9.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdePkg/Include/X64/ProcessorBind.h b/MdePkg/Include/X64/ProcessorBind.h index 705104af062a..a4aad3e524e8 100644 --- a/MdePkg/Include/X64/ProcessorBind.h +++ b/MdePkg/Include/X64/ProcessorBind.h @@ -27,6 +27,15 @@ #pragma pack() #endif +#if defined(__GNUC__) && defined(__pic__) +// +// Mark all symbol declarations and references as protected, meaning they will +// not be subject to symbol preemption. This allows the compiler to refer to +// symbols directly using relative references rather than via the GOT, which +// contains absolute symbol addresses that are subject to runtime relocation. +// +#pragma GCC visibility push (protected) +#endif #if defined(__INTEL_COMPILER) //