From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.105359.1680622840325722980 for ; Tue, 04 Apr 2023 08:40:40 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fNhs+bWE; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D8B42634F1; Tue, 4 Apr 2023 15:40:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93C96C433EF; Tue, 4 Apr 2023 15:40:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680622839; bh=HoOextL0fmcK9+3mwcIGon47+pJtGkE9op6m4qwGJa0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fNhs+bWETvF4BvSWtAAhr8zKzZ3YS5UvAEh0/X2JnrKwAzH8MLpYZL5FcgDUWkEPF J725MBW1kuLXxyKX1lL1SZZKRcxDwt8MsOVQnqTc2DEYK81q+TYiD0hN4FaMWdEjc0 vumVNlKuHYkdg9ekMxRKxTa/xtxPSn3G5oTgUXSRahDm5LtAoKyY4SzTl8JCXL5TM5 IkjZYvikcjTBQTJGxiGPgQP4IUwCB1B0yGvY98HH2YMtF0numsrMhR6jSeHZm6LCHH SSipUTL4zs28nz7LznMJveKG7I0ijZmIPmSG4mcXsh+B3tQ7vh5/5B9iY0bhR3kkO6 2GGa7rXgs/Fag== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , =?UTF-8?q?Marvin=20H=C3=A4user?= , Bob Feng , Oliver Smith-Denny Subject: [PATCH v3 2/4] BaseTools/GenFw: Add DllCharacteristicsEx field to debug data Date: Tue, 4 Apr 2023 17:40:20 +0200 Message-Id: <20230404154022.2776035-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404154022.2776035-1-ardb@kernel.org> References: <20230404154022.2776035-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The PE/COFF spec describes an additional DllCharacteristics field implemented as a debug directory entry, which carries flags related to which control flow integrity (CFI) features are supported by the binary. So let's add this entry when doing ELF to PE/COFF conversion - we will add support for setting the flags in a subsequent patch. Signed-off-by: Ard Biesheuvel Reviewed-by: Leif Lindholm Reviewed-by: Oliver Smith-Denny --- BaseTools/Source/C/GenFw/Elf64Convert.c | 54 +++++++++++++++= ----- BaseTools/Source/C/GenFw/GenFw.c | 3 +- BaseTools/Source/C/Include/IndustryStandard/PeImage.h | 13 ++++- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/G= enFw/Elf64Convert.c index 2a810e835d4a4a66..9c17c90b16602421 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -992,6 +992,16 @@ ScanSections64 ( sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +=0D strlen(mInImageName) + 1;=0D =0D + //=0D + // Add more space in the .debug data region for the DllCharacteristicsEx= =0D + // field.=0D + //=0D + if (mDllCharacteristicsEx !=3D 0) {=0D + mCoffOffset =3D DebugRvaAlign(mCoffOffset) +=0D + sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +=0D + sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY);=0D + }=0D +=0D mCoffOffset =3D CoffAlign(mCoffOffset);=0D if (SectionCount =3D=3D 0) {=0D mDataOffset =3D mCoffOffset;=0D @@ -2244,29 +2254,47 @@ WriteDebug64 ( VOID=0D )=0D {=0D - UINT32 Len;=0D - EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;=0D - EFI_IMAGE_DATA_DIRECTORY *DataDir;=0D - EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;=0D - EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;=0D + UINT32 Len;=0D + EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;=0D + EFI_IMAGE_DATA_DIRECTORY *DataDir;=0D + EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;=0D + EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;=0D + EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY *DllEntry;=0D =0D Len =3D strlen(mInImageName) + 1;=0D =0D + NtHdr =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);= =0D + DataDir =3D &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRE= CTORY_ENTRY_DEBUG];=0D + DataDir->VirtualAddress =3D mDebugOffset;=0D + DataDir->Size =3D sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);=0D +=0D Dir =3D (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);=0D +=0D + if (mDllCharacteristicsEx !=3D 0) {=0D + DataDir->Size +=3D sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);=0D +=0D + Dir->Type =3D EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS;=0D + Dir->SizeOfData =3D sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTR= Y);=0D + Dir->FileOffset =3D mDebugOffset + DataDir->Size +=0D + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +=0D + DebugRvaAlign(Len);=0D + Dir->RVA =3D Dir->FileOffset;=0D +=0D + DllEntry =3D (VOID *)(mCoffFile + Dir->FileOffset);=0D +=0D + DllEntry->DllCharacteristicsEx =3D mDllCharacteristicsEx;=0D +=0D + Dir++;=0D + }=0D +=0D Dir->Type =3D EFI_IMAGE_DEBUG_TYPE_CODEVIEW;=0D Dir->SizeOfData =3D sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;=0D - Dir->RVA =3D mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);=0D - Dir->FileOffset =3D mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTR= Y);=0D + Dir->RVA =3D mDebugOffset + DataDir->Size;=0D + Dir->FileOffset =3D mDebugOffset + DataDir->Size;=0D =0D Nb10 =3D (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);=0D Nb10->Signature =3D CODEVIEW_SIGNATURE_NB10;=0D strcpy ((char *)(Nb10 + 1), mInImageName);=0D -=0D -=0D - NtHdr =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);= =0D - DataDir =3D &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRE= CTORY_ENTRY_DEBUG];=0D - DataDir->VirtualAddress =3D mDebugOffset;=0D - DataDir->Size =3D sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);=0D }=0D =0D STATIC=0D diff --git a/BaseTools/Source/C/GenFw/GenFw.c b/BaseTools/Source/C/GenFw/Ge= nFw.c index 6f61f16788cd2a0a..d0e52ccc26431380 100644 --- a/BaseTools/Source/C/GenFw/GenFw.c +++ b/BaseTools/Source/C/GenFw/GenFw.c @@ -2932,7 +2932,8 @@ Routine Description: if (mIsConvertXip) {=0D DebugEntry->FileOffset =3D DebugEntry->RVA;=0D }=0D - if (ZeroDebugFlag || DebugEntry->Type !=3D EFI_IMAGE_DEBUG_TYPE_CODE= VIEW) {=0D + if ((ZeroDebugFlag || DebugEntry->Type !=3D EFI_IMAGE_DEBUG_TYPE_COD= EVIEW) &&=0D + (DebugEntry->Type !=3D EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTIC= S)) {=0D memset (FileBuffer + DebugEntry->FileOffset, 0, DebugEntry->SizeOf= Data);=0D memset (DebugEntry, 0, sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY));= =0D }=0D diff --git a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h b/BaseTo= ols/Source/C/Include/IndustryStandard/PeImage.h index 77ded3f611398278..22161edf443d0e22 100644 --- a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h +++ b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h @@ -615,7 +615,8 @@ typedef struct { ///=0D /// Debug Format=0D ///=0D -#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2=0D +#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2=0D +#define EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS 20=0D =0D typedef struct {=0D UINT32 Characteristics;=0D @@ -664,6 +665,16 @@ typedef struct { //=0D } EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY;=0D =0D +///=0D +/// Extended DLL Characteristics=0D +///=0D +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT 0x0001=0D +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT 0x0040=0D +=0D +typedef struct {=0D + UINT32 DllCharacteristicsEx;=0D +} EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY;=0D +=0D //=0D // .pdata entries for X64=0D //=0D --=20 2.39.2