From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web11.27355.1680187498341836040 for ; Thu, 30 Mar 2023 07:44:58 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=frDR34P9; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from [192.168.4.22] (unknown [47.201.8.94]) by linux.microsoft.com (Postfix) with ESMTPSA id 3482620FEA21; Thu, 30 Mar 2023 07:44:56 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3482620FEA21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680187497; bh=NwB3stvt51ComStDNGTlELLtFs+j+aSL7s4x0a7NKsA=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=frDR34P9Kt0A8BpZ4FkG8Acbud98bGg/OenqgM1s26k7BSiCdQdYabTA4zyA8suk4 G3n85VEn1SlV/7XLd0Vq5rfOa7hww8Z/cVkwmxdtdT1EEDGeJnn0AgB7JpLUyT8Bt2 8hYwGzk0w3IeQtg7RY2GxjgnnmuFB4I0P53PiRVw= Message-ID: <032fa5e2-d593-7129-27e4-e8d730814bb9@linux.microsoft.com> Date: Thu, 30 Mar 2023 10:44:55 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [edk2-devel] [PATCH v2 16/17] MdePkg/PeCoffLib: Capture DLL characteristics fieldis in image context To: devel@edk2.groups.io, ardb@kernel.org Cc: Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , =?UTF-8?Q?Marvin_H=c3=a4user?= , Bob Feng References: <20230327110112.262503-1-ardb@kernel.org> <20230327110112.262503-17-ardb@kernel.org> From: "Michael Kubacki" In-Reply-To: <20230327110112.262503-17-ardb@kernel.org> Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/27/2023 7:01 AM, Ard Biesheuvel wrote: > When loading a PE/COFF image, capture the DLL characteristics fields of > the header into our image context structure so we can refer to them when > mapping the image. > > Signed-off-by: Ard Biesheuvel > --- > MdePkg/Include/IndustryStandard/PeImage.h | 13 +++++- > MdePkg/Include/Library/PeCoffLib.h | 6 +++ > MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 46 +++++++++++++++----- > 3 files changed, 53 insertions(+), 12 deletions(-) > > diff --git a/MdePkg/Include/IndustryStandard/PeImage.h b/MdePkg/Include/IndustryStandard/PeImage.h > index dd4cc25483bc4bcf..a5b9b848ed391f98 100644 > --- a/MdePkg/Include/IndustryStandard/PeImage.h > +++ b/MdePkg/Include/IndustryStandard/PeImage.h > @@ -625,7 +625,8 @@ typedef struct { > UINT32 FileOffset; ///< The file pointer to the debug data. > > } EFI_IMAGE_DEBUG_DIRECTORY_ENTRY; > > > > -#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 ///< The Visual C++ debug information. > > +#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2 ///< The Visual C++ debug information. > > +#define EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS 20 > > > > /// > > /// Debug Data Structure defined in Microsoft C++. > > @@ -669,6 +670,16 @@ typedef struct { > // > > } EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY; > > > > +/// > > +/// Extended DLL Characteristics > > +/// > > +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT 0x0001 > > +#define EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT 0x0040 > > + > > +typedef struct { > > + UINT16 DllCharacteristicsEx; > > +} EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY; > > + > > /// > > /// Resource format. > > /// > > diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/PeCoffLib.h > index b45879453785c77d..d21c932076c072f6 100644 > --- a/MdePkg/Include/Library/PeCoffLib.h > +++ b/MdePkg/Include/Library/PeCoffLib.h > @@ -171,6 +171,12 @@ typedef struct { > /// > > UINT16 ImageType; > > /// > > + /// Set by PeCoffLoaderGetImageInfo() to the DLL flags stored in the PE/COFF header and > > + /// in the DllCharacteristicsEx debug table. > > + /// > > + UINT16 DllCharacteristics; > > + UINT16 DllCharacteristicsEx; > I know DllCharacteristics has a size of 2 in the spec, but the DllCharacteristicsEx is defined as 4 bytes. I will try to get a spec update to clarify this. > + /// > > /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain > > /// relocation information. > > /// > > diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > index 97a8aaf8c73d3e3c..4b71176a0c7c2ed0 100644 > --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c > @@ -308,10 +308,11 @@ PeCoffLoaderGetPeHeader ( > // > > // Use PE32 offset > > // > > - ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem; > > - ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage; > > - ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment; > > - ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders; > > + ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem; > > + ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage; > > + ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment; > > + ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders; > > + ImageContext->DllCharacteristics = Hdr.Pe32->OptionalHeader.DllCharacteristics; > > } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { > > // > > // 1. Check FileHeader.NumberOfRvaAndSizes filed. > > @@ -429,10 +430,11 @@ PeCoffLoaderGetPeHeader ( > // > > // Use PE32+ offset > > // > > - ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem; > > - ImageContext->ImageSize = (UINT64)Hdr.Pe32Plus->OptionalHeader.SizeOfImage; > > - ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment; > > - ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders; > > + ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem; > > + ImageContext->ImageSize = (UINT64)Hdr.Pe32Plus->OptionalHeader.SizeOfImage; > > + ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment; > > + ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders; > > + ImageContext->DllCharacteristics = Hdr.Pe32Plus->OptionalHeader.DllCharacteristics; > > } else { > > ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE; > > return RETURN_UNSUPPORTED; > > @@ -545,8 +547,9 @@ PeCoffLoaderGetPeHeader ( > Retrieves information about a PE/COFF image. > > > > Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, > > - DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and > > - DebugDirectoryEntryRva fields of the ImageContext structure. > > + DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, > > + DllCharacteristics, DllCharacteristicsEx and DebugDirectoryEntryRva fields of > > + the ImageContext structure. > > If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. > > If the PE/COFF image accessed through the ImageRead service in the ImageContext > > structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. > > @@ -752,7 +755,28 @@ PeCoffLoaderGetImageInfo ( > ImageContext->ImageSize += DebugEntry.SizeOfData; > > } > > > > - return RETURN_SUCCESS; > > + continue; > > + } > > + > > + if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS) { > > + Size = sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY); > > + ReadSize = sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY); > > + Status = ImageContext->ImageRead ( > > + ImageContext->Handle, > > + DebugEntry.FileOffset, > > + &Size, > > + &ImageContext->DllCharacteristicsEx > > + ); > > + if (RETURN_ERROR (Status) || (Size != ReadSize)) { > > + ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; > > + if (Size != ReadSize) { > > + Status = RETURN_UNSUPPORTED; > > + } > > + > > + return Status; > > + } > > + > > + continue; > > } > > } > > } >