]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmVirtPkg/PrePi: use standard PeCoff routines for self-relocation
authorArd Biesheuvel <ard.biesheuvel@arm.com>
Mon, 8 Jun 2020 11:07:54 +0000 (13:07 +0200)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 12 Jun 2020 22:17:46 +0000 (22:17 +0000)
Instead of having a GCC specific routine to perform self-relocation
based on ELF metadata, use the PE/COFF metadata and the existing
PeCoff library routines. This reduces the amount of bespoke assembler
code that is a burden to maintain, and is not portable across the set
of toolchains we support.

This does require some special care, as we have no control over how
the C code references global symbols, so we need to emit these
references from the calling assembler code. Otherwise, they may be
emitted as absolute references, in which case they need to be fixed
up themselves, leading to a circular dependency.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
Acked-by: Jiewen Yao <Jiewen.yao@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Sami Mujawar <Sami.Mujawar@arm.com>
ArmVirtPkg/ArmVirtQemuKernel.dsc
ArmVirtPkg/ArmVirtXen.dsc
ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S
ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S
ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf
ArmVirtPkg/PrePi/PrePi.c

index 2a6fd6bc06be1cc20d8c6f2bf00d88d593061edf..9449a01d6e40dc96644b2d53047809d0dff2fd93 100644 (file)
 [LibraryClasses.common.UEFI_DRIVER]\r
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf\r
 \r
-[BuildOptions.common.EDKII.SEC, BuildOptions.common.EDKII.BASE]\r
+[BuildOptions]\r
   #\r
-  # CLANG38 with LTO support enabled uses the GNU GOLD linker, which insists\r
-  # on emitting GOT based symbol references when running in shared mode, unless\r
-  # we override visibility to 'hidden' in all modules that make up the PrePi\r
-  # build.\r
+  # We need to avoid jump tables in SEC modules, so that the PE/COFF\r
+  # self-relocation code itself is guaranteed to be position independent.\r
   #\r
-  GCC:*_CLANG38_*_CC_FLAGS = -include $(WORKSPACE)/ArmVirtPkg/Include/Platform/Hidden.h\r
+  GCC:*_*_*_CC_FLAGS = -fno-jump-tables\r
 \r
 ################################################################################\r
 #\r
index 8a489b253684a110657b4ddc27a6bc1c3b60e523..278f5d3828cec082d5ac395000751e74425677f2 100644 (file)
 [LibraryClasses.common.UEFI_DRIVER]\r
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf\r
 \r
-[BuildOptions.common.EDKII.SEC, BuildOptions.common.EDKII.BASE]\r
+[BuildOptions]\r
   #\r
-  # CLANG38 with LTO support enabled uses the GNU GOLD linker, which insists\r
-  # on emitting GOT based symbol references when running in shared mode, unless\r
-  # we override visibility to 'hidden' in all modules that make up the PrePi\r
-  # build.\r
+  # We need to avoid jump tables in SEC modules, so that the PE/COFF\r
+  # self-relocation code itself is guaranteed to be position independent.\r
   #\r
-  GCC:*_CLANG38_*_CC_FLAGS = -include $(WORKSPACE)/ArmVirtPkg/Include/Platform/Hidden.h\r
+  GCC:*_*_*_CC_FLAGS = -fno-jump-tables\r
 \r
 ################################################################################\r
 #\r
index 34d6abecb0ac40d260464d36a0ea2e64a0bd4f12..01623b6b3591242778a5c76df5d401b1ce71834f 100644 (file)
@@ -9,40 +9,6 @@
 #include <AsmMacroIoLibV8.h>\r
 \r
 ASM_FUNC(_ModuleEntryPoint)\r
