]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UnixPkg/Sec/SecMain.c
Fix issue with fixing tabs.
[mirror_edk2.git] / UnixPkg / Sec / SecMain.c
index 98a7a4db9df9499578ba1c5215230afdc5b3fab6..a82ec1cce18642632cbfe9209a5e370c50671edb 100644 (file)
@@ -1,8 +1,8 @@
 /*++
 
-Copyright (c) 2006 - 2009 Intel Corporation.
-Portions copyright (c) 2008-2009 Apple Inc.
-All rights reserved. This program and the accompanying materials
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
+This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
@@ -36,7 +36,6 @@ Abstract:
 #include "SecMain.h"
 #include <sys/mman.h>
 #include <Ppi/UnixPeiLoadFile.h>
-#include <Framework/StatusCode.h>
 #include <Ppi/TemporaryRamSupport.h>
 #include <dlfcn.h>
 
@@ -49,18 +48,21 @@ char *gGdbWorkingFileName = NULL;
 //
 // Globals
 //
-
-UNIX_PEI_LOAD_FILE_PPI                    mSecUnixLoadFilePpi          = { SecUnixPeiLoadFile };
-
-PEI_UNIX_AUTOSCAN_PPI                     mSecUnixAutoScanPpi          = { SecUnixPeiAutoScan };
-
-PEI_UNIX_THUNK_PPI                        mSecUnixThunkPpi          = { SecUnixUnixThunkAddress };
-
+#if defined(__APPLE__) || defined(MDE_CPU_X64)
+UNIX_PEI_LOAD_FILE_PPI                    mSecUnixLoadFilePpi        = { GasketSecUnixPeiLoadFile };
+PEI_UNIX_AUTOSCAN_PPI                     mSecUnixAutoScanPpi        = { GasketSecUnixPeiAutoScan };
+PEI_UNIX_THUNK_PPI                        mSecUnixThunkPpi           = { GasketSecUnixUnixThunkAddress };
+EFI_PEI_PROGRESS_CODE_PPI                 mSecStatusCodePpi          = { GasketSecPeiReportStatusCode };
+UNIX_FWH_PPI                              mSecFwhInformationPpi      = { GasketSecUnixFdAddress };
+TEMPORARY_RAM_SUPPORT_PPI                 mSecTemporaryRamSupportPpi = { GasketSecTemporaryRamSupport };
+#else
+UNIX_PEI_LOAD_FILE_PPI                    mSecUnixLoadFilePpi        = { SecUnixPeiLoadFile };
+PEI_UNIX_AUTOSCAN_PPI                     mSecUnixAutoScanPpi        = { SecUnixPeiAutoScan };
+PEI_UNIX_THUNK_PPI                        mSecUnixThunkPpi           = { SecUnixUnixThunkAddress };
 EFI_PEI_PROGRESS_CODE_PPI                 mSecStatusCodePpi          = { SecPeiReportStatusCode };
-
 UNIX_FWH_PPI                              mSecFwhInformationPpi      = { SecUnixFdAddress };
-
-TEMPORARY_RAM_SUPPORT_PPI                 mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};
+TEMPORARY_RAM_SUPPORT_PPI                 mSecTemporaryRamSupportPpi = { SecTemporaryRamSupport };
+#endif
 
 EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {
   {
@@ -89,7 +91,6 @@ EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {
     &mSecTemporaryRamSupportPpi
   },
   {
-
     EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
     &gUnixFwhPpiGuid,
     &mSecFwhInformationPpi
@@ -142,6 +143,7 @@ MapFile (
   IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
   OUT UINT64                    *Length
   );
+
 EFI_STATUS
 EFIAPI
 SecNt32PeCoffRelocateImage (
@@ -188,10 +190,10 @@ Returns:
   setbuf(stdout, 0);
   setbuf(stderr, 0);
 
-  MemorySizeStr      = (CHAR16 *) FixedPcdGetPtr (PcdUnixMemorySizeForSecMain);
-  FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdUnixFirmwareVolume);
+  MemorySizeStr      = (CHAR16 *) PcdGetPtr (PcdUnixMemorySizeForSecMain);
+  FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdUnixFirmwareVolume);
 
-  printf ("\nEDK SEC Main UNIX Emulation Environment from www.TianoCore.org\n");
+  printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n");
 
 #ifdef __APPLE__
   //
@@ -200,7 +202,7 @@ Returns:
   // symbols when we load every PE/COFF image.
   //
   Index = strlen (*Argv);
-  gGdbWorkingFileName = malloc (Index + strlen(".gdb"));
+  gGdbWorkingFileName = malloc (Index + strlen(".gdb") + 1);
   strcpy (gGdbWorkingFileName, *Argv);
   strcat (gGdbWorkingFileName, ".gdb");
 #endif
@@ -227,7 +229,7 @@ Returns:
   //
   // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
   //
-  printf ("  BootMode 0x%02x\n", (unsigned int)FixedPcdGet32 (PcdUnixBootMode));
+  printf ("  BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdUnixBootMode));
 
   //
   // Open up a 128K file to emulate temp memory for PEI.
@@ -235,9 +237,9 @@ Returns:
   //  Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
   //
   InitialStackMemorySize  = STACK_SIZE;
-  InitialStackMemory = (UINTN)MapMemory(0, 
+  InitialStackMemory = (UINTN)MapMemory(0,
           (UINT32) InitialStackMemorySize,
-          PROT_READ | PROT_WRITE,
+          PROT_READ | PROT_WRITE | PROT_EXEC,
           MAP_ANONYMOUS | MAP_PRIVATE);
   if (InitialStackMemory == 0) {
     printf ("ERROR : Can not open SecStack Exiting\n");
@@ -247,7 +249,7 @@ Returns:
   printf ("  SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n",
     (unsigned int)(InitialStackMemorySize / 1024),
     (unsigned long)InitialStackMemory);
-    
+
   for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;
      StackPointer < (UINTN*)(UINTN)((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize);
      StackPointer ++) {
@@ -277,17 +279,17 @@ Returns:
     // Open the FD and remmeber where it got mapped into our processes address space
     //
     Status = MapFile (
-                     FileName,
-                     &gFdInfo[Index].Address,
-                     &gFdInfo[Index].Size
-                     );
+          FileName,
+          &gFdInfo[Index].Address,
+          &gFdInfo[Index].Size
+          );
     if (EFI_ERROR (Status)) {
       printf ("ERROR : Can not open Firmware Device File %s (%x).  Exiting.\n", FileName, (unsigned int)Status);
       exit (1);
     }
 
     printf ("  FD loaded from %s at 0x%08lx",
-           FileName, (unsigned long)gFdInfo[Index].Address);
+      FileName, (unsigned long)gFdInfo[Index].Address);
 
     if (PeiCoreFile == NULL) {
       //
@@ -417,15 +419,15 @@ Returns:
       /* Read entry address.  */
       lseek (fd, FileSize - 0x20, SEEK_SET);
       if (read (fd, &EntryAddress, 4) != 4)
