From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 38DC021E1455D for ; Tue, 15 Aug 2017 08:43:20 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9765468C1; Tue, 15 Aug 2017 15:45:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9765468C1 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=lersek@redhat.com Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-68.phx2.redhat.com [10.3.116.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE08C18ABA; Tue, 15 Aug 2017 15:45:35 +0000 (UTC) To: "Shi, Steven" Cc: edk2-devel-01 , Alex Williamson , Ard Biesheuvel , "Justen, Jordan L" , "Gao, Liming" , "Kinney, Michael D" References: <20170811003426.2332-1-lersek@redhat.com> <20170811003426.2332-2-lersek@redhat.com> <06C8AB66E78EE34A949939824ABE2B313B560EB2@shsmsx102.ccr.corp.intel.com> <787f4528-980e-8c71-2804-0e8be2c935aa@redhat.com> <06C8AB66E78EE34A949939824ABE2B313B56176B@shsmsx102.ccr.corp.intel.com> From: Laszlo Ersek Message-ID: <092446e6-0900-7eb3-d071-b88abcdadfa9@redhat.com> Date: Tue, 15 Aug 2017 17:45:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <06C8AB66E78EE34A949939824ABE2B313B56176B@shsmsx102.ccr.corp.intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 15 Aug 2017 15:45:44 +0000 (UTC) Subject: Re: [PATCH 1/1] BaseTools/tools_def.template: revert to large code model for X64/GCC5/LTO X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Aug 2017 15:43:20 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 08/12/17 05:05, Shi, Steven wrote: > OK. I can reproduce the failure with -smp 4 and -m 5120 in my side. > > It looks a linker bug about assemble function support in PIC/PIE > code. You know, if we only have C code, the compiler/linker will > generate all the machine code and guarantee all the address reference > are position independent under PIC/PIE build. But if we mix manually > written assemble code in the C code, the linker cannot really control > the address reference in the assemble code, and might got confused. This is an incorrect description of the situation. The address reference is *not* in assembly code. (It used to be in assembly code, but Mike changed that earlier, for XCODE5 compatibility.) At this moment the address reference is in *C code*. The C code takes the address of an external function, and assigns it to a field in a data structure. The assembly code calls the function through this field. The assembly code makes no reference to the called function by name. See Mike's commit 3b2928b46987: - mov rax, ASM_PFX(InitializeFloatingPointUnits) + mov rax, qword [esi + InitializeFloatingPointUnitsAddress] sub rsp, 20h call rax ; Call assembly function to initialize FPU per UEFI spec And, indeed, it is *not* the assembly code that's being miscompiled. It is the C-language assignment below that is miscompiled: + ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits; > So, it is not seldom we could see the compiler/linker generate wrong > code for mixed code, especially with very high level optimization, > e.g. LTO. > > Globally change memory model from small to large will bring not > trivial impact (+15%) to code size, espcial for the uncomperssed > option rom dirver. Below is some data of OvmfPkgX64.dsc platform. > > Dxecore.efi CpuDxe.efi CpuMpPei PeiCore.efi > Small+PIE: 139520 47360 30144 46720 > Large: 165696 55360 34496 53504 My argument is that the current "-mcmodel=small" option actually *lies* to the compiler about our binaries. According to the GCC documentation, "-mcmodel=small" implies that a binary built like this will never be executed from above 2GB in the address space. This is why gcc-7 believes it is allowed to generate a MOV instruction that sign-extends a 32-bit address to 64-bit -- because we promise GCC that the sign bit will always be clear to begin with. IOW, we make a promise, gcc-7 generates code accordingly, and then we break the promise, by executing the binary (the assignment in the C code of MpInitLib) from above 2GB. In particular, an X64 UEFI_DRIVER module, shipped as an option ROM on a physical PCI(E) card, could be loaded anywhere at all in the 64-bit address space (given sufficient memory in the computer). Building such a driver with "-mcmodel=small" is wrong therefore; we cannot guarantee that the driver will be executed from under 2GB. Perhaps we should use "-mcmodel=large", but *keep* "-fpie". ... I've now tried that, but it doesn't work. With "-mcmodel=large -fpie", the compiler emits R_X86_64_GOTOFF64 relocations (type 25 decimal), and I get errors like: GenFw: ERROR 3000: Invalid Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/ReportStatusCodeRouter/Pei/ReportStatusCodeRouterPei/DEBUG/ReportStatusCodeRouterPei.dll unsupported ELF EM_X86_64 relocation 0x19. I believe Ard's commit 28ade7b802e0 ("MdePkg: move to 'hidden' visibility for all symbols under GCC/X64", 2016-08-01) was meant to prevent this, but apparently it's not enough with gcc-7.1. > A simpler workaround could be to add a C function wrapper around the > assemble lib function as below. This simple workaround works in my > side. But it is necessary to find this issue's root cause and fix it > in the compiler/linker. I will try to raise this issue to > compiler/linker guys. > > diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c > old mode 100644 > new mode 100755 > index a3eea29..7afe434 > --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c > +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c > @@ -738,6 +738,15 @@ WaitApWakeup ( > } > } > > +VOID > +EFIAPI > +InitializeFloatingPointUnitsWrapper ( > + VOID > + ) > +{ > + InitializeFloatingPointUnits(); > +} > + > /** > This function will fill the exchange info structure. > > @@ -771,7 +780,7 @@ FillExchangeInfoData ( > > ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled (); > > - ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits; > + ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnitsWrapper; > > // > // Get the BSP's data of GDT and IDT I'm not convinced that this is the right fix, until we know exactly why and how it changes the behavior of gcc-7. So I've now filed to track this problem. (I also captured your suggestion in the BZ.) We do know the exact symptoms and consequences of the miscompilation, and I want to suppress those symptoms at least, as soon as possible. I will post a patch for OvmfPkg's "build.sh" to use the GCC49 toolchain settings with gcc-7.* (no LTO). I'll also ask Gerd to update the toolchain selection in his SPEC file accordingly. Thanks Laszlo