]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/BasePeCoff.c
Sync EDKII BaseTools to BaseTools project r1913.
[mirror_edk2.git] / BaseTools / Source / C / Common / BasePeCoff.c
index 72b6a52f890c162744b9f4bbbaf527598774c083..5ffb5d45032468dffdcfa96c73e928d5784bd6c5 100644 (file)
@@ -856,6 +856,7 @@ Returns:
 \r
   PeHdr = NULL;\r
   TeHdr = NULL;\r
+  OptionHeader.Header = NULL;\r
   //\r
   // Assume success\r
   //\r
@@ -1172,6 +1173,9 @@ Returns:
           ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);\r
           break;\r
 \r
+        case CODEVIEW_SIGNATURE_MTOC:\r
+          ImageContext->PdbPointer = (CHAR8 *) ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY);\r
+\r
         default:\r
           break;\r
         }\r
@@ -1252,11 +1256,11 @@ PeCoffLoaderGetPdbPointer (
       //\r
       // Get the DebugEntry offset in the raw data image.\r
       //\r
-           SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (Hdr.Te + 1);\r
-           Index = Hdr.Te->NumberOfSections;\r
+      SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (Hdr.Te + 1);\r
+      Index = Hdr.Te->NumberOfSections;\r
       for (Index1 = 0; Index1 < Index; Index1 ++) {\r
-       if ((DirectoryEntry->VirtualAddress >= SectionHeader[Index1].VirtualAddress) && \r
-                (DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
+        if ((DirectoryEntry->VirtualAddress >= SectionHeader[Index1].VirtualAddress) && \r
+           (DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
           DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r
                         DirectoryEntry->VirtualAddress - \r
                         SectionHeader [Index1].VirtualAddress + \r
@@ -1324,8 +1328,8 @@ PeCoffLoaderGetPdbPointer (
       // Get the DebugEntry offset in the raw data image.\r
       //\r
       for (Index1 = 0; Index1 < Index; Index1 ++) {\r
-       if ((DirectoryEntry->VirtualAddress >= SectionHeader[Index1].VirtualAddress) && \r
-                (DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
+        if ((DirectoryEntry->VirtualAddress >= SectionHeader[Index1].VirtualAddress) && \r
+           (DirectoryEntry->VirtualAddress < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
           DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) (\r
                        (UINTN) Pe32Data + \r
                        DirectoryEntry->VirtualAddress - \r
@@ -1349,32 +1353,35 @@ PeCoffLoaderGetPdbPointer (
   for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {\r
     if (EFI_IMAGE_DEBUG_TYPE_CODEVIEW == DebugEntry->Type) {\r
       if (DebugEntry->SizeOfData > 0) {\r
-             //\r
-             // Get the DebugEntry offset in the raw data image.\r
-             //\r
-             for (Index1 = 0; Index1 < Index; Index1 ++) {\r
-               if ((DebugEntry->RVA >= SectionHeader[Index1].VirtualAddress) && \r
-                        (DebugEntry->RVA < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
-                 CodeViewEntryPointer = (VOID *) (\r
-                                        ((UINTN)Pe32Data) + \r
-                                        (UINTN) DebugEntry->RVA - \r
-                                        SectionHeader[Index1].VirtualAddress + \r
-                                        SectionHeader[Index1].PointerToRawData + \r
-                                        (UINTN)TEImageAdjust);\r
-                 break;\r
-               }\r
-             }\r
-             if (Index1 >= Index) {\r
-               //\r
-               // Can't find CodeViewEntryPointer in raw PE/COFF image.\r
-               //\r
-               continue;\r
-             }\r
+        //\r
+        // Get the DebugEntry offset in the raw data image.\r
+        //\r
+        CodeViewEntryPointer = NULL;\r
+        for (Index1 = 0; Index1 < Index; Index1 ++) {\r
+          if ((DebugEntry->RVA >= SectionHeader[Index1].VirtualAddress) && \r
+             (DebugEntry->RVA < (SectionHeader[Index1].VirtualAddress + SectionHeader[Index1].Misc.VirtualSize))) {\r
+            CodeViewEntryPointer = (VOID *) (\r
+                                   ((UINTN)Pe32Data) + \r
+                                   (UINTN) DebugEntry->RVA - \r
+                                   SectionHeader[Index1].VirtualAddress + \r
+                                   SectionHeader[Index1].PointerToRawData + \r
+                                   (UINTN)TEImageAdjust);\r
+            break;\r
+          }\r
+        }\r
+        if (Index1 >= Index) {\r
+          //\r
+          // Can't find CodeViewEntryPointer in raw PE/COFF image.\r
+          //\r
+          continue;\r
+        }\r
         switch (* (UINT32 *) CodeViewEntryPointer) {\r
         case CODEVIEW_SIGNATURE_NB10:\r
           return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r
         case CODEVIEW_SIGNATURE_RSDS:\r
           return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r
+        case CODEVIEW_SIGNATURE_MTOC:\r
+          return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));\r
         default:\r
           break;\r
         }\r
@@ -1384,3 +1391,50 @@ PeCoffLoaderGetPdbPointer (
 \r
   return NULL;\r
 }\r
+\r
+\r
+RETURN_STATUS\r
+EFIAPI\r
+PeCoffLoaderGetEntryPoint (\r
+  IN  VOID  *Pe32Data,\r
+  OUT VOID  **EntryPoint,\r
+  OUT VOID  **BaseOfImage\r
+  )\r
+{\r
+  EFI_IMAGE_DOS_HEADER                  *DosHdr;\r
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
+\r
+  DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
+  if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
+    //\r
+    // DOS image header is present, so read the PE header after the DOS image header.\r
+    //\r
+    Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
+  } else {\r
+    //\r
+    // DOS image header is not present, so PE header is at the image base.\r
+    //\r
+    Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
+  }\r
+\r
+  //\r
+  // Calculate the entry point relative to the start of the image.\r
+  // AddressOfEntryPoint is common for PE32 & PE32+\r
+  //\r
+  if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
+    *BaseOfImage = (VOID *)(UINTN)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));\r
+    *EntryPoint = (VOID *)((UINTN)*BaseOfImage + (Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);\r
+    return RETURN_SUCCESS;\r
+  } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
+    *EntryPoint = (VOID *)(UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint;\r
+    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+      *BaseOfImage = (VOID *)(UINTN)Hdr.Pe32->OptionalHeader.ImageBase;\r
+    } else {\r
+      *BaseOfImage = (VOID *)(UINTN)Hdr.Pe32Plus->OptionalHeader.ImageBase;\r
+    }\r
+    *EntryPoint = (VOID *)(UINTN)((UINTN)*EntryPoint + (UINTN)*BaseOfImage);\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  return RETURN_UNSUPPORTED;\r
+}\r