ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

rsym.cmake.c
Go to the documentation of this file.
00001 /*
00002  * Usage: rsym input-file output-file
00003  *
00004  * There are two sources of information: the .stab/.stabstr
00005  * sections of the executable and the COFF symbol table. Most
00006  * of the information is in the .stab/.stabstr sections.
00007  * However, most of our asm files don't contain .stab directives,
00008  * so routines implemented in assembler won't show up in the
00009  * .stab section. They are present in the COFF symbol table.
00010  * So, we mostly use the .stab/.stabstr sections, but we augment
00011  * the info there with info from the COFF symbol table when
00012  * possible.
00013  *
00014  * This is a tool and is compiled using the host compiler,
00015  * i.e. on Linux gcc and not mingw-gcc (cross-compiler).
00016  * Therefore we can't include SDK headers and we have to
00017  * duplicate some definitions here.
00018  * Also note that the internal functions are "old C-style",
00019  * returning an int, where a return of 0 means success and
00020  * non-zero is failure.
00021  */
00022 
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <assert.h>
00027 
00028 #include "rsym.h"
00029 
00030 int main(int argc, char* argv[])
00031 {
00032   int i;
00033   PSYMBOLFILE_HEADER SymbolFileHeader;
00034   PIMAGE_DOS_HEADER PEDosHeader;
00035   PIMAGE_FILE_HEADER PEFileHeader;
00036   PIMAGE_OPTIONAL_HEADER PEOptHeader;
00037   PIMAGE_SECTION_HEADER PESectionHeaders;
00038   char* path1;
00039   char* path2;
00040   FILE* out;
00041   size_t FileSize;
00042   void *FileData;
00043   char elfhdr[] = { '\377', 'E', 'L', 'F' };
00044 
00045   if (3 != argc)
00046     {
00047       fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
00048       exit(1);
00049     }
00050 
00051   path1 = convert_path(argv[1]);
00052   path2 = convert_path(argv[2]);
00053 
00054   FileData = load_file ( path1, &FileSize );
00055   if ( !FileData )
00056   {
00057     fprintf ( stderr, "An error occured loading '%s'\n", path1 );
00058     exit(1);
00059   }
00060 
00061   /* Check if MZ header exists  */
00062   PEDosHeader = (PIMAGE_DOS_HEADER) FileData;
00063   if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
00064     {
00065       /* Ignore elf */
00066       if (!memcmp(PEDosHeader, elfhdr, sizeof(elfhdr)))
00067     exit(0);
00068       perror("Input file is not a PE image.\n");
00069       free(FileData);
00070       exit(1);
00071     }
00072 
00073   /* Locate PE file header  */
00074   /* sizeof(ULONG) = sizeof(MAGIC) */
00075   PEFileHeader = (PIMAGE_FILE_HEADER)((char *) FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
00076 
00077   /* Locate optional header */
00078   assert(sizeof(ULONG) == 4);
00079   PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
00080 
00081   /* Locate PE section headers  */
00082   PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
00083 
00084   for (i = 0; i < PEFileHeader->NumberOfSections; i++) {
00085       if (PESectionHeaders[i].Name[0] == '/') {
00086           PESectionHeaders[i].Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
00087           PESectionHeaders[i].Characteristics &= ~(IMAGE_SCN_MEM_PURGEABLE | IMAGE_SCN_MEM_DISCARDABLE);
00088       }
00089   }
00090 
00091   PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData =
00092       FileSize - PESectionHeaders[PEFileHeader->NumberOfSections-1].PointerToRawData;
00093   if (PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData >
00094       PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize) {
00095       PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize =
00096           ROUND_UP(PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData, 
00097                    PEOptHeader->SectionAlignment);
00098       PEOptHeader->SizeOfImage = PESectionHeaders[PEFileHeader->NumberOfSections-1].VirtualAddress + PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize;
00099   }
00100 
00101   out = fopen(path2, "wb");
00102   if (out == NULL)
00103     {
00104       perror("Cannot open output file");
00105       free(FileData);
00106       exit(1);
00107     }
00108 
00109   fwrite(FileData, 1, FileSize, out);
00110   fclose(out);
00111   free(FileData);
00112 
00113   return 0;
00114 }
00115 
00116 /* EOF */

Generated on Sun May 27 2012 04:37:47 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.