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
);
149 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
150 if (NULL
== *SymbolsBase
)
152 fprintf(stderr
, "Failed to allocate memory for converted .stab symbols\n");
157 LastFunctionAddress
= 0;
159 for (i
= 0; i
< Count
; i
++)
161 switch (StabEntry
[i
].n_type
)
164 Name
= (char *) StabStringsBase
+ StabEntry
[i
].n_strx
;
165 if (StabStringsLength
< StabEntry
[i
].n_strx
166 ||'\0' == *Name
|| '/' == Name
[strlen(Name
) - 1]
167 || '\\' == Name
[strlen(Name
) - 1]
168 || StabEntry
[i
].n_value
< ImageBase
)
172 Address
= StabEntry
[i
].n_value
- ImageBase
;
173 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
177 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
178 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= FindOrAddString((char *) StabStringsBase
179 + StabEntry
[i
].n_strx
,
182 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= 0;
183 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
184 LastFunctionAddress
= 0;
187 if (0 == StabEntry
[i
].n_desc
|| StabEntry
[i
].n_value
< ImageBase
) /* line # 0 isn't valid */
191 Address
= StabEntry
[i
].n_value
- ImageBase
;
192 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
195 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
197 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
198 if (sizeof(FuncName
) <= strlen((char *) StabStringsBase
+ StabEntry
[i
].n_strx
))
201 fprintf(stderr
, "Function name too long\n");
204 strcpy(FuncName
, (char *) StabStringsBase
+ StabEntry
[i
].n_strx
);
205 Name
= strchr(FuncName
, ':');
210 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(FuncName
,
213 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
214 LastFunctionAddress
= Address
;
217 if (0 == LastFunctionAddress
)
219 Address
= StabEntry
[i
].n_value
- ImageBase
;
223 Address
= LastFunctionAddress
+ StabEntry
[i
].n_value
;
225 if (Address
!= (*SymbolsBase
)[*SymbolsCount
].Address
&& ! First
)
228 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FileOffset
;
229 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= (*SymbolsBase
)[*SymbolsCount
- 1].FunctionOffset
;
231 (*SymbolsBase
)[*SymbolsCount
].Address
= Address
;
232 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= StabEntry
[i
].n_desc
;
241 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
247 ConvertCoffs(ULONG
*SymbolsCount
, PROSSYM_ENTRY
*SymbolsBase
,
248 ULONG
*StringsLength
, void *StringsBase
,
249 ULONG CoffSymbolsLength
, void *CoffSymbolsBase
,
250 ULONG CoffStringsLength
, void *CoffStringsBase
,
251 ULONG_PTR ImageBase
, PIMAGE_FILE_HEADER PEFileHeader
,
252 PIMAGE_SECTION_HEADER PESectionHeaders
)
255 PCOFF_SYMENT CoffEntry
;
259 CoffEntry
= (PCOFF_SYMENT
) CoffSymbolsBase
;
260 Count
= CoffSymbolsLength
/ sizeof(COFF_SYMENT
);
261 *SymbolsBase
= malloc(Count
* sizeof(ROSSYM_ENTRY
));
262 if (NULL
== *SymbolsBase
)
264 fprintf(stderr
, "Unable to allocate memory for converted COFF symbols\n");
269 for (i
= 0; i
< Count
; i
++)
271 if (ISFCN(CoffEntry
[i
].e_type
) || C_EXT
== CoffEntry
[i
].e_sclass
)
273 (*SymbolsBase
)[*SymbolsCount
].Address
= CoffEntry
[i
].e_value
;
274 if (0 < CoffEntry
[i
].e_scnum
)
276 if (PEFileHeader
->NumberOfSections
< CoffEntry
[i
].e_scnum
)
279 fprintf(stderr
, "Invalid section number %d in COFF symbols (only %d sections present)\n",
280 CoffEntry
[i
].e_scnum
, PEFileHeader
->NumberOfSections
);
283 (*SymbolsBase
)[*SymbolsCount
].Address
+= PESectionHeaders
[CoffEntry
[i
].e_scnum
- 1].VirtualAddress
;
285 (*SymbolsBase
)[*SymbolsCount
].FileOffset
= 0;
286 if (0 == CoffEntry
[i
].e
.e
.e_zeroes
)
288 if (sizeof(FuncName
) <= strlen((char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
))
291 fprintf(stderr
, "Function name too long\n");
294 strcpy(FuncName
, (char *) CoffStringsBase
+ CoffEntry
[i
].e
.e
.e_offset
);
298 memcpy(FuncName
, CoffEntry
[i
].e
.e_name
, E_SYMNMLEN
);
299 FuncName
[E_SYMNMLEN
] = '\0';
302 /* Name demangling: stdcall */
303 p
= strrchr(FuncName
, '@');
308 p
= ('_' == FuncName
[0] || '@' == FuncName
[0] ? FuncName
+ 1 : FuncName
);
309 (*SymbolsBase
)[*SymbolsCount
].FunctionOffset
= FindOrAddString(p
,
312 (*SymbolsBase
)[*SymbolsCount
].SourceLine
= 0;
315 i
+= CoffEntry
[i
].e_numaux
;
318 qsort(*SymbolsBase
, *SymbolsCount
, sizeof(ROSSYM_ENTRY
), (int (*)(const void *, const void *)) CompareSymEntry
);
324 MergeStabsAndCoffs(ULONG
*MergedSymbolCount
, PROSSYM_ENTRY
*MergedSymbols
,
325 ULONG StabSymbolsCount
, PROSSYM_ENTRY StabSymbols
,
326 ULONG CoffSymbolsCount
, PROSSYM_ENTRY CoffSymbols
)
330 ULONG_PTR StabFunctionStartAddress
;
331 ULONG StabFunctionStringOffset
, NewStabFunctionStringOffset
;
333 *MergedSymbols
= malloc(StabSymbolsCount
* sizeof(ROSSYM_ENTRY
));
334 if (NULL
== *MergedSymbols
)
336 fprintf(stderr
, "Unable to allocate memory for merged symbols\n");
339 *MergedSymbolCount
= 0;
341 StabFunctionStartAddress
= 0;
342 StabFunctionStringOffset
= 0;
344 for (StabIndex
= 0; StabIndex
< StabSymbolsCount
; StabIndex
++)
346 (*MergedSymbols
)[*MergedSymbolCount
] = StabSymbols
[StabIndex
];
347 for (j
= StabIndex
+ 1;
348 j
< StabSymbolsCount
&& StabSymbols
[j
].Address
== StabSymbols
[StabIndex
].Address
;
351 if (0 != StabSymbols
[j
].FileOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
)
353 (*MergedSymbols
)[*MergedSymbolCount
].FileOffset
= StabSymbols
[j
].FileOffset
;
355 if (0 != StabSymbols
[j
].FunctionOffset
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
)
357 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= StabSymbols
[j
].FunctionOffset
;
359 if (0 != StabSymbols
[j
].SourceLine
&& 0 == (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
)
361 (*MergedSymbols
)[*MergedSymbolCount
].SourceLine
= StabSymbols
[j
].SourceLine
;
365 while (CoffIndex
< CoffSymbolsCount
366 && CoffSymbols
[CoffIndex
+ 1].Address
<= (*MergedSymbols
)[*MergedSymbolCount
].Address
)
370 NewStabFunctionStringOffset
= (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
;
371 if (CoffSymbols
[CoffIndex
].Address
< (*MergedSymbols
)[*MergedSymbolCount
].Address
372 && StabFunctionStartAddress
< CoffSymbols
[CoffIndex
].Address
373 && 0 != CoffSymbols
[CoffIndex
].FunctionOffset
)
375 (*MergedSymbols
)[*MergedSymbolCount
].FunctionOffset
= CoffSymbols
[CoffIndex
].FunctionOffset
;
377 if (StabFunctionStringOffset
!= NewStabFunctionStringOffset
)
379 StabFunctionStartAddress
= (*MergedSymbols
)[*MergedSymbolCount
].Address
;
381 StabFunctionStringOffset
= NewStabFunctionStringOffset
;
382 (*MergedSymbolCount
)++;
388 static PIMAGE_SECTION_HEADER
389 FindSectionForRVA(DWORD RVA
, unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
393 for (Section
= 0; Section
< NumberOfSections
; Section
++)
395 if (SectionHeaders
[Section
].VirtualAddress
<= RVA
&&
396 RVA
< SectionHeaders
[Section
].VirtualAddress
+ SectionHeaders
[Section
].Misc
.VirtualSize
)
398 return SectionHeaders
+ Section
;
406 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader
)
408 static char *BlacklistedSections
[] =
413 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
416 if (0 != (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
))
421 for (i
= 0; i
< sizeof(BlacklistedSections
) / sizeof(BlacklistedSections
[0]); i
++)
423 strncpy(SectionName
, BlacklistedSections
[i
], IMAGE_SIZEOF_SHORT_NAME
);
424 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
434 ProcessRelocations(ULONG
*ProcessedRelocsLength
, void **ProcessedRelocs
,
435 void *RawData
, PIMAGE_OPTIONAL_HEADER OptHeader
,
436 unsigned NumberOfSections
, PIMAGE_SECTION_HEADER SectionHeaders
)
438 PIMAGE_SECTION_HEADER RelocSectionHeader
, TargetSectionHeader
;
439 PIMAGE_BASE_RELOCATION BaseReloc
, End
, AcceptedRelocs
;
442 if (OptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
443 || 0 == OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
445 /* No relocation entries */
446 *ProcessedRelocsLength
= 0;
447 *ProcessedRelocs
= NULL
;
451 RelocSectionHeader
= FindSectionForRVA(OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
452 NumberOfSections
, SectionHeaders
);
453 if (NULL
== RelocSectionHeader
)
455 fprintf(stderr
, "Can't find section header for relocation data\n");
459 *ProcessedRelocs
= malloc(RelocSectionHeader
->SizeOfRawData
);
460 if (NULL
== *ProcessedRelocs
)
462 fprintf(stderr
, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader
->SizeOfRawData
);
465 *ProcessedRelocsLength
= 0;
467 BaseReloc
= (PIMAGE_BASE_RELOCATION
) ((char *) RawData
468 + RelocSectionHeader
->PointerToRawData
469 + (OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
-
470 RelocSectionHeader
->VirtualAddress
));
471 End
= (PIMAGE_BASE_RELOCATION
) ((char *) BaseReloc
472 + OptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
474 while (BaseReloc
< End
&& 0 < BaseReloc
->SizeOfBlock
)
476 TargetSectionHeader
= FindSectionForRVA(BaseReloc
->VirtualAddress
, NumberOfSections
,
478 if (NULL
!= TargetSectionHeader
&& IncludeRelocationsForSection(TargetSectionHeader
))
480 AcceptedRelocs
= *ProcessedRelocs
;
482 while (AcceptedRelocs
< (PIMAGE_BASE_RELOCATION
) ((char *) *ProcessedRelocs
+
483 *ProcessedRelocsLength
)
486 Found
= BaseReloc
->SizeOfBlock
== AcceptedRelocs
->SizeOfBlock
487 && 0 == memcmp(BaseReloc
, AcceptedRelocs
, AcceptedRelocs
->SizeOfBlock
);
488 AcceptedRelocs
= (PIMAGE_BASE_RELOCATION
) ((char *) AcceptedRelocs
+
489 AcceptedRelocs
->SizeOfBlock
);
493 memcpy((char *) *ProcessedRelocs
+ *ProcessedRelocsLength
,
494 BaseReloc
, BaseReloc
->SizeOfBlock
);
495 *ProcessedRelocsLength
+= BaseReloc
->SizeOfBlock
;
498 BaseReloc
= (PIMAGE_BASE_RELOCATION
)((char *) BaseReloc
+ BaseReloc
->SizeOfBlock
);
505 CreateOutputFile(FILE *OutFile
, void *InData
,
506 PIMAGE_DOS_HEADER InDosHeader
, PIMAGE_FILE_HEADER InFileHeader
,
507 PIMAGE_OPTIONAL_HEADER InOptHeader
, PIMAGE_SECTION_HEADER InSectionHeaders
,
508 ULONG RosSymLength
, void *RosSymSection
)
510 ULONG StartOfRawData
;
512 void *OutHeader
, *ProcessedRelocs
, *PaddedRosSym
, *Data
;
513 PIMAGE_DOS_HEADER OutDosHeader
;
514 PIMAGE_FILE_HEADER OutFileHeader
;
515 PIMAGE_OPTIONAL_HEADER OutOptHeader
;
516 PIMAGE_SECTION_HEADER OutSectionHeaders
, CurrentSectionHeader
;
519 ULONG ProcessedRelocsLength
;
520 ULONG RosSymOffset
, RosSymFileLength
;
521 int InRelocSectionIndex
;
522 PIMAGE_SECTION_HEADER OutRelocSection
;
525 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
527 if ((0 == StartOfRawData
528 || InSectionHeaders
[Section
].PointerToRawData
< StartOfRawData
)
529 && 0 != InSectionHeaders
[Section
].PointerToRawData
530 && 0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
532 StartOfRawData
= InSectionHeaders
[Section
].PointerToRawData
;
535 OutHeader
= malloc(StartOfRawData
);
536 if (NULL
== OutHeader
)
538 fprintf(stderr
, "Failed to allocate %lu bytes for output file header\n", StartOfRawData
);
541 memset(OutHeader
, '\0', StartOfRawData
);
543 OutDosHeader
= (PIMAGE_DOS_HEADER
) OutHeader
;
544 memcpy(OutDosHeader
, InDosHeader
, InDosHeader
->e_lfanew
+ sizeof(ULONG
));
546 OutFileHeader
= (PIMAGE_FILE_HEADER
)((char *) OutHeader
+ OutDosHeader
->e_lfanew
+ sizeof(ULONG
));
547 memcpy(OutFileHeader
, InFileHeader
, sizeof(IMAGE_FILE_HEADER
));
548 OutFileHeader
->PointerToSymbolTable
= 0;
549 OutFileHeader
->NumberOfSymbols
= 0;
550 OutFileHeader
->Characteristics
&= ~(IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
551 IMAGE_FILE_DEBUG_STRIPPED
);
553 OutOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(OutFileHeader
+ 1);
554 memcpy(OutOptHeader
, InOptHeader
, sizeof(IMAGE_OPTIONAL_HEADER
));
555 OutOptHeader
->CheckSum
= 0;
557 OutSectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) OutOptHeader
+ OutFileHeader
->SizeOfOptionalHeader
);
559 if (ProcessRelocations(&ProcessedRelocsLength
, &ProcessedRelocs
, InData
, InOptHeader
,
560 InFileHeader
->NumberOfSections
, InSectionHeaders
))
564 if (InOptHeader
->NumberOfRvaAndSizes
< IMAGE_DIRECTORY_ENTRY_BASERELOC
565 || 0 == InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
)
567 InRelocSectionIndex
= -1;
571 InRelocSectionIndex
= FindSectionForRVA(InOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
,
572 InFileHeader
->NumberOfSections
, InSectionHeaders
) - InSectionHeaders
;
575 OutFileHeader
->NumberOfSections
= 0;
576 CurrentSectionHeader
= OutSectionHeaders
;
577 OutOptHeader
->SizeOfImage
= 0;
579 OutRelocSection
= NULL
;
580 for (Section
= 0; Section
< InFileHeader
->NumberOfSections
; Section
++)
582 if (0 == (InSectionHeaders
[Section
].Characteristics
& IMAGE_SCN_LNK_REMOVE
))
584 *CurrentSectionHeader
= InSectionHeaders
[Section
];
585 CurrentSectionHeader
->PointerToLinenumbers
= 0;
586 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
587 if (OutOptHeader
->SizeOfImage
< CurrentSectionHeader
->VirtualAddress
+
588 CurrentSectionHeader
->Misc
.VirtualSize
)
590 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
591 CurrentSectionHeader
->Misc
.VirtualSize
,
592 OutOptHeader
->SectionAlignment
);
594 if (RosSymOffset
< CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
)
596 RosSymOffset
= CurrentSectionHeader
->PointerToRawData
+ CurrentSectionHeader
->SizeOfRawData
;
598 if (Section
== InRelocSectionIndex
)
600 OutRelocSection
= CurrentSectionHeader
;
602 (OutFileHeader
->NumberOfSections
) ++;
603 CurrentSectionHeader
++;
607 if (OutRelocSection
== CurrentSectionHeader
- 1)
609 OutOptHeader
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= ProcessedRelocsLength
;
610 if (OutOptHeader
->SizeOfImage
== OutRelocSection
->VirtualAddress
+
611 ROUND_UP(OutRelocSection
->Misc
.VirtualSize
,
612 OutOptHeader
->SectionAlignment
))
614 OutOptHeader
->SizeOfImage
= OutRelocSection
->VirtualAddress
+
615 ROUND_UP(ProcessedRelocsLength
,
616 OutOptHeader
->SectionAlignment
);
618 OutRelocSection
->Misc
.VirtualSize
= ProcessedRelocsLength
;
619 if (RosSymOffset
== OutRelocSection
->PointerToRawData
620 + OutRelocSection
->SizeOfRawData
)
622 RosSymOffset
= OutRelocSection
->PointerToRawData
+
623 ROUND_UP(ProcessedRelocsLength
, OutOptHeader
->FileAlignment
);
625 OutRelocSection
->SizeOfRawData
= ROUND_UP(ProcessedRelocsLength
,
626 OutOptHeader
->FileAlignment
);
629 RosSymFileLength
= ROUND_UP(RosSymLength
, OutOptHeader
->FileAlignment
);
630 memcpy(CurrentSectionHeader
->Name
, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
631 CurrentSectionHeader
->Misc
.VirtualSize
= RosSymLength
;
632 CurrentSectionHeader
->VirtualAddress
= OutOptHeader
->SizeOfImage
;
633 CurrentSectionHeader
->SizeOfRawData
= RosSymFileLength
;
634 CurrentSectionHeader
->PointerToRawData
= RosSymOffset
;
635 CurrentSectionHeader
->PointerToRelocations
= 0;
636 CurrentSectionHeader
->PointerToLinenumbers
= 0;
637 CurrentSectionHeader
->NumberOfRelocations
= 0;
638 CurrentSectionHeader
->NumberOfLinenumbers
= 0;
639 CurrentSectionHeader
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_DISCARDABLE
640 | IMAGE_SCN_LNK_REMOVE
| IMAGE_SCN_TYPE_NOLOAD
;
641 OutOptHeader
->SizeOfImage
= ROUND_UP(CurrentSectionHeader
->VirtualAddress
+
642 CurrentSectionHeader
->Misc
.VirtualSize
,
643 OutOptHeader
->SectionAlignment
);
644 (OutFileHeader
->NumberOfSections
)++;
646 PaddedRosSym
= malloc(RosSymFileLength
);
647 if (NULL
== PaddedRosSym
)
649 fprintf(stderr
, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength
);
652 memcpy(PaddedRosSym
, RosSymSection
, RosSymLength
);
653 memset((char *) PaddedRosSym
+ RosSymLength
, '\0', RosSymFileLength
- RosSymLength
);
656 for (i
= 0; i
< StartOfRawData
/ 2; i
++)
658 CheckSum
+= ((unsigned short*) OutHeader
)[i
];
659 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
661 Length
= StartOfRawData
;
662 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
664 if (OutRelocSection
== OutSectionHeaders
+ Section
)
666 Data
= (void *) ProcessedRelocs
;
668 else if (Section
+ 1 == OutFileHeader
->NumberOfSections
)
670 Data
= (void *) PaddedRosSym
;
674 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
676 for (i
= 0; i
< OutSectionHeaders
[Section
].SizeOfRawData
/ 2; i
++)
678 CheckSum
+= ((unsigned short*) Data
)[i
];
679 CheckSum
= 0xffff & (CheckSum
+ (CheckSum
>> 16));
681 Length
+= OutSectionHeaders
[Section
].SizeOfRawData
;
684 OutOptHeader
->CheckSum
= CheckSum
;
686 if (fwrite(OutHeader
, 1, StartOfRawData
, OutFile
) != StartOfRawData
)
688 perror("Error writing output header\n");
693 for (Section
= 0; Section
< OutFileHeader
->NumberOfSections
; Section
++)
695 if (0 != OutSectionHeaders
[Section
].SizeOfRawData
)
697 fseek(OutFile
, OutSectionHeaders
[Section
].PointerToRawData
, SEEK_SET
);
698 if (OutRelocSection
== OutSectionHeaders
+ Section
)
700 Data
= (void *) ProcessedRelocs
;
702 else if (Section
+ 1 == OutFileHeader
->NumberOfSections
)
704 Data
= (void *) PaddedRosSym
;
708 Data
= (void *) ((char *) InData
+ OutSectionHeaders
[Section
].PointerToRawData
);
710 if (fwrite(Data
, 1, OutSectionHeaders
[Section
].SizeOfRawData
, OutFile
) !=
711 OutSectionHeaders
[Section
].SizeOfRawData
)
713 perror("Error writing section data\n");
727 int main(int argc
, char* argv
[])
729 PSYMBOLFILE_HEADER SymbolFileHeader
;
730 PIMAGE_DOS_HEADER PEDosHeader
;
731 PIMAGE_FILE_HEADER PEFileHeader
;
732 PIMAGE_OPTIONAL_HEADER PEOptHeader
;
733 PIMAGE_SECTION_HEADER PESectionHeaders
;
737 void *StabStringBase
;
738 ULONG StabStringsLength
;
741 void *CoffStringBase
;
742 ULONG CoffStringsLength
;
748 ULONG StabSymbolsCount
;
749 PROSSYM_ENTRY StabSymbols
;
750 ULONG CoffSymbolsCount
;
751 PROSSYM_ENTRY CoffSymbols
;
752 ULONG MergedSymbolsCount
;
753 PROSSYM_ENTRY MergedSymbols
;
761 fprintf(stderr
, "Usage: rsym <exefile> <symfile>\n");
765 path1
= convert_path(argv
[1]);
766 path2
= convert_path(argv
[2]);
768 FileData
= load_file ( path1
, &FileSize
);
771 fprintf ( stderr
, "An error occured loading '%s'\n", path1
);
775 /* Check if MZ header exists */
776 PEDosHeader
= (PIMAGE_DOS_HEADER
) FileData
;
777 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
|| PEDosHeader
->e_lfanew
== 0L)
779 perror("Input file is not a PE image.\n");
784 /* Locate PE file header */
785 /* sizeof(ULONG) = sizeof(MAGIC) */
786 PEFileHeader
= (PIMAGE_FILE_HEADER
)((char *) FileData
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
788 /* Locate optional header */
789 PEOptHeader
= (PIMAGE_OPTIONAL_HEADER
)(PEFileHeader
+ 1);
790 ImageBase
= PEOptHeader
->ImageBase
;
792 /* Locate PE section headers */
793 PESectionHeaders
= (PIMAGE_SECTION_HEADER
)((char *) PEOptHeader
+ PEFileHeader
->SizeOfOptionalHeader
);
795 if (GetStabInfo(FileData
, PEFileHeader
, PESectionHeaders
, &StabsLength
, &StabBase
,
796 &StabStringsLength
, &StabStringBase
))
802 if (GetCoffInfo(FileData
, PEFileHeader
, PESectionHeaders
, &CoffsLength
, &CoffBase
,
803 &CoffStringsLength
, &CoffStringBase
))
809 StringBase
= malloc(1 + StabStringsLength
+ CoffStringsLength
+
810 (CoffsLength
/ sizeof(ROSSYM_ENTRY
)) * (E_SYMNMLEN
+ 1));
811 if (NULL
== StringBase
)
814 fprintf(stderr
, "Failed to allocate memory for strings table\n");
817 /* Make offset 0 into an empty string */
818 *((char *) StringBase
) = '\0';
821 if (ConvertStabs(&StabSymbolsCount
, &StabSymbols
, &StringsLength
, StringBase
,
822 StabsLength
, StabBase
, StabStringsLength
, StabStringBase
,
823 ImageBase
, PEFileHeader
, PESectionHeaders
))
827 fprintf(stderr
, "Failed to allocate memory for strings table\n");
831 if (ConvertCoffs(&CoffSymbolsCount
, &CoffSymbols
, &StringsLength
, StringBase
,
832 CoffsLength
, CoffBase
, CoffStringsLength
, CoffStringBase
,
833 ImageBase
, PEFileHeader
, PESectionHeaders
))
841 if (MergeStabsAndCoffs(&MergedSymbolsCount
, &MergedSymbols
,
842 StabSymbolsCount
, StabSymbols
,
843 CoffSymbolsCount
, CoffSymbols
))
855 RosSymLength
= sizeof(SYMBOLFILE_HEADER
) + MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
)
857 RosSymSection
= malloc(RosSymLength
);
858 if (NULL
== RosSymSection
)
863 fprintf(stderr
, "Unable to allocate memory for .rossym section\n");
866 memset(RosSymSection
, '\0', RosSymLength
);
868 SymbolFileHeader
= (PSYMBOLFILE_HEADER
) RosSymSection
;
869 SymbolFileHeader
->SymbolsOffset
= sizeof(SYMBOLFILE_HEADER
);
870 SymbolFileHeader
->SymbolsLength
= MergedSymbolsCount
* sizeof(ROSSYM_ENTRY
);
871 SymbolFileHeader
->StringsOffset
= SymbolFileHeader
->SymbolsOffset
+ SymbolFileHeader
->SymbolsLength
;
872 SymbolFileHeader
->StringsLength
= StringsLength
;
874 memcpy((char *) RosSymSection
+ SymbolFileHeader
->SymbolsOffset
, MergedSymbols
,
875 SymbolFileHeader
->SymbolsLength
);
876 memcpy((char *) RosSymSection
+ SymbolFileHeader
->StringsOffset
, StringBase
,
877 SymbolFileHeader
->StringsLength
);
882 out
= fopen(path2
, "wb");
885 perror("Cannot open output file");
891 if (CreateOutputFile(out
, FileData
, PEDosHeader
, PEFileHeader
, PEOptHeader
,
892 PESectionHeaders
, RosSymLength
, RosSymSection
))