mirror of
https://github.com/reactos/reactos.git
synced 2024-07-30 16:18:43 +00:00
[RSYM]
* Formatting. No intended code changes. svn path=/trunk/; revision=59496
This commit is contained in:
parent
b666ebecda
commit
9d7768ed60
|
@ -63,8 +63,7 @@ GetStabInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
|
|||
if ((strncmp((char *) PESectionHeaders[Idx].Name, ".stab", 5) == 0)
|
||||
&& (PESectionHeaders[Idx].Name[5] == 0))
|
||||
{
|
||||
/* printf(".stab section found. Size %d\n",
|
||||
PESectionHeaders[Idx].SizeOfRawData); */
|
||||
/* printf(".stab section found. Size %d\n", PESectionHeaders[Idx].SizeOfRawData); */
|
||||
|
||||
*StabSymbolsLength = PESectionHeaders[Idx].SizeOfRawData;
|
||||
*StabSymbolsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
|
||||
|
@ -72,8 +71,7 @@ GetStabInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
|
|||
|
||||
if (strncmp((char *) PESectionHeaders[Idx].Name, ".stabstr", 8) == 0)
|
||||
{
|
||||
/* printf(".stabstr section found. Size %d\n",
|
||||
PESectionHeaders[Idx].SizeOfRawData); */
|
||||
/* printf(".stabstr section found. Size %d\n", PESectionHeaders[Idx].SizeOfRawData); */
|
||||
|
||||
*StabStringsLength = PESectionHeaders[Idx].SizeOfRawData;
|
||||
*StabStringsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
|
||||
|
@ -90,7 +88,7 @@ GetCoffInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
|
|||
ULONG *CoffStringsLength, void **CoffStringsBase)
|
||||
{
|
||||
|
||||
if (0 == PEFileHeader->PointerToSymbolTable || 0 == PEFileHeader->NumberOfSymbols)
|
||||
if (PEFileHeader->PointerToSymbolTable == 0 || PEFileHeader->NumberOfSymbols == 0)
|
||||
{
|
||||
/* No COFF symbol table */
|
||||
*CoffSymbolsLength = 0;
|
||||
|
@ -112,14 +110,14 @@ FindOrAddString(char *StringToFind, ULONG *StringsLength, void *StringsBase)
|
|||
{
|
||||
char *Search, *End;
|
||||
|
||||
Search = (char *) StringsBase;
|
||||
Search = (char *)StringsBase;
|
||||
End = Search + *StringsLength;
|
||||
|
||||
while (Search < End)
|
||||
{
|
||||
if (0 == strcmp(Search, StringToFind))
|
||||
if (strcmp(Search, StringToFind) == 0)
|
||||
{
|
||||
return Search - (char *) StringsBase;
|
||||
return Search - (char *)StringsBase;
|
||||
}
|
||||
Search += strlen(Search) + 1;
|
||||
}
|
||||
|
@ -127,7 +125,7 @@ FindOrAddString(char *StringToFind, ULONG *StringsLength, void *StringsBase)
|
|||
strcpy(Search, StringToFind);
|
||||
*StringsLength += strlen(StringToFind) + 1;
|
||||
|
||||
return Search - (char *) StringsBase;
|
||||
return Search - (char *)StringsBase;
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -150,6 +148,7 @@ ConvertStabs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
StabEntry = StabSymbolsBase;
|
||||
Count = StabSymbolsLength / sizeof(STAB_ENTRY);
|
||||
*SymbolsCount = 0;
|
||||
|
||||
if (Count == 0)
|
||||
{
|
||||
/* No symbol info */
|
||||
|
@ -158,18 +157,18 @@ ConvertStabs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
}
|
||||
|
||||
*SymbolsBase = malloc(Count * sizeof(ROSSYM_ENTRY));
|
||||
if (NULL == *SymbolsBase)
|
||||
if (*SymbolsBase == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate memory for converted .stab symbols\n");
|
||||
return 1;
|
||||
}
|
||||
Current = *SymbolsBase;
|
||||
memset ( Current, 0, sizeof(*Current) );
|
||||
memset(Current, 0, sizeof(*Current));
|
||||
|
||||
LastFunctionAddress = 0;
|
||||
for (i = 0; i < Count; i++)
|
||||
{
|
||||
if ( 0 == LastFunctionAddress )
|
||||
if (LastFunctionAddress == 0)
|
||||
{
|
||||
Address = StabEntry[i].n_value - ImageBase;
|
||||
}
|
||||
|
@ -184,17 +183,17 @@ ConvertStabs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
case N_BINCL:
|
||||
Name = (char *) StabStringsBase + StabEntry[i].n_strx;
|
||||
if (StabStringsLength < StabEntry[i].n_strx
|
||||
||'\0' == *Name || '/' == Name[strlen(Name) - 1]
|
||||
|| '\\' == Name[strlen(Name) - 1]
|
||||
|| *Name == '\0' || Name[strlen(Name) - 1] == '/'
|
||||
|| Name[strlen(Name) - 1] == '\\'
|
||||
|| StabEntry[i].n_value < ImageBase)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if ( First || Address != Current->Address )
|
||||
if (First || Address != Current->Address)
|
||||
{
|
||||
if ( !First )
|
||||
if (!First)
|
||||
{
|
||||
memset ( ++Current, 0, sizeof(*Current) );
|
||||
memset(++Current, 0, sizeof(*Current));
|
||||
Current->FunctionOffset = Current[-1].FunctionOffset;
|
||||
}
|
||||
else
|
||||
|
@ -207,21 +206,21 @@ ConvertStabs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
StringsBase);
|
||||
break;
|
||||
case N_FUN:
|
||||
if (0 == StabEntry[i].n_desc || StabEntry[i].n_value < ImageBase)
|
||||
if (StabEntry[i].n_desc == 0 || StabEntry[i].n_value < ImageBase)
|
||||
{
|
||||
LastFunctionAddress = 0; /* line # 0 = end of function */
|
||||
continue;
|
||||
}
|
||||
if ( First || Address != Current->Address )
|
||||
if (First || Address != Current->Address)
|
||||
{
|
||||
if ( !First )
|
||||
memset ( ++Current, 0, sizeof(*Current) );
|
||||
if (!First)
|
||||
memset(++Current, 0, sizeof(*Current));
|
||||
else
|
||||
First = 0;
|
||||
Current->Address = Address;
|
||||
Current->FileOffset = Current[-1].FileOffset;
|
||||
}
|
||||
Name = (char *) StabStringsBase + StabEntry[i].n_strx;
|
||||
Name = (char *)StabStringsBase + StabEntry[i].n_strx;
|
||||
NameLen = strcspn(Name, ":");
|
||||
if (sizeof(FuncName) <= NameLen)
|
||||
{
|
||||
|
@ -238,11 +237,11 @@ ConvertStabs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
LastFunctionAddress = Address;
|
||||
break;
|
||||
case N_SLINE:
|
||||
if ( First || Address != Current->Address )
|
||||
if (First || Address != Current->Address)
|
||||
{
|
||||
if ( !First )
|
||||
if (!First)
|
||||
{
|
||||
memset ( ++Current, 0, sizeof(*Current) );
|
||||
memset(++Current, 0, sizeof(*Current));
|
||||
Current->FileOffset = Current[-1].FileOffset;
|
||||
Current->FunctionOffset = Current[-1].FunctionOffset;
|
||||
}
|
||||
|
@ -279,8 +278,9 @@ ConvertCoffs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
|
||||
CoffEntry = (PCOFF_SYMENT) CoffSymbolsBase;
|
||||
Count = CoffSymbolsLength / sizeof(COFF_SYMENT);
|
||||
|
||||
*SymbolsBase = malloc(Count * sizeof(ROSSYM_ENTRY));
|
||||
if (NULL == *SymbolsBase)
|
||||
if (*SymbolsBase == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to allocate memory for converted COFF symbols\n");
|
||||
return 1;
|
||||
|
@ -293,19 +293,21 @@ ConvertCoffs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
if (ISFCN(CoffEntry[i].e_type) || C_EXT == CoffEntry[i].e_sclass)
|
||||
{
|
||||
Current->Address = CoffEntry[i].e_value;
|
||||
if (0 < CoffEntry[i].e_scnum)
|
||||
if (CoffEntry[i].e_scnum > 0)
|
||||
{
|
||||
if (PEFileHeader->NumberOfSections < CoffEntry[i].e_scnum)
|
||||
{
|
||||
free(*SymbolsBase);
|
||||
fprintf(stderr, "Invalid section number %d in COFF symbols (only %d sections present)\n",
|
||||
CoffEntry[i].e_scnum, PEFileHeader->NumberOfSections);
|
||||
fprintf(stderr,
|
||||
"Invalid section number %d in COFF symbols (only %d sections present)\n",
|
||||
CoffEntry[i].e_scnum,
|
||||
PEFileHeader->NumberOfSections);
|
||||
return 1;
|
||||
}
|
||||
Current->Address += PESectionHeaders[CoffEntry[i].e_scnum - 1].VirtualAddress;
|
||||
}
|
||||
Current->FileOffset = 0;
|
||||
if (0 == CoffEntry[i].e.e.e_zeroes)
|
||||
if (CoffEntry[i].e.e.e_zeroes == 0)
|
||||
{
|
||||
if (sizeof(FuncName) <= strlen((char *) CoffStringsBase + CoffEntry[i].e.e.e_offset))
|
||||
{
|
||||
|
@ -323,7 +325,7 @@ ConvertCoffs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
|
||||
/* Name demangling: stdcall */
|
||||
p = strrchr(FuncName, '@');
|
||||
if (NULL != p)
|
||||
if (p != NULL)
|
||||
{
|
||||
*p = '\0';
|
||||
}
|
||||
|
@ -332,8 +334,9 @@ ConvertCoffs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
|
|||
StringsLength,
|
||||
StringsBase);
|
||||
Current->SourceLine = 0;
|
||||
memset ( ++Current, 0, sizeof(*Current) );
|
||||
memset(++Current, 0, sizeof(*Current));
|
||||
}
|
||||
|
||||
i += CoffEntry[i].e_numaux;
|
||||
}
|
||||
|
||||
|
@ -360,7 +363,7 @@ MergeStabsAndCoffs(ULONG *MergedSymbolCount, PROSSYM_ENTRY *MergedSymbols,
|
|||
return 0;
|
||||
}
|
||||
*MergedSymbols = malloc(StabSymbolsCount * sizeof(ROSSYM_ENTRY));
|
||||
if (NULL == *MergedSymbols)
|
||||
if (*MergedSymbols == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to allocate memory for merged symbols\n");
|
||||
return 1;
|
||||
|
@ -376,30 +379,31 @@ MergeStabsAndCoffs(ULONG *MergedSymbolCount, PROSSYM_ENTRY *MergedSymbols,
|
|||
j < StabSymbolsCount && StabSymbols[j].Address == StabSymbols[StabIndex].Address;
|
||||
j++)
|
||||
{
|
||||
if (0 != StabSymbols[j].FileOffset && 0 == (*MergedSymbols)[*MergedSymbolCount].FileOffset)
|
||||
if (StabSymbols[j].FileOffset != 0 && (*MergedSymbols)[*MergedSymbolCount].FileOffset == 0)
|
||||
{
|
||||
(*MergedSymbols)[*MergedSymbolCount].FileOffset = StabSymbols[j].FileOffset;
|
||||
}
|
||||
if (0 != StabSymbols[j].FunctionOffset && 0 == (*MergedSymbols)[*MergedSymbolCount].FunctionOffset)
|
||||
if (StabSymbols[j].FunctionOffset != 0 && (*MergedSymbols)[*MergedSymbolCount].FunctionOffset == 0)
|
||||
{
|
||||
(*MergedSymbols)[*MergedSymbolCount].FunctionOffset = StabSymbols[j].FunctionOffset;
|
||||
}
|
||||
if (0 != StabSymbols[j].SourceLine && 0 == (*MergedSymbols)[*MergedSymbolCount].SourceLine)
|
||||
if (StabSymbols[j].SourceLine != 0 && (*MergedSymbols)[*MergedSymbolCount].SourceLine == 0)
|
||||
{
|
||||
(*MergedSymbols)[*MergedSymbolCount].SourceLine = StabSymbols[j].SourceLine;
|
||||
}
|
||||
}
|
||||
StabIndex = j - 1;
|
||||
while (CoffIndex < CoffSymbolsCount
|
||||
&& CoffSymbols[CoffIndex + 1].Address <= (*MergedSymbols)[*MergedSymbolCount].Address)
|
||||
|
||||
while (CoffIndex < CoffSymbolsCount &&
|
||||
CoffSymbols[CoffIndex + 1].Address <= (*MergedSymbols)[*MergedSymbolCount].Address)
|
||||
{
|
||||
CoffIndex++;
|
||||
}
|
||||
NewStabFunctionStringOffset = (*MergedSymbols)[*MergedSymbolCount].FunctionOffset;
|
||||
if (CoffSymbolsCount > 0 &&
|
||||
CoffSymbols[CoffIndex].Address < (*MergedSymbols)[*MergedSymbolCount].Address
|
||||
&& StabFunctionStartAddress < CoffSymbols[CoffIndex].Address
|
||||
&& 0 != CoffSymbols[CoffIndex].FunctionOffset)
|
||||
CoffSymbols[CoffIndex].Address < (*MergedSymbols)[*MergedSymbolCount].Address &&
|
||||
StabFunctionStartAddress < CoffSymbols[CoffIndex].Address &&
|
||||
CoffSymbols[CoffIndex].FunctionOffset != 0)
|
||||
{
|
||||
(*MergedSymbols)[*MergedSymbolCount].FunctionOffset = CoffSymbols[CoffIndex].FunctionOffset;
|
||||
}
|
||||
|
@ -440,8 +444,8 @@ ProcessRelocations(ULONG *ProcessedRelocsLength, void **ProcessedRelocs,
|
|||
PIMAGE_BASE_RELOCATION BaseReloc, End, AcceptedRelocs;
|
||||
int Found;
|
||||
|
||||
if (OptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
|
||||
|| 0 == OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
|
||||
if (OptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC ||
|
||||
OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress == 0)
|
||||
{
|
||||
/* No relocation entries */
|
||||
*ProcessedRelocsLength = 0;
|
||||
|
@ -451,48 +455,52 @@ ProcessRelocations(ULONG *ProcessedRelocsLength, void **ProcessedRelocs,
|
|||
|
||||
RelocSectionHeader = FindSectionForRVA(OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
|
||||
NumberOfSections, SectionHeaders);
|
||||
if (NULL == RelocSectionHeader)
|
||||
if (RelocSectionHeader == NULL)
|
||||
{
|
||||
fprintf(stderr, "Can't find section header for relocation data\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
*ProcessedRelocs = malloc(RelocSectionHeader->SizeOfRawData);
|
||||
if (NULL == *ProcessedRelocs)
|
||||
if (*ProcessedRelocs == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate %u bytes for relocations\n", (unsigned int)RelocSectionHeader->SizeOfRawData);
|
||||
fprintf(stderr,
|
||||
"Failed to allocate %u bytes for relocations\n",
|
||||
(unsigned int)RelocSectionHeader->SizeOfRawData);
|
||||
return 1;
|
||||
}
|
||||
*ProcessedRelocsLength = 0;
|
||||
|
||||
BaseReloc = (PIMAGE_BASE_RELOCATION) ((char *) RawData
|
||||
+ RelocSectionHeader->PointerToRawData
|
||||
+ (OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress -
|
||||
BaseReloc = (PIMAGE_BASE_RELOCATION) ((char *) RawData +
|
||||
RelocSectionHeader->PointerToRawData +
|
||||
(OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress -
|
||||
RelocSectionHeader->VirtualAddress));
|
||||
End = (PIMAGE_BASE_RELOCATION) ((char *) BaseReloc
|
||||
+ OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
|
||||
End = (PIMAGE_BASE_RELOCATION) ((char *) BaseReloc +
|
||||
OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
|
||||
|
||||
while (BaseReloc < End && 0 < BaseReloc->SizeOfBlock)
|
||||
while (BaseReloc < End && BaseReloc->SizeOfBlock > 0)
|
||||
{
|
||||
TargetSectionHeader = FindSectionForRVA(BaseReloc->VirtualAddress, NumberOfSections,
|
||||
TargetSectionHeader = FindSectionForRVA(BaseReloc->VirtualAddress,
|
||||
NumberOfSections,
|
||||
SectionHeaders);
|
||||
if (NULL != TargetSectionHeader)
|
||||
if (TargetSectionHeader != NULL)
|
||||
{
|
||||
AcceptedRelocs = *ProcessedRelocs;
|
||||
Found = 0;
|
||||
while (AcceptedRelocs < (PIMAGE_BASE_RELOCATION) ((char *) *ProcessedRelocs +
|
||||
*ProcessedRelocsLength)
|
||||
&& ! Found)
|
||||
&& !Found)
|
||||
{
|
||||
Found = BaseReloc->SizeOfBlock == AcceptedRelocs->SizeOfBlock
|
||||
&& 0 == memcmp(BaseReloc, AcceptedRelocs, AcceptedRelocs->SizeOfBlock);
|
||||
AcceptedRelocs= (PIMAGE_BASE_RELOCATION) ((char *) AcceptedRelocs +
|
||||
Found = BaseReloc->SizeOfBlock == AcceptedRelocs->SizeOfBlock &&
|
||||
memcmp(BaseReloc, AcceptedRelocs, AcceptedRelocs->SizeOfBlock) == 0;
|
||||
AcceptedRelocs = (PIMAGE_BASE_RELOCATION) ((char *) AcceptedRelocs +
|
||||
AcceptedRelocs->SizeOfBlock);
|
||||
}
|
||||
if (! Found)
|
||||
if (!Found)
|
||||
{
|
||||
memcpy((char *) *ProcessedRelocs + *ProcessedRelocsLength,
|
||||
BaseReloc, BaseReloc->SizeOfBlock);
|
||||
BaseReloc,
|
||||
BaseReloc->SizeOfBlock);
|
||||
*ProcessedRelocsLength += BaseReloc->SizeOfBlock;
|
||||
}
|
||||
}
|
||||
|
@ -525,18 +533,20 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
StartOfRawData = 0;
|
||||
for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
|
||||
{
|
||||
if ((0 == StartOfRawData
|
||||
|| InSectionHeaders[Section].PointerToRawData < StartOfRawData)
|
||||
&& 0 != InSectionHeaders[Section].PointerToRawData
|
||||
&& 0 != (strncmp((char *) InSectionHeaders[Section].Name, ".stab", 5)))
|
||||
if ((StartOfRawData == 0 ||
|
||||
InSectionHeaders[Section].PointerToRawData < StartOfRawData)
|
||||
&& InSectionHeaders[Section].PointerToRawData != 0
|
||||
&& (strncmp((char *) InSectionHeaders[Section].Name, ".stab", 5)) != 0)
|
||||
{
|
||||
StartOfRawData = InSectionHeaders[Section].PointerToRawData;
|
||||
}
|
||||
}
|
||||
OutHeader = malloc(StartOfRawData);
|
||||
if (NULL == OutHeader)
|
||||
if (OutHeader == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate %u bytes for output file header\n", (unsigned int)StartOfRawData);
|
||||
fprintf(stderr,
|
||||
"Failed to allocate %u bytes for output file header\n",
|
||||
(unsigned int)StartOfRawData);
|
||||
return 1;
|
||||
}
|
||||
memset(OutHeader, '\0', StartOfRawData);
|
||||
|
@ -557,13 +567,17 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
|
||||
OutSectionHeaders = (PIMAGE_SECTION_HEADER)((char *) OutOptHeader + OutFileHeader->SizeOfOptionalHeader);
|
||||
|
||||
if (ProcessRelocations(&ProcessedRelocsLength, &ProcessedRelocs, InData, InOptHeader,
|
||||
InFileHeader->NumberOfSections, InSectionHeaders))
|
||||
if (ProcessRelocations(&ProcessedRelocsLength,
|
||||
&ProcessedRelocs,
|
||||
InData,
|
||||
InOptHeader,
|
||||
InFileHeader->NumberOfSections,
|
||||
InSectionHeaders))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (InOptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
|
||||
|| 0 == InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
|
||||
if (InOptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC ||
|
||||
InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress == 0)
|
||||
{
|
||||
InRelocSectionIndex = -1;
|
||||
}
|
||||
|
@ -580,7 +594,7 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
OutRelocSection = NULL;
|
||||
for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
|
||||
{
|
||||
if (0 != (strncmp((char *) InSectionHeaders[Section].Name, ".stab", 5)))
|
||||
if ((strncmp((char *) InSectionHeaders[Section].Name, ".stab", 5)) != 0)
|
||||
{
|
||||
*CurrentSectionHeader = InSectionHeaders[Section];
|
||||
CurrentSectionHeader->PointerToLinenumbers = 0;
|
||||
|
@ -600,7 +614,7 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
{
|
||||
OutRelocSection = CurrentSectionHeader;
|
||||
}
|
||||
(OutFileHeader->NumberOfSections) ++;
|
||||
(OutFileHeader->NumberOfSections)++;
|
||||
CurrentSectionHeader++;
|
||||
}
|
||||
}
|
||||
|
@ -617,11 +631,12 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
OutOptHeader->SectionAlignment);
|
||||
}
|
||||
OutRelocSection->Misc.VirtualSize = ProcessedRelocsLength;
|
||||
if (RosSymOffset == OutRelocSection->PointerToRawData
|
||||
+ OutRelocSection->SizeOfRawData)
|
||||
if (RosSymOffset == OutRelocSection->PointerToRawData +
|
||||
OutRelocSection->SizeOfRawData)
|
||||
{
|
||||
RosSymOffset = OutRelocSection->PointerToRawData +
|
||||
ROUND_UP(ProcessedRelocsLength, OutOptHeader->FileAlignment);
|
||||
ROUND_UP(ProcessedRelocsLength,
|
||||
OutOptHeader->FileAlignment);
|
||||
}
|
||||
OutRelocSection->SizeOfRawData = ROUND_UP(ProcessedRelocsLength,
|
||||
OutOptHeader->FileAlignment);
|
||||
|
@ -641,19 +656,22 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
CurrentSectionHeader->NumberOfLinenumbers = 0;
|
||||
CurrentSectionHeader->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
|
||||
| IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_TYPE_NOLOAD;
|
||||
OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress +
|
||||
CurrentSectionHeader->Misc.VirtualSize,
|
||||
OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress + CurrentSectionHeader->Misc.VirtualSize,
|
||||
OutOptHeader->SectionAlignment);
|
||||
(OutFileHeader->NumberOfSections)++;
|
||||
|
||||
PaddedRosSym = malloc(RosSymFileLength);
|
||||
if (NULL == PaddedRosSym)
|
||||
if (PaddedRosSym == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate %u bytes for padded .rossym\n", (unsigned int)RosSymFileLength);
|
||||
fprintf(stderr,
|
||||
"Failed to allocate %u bytes for padded .rossym\n",
|
||||
(unsigned int)RosSymFileLength);
|
||||
return 1;
|
||||
}
|
||||
memcpy(PaddedRosSym, RosSymSection, RosSymLength);
|
||||
memset((char *) PaddedRosSym + RosSymLength, '\0', RosSymFileLength - RosSymLength);
|
||||
memset((char *) PaddedRosSym + RosSymLength,
|
||||
'\0',
|
||||
RosSymFileLength - RosSymLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -703,7 +721,7 @@ CreateOutputFile(FILE *OutFile, void *InData,
|
|||
|
||||
for (Section = 0; Section < OutFileHeader->NumberOfSections; Section++)
|
||||
{
|
||||
if (0 != OutSectionHeaders[Section].SizeOfRawData)
|
||||
if (OutSectionHeaders[Section].SizeOfRawData != 0)
|
||||
{
|
||||
DWORD SizeOfRawData;
|
||||
fseek(OutFile, OutSectionHeaders[Section].PointerToRawData, SEEK_SET);
|
||||
|
@ -774,7 +792,7 @@ int main(int argc, char* argv[])
|
|||
void *RosSymSection;
|
||||
char elfhdr[4] = { '\177', 'E', 'L', 'F' };
|
||||
|
||||
if (3 != argc)
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
|
||||
exit(1);
|
||||
|
@ -783,16 +801,17 @@ int main(int argc, char* argv[])
|
|||
path1 = convert_path(argv[1]);
|
||||
path2 = convert_path(argv[2]);
|
||||
|
||||
FileData = load_file ( path1, &FileSize );
|
||||
if ( !FileData )
|
||||
FileData = load_file(path1, &FileSize);
|
||||
if (!FileData)
|
||||
{
|
||||
fprintf ( stderr, "An error occured loading '%s'\n", path1 );
|
||||
fprintf(stderr, "An error occured loading '%s'\n", path1);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Check if MZ header exists */
|
||||
PEDosHeader = (PIMAGE_DOS_HEADER) FileData;
|
||||
if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
|
||||
if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC ||
|
||||
PEDosHeader->e_lfanew == 0L)
|
||||
{
|
||||
/* Ignore elf */
|
||||
if (!memcmp(PEDosHeader, elfhdr, sizeof(elfhdr)))
|
||||
|
@ -814,15 +833,25 @@ int main(int argc, char* argv[])
|
|||
/* Locate PE section headers */
|
||||
PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
|
||||
|
||||
if (GetStabInfo(FileData, PEFileHeader, PESectionHeaders, &StabsLength, &StabBase,
|
||||
&StabStringsLength, &StabStringBase))
|
||||
if (GetStabInfo(FileData,
|
||||
PEFileHeader,
|
||||
PESectionHeaders,
|
||||
&StabsLength,
|
||||
&StabBase,
|
||||
&StabStringsLength,
|
||||
&StabStringBase))
|
||||
{
|
||||
free(FileData);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (GetCoffInfo(FileData, PEFileHeader, PESectionHeaders, &CoffsLength, &CoffBase,
|
||||
&CoffStringsLength, &CoffStringBase))
|
||||
if (GetCoffInfo(FileData,
|
||||
PEFileHeader,
|
||||
PESectionHeaders,
|
||||
&CoffsLength,
|
||||
&CoffBase,
|
||||
&CoffStringsLength,
|
||||
&CoffStringBase))
|
||||
{
|
||||
free(FileData);
|
||||
exit(1);
|
||||
|
@ -830,7 +859,7 @@ int main(int argc, char* argv[])
|
|||
|
||||
StringBase = malloc(1 + StabStringsLength + CoffStringsLength +
|
||||
(CoffsLength / sizeof(ROSSYM_ENTRY)) * (E_SYMNMLEN + 1));
|
||||
if (NULL == StringBase)
|
||||
if (StringBase == NULL)
|
||||
{
|
||||
free(FileData);
|
||||
fprintf(stderr, "Failed to allocate memory for strings table\n");
|
||||
|
@ -840,9 +869,17 @@ int main(int argc, char* argv[])
|
|||
*((char *) StringBase) = '\0';
|
||||
StringsLength = 1;
|
||||
|
||||
if (ConvertStabs(&StabSymbolsCount, &StabSymbols, &StringsLength, StringBase,
|
||||
StabsLength, StabBase, StabStringsLength, StabStringBase,
|
||||
ImageBase, PEFileHeader, PESectionHeaders))
|
||||
if (ConvertStabs(&StabSymbolsCount,
|
||||
&StabSymbols,
|
||||
&StringsLength,
|
||||
StringBase,
|
||||
StabsLength,
|
||||
StabBase,
|
||||
StabStringsLength,
|
||||
StabStringBase,
|
||||
ImageBase,
|
||||
PEFileHeader,
|
||||
PESectionHeaders))
|
||||
{
|
||||
free(StringBase);
|
||||
free(FileData);
|
||||
|
@ -850,9 +887,17 @@ int main(int argc, char* argv[])
|
|||
exit(1);
|
||||
}
|
||||
|
||||
if (ConvertCoffs(&CoffSymbolsCount, &CoffSymbols, &StringsLength, StringBase,
|
||||
CoffsLength, CoffBase, CoffStringsLength, CoffStringBase,
|
||||
ImageBase, PEFileHeader, PESectionHeaders))
|
||||
if (ConvertCoffs(&CoffSymbolsCount,
|
||||
&CoffSymbols,
|
||||
&StringsLength,
|
||||
StringBase,
|
||||
CoffsLength,
|
||||
CoffBase,
|
||||
CoffStringsLength,
|
||||
CoffStringBase,
|
||||
ImageBase,
|
||||
PEFileHeader,
|
||||
PESectionHeaders))
|
||||
{
|
||||
if (StabSymbols)
|
||||
{
|
||||
|
@ -863,9 +908,12 @@ int main(int argc, char* argv[])
|
|||
exit(1);
|
||||
}
|
||||
|
||||
if (MergeStabsAndCoffs(&MergedSymbolsCount, &MergedSymbols,
|
||||
StabSymbolsCount, StabSymbols,
|
||||
CoffSymbolsCount, CoffSymbols))
|
||||
if (MergeStabsAndCoffs(&MergedSymbolsCount,
|
||||
&MergedSymbols,
|
||||
StabSymbolsCount,
|
||||
StabSymbols,
|
||||
CoffSymbolsCount,
|
||||
CoffSymbols))
|
||||
{
|
||||
if (CoffSymbols)
|
||||
{
|
||||
|
@ -895,10 +943,12 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
else
|
||||
{
|
||||
RosSymLength = sizeof(SYMBOLFILE_HEADER) + MergedSymbolsCount * sizeof(ROSSYM_ENTRY)
|
||||
+ StringsLength;
|
||||
RosSymLength = sizeof(SYMBOLFILE_HEADER) +
|
||||
MergedSymbolsCount * sizeof(ROSSYM_ENTRY) +
|
||||
StringsLength;
|
||||
|
||||
RosSymSection = malloc(RosSymLength);
|
||||
if (NULL == RosSymSection)
|
||||
if (RosSymSection == NULL)
|
||||
{
|
||||
free(MergedSymbols);
|
||||
free(StringBase);
|
||||
|
@ -908,19 +958,24 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
memset(RosSymSection, '\0', RosSymLength);
|
||||
|
||||
SymbolFileHeader = (PSYMBOLFILE_HEADER) RosSymSection;
|
||||
SymbolFileHeader = (PSYMBOLFILE_HEADER)RosSymSection;
|
||||
SymbolFileHeader->SymbolsOffset = sizeof(SYMBOLFILE_HEADER);
|
||||
SymbolFileHeader->SymbolsLength = MergedSymbolsCount * sizeof(ROSSYM_ENTRY);
|
||||
SymbolFileHeader->StringsOffset = SymbolFileHeader->SymbolsOffset + SymbolFileHeader->SymbolsLength;
|
||||
SymbolFileHeader->StringsOffset = SymbolFileHeader->SymbolsOffset +
|
||||
SymbolFileHeader->SymbolsLength;
|
||||
SymbolFileHeader->StringsLength = StringsLength;
|
||||
|
||||
memcpy((char *) RosSymSection + SymbolFileHeader->SymbolsOffset, MergedSymbols,
|
||||
memcpy((char *) RosSymSection + SymbolFileHeader->SymbolsOffset,
|
||||
MergedSymbols,
|
||||
SymbolFileHeader->SymbolsLength);
|
||||
memcpy((char *) RosSymSection + SymbolFileHeader->StringsOffset, StringBase,
|
||||
|
||||
memcpy((char *) RosSymSection + SymbolFileHeader->StringsOffset,
|
||||
StringBase,
|
||||
SymbolFileHeader->StringsLength);
|
||||
|
||||
free(MergedSymbols);
|
||||
}
|
||||
|
||||
free(StringBase);
|
||||
out = fopen(path2, "wb");
|
||||
if (out == NULL)
|
||||
|
@ -931,8 +986,14 @@ int main(int argc, char* argv[])
|
|||
exit(1);
|
||||
}
|
||||
|
||||
if (CreateOutputFile(out, FileData, PEDosHeader, PEFileHeader, PEOptHeader,
|
||||
PESectionHeaders, RosSymLength, RosSymSection))
|
||||
if (CreateOutputFile(out,
|
||||
FileData,
|
||||
PEDosHeader,
|
||||
PEFileHeader,
|
||||
PEOptHeader,
|
||||
PESectionHeaders,
|
||||
RosSymLength,
|
||||
RosSymSection))
|
||||
{
|
||||
fclose(out);
|
||||
if (RosSymSection)
|
||||
|
|
Loading…
Reference in a new issue