Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenfromfile.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS kernel 00004 * FILE: lib/rossym/fromfile.c 00005 * PURPOSE: Creating rossym info from a file 00006 * 00007 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com) 00008 */ 00009 00010 #include <precomp.h> 00011 00012 #define NDEBUG 00013 #include <debug.h> 00014 00015 #define SYMBOL_SIZE 18 00016 00017 extern NTSTATUS RosSymStatus; 00018 00019 BOOLEAN 00020 RosSymCreateFromFile(PVOID FileContext, PROSSYM_INFO *RosSymInfo) 00021 { 00022 IMAGE_DOS_HEADER DosHeader; 00023 IMAGE_NT_HEADERS NtHeaders; 00024 PIMAGE_SECTION_HEADER SectionHeaders; 00025 unsigned SectionIndex; 00026 unsigned SymbolTable, NumSymbols; 00027 00028 /* Load DOS header */ 00029 if (! RosSymSeekFile(FileContext, 0)) 00030 { 00031 werrstr("Could not rewind file\n"); 00032 return FALSE; 00033 } 00034 if (! RosSymReadFile(FileContext, &DosHeader, sizeof(IMAGE_DOS_HEADER))) 00035 { 00036 werrstr("Failed to read DOS header %x\n", RosSymStatus); 00037 return FALSE; 00038 } 00039 if (! ROSSYM_IS_VALID_DOS_HEADER(&DosHeader)) 00040 { 00041 werrstr("Image doesn't have a valid DOS header\n"); 00042 return FALSE; 00043 } 00044 00045 /* Load NT headers */ 00046 if (! RosSymSeekFile(FileContext, DosHeader.e_lfanew)) 00047 { 00048 werrstr("Failed seeking to NT headers\n"); 00049 return FALSE; 00050 } 00051 if (! RosSymReadFile(FileContext, &NtHeaders, sizeof(IMAGE_NT_HEADERS))) 00052 { 00053 werrstr("Failed to read NT headers\n"); 00054 return FALSE; 00055 } 00056 if (! ROSSYM_IS_VALID_NT_HEADERS(&NtHeaders)) 00057 { 00058 werrstr("Image doesn't have a valid PE header\n"); 00059 return FALSE; 00060 } 00061 00062 SymbolTable = NtHeaders.FileHeader.PointerToSymbolTable; 00063 NumSymbols = NtHeaders.FileHeader.NumberOfSymbols; 00064 00065 if (!NumSymbols) 00066 { 00067 werrstr("Image doesn't have debug symbols\n"); 00068 return FALSE; 00069 } 00070 00071 DPRINT("SymbolTable %x NumSymbols %x\n", SymbolTable, NumSymbols); 00072 00073 /* Load section headers */ 00074 if (! RosSymSeekFile(FileContext, (char *) IMAGE_FIRST_SECTION(&NtHeaders) - 00075 (char *) &NtHeaders + DosHeader.e_lfanew)) 00076 { 00077 werrstr("Failed seeking to section headers\n"); 00078 return FALSE; 00079 } 00080 DPRINT("Alloc section headers\n"); 00081 SectionHeaders = RosSymAllocMem(NtHeaders.FileHeader.NumberOfSections 00082 * sizeof(IMAGE_SECTION_HEADER)); 00083 if (NULL == SectionHeaders) 00084 { 00085 werrstr("Failed to allocate memory for %u section headers\n", 00086 NtHeaders.FileHeader.NumberOfSections); 00087 return FALSE; 00088 } 00089 if (! RosSymReadFile(FileContext, SectionHeaders, 00090 NtHeaders.FileHeader.NumberOfSections 00091 * sizeof(IMAGE_SECTION_HEADER))) 00092 { 00093 RosSymFreeMem(SectionHeaders); 00094 werrstr("Failed to read section headers\n"); 00095 return FALSE; 00096 } 00097 00098 // Convert names to ANSI_STRINGs 00099 for (SectionIndex = 0; SectionIndex < NtHeaders.FileHeader.NumberOfSections; 00100 SectionIndex++) 00101 { 00102 ANSI_STRING astr; 00103 if (SectionHeaders[SectionIndex].Name[0] != '/') { 00104 DPRINT("Short name string %d, %s\n", SectionIndex, SectionHeaders[SectionIndex].Name); 00105 astr.Buffer = RosSymAllocMem(IMAGE_SIZEOF_SHORT_NAME); 00106 memcpy(astr.Buffer, SectionHeaders[SectionIndex].Name, IMAGE_SIZEOF_SHORT_NAME); 00107 astr.MaximumLength = IMAGE_SIZEOF_SHORT_NAME; 00108 astr.Length = GetStrnlen(astr.Buffer, IMAGE_SIZEOF_SHORT_NAME); 00109 } else { 00110 UNICODE_STRING intConv; 00111 NTSTATUS Status; 00112 ULONG StringOffset; 00113 00114 Status = RtlCreateUnicodeStringFromAsciiz(&intConv, (PCSZ)SectionHeaders[SectionIndex].Name + 1); 00115 if (!NT_SUCCESS(Status)) goto freeall; 00116 Status = RtlUnicodeStringToInteger(&intConv, 10, &StringOffset); 00117 RtlFreeUnicodeString(&intConv); 00118 if (!NT_SUCCESS(Status)) goto freeall; 00119 if (!RosSymSeekFile(FileContext, SymbolTable + NumSymbols * SYMBOL_SIZE + StringOffset)) 00120 goto freeall; 00121 astr.Buffer = RosSymAllocMem(MAXIMUM_DWARF_NAME_SIZE); 00122 if (!RosSymReadFile(FileContext, astr.Buffer, MAXIMUM_DWARF_NAME_SIZE)) 00123 goto freeall; 00124 astr.Length = GetStrnlen(astr.Buffer, MAXIMUM_DWARF_NAME_SIZE); 00125 astr.MaximumLength = MAXIMUM_DWARF_NAME_SIZE; 00126 DPRINT("Long name %d, %s\n", SectionIndex, astr.Buffer); 00127 } 00128 *ANSI_NAME_STRING(&SectionHeaders[SectionIndex]) = astr; 00129 } 00130 00131 DPRINT("Done with sections\n"); 00132 Pe *pe = RosSymAllocMem(sizeof(*pe)); 00133 pe->fd = FileContext; 00134 pe->e2 = peget2; 00135 pe->e4 = peget4; 00136 pe->e8 = peget8; 00137 pe->nsections = NtHeaders.FileHeader.NumberOfSections; 00138 pe->sect = SectionHeaders; 00139 pe->imagebase = pe->loadbase = NtHeaders.OptionalHeader.ImageBase; 00140 pe->imagesize = NtHeaders.OptionalHeader.SizeOfImage; 00141 pe->codestart = NtHeaders.OptionalHeader.BaseOfCode; 00142 pe->datastart = NtHeaders.OptionalHeader.BaseOfData; 00143 pe->loadsection = loaddisksection; 00144 *RosSymInfo = dwarfopen(pe); 00145 00146 return TRUE; 00147 00148 freeall: 00149 for (SectionIndex = 0; SectionIndex < NtHeaders.FileHeader.NumberOfSections; 00150 SectionIndex++) 00151 RtlFreeAnsiString(ANSI_NAME_STRING(&SectionHeaders[SectionIndex])); 00152 RosSymFreeMem(SectionHeaders); 00153 00154 return FALSE; 00155 } 00156 00157 /* EOF */ Generated on Fri May 25 2012 04:34:47 for ReactOS by
1.7.6.1
|