-  //\r
-  // We are built as a ET_DYN PIE executable, so we need to process all\r
-  // relative relocations regardless of whether or not we are executing from\r
-  // the same offset we were linked at. This is only possible if we are\r
-  // running from RAM.\r
-  //\r
-  adr   x8, __reloc_base\r
-  adr   x9, __reloc_start\r
-  adr   x10, __reloc_end\r
-\r
-.Lreloc_loop:\r
-  cmp   x9, x10\r
-  bhs   .Lreloc_done\r
-\r
-  //\r
-  // AArch64 uses the ELF64 RELA format, which means each entry in the\r
-  // relocation table consists of\r
-  //\r
-  //   UINT64 offset          : the relative offset of the value that needs to\r
-  //                            be relocated\r
-  //   UINT64 info            : relocation type and symbol index (the latter is\r
-  //                            not used for R_AARCH64_RELATIVE relocations)\r
-  //   UINT64 addend          : value to be added to the value being relocated\r
-  //\r
-  ldp   x11, x12, [x9], #24   // read offset into x11 and info into x12\r
-  cmp   x12, #0x403           // check info == R_AARCH64_RELATIVE?\r
-  bne   .Lreloc_loop          // not a relative relocation? then skip\r
-\r
-  ldr   x12, [x9, #-8]        // read addend into x12\r
-  add   x12, x12, x8          // add reloc base to addend to get relocated value\r
-  str   x12, [x11, x8]        // write relocated value at offset\r
-  b     .Lreloc_loop\r
-.Lreloc_done:\r
-\r
   bl    ASM_PFX(DiscoverDramFromDt)\r
 \r
   // Get ID of this CPU in Multicore system\r
@@ -170,15 +136,24 @@ ASM_PFX(DiscoverDramFromDt):
   str   x1, [x8]\r
   str   x7, [x9]\r
 \r
+  //\r
+  // The runtime address may be different from the link time address so fix\r
+  // up the PE/COFF relocations. Since we are calling a C function, use the\r
+  // window at the beginning of the FD image as a temp stack.\r
+  //\r
+  mov   x0, x7\r
+  adr   x1, PeCoffLoaderImageReadFromMemory\r
+  mov   sp, x7\r
+  bl    RelocatePeCoffImage\r
+\r
   //\r
   // Discover the memory size and offset from the DTB, and record in the\r
   // respective PCDs. This will also return false if a corrupt DTB is\r
-  // encountered. Since we are calling a C function, use the window at the\r
-  // beginning of the FD image as a temp stack.\r
+  // encountered.\r
   //\r
+  mov   x0, x28\r
   adr   x1, PcdGet64 (PcdSystemMemoryBase)\r
   adr   x2, PcdGet64 (PcdSystemMemorySize)\r
-  mov   sp, x7\r
   bl    FindMemnode\r
   cbz   x0, .Lout\r
 \r
index 72d756fdb571add368e52cc56bf25e857f5409f6..f0536c65eb52f78e9c21d600731c22795d498029 100644 (file)
@@ -9,38 +9,6 @@
 #include <AsmMacroIoLib.h>\r
 \r
 ASM_FUNC(_ModuleEntryPoint)\r
-  //\r
-  // We are built as a ET_DYN PIE executable, so we need to process all\r
-  // relative relocations if we are executing from a different offset than we\r
-  // were linked at. This is only possible if we are running from RAM.\r
-  //\r
-  ADRL  (r4, __reloc_base)\r
-  ADRL  (r5, __reloc_start)\r
-  ADRL  (r6, __reloc_end)\r
-\r
-.Lreloc_loop:\r
-  cmp   r5, r6\r
-  bhs   .Lreloc_done\r
-\r
-  //\r
-  // AArch32 uses the ELF32 REL format, which means each entry in the\r
-  // relocation table consists of\r
-  //\r
-  //   UINT32 offset          : the relative offset of the value that needs to\r
-  //                            be relocated\r
-  //   UINT32 info            : relocation type and symbol index (the latter is\r
-  //                            not used for R_ARM_RELATIVE relocations)\r
-  //\r
-  ldrd  r8, r9, [r5], #8      // read offset into r8 and info into r9\r
-  cmp   r9, #23               // check info == R_ARM_RELATIVE?\r
-  bne   .Lreloc_loop          // not a relative relocation? then skip\r
-\r
-  ldr   r9, [r8, r4]          // read addend into r9\r
-  add   r9, r9, r1            // add image base to addend to get relocated value\r
-  str   r9, [r8, r4]          // write relocated value at offset\r
-  b     .Lreloc_loop\r
-.Lreloc_done:\r
-\r
   // Do early platform specific actions\r
   bl    ASM_PFX(ArmPlatformPeiBootAction)\r
 \r
@@ -172,15 +140,24 @@ ASM_PFX(ArmPlatformPeiBootAction):
   str   r1, [r8]\r
   str   r5, [r7]\r
 \r
+  //\r
+  // The runtime address may be different from the link time address so fix\r
+  // up the PE/COFF relocations. Since we are calling a C function, use the\r
+  // window at the beginning of the FD image as a temp stack.\r
+  //\r
+  mov   r0, r5\r
+  ADRL  (r1, PeCoffLoaderImageReadFromMemory)\r
+  mov   sp, r5\r
+  bl    RelocatePeCoffImage\r
+\r
   //\r
   // Discover the memory size and offset from the DTB, and record in the\r
   // respective PCDs. This will also return false if a corrupt DTB is\r
-  // encountered. Since we are calling a C function, use the window at the\r
-  // beginning of the FD image as a temp stack.\r
+  // encountered.\r
   //\r
+  mov   r0, r10\r
   ADRL  (r1, PcdGet64 (PcdSystemMemoryBase))\r
   ADRL  (r2, PcdGet64 (PcdSystemMemorySize))\r
-  mov   sp, r5\r
   bl    FindMemnode\r
   teq   r0, #0\r
   beq   .Lout\r
index 9e58e56fce092439fc40aa5e52218851cc27ec6b..7edf5018089d0710564159a06e3f50b8890d2795 100755 (executable)
@@ -46,6 +46,7 @@
   SerialPortLib\r
   ExtractGuidedSectionLib\r
   LzmaDecompressLib\r
+  PeCoffLib\r
   PrePiLib\r
   MemoryAllocationLib\r
   HobLib\r
@@ -95,6 +96,3 @@
   gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress\r
   gArmTokenSpaceGuid.PcdFdBaseAddress\r
   gArmTokenSpaceGuid.PcdFvBaseAddress\r
-\r
-[BuildOptions]\r
-  GCC:*_*_*_DLINK_FLAGS = -Wl,-Bsymbolic,-pie,-T,$(MODULE_DIR)/Scripts/PrePi-PIE.lds\r
index 72e35028c5bb96e5cc899bd1f4309c61d2b5db7d..4f0c3f98bad63d0682cccd76a0a062d1fd4f46ab 100755 (executable)
@@ -9,6 +9,7 @@
 #include <PiPei.h>\r
 #include <Pi/PiBootMode.h>\r
 \r
+#include <Library/PeCoffLib.h>\r
 #include <Library/PrePiLib.h>\r
 #include <Library/PrintLib.h>\r
 #include <Library/PrePiHobListPointerLib.h>\r
@@ -128,3 +129,37 @@ CEntryPoint (
   // DXE Core should always load and never return\r
   ASSERT (FALSE);\r
 }\r
+\r
+VOID\r
+RelocatePeCoffImage (\r
+  IN  EFI_PEI_FV_HANDLE             FwVolHeader,\r
+  IN  PE_COFF_LOADER_READ_FILE      ImageRead\r
+  )\r
+{\r
+  EFI_PEI_FILE_HANDLE           FileHandle;\r
+  VOID                          *SectionData;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;\r
+  EFI_STATUS                    Status;\r
+\r
+  FileHandle = NULL;\r
+  Status = FfsFindNextFile (EFI_FV_FILETYPE_SECURITY_CORE, FwVolHeader,\r
+             &FileHandle);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SectionData);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = FfsFindSectionData (EFI_SECTION_TE, FileHandle, &SectionData);\r
+  }\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ZeroMem (&ImageContext, sizeof ImageContext);\r
+\r
+  ImageContext.Handle       = (EFI_HANDLE)SectionData;\r
+  ImageContext.ImageRead    = ImageRead;\r
+  PeCoffLoaderGetImageInfo (&ImageContext);\r
+\r
+  if (ImageContext.ImageAddress != (UINTN)SectionData) {\r
+    ImageContext.ImageAddress = (UINTN)SectionData;\r
+    PeCoffLoaderRelocateImage (&ImageContext);\r
+  }\r
+}\r