PeiEmuPeCoffGetEntryPointLib.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*++ @file
  2. Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PiPei.h"
  7. #include <Library/PeCoffGetEntryPointLib.h>
  8. #include <Library/PeiServicesLib.h>
  9. #include <IndustryStandard/PeImage.h>
  10. #include <Library/DebugLib.h>
  11. #include <Ppi/EmuThunk.h>
  12. #include <Protocol/EmuThunk.h>
  13. /**
  14. Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
  15. into system memory with the PE/COFF Loader Library functions.
  16. Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
  17. point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
  18. return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
  19. If Pe32Data is NULL, then ASSERT().
  20. If EntryPoint is NULL, then ASSERT().
  21. @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.
  22. @param EntryPoint The pointer to entry point to the PE/COFF image to return.
  23. @retval RETURN_SUCCESS EntryPoint was returned.
  24. @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
  25. **/
  26. RETURN_STATUS
  27. EFIAPI
  28. PeCoffLoaderGetEntryPoint (
  29. IN VOID *Pe32Data,
  30. IN OUT VOID **EntryPoint
  31. )
  32. {
  33. EMU_THUNK_PPI *ThunkPpi;
  34. EFI_STATUS Status;
  35. EMU_THUNK_PROTOCOL *Thunk;
  36. //
  37. // Locate EmuThunkPpi for retrieving standard output handle
  38. //
  39. Status = PeiServicesLocatePpi (
  40. &gEmuThunkPpiGuid,
  41. 0,
  42. NULL,
  43. (VOID **)&ThunkPpi
  44. );
  45. ASSERT_EFI_ERROR (Status);
  46. Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
  47. return Thunk->PeCoffGetEntryPoint (Pe32Data, EntryPoint);
  48. }
  49. /**
  50. Returns the machine type of PE/COFF image.
  51. This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.
  52. The reason is Emu package needs to load the image to memory to support source
  53. level debug.
  54. @param Pe32Data Pointer to a PE/COFF header
  55. @return Machine type or zero if not a valid iamge
  56. **/
  57. UINT16
  58. EFIAPI
  59. PeCoffLoaderGetMachineType (
  60. IN VOID *Pe32Data
  61. )
  62. {
  63. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
  64. EFI_IMAGE_DOS_HEADER *DosHdr;
  65. ASSERT (Pe32Data != NULL);
  66. DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
  67. if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
  68. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
  69. } else {
  70. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data);
  71. }
  72. if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
  73. return Hdr.Te->Machine;
  74. } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
  75. return Hdr.Pe32->FileHeader.Machine;
  76. }
  77. return 0x0000;
  78. }
  79. /**
  80. Returns a pointer to the PDB file name for a PE/COFF image that has been
  81. loaded into system memory with the PE/COFF Loader Library functions.
  82. Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
  83. the PE/COFF image specified by Pe32Data is not a valid, then NULL is
  84. returned. If the PE/COFF image specified by Pe32Data does not contain a
  85. debug directory entry, then NULL is returned. If the debug directory entry
  86. in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
  87. then NULL is returned.
  88. If Pe32Data is NULL, then ASSERT().
  89. @param Pe32Data Pointer to the PE/COFF image that is loaded in system
  90. memory.
  91. @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
  92. if it cannot be retrieved.
  93. **/
  94. VOID *
  95. EFIAPI
  96. PeCoffLoaderGetPdbPointer (
  97. IN VOID *Pe32Data
  98. )
  99. {
  100. EFI_IMAGE_DOS_HEADER *DosHdr;
  101. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
  102. EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
  103. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
  104. UINTN DirCount;
  105. VOID *CodeViewEntryPointer;
  106. INTN TEImageAdjust;
  107. UINT32 NumberOfRvaAndSizes;
  108. UINT16 Magic;
  109. ASSERT (Pe32Data != NULL);
  110. TEImageAdjust = 0;
  111. DirectoryEntry = NULL;
  112. DebugEntry = NULL;
  113. NumberOfRvaAndSizes = 0;
  114. DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
  115. if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
  116. //
  117. // DOS image header is present, so read the PE header after the DOS image header.
  118. //
  119. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
  120. } else {
  121. //
  122. // DOS image header is not present, so PE header is at the image base.
  123. //
  124. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
  125. }
  126. if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
  127. if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {
  128. DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];
  129. TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;
  130. DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN)Hdr.Te +
  131. Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +
  132. TEImageAdjust);
  133. }
  134. } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
  135. //
  136. // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
  137. // It is due to backward-compatibility, for some system might
  138. // generate PE32+ image with PE32 Magic.
  139. //
  140. switch (Hdr.Pe32->FileHeader.Machine) {
  141. case EFI_IMAGE_MACHINE_IA32:
  142. //
  143. // Assume PE32 image with IA32 Machine field.
  144. //
  145. Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
  146. break;
  147. case EFI_IMAGE_MACHINE_X64:
  148. case EFI_IMAGE_MACHINE_IA64:
  149. //
  150. // Assume PE32+ image with X64 or IA64 Machine field
  151. //
  152. Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
  153. break;
  154. default:
  155. //
  156. // For unknow Machine field, use Magic in optional Header
  157. //
  158. Magic = Hdr.Pe32->OptionalHeader.Magic;
  159. }
  160. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  161. //
  162. // Use PE32 offset get Debug Directory Entry
  163. //
  164. NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
  165. DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  166. DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN)Pe32Data + DirectoryEntry->VirtualAddress);
  167. } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  168. //
  169. // Use PE32+ offset get Debug Directory Entry
  170. //
  171. NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
  172. DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
  173. DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN)Pe32Data + DirectoryEntry->VirtualAddress);
  174. }
  175. if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
  176. DirectoryEntry = NULL;
  177. DebugEntry = NULL;
  178. }
  179. } else {
  180. return NULL;
  181. }
  182. if ((DebugEntry == NULL) || (DirectoryEntry == NULL)) {
  183. return NULL;
  184. }
  185. for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {
  186. if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
  187. if (DebugEntry->SizeOfData > 0) {
  188. CodeViewEntryPointer = (VOID *)((UINTN)DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);
  189. switch (*(UINT32 *)CodeViewEntryPointer) {
  190. case CODEVIEW_SIGNATURE_NB10:
  191. return (VOID *)((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));
  192. case CODEVIEW_SIGNATURE_RSDS:
  193. return (VOID *)((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));
  194. case CODEVIEW_SIGNATURE_MTOC:
  195. return (VOID *)((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));
  196. default:
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. return NULL;
  203. }
  204. /**
  205. Returns the size of the PE/COFF headers
  206. Returns the size of the PE/COFF header specified by Pe32Data.
  207. If Pe32Data is NULL, then ASSERT().
  208. @param Pe32Data Pointer to the PE/COFF image that is loaded in system
  209. memory.
  210. @return Size of PE/COFF header in bytes or zero if not a valid image.
  211. **/
  212. UINT32
  213. EFIAPI
  214. PeCoffGetSizeOfHeaders (
  215. IN VOID *Pe32Data
  216. )
  217. {
  218. EFI_IMAGE_DOS_HEADER *DosHdr;
  219. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
  220. UINTN SizeOfHeaders;
  221. ASSERT (Pe32Data != NULL);
  222. DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
  223. if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
  224. //
  225. // DOS image header is present, so read the PE header after the DOS image header.
  226. //
  227. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
  228. } else {
  229. //
  230. // DOS image header is not present, so PE header is at the image base.
  231. //
  232. Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
  233. }
  234. if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
  235. SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;
  236. } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
  237. SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;
  238. } else {
  239. SizeOfHeaders = 0;
  240. }
  241. return (UINT32)SizeOfHeaders;
  242. }