-       {
-         close (fd);
-         return EFI_DEVICE_ERROR;
-       }      
+  {
+    close (fd);
+    return EFI_DEVICE_ERROR;
+  }
     }
 #endif
 
   res = MapMemory(fd, FileSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
-  
+
   close (fd);
 
   if (res == MAP_FAILED)
@@ -491,7 +493,7 @@ Returns:
 
   } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
     //
-    // Process DEBUG () macro 
+    // Process DEBUG () macro
     //
     AsciiBSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);
     printf ("%s", PrintBuffer);
@@ -500,28 +502,6 @@ Returns:
   return EFI_SUCCESS;
 }
 
-/**
-  Transfers control to a function starting with a new stack.
-
-  Transfers control to the function specified by EntryPoint using the new stack
-  specified by NewStack and passing in the parameters specified by Context1 and
-  Context2. Context1 and Context2 are optional and may be NULL. The function
-  EntryPoint must never return.
-
-  If EntryPoint is NULL, then ASSERT().
-  If NewStack is NULL, then ASSERT().
-
-  @param  EntryPoint  A pointer to function to call with the new stack.
-  @param  Context1    A pointer to the context to pass into the EntryPoint
-                      function.
-  @param  Context2    A pointer to the context to pass into the EntryPoint
-                      function.
-  @param  NewStack    A pointer to the new stack to use for the EntryPoint
-                      function.
-  @param  NewBsp      A pointer to the new BSP for the EntryPoint on IPF. It's
-                      Reserved on other architectures.
-
-**/
 VOID
 EFIAPI
 PeiSwitchStacks (
@@ -530,33 +510,7 @@ PeiSwitchStacks (
   IN      VOID                      *Context2,  OPTIONAL
   IN      VOID                      *Context3,  OPTIONAL
   IN      VOID                      *NewStack
-  )
-{
-  BASE_LIBRARY_JUMP_BUFFER  JumpBuffer;
-  
-  ASSERT (EntryPoint != NULL);
-  ASSERT (NewStack != NULL);
-
-  //
-  // Stack should be aligned with CPU_STACK_ALIGNMENT
-  //
-  ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);
-
-  JumpBuffer.Eip = (UINTN)EntryPoint;
-  JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*);
-  JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3);
-  ((VOID**)JumpBuffer.Esp)[1] = Context1;
-  ((VOID**)JumpBuffer.Esp)[2] = Context2;
-  ((VOID**)JumpBuffer.Esp)[3] = Context3;
-
-  LongJump (&JumpBuffer, (UINTN)-1);
-  
-
-  //
-  // InternalSwitchStack () will never return
-  //
-  ASSERT (FALSE);  
-}
+  );
 
 VOID
 SecLoadFromCore (
@@ -589,6 +543,8 @@ Returns:
   EFI_PHYSICAL_ADDRESS        PeiImageAddress;
   EFI_SEC_PEI_HAND_OFF        *SecCoreData;
   UINTN                       PeiStackSize;
+  EFI_PEI_PPI_DESCRIPTOR      *DispatchTable;
+  UINTN                       DispatchTableSize;
 
   //
   // Compute Top Of Memory for Stack and PEI Core Allocations
@@ -604,25 +560,25 @@ Returns:
   // |           |
   // |  Stack    |
   // |-----------| <---- TemporaryRamBase
-  // 
+  //
   TopOfStack  = (VOID *)(LargestRegion + PeiStackSize);
   TopOfMemory = LargestRegion + PeiStackSize;
 
   //
   // Reservet space for storing PeiCore's parament in stack.
-  // 
+  //
   TopOfStack  = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);
   TopOfStack  = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
 
