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
;
147 PROSSYM_ENTRY Current
;
149 StabEntry
= StabSymbolsBase
;
150 Count
= StabSymbolsLength
/ sizeof(STAB_ENTRY
);
159 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
160 if (NULL
== *SymbolsBase
)
162 fprintf(stderr
, "Failed to allocate memory for converted .stab symbols\n");
165 Current
= *SymbolsBase
;
166 memset ( Current
, 0, sizeof(*Current
) );
168 LastFunctionAddress
= 0;
169 for (i
= 0; i
< Count
; i
++)
171 if ( 0 == LastFunctionAddress
)
173 Address
= StabEntry
[i
].n_value
- ImageBase
;
177 Address
= LastFunctionAddress
+ StabEntry
[i
].n_value
;
179 switch (StabEntry
[i
].n_type
)
184 Name
= (char *) StabStringsBase
+ StabEntry
[i
].n_strx
;
185 if (StabStringsLength
< StabEntry
[i
].n_strx
186 ||'\0' == *Name
|| '/' == Name
[strlen(Name
) - 1]
187 || '\\' == Name
[strlen(Name
) - 1]
188 || StabEntry
[i
].n_value
< ImageBase
)
192 if ( First
|| Address
!= Current
->Address
)
196 memset ( ++Current
, 0, sizeof(*Current
) );
197 Current
->FunctionOffset
= Current
[-1].FunctionOffset
;
201 Current
->Address
= Address
;
203 Current
->FileOffset
= FindOrAddString((char *)StabStringsBase
204 + StabEntry
[i
].n_strx
,
209 if (0 == StabEntry
[i
].n_desc
|| StabEntry
[i
].n_value
< ImageBase
)
211 LastFunctionAddress
= 0; /* line # 0 = end of function */
214 if ( First
|| Address
!= Current
->Address
)
217 memset ( ++Current
, 0, sizeof(*Current
) );
220 Current
->Address
= Address
;
221 Current
->FileOffset
= Current
[-1].FileOffset
;
223 if (sizeof(FuncName
) <= strlen((char *) StabStringsBase
+ StabEntry
[i
].n_strx
))
226 fprintf(stderr
, "Function name too long\n");
229 strcpy(FuncName
, (char *) StabStringsBase
+ StabEntry
[i
].n_strx
);
230 Name
= strchr(FuncName
, ':');
235 Current
->FunctionOffset
= FindOrAddString(FuncName
,
238 Current
->SourceLine
= 0;
239 LastFunctionAddress
= Address
;
242 if ( First
|| Address
!= Current
->Address
)
246 memset ( ++Current
, 0, sizeof(*Current
) );
247 Current
->FileOffset
= Current
[-1].FileOffset
;
248 Current
->FunctionOffset
= Current
[-1].FunctionOffset
;
252 Current
->Address
= Address
;
254 Current
->SourceLine
= StabEntry
[i
].n_desc
;
260 *SymbolsCount
= (Current
- *SymbolsBase
+ 1);
262 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
268 ConvertCoffs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
269 ULONG
*StringsLength
, void *StringsBase
,
270 ULONG CoffSymbolsLength
, void *CoffSymbolsBase
,
271 ULONG CoffStringsLength
, void *CoffStringsBase
,
272 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
273 PIMAGE_SECTION_HEADER PESectionHeaders
)
276 PCOFF_SYMENT CoffEntry
;
279 PROSSYM_ENTRY Current
;
281 CoffEntry
= (PCOFF_SYMENT
) CoffSymbolsBase
;
282 Count
= CoffSymbolsLength
/ sizeof(COFF_SYMENT
);
283 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
284 if (NULL
== *SymbolsBase
)
286 fprintf(stderr
, "Unable to allocate memory for converted COFF symbols\n");
290 Current
= *SymbolsBase
;
292 for (i
= 0; i
< Count
; i
++)
294 if (ISFCN(CoffEntry
[i
].e_type
) || C_EXT
== CoffEntry
[i
].e_sclass
)
296 Current
->Address
= CoffEntry
[i
].e_value
;
297 if (0 < CoffEntry
[i
].e_scnum
)
299 if (PEFileHeader
->NumberOfSections
< CoffEntry
[i
].e_scnum
)
302 fprintf(stderr
, "Invalid section number %d in COFF symbols (only %d sections present)\n",
303 CoffEntry
[i
].e_scnum
, PEFileHeader
->NumberOfSections
);
306 Current
->Address
+= PESectionHeaders
[CoffEntry
[i
].e_scnum
- 1].VirtualAddress
;
308 Current
->FileOffset
= 0;
309 if (0 == CoffEntry
[i
].e
.e
.e_zeroes
)
311 if (sizeof(FuncName
) <= strlen((char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
))
314 fprintf(stderr
, "Function name too long\n");
317 strcpy(FuncName
, (char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
);
321 memcpy(FuncName
, CoffEntry
[i
].e
.e_name
, E_SYMNMLEN
);
322 FuncName
[E_SYMNMLEN
] = '\0';
325 /* Name demangling: stdcall */
326 p
= strrchr(FuncName
, '@');
331 p
= ('_' == FuncName
[0] || '@' == FuncName
[0] ? FuncName
+ 1 : FuncName
);
332 Current
->FunctionOffset
= FindOrAddString(p
,
335 Current
->SourceLine
= 0;
336 memset ( ++Current
, 0, sizeof(*Current
) );
338 i
+= CoffEntry
[i
].e_numaux
;
341 *SymbolsCount
= (Current
- *SymbolsBase
+ 1);
342 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
348 MergeStabsAndCoffs(ULONG
*MergedSymbolCount
, PROSSYM_ENTRY
*MergedSymbols
,
349 ULONG StabSymbolsCount
, PROSSYM_ENTRY StabSymbols
,
350 ULONG CoffSymbolsCount
, PROSSYM_ENTRY CoffSymbols
)
354 ULONG_PTR StabFunctionStartAddress
;
355 ULONG StabFunctionStringOffset
, NewStabFunctionStringOffset
;
357 *MergedSymbolCount
= 0;
358 if (StabSymbolsCount
== 0)
360 *MergedSymbols
= NULL
;
363 *MergedSymbols
= malloc(StabSymbolsCount
* sizeof(ROSSYM_ENTRY
));
364 if (NULL
== *MergedSymbols
)
366 fprintf(stderr
, "Unable to allocate memory for merged symbols\n");
370 StabFunctionStartAddress
= 0;
371 StabFunctionStringOffset
= 0;
373 for (StabIndex
= 0; StabIndex
< StabSymbolsCount
; StabIndex
++)
375 (*MergedSymbols
)[*MergedSymbolCount
] = StabSymbols
[StabIndex
];
376 for (j
= StabIndex
+ 1;
377 j
< StabSymbolsCount
&& StabSymbols
[j
].Address
== StabSymbols
[StabIndex
].Address
;
380 if (0 != StabSymbols
[j
].FileOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
)
382 (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
= StabSymbols
[j
].FileOffset
;
384 if (0 != StabSymbols
[j
].FunctionOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
)
386 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= StabSymbols
[j
].FunctionOffset
;
388 if (0 != StabSymbols
[j
].SourceLine
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
)
390 (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
= StabSymbols
[j
].SourceLine
;
394 while (CoffIndex
< CoffSymbolsCount
395 && CoffSymbols
[CoffIndex
+ 1].Address
<= (*MergedSymbols
)[*MergedSymbolCount
].Address
)
399 NewStabFunctionStringOffset
= (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
;
400 if (CoffSymbolsCount
> 0 &&
401 CoffSymbols
[CoffIndex
].Address
< (*MergedSymbols
)[*MergedSymbolCount
].Address
402 && StabFunctionStartAddress
< CoffSymbols
[CoffIndex
].Address
403 && 0 != CoffSymbols
[CoffIndex
].FunctionOffset
)
405 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= CoffSymbols
[CoffIndex
].FunctionOffset
;
407 if (StabFunctionStringOffset
!= NewStabFunctionStringOffset
)
409 StabFunctionStartAddress
= (*MergedSymbols
)[*MergedSymbolCount
].Address
;
411 StabFunctionStringOffset
= NewStabFunctionStringOffset
;
412 (*MergedSymbolCount
)++;
418 static PIMAGE_SECTION_HEADER
419 FindSectionForRVA(DWORD RVA
, unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
423 for (Section
= 0; Section
< NumberOfSections
; Section
++)
425 if (SectionHeaders
[Section
].VirtualAddress
<= RVA
&&
426 RVA
< SectionHeaders
[Section
].VirtualAddress
+ SectionHeaders
[Section
].Misc
.VirtualSize
)
428 return SectionHeaders
+ Section
;
436 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader
)
438 static char *BlacklistedSections
[] =
444 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
447 if (0 != (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
))
452 for (i
= 0; i
< sizeof(BlacklistedSections
) / sizeof(BlacklistedSections
[0]); i
++)
454 strncpy(SectionName
, BlacklistedSections
[i
], IMAGE_SIZEOF_SHORT_NAME
);
455 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
465 ProcessRelocations(ULONG
*ProcessedRelocsLength
, void **ProcessedRelocs
,
466 void *RawData
, PIMAGE_OPTIONAL_HEADER OptHeader
,
467 unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
469 PIMAGE_SECTION_HEADER RelocSectionHeader
, TargetSectionHeader
;
470 PIMAGE_BASE_RELOCATION BaseReloc
, End
, AcceptedRelocs
;
473 if (OptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
474 || 0 == OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
476 /* No relocation entries */
477 *ProcessedRelocsLength
= 0;
478 *ProcessedRelocs
= NULL
;
482 RelocSectionHeader
= FindSectionForRVA(OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
483 NumberOfSections
, SectionHeaders
);
484 if (NULL
== RelocSectionHeader
)
486 fprintf(stderr
, "Can't find section header for relocation data\n");
490 *ProcessedRelocs
= malloc(RelocSectionHeader
->SizeOfRawData
);
491 if (NULL
== *ProcessedRelocs
)
493 fprintf(stderr
, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader
->SizeOfRawData
);
496 *ProcessedRelocsLength
= 0;
498 BaseReloc
= (PIMAGE_BASE_RELOCATION
) ((char *) RawData
499 + RelocSectionHeader
->PointerToRawData
500 + (OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
-
501 RelocSectionHeader
->VirtualAddress
));
502 End
= (PIMAGE_BASE_RELOCATION
) ((char *) BaseReloc
503 + OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
505 while (BaseReloc
< End
&& 0 < BaseReloc
->SizeOfBlock
)
507 TargetSectionHeader
= FindSectionForRVA(BaseReloc
->VirtualAddress
, NumberOfSections
,
509 if (NULL
!= TargetSectionHeader
&& IncludeRelocationsForSection(TargetSectionHeader
))
511 AcceptedRelocs
= *ProcessedRelocs
;
513 while (AcceptedRelocs
< (PIMAGE_BASE_RELOCATION
) ((char *) *ProcessedRelocs
+
514 *ProcessedRelocsLength
)
517 Found
= BaseReloc
->SizeOfBlock
== AcceptedRelocs
->SizeOfBlock
518 && 0 == memcmp(BaseReloc
, AcceptedRelocs
, AcceptedRelocs
->SizeOfBlock
);
519 AcceptedRelocs
= (PIMAGE_BASE_RELOCATION
) ((char *) AcceptedRelocs
+
520 AcceptedRelocs
->SizeOfBlock
);
524 memcpy((char *) *ProcessedRelocs
+ *ProcessedRelocsLength
,
525 BaseReloc
, BaseReloc
->SizeOfBlock
);
526 *ProcessedRelocsLength
+= BaseReloc
->SizeOfBlock
;
529 BaseReloc
= (PIMAGE_BASE_RELOCATION
)((char *) BaseReloc
+ BaseReloc
->SizeOfBlock
);
536 CreateOutputFile(FILE *OutFile
, void *InData
,
537 PIMAGE_DOS_HEADER InDosHeader
, PIMAGE_FILE_HEADER InFileHeader
,
538 PIMAGE_OPTIONAL_HEADER InOptHeader
, PIMAGE_SECTION_HEADER InSectionHeaders
,
539 ULONG RosSymLength
, void *RosSymSection
)
541 ULONG StartOfRawData
;
543 void *OutHeader
, *ProcessedRelocs
, *PaddedRosSym
, *Data
;
544 PIMAGE_DOS_HEADER OutDosHeader
;
545 PIMAGE_FILE_HEADER OutFileHeader
;
546 PIMAGE_OPTIONAL_HEADER OutOptHeader
;
547 PIMAGE_SECTION_HEADER OutSectionHeaders
, CurrentSectionHeader
;
550 ULONG ProcessedRelocsLength
;
551 ULONG RosSymOffset
, RosSymFileLength
;
552 int InRelocSectionIndex
;
553 PIMAGE_SECTION_HEADER OutRelocSection
;
556 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
558 if ((0 == StartOfRawData
559 || InSectionHeaders
[Section
].PointerToRawData
< StartOfRawData
)
560 && 0 != InSectionHeaders
[Section
].PointerToRawData
561 && 0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
563 StartOfRawData
= InSectionHeaders
[Section
].PointerToRawData
;
566 OutHeader
= malloc(StartOfRawData
);
567 if (NULL
== OutHeader
)
569 fprintf(stderr
, "Failed to allocate %lu bytes for output file header\n", StartOfRawData
);
572 memset(OutHeader
, '\0', StartOfRawData
);
574 OutDosHeader
= (PIMAGE_DOS_HEADER
) OutHeader
;
575 memcpy(OutDosHeader
, InDosHeader
, InDosHeader
->e_lfanew
+ sizeof(ULONG
));
577 OutFileHeader
= (PIMAGE_FILE_HEADER
)((char *) OutHeader
+ OutDosHeader
->e_lfanew
+ sizeof(ULONG
));
578 memcpy(OutFileHeader
, InFileHeader
, sizeof(IMAGE_FILE_HEADER
));
579 OutFileHeader
->PointerToSymbolTable
= 0;
580 OutFileHeader
->NumberOfSymbols
= 0;
581 OutFileHeader
->Characteristics
&= ~(IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
582 IMAGE_FILE_DEBUG_STRIPPED
);
584 OutOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(OutFileHeader
+ 1);
585 memcpy(OutOptHeader
, InOptHeader
, sizeof(IMAGE_OPTIONAL_HEADER
));
586 OutOptHeader
->CheckSum
= 0;
588 OutSectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) OutOptHeader
+ OutFileHeader
->SizeOfOptionalHeader
);
590 if (ProcessRelocations(&ProcessedRelocsLength
, &ProcessedRelocs
, InData
, InOptHeader
,
591 InFileHeader
->NumberOfSections
, InSectionHeaders
))
595 if (InOptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
596 || 0 == InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
598 InRelocSectionIndex
= -1;
602 InRelocSectionIndex
= FindSectionForRVA(InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
603 InFileHeader
->NumberOfSections
, InSectionHeaders
) - InSectionHeaders
;
606 OutFileHeader
->NumberOfSections
= 0;
607 CurrentSectionHeader
= OutSectionHeaders
;
608 OutOptHeader
->SizeOfImage
= 0;
610 OutRelocSection
= NULL
;
611 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
613 if (0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
615 *CurrentSectionHeader
= InSectionHeaders
[Section
];
616 CurrentSectionHeader
->PointerToLinenumbers
= 0;
617 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
618 if (OutOptHeader
->SizeOfImage
< CurrentSectionHeader
->VirtualAddress
+
619 CurrentSectionHeader
->Misc
.VirtualSize
)
621 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
622 CurrentSectionHeader
->Misc
.VirtualSize
,
623 OutOptHeader
->SectionAlignment
);
625 if (RosSymOffset
< CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
)
627 RosSymOffset
= CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
;
629 if (Section
== (ULONG
)InRelocSectionIndex
)
631 OutRelocSection
= CurrentSectionHeader
;
633 (OutFileHeader
->NumberOfSections
) ++;
634 CurrentSectionHeader
++;
638 if (OutRelocSection
== CurrentSectionHeader
- 1)
640 OutOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= ProcessedRelocsLength
;
641 if (OutOptHeader
->SizeOfImage
== OutRelocSection
->VirtualAddress
+
642 ROUND_UP(OutRelocSection
->Misc
.VirtualSize
,
643 OutOptHeader
->SectionAlignment
))
645 OutOptHeader
->SizeOfImage
= OutRelocSection
->VirtualAddress
+
646 ROUND_UP(ProcessedRelocsLength
,
647 OutOptHeader
->SectionAlignment
);
649 OutRelocSection
->Misc
.VirtualSize
= ProcessedRelocsLength
;
650 if (RosSymOffset
== OutRelocSection
->PointerToRawData
651 + OutRelocSection
->SizeOfRawData
)
653 RosSymOffset
= OutRelocSection
->PointerToRawData
+
654 ROUND_UP(ProcessedRelocsLength
, OutOptHeader
->FileAlignment
);
656 OutRelocSection
->SizeOfRawData
= ROUND_UP(ProcessedRelocsLength
,
657 OutOptHeader
->FileAlignment
);
660 if (RosSymLength
> 0)
662 RosSymFileLength
= ROUND_UP(RosSymLength
, OutOptHeader
->FileAlignment
);
663 memcpy(CurrentSectionHeader
->Name
, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
664 CurrentSectionHeader
->Misc
.VirtualSize
= RosSymLength
;
665 CurrentSectionHeader
->VirtualAddress
= OutOptHeader
->SizeOfImage
;
666 CurrentSectionHeader
->SizeOfRawData
= RosSymFileLength
;
667 CurrentSectionHeader
->PointerToRawData
= RosSymOffset
;
668 CurrentSectionHeader
->PointerToRelocations
= 0;
669 CurrentSectionHeader
->PointerToLinenumbers
= 0;
670 CurrentSectionHeader
->NumberOfRelocations
= 0;
671 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
672 CurrentSectionHeader
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_DISCARDABLE
673 | IMAGE_SCN_LNK_REMOVE
| IMAGE_SCN_TYPE_NOLOAD
;
674 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
675 CurrentSectionHeader
->Misc
.VirtualSize
,
676 OutOptHeader
->SectionAlignment
);
677 (OutFileHeader
->NumberOfSections
)++;
679 PaddedRosSym
= malloc(RosSymFileLength
);
680 if (NULL
== PaddedRosSym
)
682 fprintf(stderr
, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength
);
685 memcpy(PaddedRosSym
, RosSymSection
, RosSymLength
);
686 memset((char *) PaddedRosSym
+ RosSymLength
, '\0', RosSymFileLength
- RosSymLength
);
693 for (i
= 0; i
< StartOfRawData
/ 2; i
++)
695 CheckSum
+= ((unsigned short*) OutHeader
)[i
];
696 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
698 Length
= StartOfRawData
;
699 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
701 if (OutRelocSection
== OutSectionHeaders
+ Section
)
703 Data
= (void *) ProcessedRelocs
;
705 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
707 Data
= (void *) PaddedRosSym
;
711 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
713 for (i
= 0; i
< OutSectionHeaders
[Section
].SizeOfRawData
/ 2; i
++)
715 CheckSum
+= ((unsigned short*) Data
)[i
];
716 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
718 Length
+= OutSectionHeaders
[Section
].SizeOfRawData
;
721 OutOptHeader
->CheckSum
= CheckSum
;
723 if (fwrite(OutHeader
, 1, StartOfRawData
, OutFile
) != StartOfRawData
)
725 perror("Error writing output header\n");
730 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
732 if (0 != OutSectionHeaders
[Section
].SizeOfRawData
)
734 fseek(OutFile
, OutSectionHeaders
[Section
].PointerToRawData
, SEEK_SET
);
735 if (OutRelocSection
== OutSectionHeaders
+ Section
)
737 Data
= (void *) ProcessedRelocs
;
739 else if (RosSymLength
> 0 && Section
+ 1 == OutFileHeader
->NumberOfSections
)
741 Data
= (void *) PaddedRosSym
;
745 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
747 if (fwrite(Data
, 1, OutSectionHeaders
[Section
].SizeOfRawData
, OutFile
) !=
748 OutSectionHeaders
[Section
].SizeOfRawData
)
750 perror("Error writing section data\n");
767 int main(int argc
, char* argv
[])
769 PSYMBOLFILE_HEADER SymbolFileHeader
;
770 PIMAGE_DOS_HEADER PEDosHeader
;
771 PIMAGE_FILE_HEADER PEFileHeader
;
772 PIMAGE_OPTIONAL_HEADER PEOptHeader
;
773 PIMAGE_SECTION_HEADER PESectionHeaders
;
777 void *StabStringBase
;
778 ULONG StabStringsLength
;
779 void *CoffBase
= NULL
;
781 void *CoffStringBase
= NULL
;
782 ULONG CoffStringsLength
;
788 ULONG StabSymbolsCount
;
789 PROSSYM_ENTRY StabSymbols
;
790 ULONG CoffSymbolsCount
;
791 PROSSYM_ENTRY CoffSymbols
;
792 ULONG MergedSymbolsCount
;
793 PROSSYM_ENTRY MergedSymbols
;
801 fprintf(stderr
, "Usage: rsym <exefile> <symfile>\n");
805 path1
= convert_path(argv
[1]);
806 path2
= convert_path(argv
[2]);
808 FileData
= load_file ( path1
, &FileSize
);
811 fprintf ( stderr
, "An error occured loading '%s'\n", path1
);
815 /* Check if MZ header exists */
816 PEDosHeader
= (PIMAGE_DOS_HEADER
) FileData
;
817 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
|| PEDosHeader
->e_lfanew
== 0L)
819 perror("Input file is not a PE image.\n");
824 /* Locate PE file header */
825 /* sizeof(ULONG) = sizeof(MAGIC) */
826 PEFileHeader
= (PIMAGE_FILE_HEADER
)((char *) FileData
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
828 /* Locate optional header */
829 assert(sizeof(ULONG
) == 4);
830 PEOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(PEFileHeader
+ 1);
831 ImageBase
= PEOptHeader
->ImageBase
;
833 /* Locate PE section headers */
834 PESectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) PEOptHeader
+ PEFileHeader
->SizeOfOptionalHeader
);
836 if (GetStabInfo(FileData
, PEFileHeader
, PESectionHeaders
, &StabsLength
, &StabBase
,
837 &StabStringsLength
, &StabStringBase
))
843 if (GetCoffInfo(FileData
, PEFileHeader
, PESectionHeaders
, &CoffsLength
, &CoffBase
,
844 &CoffStringsLength
, &CoffStringBase
))
850 StringBase
= malloc(1 + StabStringsLength
+ CoffStringsLength
+
851 (CoffsLength
/ sizeof(ROSSYM_ENTRY
)) * (E_SYMNMLEN
+ 1));
852 if (NULL
== StringBase
)
855 fprintf(stderr
, "Failed to allocate memory for strings table\n");
858 /* Make offset 0 into an empty string */
859 *((char *) StringBase
) = '\0';
862 if (ConvertStabs(&StabSymbolsCount
, &StabSymbols
, &StringsLength
, StringBase
,
863 StabsLength
, StabBase
, StabStringsLength
, StabStringBase
,
864 ImageBase
, PEFileHeader
, PESectionHeaders
))
868 fprintf(stderr
, "Failed to allocate memory for strings table\n");
872 if (ConvertCoffs(&CoffSymbolsCount
, &CoffSymbols
, &StringsLength
, StringBase
,
873 CoffsLength
, CoffBase
, CoffStringsLength
, CoffStringBase
,
874 ImageBase
, PEFileHeader
, PESectionHeaders
))
885 if (MergeStabsAndCoffs(&MergedSymbolsCount
, &MergedSymbols
,
886 StabSymbolsCount
, StabSymbols
,
887 CoffSymbolsCount
, CoffSymbols
))
910 if (MergedSymbolsCount
== 0)
913 RosSymSection
= NULL
;
917 RosSymLength
= sizeof(SYMBOLFILE_HEADER
) + MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
)
919 RosSymSection
= malloc(RosSymLength
);
920 if (NULL
== RosSymSection
)
925 fprintf(stderr
, "Unable to allocate memory for .rossym section\n");
928 memset(RosSymSection
, '\0', RosSymLength
);
930 SymbolFileHeader
= (PSYMBOLFILE_HEADER
) RosSymSection
;
931 SymbolFileHeader
->SymbolsOffset
= sizeof(SYMBOLFILE_HEADER
);
932 SymbolFileHeader
->SymbolsLength
= MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
);
933 SymbolFileHeader
->StringsOffset
= SymbolFileHeader
->SymbolsOffset
+ SymbolFileHeader
->SymbolsLength
;
934 SymbolFileHeader
->StringsLength
= StringsLength
;
936 memcpy((char *) RosSymSection
+ SymbolFileHeader
->SymbolsOffset
, MergedSymbols
,
937 SymbolFileHeader
->SymbolsLength
);
938 memcpy((char *) RosSymSection
+ SymbolFileHeader
->StringsOffset
, StringBase
,
939 SymbolFileHeader
->StringsLength
);
944 out
= fopen(path2
, "wb");
947 perror("Cannot open output file");
953 if (CreateOutputFile(out
, FileData
, PEDosHeader
, PEFileHeader
, PEOptHeader
,
954 PESectionHeaders
, RosSymLength
, RosSymSection
))