1 /** @file
2 
3   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <Uefi.h>
16 #include <Library/PeCoffGetEntryPointLib.h>
17 #include <Library/UefiLib.h>
18 
19 #include <Guid/DebugImageInfoTable.h>
20 
21 /**
22   Use the EFI Debug Image Table to lookup the FaultAddress and find which PE/COFF image
23   it came from. As long as the PE/COFF image contains a debug directory entry a
24   string can be returned. For ELF and Mach-O images the string points to the Mach-O or ELF
25   image. Microsoft tools contain a pointer to the PDB file that contains the debug information.
26 
27   @param  FaultAddress         Address to find PE/COFF image for.
28   @param  ImageBase            Return load address of found image
29   @param  PeCoffSizeOfHeaders  Return the size of the PE/COFF header for the image that was found
30 
31   @retval NULL                 FaultAddress not in a loaded PE/COFF image.
32   @retval                      Path and file name of PE/COFF image.
33 
34 **/
35 CHAR8 *
GetImageName(IN UINTN FaultAddress,OUT UINTN * ImageBase,OUT UINTN * PeCoffSizeOfHeaders)36 GetImageName (
37   IN  UINTN  FaultAddress,
38   OUT UINTN  *ImageBase,
39   OUT UINTN  *PeCoffSizeOfHeaders
40   )
41 {
42   EFI_STATUS                          Status;
43   EFI_DEBUG_IMAGE_INFO_TABLE_HEADER   *DebugTableHeader;
44   EFI_DEBUG_IMAGE_INFO                *DebugTable;
45   UINTN                               Entry;
46   CHAR8                               *Address;
47 
48   Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&DebugTableHeader);
49   if (EFI_ERROR (Status)) {
50     return NULL;
51   }
52 
53   DebugTable = DebugTableHeader->EfiDebugImageInfoTable;
54   if (DebugTable == NULL) {
55     return NULL;
56   }
57 
58   Address = (CHAR8 *)(UINTN)FaultAddress;
59   for (Entry = 0; Entry < DebugTableHeader->TableSize; Entry++, DebugTable++) {
60     if (DebugTable->NormalImage != NULL) {
61       if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) &&
62           (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {
63         if ((Address >= (CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase) &&
64             (Address <= ((CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase + DebugTable->NormalImage->LoadedImageProtocolInstance->ImageSize))) {
65           *ImageBase = (UINTN)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;
66           *PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)*ImageBase);
67           return PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);
68         }
69       }
70     }
71   }
72 
73   return NULL;
74 }
75 
76