-  
+
   //
   // Bind this information into the SEC hand-off state
   //
   SecCoreData                        = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;
   SecCoreData->DataSize               = sizeof(EFI_SEC_PEI_HAND_OFF);
   SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;
-  SecCoreData->BootFirmwareVolumeSize = FixedPcdGet32(PcdUnixFirmwareFdSize);
-  SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)LargestRegion; 
+  SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdUnixFirmwareFdSize);
+  SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)LargestRegion;
   SecCoreData->TemporaryRamSize       = STACK_SIZE;
   SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;
   SecCoreData->StackSize              = PeiStackSize;
@@ -641,14 +597,28 @@ Returns:
   if (EFI_ERROR (Status)) {
     return ;
   }
-  
+
+  DispatchTableSize = sizeof (gPrivateDispatchTable);
+  DispatchTableSize += OverrideDispatchTableExtraSize ();
+
+  DispatchTable = malloc (DispatchTableSize);
+  if (DispatchTable == NULL) {
+    return;
+  }
+
+  //
+  // Allow an override for extra PPIs to be passed up to PEI
+  // This is an easy way to enable OS specific customizations
+  //
+  OverrideDispatchTable (&gPrivateDispatchTable[0], sizeof (gPrivateDispatchTable), DispatchTable, DispatchTableSize);
+
   //
   // Transfer control to the PEI Core
   //
   PeiSwitchStacks (
     (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,
     SecCoreData,
-    (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable),
+    (VOID *)DispatchTable,
     NULL,
     TopOfStack
     );
@@ -694,8 +664,8 @@ Returns:
 
   *MemoryBase = 0;
   res = MapMemory(0, gSystemMemory[Index].Size,
-                 PROT_READ | PROT_WRITE | PROT_EXEC,
-                 MAP_PRIVATE | MAP_ANONYMOUS);
+      PROT_READ | PROT_WRITE | PROT_EXEC,
+      MAP_PRIVATE | MAP_ANONYMOUS);
   if (res == MAP_FAILED)
     return EFI_DEVICE_ERROR;
   *MemorySize = gSystemMemory[Index].Size;
@@ -765,18 +735,26 @@ Returns:
   if (EFI_ERROR (Status)) {
     return Status;
   }
+
+
   //
   // Allocate space in UNIX (not emulator) memory. Extra space is for alignment
   //
