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
)
172 Name
= (char *) StabStringsBase
+ StabEntry
[i
].n_strx
;
173 if (StabStringsLength
< StabEntry
[i
].n_strx
174 ||'\0' == *Name
|| '/' == Name
[strlen(Name
) - 1]
175 || '\\' == Name
[strlen(Name
) - 1]
176 || StabEntry
[i
].n_value
< ImageBase
)
180 Address
= StabEntry
[i
].n_value
- ImageBase
;
181 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
185 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
186 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= FindOrAddString((char *) StabStringsBase
187 + StabEntry
[i
].n_strx
,
190 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= 0;
191 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
192 LastFunctionAddress
= 0;
195 if (0 == StabEntry
[i
].n_desc
|| StabEntry
[i
].n_value
< ImageBase
) /* line # 0 isn't valid */
199 Address
= StabEntry
[i
].n_value
- ImageBase
;
200 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
203 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
205 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
206 if (sizeof(FuncName
) <= strlen((char *) StabStringsBase
+ StabEntry
[i
].n_strx
))
209 fprintf(stderr
, "Function name too long\n");
212 strcpy(FuncName
, (char *) StabStringsBase
+ StabEntry
[i
].n_strx
);
213 Name
= strchr(FuncName
, ':');
218 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(FuncName
,
221 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
222 LastFunctionAddress
= Address
;
225 if (0 == LastFunctionAddress
)
227 Address
= StabEntry
[i
].n_value
- ImageBase
;
231 Address
= LastFunctionAddress
+ StabEntry
[i
].n_value
;
233 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
236 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
237 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FunctionOffset
;
239 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
240 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= StabEntry
[i
].n_desc
;
249 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
255 ConvertCoffs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
256 ULONG
*StringsLength
, void *StringsBase
,
257 ULONG CoffSymbolsLength
, void *CoffSymbolsBase
,
258 ULONG CoffStringsLength
, void *CoffStringsBase
,
259 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
260 PIMAGE_SECTION_HEADER PESectionHeaders
)
263 PCOFF_SYMENT CoffEntry
;
267 CoffEntry
= (PCOFF_SYMENT
) CoffSymbolsBase
;
268 Count
= CoffSymbolsLength
/ sizeof(COFF_SYMENT
);
269 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
270 if (NULL
== *SymbolsBase
)
272 fprintf(stderr
, "Unable to allocate memory for converted COFF symbols\n");
277 for (i
= 0; i
< Count
; i
++)
279 if (ISFCN(CoffEntry
[i
].e_type
) || C_EXT
== CoffEntry
[i
].e_sclass
)
281 (*SymbolsBase
)[*SymbolsCount
].Address
= CoffEntry
[i
].e_value
;
282 if (0 < CoffEntry
[i
].e_scnum
)
284 if (PEFileHeader
->NumberOfSections
< CoffEntry
[i
].e_scnum
)
287 fprintf(stderr
, "Invalid section number %d in COFF symbols (only %d sections present)\n",
288 CoffEntry
[i
].e_scnum
, PEFileHeader
->NumberOfSections
);
291 (*SymbolsBase
)[*SymbolsCount
].Address
+= PESectionHeaders
[CoffEntry
[i
].e_scnum
- 1].VirtualAddress
;
293 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= 0;
294 if (0 == CoffEntry
[i
].e
.e
.e_zeroes
)
296 if (sizeof(FuncName
) <= strlen((char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
))
299 fprintf(stderr
, "Function name too long\n");
302 strcpy(FuncName
, (char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
);
306 memcpy(FuncName
, CoffEntry
[i
].e
.e_name
, E_SYMNMLEN
);
307 FuncName
[E_SYMNMLEN
] = '\0';
310 /* Name demangling: stdcall */
311 p
= strrchr(FuncName
, '@');
316 p
= ('_' == FuncName
[0] || '@' == FuncName
[0] ? FuncName
+ 1 : FuncName
);
317 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(p
,
320 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
323 i
+= CoffEntry
[i
].e_numaux
;
326 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
332 MergeStabsAndCoffs(ULONG
*MergedSymbolCount
, PROSSYM_ENTRY
*MergedSymbols
,
333 ULONG StabSymbolsCount
, PROSSYM_ENTRY StabSymbols
,
334 ULONG CoffSymbolsCount
, PROSSYM_ENTRY CoffSymbols
)
338 ULONG_PTR StabFunctionStartAddress
;
339 ULONG StabFunctionStringOffset
, NewStabFunctionStringOffset
;
341 *MergedSymbolCount
= 0;
342 if (StabSymbolsCount
== 0)
344 *MergedSymbols
= NULL
;
347 *MergedSymbols
= malloc(StabSymbolsCount
* sizeof(ROSSYM_ENTRY
));
348 if (NULL
== *MergedSymbols
)
350 fprintf(stderr
, "Unable to allocate memory for merged symbols\n");
354 StabFunctionStartAddress
= 0;
355 StabFunctionStringOffset
= 0;
357 for (StabIndex
= 0; StabIndex
< StabSymbolsCount
; StabIndex
++)
359 (*MergedSymbols
)[*MergedSymbolCount
] = StabSymbols
[StabIndex
];
360 for (j
= StabIndex
+ 1;
361 j
< StabSymbolsCount
&& StabSymbols
[j
].Address
== StabSymbols
[StabIndex
].Address
;
364 if (0 != StabSymbols
[j
].FileOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
)
366 (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
= StabSymbols
[j
].FileOffset
;
368 if (0 != StabSymbols
[j
].FunctionOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
)
370 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= StabSymbols
[j
].FunctionOffset
;
372 if (0 != StabSymbols
[j
].SourceLine
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
)
374 (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
= StabSymbols
[j
].SourceLine
;
378 while (CoffIndex
< CoffSymbolsCount
379 && CoffSymbols
[CoffIndex
+ 1].Address
<= (*MergedSymbols
)[*MergedSymbolCount
].Address
)
383 NewStabFunctionStringOffset
= (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
;
384 if (CoffSymbolsCount
> 0 &&
385 CoffSymbols
[CoffIndex
].Address
< (*MergedSymbols
)[*MergedSymbolCount
].Address
386 && StabFunctionStartAddress
< CoffSymbols
[CoffIndex
].Address
387 && 0 != CoffSymbols
[CoffIndex
].FunctionOffset
)
389 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= CoffSymbols
[CoffIndex
].FunctionOffset
;
391 if (StabFunctionStringOffset
!= NewStabFunctionStringOffset
)
393 StabFunctionStartAddress
= (*MergedSymbols
)[*MergedSymbolCount
].Address
;
395 StabFunctionStringOffset
= NewStabFunctionStringOffset
;
396 (*MergedSymbolCount
)++;
402 static PIMAGE_SECTION_HEADER
403 FindSectionForRVA(DWORD RVA
, unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
407 for (Section
= 0; Section
< NumberOfSections
; Section
++)
409 if (SectionHeaders
[Section
].VirtualAddress
<= RVA
&&
410 RVA
< SectionHeaders
[Section
].VirtualAddress
+ SectionHeaders
[Section
].Misc
.VirtualSize
)
412 return SectionHeaders
+ Section
;
420 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader
)
422 static char *BlacklistedSections
[] =
428 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
431 if (0 != (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
))
436 for (i
= 0; i
< sizeof(BlacklistedSections
) / sizeof(BlacklistedSections
[0]); i
++)
438 strncpy(SectionName
, BlacklistedSections
[i
], IMAGE_SIZEOF_SHORT_NAME
);
439 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
449 ProcessRelocations(ULONG
*ProcessedRelocsLength
, void **ProcessedRelocs
,
450 void *RawData
, PIMAGE_OPTIONAL_HEADER OptHeader
,
451 unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
453 PIMAGE_SECTION_HEADER RelocSectionHeader
, TargetSectionHeader
;
454 PIMAGE_BASE_RELOCATION BaseReloc
, End
, AcceptedRelocs
;
457 if (OptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
458 || 0 == OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
460 /* No relocation entries */
461 *ProcessedRelocsLength
= 0;
462 *ProcessedRelocs
= NULL
;
466 RelocSectionHeader
= FindSectionForRVA(OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
467 NumberOfSections
, SectionHeaders
);
468 if (NULL
== RelocSectionHeader
)
470 fprintf(stderr
, "Can't find section header for relocation data\n");
474 *ProcessedRelocs
= malloc(RelocSectionHeader
->SizeOfRawData
);
475 if (NULL
== *ProcessedRelocs
)
477 fprintf(stderr
, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader
->SizeOfRawData
);
480 *ProcessedRelocsLength
= 0;
482 BaseReloc
= (PIMAGE_BASE_RELOCATION
) ((char *) RawData
483 + RelocSectionHeader
->PointerToRawData
484 + (OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
-
485 RelocSectionHeader
->VirtualAddress
));
486 End
= (PIMAGE_BASE_RELOCATION
) ((char *) BaseReloc
487 + OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
489 while (BaseReloc
< End
&& 0 < BaseReloc
->SizeOfBlock
)
491 TargetSectionHeader
= FindSectionForRVA(BaseReloc
->VirtualAddress
, NumberOfSections
,
493 if (NULL
!= TargetSectionHeader
&& IncludeRelocationsForSection(TargetSectionHeader
))
495 AcceptedRelocs
= *ProcessedRelocs
;
497 while (AcceptedRelocs
< (PIMAGE_BASE_RELOCATION
) ((char *) *ProcessedRelocs
+
498 *ProcessedRelocsLength
)
501 Found
= BaseReloc
->SizeOfBlock
== AcceptedRelocs
->SizeOfBlock
502 && 0 == memcmp(BaseReloc
, AcceptedRelocs
, AcceptedRelocs
->SizeOfBlock
);
503 AcceptedRelocs
= (PIMAGE_BASE_RELOCATION
) ((char *) AcceptedRelocs
+
504 AcceptedRelocs
->SizeOfBlock
);
508 memcpy((char *) *ProcessedRelocs
+ *ProcessedRelocsLength
,
509 BaseReloc
, BaseReloc
->SizeOfBlock
);
510 *ProcessedRelocsLength
+= BaseReloc
->SizeOfBlock
;
513 BaseReloc
= (PIMAGE_BASE_RELOCATION
)((char *) BaseReloc
+ BaseReloc
->SizeOfBlock
);
520 CreateOutputFile(FILE *OutFile
, void *InData
,
521 PIMAGE_DOS_HEADER InDosHeader
, PIMAGE_FILE_HEADER InFileHeader
,
522 PIMAGE_OPTIONAL_HEADER InOptHeader
, PIMAGE_SECTION_HEADER InSectionHeaders
,
523 ULONG RosSymLength
, void *RosSymSection
)
525 ULONG StartOfRawData
;
527 void *OutHeader
, *ProcessedRelocs
, *PaddedRosSym
, *Data
;
528 PIMAGE_DOS_HEADER OutDosHeader
;
529 PIMAGE_FILE_HEADER OutFileHeader
;
530 PIMAGE_OPTIONAL_HEADER OutOptHeader
;
531 PIMAGE_SECTION_HEADER OutSectionHeaders
, CurrentSectionHeader
;
534 ULONG ProcessedRelocsLength
;
535 ULONG RosSymOffset
, RosSymFileLength
;
536 int InRelocSectionIndex
;
537 PIMAGE_SECTION_HEADER OutRelocSection
;
540 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
542 if ((0 == StartOfRawData
543 || InSectionHeaders
[Section
].PointerToRawData
< StartOfRawData
)
544 && 0 != InSectionHeaders
[Section
].PointerToRawData
545 && 0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
547 StartOfRawData
= InSectionHeaders
[Section
].PointerToRawData
;
550 OutHeader
= malloc(StartOfRawData
);
551 if (NULL
== OutHeader
)
553 fprintf(stderr
, "Failed to allocate %lu bytes for output file header\n", StartOfRawData
);
556 memset(OutHeader
, '\0', StartOfRawData
);
558 OutDosHeader
= (PIMAGE_DOS_HEADER
) OutHeader
;
559 memcpy(OutDosHeader
, InDosHeader
, InDosHeader
->e_lfanew
+ sizeof(ULONG
));
561 OutFileHeader
= (PIMAGE_FILE_HEADER
)((char *) OutHeader
+ OutDosHeader
->e_lfanew
+ sizeof(ULONG
));
562 memcpy(OutFileHeader
, InFileHeader
, sizeof(IMAGE_FILE_HEADER
));
563 OutFileHeader
->PointerToSymbolTable
= 0;
564 OutFileHeader
->NumberOfSymbols
= 0;
565 OutFileHeader
->Characteristics
&= ~(IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
566 IMAGE_FILE_DEBUG_STRIPPED
);
568 OutOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(OutFileHeader
+ 1);
569 memcpy(OutOptHeader
, InOptHeader
, sizeof(IMAGE_OPTIONAL_HEADER
));
570 OutOptHeader
->CheckSum
= 0;
572 OutSectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) OutOptHeader
+ OutFileHeader
->SizeOfOptionalHeader
);
574 if (ProcessRelocations(&ProcessedRelocsLength
, &ProcessedRelocs
, InData
, InOptHeader
,
575 InFileHeader
->NumberOfSections
, InSectionHeaders
))
579 if (InOptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
580 || 0 == InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
582 InRelocSectionIndex
= -1;
586 InRelocSectionIndex
= FindSectionForRVA(InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
587 InFileHeader
->NumberOfSections
, InSectionHeaders
) - InSectionHeaders
;
590 OutFileHeader
->NumberOfSections
= 0;
591 CurrentSectionHeader
= OutSectionHeaders
;
592 OutOptHeader
->SizeOfImage
= 0;
594 OutRelocSection
= NULL
;
595 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
597 if (0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
599 *CurrentSectionHeader
= InSectionHeaders
[Section
];
600 CurrentSectionHeader
->PointerToLinenumbers
= 0;
601 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
602 if (OutOptHeader
->SizeOfImage
< CurrentSectionHeader
->VirtualAddress
+
603 CurrentSectionHeader
->Misc
.VirtualSize
)
605 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
606 CurrentSectionHeader
->Misc
.VirtualSize
,
607 OutOptHeader
->SectionAlignment
);
609 if (RosSymOffset
< CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
)
611 RosSymOffset
= CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
;
613 if (Section
== InRelocSectionIndex
)
615 OutRelocSection
= CurrentSectionHeader
;
617 (OutFileHeader
->NumberOfSections
) ++;
618 CurrentSectionHeader
++;
622 if (OutRelocSection
== CurrentSectionHeader
- 1)
624 OutOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= ProcessedRelocsLength
;
625 if (OutOptHeader
->SizeOfImage
== OutRelocSection
->VirtualAddress
+
626 ROUND_UP(OutRelocSection
->Misc
.VirtualSize
,
627 OutOptHeader
->SectionAlignment
))
629 OutOptHeader
->SizeOfImage
= OutRelocSection
->VirtualAddress
+
630 ROUND_UP(ProcessedRelocsLength
,
631 OutOptHeader
->SectionAlignment
);
633 OutRelocSection
->Misc
.VirtualSize
= ProcessedRelocsLength
;
634 if (RosSymOffset
== OutRelocSection
->PointerToRawData
635 + OutRelocSection
->SizeOfRawData
)
637 RosSymOffset
= OutRelocSection
->PointerToRawData
+
638 ROUND_UP(ProcessedRelocsLength
, OutOptHeader
->FileAlignment
);
640 OutRelocSection
->SizeOfRawData
= ROUND_UP(ProcessedRelocsLength
,
641 OutOptHeader
->FileAlignment
);
644 if (RosSymLength
> 0)
646 RosSymFileLength
= ROUND_UP(RosSymLength
, OutOptHeader
->FileAlignment
);
647 memcpy(CurrentSectionHeader
->Name
, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
648 CurrentSectionHeader
->Misc
.VirtualSize
= RosSymLength
;
649 CurrentSectionHeader
->VirtualAddress
= OutOptHeader
->SizeOfImage
;
650 CurrentSectionHeader
->SizeOfRawData
= RosSymFileLength
;
651 CurrentSectionHeader
->PointerToRawData
= RosSymOffset
;
652 CurrentSectionHeader
->PointerToRelocations
= 0;
653 CurrentSectionHeader
->PointerToLinenumbers
= 0;
654 CurrentSectionHeader
->NumberOfRelocations
= 0;
655 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
656 CurrentSectionHeader
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_DISCARDABLE
657 | IMAGE_SCN_LNK_REMOVE
| IMAGE_SCN_TYPE_NOLOAD
;
658 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
659 CurrentSectionHeader
->Misc
.VirtualSize
,
660 OutOptHeader
->SectionAlignment
);
661 (OutFileHeader
->NumberOfSections
)++;
663 PaddedRosSym
= malloc(RosSymFileLength
);
664 if (NULL
== PaddedRosSym
)
666 fprintf(stderr
, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength
);
669 memcpy(PaddedRosSym
, RosSymSection
, RosSymLength
);
670 memset((char *) PaddedRosSym
+ RosSymLength
, '\0', RosSymFileLength
- RosSymLength
);
677 for (i
= 0; i
< StartOfRawData
/ 2; i
++)
679 CheckSum
+= ((unsigned short*) OutHeader
)[i
];
680 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
682 Length
= StartOfRawData
;
683 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
685 if (OutRelocSection
== OutSectionHeaders
+ Section
)
687 Data
= (void *) ProcessedRelocs
;
689 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
691 Data
= (void *) PaddedRosSym
;
695 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
697 for (i
= 0; i
< OutSectionHeaders
[Section
].SizeOfRawData
/ 2; i
++)
699 CheckSum
+= ((unsigned short*) Data
)[i
];
700 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
702 Length
+= OutSectionHeaders
[Section
].SizeOfRawData
;
705 OutOptHeader
->CheckSum
= CheckSum
;
707 if (fwrite(OutHeader
, 1, StartOfRawData
, OutFile
) != StartOfRawData
)
709 perror("Error writing output header\n");
714 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
716 if (0 != OutSectionHeaders
[Section
].SizeOfRawData
)
718 fseek(OutFile
, OutSectionHeaders
[Section
].PointerToRawData
, SEEK_SET
);
719 if (OutRelocSection
== OutSectionHeaders
+ Section
)
721 Data
= (void *) ProcessedRelocs
;
723 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
725 Data
= (void *) PaddedRosSym
;
729 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
731 if (fwrite(Data
, 1, OutSectionHeaders
[Section
].SizeOfRawData
, OutFile
) !=
732 OutSectionHeaders
[Section
].SizeOfRawData
)
734 perror("Error writing section data\n");
751 int main(int argc
, char* argv
[])
753 PSYMBOLFILE_HEADER SymbolFileHeader
;
754 PIMAGE_DOS_HEADER PEDosHeader
;
755 PIMAGE_FILE_HEADER PEFileHeader
;
756 PIMAGE_OPTIONAL_HEADER PEOptHeader
;
757 PIMAGE_SECTION_HEADER PESectionHeaders
;
761 void *StabStringBase
;
762 ULONG StabStringsLength
;
763 void *CoffBase
= NULL
;
765 void *CoffStringBase
= NULL
;
766 ULONG CoffStringsLength
;
772 ULONG StabSymbolsCount
;
773 PROSSYM_ENTRY StabSymbols
;
774 ULONG CoffSymbolsCount
;
775 PROSSYM_ENTRY CoffSymbols
;
776 ULONG MergedSymbolsCount
;
777 PROSSYM_ENTRY MergedSymbols
;
785 fprintf(stderr
, "Usage: rsym <exefile> <symfile>\n");
789 path1
= convert_path(argv
[1]);
790 path2
= convert_path(argv
[2]);
792 FileData
= load_file ( path1
, &FileSize
);
795 fprintf ( stderr
, "An error occured loading '%s'\n", path1
);
799 /* Check if MZ header exists */
800 PEDosHeader
= (PIMAGE_DOS_HEADER
) FileData
;
801 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
|| PEDosHeader
->e_lfanew
== 0L)
803 perror("Input file is not a PE image.\n");
808 /* Locate PE file header */
809 /* sizeof(ULONG) = sizeof(MAGIC) */
810 PEFileHeader
= (PIMAGE_FILE_HEADER
)((char *) FileData
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
812 /* Locate optional header */
813 assert(sizeof(ULONG
) == 4);
814 PEOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(PEFileHeader
+ 1);
815 ImageBase
= PEOptHeader
->ImageBase
;
817 /* Locate PE section headers */
818 PESectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) PEOptHeader
+ PEFileHeader
->SizeOfOptionalHeader
);
820 if (GetStabInfo(FileData
, PEFileHeader
, PESectionHeaders
, &StabsLength
, &StabBase
,
821 &StabStringsLength
, &StabStringBase
))
827 if (GetCoffInfo(FileData
, PEFileHeader
, PESectionHeaders
, &CoffsLength
, &CoffBase
,
828 &CoffStringsLength
, &CoffStringBase
))
834 StringBase
= malloc(1 + StabStringsLength
+ CoffStringsLength
+
835 (CoffsLength
/ sizeof(ROSSYM_ENTRY
)) * (E_SYMNMLEN
+ 1));
836 if (NULL
== StringBase
)
839 fprintf(stderr
, "Failed to allocate memory for strings table\n");
842 /* Make offset 0 into an empty string */
843 *((char *) StringBase
) = '\0';
846 if (ConvertStabs(&StabSymbolsCount
, &StabSymbols
, &StringsLength
, StringBase
,
847 StabsLength
, StabBase
, StabStringsLength
, StabStringBase
,
848 ImageBase
, PEFileHeader
, PESectionHeaders
))
852 fprintf(stderr
, "Failed to allocate memory for strings table\n");
856 if (ConvertCoffs(&CoffSymbolsCount
, &CoffSymbols
, &StringsLength
, StringBase
,
857 CoffsLength
, CoffBase
, CoffStringsLength
, CoffStringBase
,
858 ImageBase
, PEFileHeader
, PESectionHeaders
))
869 if (MergeStabsAndCoffs(&MergedSymbolsCount
, &MergedSymbols
,
870 StabSymbolsCount
, StabSymbols
,
871 CoffSymbolsCount
, CoffSymbols
))
894 if (MergedSymbolsCount
== 0)
897 RosSymSection
= NULL
;
901 RosSymLength
= sizeof(SYMBOLFILE_HEADER
) + MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
)
903 RosSymSection
= malloc(RosSymLength
);
904 if (NULL
== RosSymSection
)
909 fprintf(stderr
, "Unable to allocate memory for .rossym section\n");
912 memset(RosSymSection
, '\0', RosSymLength
);
914 SymbolFileHeader
= (PSYMBOLFILE_HEADER
) RosSymSection
;
915 SymbolFileHeader
->SymbolsOffset
= sizeof(SYMBOLFILE_HEADER
);
916 SymbolFileHeader
->SymbolsLength
= MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
);
917 SymbolFileHeader
->StringsOffset
= SymbolFileHeader
->SymbolsOffset
+ SymbolFileHeader
->SymbolsLength
;
918 SymbolFileHeader
->StringsLength
= StringsLength
;
920 memcpy((char *) RosSymSection
+ SymbolFileHeader
->SymbolsOffset
, MergedSymbols
,
921 SymbolFileHeader
->SymbolsLength
);
922 memcpy((char *) RosSymSection
+ SymbolFileHeader
->StringsOffset
, StringBase
,
923 SymbolFileHeader
->StringsLength
);
928 out
= fopen(path2
, "wb");
931 perror("Cannot open output file");
937 if (CreateOutputFile(out
, FileData
, PEDosHeader
, PEFileHeader
, PEOptHeader
,
938 PESectionHeaders
, RosSymLength
, RosSymSection
))