]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Add support for ARM MOVW/MOVT instructions that were added in the latest PE/COFF...
authorandrewfish <andrewfish@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 16 Dec 2010 21:44:49 +0000 (21:44 +0000)
committerandrewfish <andrewfish@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 16 Dec 2010 21:44:49 +0000 (21:44 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11176 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Library/BasePeCoffLib/Arm/PeCoffLoaderEx.c

index b47caf1ad6a39c422d788ff9806a69409ffb2db9..2fb408e680a283e9546dd211041b8da4cb9f4553 100644 (file)
@@ -2,7 +2,7 @@
   Specific relocation fixups for ARM architecture.\r
 \r
   Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
-  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
+  Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 **/\r
 \r
 #include "BasePeCoffLibInternals.h"\r
+#include <Library/BaseLib.h>\r
 \r
 \r
+/**\r
+  Pass in a pointer to an ARM MOVT or MOVW immediate instruciton and \r
+  return the immediate data encoded in the instruction\r
+\r
+  @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction\r
+\r
+  @return Immediate address encoded in the instruction\r
+\r
+**/\r
+UINT16\r
+ThumbMovtImmediateAddress (\r
+  IN UINT16 *Instruction\r
+  )\r
+{\r
+  UINT32  Movt;\r
+  UINT16  Address;\r
+  \r
+  // Thumb2 is two 16-bit instructions working together. Not a single 32-bit instruction\r
+  // Example MOVT R0, #0 is 0x0000f2c0 or 0xf2c0 0x0000\r
+  Movt = (*Instruction << 16) | (*(Instruction + 1)); \r
+\r
+  // imm16 = imm4:i:imm3:imm8\r
+  //         imm4 -> Bit19:Bit16\r
+  //         i    -> Bit26\r
+  //         imm3 -> Bit14:Bit12\r
+  //         imm8 -> Bit7:Bit0\r
+  Address  = (UINT16)(Movt & 0x000000ff);         // imm8\r
+  Address |= (UINT16)((Movt >> 4) &  0x0000f700); // imm4 imm3\r
+  Address |= ((Movt & BIT26) ? BIT11 : 0);        // i\r
+  return Address;\r
+}\r
+\r
+\r
+/**\r
+  Update an ARM MOVT or MOVW immediate instruction immediate data.\r
+\r
+  @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction\r
+  @param  Address       New addres to patch into the instruction\r
+**/\r
+VOID\r
+ThumbMovtImmediatePatch (\r
+  IN OUT UINT16 *Instruction,\r
+  IN     UINT16 Address\r
+  )\r
+{\r
+  UINT16  Patch;\r
+\r
+  // First 16-bit chunk of instruciton\r
+  Patch  = ((Address >> 12) & 0x000f);            // imm4 \r
+  Patch |= (((Address & BIT11) != 0) ? BIT10 : 0); // i\r
+  // Mask out instruction bits and or in address\r
+  *(Instruction) = (*Instruction & ~0x040f) | Patch;\r
+\r
+  // Second 16-bit chunk of instruction\r
+  Patch  =  Address & 0x000000ff;          // imm8\r
+  Patch |=  ((Address << 4) & 0x00007000); // imm3\r
+  // Mask out instruction bits and or in address\r
+  Instruction++;\r
+  *Instruction = (*Instruction & ~0x70ff) | Patch;\r
+}\r
+\r
+\r
+\r
+/**\r
+  Pass in a pointer to an ARM MOVW/MOVT instruciton pair and \r
+  return the immediate data encoded in the two` instruction\r
+\r
+  @param  Instructions  Pointer to ARM MOVW/MOVT insturction pair\r
+\r
+  @return Immediate address encoded in the instructions\r
+\r
+**/\r
+UINT32\r
+ThumbMovwMovtImmediateAddress (\r
+  IN UINT16 *Instructions\r
+  )\r
+{\r
+  UINT16  *Word;\r
+  UINT16  *Top;\r
+  \r
+  Word = Instructions;  // MOVW\r
+  Top  = Word + 2;      // MOVT\r
+  \r
+  return (ThumbMovtImmediateAddress (Top) << 16) + ThumbMovtImmediateAddress (Word);\r
+}\r
+\r
+\r
+/**\r
+  Update an ARM MOVW/MOVT immediate instruction instruction pair.\r
+\r
+  @param  Instructions  Pointer to ARM MOVW/MOVT instruction pair\r
+  @param  Address       New addres to patch into the instructions\r
+**/\r
+VOID\r
+ThumbMovwMovtImmediatePatch (\r
+  IN OUT UINT16 *Instructions,\r
+  IN     UINT32 Address\r
+  )\r
+{\r
+  UINT16  *Word;\r
+  UINT16  *Top;\r
+  \r
+  Word = Instructions;  // MOVW\r
+  Top  = Word + 2;      // MOVT\r
+\r
+  ThumbMovtImmediatePatch (Word, (UINT16)(Address & 0xffff));\r
+  ThumbMovtImmediatePatch (Top, (UINT16)(Address >> 16));\r
+}\r
+  \r
+  \r
+\r
 /**\r
   Performs an ARM-based specific relocation fixup and is a no-op on other\r
   instruction sets.\r
@@ -36,7 +148,33 @@ PeCoffLoaderRelocateImageEx (
   IN UINT64      Adjust\r
   )\r
 {\r
-  return RETURN_UNSUPPORTED;\r
+  UINT16      *Fixup16;\r
+  UINT32      FixupVal;\r
+\r
+  Fixup16   = (UINT16 *) Fixup;\r
+\r
+  switch ((*Reloc) >> 12) {\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32T:\r
+    FixupVal = ThumbMovwMovtImmediateAddress (Fixup16) + (UINT32)Adjust;\r
+    ThumbMovwMovtImmediatePatch (Fixup16, FixupVal);\r
+\r
+    if (*FixupData != NULL) {\r
+      *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));\r
+      // Fixup16 is not aligned so we must copy it. Thumb instructions are streams of 16 bytes. \r
+      CopyMem (*FixupData, Fixup16, sizeof (UINT64));\r
+      *FixupData = *FixupData + sizeof(UINT64);\r
+    }\r
+    break;\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32A:\r
+     ASSERT (FALSE);\r
+     // break omitted - ARM instruction encoding not implemented\r
+  default:\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -45,8 +183,6 @@ PeCoffLoaderRelocateImageEx (
   loading and relocating of the image type. It's up to the caller to support\r
   the entry point.\r
   \r
-  The IA32/X64 version PE/COFF loader/relocater both support IA32, X64 and EBC images.\r
-\r
   @param  Machine   Machine type from the PE Header.\r
 \r
   @return TRUE if this PE/COFF loader can load the image\r
@@ -85,6 +221,29 @@ PeHotRelocateImageEx (
   IN UINT64      Adjust\r
   )\r
 {\r
-  return RETURN_UNSUPPORTED;\r
+  UINT16  *Fixup16;\r
+  UINT32  FixupVal;\r
+\r
+  Fixup16 = (UINT16 *)Fixup;\r
+\r
+  switch ((*Reloc) >> 12) {\r
+\r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32T:\r
+    *FixupData  = ALIGN_POINTER (*FixupData, sizeof (UINT64));\r
+    if (*(UINT64 *) (*FixupData) == ReadUnaligned64 ((UINT64 *)Fixup16)) {\r
+      FixupVal = ThumbMovwMovtImmediateAddress (Fixup16) + (UINT32)Adjust;\r
+      ThumbMovwMovtImmediatePatch (Fixup16, FixupVal);\r
+    }\r
+    break;\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32A:\r
+    ASSERT (FALSE);\r
+    // break omitted - ARM instruction encoding not implemented\r
+  default:\r
+    DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n"));\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+  \r
+  return RETURN_SUCCESS;\r
 }\r
 \r