-  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)));
+  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) MapMemory (
+    0,
+    (UINT32) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)),
+    PROT_READ | PROT_WRITE | PROT_EXEC,
+    MAP_ANONYMOUS | MAP_PRIVATE
+    );
   if (ImageContext.ImageAddress == 0) {
     return EFI_OUT_OF_RESOURCES;
   }
+
   //
   // Align buffer on section boundry
   //
   ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
-  ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
 
 
   Status = PeCoffLoaderLoadImage (&ImageContext);
@@ -784,6 +762,12 @@ Returns:
     return Status;
   }
 
+  Status = PeCoffLoaderRelocateImage (&ImageContext);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+
   SecPeCoffRelocateImageExtraAction (&ImageContext);
 
   //
@@ -823,7 +807,8 @@ EFIAPI
 SecUnixFdAddress (
   IN     UINTN                 Index,
   IN OUT EFI_PHYSICAL_ADDRESS  *FdBase,
-  IN OUT UINT64                *FdSize
+  IN OUT UINT64                *FdSize,
+  IN OUT EFI_PHYSICAL_ADDRESS  *FixUp
   )
 /*++
 
@@ -835,6 +820,7 @@ Arguments:
   Index  - Which FD, starts at zero.
   FdSize - Size of the FD in bytes
   FdBase - Start address of the FD. Assume it points to an FV Header
+  FixUp  - Difference between actual FD address and build address
 
 Returns:
   EFI_SUCCESS     - Return the Base address and size of the FV
@@ -848,11 +834,21 @@ Returns:
 
   *FdBase = gFdInfo[Index].Address;
   *FdSize = gFdInfo[Index].Size;
+  *FixUp  = 0;
 
   if (*FdBase == 0 && *FdSize == 0) {
     return EFI_UNSUPPORTED;
   }
 
+  if (Index == 0) {
+    //
+    // FD 0 has XIP code and well known PCD values
+    // If the memory buffer could not be allocated at the FD build address
+    // the Fixup is the difference.
+    //
+    *FixUp = *FdBase - PcdGet64 (PcdUnixFdBaseAddress);
+  }
+
   return EFI_SUCCESS;
 }
 
@@ -934,16 +930,16 @@ AddHandle (
 
 Routine Description:
   Store the ModHandle in an array indexed by the Pdb File name.
-  The ModHandle is needed to unload the image. 
+  The ModHandle is needed to unload the image.
 
 Arguments:
-  ImageContext - Input data returned from PE Laoder Library. Used to find the 
+  ImageContext - Input data returned from PE Laoder Library. Used to find the
                  .PDB file name of the PE Image.
-  ModHandle    - Returned from LoadLibraryEx() and stored for call to 
+  ModHandle    - Returned from LoadLibraryEx() and stored for call to
                  FreeLibrary().
 
 Returns:
-  EFI_SUCCESS - ModHandle was stored. 
+  EFI_SUCCESS - ModHandle was stored.
 
 --*/
 {
@@ -963,9 +959,9 @@ Returns:
       return EFI_SUCCESS;
     }
   }
-  
+
   //
-  // No free space in mImageContextModHandleArray so grow it by 
+  // No free space in mImageContextModHandleArray so grow it by
   // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
   // copy the old values to the new locaiton. But it does
   // not zero the new memory area.
@@ -978,9 +974,9 @@ Returns:
     ASSERT (FALSE);
     return EFI_OUT_OF_RESOURCES;
   }
-  
+
   memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE));
+
   return AddHandle (ImageContext, ModHandle);
 }
 
@@ -995,7 +991,7 @@ Routine Description:
   Return the ModHandle and delete the entry in the array.
 
 Arguments:
-  ImageContext - Input data returned from PE Laoder Library. Used to find the 
+  ImageContext - Input data returned from PE Laoder Library. Used to find the
                  .PDB file name of the PE Image.
 
 Returns:
@@ -1031,7 +1027,7 @@ Returns:
 
 
 //
-// Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a 
+// Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a
 // add-symbol-file command. Hey what can you say scripting in gdb is not that great....
 //
 // Put .gdbinit in the CWD where you do gdb SecMain.dll for source level debug
