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.
30 CompareSymEntry(const PROSSYM_ENTRY SymEntry1
, const PROSSYM_ENTRY SymEntry2
)
32 if (SymEntry1
->Address
< SymEntry2
->Address
)
37 if (SymEntry2
->Address
< SymEntry1
->Address
)
46 GetStabInfo(void *FileData
, PIMAGE_FILE_HEADER PEFileHeader
,
47 PIMAGE_SECTION_HEADER PESectionHeaders
,
48 ULONG
*StabSymbolsLength
, void **StabSymbolsBase
,
49 ULONG
*StabStringsLength
, void **StabStringsBase
)
53 /* Load .stab and .stabstr sections if available */
54 *StabSymbolsBase
= NULL
;
55 *StabSymbolsLength
= 0;
56 *StabStringsBase
= NULL
;
57 *StabStringsLength
= 0;
59 for (Idx
= 0; Idx
< PEFileHeader
->NumberOfSections
; Idx
++)
61 /* printf("section: '%.08s'\n", PESectionHeaders[Idx].Name); */
62 if ((strncmp((char*)PESectionHeaders
[Idx
].Name
, ".stab", 5) == 0)
63 && (PESectionHeaders
[Idx
].Name
[5] == 0))
65 /* printf(".stab section found. Size %d\n",
66 PESectionHeaders[Idx].SizeOfRawData); */
68 *StabSymbolsLength
= PESectionHeaders
[Idx
].SizeOfRawData
;
69 *StabSymbolsBase
= (void *)((char *) FileData
+ PESectionHeaders
[Idx
].PointerToRawData
);
72 if (strncmp((char*)PESectionHeaders
[Idx
].Name
, ".stabstr", 8) == 0)
74 /* printf(".stabstr section found. Size %d\n",
75 PESectionHeaders[Idx].SizeOfRawData); */
77 *StabStringsLength
= PESectionHeaders
[Idx
].SizeOfRawData
;
78 *StabStringsBase
= (void *)((char *) FileData
+ PESectionHeaders
[Idx
].PointerToRawData
);
86 GetCoffInfo(void *FileData
, PIMAGE_FILE_HEADER PEFileHeader
,
87 PIMAGE_SECTION_HEADER PESectionHeaders
,
88 ULONG
*CoffSymbolsLength
, void **CoffSymbolsBase
,
89 ULONG
*CoffStringsLength
, void **CoffStringsBase
)
92 if (0 == PEFileHeader
->PointerToSymbolTable
|| 0 == PEFileHeader
->NumberOfSymbols
)
94 /* No COFF symbol table */
95 *CoffSymbolsLength
= 0;
96 *CoffStringsLength
= 0;
100 *CoffSymbolsLength
= PEFileHeader
->NumberOfSymbols
* sizeof(COFF_SYMENT
);
101 *CoffSymbolsBase
= (void *)((char *) FileData
+ PEFileHeader
->PointerToSymbolTable
);
102 *CoffStringsLength
= *((ULONG
*) ((char *) *CoffSymbolsBase
+ *CoffSymbolsLength
));
103 *CoffStringsBase
= (void *)((char *) *CoffSymbolsBase
+ *CoffSymbolsLength
);
110 FindOrAddString(char *StringToFind
, ULONG
*StringsLength
, void *StringsBase
)
114 Search
= (char *) StringsBase
;
115 End
= Search
+ *StringsLength
;
119 if (0 == strcmp(Search
, StringToFind
))
121 return Search
- (char *) StringsBase
;
123 Search
+= strlen(Search
) + 1;
126 strcpy(Search
, StringToFind
);
127 *StringsLength
+= strlen(StringToFind
) + 1;
129 return Search
- (char *) StringsBase
;
133 ConvertStabs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
134 ULONG
*StringsLength
, void *StringsBase
,
135 ULONG StabSymbolsLength
, void *StabSymbolsBase
,
136 ULONG StabStringsLength
, void *StabStringsBase
,
137 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
138 PIMAGE_SECTION_HEADER PESectionHeaders
)
140 PSTAB_ENTRY StabEntry
;
142 ULONG_PTR Address
, LastFunctionAddress
;
147 StabEntry
= StabSymbolsBase
;
148 Count
= StabSymbolsLength
/ sizeof(STAB_ENTRY
);
157 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
158 if (NULL
== *SymbolsBase
)
160 fprintf(stderr
, "Failed to allocate memory for converted .stab symbols\n");
164 LastFunctionAddress
= 0;
166 for (i
= 0; i
< Count
; i
++)
168 switch (StabEntry
[i
].n_type
)
171 Name
= (char *) StabStringsBase
+ StabEntry
[i
].n_strx
;
172 if (StabStringsLength
< StabEntry
[i
].n_strx
173 ||'\0' == *Name
|| '/' == Name
[strlen(Name
) - 1]
174 || '\\' == Name
[strlen(Name
) - 1]
175 || StabEntry
[i
].n_value
< ImageBase
)
179 Address
= StabEntry
[i
].n_value
- ImageBase
;
180 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
184 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
185 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= FindOrAddString((char *) StabStringsBase
186 + StabEntry
[i
].n_strx
,
189 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= 0;
190 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
191 LastFunctionAddress
= 0;
194 if (0 == StabEntry
[i
].n_desc
|| StabEntry
[i
].n_value
< ImageBase
) /* line # 0 isn't valid */
198 Address
= StabEntry
[i
].n_value
- ImageBase
;
199 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
202 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
204 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
205 if (sizeof(FuncName
) <= strlen((char *) StabStringsBase
+ StabEntry
[i
].n_strx
))
208 fprintf(stderr
, "Function name too long\n");
211 strcpy(FuncName
, (char *) StabStringsBase
+ StabEntry
[i
].n_strx
);
212 Name
= strchr(FuncName
, ':');
217 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(FuncName
,
220 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
221 LastFunctionAddress
= Address
;
224 if (0 == LastFunctionAddress
)
226 Address
= StabEntry
[i
].n_value
- ImageBase
;
230 Address
= LastFunctionAddress
+ StabEntry
[i
].n_value
;
232 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
235 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
236 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FunctionOffset
;
238 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
239 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= StabEntry
[i
].n_desc
;
248 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
254 ConvertCoffs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
255 ULONG
*StringsLength
, void *StringsBase
,
256 ULONG CoffSymbolsLength
, void *CoffSymbolsBase
,
257 ULONG CoffStringsLength
, void *CoffStringsBase
,
258 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
259 PIMAGE_SECTION_HEADER PESectionHeaders
)
262 PCOFF_SYMENT CoffEntry
;
266 CoffEntry
= (PCOFF_SYMENT
) CoffSymbolsBase
;
267 Count
= CoffSymbolsLength
/ sizeof(COFF_SYMENT
);
268 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
269 if (NULL
== *SymbolsBase
)
271 fprintf(stderr
, "Unable to allocate memory for converted COFF symbols\n");
276 for (i
= 0; i
< Count
; i
++)
278 if (ISFCN(CoffEntry
[i
].e_type
) || C_EXT
== CoffEntry
[i
].e_sclass
)
280 (*SymbolsBase
)[*SymbolsCount
].Address
= CoffEntry
[i
].e_value
;
281 if (0 < CoffEntry
[i
].e_scnum
)
283 if (PEFileHeader
->NumberOfSections
< CoffEntry
[i
].e_scnum
)
286 fprintf(stderr
, "Invalid section number %d in COFF symbols (only %d sections present)\n",
287 CoffEntry
[i
].e_scnum
, PEFileHeader
->NumberOfSections
);
290 (*SymbolsBase
)[*SymbolsCount
].Address
+= PESectionHeaders
[CoffEntry
[i
].e_scnum
- 1].VirtualAddress
;
292 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= 0;
293 if (0 == CoffEntry
[i
].e
.e
.e_zeroes
)
295 if (sizeof(FuncName
) <= strlen((char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
))
298 fprintf(stderr
, "Function name too long\n");
301 strcpy(FuncName
, (char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
);
305 memcpy(FuncName
, CoffEntry
[i
].e
.e_name
, E_SYMNMLEN
);
306 FuncName
[E_SYMNMLEN
] = '\0';
309 /* Name demangling: stdcall */
310 p
= strrchr(FuncName
, '@');
315 p
= ('_' == FuncName
[0] || '@' == FuncName
[0] ? FuncName
+ 1 : FuncName
);
316 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(p
,
319 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
322 i
+= CoffEntry
[i
].e_numaux
;
325 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
331 MergeStabsAndCoffs(ULONG
*MergedSymbolCount
, PROSSYM_ENTRY
*MergedSymbols
,
332 ULONG StabSymbolsCount
, PROSSYM_ENTRY StabSymbols
,
333 ULONG CoffSymbolsCount
, PROSSYM_ENTRY CoffSymbols
)
337 ULONG_PTR StabFunctionStartAddress
;
338 ULONG StabFunctionStringOffset
, NewStabFunctionStringOffset
;
340 *MergedSymbolCount
= 0;
341 if (StabSymbolsCount
== 0)
343 *MergedSymbols
= NULL
;
346 *MergedSymbols
= malloc(StabSymbolsCount
* sizeof(ROSSYM_ENTRY
));
347 if (NULL
== *MergedSymbols
)
349 fprintf(stderr
, "Unable to allocate memory for merged symbols\n");
353 StabFunctionStartAddress
= 0;
354 StabFunctionStringOffset
= 0;
356 for (StabIndex
= 0; StabIndex
< StabSymbolsCount
; StabIndex
++)
358 (*MergedSymbols
)[*MergedSymbolCount
] = StabSymbols
[StabIndex
];
359 for (j
= StabIndex
+ 1;
360 j
< StabSymbolsCount
&& StabSymbols
[j
].Address
== StabSymbols
[StabIndex
].Address
;
363 if (0 != StabSymbols
[j
].FileOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
)
365 (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
= StabSymbols
[j
].FileOffset
;
367 if (0 != StabSymbols
[j
].FunctionOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
)
369 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= StabSymbols
[j
].FunctionOffset
;
371 if (0 != StabSymbols
[j
].SourceLine
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
)
373 (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
= StabSymbols
[j
].SourceLine
;
377 while (CoffIndex
< CoffSymbolsCount
378 && CoffSymbols
[CoffIndex
+ 1].Address
<= (*MergedSymbols
)[*MergedSymbolCount
].Address
)
382 NewStabFunctionStringOffset
= (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
;
383 if (CoffSymbolsCount
> 0 &&
384 CoffSymbols
[CoffIndex
].Address
< (*MergedSymbols
)[*MergedSymbolCount
].Address
385 && StabFunctionStartAddress
< CoffSymbols
[CoffIndex
].Address
386 && 0 != CoffSymbols
[CoffIndex
].FunctionOffset
)
388 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= CoffSymbols
[CoffIndex
].FunctionOffset
;
390 if (StabFunctionStringOffset
!= NewStabFunctionStringOffset
)
392 StabFunctionStartAddress
= (*MergedSymbols
)[*MergedSymbolCount
].Address
;
394 StabFunctionStringOffset
= NewStabFunctionStringOffset
;
395 (*MergedSymbolCount
)++;
401 static PIMAGE_SECTION_HEADER
402 FindSectionForRVA(DWORD RVA
, unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
406 for (Section
= 0; Section
< NumberOfSections
; Section
++)
408 if (SectionHeaders
[Section
].VirtualAddress
<= RVA
&&
409 RVA
< SectionHeaders
[Section
].VirtualAddress
+ SectionHeaders
[Section
].Misc
.VirtualSize
)
411 return SectionHeaders
+ Section
;
419 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader
)
421 static char *BlacklistedSections
[] =
427 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
430 if (0 != (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
))
435 for (i
= 0; i
< sizeof(BlacklistedSections
) / sizeof(BlacklistedSections
[0]); i
++)
437 strncpy(SectionName
, BlacklistedSections
[i
], IMAGE_SIZEOF_SHORT_NAME
);
438 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
448 ProcessRelocations(ULONG
*ProcessedRelocsLength
, void **ProcessedRelocs
,
449 void *RawData
, PIMAGE_OPTIONAL_HEADER OptHeader
,
450 unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
452 PIMAGE_SECTION_HEADER RelocSectionHeader
, TargetSectionHeader
;
453 PIMAGE_BASE_RELOCATION BaseReloc
, End
, AcceptedRelocs
;
456 if (OptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
457 || 0 == OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
459 /* No relocation entries */
460 *ProcessedRelocsLength
= 0;
461 *ProcessedRelocs
= NULL
;
465 RelocSectionHeader
= FindSectionForRVA(OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
466 NumberOfSections
, SectionHeaders
);
467 if (NULL
== RelocSectionHeader
)
469 fprintf(stderr
, "Can't find section header for relocation data\n");
473 *ProcessedRelocs
= malloc(RelocSectionHeader
->SizeOfRawData
);
474 if (NULL
== *ProcessedRelocs
)
476 fprintf(stderr
, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader
->SizeOfRawData
);
479 *ProcessedRelocsLength
= 0;
481 BaseReloc
= (PIMAGE_BASE_RELOCATION
) ((char *) RawData
482 + RelocSectionHeader
->PointerToRawData
483 + (OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
-
484 RelocSectionHeader
->VirtualAddress
));
485 End
= (PIMAGE_BASE_RELOCATION
) ((char *) BaseReloc
486 + OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
488 while (BaseReloc
< End
&& 0 < BaseReloc
->SizeOfBlock
)
490 TargetSectionHeader
= FindSectionForRVA(BaseReloc
->VirtualAddress
, NumberOfSections
,
492 if (NULL
!= TargetSectionHeader
&& IncludeRelocationsForSection(TargetSectionHeader
))
494 AcceptedRelocs
= *ProcessedRelocs
;
496 while (AcceptedRelocs
< (PIMAGE_BASE_RELOCATION
) ((char *) *ProcessedRelocs
+
497 *ProcessedRelocsLength
)
500 Found
= BaseReloc
->SizeOfBlock
== AcceptedRelocs
->SizeOfBlock
501 && 0 == memcmp(BaseReloc
, AcceptedRelocs
, AcceptedRelocs
->SizeOfBlock
);
502 AcceptedRelocs
= (PIMAGE_BASE_RELOCATION
) ((char *) AcceptedRelocs
+
503 AcceptedRelocs
->SizeOfBlock
);
507 memcpy((char *) *ProcessedRelocs
+ *ProcessedRelocsLength
,
508 BaseReloc
, BaseReloc
->SizeOfBlock
);
509 *ProcessedRelocsLength
+= BaseReloc
->SizeOfBlock
;
512 BaseReloc
= (PIMAGE_BASE_RELOCATION
)((char *) BaseReloc
+ BaseReloc
->SizeOfBlock
);
519 CreateOutputFile(FILE *OutFile
, void *InData
,
520 PIMAGE_DOS_HEADER InDosHeader
, PIMAGE_FILE_HEADER InFileHeader
,
521 PIMAGE_OPTIONAL_HEADER InOptHeader
, PIMAGE_SECTION_HEADER InSectionHeaders
,
522 ULONG RosSymLength
, void *RosSymSection
)
524 ULONG StartOfRawData
;
526 void *OutHeader
, *ProcessedRelocs
, *PaddedRosSym
, *Data
;
527 PIMAGE_DOS_HEADER OutDosHeader
;
528 PIMAGE_FILE_HEADER OutFileHeader
;
529 PIMAGE_OPTIONAL_HEADER OutOptHeader
;
530 PIMAGE_SECTION_HEADER OutSectionHeaders
, CurrentSectionHeader
;
533 ULONG ProcessedRelocsLength
;
534 ULONG RosSymOffset
, RosSymFileLength
;
535 int InRelocSectionIndex
;
536 PIMAGE_SECTION_HEADER OutRelocSection
;
539 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
541 if ((0 == StartOfRawData
542 || InSectionHeaders
[Section
].PointerToRawData
< StartOfRawData
)
543 && 0 != InSectionHeaders
[Section
].PointerToRawData
544 && 0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
546 StartOfRawData
= InSectionHeaders
[Section
].PointerToRawData
;
549 OutHeader
= malloc(StartOfRawData
);
550 if (NULL
== OutHeader
)
552 fprintf(stderr
, "Failed to allocate %lu bytes for output file header\n", StartOfRawData
);
555 memset(OutHeader
, '\0', StartOfRawData
);
557 OutDosHeader
= (PIMAGE_DOS_HEADER
) OutHeader
;
558 memcpy(OutDosHeader
, InDosHeader
, InDosHeader
->e_lfanew
+ sizeof(ULONG
));
560 OutFileHeader
= (PIMAGE_FILE_HEADER
)((char *) OutHeader
+ OutDosHeader
->e_lfanew
+ sizeof(ULONG
));
561 memcpy(OutFileHeader
, InFileHeader
, sizeof(IMAGE_FILE_HEADER
));
562 OutFileHeader
->PointerToSymbolTable
= 0;
563 OutFileHeader
->NumberOfSymbols
= 0;
564 OutFileHeader
->Characteristics
&= ~(IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
565 IMAGE_FILE_DEBUG_STRIPPED
);
567 OutOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(OutFileHeader
+ 1);
568 memcpy(OutOptHeader
, InOptHeader
, sizeof(IMAGE_OPTIONAL_HEADER
));
569 OutOptHeader
->CheckSum
= 0;
571 OutSectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) OutOptHeader
+ OutFileHeader
->SizeOfOptionalHeader
);
573 if (ProcessRelocations(&ProcessedRelocsLength
, &ProcessedRelocs
, InData
, InOptHeader
,
574 InFileHeader
->NumberOfSections
, InSectionHeaders
))
578 if (InOptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
579 || 0 == InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
581 InRelocSectionIndex
= -1;
585 InRelocSectionIndex
= FindSectionForRVA(InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
586 InFileHeader
->NumberOfSections
, InSectionHeaders
) - InSectionHeaders
;
589 OutFileHeader
->NumberOfSections
= 0;
590 CurrentSectionHeader
= OutSectionHeaders
;
591 OutOptHeader
->SizeOfImage
= 0;
593 OutRelocSection
= NULL
;
594 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
596 if (0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
598 *CurrentSectionHeader
= InSectionHeaders
[Section
];
599 CurrentSectionHeader
->PointerToLinenumbers
= 0;
600 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
601 if (OutOptHeader
->SizeOfImage
< CurrentSectionHeader
->VirtualAddress
+
602 CurrentSectionHeader
->Misc
.VirtualSize
)
604 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
605 CurrentSectionHeader
->Misc
.VirtualSize
,
606 OutOptHeader
->SectionAlignment
);
608 if (RosSymOffset
< CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
)
610 RosSymOffset
= CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
;
612 if (Section
== InRelocSectionIndex
)
614 OutRelocSection
= CurrentSectionHeader
;
616 (OutFileHeader
->NumberOfSections
) ++;
617 CurrentSectionHeader
++;
621 if (OutRelocSection
== CurrentSectionHeader
- 1)
623 OutOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= ProcessedRelocsLength
;
624 if (OutOptHeader
->SizeOfImage
== OutRelocSection
->VirtualAddress
+
625 ROUND_UP(OutRelocSection
->Misc
.VirtualSize
,
626 OutOptHeader
->SectionAlignment
))
628 OutOptHeader
->SizeOfImage
= OutRelocSection
->VirtualAddress
+
629 ROUND_UP(ProcessedRelocsLength
,
630 OutOptHeader
->SectionAlignment
);
632 OutRelocSection
->Misc
.VirtualSize
= ProcessedRelocsLength
;
633 if (RosSymOffset
== OutRelocSection
->PointerToRawData
634 + OutRelocSection
->SizeOfRawData
)
636 RosSymOffset
= OutRelocSection
->PointerToRawData
+
637 ROUND_UP(ProcessedRelocsLength
, OutOptHeader
->FileAlignment
);
639 OutRelocSection
->SizeOfRawData
= ROUND_UP(ProcessedRelocsLength
,
640 OutOptHeader
->FileAlignment
);
643 if (RosSymLength
> 0)
645 RosSymFileLength
= ROUND_UP(RosSymLength
, OutOptHeader
->FileAlignment
);
646 memcpy(CurrentSectionHeader
->Name
, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
647 CurrentSectionHeader
->Misc
.VirtualSize
= RosSymLength
;
648 CurrentSectionHeader
->VirtualAddress
= OutOptHeader
->SizeOfImage
;
649 CurrentSectionHeader
->SizeOfRawData
= RosSymFileLength
;
650 CurrentSectionHeader
->PointerToRawData
= RosSymOffset
;
651 CurrentSectionHeader
->PointerToRelocations
= 0;
652 CurrentSectionHeader
->PointerToLinenumbers
= 0;
653 CurrentSectionHeader
->NumberOfRelocations
= 0;
654 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
655 CurrentSectionHeader
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_DISCARDABLE
656 | IMAGE_SCN_LNK_REMOVE
| IMAGE_SCN_TYPE_NOLOAD
;
657 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
658 CurrentSectionHeader
->Misc
.VirtualSize
,
659 OutOptHeader
->SectionAlignment
);
660 (OutFileHeader
->NumberOfSections
)++;
662 PaddedRosSym
= malloc(RosSymFileLength
);
663 if (NULL
== PaddedRosSym
)
665 fprintf(stderr
, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength
);
668 memcpy(PaddedRosSym
, RosSymSection
, RosSymLength
);
669 memset((char *) PaddedRosSym
+ RosSymLength
, '\0', RosSymFileLength
- RosSymLength
);
676 for (i
= 0; i
< StartOfRawData
/ 2; i
++)
678 CheckSum
+= ((unsigned short*) OutHeader
)[i
];
679 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
681 Length
= StartOfRawData
;
682 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
684 if (OutRelocSection
== OutSectionHeaders
+ Section
)
686 Data
= (void *) ProcessedRelocs
;
688 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
690 Data
= (void *) PaddedRosSym
;
694 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
696 for (i
= 0; i
< OutSectionHeaders
[Section
].SizeOfRawData
/ 2; i
++)
698 CheckSum
+= ((unsigned short*) Data
)[i
];
699 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
701 Length
+= OutSectionHeaders
[Section
].SizeOfRawData
;
704 OutOptHeader
->CheckSum
= CheckSum
;
706 if (fwrite(OutHeader
, 1, StartOfRawData
, OutFile
) != StartOfRawData
)
708 perror("Error writing output header\n");
713 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
715 if (0 != OutSectionHeaders
[Section
].SizeOfRawData
)
717 fseek(OutFile
, OutSectionHeaders
[Section
].PointerToRawData
, SEEK_SET
);
718 if (OutRelocSection
== OutSectionHeaders
+ Section
)
720 Data
= (void *) ProcessedRelocs
;
722 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
724 Data
= (void *) PaddedRosSym
;
728 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
730 if (fwrite(Data
, 1, OutSectionHeaders
[Section
].SizeOfRawData
, OutFile
) !=
731 OutSectionHeaders
[Section
].SizeOfRawData
)
733 perror("Error writing section data\n");
750 int main(int argc
, char* argv
[])
752 PSYMBOLFILE_HEADER SymbolFileHeader
;
753 PIMAGE_DOS_HEADER PEDosHeader
;
754 PIMAGE_FILE_HEADER PEFileHeader
;
755 PIMAGE_OPTIONAL_HEADER PEOptHeader
;
756 PIMAGE_SECTION_HEADER PESectionHeaders
;
760 void *StabStringBase
;
761 ULONG StabStringsLength
;
764 void *CoffStringBase
;
765 ULONG CoffStringsLength
;
771 ULONG StabSymbolsCount
;
772 PROSSYM_ENTRY StabSymbols
;
773 ULONG CoffSymbolsCount
;
774 PROSSYM_ENTRY CoffSymbols
;
775 ULONG MergedSymbolsCount
;
776 PROSSYM_ENTRY MergedSymbols
;
784 fprintf(stderr
, "Usage: rsym <exefile> <symfile>\n");
788 path1
= convert_path(argv
[1]);
789 path2
= convert_path(argv
[2]);
791 FileData
= load_file ( path1
, &FileSize
);
794 fprintf ( stderr
, "An error occured loading '%s'\n", path1
);
798 /* Check if MZ header exists */
799 PEDosHeader
= (PIMAGE_DOS_HEADER
) FileData
;
800 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
|| PEDosHeader
->e_lfanew
== 0L)
802 perror("Input file is not a PE image.\n");
807 /* Locate PE file header */
808 /* sizeof(ULONG) = sizeof(MAGIC) */
809 PEFileHeader
= (PIMAGE_FILE_HEADER
)((char *) FileData
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
811 /* Locate optional header */
812 PEOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(PEFileHeader
+ 1);
813 ImageBase
= PEOptHeader
->ImageBase
;
815 /* Locate PE section headers */
816 PESectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) PEOptHeader
+ PEFileHeader
->SizeOfOptionalHeader
);
818 if (GetStabInfo(FileData
, PEFileHeader
, PESectionHeaders
, &StabsLength
, &StabBase
,
819 &StabStringsLength
, &StabStringBase
))
825 if (GetCoffInfo(FileData
, PEFileHeader
, PESectionHeaders
, &CoffsLength
, &CoffBase
,
826 &CoffStringsLength
, &CoffStringBase
))
832 StringBase
= malloc(1 + StabStringsLength
+ CoffStringsLength
+
833 (CoffsLength
/ sizeof(ROSSYM_ENTRY
)) * (E_SYMNMLEN
+ 1));
834 if (NULL
== StringBase
)
837 fprintf(stderr
, "Failed to allocate memory for strings table\n");
840 /* Make offset 0 into an empty string */
841 *((char *) StringBase
) = '\0';
844 if (ConvertStabs(&StabSymbolsCount
, &StabSymbols
, &StringsLength
, StringBase
,
845 StabsLength
, StabBase
, StabStringsLength
, StabStringBase
,
846 ImageBase
, PEFileHeader
, PESectionHeaders
))
850 fprintf(stderr
, "Failed to allocate memory for strings table\n");
854 if (ConvertCoffs(&CoffSymbolsCount
, &CoffSymbols
, &StringsLength
, StringBase
,
855 CoffsLength
, CoffBase
, CoffStringsLength
, CoffStringBase
,
856 ImageBase
, PEFileHeader
, PESectionHeaders
))
867 if (MergeStabsAndCoffs(&MergedSymbolsCount
, &MergedSymbols
,
868 StabSymbolsCount
, StabSymbols
,
869 CoffSymbolsCount
, CoffSymbols
))
892 if (MergedSymbolsCount
== 0)
895 RosSymSection
= NULL
;
899 RosSymLength
= sizeof(SYMBOLFILE_HEADER
) + MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
)
901 RosSymSection
= malloc(RosSymLength
);
902 if (NULL
== RosSymSection
)
907 fprintf(stderr
, "Unable to allocate memory for .rossym section\n");
910 memset(RosSymSection
, '\0', RosSymLength
);
912 SymbolFileHeader
= (PSYMBOLFILE_HEADER
) RosSymSection
;
913 SymbolFileHeader
->SymbolsOffset
= sizeof(SYMBOLFILE_HEADER
);
914 SymbolFileHeader
->SymbolsLength
= MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
);
915 SymbolFileHeader
->StringsOffset
= SymbolFileHeader
->SymbolsOffset
+ SymbolFileHeader
->SymbolsLength
;
916 SymbolFileHeader
->StringsLength
= StringsLength
;
918 memcpy((char *) RosSymSection
+ SymbolFileHeader
->SymbolsOffset
, MergedSymbols
,
919 SymbolFileHeader
->SymbolsLength
);
920 memcpy((char *) RosSymSection
+ SymbolFileHeader
->StringsOffset
, StringBase
,
921 SymbolFileHeader
->StringsLength
);
926 out
= fopen(path2
, "wb");
929 perror("Cannot open output file");
935 if (CreateOutputFile(out
, FileData
, PEDosHeader
, PEFileHeader
, PEOptHeader
,
936 PESectionHeaders
, RosSymLength
, RosSymSection
))