From patchwork Wed Jul 15 19:26:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 51131 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id AD6D322A24 for ; Wed, 15 Jul 2015 19:27:01 +0000 (UTC) Received: by wgfk9 with SMTP id k9sf13344495wgf.1 for ; Wed, 15 Jul 2015 12:27:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:x-original-sender :x-original-authentication-results:mailing-list; bh=h5+dXxUcM28iTlDnUALQRMCdX1G7H8tbiwWFQED86+0=; b=EeNP6n0WP8UQAKB8089f6eobEMlrvCY5D/Y0BOvG5H8wY28vaLINRwF83Y81IdBazI Deq+7YeTLfbxw0KmyGT/sxLQUUruEPmAdeFUs+5nDfI16iPfsju9jfmZOlYMfDjIf36V DutMb/w5t2/2cHpOyDpTfh9ACxdGTEyL2A+78OV+7SXaNQPXhlpuMvpF+4jIap7/ylW2 zXB2ZzVNe1VI6TFyE1MCrKSg1ei9oaPQTnDexUEyP8mrwo+VpV0lV5eGj4yLcsDC3wuD kEBpKJvPMEdNYKNV1PokP3xW0T/36OcM1oaQ1s278qK88LvYl37JaJptVPy9tA0aiJim 55Ww== X-Gm-Message-State: ALoCoQnIQvFKpCRDEvFbRu5cY8tivK/O6NDTHxf8j66ZCMCFcTaitGyVawRZZL4p/29DSgqFHKRz X-Received: by 10.180.182.102 with SMTP id ed6mr143778wic.1.1436988420957; Wed, 15 Jul 2015 12:27:00 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.37.105 with SMTP id x9ls270231laj.11.gmail; Wed, 15 Jul 2015 12:27:00 -0700 (PDT) X-Received: by 10.112.146.36 with SMTP id sz4mr5879410lbb.54.1436988420594; Wed, 15 Jul 2015 12:27:00 -0700 (PDT) Received: from mail-lb0-f175.google.com (mail-lb0-f175.google.com. [209.85.217.175]) by mx.google.com with ESMTPS id ju5si4841053lbc.39.2015.07.15.12.27.00 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Jul 2015 12:27:00 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.175 as permitted sender) client-ip=209.85.217.175; Received: by lbbzr7 with SMTP id zr7so31056460lbb.1 for ; Wed, 15 Jul 2015 12:27:00 -0700 (PDT) X-Received: by 10.152.37.228 with SMTP id b4mr5669925lak.117.1436988420404; Wed, 15 Jul 2015 12:27:00 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp3294018lbb; Wed, 15 Jul 2015 12:26:59 -0700 (PDT) X-Received: by 10.107.5.1 with SMTP id 1mr6983121iof.88.1436988418684; Wed, 15 Jul 2015 12:26:58 -0700 (PDT) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id a17si5030102igm.8.2015.07.15.12.26.57 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 15 Jul 2015 12:26:58 -0700 (PDT) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ZFSKM-0001Wl-UT; Wed, 15 Jul 2015 19:26:46 +0000 Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ZFSKL-0001We-Ku for edk2-devel@lists.sourceforge.net; Wed, 15 Jul 2015 19:26:45 +0000 Received-SPF: pass (sog-mx-4.v43.ch3.sourceforge.com: domain of linaro.org designates 209.85.212.169 as permitted sender) client-ip=209.85.212.169; envelope-from=ard.biesheuvel@linaro.org; helo=mail-wi0-f169.google.com; Received: from mail-wi0-f169.google.com ([209.85.212.169]) by sog-mx-4.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76) id 1ZFSKK-0003Rh-Bc for edk2-devel@lists.sourceforge.net; Wed, 15 Jul 2015 19:26:45 +0000 Received: by wibud3 with SMTP id ud3so11111640wib.1 for ; Wed, 15 Jul 2015 12:26:38 -0700 (PDT) X-Received: by 10.194.59.212 with SMTP id b20mr10789832wjr.31.1436988398327; Wed, 15 Jul 2015 12:26:38 -0700 (PDT) Received: from localhost.localdomain ([185.13.106.86]) by smtp.gmail.com with ESMTPSA id i6sm9311684wjf.29.2015.07.15.12.26.35 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 15 Jul 2015 12:26:37 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.sourceforge.net, yingke.d.liu@intel.com Date: Wed, 15 Jul 2015 21:26:31 +0200 Message-Id: <1436988391-16305-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.9.1 X-Spam-Score: -1.5 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record -0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1ZFSKK-0003Rh-Bc Subject: [edk2] [PATCH] BaseTools/GenFw: move .debug contents to .data to save space X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.175 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 In order to reduce the memory footprint of PE/COFF images when using large values for the PE/COFF section alignment, move the contents of the .debug section to .data, and point the debug data directory entry to it. This allows us to drop the .debug section entirely, as well as any associated rounding. Since our .debug section only contains the filename of the ELF input image, the penalty of keeping this data in a non-discardable section is negligible. Note that the PE/COFF spec v6.3 explicitly mentions that this is allowed. This also makes it possible to actually drop the .reloc section from a XIP binary, since .reloc is now the final section in the image. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- BaseTools/Source/C/GenFw/Elf32Convert.c | 55 ++++++++++++++++----------------- BaseTools/Source/C/GenFw/Elf64Convert.c | 54 ++++++++++++++++---------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/BaseTools/Source/C/GenFw/Elf32Convert.c b/BaseTools/Source/C/GenFw/Elf32Convert.c index 10d9892ba1e0..ba68833f2fb8 100644 --- a/BaseTools/Source/C/GenFw/Elf32Convert.c +++ b/BaseTools/Source/C/GenFw/Elf32Convert.c @@ -101,7 +101,7 @@ STATIC UINT32 mCoffAlignment = 0x20; // // PE section alignment. // -STATIC const UINT16 mCoffNbrSections = 5; +STATIC const UINT16 mCoffNbrSections = 4; // // ELF sections to offset in Coff file. @@ -116,6 +116,7 @@ STATIC UINT32 mTextOffset; STATIC UINT32 mDataOffset; STATIC UINT32 mHiiRsrcOffset; STATIC UINT32 mRelocOffset; +STATIC UINT32 mDebugOffset; // // Initialization Function @@ -354,6 +355,8 @@ ScanSections32 ( assert (FALSE); } + mDebugOffset = mCoffOffset; + if (mEhdr->e_machine != EM_ARM) { mCoffOffset = CoffAlign(mCoffOffset); } @@ -405,6 +408,24 @@ ScanSections32 ( } // + // Make room for .debug data in .data (or .text if .data is empty) instead of + // putting it in a section of its own. This is explicitly allowed by the + // PE/COFF spec, and prevents bloat in the binary when using large values for + // section alignment. + // + if (SectionCount > 0) { + mDebugOffset = mCoffOffset; + } + mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) + + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + + strlen(mInImageName) + 1; + + mCoffOffset = CoffAlign(mCoffOffset); + if (SectionCount == 0) { + mDataOffset = mCoffOffset; + } + + // // The HII resource sections. // mHiiRsrcOffset = mCoffOffset; @@ -998,28 +1019,18 @@ WriteDebug32 ( ) { UINT32 Len; - UINT32 DebugOffset; EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr; EFI_IMAGE_DATA_DIRECTORY *DataDir; EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir; EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10; Len = strlen(mInImageName) + 1; - DebugOffset = mCoffOffset; - - mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) - + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) - + Len; - mCoffOffset = CoffAlign(mCoffOffset); - - mCoffFile = realloc(mCoffFile, mCoffOffset); - memset(mCoffFile + DebugOffset, 0, mCoffOffset - DebugOffset); - Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + DebugOffset); + Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset); Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW; Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len; - Dir->RVA = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); - Dir->FileOffset = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1); Nb10->Signature = CODEVIEW_SIGNATURE_NB10; @@ -1028,20 +1039,8 @@ WriteDebug32 ( NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset); DataDir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]; - DataDir->VirtualAddress = DebugOffset; - DataDir->Size = mCoffOffset - DebugOffset; - if (DataDir->Size == 0) { - // If no debug, null out the directory entry and don't add the .debug section - DataDir->VirtualAddress = 0; - NtHdr->Pe32.FileHeader.NumberOfSections--; - } else { - DataDir->VirtualAddress = DebugOffset; - CreateSectionHeader (".debug", DebugOffset, mCoffOffset - DebugOffset, - EFI_IMAGE_SCN_CNT_INITIALIZED_DATA - | EFI_IMAGE_SCN_MEM_DISCARDABLE - | EFI_IMAGE_SCN_MEM_READ); - - } + DataDir->VirtualAddress = mDebugOffset; + DataDir->Size = Dir->SizeOfData + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); } STATIC diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c index d2becf165f55..9265e6f9d03c 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -102,7 +102,7 @@ STATIC UINT32 mCoffAlignment = 0x20; // // PE section alignment. // -STATIC const UINT16 mCoffNbrSections = 5; +STATIC const UINT16 mCoffNbrSections = 4; // // ELF sections to offset in Coff file. @@ -117,6 +117,7 @@ STATIC UINT32 mTextOffset; STATIC UINT32 mDataOffset; STATIC UINT32 mHiiRsrcOffset; STATIC UINT32 mRelocOffset; +STATIC UINT32 mDebugOffset; // // Initialization Function @@ -348,6 +349,8 @@ ScanSections64 ( assert (FALSE); } + mDebugOffset = mCoffOffset; + if (mEhdr->e_machine != EM_ARM) { mCoffOffset = CoffAlign(mCoffOffset); } @@ -391,7 +394,24 @@ ScanSections64 ( SectionCount ++; } } + + // + // Make room for .debug data in .data (or .text if .data is empty) instead of + // putting it in a section of its own. This is explicitly allowed by the + // PE/COFF spec, and prevents bloat in the binary when using large values for + // section alignment. + // + if (SectionCount > 0) { + mDebugOffset = mCoffOffset; + } + mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) + + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + + strlen(mInImageName) + 1; + mCoffOffset = CoffAlign(mCoffOffset); + if (SectionCount == 0) { + mDataOffset = mCoffOffset; + } if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) { Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName); @@ -903,28 +923,18 @@ WriteDebug64 ( ) { UINT32 Len; - UINT32 DebugOffset; EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr; EFI_IMAGE_DATA_DIRECTORY *DataDir; EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir; EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10; Len = strlen(mInImageName) + 1; - DebugOffset = mCoffOffset; - - mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) - + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) - + Len; - mCoffOffset = CoffAlign(mCoffOffset); - mCoffFile = realloc(mCoffFile, mCoffOffset); - memset(mCoffFile + DebugOffset, 0, mCoffOffset - DebugOffset); - - Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + DebugOffset); + Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset); Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW; Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len; - Dir->RVA = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); - Dir->FileOffset = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1); Nb10->Signature = CODEVIEW_SIGNATURE_NB10; @@ -933,20 +943,8 @@ WriteDebug64 ( NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset); DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]; - DataDir->VirtualAddress = DebugOffset; - DataDir->Size = mCoffOffset - DebugOffset; - if (DataDir->Size == 0) { - // If no debug, null out the directory entry and don't add the .debug section - DataDir->VirtualAddress = 0; - NtHdr->Pe32Plus.FileHeader.NumberOfSections--; - } else { - DataDir->VirtualAddress = DebugOffset; - CreateSectionHeader (".debug", DebugOffset, mCoffOffset - DebugOffset, - EFI_IMAGE_SCN_CNT_INITIALIZED_DATA - | EFI_IMAGE_SCN_MEM_DISCARDABLE - | EFI_IMAGE_SCN_MEM_READ); - - } + DataDir->VirtualAddress = mDebugOffset; + DataDir->Size = Dir->SizeOfData + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); } STATIC