@@ -1073,13 +1069,13 @@ IsPdbFile (
   if ((Len < 5)|| (PdbFileName[Len - 4] != '.')) {
     return FALSE;
   }
-  
+
   if ((PdbFileName[Len - 3] == 'P' || PdbFileName[Len - 3] == 'p') &&
       (PdbFileName[Len - 2] == 'D' || PdbFileName[Len - 2] == 'd') &&
       (PdbFileName[Len - 1] == 'B' || PdbFileName[Len - 1] == 'b')) {
     return TRUE;
   }
-  
+
   return FALSE;
 }
 
@@ -1091,13 +1087,20 @@ PrintLoadAddress (
   IN PE_COFF_LOADER_IMAGE_CONTEXT          *ImageContext
   )
 {
-  fprintf (stderr,
-     "0x%08lx Loading %s with entry point 0x%08lx\n",
-     (unsigned long)ImageContext->ImageAddress + ImageContext->SizeOfHeaders,
-     ImageContext->PdbPointer,   
-     (unsigned long)ImageContext->EntryPoint
-     );
-     
+  if (ImageContext->PdbPointer == NULL) {
+    fprintf (stderr,
+      "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n",
+      (unsigned long)(ImageContext->ImageAddress),
+      (unsigned long)ImageContext->EntryPoint
+      );
+  } else {
+    fprintf (stderr,
+      "0x%08lx Loading %s with entry point 0x%08lx\n",
+      (unsigned long)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders),
+      ImageContext->PdbPointer,
+      (unsigned long)ImageContext->EntryPoint
+      );
+  }
   // Keep output synced up
   fflush (stderr);
 }
