3894d406dea83527fcd494ac5454c01e8e9f85c5
[reactos.git] / reactos / tools / rsym / rsym.c
1 /*
2 * Usage: rsym input-file output-file
3 *
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
12 * possible.
13 *
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.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <assert.h>
27
28 #include "rsym.h"
29
30 static int
31 CompareSymEntry(const PROSSYM_ENTRY SymEntry1, const PROSSYM_ENTRY SymEntry2)
32 {
33 if (SymEntry1->Address < SymEntry2->Address)
34 {
35 return -1;
36 }
37
38 if (SymEntry2->Address < SymEntry1->Address)
39 {
40 return +1;
41 }
42
43 return 0;
44 }
45
46 static int
47 GetStabInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
48 PIMAGE_SECTION_HEADER PESectionHeaders,
49 ULONG *StabSymbolsLength, void **StabSymbolsBase,
50 ULONG *StabStringsLength, void **StabStringsBase)
51 {
52 ULONG Idx;
53
54 /* Load .stab and .stabstr sections if available */
55 *StabSymbolsBase = NULL;
56 *StabSymbolsLength = 0;
57 *StabStringsBase = NULL;
58 *StabStringsLength = 0;
59
60 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
61 {
62 /* printf("section: '%.08s'\n", PESectionHeaders[Idx].Name); */
63 if ((strncmp((char*)PESectionHeaders[Idx].Name, ".stab", 5) == 0)
64 && (PESectionHeaders[Idx].Name[5] == 0))
65 {
66 /* printf(".stab section found. Size %d\n",
67 PESectionHeaders[Idx].SizeOfRawData); */
68
69 *StabSymbolsLength = PESectionHeaders[Idx].SizeOfRawData;
70 *StabSymbolsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
71 }
72
73 if (strncmp((char*)PESectionHeaders[Idx].Name, ".stabstr", 8) == 0)
74 {
75 /* printf(".stabstr section found. Size %d\n",
76 PESectionHeaders[Idx].SizeOfRawData); */
77
78 *StabStringsLength = PESectionHeaders[Idx].SizeOfRawData;
79 *StabStringsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
80 }
81 }
82
83 return 0;
84 }
85
86 static int
87 GetCoffInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
88 PIMAGE_SECTION_HEADER PESectionHeaders,
89 ULONG *CoffSymbolsLength, void **CoffSymbolsBase,
90 ULONG *CoffStringsLength, void **CoffStringsBase)
91 {
92
93 if (0 == PEFileHeader->PointerToSymbolTable || 0 == PEFileHeader->NumberOfSymbols)
94 {
95 /* No COFF symbol table */
96 *CoffSymbolsLength = 0;
97 *CoffStringsLength = 0;
98 }
99 else
100 {
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);
105 }
106
107 return 0;
108 }
109
110 static ULONG
111 FindOrAddString(char *StringToFind, ULONG *StringsLength, void *StringsBase)
112 {
113 char *Search, *End;
114
115 Search = (char *) StringsBase;
116 End = Search + *StringsLength;
117
118 while (Search < End)
119 {
120 if (0 == strcmp(Search, StringToFind))
121 {
122 return Search - (char *) StringsBase;
123 }
124 Search += strlen(Search) + 1;
125 }
126
127 strcpy(Search, StringToFind);
128 *StringsLength += strlen(StringToFind) + 1;
129
130 return Search - (char *) StringsBase;
131 }
132
133 static int
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)
140 {
141 PSTAB_ENTRY StabEntry;
142 ULONG Count, i;
143 ULONG_PTR Address, LastFunctionAddress;
144 int First = 1;
145 char *Name;
146 ULONG NameLen;
147 char FuncName[256];
148 PROSSYM_ENTRY Current;
149
150 StabEntry = StabSymbolsBase;
151 Count = StabSymbolsLength / sizeof(STAB_ENTRY);
152 *SymbolsCount = 0;
153 if (Count == 0)
154 {
155 /* No symbol info */
156 *SymbolsBase = NULL;
157 return 0;
158 }
159
160 *SymbolsBase = malloc(Count * sizeof(ROSSYM_ENTRY));
161 if (NULL == *SymbolsBase)
162 {
163 fprintf(stderr, "Failed to allocate memory for converted .stab symbols\n");
164 return 1;
165 }
166 Current = *SymbolsBase;
167 memset ( Current, 0, sizeof(*Current) );
168
169 LastFunctionAddress = 0;
170 for (i = 0; i < Count; i++)
171 {
172 if ( 0 == LastFunctionAddress )
173 {
174 Address = StabEntry[i].n_value - ImageBase;
175 }
176 else
177 {
178 Address = LastFunctionAddress + StabEntry[i].n_value;
179 }
180 switch (StabEntry[i].n_type)
181 {
182 case N_SO:
183 case N_SOL:
184 case N_BINCL:
185 Name = (char *) StabStringsBase + StabEntry[i].n_strx;
186 if (StabStringsLength < StabEntry[i].n_strx
187 ||'\0' == *Name || '/' == Name[strlen(Name) - 1]
188 || '\\' == Name[strlen(Name) - 1]
189 || StabEntry[i].n_value < ImageBase)
190 {
191 continue;
192 }
193 if ( First || Address != Current->Address )
194 {
195 if ( !First )
196 {
197 memset ( ++Current, 0, sizeof(*Current) );
198 Current->FunctionOffset = Current[-1].FunctionOffset;
199 }
200 else
201 First = 0;
202 Current->Address = Address;
203 }
204 Current->FileOffset = FindOrAddString((char *)StabStringsBase
205 + StabEntry[i].n_strx,
206 StringsLength,
207 StringsBase);
208 break;
209 case N_FUN:
210 if (0 == StabEntry[i].n_desc || StabEntry[i].n_value < ImageBase)
211 {
212 LastFunctionAddress = 0; /* line # 0 = end of function */
213 continue;
214 }
215 if ( First || Address != Current->Address )
216 {
217 if ( !First )
218 memset ( ++Current, 0, sizeof(*Current) );
219 else
220 First = 0;
221 Current->Address = Address;
222 Current->FileOffset = Current[-1].FileOffset;
223 }
224 Name = (char *) StabStringsBase + StabEntry[i].n_strx;
225 NameLen = strcspn(Name, ":");
226 if (sizeof(FuncName) <= NameLen)
227 {
228 free(*SymbolsBase);
229 fprintf(stderr, "Function name too long\n");
230 return 1;
231 }
232 memcpy(FuncName, Name, NameLen);
233 FuncName[NameLen] = '\0';
234 Current->FunctionOffset = FindOrAddString(FuncName,
235 StringsLength,
236 StringsBase);
237 Current->SourceLine = 0;
238 LastFunctionAddress = Address;
239 break;
240 case N_SLINE:
241 if ( First || Address != Current->Address )
242 {
243 if ( !First )
244 {
245 memset ( ++Current, 0, sizeof(*Current) );
246 Current->FileOffset = Current[-1].FileOffset;
247 Current->FunctionOffset = Current[-1].FunctionOffset;
248 }
249 else
250 First = 0;
251 Current->Address = Address;
252 }
253 Current->SourceLine = StabEntry[i].n_desc;
254 break;
255 default:
256 continue;
257 }
258 }
259 *SymbolsCount = (Current - *SymbolsBase + 1);
260
261 qsort(*SymbolsBase, *SymbolsCount, sizeof(ROSSYM_ENTRY), (int (*)(const void *, const void *)) CompareSymEntry);
262
263 return 0;
264 }
265
266 static int
267 ConvertCoffs(ULONG *SymbolsCount, PROSSYM_ENTRY *SymbolsBase,
268 ULONG *StringsLength, void *StringsBase,
269 ULONG CoffSymbolsLength, void *CoffSymbolsBase,
270 ULONG CoffStringsLength, void *CoffStringsBase,
271 ULONG_PTR ImageBase, PIMAGE_FILE_HEADER PEFileHeader,
272 PIMAGE_SECTION_HEADER PESectionHeaders)
273 {
274 ULONG Count, i;
275 PCOFF_SYMENT CoffEntry;
276 char FuncName[256];
277 char *p;
278 PROSSYM_ENTRY Current;
279
280 CoffEntry = (PCOFF_SYMENT) CoffSymbolsBase;
281 Count = CoffSymbolsLength / sizeof(COFF_SYMENT);
282 *SymbolsBase = malloc(Count * sizeof(ROSSYM_ENTRY));
283 if (NULL == *SymbolsBase)
284 {
285 fprintf(stderr, "Unable to allocate memory for converted COFF symbols\n");
286 return 1;
287 }
288 *SymbolsCount = 0;
289 Current = *SymbolsBase;
290
291 for (i = 0; i < Count; i++)
292 {
293 if (ISFCN(CoffEntry[i].e_type) || C_EXT == CoffEntry[i].e_sclass)
294 {
295 Current->Address = CoffEntry[i].e_value;
296 if (0 < CoffEntry[i].e_scnum)
297 {
298 if (PEFileHeader->NumberOfSections < CoffEntry[i].e_scnum)
299 {
300 free(*SymbolsBase);
301 fprintf(stderr, "Invalid section number %d in COFF symbols (only %d sections present)\n",
302 CoffEntry[i].e_scnum, PEFileHeader->NumberOfSections);
303 return 1;
304 }
305 Current->Address += PESectionHeaders[CoffEntry[i].e_scnum - 1].VirtualAddress;
306 }
307 Current->FileOffset = 0;
308 if (0 == CoffEntry[i].e.e.e_zeroes)
309 {
310 if (sizeof(FuncName) <= strlen((char *) CoffStringsBase + CoffEntry[i].e.e.e_offset))
311 {
312 free(*SymbolsBase);
313 fprintf(stderr, "Function name too long\n");
314 return 1;
315 }
316 strcpy(FuncName, (char *) CoffStringsBase + CoffEntry[i].e.e.e_offset);
317 }
318 else
319 {
320 memcpy(FuncName, CoffEntry[i].e.e_name, E_SYMNMLEN);
321 FuncName[E_SYMNMLEN] = '\0';
322 }
323
324 /* Name demangling: stdcall */
325 p = strrchr(FuncName, '@');
326 if (NULL != p)
327 {
328 *p = '\0';
329 }
330 p = ('_' == FuncName[0] || '@' == FuncName[0] ? FuncName + 1 : FuncName);
331 Current->FunctionOffset = FindOrAddString(p,
332 StringsLength,
333 StringsBase);
334 Current->SourceLine = 0;
335 memset ( ++Current, 0, sizeof(*Current) );
336 }
337 i += CoffEntry[i].e_numaux;
338 }
339
340 *SymbolsCount = (Current - *SymbolsBase + 1);
341 qsort(*SymbolsBase, *SymbolsCount, sizeof(ROSSYM_ENTRY), (int (*)(const void *, const void *)) CompareSymEntry);
342
343 return 0;
344 }
345
346 static int
347 MergeStabsAndCoffs(ULONG *MergedSymbolCount, PROSSYM_ENTRY *MergedSymbols,
348 ULONG StabSymbolsCount, PROSSYM_ENTRY StabSymbols,
349 ULONG CoffSymbolsCount, PROSSYM_ENTRY CoffSymbols)
350 {
351 ULONG StabIndex, j;
352 ULONG CoffIndex;
353 ULONG_PTR StabFunctionStartAddress;
354 ULONG StabFunctionStringOffset, NewStabFunctionStringOffset;
355
356 *MergedSymbolCount = 0;
357 if (StabSymbolsCount == 0)
358 {
359 *MergedSymbols = NULL;
360 return 0;
361 }
362 *MergedSymbols = malloc(StabSymbolsCount * sizeof(ROSSYM_ENTRY));
363 if (NULL == *MergedSymbols)
364 {
365 fprintf(stderr, "Unable to allocate memory for merged symbols\n");
366 return 1;
367 }
368
369 StabFunctionStartAddress = 0;
370 StabFunctionStringOffset = 0;
371 CoffIndex = 0;
372 for (StabIndex = 0; StabIndex < StabSymbolsCount; StabIndex++)
373 {
374 (*MergedSymbols)[*MergedSymbolCount] = StabSymbols[StabIndex];
375 for (j = StabIndex + 1;
376 j < StabSymbolsCount && StabSymbols[j].Address == StabSymbols[StabIndex].Address;
377 j++)
378 {
379 if (0 != StabSymbols[j].FileOffset && 0 == (*MergedSymbols)[*MergedSymbolCount].FileOffset)
380 {
381 (*MergedSymbols)[*MergedSymbolCount].FileOffset = StabSymbols[j].FileOffset;
382 }
383 if (0 != StabSymbols[j].FunctionOffset && 0 == (*MergedSymbols)[*MergedSymbolCount].FunctionOffset)
384 {
385 (*MergedSymbols)[*MergedSymbolCount].FunctionOffset = StabSymbols[j].FunctionOffset;
386 }
387 if (0 != StabSymbols[j].SourceLine && 0 == (*MergedSymbols)[*MergedSymbolCount].SourceLine)
388 {
389 (*MergedSymbols)[*MergedSymbolCount].SourceLine = StabSymbols[j].SourceLine;
390 }
391 }
392 StabIndex = j - 1;
393 while (CoffIndex < CoffSymbolsCount
394 && CoffSymbols[CoffIndex + 1].Address <= (*MergedSymbols)[*MergedSymbolCount].Address)
395 {
396 CoffIndex++;
397 }
398 NewStabFunctionStringOffset = (*MergedSymbols)[*MergedSymbolCount].FunctionOffset;
399 if (CoffSymbolsCount > 0 &&
400 CoffSymbols[CoffIndex].Address < (*MergedSymbols)[*MergedSymbolCount].Address
401 && StabFunctionStartAddress < CoffSymbols[CoffIndex].Address
402 && 0 != CoffSymbols[CoffIndex].FunctionOffset)
403 {
404 (*MergedSymbols)[*MergedSymbolCount].FunctionOffset = CoffSymbols[CoffIndex].FunctionOffset;
405 }
406 if (StabFunctionStringOffset != NewStabFunctionStringOffset)
407 {
408 StabFunctionStartAddress = (*MergedSymbols)[*MergedSymbolCount].Address;
409 }
410 StabFunctionStringOffset = NewStabFunctionStringOffset;
411 (*MergedSymbolCount)++;
412 }
413
414 return 0;
415 }
416
417 static PIMAGE_SECTION_HEADER
418 FindSectionForRVA(DWORD RVA, unsigned NumberOfSections, PIMAGE_SECTION_HEADER SectionHeaders)
419 {
420 unsigned Section;
421
422 for (Section = 0; Section < NumberOfSections; Section++)
423 {
424 if (SectionHeaders[Section].VirtualAddress <= RVA &&
425 RVA < SectionHeaders[Section].VirtualAddress + SectionHeaders[Section].Misc.VirtualSize)
426 {
427 return SectionHeaders + Section;
428 }
429 }
430
431 return NULL;
432 }
433
434 static int
435 ProcessRelocations(ULONG *ProcessedRelocsLength, void **ProcessedRelocs,
436 void *RawData, PIMAGE_OPTIONAL_HEADER OptHeader,
437 unsigned NumberOfSections, PIMAGE_SECTION_HEADER SectionHeaders)
438 {
439 PIMAGE_SECTION_HEADER RelocSectionHeader, TargetSectionHeader;
440 PIMAGE_BASE_RELOCATION BaseReloc, End, AcceptedRelocs;
441 int Found;
442
443 if (OptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
444 || 0 == OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
445 {
446 /* No relocation entries */
447 *ProcessedRelocsLength = 0;
448 *ProcessedRelocs = NULL;
449 return 0;
450 }
451
452 RelocSectionHeader = FindSectionForRVA(OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
453 NumberOfSections, SectionHeaders);
454 if (NULL == RelocSectionHeader)
455 {
456 fprintf(stderr, "Can't find section header for relocation data\n");
457 return 1;
458 }
459
460 *ProcessedRelocs = malloc(RelocSectionHeader->SizeOfRawData);
461 if (NULL == *ProcessedRelocs)
462 {
463 fprintf(stderr, "Failed to allocate %u bytes for relocations\n", (unsigned int)RelocSectionHeader->SizeOfRawData);
464 return 1;
465 }
466 *ProcessedRelocsLength = 0;
467
468 BaseReloc = (PIMAGE_BASE_RELOCATION) ((char *) RawData
469 + RelocSectionHeader->PointerToRawData
470 + (OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress -
471 RelocSectionHeader->VirtualAddress));
472 End = (PIMAGE_BASE_RELOCATION) ((char *) BaseReloc
473 + OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
474
475 while (BaseReloc < End && 0 < BaseReloc->SizeOfBlock)
476 {
477 TargetSectionHeader = FindSectionForRVA(BaseReloc->VirtualAddress, NumberOfSections,
478 SectionHeaders);
479 if (NULL != TargetSectionHeader)
480 {
481 AcceptedRelocs = *ProcessedRelocs;
482 Found = 0;
483 while (AcceptedRelocs < (PIMAGE_BASE_RELOCATION) ((char *) *ProcessedRelocs +
484 *ProcessedRelocsLength)
485 && ! Found)
486 {
487 Found = BaseReloc->SizeOfBlock == AcceptedRelocs->SizeOfBlock
488 && 0 == memcmp(BaseReloc, AcceptedRelocs, AcceptedRelocs->SizeOfBlock);
489 AcceptedRelocs= (PIMAGE_BASE_RELOCATION) ((char *) AcceptedRelocs +
490 AcceptedRelocs->SizeOfBlock);
491 }
492 if (! Found)
493 {
494 memcpy((char *) *ProcessedRelocs + *ProcessedRelocsLength,
495 BaseReloc, BaseReloc->SizeOfBlock);
496 *ProcessedRelocsLength += BaseReloc->SizeOfBlock;
497 }
498 }
499 BaseReloc = (PIMAGE_BASE_RELOCATION)((char *) BaseReloc + BaseReloc->SizeOfBlock);
500 }
501
502 return 0;
503 }
504
505 static int
506 CreateOutputFile(FILE *OutFile, void *InData,
507 PIMAGE_DOS_HEADER InDosHeader, PIMAGE_FILE_HEADER InFileHeader,
508 PIMAGE_OPTIONAL_HEADER InOptHeader, PIMAGE_SECTION_HEADER InSectionHeaders,
509 ULONG RosSymLength, void *RosSymSection)
510 {
511 ULONG StartOfRawData;
512 unsigned Section;
513 void *OutHeader, *ProcessedRelocs, *PaddedRosSym, *Data;
514 PIMAGE_DOS_HEADER OutDosHeader;
515 PIMAGE_FILE_HEADER OutFileHeader;
516 PIMAGE_OPTIONAL_HEADER OutOptHeader;
517 PIMAGE_SECTION_HEADER OutSectionHeaders, CurrentSectionHeader;
518 DWORD CheckSum;
519 ULONG Length, i;
520 ULONG ProcessedRelocsLength;
521 ULONG RosSymOffset, RosSymFileLength;
522 int InRelocSectionIndex;
523 PIMAGE_SECTION_HEADER OutRelocSection;
524
525 StartOfRawData = 0;
526 for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
527 {
528 if ((0 == StartOfRawData
529 || InSectionHeaders[Section].PointerToRawData < StartOfRawData)
530 && 0 != InSectionHeaders[Section].PointerToRawData
531 && 0 != (strncmp(InSectionHeaders[Section].Name, ".stab", 5)))
532 {
533 StartOfRawData = InSectionHeaders[Section].PointerToRawData;
534 }
535 }
536 OutHeader = malloc(StartOfRawData);
537 if (NULL == OutHeader)
538 {
539 fprintf(stderr, "Failed to allocate %u bytes for output file header\n", (unsigned int)StartOfRawData);
540 return 1;
541 }
542 memset(OutHeader, '\0', StartOfRawData);
543
544 OutDosHeader = (PIMAGE_DOS_HEADER) OutHeader;
545 memcpy(OutDosHeader, InDosHeader, InDosHeader->e_lfanew + sizeof(ULONG));
546
547 OutFileHeader = (PIMAGE_FILE_HEADER)((char *) OutHeader + OutDosHeader->e_lfanew + sizeof(ULONG));
548 memcpy(OutFileHeader, InFileHeader, sizeof(IMAGE_FILE_HEADER));
549 OutFileHeader->PointerToSymbolTable = 0;
550 OutFileHeader->NumberOfSymbols = 0;
551 OutFileHeader->Characteristics &= ~(IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_LOCAL_SYMS_STRIPPED |
552 IMAGE_FILE_DEBUG_STRIPPED);
553
554 OutOptHeader = (PIMAGE_OPTIONAL_HEADER)(OutFileHeader + 1);
555 memcpy(OutOptHeader, InOptHeader, sizeof(IMAGE_OPTIONAL_HEADER));
556 OutOptHeader->CheckSum = 0;
557
558 OutSectionHeaders = (PIMAGE_SECTION_HEADER)((char *) OutOptHeader + OutFileHeader->SizeOfOptionalHeader);
559
560 if (ProcessRelocations(&ProcessedRelocsLength, &ProcessedRelocs, InData, InOptHeader,
561 InFileHeader->NumberOfSections, InSectionHeaders))
562 {
563 return 1;
564 }
565 if (InOptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
566 || 0 == InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
567 {
568 InRelocSectionIndex = -1;
569 }
570 else
571 {
572 InRelocSectionIndex = FindSectionForRVA(InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
573 InFileHeader->NumberOfSections, InSectionHeaders) - InSectionHeaders;
574 }
575
576 OutFileHeader->NumberOfSections = 0;
577 CurrentSectionHeader = OutSectionHeaders;
578 OutOptHeader->SizeOfImage = 0;
579 RosSymOffset = 0;
580 OutRelocSection = NULL;
581 for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
582 {
583 if (0 != (strncmp(InSectionHeaders[Section].Name, ".stab", 5)))
584 {
585 *CurrentSectionHeader = InSectionHeaders[Section];
586 CurrentSectionHeader->PointerToLinenumbers = 0;
587 CurrentSectionHeader->NumberOfLinenumbers = 0;
588 if (OutOptHeader->SizeOfImage < CurrentSectionHeader->VirtualAddress +
589 CurrentSectionHeader->Misc.VirtualSize)
590 {
591 OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress +
592 CurrentSectionHeader->Misc.VirtualSize,
593 OutOptHeader->SectionAlignment);
594 }
595 if (RosSymOffset < CurrentSectionHeader->PointerToRawData + CurrentSectionHeader->SizeOfRawData)
596 {
597 RosSymOffset = CurrentSectionHeader->PointerToRawData + CurrentSectionHeader->SizeOfRawData;
598 }
599 if (Section == (ULONG)InRelocSectionIndex)
600 {
601 OutRelocSection = CurrentSectionHeader;
602 }
603 (OutFileHeader->NumberOfSections) ++;
604 CurrentSectionHeader++;
605 }
606 }
607
608 if (OutRelocSection == CurrentSectionHeader - 1)
609 {
610 OutOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = ProcessedRelocsLength;
611 if (OutOptHeader->SizeOfImage == OutRelocSection->VirtualAddress +
612 ROUND_UP(OutRelocSection->Misc.VirtualSize,
613 OutOptHeader->SectionAlignment))
614 {
615 OutOptHeader->SizeOfImage = OutRelocSection->VirtualAddress +
616 ROUND_UP(ProcessedRelocsLength,
617 OutOptHeader->SectionAlignment);
618 }
619 OutRelocSection->Misc.VirtualSize = ProcessedRelocsLength;
620 if (RosSymOffset == OutRelocSection->PointerToRawData
621 + OutRelocSection->SizeOfRawData)
622 {
623 RosSymOffset = OutRelocSection->PointerToRawData +
624 ROUND_UP(ProcessedRelocsLength, OutOptHeader->FileAlignment);
625 }
626 OutRelocSection->SizeOfRawData = ROUND_UP(ProcessedRelocsLength,
627 OutOptHeader->FileAlignment);
628 }
629
630 if (RosSymLength > 0)
631 {
632 RosSymFileLength = ROUND_UP(RosSymLength, OutOptHeader->FileAlignment);
633 memcpy(CurrentSectionHeader->Name, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
634 CurrentSectionHeader->Misc.VirtualSize = RosSymLength;
635 CurrentSectionHeader->VirtualAddress = OutOptHeader->SizeOfImage;
636 CurrentSectionHeader->SizeOfRawData = RosSymFileLength;
637 CurrentSectionHeader->PointerToRawData = RosSymOffset;
638 CurrentSectionHeader->PointerToRelocations = 0;
639 CurrentSectionHeader->PointerToLinenumbers = 0;
640 CurrentSectionHeader->NumberOfRelocations = 0;
641 CurrentSectionHeader->NumberOfLinenumbers = 0;
642 CurrentSectionHeader->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
643 | IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_TYPE_NOLOAD;
644 OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress +
645 CurrentSectionHeader->Misc.VirtualSize,
646 OutOptHeader->SectionAlignment);
647 (OutFileHeader->NumberOfSections)++;
648
649 PaddedRosSym = malloc(RosSymFileLength);
650 if (NULL == PaddedRosSym)
651 {
652 fprintf(stderr, "Failed to allocate %u bytes for padded .rossym\n", (unsigned int)RosSymFileLength);
653 return 1;
654 }
655 memcpy(PaddedRosSym, RosSymSection, RosSymLength);
656 memset((char *) PaddedRosSym + RosSymLength, '\0', RosSymFileLength - RosSymLength);
657 }
658 else
659 {
660 PaddedRosSym = NULL;
661 }
662 CheckSum = 0;
663 for (i = 0; i < StartOfRawData / 2; i++)
664 {
665 CheckSum += ((unsigned short*) OutHeader)[i];
666 CheckSum = 0xffff & (CheckSum + (CheckSum >> 16));
667 }
668 Length = StartOfRawData;
669 for (Section = 0; Section < OutFileHeader->NumberOfSections; Section++)
670 {
671 DWORD SizeOfRawData;
672 if (OutRelocSection == OutSectionHeaders + Section)
673 {
674 Data = (void *) ProcessedRelocs;
675 SizeOfRawData = ProcessedRelocsLength;
676 }
677 else if (RosSymLength > 0 && Section + 1 == OutFileHeader->NumberOfSections)
678 {
679 Data = (void *) PaddedRosSym;
680 SizeOfRawData = OutSectionHeaders[Section].SizeOfRawData;
681 }
682 else
683 {
684 Data = (void *) ((char *) InData + OutSectionHeaders[Section].PointerToRawData);
685 SizeOfRawData = OutSectionHeaders[Section].SizeOfRawData;
686 }
687 for (i = 0; i < SizeOfRawData / 2; i++)
688 {
689 CheckSum += ((unsigned short*) Data)[i];
690 CheckSum = 0xffff & (CheckSum + (CheckSum >> 16));
691 }
692 Length += OutSectionHeaders[Section].SizeOfRawData;
693 }
694 CheckSum += Length;
695 OutOptHeader->CheckSum = CheckSum;
696
697 if (fwrite(OutHeader, 1, StartOfRawData, OutFile) != StartOfRawData)
698 {
699 perror("Error writing output header\n");
700 free(OutHeader);
701 return 1;
702 }
703
704 for (Section = 0; Section < OutFileHeader->NumberOfSections; Section++)
705 {
706 if (0 != OutSectionHeaders[Section].SizeOfRawData)
707 {
708 DWORD SizeOfRawData;
709 fseek(OutFile, OutSectionHeaders[Section].PointerToRawData, SEEK_SET);
710 if (OutRelocSection == OutSectionHeaders + Section)
711 {
712 Data = (void *) ProcessedRelocs;
713 SizeOfRawData = ProcessedRelocsLength;
714 }
715 else if (RosSymLength > 0 && Section + 1 == OutFileHeader->NumberOfSections)
716 {
717 Data = (void *) PaddedRosSym;
718 SizeOfRawData = OutSectionHeaders[Section].SizeOfRawData;
719 }
720 else
721 {
722 Data = (void *) ((char *) InData + OutSectionHeaders[Section].PointerToRawData);
723 SizeOfRawData = OutSectionHeaders[Section].SizeOfRawData;
724 }
725 if (fwrite(Data, 1, SizeOfRawData, OutFile) != SizeOfRawData)
726 {
727 perror("Error writing section data\n");
728 free(PaddedRosSym);
729 free(OutHeader);
730 return 1;
731 }
732 }
733 }
734
735 if (PaddedRosSym)
736 {
737 free(PaddedRosSym);
738 }
739 free(OutHeader);
740
741 return 0;
742 }
743
744 int main(int argc, char* argv[])
745 {
746 PSYMBOLFILE_HEADER SymbolFileHeader;
747 PIMAGE_DOS_HEADER PEDosHeader;
748 PIMAGE_FILE_HEADER PEFileHeader;
749 PIMAGE_OPTIONAL_HEADER PEOptHeader;
750 PIMAGE_SECTION_HEADER PESectionHeaders;
751 ULONG ImageBase;
752 void *StabBase;
753 ULONG StabsLength;
754 void *StabStringBase;
755 ULONG StabStringsLength;
756 void *CoffBase = NULL;
757 ULONG CoffsLength;
758 void *CoffStringBase = NULL;
759 ULONG CoffStringsLength;
760 char* path1;
761 char* path2;
762 FILE* out;
763 void *StringBase;
764 ULONG StringsLength;
765 ULONG StabSymbolsCount;
766 PROSSYM_ENTRY StabSymbols;
767 ULONG CoffSymbolsCount;
768 PROSSYM_ENTRY CoffSymbols;
769 ULONG MergedSymbolsCount;
770 PROSSYM_ENTRY MergedSymbols;
771 size_t FileSize;
772 void *FileData;
773 ULONG RosSymLength;
774 void *RosSymSection;
775 char elfhdr[4] = { '\177', 'E', 'L', 'F' };
776
777 if (3 != argc)
778 {
779 fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
780 exit(1);
781 }
782
783 path1 = convert_path(argv[1]);
784 path2 = convert_path(argv[2]);
785
786 FileData = load_file ( path1, &FileSize );
787 if ( !FileData )
788 {
789 fprintf ( stderr, "An error occured loading '%s'\n", path1 );
790 exit(1);
791 }
792
793 /* Check if MZ header exists */
794 PEDosHeader = (PIMAGE_DOS_HEADER) FileData;
795 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
796 {
797 /* Ignore elf */
798 if (!memcmp(PEDosHeader, elfhdr, sizeof(elfhdr)))
799 exit(0);
800 perror("Input file is not a PE image.\n");
801 free(FileData);
802 exit(1);
803 }
804
805 /* Locate PE file header */
806 /* sizeof(ULONG) = sizeof(MAGIC) */
807 PEFileHeader = (PIMAGE_FILE_HEADER)((char *) FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
808
809 /* Locate optional header */
810 assert(sizeof(ULONG) == 4);
811 PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
812 ImageBase = PEOptHeader->ImageBase;
813
814 /* Locate PE section headers */
815 PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
816
817 if (GetStabInfo(FileData, PEFileHeader, PESectionHeaders, &StabsLength, &StabBase,
818 &StabStringsLength, &StabStringBase))
819 {
820 free(FileData);
821 exit(1);
822 }
823
824 if (GetCoffInfo(FileData, PEFileHeader, PESectionHeaders, &CoffsLength, &CoffBase,
825 &CoffStringsLength, &CoffStringBase))
826 {
827 free(FileData);
828 exit(1);
829 }
830
831 StringBase = malloc(1 + StabStringsLength + CoffStringsLength +
832 (CoffsLength / sizeof(ROSSYM_ENTRY)) * (E_SYMNMLEN + 1));
833 if (NULL == StringBase)
834 {
835 free(FileData);
836 fprintf(stderr, "Failed to allocate memory for strings table\n");
837 exit(1);
838 }
839 /* Make offset 0 into an empty string */
840 *((char *) StringBase) = '\0';
841 StringsLength = 1;
842
843 if (ConvertStabs(&StabSymbolsCount, &StabSymbols, &StringsLength, StringBase,
844 StabsLength, StabBase, StabStringsLength, StabStringBase,
845 ImageBase, PEFileHeader, PESectionHeaders))
846 {
847 free(StringBase);
848 free(FileData);
849 fprintf(stderr, "Failed to allocate memory for strings table\n");
850 exit(1);
851 }
852
853 if (ConvertCoffs(&CoffSymbolsCount, &CoffSymbols, &StringsLength, StringBase,
854 CoffsLength, CoffBase, CoffStringsLength, CoffStringBase,
855 ImageBase, PEFileHeader, PESectionHeaders))
856 {
857 if (StabSymbols)
858 {
859 free(StabSymbols);
860 }
861 free(StringBase);
862 free(FileData);
863 exit(1);
864 }
865
866 if (MergeStabsAndCoffs(&MergedSymbolsCount, &MergedSymbols,
867 StabSymbolsCount, StabSymbols,
868 CoffSymbolsCount, CoffSymbols))
869 {
870 if (CoffSymbols)
871 {
872 free(CoffSymbols);
873 }
874 if (StabSymbols)
875 {
876 free(StabSymbols);
877 }
878 free(StringBase);
879 free(FileData);
880 exit(1);
881 }
882
883 if (CoffSymbols)
884 {
885 free(CoffSymbols);
886 }
887 if (StabSymbols)
888 {
889 free(StabSymbols);
890 }
891 if (MergedSymbolsCount == 0)
892 {
893 RosSymLength = 0;
894 RosSymSection = NULL;
895 }
896 else
897 {
898 RosSymLength = sizeof(SYMBOLFILE_HEADER) + MergedSymbolsCount * sizeof(ROSSYM_ENTRY)
899 + StringsLength;
900 RosSymSection = malloc(RosSymLength);
901 if (NULL == RosSymSection)
902 {
903 free(MergedSymbols);
904 free(StringBase);
905 free(FileData);
906 fprintf(stderr, "Unable to allocate memory for .rossym section\n");
907 exit(1);
908 }
909 memset(RosSymSection, '\0', RosSymLength);
910
911 SymbolFileHeader = (PSYMBOLFILE_HEADER) RosSymSection;
912 SymbolFileHeader->SymbolsOffset = sizeof(SYMBOLFILE_HEADER);
913 SymbolFileHeader->SymbolsLength = MergedSymbolsCount * sizeof(ROSSYM_ENTRY);
914 SymbolFileHeader->StringsOffset = SymbolFileHeader->SymbolsOffset + SymbolFileHeader->SymbolsLength;
915 SymbolFileHeader->StringsLength = StringsLength;
916
917 memcpy((char *) RosSymSection + SymbolFileHeader->SymbolsOffset, MergedSymbols,
918 SymbolFileHeader->SymbolsLength);
919 memcpy((char *) RosSymSection + SymbolFileHeader->StringsOffset, StringBase,
920 SymbolFileHeader->StringsLength);
921
922 free(MergedSymbols);
923 }
924 free(StringBase);
925 out = fopen(path2, "wb");
926 if (out == NULL)
927 {
928 perror("Cannot open output file");
929 free(RosSymSection);
930 free(FileData);
931 exit(1);
932 }
933
934 if (CreateOutputFile(out, FileData, PEDosHeader, PEFileHeader, PEOptHeader,
935 PESectionHeaders, RosSymLength, RosSymSection))
936 {
937 fclose(out);
938 if (RosSymSection)
939 {
940 free(RosSymSection);
941 }
942 free(FileData);
943 exit(1);
944 }
945
946 fclose(out);
947 if (RosSymSection)
948 {
949 free(RosSymSection);
950 }
951 free(FileData);
952
953 return 0;
954 }
955
956 /* EOF */