+
+#ifdef __APPLE__
+ BOOLEAN EnabledOnEntry;
+
+ //
+ // Make sure writting of the file is an atomic operation
+ //
+ if (UnixInterruptEanbled ()) {
+ UnixDisableInterrupt ();
+ EnabledOnEntry = TRUE;
+ } else {
+ EnabledOnEntry = FALSE;
+ }
+
+ PrintLoadAddress (ImageContext);
+
+ //
+ // In mach-o (OS X executable) dlopen() can only load files in the MH_DYLIB of MH_BUNDLE format.
+ // To convert to PE/COFF we need to construct a mach-o with the MH_PRELOAD format. We create
+ // .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.
+ //
+ if (ImageContext->PdbPointer != NULL && !IsPdbFile (ImageContext->PdbPointer)) {
+ //
+ // 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.
+ //
+ // 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
+ // data base of info ready to roll.
+ //
+ // 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>
+ //
+
+ //
+ // Write the file we need for the gdb script
+ //
+ GdbTempFile = fopen (gGdbWorkingFileName, "w");
+ if (GdbTempFile != NULL) {
+ 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.
+ // 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;
+
+ if (ImageContext->PdbPointer == NULL) {
+ return;
+ }
+
+ if (!IsPdbFile (ImageContext->PdbPointer)) {
+ return;
+ }
+
+ fprintf (stderr,