1 /** @file
2   Specific relocation fixups for ARM architecture.
3 
4   Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5   Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
6   Portions copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
7 
8   This program and the accompanying materials
9   are licensed and made available under the terms and conditions of the BSD License
10   which accompanies this distribution.  The full text of the license may be found at
11   http://opensource.org/licenses/bsd-license.php.
12 
13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 
16 **/
17 
18 #include "BasePeCoffLibInternals.h"
19 #include <Library/BaseLib.h>
20 
21 // Note: Currently only large memory model is supported by UEFI relocation code.
22 
23 /**
24   Performs an AARCH64-based specific relocation fixup and is a no-op on other
25   instruction sets.
26 
27   @param  Reloc       The pointer to the relocation record.
28   @param  Fixup       The pointer to the address to fix up.
29   @param  FixupData   The pointer to a buffer to log the fixups.
30   @param  Adjust      The offset to adjust the fixup.
31 
32   @return Status code.
33 
34 **/
35 RETURN_STATUS
PeCoffLoaderRelocateImageEx(IN UINT16 * Reloc,IN OUT CHAR8 * Fixup,IN OUT CHAR8 ** FixupData,IN UINT64 Adjust)36 PeCoffLoaderRelocateImageEx (
37   IN UINT16      *Reloc,
38   IN OUT CHAR8   *Fixup,
39   IN OUT CHAR8   **FixupData,
40   IN UINT64      Adjust
41   )
42 {
43   UINT64      *Fixup64;
44 
45   switch ((*Reloc) >> 12) {
46 
47     case EFI_IMAGE_REL_BASED_DIR64:
48       Fixup64 = (UINT64 *) Fixup;
49       *Fixup64 = *Fixup64 + (UINT64) Adjust;
50       if (*FixupData != NULL) {
51         *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));
52         *(UINT64 *)(*FixupData) = *Fixup64;
53         *FixupData = *FixupData + sizeof(UINT64);
54       }
55       break;
56 
57     default:
58       return RETURN_UNSUPPORTED;
59   }
60 
61   return RETURN_SUCCESS;
62 }
63 
64 /**
65   Returns TRUE if the machine type of PE/COFF image is supported. Supported
66   does not mean the image can be executed it means the PE/COFF loader supports
67   loading and relocating of the image type. It's up to the caller to support
68   the entry point.
69 
70   @param  Machine   Machine type from the PE Header.
71 
72   @return TRUE if this PE/COFF loader can load the image
73 
74 **/
75 BOOLEAN
PeCoffLoaderImageFormatSupported(IN UINT16 Machine)76 PeCoffLoaderImageFormatSupported (
77   IN  UINT16  Machine
78   )
79 {
80   if ((Machine == IMAGE_FILE_MACHINE_ARM64) || (Machine ==  IMAGE_FILE_MACHINE_EBC)) {
81     return TRUE;
82   }
83 
84   return FALSE;
85 }
86 
87 /**
88   Performs an ARM-based specific re-relocation fixup and is a no-op on other
89   instruction sets. This is used to re-relocated the image into the EFI virtual
90   space for runtime calls.
91 
92   @param  Reloc       The pointer to the relocation record.
93   @param  Fixup       The pointer to the address to fix up.
94   @param  FixupData   The pointer to a buffer to log the fixups.
95   @param  Adjust      The offset to adjust the fixup.
96 
97   @return Status code.
98 
99 **/
100 RETURN_STATUS
PeHotRelocateImageEx(IN UINT16 * Reloc,IN OUT CHAR8 * Fixup,IN OUT CHAR8 ** FixupData,IN UINT64 Adjust)101 PeHotRelocateImageEx (
102   IN UINT16      *Reloc,
103   IN OUT CHAR8   *Fixup,
104   IN OUT CHAR8   **FixupData,
105   IN UINT64      Adjust
106   )
107 {
108   UINT64  *Fixup64;
109 
110   switch ((*Reloc) >> 12) {
111   case EFI_IMAGE_REL_BASED_DIR64:
112     Fixup64     = (UINT64 *) Fixup;
113     *FixupData  = ALIGN_POINTER (*FixupData, sizeof (UINT64));
114     if (*(UINT64 *) (*FixupData) == *Fixup64) {
115       *Fixup64 = *Fixup64 + (UINT64) Adjust;
116     }
117 
118     *FixupData = *FixupData + sizeof (UINT64);
119     break;
120 
121   default:
122     DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n"));
123     return RETURN_UNSUPPORTED;
124   }
125 
126   return RETURN_SUCCESS;
127 }
128