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