Home | History | Annotate | Download | only in RvdPeCoffExtraActionLib
      1 /**@file
      2 
      3 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
      4 Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
      5 Portions copyright (c) 2011 - 2012, ARM Ltd. All rights reserved.<BR>
      6 
      7 This program and the accompanying materials
      8 are licensed and made available under the terms and conditions of the BSD License
      9 which accompanies this distribution.  The full text of the license may be found at
     10 http://opensource.org/licenses/bsd-license.php
     11 
     12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     14 
     15 **/
     16 
     17 #include <PiDxe.h>
     18 #include <Library/PeCoffLib.h>
     19 
     20 #include <Library/BaseLib.h>
     21 #include <Library/DebugLib.h>
     22 #include <Library/BaseMemoryLib.h>
     23 #include <Library/PeCoffExtraActionLib.h>
     24 #include <Library/SemihostLib.h>
     25 #include <Library/PrintLib.h>
     26 
     27 /**
     28   Append string to debugger script file, create file if needed.
     29 
     30   This library can show up in mulitple places so we need to append the file every time we write to it.
     31   For example Sec can use this to load the DXE core, and the DXE core would use this to load all the
     32   other modules. So we have two instances of the library in the system.
     33 
     34   @param  Buffer  Buffer to write to file.
     35   @param  Length  Length of Buffer in bytes.
     36 **/
     37 VOID
     38 WriteStringToFile (
     39   IN  VOID    *Buffer,
     40   IN  UINT32  Length
     41   )
     42 {
     43   // Working around and issue with the code that is commented out. For now send it to the console.
     44   // You can copy the console into a file and source the file as a script and you get symbols.
     45   // This gets you all the symbols except for SEC. To get SEC symbols you need to copy the
     46   // debug print in the SEC into the debugger manually
     47   SemihostWriteString (Buffer);
     48 /*
     49   I'm currently having issues with this code crashing the debugger. Seems like it should work.
     50 
     51   UINT32        SemihostHandle;
     52   UINT32        SemihostMode = SEMIHOST_FILE_MODE_WRITE | SEMIHOST_FILE_MODE_BINARY | SEMIHOST_FILE_MODE_UPDATE;
     53 
     54   SemihostFileOpen ("c:\rvi_symbols.inc", SemihostMode, &SemihostHandle);
     55   SemihostFileWrite (SemihostHandle, &Length, Buffer);
     56   SemihostFileClose (SemihostHandle);
     57  */
     58 }
     59 
     60 
     61 /**
     62   If the build is done on cygwin the paths are cygpaths.
     63   /cygdrive/c/tmp.txt vs c:\tmp.txt so we need to convert
     64   them to work with RVD commands
     65 
     66   @param  Name  Path to convert if needed
     67 
     68 **/
     69 CHAR8 *
     70 DeCygwinPathIfNeeded (
     71   IN  CHAR8   *Name
     72   )
     73 {
     74   CHAR8   *Ptr;
     75   UINTN   Index;
     76   UINTN   Len;
     77 
     78   Ptr = AsciiStrStr (Name, "/cygdrive/");
     79   if (Ptr == NULL) {
     80     return Name;
     81   }
     82 
     83   Len = AsciiStrLen (Ptr);
     84 
     85   // convert "/cygdrive" to spaces
     86   for (Index = 0; Index < 9; Index++) {
     87     Ptr[Index] = ' ';
     88   }
     89 
     90   // convert /c to c:
     91   Ptr[9]  = Ptr[10];
     92   Ptr[10] = ':';
     93 
     94   // switch path separators
     95   for (Index = 11; Index < Len; Index++) {
     96     if (Ptr[Index] == '/') {
     97       Ptr[Index] = '\\' ;
     98     }
     99   }
    100 
    101   return Name;
    102 }
    103 
    104 
    105 /**
    106   Performs additional actions after a PE/COFF image has been loaded and relocated.
    107 
    108   If ImageContext is NULL, then ASSERT().
    109 
    110   @param  ImageContext  Pointer to the image context structure that describes the
    111                         PE/COFF image that has already been loaded and relocated.
    112 
    113 **/
    114 VOID
    115 EFIAPI
    116 PeCoffLoaderRelocateImageExtraAction (
    117   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
    118   )
    119 {
    120   CHAR8 Buffer[256];
    121 
    122 #if (__ARMCC_VERSION < 500000)
    123   AsciiSPrint (Buffer, sizeof(Buffer), "load /a /ni /np \"%a\" &0x%08x\n", ImageContext->PdbPointer, (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
    124 #else
    125   AsciiSPrint (Buffer, sizeof(Buffer), "add-symbol-file %a 0x%08x\n", ImageContext->PdbPointer, (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
    126 #endif
    127   DeCygwinPathIfNeeded (&Buffer[16]);
    128 
    129   WriteStringToFile (Buffer, AsciiStrSize (Buffer));
    130 }
    131 
    132 
    133 
    134 /**
    135   Performs additional actions just before a PE/COFF image is unloaded.  Any resources
    136   that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
    137 
    138   If ImageContext is NULL, then ASSERT().
    139 
    140   @param  ImageContext  Pointer to the image context structure that describes the
    141                         PE/COFF image that is being unloaded.
    142 
    143 **/
    144 VOID
    145 EFIAPI
    146 PeCoffLoaderUnloadImageExtraAction (
    147   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
    148   )
    149 {
    150   CHAR8 Buffer[256];
    151 
    152   AsciiSPrint (Buffer, sizeof(Buffer), "unload symbols_only \"%a\"\n", ImageContext->PdbPointer);
    153   DeCygwinPathIfNeeded (Buffer);
    154 
    155   WriteStringToFile (Buffer, AsciiStrSize (Buffer));
    156 }
    157