From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web11.32027.1679914936261764428 for ; Mon, 27 Mar 2023 04:02:16 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DsItktlX; spf=pass (domain: kernel.org, ip: 145.40.68.75, 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 ams.source.kernel.org (Postfix) with ESMTPS id B18EFB80DA3; Mon, 27 Mar 2023 11:02:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE846C433A1; Mon, 27 Mar 2023 11:02:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679914933; bh=FSbO4xuZK00mkh4Aj13DgvEpN1XroFToOKX0pr/Pn6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DsItktlXO+Kq4nc5bAoWlbpm5sAC5B5gkjHAxkKPaChTzwdJfbfBZy3MjKa7FxWTE hboWv1pfbELToTzMQGr+TWzFsZ9UQe1uFg23e/2DEgJsRdDLAbUTI+1yT6dlwtZ5Ul e7nB1Inre5E3weML0EzAl5rmQ/JzH6qHN4tGazz16LojE3kBJijAJc5Bb2TEBvACrm 0DcZ5zZE7xQJEUKLmVbphtIOe251hpMKo3pYenzFsvy+VvYnQoZnkAe2fFkLVG07ti K5k+GTngsORvz+miPI5RLX5HQh/R6HaVhcNdcfNv/+0i99Z6P1pKZNSLQQZHJn7Uyj bDHPRGoe4ubWg== 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 Subject: [PATCH v2 14/17] BaseTools/GenFw: Add DllCharacteristicsEx field to debug data Date: Mon, 27 Mar 2023 13:01:09 +0200 Message-Id: <20230327110112.262503-15-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230327110112.262503-1-ardb@kernel.org> References: <20230327110112.262503-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 --- 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..5e9428ab98c7f68a 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 + UINT16 DllCharacteristicsEx;=0D +} EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY;=0D +=0D //=0D // .pdata entries for X64=0D //=0D --=20 2.39.2