@@ -1109,15 +1112,20 @@ SecPeCoffRelocateImageExtraAction (
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext
   )
 {
-  EFI_STATUS Status;
 
-  Status = PeCoffLoaderRelocateImage (ImageContext);
-  if (EFI_ERROR (Status)) {
-    PrintLoadAddress (ImageContext);
-    return;
-  }
-    
 #ifdef __APPLE__
+  BOOLEAN EnabledOnEntry;
+
+   //
+   // Make sure writting of the file is an atomic operation
+   //
+   if (UnixInterruptEanbled ()) {
+     UnixDisableInterrupt ();
+     EnabledOnEntry = TRUE;
+   } else {
+     EnabledOnEntry = FALSE;
+   }
+
   PrintLoadAddress (ImageContext);
 
   //
@@ -1126,7 +1134,7 @@ SecPeCoffRelocateImageExtraAction (
   // .dSYM files for the PE/COFF images that can be used by gdb for source level debugging.
   //
   FILE  *GdbTempFile;
-  
+
   //
   // In the Mach-O to PE/COFF conversion the size of the PE/COFF headers is not accounted for.
   // Thus we need to skip over the PE/COFF header when giving load addresses for our symbol table.
@@ -1135,65 +1143,80 @@ SecPeCoffRelocateImageExtraAction (
     //
     // Now we have a database of the images that are currently loaded
     //
-    
+
+    //
+    // 'symbol-file' will clear out currnet symbol mappings in gdb.
+    // you can do a 'add-symbol-file filename address' for every image we loaded to get source
+    // level debug in gdb. Note Sec, being a true application will work differently.
     //
-    // 'symbol-file' will clear out currnet symbol mappings in gdb. 
-    // you can do a 'add-symbol-file filename address' for every image we loaded to get source 
-    // level debug in gdb. Note Sec, being a true application will work differently. 
+    // We add the PE/COFF header size into the image as the mach-O does not have a header in
+    // loaded into system memory.
     //
-    // We add the PE/COFF header size into the image as the mach-O does not have a header in 
-    // loaded into system memory. 
-    // 
     // This gives us a data base of gdb commands and after something is unloaded that entry will be
-    // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the 
+    // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the
     // data base of info ready to roll.
     //
-    // We could use qXfer:libraries:read, but OS X GDB does not currently support it. 
-    //  <library-list> 
+    // We could use qXfer:libraries:read, but OS X GDB does not currently support it.
+    //  <library-list>
     //    <library name="/lib/libc.so.6">   // ImageContext->PdbPointer
     //      <segment address="0x10000000"/> // ImageContext->ImageAddress + ImageContext->SizeOfHeaders
-    //    </library> 
-    //  </library-list> 
+    //    </library>
+    //  </library-list>
     //
-    
+
     //
     // Write the file we need for the gdb script
     //
     GdbTempFile = fopen (gGdbWorkingFileName, "w");
     if (GdbTempFile != NULL) {
-      fprintf (GdbTempFile, "add-symbol-file %s 0x%x\n", ImageContext->PdbPointer, (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
+      fprintf (GdbTempFile, "add-symbol-file %s 0x%08lx\n", ImageContext->PdbPointer, (long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
       fclose (GdbTempFile);
-      
+
       //
-      // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint. 
+      // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
       // Hey what can you say scripting in gdb is not that great....
       //
       SecGdbScriptBreak ();
+    } else {
+      ASSERT (FALSE);
     }
 
     AddHandle (ImageContext, ImageContext->PdbPointer);
+
+    if (EnabledOnEntry) {
+      UnixEnableInterrupt ();
+    }
+
     
   }
-  
+
 #else
-  
+
   void        *Handle = NULL;
   void        *Entry = NULL;
-  fprintf (stderr, 
+
+  if (ImageContext->PdbPointer == NULL) {
+    return;
+  }
+
+  if (!IsPdbFile (ImageContext->PdbPointer)) {
+    return;
+  }
+
+  fprintf (stderr,
      "Loading %s 0x%08lx - entry point 0x%08lx\n",
      ImageContext->PdbPointer,
      (unsigned long)ImageContext->ImageAddress,
      (unsigned long)ImageContext->EntryPoint);
 
-  Handle = dlopen(ImageContext->PdbPointer, RTLD_NOW);
-  
+  Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW);
+
   if (Handle) {
-    Entry = dlsym(Handle, "_ModuleEntryPoint");
+    Entry = dlsym (Handle, "_ModuleEntryPoint");
   } else {
-    printf("%s\n", dlerror());  
+    printf("%s\n", dlerror());
   }
-  
+
   if (Entry != NULL) {
     ImageContext->EntryPoint = (UINTN)Entry;
     printf("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (unsigned long)Entry);
@@ -1219,12 +1242,20 @@ SecPeCoffLoaderUnloadImageExtraAction (
 
 #ifdef __APPLE__
   FILE  *GdbTempFile;
-  
+  BOOLEAN EnabledOnEntry;
+
   if (Handle != NULL) {
     //
     // Need to skip .PDB files created from VC++
     //
-    if (!IsPdbFile (ImageContext->PdbPointer)) {      
+    if (!IsPdbFile (ImageContext->PdbPointer)) {
+       if (UnixInterruptEanbled ()) {
+         UnixDisableInterrupt ();
+         EnabledOnEntry = TRUE;
+       } else {
+         EnabledOnEntry = FALSE;
+       }
+       
       //
       // Write the file we need for the gdb script
       //
@@ -1232,16 +1263,22 @@ SecPeCoffLoaderUnloadImageExtraAction (
       if (GdbTempFile != NULL) {
         fprintf (GdbTempFile, "remove-symbol-file %s\n", ImageContext->PdbPointer);
         fclose (GdbTempFile);
-  
+
         //
-        // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint. 
+        // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
         // Hey what can you say scripting in gdb is not that great....
         //
         SecGdbScriptBreak ();
+      } else {
+        ASSERT (FALSE);
+      }
+      
+      if (EnabledOnEntry) {
+        UnixEnableInterrupt ();
       }
     }
   }
-  
+
 #else
   //
   // Don't want to confuse gdb with symbols for something that got unloaded
@@ -1272,32 +1309,32 @@ SecTemporaryRamSupport (
 {
   //
   // Migrate the whole temporary memory to permenent memory.
-  // 
+  //
   CopyMem (
-    (VOID*)(UINTN)PermanentMemoryBase, 
-    (VOID*)(UINTN)TemporaryMemoryBase, 
+    (VOID*)(UINTN)PermanentMemoryBase,
+    (VOID*)(UINTN)TemporaryMemoryBase,
     CopySize
     );
 
   //
   // SecSwitchStack function must be invoked after the memory migration
-  // immediatly, also we need fixup the stack change caused by new call into 
+  // immediatly, also we need fixup the stack change caused by new call into
   // permenent memory.
-  // 
+  //
   SecSwitchStack (
     (UINT32) TemporaryMemoryBase,
     (UINT32) PermanentMemoryBase
     );
 
   //
-  // We need *not* fix the return address because currently, 
+  // We need *not* fix the return address because currently,
   // The PeiCore is excuted in flash.
   //
 
   //
   // Simulate to invalid temporary memory, terminate temporary memory
-  // 
+  //
   //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);
-  
+
   return EFI_SUCCESS;
 }