From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web12.4002.1590549348009849854 for ; Tue, 26 May 2020 20:15:48 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@intel.onmicrosoft.com header.s=selector2-intel-onmicrosoft-com header.b=QwgvuO5A; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: bob.c.feng@intel.com) IronPort-SDR: JSMPKnO7QKUX/RH673XfI8Z5UURc/2Y7Pg/w8EG3K7rqfCoD76gTn283e48FyHl1ll/jfhdCei cDh8ieAYV+jw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2020 20:15:47 -0700 IronPort-SDR: ZLO3+EUAmapUzgZmSknZE6mQjddIgGOBkxY5PB63mWE/Ec3N8uNUNs/ilxf7KOsdFkeiw7qBYl awllCIce+Log== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,439,1583222400"; d="scan'208";a="291442537" Received: from fmsmsx108.amr.corp.intel.com ([10.18.124.206]) by fmsmga004.fm.intel.com with ESMTP; 26 May 2020 20:15:46 -0700 Received: from fmsmsx126.amr.corp.intel.com (10.18.125.43) by FMSMSX108.amr.corp.intel.com (10.18.124.206) with Microsoft SMTP Server (TLS) id 14.3.439.0; Tue, 26 May 2020 20:15:46 -0700 Received: from FMSEDG001.ED.cps.intel.com (10.1.192.133) by FMSMSX126.amr.corp.intel.com (10.18.125.43) with Microsoft SMTP Server (TLS) id 14.3.439.0; Tue, 26 May 2020 20:15:42 -0700 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.174) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (TLS) id 14.3.439.0; Tue, 26 May 2020 20:15:42 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PvAxB0dtkfmALM0a1Zzah2JBrHSB7xMIdNHYPeIK9gDMKMfRTBNgIVz8vNlj6z2bir7m4SgDHl+WkoM6K1ozOt7GWKx6Ovmb+pbKngRwhlSBE2+h88pEqoIwy/KJ8yFao/tFoYpvs4IProBYJsd4q9cOLcaoI4ZEmypu6gbxvTx8Ui9O3WzPa4xxj15tljJFKru6MVy9mHPB5sz+71jOmgIM6BKslxu/Mnow2Ge+daO85Nyrds7rbLe+S7hQdNT+BzEPR1kZc/8lugVNkrZmvy1SJnl/4HNSj9GmKc5a51TVxq304lHPQ6vtMiEiYFcIGdIknPxEjlTShJOBPBNDug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KDW9+vMinPAUIpx+lKIdRjJPzv9tJOwW7aNTSnM+IQE=; b=Smgmqy85Cc55ErLgYjRjNAm0DOrBaQ01xhntGHo7VGBlrNXp9W7euFOwEo5anppjtZJJU1qpyMT6nXLZr6DX1/g/41GYRFpBKaLM3Kk01LJEcB7UAkkmcTE3x93ivJbUHWzQCSjnTnL5ERO+/X21nIBk0hH19MJH5yd+R6BY9OOmgeX9MnjVyzPo7TAr3jRsto8PEn8tUNcvB1qeUWE9xqcxXEd8bZtKaqbgWQ+MYMqjOrb6rEvnGp8BaaCdPHhk8OFBy3cTZHbPl6vfBbDJ1pHZYJKE+4Nkdip2AoJnZWAfIN+xGTe7K/iOXyGzwfrvdMFoS/WtAX1yvKQmtyyLNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.onmicrosoft.com; s=selector2-intel-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KDW9+vMinPAUIpx+lKIdRjJPzv9tJOwW7aNTSnM+IQE=; b=QwgvuO5AEQcPGyxVtGnCLMAx2OoYaZ1NHR59pDPGlF2sdjIiU5XpEutraXWQk7rZ8/TFlIDh5W5tRBTebPOkvAt2DYRGHJdQ/hFSKbD5K/+PkE7ekQiPC72ZHgLPRyi8SA8WhQIVVwFD+F4IukIiksPktAyq/KNQvXxaaspitFE= Received: from BN6PR11MB0068.namprd11.prod.outlook.com (2603:10b6:405:69::17) by BN6PR11MB1251.namprd11.prod.outlook.com (2603:10b6:404:48::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3045.17; Wed, 27 May 2020 03:15:40 +0000 Received: from BN6PR11MB0068.namprd11.prod.outlook.com ([fe80::2d53:d47c:1269:d601]) by BN6PR11MB0068.namprd11.prod.outlook.com ([fe80::2d53:d47c:1269:d601%6]) with mapi id 15.20.3021.030; Wed, 27 May 2020 03:15:40 +0000 From: "Bob Feng" To: Andrew Fish , "devel@edk2.groups.io" CC: "Gao, Liming" Subject: Re: [PATCH 1/3] BaseTools/GenFv: Add PE/COFF resource sections injection to GenFw Thread-Topic: [PATCH 1/3] BaseTools/GenFv: Add PE/COFF resource sections injection to GenFw Thread-Index: AQHWMhFPVFP26/sDJ0mPj4gEcFTvG6i7Prvg Date: Wed, 27 May 2020 03:15:40 +0000 Message-ID: References: <7471ab95f62b357b59ef22bb1c59ccea43f785a3.1590354726.git.afish@apple.com> In-Reply-To: <7471ab95f62b357b59ef22bb1c59ccea43f785a3.1590354726.git.afish@apple.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: apple.com; dkim=none (message not signed) header.d=none;apple.com; dmarc=none action=none header.from=intel.com; x-originating-ip: [192.102.204.45] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: e575b2c5-828b-4ffd-f2bf-08d801ec3c45 x-ms-traffictypediagnostic: BN6PR11MB1251: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:935; x-forefront-prvs: 04163EF38A x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: iNJXgv6j3BpuXcxYDPTT4qMEIKJo93zpBSU6x/pJUM/0ibwj6uzIOEVwUxVFovWmRD0OGy8ouqtPlWo4NN2rEnkw9g9kbSOhoB7C4Ho8l45G0wEAA4qAC1DvvkSxM8PgMR3qJ5W80SjhwqVwIh+/fEVQZh6jD/ma9juxbi5fB5WLIO+/Bn3XfC/wKn6fvI8Yjrf83EUrtoBxr0VZ0SV4k4XVToUv9Uuid7t2I0/SwMBSr+aIcz4T5CTA/5vW2VNTPSM/Cmyxn0DOp96E1ykugOkNVRnsx4JR0yDHEtMU0hidosn/PHqHFCaC2FE1MGV3pFy8gi8ZvLInHxU8zWzoyU/55hpvz9gS61QGKTKjGQXfb4IN5mpCeeUUY6hVDU1uh1vxKwDvEGs0iqHfuU2JZMNOgGtMY8gbfhsYesZs0Y+dq5YOE0sbTjaF8/e/W1A5y6z/CITwsuV/S0jQ/rR7w8+SrFro4tU1GS+ywlHb1MQ= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN6PR11MB0068.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(136003)(346002)(39860400002)(396003)(376002)(366004)(66446008)(66946007)(66556008)(8676002)(7696005)(33656002)(26005)(53546011)(186003)(316002)(52536014)(6506007)(110136005)(30864003)(83380400001)(71200400001)(5660300002)(9686003)(478600001)(2906002)(64756008)(66476007)(4326008)(107886003)(86362001)(45080400002)(55016002)(76116006)(966005)(8936002)(545624004)(460985005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: pTq3pdVWsAvF0T23hZvmgsLJUBZ1wxiMA7+SHKsnGUNIFquo+ORC3xinzKY1ilpbW0whWVL7X6d9V4UdMiH07UCqTlIt/fGTh+15vmAkzpx5zGYV16MfOJ6MRD++5yF07Lxj3EPOXJ/FoT+AxJmvj7wsjzr0Mrtec5nJo/rOhKQZFalIT2Dh73LBAfN2EnE0KG5sP2t48CaVj3XBaQjWzRE7tGCYne8YsFewegfZEVct3fysye1m3UuEuzFoBFbn1zxfApGpJ+3+DhZdx4arNNgFhpqI074HYEbVasBkg5seyfDFh7t/MMFD+rLrn46hurFxkf26QVBjYj8I7pAGnJIeqnjH9DKEzajVoERt5Mubz4fCHzPBtEXLsmIhLpJBUsLRR1nUWPzOSRurowXQQW8/jfJKlA+QpWz2HFhVZcXWOsv4v+o8CdlrfiL6fu5IsIUyVx07RKf5PEA2wZMicJjgOY5Ro+X9JREOHvPNScYywVHV2/5VukpmEZKY+ncj MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: e575b2c5-828b-4ffd-f2bf-08d801ec3c45 X-MS-Exchange-CrossTenant-originalarrivaltime: 27 May 2020 03:15:40.6942 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: JrdxRpOIBzJ2bxzbkiaGZnt/TTYZ7Dk549NRy8bwsbuhk012SVwh7qnUIkDOdewZhr8qdtq0TBTYf54Zaie0YQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR11MB1251 Return-Path: bob.c.feng@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Andrew, This patch cause building GenFw failure. cl.exe -c /nologo /Zi /c /O2 /MT /W4 /WX /D _CRT_SECURE_NO_DEPRECATE /D _C= RT_NONSTDC_NO_DEPRECATE -I . -I D:\Edk2Maintain\edk2head\edk2\BaseTools\Sou= rce\C\Include -I D:\Edk2Maintain\edk2head\edk2\BaseTools\Source\C\Include\I= a32 -I D:\Edk2Maintain\edk2head\edk2\BaseTools\Source\C\Common GenFw.c -Fo= GenFw.obj GenFw.c GenFw.c(3047): error C2220: the following warning is treated as an error GenFw.c(3047): warning C4244: '=3D': conversion from 'UINT32' to 'UINT16', = possible loss of data NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio= \2019\Community\VC\Tools\MSVC\14.25.28610\bin\HostX86\x86\cl.exe"' : return= code '0x2' Stop. The possible fix could be change the parameter Type as UINT16 data type. Wo= uld you update the patch? + +STATIC +EFI_STATUS +PatchResourceData ( + IN UINT32 Type, + IN UINT8 *ResourceData, + IN UINT32 ResourceDataSize, + IN OUT UINT8 **PeCoff, + IN OUT UINT32 *PeCoffSize + ) +/*++ And a minor comment is about the commit message. BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D557 should change to R= EF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D557=20 Thanks, Bob -----Original Message----- From: Andrew Fish =20 Sent: Monday, May 25, 2020 5:20 AM To: devel@edk2.groups.io Cc: Andrew Fish ; Feng, Bob C ; Gao,= Liming Subject: [PATCH 1/3] BaseTools/GenFv: Add PE/COFF resource sections injecti= on to GenFw BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D557 The XCODE toolchain does not suport injecting resource sections via librari= es so add --rc to GenFw to inject $(MODULE_NAME)hii.rc into the final PE/CO= FF image. Since moving exiting code around would break source level debugging we must= reuse an existing empty section, or add a new section header. If there is not space to add a new section header append to the last sectio= n. The resource entry if found via a directory entry so the PE/COFF loading= code does not depend on the section type. Signed-off-by: Andrew Fish Cc: Bob Feng Cc: Liming Gao --- BaseTools/Source/C/GenFw/GenFw.c | 370 +++++++++++++++++++++++++++++++ 1 file changed, 370 insertions(+) diff --git a/BaseTools/Source/C/GenFw/GenFw.c b/BaseTools/Source/C/GenFw/Ge= nFw.c index 8cab70ba4d5f..748af5dff259 100644 --- a/BaseTools/Source/C/GenFw/GenFw.c +++ b/BaseTools/Source/C/GenFw/GenFw.c @@ -96,6 +96,16 @@ ZeroDebugData ( BOOLEAN ZeroDebug ); +STATIC+EFI_STATUS+PatchResourceData (+ I= N UINT32 Type,+ IN UINT8 *ResourceData,+ IN UINT32 Resour= ceDataSize,+ IN OUT UINT8 **PeCoff,+ IN OUT UINT32 *PeCoffSize+ );+ S= TATIC EFI_STATUS SetStamp (@@ -267,6 +277,11 @@ Returns: except for -o option. It is a action option.\n\ = If it is combined with other action options, the late= r\n\ input action option will override the previous= one.\n");+ fprintf (stdout, " --rc FlieName Append a Hii resourc= e section to the\n\+ last PE/COFF section. The FileN= ame is the resource section to append\n\+ If FileNam= e does not exist this operation is skipped. This feature is\n\+ = only intended for toolchains, like XCODE, that don't suport $(R= C).\n\+ This option can only be combined with -e\n")= ; fprintf (stdout, " --rebase NewAddress Rebase image to new base addr= ess. New address \n\ is also set to the first none = code section header.\n\ It can't be combined with o= ther action options\n\@@ -1059,10 +1074,12 @@ Returns: CHAR8 **InputFileName; char = *OutImageName; char *ModuleType;= + char *RcFileName; CHAR8 = *TimeStamp; FILE *fpIn; FILE = *fpOut; FILE *fpInOut;= + FILE *fpRc; UINT32 = Data; UINT32 *DataPointer; UINT32 = *OldDataPointer;@@ -1080,6 +1097,8 @@ Returns: UINT32 OutputFileLength; UINT8 = *InputFileBuffer; UINT32 InputFil= eLength;+ UINT8 *RcFileBuffer;+ UINT32 = RcFileLength; RUNTIME_FUNCTION *Runtim= eFunction; UNWIND_INFO *UnwindInfo; STATUS = Status;@@ -1116,6 +1135,7 @@ Returns: time_t OutputFileTime; struct stat = Stat_Buf; BOOLEAN ZeroDebugFlag;+ B= OOLEAN InsertRcFile; SetUtilityName (UTILITY_NA= ME); @@ -1128,6 +1148,7 @@ Returns: mInImageName =3D NULL; OutImageName =3D NULL; ModuleType = =3D NULL;+ RcFileName =3D NULL; Type =3D 0; = Status =3D STATUS_SUCCESS; FileBuffer =3D NULL;@@ -1164= ,6 +1185,7 @@ Returns: InputFileTime =3D 0; OutputFileTime =3D 0; ZeroDebu= gFlag =3D FALSE;+ InsertRcFile =3D FALSE; if (argc = =3D=3D 1) { Error (NULL, 0, 1001, "Missing options", "No input options.= ");@@ -1436,6 +1458,20 @@ Returns: continue; } + if (stricmp (argv[0], "--rc") =3D=3D 0) {+ = RcFileName =3D argv[1];+ argc -=3D 2;+ argv +=3D 2;++ if (s= tat (RcFileName, &Stat_Buf) =3D=3D 0) {+ //+ // File exists+ = //+ InsertRcFile =3D TRUE;+ }+ continue;+ }+ = if (argv[0][0] =3D=3D '-') { Error (NULL, 0, 1000, "Unknown option",= argv[0]); goto Finish;@@ -1568,6 +1604,10 @@ Returns: break; } + if (InsertRcFile) {+ VerboseMsg ("RC input file %s", = RcFileName);+ }+ if (ReplaceFlag) { VerboseMsg ("Overwrite the input= file with the output content."); }@@ -2052,6 +2092,52 @@ Returns: } } + //+ // Insert Resources into the image.+ //+ if (InsertRcF= ile) {+ fpRc =3D fopen (LongFilePath (RcFileName), "rb");+ if (fpRc = =3D=3D NULL) {+ Error (NULL, 0, 0001, "Error opening file", RcFileName= );+ goto Finish;+ }++ RcFileLength =3D _filelength (fileno (fpRc= ));+ RcFileBuffer =3D malloc (RcFileLength);+ if (FileBuffer =3D=3D N= ULL) {+ Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!= ");+ fclose (fpRc);+ goto Finish;+ }++ fread (RcFileBuffer,= 1, RcFileLength, fpRc);+ fclose (fpRc);++ Status =3D PatchResourceDa= ta (Type, RcFileBuffer, RcFileLength, &FileBuffer, &FileLength);+ if (EF= I_ERROR (Status)) {+ Error (NULL, 0, 3000, "Invalid", "RC Patch Data E= rror status is 0x%x", (int) Status);+ goto Finish;+ }++ fpOut = =3D fopen (LongFilePath (OutImageName), "wb");+ if (fpOut =3D=3D NULL) {= + Error (NULL, 0, 0001, "Error opening output file", OutImageName);+ = goto Finish;+ }++ fwrite (FileBuffer, 1, FileLength, fpOut);++ = fclose (fpOut);+ fpOut =3D NULL;+ VerboseMsg ("the size of output fi= le is %u bytes", (unsigned) FileLength);++ //+ // Write the updated I= mage+ //+ goto Finish;+ }++ // // Convert ELF image to PeImage = //@@ -2761,6 +2847,290 @@ Finish: return GetUtilityStatus (); } +#define ALIGN_VALUE(Value, Alignment) ((V= alue) + (((Alignment) - (Value)) & ((Alignment) - 1)))++STATIC+EFI_STATUS+P= atchResourceData (+ IN UINT32 Type,+ IN UINT8 *ResourceData,+ = IN UINT32 ResourceDataSize,+ IN OUT UINT8 **PeCoff,+ IN OUT UINT3= 2 *PeCoffSize+ )+/*++++Routine Description:++ Embed Resource data into a= PE/COFF image.++ If there is free space between the header and the image = add a new+ .rsrc section to the PE/COFF image. If no space exists then app= end+ the resource data to the last section.++Arguments:++ Type = - If not zero them update PE/COFF header module type.+ ResourceData = - *hii.rc data to insert into the image.+ ResourceDataSize - Size of Resou= rceData in bytes.+ PeCoff - On input existing PE/COFF, on output= input PE/COFF with+ ResourceData embedded.+ PeCoffSiz= e - Size of PeCoff in bytes.++Returns:++ EFI_ABORTED - PeImage is = invalid.+ EFI_SUCCESS - Zero debug data successfully.++--*/+{+ UINT8 = *FileBuffer;+ UINT32 = FileBufferSize;+ EFI_IMAGE_DOS_HEADER *DosHdr;+ = EFI_IMAGE_FILE_HEADER *FileHdr;+ EFI_IMAGE_OPTIONAL_HEADER= 32 *Optional32Hdr;+ EFI_IMAGE_OPTIONAL_HEADER64 *Optio= nal64Hdr;+ EFI_IMAGE_SECTION_HEADER *SectionHeader;+ UINT32 = FileAlignment;+ UINT32 = Max;+ INTN LastSection;+ INTN= EmptySection;+ UINT32 = ActualHeaderSize;+ UINT32 Star= tingPeCoffSize;+ UINT32 NewHeaderSize;+ CH= AR8 *Base;+ EFI_IMAGE_RESOURCE_DIRECTORY = *ResourceDirectory;+ EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *Resour= ceDirectoryEntry;+ EFI_IMAGE_RESOURCE_DIRECTORY_STRING *ResourceDirector= yString;+ EFI_IMAGE_RESOURCE_DATA_ENTRY *ResourceDataEntry;+ EFI_= IMAGE_DATA_DIRECTORY *DirectoryEntry;+ CHAR16 = *String;+ UINT32 Offset;+ = UINT32 Index;++ //+ // Grow the file in un= its of FileAlignment+ //+ DosHdr =3D (EFI_IMAGE_DOS_HEADER *) *PeCoff;= + if (DosHdr->e_magic !=3D EFI_IMAGE_DOS_SIGNATURE) {+ // NO DOS header= , must start with PE/COFF header+ FileHdr =3D (EFI_IMAGE_FILE_HEADER *)= (*PeCoff + sizeof (UINT32));+ } else {+ FileHdr =3D (EFI_IMAGE_FILE_HE= ADER *)(*PeCoff + DosHdr->e_lfanew + sizeof (UINT32));+ }++ Optional32Hdr= =3D (EFI_IMAGE_OPTIONAL_HEADER32 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_= FILE_HEADER));+ Optional64Hdr =3D (EFI_IMAGE_OPTIONAL_HEADER64 *) ((UINT8*= ) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));+ if (Optional32Hdr->Magic =3D= =3D EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {+ FileAlignment =3D Optional= 32Hdr->FileAlignment;+ SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *= ) ((UINT8 *) Optional32Hdr + FileHdr->SizeOfOptionalHeader);+ } else {+ = FileAlignment =3D Optional64Hdr->FileAlignment;+ SectionHeader = =3D (EFI_IMAGE_SECTION_HEADER *) ((UINT8 *) Optional64Hdr + FileHdr->Size= OfOptionalHeader);+ }++ LastSection =3D -1;+ for (Index =3D 0, Max =3D 0= ; Index < FileHdr->NumberOfSections; Index++) {+ if (SectionHeader[Index= ].PointerToRawData > Max) {+ Max =3D SectionHeader[Index].PointerToRaw= Data;+ LastSection =3D Index;+ }+ }++ EmptySection =3D -1;+ for = (Index =3D 0; Index < FileHdr->NumberOfSections; Index++) {+ if ((Sectio= nHeader[Index].Misc.VirtualSize =3D=3D 0) && (SectionHeader[Index].SizeOfRa= wData =3D=3D 0)) {+ //+ // No Data or Zero Fill so we can repurp= ose this entry.+ //+ EmptySection =3D Index;+ break;+ }+ = }++ if (EmptySection =3D=3D -1) {+ ActualHeaderSize =3D (UINTN)(&Secti= onHeader[FileHdr->NumberOfSections]) - (UINTN)*PeCoff;+ if ((ActualHeade= rSize + sizeof (EFI_IMAGE_SECTION_HEADER)) <=3D Optional32Hdr->SizeOfHeader= s) {+ //+ // There is space to inject a new section.+ //+ = FileHdr->NumberOfSections +=3D 1;+ EmptySection =3D Index;+ }+ = }++ StartingPeCoffSize =3D SectionHeader[LastSection].PointerToRawData + S= ectionHeader[LastSection].SizeOfRawData;+ if (SectionHeader[LastSection].M= isc.VirtualSize > SectionHeader[LastSection].SizeOfRawData) {+ StartingP= eCoffSize +=3D SectionHeader[LastSection].Misc.VirtualSize - SectionHeader[= LastSection].SizeOfRawData;+ }++ FileBufferSize =3D ALIGN_VALUE(Start= ingPeCoffSize + ResourceDataSize, FileAlignment);+ FileBuffer =3D malloc (= FileBufferSize);+ if (FileBuffer =3D=3D NULL) {+ return RETURN_OUT_OF_R= ESOURCES;+ }+ memset (FileBuffer, 0, FileBufferSize);++ //+ // Append t= he Resource Data to the end of the PE/COFF image.+ //+ NewHeaderSize =3D = Optional32Hdr->SizeOfHeaders;+ CopyMem (FileBuffer, *PeCoff, (StartingPeCo= ffSize > *PeCoffSize) ? *PeCoffSize: StartingPeCoffSize);+ CopyMem (FileB= uffer + StartingPeCoffSize, ResourceData, ResourceDataSize);++ free (*PeCo= ff);+ *PeCoff =3D FileBuffer;+ *PeCoffSize =3D FileBufferSize;++ Dos= Hdr =3D (EFI_IMAGE_DOS_HEADER *)FileBuffer;+ if (DosHdr->e_magic !=3D EFI_= IMAGE_DOS_SIGNATURE) {+ // NO DOS header, must start with PE/COFF header= + FileHdr =3D (EFI_IMAGE_FILE_HEADER *)(FileBuffer + sizeof (UINT32));+= } else {+ FileHdr =3D (EFI_IMAGE_FILE_HEADER *)(FileBuffer + DosHdr->= e_lfanew + sizeof (UINT32));+ }++ //+ // Get Resource EntryTable offset,= and Section header+ //+ Optional32Hdr =3D (EFI_IMAGE_OPTIONAL_HEADER32 *= ) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));+ Optional64Hdr =3D = (EFI_IMAGE_OPTIONAL_HEADER64 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_= HEADER));+ DirectoryEntry =3D NULL;+ if (Optional32Hdr->Magic =3D=3D EFI_= IMAGE_NT_OPTIONAL_HDR32_MAGIC) {+ SectionHeader =3D (EFI_IMAGE_SECTION_H= EADER *) ((UINT8 *) Optional32Hdr + FileHdr->SizeOfOptionalHeader);+ if= (Optional32Hdr->NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE) = {+ DirectoryEntry =3D &Optional32Hdr->DataDirectory[EFI_IMAGE_DIRECTOR= Y_ENTRY_RESOURCE];+ }+ } else {+ SectionHeader =3D (EFI_IMAGE_SECTIO= N_HEADER *) ((UINT8 *) Optional64Hdr + FileHdr->SizeOfOptionalHeader);+ = if (Optional64Hdr->NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_RESOURC= E) {+ DirectoryEntry =3D &Optional64Hdr->DataDirectory[EFI_IMAGE_DIREC= TORY_ENTRY_RESOURCE];+ }+ }++ if (DirectoryEntry =3D=3D NULL) {+ re= turn RETURN_OUT_OF_RESOURCES;+ }++ if (EmptySection !=3D -1) {+ //+ = // Create a new section+ //+ CopyMem (SectionHeader[EmptySection].Na= me, ".rsrc\0\0\0", EFI_IMAGE_SIZEOF_SHORT_NAME);+ SectionHeader[EmptySec= tion].Misc.VirtualSize =3D ResourceDataSize;+ SectionHeader[EmptySection= ].VirtualAddress =3D StartingPeCoffSize;+ SectionHeader[EmptySection].= SizeOfRawData =3D ALIGN_VALUE(ResourceDataSize, FileAlignment);+ Sect= ionHeader[EmptySection].PointerToRawData =3D StartingPeCoffSize;++ Secti= onHeader[EmptySection].Characteristics =3D EFI_IMAGE_SCN_MEM_EXECUTE | EFI= _IMAGE_SCN_MEM_READ;+ SectionHeader[EmptySection].Characteristics |=3D E= FI_IMAGE_SCN_CNT_INITIALIZED_DATA | EFI_IMAGE_SCN_CNT_CODE;++ DirectoryE= ntry->VirtualAddress =3D SectionHeader[EmptySection].VirtualAddress;+ } el= se {+ //+ // Grow the last section to include the resources.+ // F= or Xcode this is always the .debug section.+ //+ SectionHeader[LastSe= ction].SizeOfRawData =3D ALIGN_VALUE(SectionHeader[LastSection].SizeOfRawDa= ta + ResourceDataSize, FileAlignment);++ //+ // Make sure the Virtual= Size uses the file aligned actual size, since we are growing the file.+ = //+ SectionHeader[LastSection].Misc.VirtualSize =3D SectionHeader[LastS= ection].SizeOfRawData;++ DirectoryEntry->VirtualAddress =3D StartingPeCo= ffSize;+ }+ DirectoryEntry->Size =3D ResourceDataSize;++ Optio= nal32Hdr->SizeOfImage =3D ALIGN_VALUE(*PeCoffSize, Optional32Hdr->SectionAl= ignment);+ if (Type !=3D 0) {+ Optional32Hdr->Subsystem =3D Type;+ }++= //+ // It looks like the ResourceDataEntry->OffsetToData is relative to = the rc file,+ // but PeCoffLoaderLoadImage() assumes it is a PE/COFF Virtu= alAddress so we need+ // to fix it up. Walk the ResourceDataEntry just lik= e PeCoffLoaderLoadImage() and+ // patch it.+ //+ Base = =3D (CHAR8 *)(FileBuffer + StartingPeCoffSize);+ ResourceDirectory = =3D (EFI_IMAGE_RESOURCE_DIRECTORY *)Base;+ ResourceDirectoryEntry =3D (EFI= _IMAGE_RESOURCE_DIRECTORY_ENTRY *)(ResourceDirectory + 1);++ for (Index = =3D 0; Index < ResourceDirectory->NumberOfNamedEntries; Index++) {+ if (= ResourceDirectoryEntry->u1.s.NameIsString) {+ //+ // Check the Re= sourceDirectoryEntry->u1.s.NameOffset before use it.+ //+ if (Res= ourceDirectoryEntry->u1.s.NameOffset >=3D DirectoryEntry->Size) {+ = return RETURN_UNSUPPORTED;+ }+ ResourceDirectoryString =3D (EFI_I= MAGE_RESOURCE_DIRECTORY_STRING *) (Base + ResourceDirectoryEntry->u1.s.Name= Offset);+ String =3D &ResourceDirectoryString->String[0];++ if (R= esourceDirectoryString->Length =3D=3D 3 &&+ String[0] =3D=3D L'H' = &&+ String[1] =3D=3D L'I' &&+ String[2] =3D=3D L'I') {+ = //+ // Resource Type "HII" found+ //+ if (Resour= ceDirectoryEntry->u2.s.DataIsDirectory) {+ //+ // Move to= next level - resource Name+ //+ if (ResourceDirectoryEnt= ry->u2.s.OffsetToDirectory >=3D DirectoryEntry->Size) {+ return = RETURN_UNSUPPORTED;+ }+ ResourceDirectory =3D (EFI_IMAGE_= RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirector= y);+ Offset =3D ResourceDirectoryEntry->u2.s.OffsetToDirectory + s= izeof (EFI_IMAGE_RESOURCE_DIRECTORY) ++ sizeof (EFI_IMAGE= _RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + Res= ourceDirectory->NumberOfIdEntries);+ if (Offset > DirectoryEntry->= Size) {+ return RETURN_UNSUPPORTED;+ }+ Resour= ceDirectoryEntry =3D (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *) (ResourceDirect= ory + 1);++ if (ResourceDirectoryEntry->u2.s.DataIsDirectory) {+ = //+ // Move to next level - resource Language+ = //+ if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >=3D = DirectoryEntry->Size) {+ return RETURN_UNSUPPORTED;+ = }+ ResourceDirectory =3D (EFI_IMAGE_RESOURCE_DIRECTORY *) (Bas= e + ResourceDirectoryEntry->u2.s.OffsetToDirectory);+ Offset =3D= ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURC= E_DIRECTORY) ++ sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_EN= TRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->Number= OfIdEntries);+ if (Offset > DirectoryEntry->Size) {+ = return RETURN_UNSUPPORTED;+ }+ ResourceDirectoryEnt= ry =3D (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *) (ResourceDirectory + 1);+ = }+ }++ //+ // Now it ought to be resource Data+ = //+ if (!ResourceDirectoryEntry->u2.s.DataIsDirectory) {+ = if (ResourceDirectoryEntry->u2.OffsetToData >=3D DirectoryEntry->Size= ) {+ return RETURN_UNSUPPORTED;+ }+ ResourceDa= taEntry =3D (EFI_IMAGE_RESOURCE_DATA_ENTRY *) (Base + ResourceDirectoryEntr= y->u2.OffsetToData);++ //+ // Adjust OffsetToData to be a= PE/COFF Virtual address in the updated image.+ //+ Resou= rceDataEntry->OffsetToData +=3D (UINTN)DirectoryEntry->VirtualAddress;+ = break;+ }+ }+ }+ ResourceDirectoryEntry++;+ }++ = return EFI_SUCCESS;+}++ STATIC EFI_STATUS ZeroDebugData (--=20 2.24.1 (Apple Git-126)