PeCoffLib.h 5.6 KB

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