]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/PeCoffLoaderEx.c
Sync EDKII BaseTools to BaseTools project r1971
[mirror_edk2.git] / BaseTools / Source / C / Common / PeCoffLoaderEx.c
index fedc0941cb68699ee2ad685480da40d347473174..5d827cefe42d32727a08f3af645949d6def2572c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2004 - 2008, Intel Corporation. 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
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -315,3 +315,121 @@ PeCoffLoaderRelocateX64Image (
   return RETURN_SUCCESS;\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) != 0) ? 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
+  *Instruction = (*Instruction & ~0x040f) | Patch;\r
+\r
+  // Second 16-bit chunk of instruction\r
+  Patch  =  Address & 0x000000ff;          // imm8\r
+  Patch |=  ((Address << 4) & 0x00007000); // imm3\r
+  Instruction++;\r
+  *Instruction = (*Instruction & ~0x70ff) | Patch;\r
+}\r
+\r
+/**\r
+  Performs an ARM-based specific relocation fixup and is a no-op on other\r
+  instruction sets.\r
+\r
+  @param  Reloc       Pointer to the relocation record.\r
+  @param  Fixup       Pointer to the address to fix up.\r
+  @param  FixupData   Pointer to a buffer to log the fixups.\r
+  @param  Adjust      The offset to adjust the fixup.\r
+\r
+  @return Status code.\r
+\r
+**/\r
+RETURN_STATUS\r
+PeCoffLoaderRelocateArmImage (\r
+  IN UINT16      **Reloc,\r
+  IN OUT CHAR8   *Fixup,\r
+  IN OUT CHAR8   **FixupData,\r
+  IN UINT64      Adjust\r
+  )\r
+{\r
+  UINT16      *Fixup16;\r
+  UINT16      FixupVal;\r
+  UINT16      *Addend;\r
+\r
+  Fixup16    = (UINT16 *) Fixup;\r
+\r
+  switch ((**Reloc) >> 12) {\r
+  case EFI_IMAGE_REL_BASED_ARM_THUMB_MOVW:\r
+    FixupVal = ThumbMovtImmediateAddress (Fixup16) + (UINT16)Adjust;\r
+    ThumbMovtImmediatePatch (Fixup16, FixupVal);\r
+\r
+    if (*FixupData != NULL) {\r
+      *FixupData             = ALIGN_POINTER (*FixupData, sizeof (UINT16));\r
+      *(UINT16 *)*FixupData  = *Fixup16;\r
+      *FixupData             = *FixupData + sizeof (UINT16);\r
+    }\r
+    break;\r
+\r
+  case EFI_IMAGE_REL_BASED_ARM_THUMB_MOVT:\r
+    // For MOVT you need to know the lower 16-bits do do the math\r
+    // So this relocation entry is really two entries.\r
+    *Reloc = *Reloc + 1;\r
+    Addend = *Reloc; \r
+    FixupVal = (UINT16)(((ThumbMovtImmediateAddress (Fixup16) << 16) + Adjust + *Addend) >> 16);\r
+    ThumbMovtImmediatePatch (Fixup16, FixupVal);\r
+\r
+    if (*FixupData != NULL) {\r
+      *FixupData             = ALIGN_POINTER (*FixupData, sizeof (UINT16));\r
+      *(UINT16 *)*FixupData  = *Fixup16;\r
+      *FixupData             = *FixupData + sizeof (UINT16);\r
+    }\r
+    break;\r
+  \r
+  default:\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r