From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ml01.01.org (Postfix) with ESMTP id 3BBA51A1E05 for ; Fri, 19 Aug 2016 00:51:37 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 19 Aug 2016 00:51:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,544,1464678000"; d="scan'208";a="1044045996" Received: from fmsmsx107.amr.corp.intel.com ([10.18.124.205]) by fmsmga002.fm.intel.com with ESMTP; 19 Aug 2016 00:51:36 -0700 Received: from fmsmsx155.amr.corp.intel.com (10.18.116.71) by fmsmsx107.amr.corp.intel.com (10.18.124.205) with Microsoft SMTP Server (TLS) id 14.3.248.2; Fri, 19 Aug 2016 00:51:36 -0700 Received: from shsmsx152.ccr.corp.intel.com (10.239.6.52) by FMSMSX155.amr.corp.intel.com (10.18.116.71) with Microsoft SMTP Server (TLS) id 14.3.248.2; Fri, 19 Aug 2016 00:51:35 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.147]) by SHSMSX152.ccr.corp.intel.com ([169.254.6.107]) with mapi id 14.03.0248.002; Fri, 19 Aug 2016 15:51:34 +0800 From: "Gao, Liming" To: Ard Biesheuvel , "edk2-devel@lists.01.org" , "Zhu, Yonghong" CC: "leif.lindholm@linaro.org" Thread-Topic: [PATCH] BaseTools/GenFv: allow TE/PE images with no .reloc section Thread-Index: AQHR+XIMpEwAnPPo1UaflzRGm/3WsaBP4yKA Date: Fri, 19 Aug 2016 07:51:33 +0000 Message-ID: <4A89E2EF3DFEDB4C8BFDE51014F606A1155ED043@shsmsx102.ccr.corp.intel.com> References: <1471539628-6257-1-git-send-email-ard.biesheuvel@linaro.org> In-Reply-To: <1471539628-6257-1-git-send-email-ard.biesheuvel@linaro.org> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH] BaseTools/GenFv: allow TE/PE images with no .reloc section X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Aug 2016 07:51:37 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Ard: RelocationsStripped is decided by Pe32.FileHeader.Characteristics EFI_IMA= GE_FILE_RELOCS_STRIPPED flag. Per PE/COFF spec, if IMAGE_FILE_RELOCS_STRIPP= ED flag is set, the image must therefore be loaded at its preferred base ad= dress. If the image could be loaded at other address, it should not set thi= s flag in its PE header. Could you help check your PE/COFF image? Thanks Liming > -----Original Message----- > From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] > Sent: Friday, August 19, 2016 1:00 AM > To: edk2-devel@lists.01.org; Gao, Liming ; Zhu, > Yonghong > Cc: leif.lindholm@linaro.org; Ard Biesheuvel > Subject: [PATCH] BaseTools/GenFv: allow TE/PE images with no .reloc secti= on >=20 > Currently, GenFv warns about input files without a relocation section, > but still includes the FFS file in the image, but with its ImageBase > field left at zero. This confuses the loader routines in PEI core, > resulting in spurious write attempts to be made at the NOR flash. >=20 > When using the GCC LTO compiler for AArch64, the optimizations are so > effective that in some cases, all absolute symbol references are > optimized away completely, resulting in a PE/COFF image that has no > .reloc section at all (i.e., the PE/COFF image is position independent, > but purely by accident) >=20 > This means that, while unusual, it is not an error for a XIP image to > have no .reloc section. So teach GenFv about this, i.e., let it skip > the relocation routines, but still update the ImageBase address in the > header, and recalculate the checksums. >=20 > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Ard Biesheuvel > --- > BaseTools/Source/C/GenFv/GenFvInternalLib.c | 223 ++++++++++---------- > 1 file changed, 111 insertions(+), 112 deletions(-) >=20 > diff --git a/BaseTools/Source/C/GenFv/GenFvInternalLib.c > b/BaseTools/Source/C/GenFv/GenFvInternalLib.c > index 7c839e277944..db0978d4089d 100644 > --- a/BaseTools/Source/C/GenFv/GenFvInternalLib.c > +++ b/BaseTools/Source/C/GenFv/GenFvInternalLib.c > @@ -3500,63 +3500,63 @@ Returns: > // > if (ImageContext.RelocationsStripped) { > Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section= .", > FileName); > - continue; > - } > + } else { >=20 > - // > - // Relocation exist and rebase > - // > - // > - // Load and Relocate Image Data > - // > - MemoryImagePointer =3D (UINT8 *) malloc ((UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > - if (MemoryImagePointer =3D=3D NULL) { > - Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on r= ebase > of %s", FileName); > - return EFI_OUT_OF_RESOURCES; > - } > - memset ((VOID *) MemoryImagePointer, 0, (UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > - ImageContext.ImageAddress =3D ((UINTN) MemoryImagePointer + > ImageContext.SectionAlignment - 1) & (~((UINTN) > ImageContext.SectionAlignment - 1)); > - > - Status =3D PeCoffLoaderLoadImage (&ImageContext); > - if (EFI_ERROR (Status)) { > - Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on reb= ase of %s", > FileName); > - free ((VOID *) MemoryImagePointer); > - return Status; > - } > - > - ImageContext.DestinationAddress =3D NewPe32BaseAddress; > - Status =3D PeCoffLoaderRelocateImage (&Imag= eContext); > - if (EFI_ERROR (Status)) { > - Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on r= ebase > of %s", FileName); > - free ((VOID *) MemoryImagePointer); > - return Status; > - } > + // > + // Relocation exist and rebase > + // > + // > + // Load and Relocate Image Data > + // > + MemoryImagePointer =3D (UINT8 *) malloc ((UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > + if (MemoryImagePointer =3D=3D NULL) { > + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on > rebase of %s", FileName); > + return EFI_OUT_OF_RESOURCES; > + } > + memset ((VOID *) MemoryImagePointer, 0, (UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > + ImageContext.ImageAddress =3D ((UINTN) MemoryImagePointer + > ImageContext.SectionAlignment - 1) & (~((UINTN) > ImageContext.SectionAlignment - 1)); >=20 > - // > - // Copy Relocated data to raw image file. > - // > - SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *) ( > - (UINTN) ImgHdr + > - sizeof (UINT32) + > - sizeof (EFI_IMAGE_FILE_HEADER) + > - ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader > - ); > - > - for (Index =3D 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; > Index ++, SectionHeader ++) { > - CopyMem ( > - (UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize + > SectionHeader->PointerToRawData, > - (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader- > >VirtualAddress), > - SectionHeader->SizeOfRawData > - ); > - } > + Status =3D PeCoffLoaderLoadImage (&ImageContext); > + if (EFI_ERROR (Status)) { > + Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on r= ebase > of %s", FileName); > + free ((VOID *) MemoryImagePointer); > + return Status; > + } >=20 > - free ((VOID *) MemoryImagePointer); > - MemoryImagePointer =3D NULL; > - if (PeFileBuffer !=3D NULL) { > - free (PeFileBuffer); > - PeFileBuffer =3D NULL; > + ImageContext.DestinationAddress =3D NewPe32BaseAddress; > + Status =3D PeCoffLoaderRelocateImage (&Im= ageContext); > + if (EFI_ERROR (Status)) { > + Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on= rebase > of %s", FileName); > + free ((VOID *) MemoryImagePointer); > + return Status; > + } > + > + // > + // Copy Relocated data to raw image file. > + // > + SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *) ( > + (UINTN) ImgHdr + > + sizeof (UINT32) + > + sizeof (EFI_IMAGE_FILE_HEADER) + > + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader > + ); > + > + for (Index =3D 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections= ; > Index ++, SectionHeader ++) { > + CopyMem ( > + (UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize + > SectionHeader->PointerToRawData, > + (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader- > >VirtualAddress), > + SectionHeader->SizeOfRawData > + ); > + } > + > + free ((VOID *) MemoryImagePointer); > + MemoryImagePointer =3D NULL; > + if (PeFileBuffer !=3D NULL) { > + free (PeFileBuffer); > + PeFileBuffer =3D NULL; > + } > } > - > + > // > // Update Image Base Address > // > @@ -3730,70 +3730,69 @@ Returns: > // > if (ImageContext.RelocationsStripped) { > Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section= .", > FileName); > - continue; > - } > + } else { > + // > + // Relocation exist and rebase > + // > + // > + // Load and Relocate Image Data > + // > + MemoryImagePointer =3D (UINT8 *) malloc ((UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > + if (MemoryImagePointer =3D=3D NULL) { > + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on > rebase of %s", FileName); > + return EFI_OUT_OF_RESOURCES; > + } > + memset ((VOID *) MemoryImagePointer, 0, (UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > + ImageContext.ImageAddress =3D ((UINTN) MemoryImagePointer + > ImageContext.SectionAlignment - 1) & (~((UINTN) > ImageContext.SectionAlignment - 1)); >=20 > - // > - // Relocation exist and rebase > - // > - // > - // Load and Relocate Image Data > - // > - MemoryImagePointer =3D (UINT8 *) malloc ((UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > - if (MemoryImagePointer =3D=3D NULL) { > - Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on r= ebase > of %s", FileName); > - return EFI_OUT_OF_RESOURCES; > - } > - memset ((VOID *) MemoryImagePointer, 0, (UINTN) > ImageContext.ImageSize + ImageContext.SectionAlignment); > - ImageContext.ImageAddress =3D ((UINTN) MemoryImagePointer + > ImageContext.SectionAlignment - 1) & (~((UINTN) > ImageContext.SectionAlignment - 1)); > + Status =3D PeCoffLoaderLoadImage (&ImageContext); > + if (EFI_ERROR (Status)) { > + Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on r= ebase > of %s", FileName); > + free ((VOID *) MemoryImagePointer); > + return Status; > + } > + // > + // Relocate TeImage > + // > + ImageContext.DestinationAddress =3D NewPe32BaseAddress; > + Status =3D PeCoffLoaderRelocateImage (&Im= ageContext); > + if (EFI_ERROR (Status)) { > + Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on= rebase of > TE image %s", FileName); > + free ((VOID *) MemoryImagePointer); > + return Status; > + } >=20 > - Status =3D PeCoffLoaderLoadImage (&ImageContext); > - if (EFI_ERROR (Status)) { > - Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on reb= ase of %s", > FileName); > - free ((VOID *) MemoryImagePointer); > - return Status; > - } > - // > - // Reloacate TeImage > - // > - ImageContext.DestinationAddress =3D NewPe32BaseAddress; > - Status =3D PeCoffLoaderRelocateImage (&Imag= eContext); > - if (EFI_ERROR (Status)) { > - Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on r= ebase of > TE image %s", FileName); > + // > + // Copy the relocated image into raw image file. > + // > + SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1)= ; > + for (Index =3D 0; Index < TEImageHeader->NumberOfSections; Index += +, > SectionHeader ++) { > + if (!ImageContext.IsTeImage) { > + CopyMem ( > + (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - > TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, > + (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader- > >VirtualAddress), > + SectionHeader->SizeOfRawData > + ); > + } else { > + CopyMem ( > + (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - > TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, > + (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof > (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader- > >VirtualAddress), > + SectionHeader->SizeOfRawData > + ); > + } > + } > + > + // > + // Free the allocated memory resource > + // > free ((VOID *) MemoryImagePointer); > - return Status; > - } > - > - // > - // Copy the relocated image into raw image file. > - // > - SectionHeader =3D (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1); > - for (Index =3D 0; Index < TEImageHeader->NumberOfSections; Index ++, > SectionHeader ++) { > - if (!ImageContext.IsTeImage) { > - CopyMem ( > - (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - > TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, > - (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader- > >VirtualAddress), > - SectionHeader->SizeOfRawData > - ); > - } else { > - CopyMem ( > - (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - > TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, > - (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof > (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader- > >VirtualAddress), > - SectionHeader->SizeOfRawData > - ); > + MemoryImagePointer =3D NULL; > + if (PeFileBuffer !=3D NULL) { > + free (PeFileBuffer); > + PeFileBuffer =3D NULL; > } > } > - > - // > - // Free the allocated memory resource > - // > - free ((VOID *) MemoryImagePointer); > - MemoryImagePointer =3D NULL; > - if (PeFileBuffer !=3D NULL) { > - free (PeFileBuffer); > - PeFileBuffer =3D NULL; > - } > - > + > // > // Update Image Base Address > // > -- > 2.7.4