PeCoffLib.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /** @file
  2. Function prototypes and defines on Memory Only PE COFF loader
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. This program and the accompanying materials
  5. are licensed and made available under the terms and conditions of the BSD License
  6. which accompanies this distribution. The full text of the license may be found at
  7. http://opensource.org/licenses/bsd-license.php
  8. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #ifndef __BASE_PE_COFF_LIB_H__
  12. #define __BASE_PE_COFF_LIB_H__
  13. //
  14. // Return status codes from the PE/COFF Loader services
  15. // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
  16. //
  17. #define IMAGE_ERROR_SUCCESS 0
  18. #define IMAGE_ERROR_IMAGE_READ 1
  19. #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
  20. #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
  21. #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
  22. #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
  23. #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
  24. #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
  25. #define IMAGE_ERROR_SECTION_NOT_LOADED 8
  26. #define IMAGE_ERROR_FAILED_RELOCATION 9
  27. #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
  28. //
  29. // PE/COFF Loader Read Function passed in by caller
  30. //
  31. typedef
  32. RETURN_STATUS
  33. (EFIAPI *PE_COFF_LOADER_READ_FILE) (
  34. IN VOID *FileHandle,
  35. IN UINTN FileOffset,
  36. IN OUT UINTN *ReadSize,
  37. OUT VOID *Buffer
  38. );
  39. //
  40. // Context structure used while PE/COFF image is being loaded and relocated
  41. //
  42. typedef struct {
  43. PHYSICAL_ADDRESS ImageAddress;
  44. UINT64 ImageSize;
  45. PHYSICAL_ADDRESS DestinationAddress;
  46. PHYSICAL_ADDRESS EntryPoint;
  47. PE_COFF_LOADER_READ_FILE ImageRead;
  48. VOID *Handle;
  49. VOID *FixupData;
  50. UINT32 SectionAlignment;
  51. UINT32 PeCoffHeaderOffset;
  52. UINT32 DebugDirectoryEntryRva;
  53. VOID *CodeView;
  54. CHAR8 *PdbPointer;
  55. UINTN SizeOfHeaders;
  56. UINT32 ImageCodeMemoryType;
  57. UINT32 ImageDataMemoryType;
  58. UINT32 ImageError;
  59. UINTN FixupDataSize;
  60. UINT16 Machine;
  61. UINT16 ImageType;
  62. BOOLEAN RelocationsStripped;
  63. BOOLEAN IsTeImage;
  64. } PE_COFF_LOADER_IMAGE_CONTEXT;
  65. /**
  66. Retrieves information on a PE/COFF image
  67. @param ImageContext The context of the image being loaded
  68. @retval EFI_SUCCESS The information on the PE/COFF image was collected.
  69. @retval EFI_INVALID_PARAMETER ImageContext is NULL.
  70. @retval EFI_UNSUPPORTED The PE/COFF image is not supported.
  71. @retval Otherwise The error status from reading the PE/COFF image using the
  72. ImageContext->ImageRead() function
  73. **/
  74. RETURN_STATUS
  75. EFIAPI
  76. PeCoffLoaderGetImageInfo (
  77. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  78. )
  79. ;
  80. /**
  81. Relocates a PE/COFF image in memory
  82. @param ImageContext Contains information on the loaded image to relocate
  83. @retval EFI_SUCCESS if the PE/COFF image was relocated
  84. @retval EFI_LOAD_ERROR if the image is not a valid PE/COFF image
  85. @retval EFI_UNSUPPORTED not support
  86. **/
  87. RETURN_STATUS
  88. EFIAPI
  89. PeCoffLoaderRelocateImage (
  90. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  91. )
  92. ;
  93. /**
  94. Loads a PE/COFF image into memory
  95. @param ImageContext Contains information on image to load into memory
  96. @retval EFI_SUCCESS if the PE/COFF image was loaded
  97. @retval EFI_BUFFER_TOO_SMALL if the caller did not provide a large enough buffer
  98. @retval EFI_LOAD_ERROR if the image is a runtime driver with no relocations
  99. @retval EFI_INVALID_PARAMETER if the image address is invalid
  100. **/
  101. RETURN_STATUS
  102. EFIAPI
  103. PeCoffLoaderLoadImage (
  104. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  105. )
  106. ;
  107. VOID *
  108. EFIAPI
  109. PeCoffLoaderGetPdbPointer (
  110. IN VOID *Pe32Data
  111. )
  112. ;
  113. RETURN_STATUS
  114. EFIAPI
  115. PeCoffLoaderGetEntryPoint (
  116. IN VOID *Pe32Data,
  117. OUT VOID **EntryPoint,
  118. OUT VOID **BaseOfImage
  119. )
  120. ;
  121. //
  122. // These functions are used by the ARM PE/COFF relocation code and by
  123. // the ELF to PE/COFF converter so that is why they are public
  124. //
  125. /**
  126. Pass in a pointer to an ARM MOVT or MOVW immediate instruction and
  127. return the immediate data encoded in the instruction
  128. @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
  129. @return Immediate address encoded in the instruction
  130. **/
  131. UINT16
  132. EFIAPI
  133. ThumbMovtImmediateAddress (
  134. IN UINT16 *Instruction
  135. );
  136. /**
  137. Update an ARM MOVT or MOVW immediate instruction immediate data.
  138. @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
  139. @param Address New address to patch into the instruction
  140. **/
  141. VOID
  142. EFIAPI
  143. ThumbMovtImmediatePatch (
  144. IN OUT UINT16 *Instruction,
  145. IN UINT16 Address
  146. );
  147. /**
  148. Pass in a pointer to an ARM MOVW/MOVT instruction pair and
  149. return the immediate data encoded in the two` instruction
  150. @param Instructions Pointer to ARM MOVW/MOVT instruction pair
  151. @return Immediate address encoded in the instructions
  152. **/
  153. UINT32
  154. EFIAPI
  155. ThumbMovwMovtImmediateAddress (
  156. IN UINT16 *Instructions
  157. );
  158. /**
  159. Update an ARM MOVW/MOVT immediate instruction instruction pair.
  160. @param Instructions Pointer to ARM MOVW/MOVT instruction pair
  161. @param Address New address to patch into the instructions
  162. **/
  163. VOID
  164. EFIAPI
  165. ThumbMovwMovtImmediatePatch (
  166. IN OUT UINT16 *Instructions,
  167. IN UINT32 Address
  168. );
  169. #endif