2 * Usage: rsym input-file output-file
4 * There are two sources of information: the .stab/.stabstr
5 * sections of the executable and the COFF symbol table. Most
6 * of the information is in the .stab/.stabstr sections.
7 * However, most of our asm files don't contain .stab directives,
8 * so routines implemented in assembler won't show up in the
9 * .stab section. They are present in the COFF symbol table.
10 * So, we mostly use the .stab/.stabstr sections, but we augment
11 * the info there with info from the COFF symbol table when
14 * This is a tool and is compiled using the host compiler,
15 * i.e. on Linux gcc and not mingw-gcc (cross-compiler).
16 * Therefore we can't include SDK headers and we have to
17 * duplicate some definitions here.
18 * Also note that the internal functions are "old C-style",
19 * returning an int, where a return of 0 means success and
20 * non-zero is failure.
31 CompareSymEntry(const PROSSYM_ENTRY SymEntry1
, const PROSSYM_ENTRY SymEntry2
)
33 if (SymEntry1
->Address
< SymEntry2
->Address
)
38 if (SymEntry2
->Address
< SymEntry1
->Address
)
47 GetStabInfo(void *FileData
, PIMAGE_FILE_HEADER PEFileHeader
,
48 PIMAGE_SECTION_HEADER PESectionHeaders
,
49 ULONG
*StabSymbolsLength
, void **StabSymbolsBase
,
50 ULONG
*StabStringsLength
, void **StabStringsBase
)
54 /* Load .stab and .stabstr sections if available */
55 *StabSymbolsBase
= NULL
;
56 *StabSymbolsLength
= 0;
57 *StabStringsBase
= NULL
;
58 *StabStringsLength
= 0;
60 for (Idx
= 0; Idx
< PEFileHeader
->NumberOfSections
; Idx
++)
62 /* printf("section: '%.08s'\n", PESectionHeaders[Idx].Name); */
63 if ((strncmp((char*)PESectionHeaders
[Idx
].Name
, ".stab", 5) == 0)
64 && (PESectionHeaders
[Idx
].Name
[5] == 0))
66 /* printf(".stab section found. Size %d\n",
67 PESectionHeaders[Idx].SizeOfRawData); */
69 *StabSymbolsLength
= PESectionHeaders
[Idx
].SizeOfRawData
;
70 *StabSymbolsBase
= (void *)((char *) FileData
+ PESectionHeaders
[Idx
].PointerToRawData
);
73 if (strncmp((char*)PESectionHeaders
[Idx
].Name
, ".stabstr", 8) == 0)
75 /* printf(".stabstr section found. Size %d\n",
76 PESectionHeaders[Idx].SizeOfRawData); */
78 *StabStringsLength
= PESectionHeaders
[Idx
].SizeOfRawData
;
79 *StabStringsBase
= (void *)((char *) FileData
+ PESectionHeaders
[Idx
].PointerToRawData
);
87 GetCoffInfo(void *FileData
, PIMAGE_FILE_HEADER PEFileHeader
,
88 PIMAGE_SECTION_HEADER PESectionHeaders
,
89 ULONG
*CoffSymbolsLength
, void **CoffSymbolsBase
,
90 ULONG
*CoffStringsLength
, void **CoffStringsBase
)
93 if (0 == PEFileHeader
->PointerToSymbolTable
|| 0 == PEFileHeader
->NumberOfSymbols
)
95 /* No COFF symbol table */
96 *CoffSymbolsLength
= 0;
97 *CoffStringsLength
= 0;
101 *CoffSymbolsLength
= PEFileHeader
->NumberOfSymbols
* sizeof(COFF_SYMENT
);
102 *CoffSymbolsBase
= (void *)((char *) FileData
+ PEFileHeader
->PointerToSymbolTable
);
103 *CoffStringsLength
= *((ULONG
*) ((char *) *CoffSymbolsBase
+ *CoffSymbolsLength
));
104 *CoffStringsBase
= (void *)((char *) *CoffSymbolsBase
+ *CoffSymbolsLength
);
111 FindOrAddString(char *StringToFind
, ULONG
*StringsLength
, void *StringsBase
)
115 Search
= (char *) StringsBase
;
116 End
= Search
+ *StringsLength
;
120 if (0 == strcmp(Search
, StringToFind
))
122 return Search
- (char *) StringsBase
;
124 Search
+= strlen(Search
) + 1;
127 strcpy(Search
, StringToFind
);
128 *StringsLength
+= strlen(StringToFind
) + 1;
130 return Search
- (char *) StringsBase
;
134 ConvertStabs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
135 ULONG
*StringsLength
, void *StringsBase
,
136 ULONG StabSymbolsLength
, void *StabSymbolsBase
,
137 ULONG StabStringsLength
, void *StabStringsBase
,
138 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
139 PIMAGE_SECTION_HEADER PESectionHeaders
)
141 PSTAB_ENTRY StabEntry
;
143 ULONG_PTR Address
, LastFunctionAddress
;
148 StabEntry
= StabSymbolsBase
;
149 Count
= StabSymbolsLength
/ sizeof(STAB_ENTRY
);
158 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
159 if (NULL
== *SymbolsBase
)
161 fprintf(stderr
, "Failed to allocate memory for converted .stab symbols\n");
165 LastFunctionAddress
= 0;
167 for (i
= 0; i
< Count
; i
++)
169 switch (StabEntry
[i
].n_type
)
174 Name
= (char *) StabStringsBase
+ StabEntry
[i
].n_strx
;
175 if (StabStringsLength
< StabEntry
[i
].n_strx
176 ||'\0' == *Name
|| '/' == Name
[strlen(Name
) - 1]
177 || '\\' == Name
[strlen(Name
) - 1]
178 || StabEntry
[i
].n_value
< ImageBase
)
182 Address
= StabEntry
[i
].n_value
- ImageBase
;
183 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
187 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
188 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= FindOrAddString((char *) StabStringsBase
189 + StabEntry
[i
].n_strx
,
192 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= 0;
193 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
194 LastFunctionAddress
= 0;
197 if (0 == StabEntry
[i
].n_desc
|| StabEntry
[i
].n_value
< ImageBase
) /* line # 0 isn't valid */
201 Address
= StabEntry
[i
].n_value
- ImageBase
;
202 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
205 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
207 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
208 if (sizeof(FuncName
) <= strlen((char *) StabStringsBase
+ StabEntry
[i
].n_strx
))
211 fprintf(stderr
, "Function name too long\n");
214 strcpy(FuncName
, (char *) StabStringsBase
+ StabEntry
[i
].n_strx
);
215 Name
= strchr(FuncName
, ':');
220 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(FuncName
,
223 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
224 LastFunctionAddress
= Address
;
227 if (0 == LastFunctionAddress
)
229 Address
= StabEntry
[i
].n_value
- ImageBase
;
233 Address
= LastFunctionAddress
+ StabEntry
[i
].n_value
;
235 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
238 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
239 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FunctionOffset
;
241 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
242 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= StabEntry
[i
].n_desc
;
251 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
257 ConvertCoffs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
258 ULONG
*StringsLength
, void *StringsBase
,
259 ULONG CoffSymbolsLength
, void *CoffSymbolsBase
,
260 ULONG CoffStringsLength
, void *CoffStringsBase
,
261 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
262 PIMAGE_SECTION_HEADER PESectionHeaders
)
265 PCOFF_SYMENT CoffEntry
;
269 CoffEntry
= (PCOFF_SYMENT
) CoffSymbolsBase
;
270 Count
= CoffSymbolsLength
/ sizeof(COFF_SYMENT
);
271 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
272 if (NULL
== *SymbolsBase
)
274 fprintf(stderr
, "Unable to allocate memory for converted COFF symbols\n");
279 for (i
= 0; i
< Count
; i
++)
281 if (ISFCN(CoffEntry
[i
].e_type
) || C_EXT
== CoffEntry
[i
].e_sclass
)
283 (*SymbolsBase
)[*SymbolsCount
].Address
= CoffEntry
[i
].e_value
;
284 if (0 < CoffEntry
[i
].e_scnum
)
286 if (PEFileHeader
->NumberOfSections
< CoffEntry
[i
].e_scnum
)
289 fprintf(stderr
, "Invalid section number %d in COFF symbols (only %d sections present)\n",
290 CoffEntry
[i
].e_scnum
, PEFileHeader
->NumberOfSections
);
293 (*SymbolsBase
)[*SymbolsCount
].Address
+= PESectionHeaders
[CoffEntry
[i
].e_scnum
- 1].VirtualAddress
;
295 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= 0;
296 if (0 == CoffEntry
[i
].e
.e
.e_zeroes
)
298 if (sizeof(FuncName
) <= strlen((char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
))
301 fprintf(stderr
, "Function name too long\n");
304 strcpy(FuncName
, (char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
);
308 memcpy(FuncName
, CoffEntry
[i
].e
.e_name
, E_SYMNMLEN
);
309 FuncName
[E_SYMNMLEN
] = '\0';
312 /* Name demangling: stdcall */
313 p
= strrchr(FuncName
, '@');
318 p
= ('_' == FuncName
[0] || '@' == FuncName
[0] ? FuncName
+ 1 : FuncName
);
319 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(p
,
322 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
325 i
+= CoffEntry
[i
].e_numaux
;
328 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
334 MergeStabsAndCoffs(ULONG
*MergedSymbolCount
, PROSSYM_ENTRY
*MergedSymbols
,
335 ULONG StabSymbolsCount
, PROSSYM_ENTRY StabSymbols
,
336 ULONG CoffSymbolsCount
, PROSSYM_ENTRY CoffSymbols
)
340 ULONG_PTR StabFunctionStartAddress
;
341 ULONG StabFunctionStringOffset
, NewStabFunctionStringOffset
;
343 *MergedSymbolCount
= 0;
344 if (StabSymbolsCount
== 0)
346 *MergedSymbols
= NULL
;
349 *MergedSymbols
= malloc(StabSymbolsCount
* sizeof(ROSSYM_ENTRY
));
350 if (NULL
== *MergedSymbols
)
352 fprintf(stderr
, "Unable to allocate memory for merged symbols\n");
356 StabFunctionStartAddress
= 0;
357 StabFunctionStringOffset
= 0;
359 for (StabIndex
= 0; StabIndex
< StabSymbolsCount
; StabIndex
++)
361 (*MergedSymbols
)[*MergedSymbolCount
] = StabSymbols
[StabIndex
];
362 for (j
= StabIndex
+ 1;
363 j
< StabSymbolsCount
&& StabSymbols
[j
].Address
== StabSymbols
[StabIndex
].Address
;
366 if (0 != StabSymbols
[j
].FileOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
)
368 (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
= StabSymbols
[j
].FileOffset
;
370 if (0 != StabSymbols
[j
].FunctionOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
)
372 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= StabSymbols
[j
].FunctionOffset
;
374 if (0 != StabSymbols
[j
].SourceLine
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
)
376 (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
= StabSymbols
[j
].SourceLine
;
380 while (CoffIndex
< CoffSymbolsCount
381 && CoffSymbols
[CoffIndex
+ 1].Address
<= (*MergedSymbols
)[*MergedSymbolCount
].Address
)
385 NewStabFunctionStringOffset
= (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
;
386 if (CoffSymbolsCount
> 0 &&
387 CoffSymbols
[CoffIndex
].Address
< (*MergedSymbols
)[*MergedSymbolCount
].Address
388 && StabFunctionStartAddress
< CoffSymbols
[CoffIndex
].Address
389 && 0 != CoffSymbols
[CoffIndex
].FunctionOffset
)
391 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= CoffSymbols
[CoffIndex
].FunctionOffset
;
393 if (StabFunctionStringOffset
!= NewStabFunctionStringOffset
)
395 StabFunctionStartAddress
= (*MergedSymbols
)[*MergedSymbolCount
].Address
;
397 StabFunctionStringOffset
= NewStabFunctionStringOffset
;
398 (*MergedSymbolCount
)++;
404 static PIMAGE_SECTION_HEADER
405 FindSectionForRVA(DWORD RVA
, unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
409 for (Section
= 0; Section
< NumberOfSections
; Section
++)
411 if (SectionHeaders
[Section
].VirtualAddress
<= RVA
&&
412 RVA
< SectionHeaders
[Section
].VirtualAddress
+ SectionHeaders
[Section
].Misc
.VirtualSize
)
414 return SectionHeaders
+ Section
;
422 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader
)
424 static char *BlacklistedSections
[] =
430 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
433 if (0 != (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
))
438 for (i
= 0; i
< sizeof(BlacklistedSections
) / sizeof(BlacklistedSections
[0]); i
++)
440 strncpy(SectionName
, BlacklistedSections
[i
], IMAGE_SIZEOF_SHORT_NAME
);
441 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
451 ProcessRelocations(ULONG
*ProcessedRelocsLength
, void **ProcessedRelocs
,
452 void *RawData
, PIMAGE_OPTIONAL_HEADER OptHeader
,
453 unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
455 PIMAGE_SECTION_HEADER RelocSectionHeader
, TargetSectionHeader
;
456 PIMAGE_BASE_RELOCATION BaseReloc
, End
, AcceptedRelocs
;
459 if (OptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
460 || 0 == OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
462 /* No relocation entries */
463 *ProcessedRelocsLength
= 0;
464 *ProcessedRelocs
= NULL
;
468 RelocSectionHeader
= FindSectionForRVA(OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
469 NumberOfSections
, SectionHeaders
);
470 if (NULL
== RelocSectionHeader
)
472 fprintf(stderr
, "Can't find section header for relocation data\n");
476 *ProcessedRelocs
= malloc(RelocSectionHeader
->SizeOfRawData
);
477 if (NULL
== *ProcessedRelocs
)
479 fprintf(stderr
, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader
->SizeOfRawData
);
482 *ProcessedRelocsLength
= 0;
484 BaseReloc
= (PIMAGE_BASE_RELOCATION
) ((char *) RawData
485 + RelocSectionHeader
->PointerToRawData
486 + (OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
-
487 RelocSectionHeader
->VirtualAddress
));
488 End
= (PIMAGE_BASE_RELOCATION
) ((char *) BaseReloc
489 + OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
491 while (BaseReloc
< End
&& 0 < BaseReloc
->SizeOfBlock
)
493 TargetSectionHeader
= FindSectionForRVA(BaseReloc
->VirtualAddress
, NumberOfSections
,
495 if (NULL
!= TargetSectionHeader
&& IncludeRelocationsForSection(TargetSectionHeader
))
497 AcceptedRelocs
= *ProcessedRelocs
;
499 while (AcceptedRelocs
< (PIMAGE_BASE_RELOCATION
) ((char *) *ProcessedRelocs
+
500 *ProcessedRelocsLength
)
503 Found
= BaseReloc
->SizeOfBlock
== AcceptedRelocs
->SizeOfBlock
504 && 0 == memcmp(BaseReloc
, AcceptedRelocs
, AcceptedRelocs
->SizeOfBlock
);
505 AcceptedRelocs
= (PIMAGE_BASE_RELOCATION
) ((char *) AcceptedRelocs
+
506 AcceptedRelocs
->SizeOfBlock
);
510 memcpy((char *) *ProcessedRelocs
+ *ProcessedRelocsLength
,
511 BaseReloc
, BaseReloc
->SizeOfBlock
);
512 *ProcessedRelocsLength
+= BaseReloc
->SizeOfBlock
;
515 BaseReloc
= (PIMAGE_BASE_RELOCATION
)((char *) BaseReloc
+ BaseReloc
->SizeOfBlock
);
522 CreateOutputFile(FILE *OutFile
, void *InData
,
523 PIMAGE_DOS_HEADER InDosHeader
, PIMAGE_FILE_HEADER InFileHeader
,
524 PIMAGE_OPTIONAL_HEADER InOptHeader
, PIMAGE_SECTION_HEADER InSectionHeaders
,
525 ULONG RosSymLength
, void *RosSymSection
)
527 ULONG StartOfRawData
;
529 void *OutHeader
, *ProcessedRelocs
, *PaddedRosSym
, *Data
;
530 PIMAGE_DOS_HEADER OutDosHeader
;
531 PIMAGE_FILE_HEADER OutFileHeader
;
532 PIMAGE_OPTIONAL_HEADER OutOptHeader
;
533 PIMAGE_SECTION_HEADER OutSectionHeaders
, CurrentSectionHeader
;
536 ULONG ProcessedRelocsLength
;
537 ULONG RosSymOffset
, RosSymFileLength
;
538 int InRelocSectionIndex
;
539 PIMAGE_SECTION_HEADER OutRelocSection
;
542 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
544 if ((0 == StartOfRawData
545 || InSectionHeaders
[Section
].PointerToRawData
< StartOfRawData
)
546 && 0 != InSectionHeaders
[Section
].PointerToRawData
547 && 0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
549 StartOfRawData
= InSectionHeaders
[Section
].PointerToRawData
;
552 OutHeader
= malloc(StartOfRawData
);
553 if (NULL
== OutHeader
)
555 fprintf(stderr
, "Failed to allocate %lu bytes for output file header\n", StartOfRawData
);
558 memset(OutHeader
, '\0', StartOfRawData
);
560 OutDosHeader
= (PIMAGE_DOS_HEADER
) OutHeader
;
561 memcpy(OutDosHeader
, InDosHeader
, InDosHeader
->e_lfanew
+ sizeof(ULONG
));
563 OutFileHeader
= (PIMAGE_FILE_HEADER
)((char *) OutHeader
+ OutDosHeader
->e_lfanew
+ sizeof(ULONG
));
564 memcpy(OutFileHeader
, InFileHeader
, sizeof(IMAGE_FILE_HEADER
));
565 OutFileHeader
->PointerToSymbolTable
= 0;
566 OutFileHeader
->NumberOfSymbols
= 0;
567 OutFileHeader
->Characteristics
&= ~(IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
568 IMAGE_FILE_DEBUG_STRIPPED
);
570 OutOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(OutFileHeader
+ 1);
571 memcpy(OutOptHeader
, InOptHeader
, sizeof(IMAGE_OPTIONAL_HEADER
));
572 OutOptHeader
->CheckSum
= 0;
574 OutSectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) OutOptHeader
+ OutFileHeader
->SizeOfOptionalHeader
);
576 if (ProcessRelocations(&ProcessedRelocsLength
, &ProcessedRelocs
, InData
, InOptHeader
,
577 InFileHeader
->NumberOfSections
, InSectionHeaders
))
581 if (InOptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
582 || 0 == InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
584 InRelocSectionIndex
= -1;
588 InRelocSectionIndex
= FindSectionForRVA(InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
589 InFileHeader
->NumberOfSections
, InSectionHeaders
) - InSectionHeaders
;
592 OutFileHeader
->NumberOfSections
= 0;
593 CurrentSectionHeader
= OutSectionHeaders
;
594 OutOptHeader
->SizeOfImage
= 0;
596 OutRelocSection
= NULL
;
597 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
599 if (0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
601 *CurrentSectionHeader
= InSectionHeaders
[Section
];
602 CurrentSectionHeader
->PointerToLinenumbers
= 0;
603 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
604 if (OutOptHeader
->SizeOfImage
< CurrentSectionHeader
->VirtualAddress
+
605 CurrentSectionHeader
->Misc
.VirtualSize
)
607 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
608 CurrentSectionHeader
->Misc
.VirtualSize
,
609 OutOptHeader
->SectionAlignment
);
611 if (RosSymOffset
< CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
)
613 RosSymOffset
= CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
;
615 if (Section
== (ULONG
)InRelocSectionIndex
)
617 OutRelocSection
= CurrentSectionHeader
;
619 (OutFileHeader
->NumberOfSections
) ++;
620 CurrentSectionHeader
++;
624 if (OutRelocSection
== CurrentSectionHeader
- 1)
626 OutOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= ProcessedRelocsLength
;
627 if (OutOptHeader
->SizeOfImage
== OutRelocSection
->VirtualAddress
+
628 ROUND_UP(OutRelocSection
->Misc
.VirtualSize
,
629 OutOptHeader
->SectionAlignment
))
631 OutOptHeader
->SizeOfImage
= OutRelocSection
->VirtualAddress
+
632 ROUND_UP(ProcessedRelocsLength
,
633 OutOptHeader
->SectionAlignment
);
635 OutRelocSection
->Misc
.VirtualSize
= ProcessedRelocsLength
;
636 if (RosSymOffset
== OutRelocSection
->PointerToRawData
637 + OutRelocSection
->SizeOfRawData
)
639 RosSymOffset
= OutRelocSection
->PointerToRawData
+
640 ROUND_UP(ProcessedRelocsLength
, OutOptHeader
->FileAlignment
);
642 OutRelocSection
->SizeOfRawData
= ROUND_UP(ProcessedRelocsLength
,
643 OutOptHeader
->FileAlignment
);
646 if (RosSymLength
> 0)
648 RosSymFileLength
= ROUND_UP(RosSymLength
, OutOptHeader
->FileAlignment
);
649 memcpy(CurrentSectionHeader
->Name
, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
650 CurrentSectionHeader
->Misc
.VirtualSize
= RosSymLength
;
651 CurrentSectionHeader
->VirtualAddress
= OutOptHeader
->SizeOfImage
;
652 CurrentSectionHeader
->SizeOfRawData
= RosSymFileLength
;
653 CurrentSectionHeader
->PointerToRawData
= RosSymOffset
;
654 CurrentSectionHeader
->PointerToRelocations
= 0;
655 CurrentSectionHeader
->PointerToLinenumbers
= 0;
656 CurrentSectionHeader
->NumberOfRelocations
= 0;
657 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
658 CurrentSectionHeader
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_DISCARDABLE
659 | IMAGE_SCN_LNK_REMOVE
| IMAGE_SCN_TYPE_NOLOAD
;
660 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
661 CurrentSectionHeader
->Misc
.VirtualSize
,
662 OutOptHeader
->SectionAlignment
);
663 (OutFileHeader
->NumberOfSections
)++;
665 PaddedRosSym
= malloc(RosSymFileLength
);
666 if (NULL
== PaddedRosSym
)
668 fprintf(stderr
, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength
);
671 memcpy(PaddedRosSym
, RosSymSection
, RosSymLength
);
672 memset((char *) PaddedRosSym
+ RosSymLength
, '\0', RosSymFileLength
- RosSymLength
);
679 for (i
= 0; i
< StartOfRawData
/ 2; i
++)
681 CheckSum
+= ((unsigned short*) OutHeader
)[i
];
682 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
684 Length
= StartOfRawData
;
685 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
687 if (OutRelocSection
== OutSectionHeaders
+ Section
)
689 Data
= (void *) ProcessedRelocs
;
691 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
693 Data
= (void *) PaddedRosSym
;
697 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
699 for (i
= 0; i
< OutSectionHeaders
[Section
].SizeOfRawData
/ 2; i
++)
701 CheckSum
+= ((unsigned short*) Data
)[i
];
702 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
704 Length
+= OutSectionHeaders
[Section
].SizeOfRawData
;
707 OutOptHeader
->CheckSum
= CheckSum
;
709 if (fwrite(OutHeader
, 1, StartOfRawData
, OutFile
) != StartOfRawData
)
711 perror("Error writing output header\n");
716 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
718 if (0 != OutSectionHeaders
[Section
].SizeOfRawData
)
720 fseek(OutFile
, OutSectionHeaders
[Section
].PointerToRawData
, SEEK_SET
);
721 if (OutRelocSection
== OutSectionHeaders
+ Section
)
723 Data
= (void *) ProcessedRelocs
;
725 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
727 Data
= (void *) PaddedRosSym
;
731 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
733 if (fwrite(Data
, 1, OutSectionHeaders
[Section
].SizeOfRawData
, OutFile
) !=
734 OutSectionHeaders
[Section
].SizeOfRawData
)
736 perror("Error writing section data\n");
753 int main(int argc
, char* argv
[])
755 PSYMBOLFILE_HEADER SymbolFileHeader
;
756 PIMAGE_DOS_HEADER PEDosHeader
;
757 PIMAGE_FILE_HEADER PEFileHeader
;
758 PIMAGE_OPTIONAL_HEADER PEOptHeader
;
759 PIMAGE_SECTION_HEADER PESectionHeaders
;
763 void *StabStringBase
;
764 ULONG StabStringsLength
;
765 void *CoffBase
= NULL
;
767 void *CoffStringBase
= NULL
;
768 ULONG CoffStringsLength
;
774 ULONG StabSymbolsCount
;
775 PROSSYM_ENTRY StabSymbols
;
776 ULONG CoffSymbolsCount
;
777 PROSSYM_ENTRY CoffSymbols
;
778 ULONG MergedSymbolsCount
;
779 PROSSYM_ENTRY MergedSymbols
;
787 fprintf(stderr
, "Usage: rsym <exefile> <symfile>\n");
791 path1
= convert_path(argv
[1]);
792 path2
= convert_path(argv
[2]);
794 FileData
= load_file ( path1
, &FileSize
);
797 fprintf ( stderr
, "An error occured loading '%s'\n", path1
);
801 /* Check if MZ header exists */
802 PEDosHeader
= (PIMAGE_DOS_HEADER
) FileData
;
803 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
|| PEDosHeader
->e_lfanew
== 0L)
805 perror("Input file is not a PE image.\n");
810 /* Locate PE file header */
811 /* sizeof(ULONG) = sizeof(MAGIC) */
812 PEFileHeader
= (PIMAGE_FILE_HEADER
)((char *) FileData
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
814 /* Locate optional header */
815 assert(sizeof(ULONG
) == 4);
816 PEOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(PEFileHeader
+ 1);
817 ImageBase
= PEOptHeader
->ImageBase
;
819 /* Locate PE section headers */
820 PESectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) PEOptHeader
+ PEFileHeader
->SizeOfOptionalHeader
);
822 if (GetStabInfo(FileData
, PEFileHeader
, PESectionHeaders
, &StabsLength
, &StabBase
,
823 &StabStringsLength
, &StabStringBase
))
829 if (GetCoffInfo(FileData
, PEFileHeader
, PESectionHeaders
, &CoffsLength
, &CoffBase
,
830 &CoffStringsLength
, &CoffStringBase
))
836 StringBase
= malloc(1 + StabStringsLength
+ CoffStringsLength
+
837 (CoffsLength
/ sizeof(ROSSYM_ENTRY
)) * (E_SYMNMLEN
+ 1));
838 if (NULL
== StringBase
)
841 fprintf(stderr
, "Failed to allocate memory for strings table\n");
844 /* Make offset 0 into an empty string */
845 *((char *) StringBase
) = '\0';
848 if (ConvertStabs(&StabSymbolsCount
, &StabSymbols
, &StringsLength
, StringBase
,
849 StabsLength
, StabBase
, StabStringsLength
, StabStringBase
,
850 ImageBase
, PEFileHeader
, PESectionHeaders
))
854 fprintf(stderr
, "Failed to allocate memory for strings table\n");
858 if (ConvertCoffs(&CoffSymbolsCount
, &CoffSymbols
, &StringsLength
, StringBase
,
859 CoffsLength
, CoffBase
, CoffStringsLength
, CoffStringBase
,
860 ImageBase
, PEFileHeader
, PESectionHeaders
))
871 if (MergeStabsAndCoffs(&MergedSymbolsCount
, &MergedSymbols
,
872 StabSymbolsCount
, StabSymbols
,
873 CoffSymbolsCount
, CoffSymbols
))
896 if (MergedSymbolsCount
== 0)
899 RosSymSection
= NULL
;
903 RosSymLength
= sizeof(SYMBOLFILE_HEADER
) + MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
)
905 RosSymSection
= malloc(RosSymLength
);
906 if (NULL
== RosSymSection
)
911 fprintf(stderr
, "Unable to allocate memory for .rossym section\n");
914 memset(RosSymSection
, '\0', RosSymLength
);
916 SymbolFileHeader
= (PSYMBOLFILE_HEADER
) RosSymSection
;
917 SymbolFileHeader
->SymbolsOffset
= sizeof(SYMBOLFILE_HEADER
);
918 SymbolFileHeader
->SymbolsLength
= MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
);
919 SymbolFileHeader
->StringsOffset
= SymbolFileHeader
->SymbolsOffset
+ SymbolFileHeader
->SymbolsLength
;
920 SymbolFileHeader
->StringsLength
= StringsLength
;
922 memcpy((char *) RosSymSection
+ SymbolFileHeader
->SymbolsOffset
, MergedSymbols
,
923 SymbolFileHeader
->SymbolsLength
);
924 memcpy((char *) RosSymSection
+ SymbolFileHeader
->StringsOffset
, StringBase
,
925 SymbolFileHeader
->StringsLength
);
930 out
= fopen(path2
, "wb");
933 perror("Cannot open output file");
939 if (CreateOutputFile(out
, FileData
, PEDosHeader
, PEFileHeader
, PEOptHeader
,
940 PESectionHeaders
, RosSymLength
, RosSymSection
))