merge trunk head (37902)
[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 IncludeRelocationsForSection(PIMAGE_SECTION_HEADER SectionHeader)
436 {
437 static char *BlacklistedSections[] =
438 {
439 ".edata",
440 ".idata",
441 ".reloc"
442 };
443 char SectionName[IMAGE_SIZEOF_SHORT_NAME];
444 unsigned i;
445
446 if (0 != (SectionHeader->Characteristics & IMAGE_SCN_LNK_REMOVE))
447 {
448 return 0;
449 }
450
451 for (i = 0; i < sizeof(BlacklistedSections) / sizeof(BlacklistedSections[0]); i++)
452 {
453 strncpy(SectionName, BlacklistedSections[i], IMAGE_SIZEOF_SHORT_NAME);
454 if (0 == memcmp(SectionName, SectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME))
455 {
456 return 0;
457 }
458 }
459
460 return 1;
461 }
462
463 static int
464 ProcessRelocations(ULONG *ProcessedRelocsLength, void **ProcessedRelocs,
465 void *RawData, PIMAGE_OPTIONAL_HEADER OptHeader,
466 unsigned NumberOfSections, PIMAGE_SECTION_HEADER SectionHeaders)
467 {
468 PIMAGE_SECTION_HEADER RelocSectionHeader, TargetSectionHeader;
469 PIMAGE_BASE_RELOCATION BaseReloc, End, AcceptedRelocs;
470 int Found;
471
472 if (OptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
473 || 0 == OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
474 {
475 /* No relocation entries */
476 *ProcessedRelocsLength = 0;
477 *ProcessedRelocs = NULL;
478 return 0;
479 }
480
481 RelocSectionHeader = FindSectionForRVA(OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
482 NumberOfSections, SectionHeaders);
483 if (NULL == RelocSectionHeader)
484 {
485 fprintf(stderr, "Can't find section header for relocation data\n");
486 return 1;
487 }
488
489 *ProcessedRelocs = malloc(RelocSectionHeader->SizeOfRawData);
490 if (NULL == *ProcessedRelocs)
491 {
492 fprintf(stderr, "Failed to allocate %lu bytes for relocations\n", RelocSectionHeader->SizeOfRawData);
493 return 1;
494 }
495 *ProcessedRelocsLength = 0;
496
497 BaseReloc = (PIMAGE_BASE_RELOCATION) ((char *) RawData
498 + RelocSectionHeader->PointerToRawData
499 + (OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress -
500 RelocSectionHeader->VirtualAddress));
501 End = (PIMAGE_BASE_RELOCATION) ((char *) BaseReloc
502 + OptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
503
504 while (BaseReloc < End && 0 < BaseReloc->SizeOfBlock)
505 {
506 TargetSectionHeader = FindSectionForRVA(BaseReloc->VirtualAddress, NumberOfSections,
507 SectionHeaders);
508 if (NULL != TargetSectionHeader && IncludeRelocationsForSection(TargetSectionHeader))
509 {
510 AcceptedRelocs = *ProcessedRelocs;
511 Found = 0;
512 while (AcceptedRelocs < (PIMAGE_BASE_RELOCATION) ((char *) *ProcessedRelocs +
513 *ProcessedRelocsLength)
514 && ! Found)
515 {
516 Found = BaseReloc->SizeOfBlock == AcceptedRelocs->SizeOfBlock
517 && 0 == memcmp(BaseReloc, AcceptedRelocs, AcceptedRelocs->SizeOfBlock);
518 AcceptedRelocs= (PIMAGE_BASE_RELOCATION) ((char *) AcceptedRelocs +
519 AcceptedRelocs->SizeOfBlock);
520 }
521 if (! Found)
522 {
523 memcpy((char *) *ProcessedRelocs + *ProcessedRelocsLength,
524 BaseReloc, BaseReloc->SizeOfBlock);
525 *ProcessedRelocsLength += BaseReloc->SizeOfBlock;
526 }
527 }
528 BaseReloc = (PIMAGE_BASE_RELOCATION)((char *) BaseReloc + BaseReloc->SizeOfBlock);
529 }
530
531 return 0;
532 }
533
534 static int
535 CreateOutputFile(FILE *OutFile, void *InData,
536 PIMAGE_DOS_HEADER InDosHeader, PIMAGE_FILE_HEADER InFileHeader,
537 PIMAGE_OPTIONAL_HEADER InOptHeader, PIMAGE_SECTION_HEADER InSectionHeaders,
538 ULONG RosSymLength, void *RosSymSection)
539 {
540 ULONG StartOfRawData;
541 unsigned Section;
542 void *OutHeader, *ProcessedRelocs, *PaddedRosSym, *Data;
543 PIMAGE_DOS_HEADER OutDosHeader;
544 PIMAGE_FILE_HEADER OutFileHeader;
545 PIMAGE_OPTIONAL_HEADER OutOptHeader;
546 PIMAGE_SECTION_HEADER OutSectionHeaders, CurrentSectionHeader;
547 DWORD CheckSum;
548 ULONG Length, i;
549 ULONG ProcessedRelocsLength;
550 ULONG RosSymOffset, RosSymFileLength;
551 int InRelocSectionIndex;
552 PIMAGE_SECTION_HEADER OutRelocSection;
553
554 StartOfRawData = 0;
555 for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
556 {
557 if ((0 == StartOfRawData
558 || InSectionHeaders[Section].PointerToRawData < StartOfRawData)
559 && 0 != InSectionHeaders[Section].PointerToRawData
560 && 0 == (InSectionHeaders[Section].Characteristics & IMAGE_SCN_LNK_REMOVE))
561 {
562 StartOfRawData = InSectionHeaders[Section].PointerToRawData;
563 }
564 }
565 OutHeader = malloc(StartOfRawData);
566 if (NULL == OutHeader)
567 {
568 fprintf(stderr, "Failed to allocate %lu bytes for output file header\n", StartOfRawData);
569 return 1;
570 }
571 memset(OutHeader, '\0', StartOfRawData);
572
573 OutDosHeader = (PIMAGE_DOS_HEADER) OutHeader;
574 memcpy(OutDosHeader, InDosHeader, InDosHeader->e_lfanew + sizeof(ULONG));
575
576 OutFileHeader = (PIMAGE_FILE_HEADER)((char *) OutHeader + OutDosHeader->e_lfanew + sizeof(ULONG));
577 memcpy(OutFileHeader, InFileHeader, sizeof(IMAGE_FILE_HEADER));
578 OutFileHeader->PointerToSymbolTable = 0;
579 OutFileHeader->NumberOfSymbols = 0;
580 OutFileHeader->Characteristics &= ~(IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_LOCAL_SYMS_STRIPPED |
581 IMAGE_FILE_DEBUG_STRIPPED);
582
583 OutOptHeader = (PIMAGE_OPTIONAL_HEADER)(OutFileHeader + 1);
584 memcpy(OutOptHeader, InOptHeader, sizeof(IMAGE_OPTIONAL_HEADER));
585 OutOptHeader->CheckSum = 0;
586
587 OutSectionHeaders = (PIMAGE_SECTION_HEADER)((char *) OutOptHeader + OutFileHeader->SizeOfOptionalHeader);
588
589 if (ProcessRelocations(&ProcessedRelocsLength, &ProcessedRelocs, InData, InOptHeader,
590 InFileHeader->NumberOfSections, InSectionHeaders))
591 {
592 return 1;
593 }
594 if (InOptHeader->NumberOfRvaAndSizes < IMAGE_DIRECTORY_ENTRY_BASERELOC
595 || 0 == InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
596 {
597 InRelocSectionIndex = -1;
598 }
599 else
600 {
601 InRelocSectionIndex = FindSectionForRVA(InOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
602 InFileHeader->NumberOfSections, InSectionHeaders) - InSectionHeaders;
603 }
604
605 OutFileHeader->NumberOfSections = 0;
606 CurrentSectionHeader = OutSectionHeaders;
607 OutOptHeader->SizeOfImage = 0;
608 RosSymOffset = 0;
609 OutRelocSection = NULL;
610 for (Section = 0; Section < InFileHeader->NumberOfSections; Section++)
611 {
612 if (0 == (InSectionHeaders[Section].Characteristics & IMAGE_SCN_LNK_REMOVE))
613 {
614 *CurrentSectionHeader = InSectionHeaders[Section];
615 CurrentSectionHeader->PointerToLinenumbers = 0;
616 CurrentSectionHeader->NumberOfLinenumbers = 0;
617 if (OutOptHeader->SizeOfImage < CurrentSectionHeader->VirtualAddress +
618 CurrentSectionHeader->Misc.VirtualSize)
619 {
620 OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress +
621 CurrentSectionHeader->Misc.VirtualSize,
622 OutOptHeader->SectionAlignment);
623 }
624 if (RosSymOffset < CurrentSectionHeader->PointerToRawData + CurrentSectionHeader->SizeOfRawData)
625 {
626 RosSymOffset = CurrentSectionHeader->PointerToRawData + CurrentSectionHeader->SizeOfRawData;
627 }
628 if (Section == (ULONG)InRelocSectionIndex)
629 {
630 OutRelocSection = CurrentSectionHeader;
631 }
632 (OutFileHeader->NumberOfSections) ++;
633 CurrentSectionHeader++;
634 }
635 }
636
637 if (OutRelocSection == CurrentSectionHeader - 1)
638 {
639 OutOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = ProcessedRelocsLength;
640 if (OutOptHeader->SizeOfImage == OutRelocSection->VirtualAddress +
641 ROUND_UP(OutRelocSection->Misc.VirtualSize,
642 OutOptHeader->SectionAlignment))
643 {
644 OutOptHeader->SizeOfImage = OutRelocSection->VirtualAddress +
645 ROUND_UP(ProcessedRelocsLength,
646 OutOptHeader->SectionAlignment);
647 }
648 OutRelocSection->Misc.VirtualSize = ProcessedRelocsLength;
649 if (RosSymOffset == OutRelocSection->PointerToRawData
650 + OutRelocSection->SizeOfRawData)
651 {
652 RosSymOffset = OutRelocSection->PointerToRawData +
653 ROUND_UP(ProcessedRelocsLength, OutOptHeader->FileAlignment);
654 }
655 OutRelocSection->SizeOfRawData = ROUND_UP(ProcessedRelocsLength,
656 OutOptHeader->FileAlignment);
657 }
658
659 if (RosSymLength > 0)
660 {
661 RosSymFileLength = ROUND_UP(RosSymLength, OutOptHeader->FileAlignment);
662 memcpy(CurrentSectionHeader->Name, ".rossym", 8); /* We're lucky: string is exactly 8 bytes long */
663 CurrentSectionHeader->Misc.VirtualSize = RosSymLength;
664 CurrentSectionHeader->VirtualAddress = OutOptHeader->SizeOfImage;
665 CurrentSectionHeader->SizeOfRawData = RosSymFileLength;
666 CurrentSectionHeader->PointerToRawData = RosSymOffset;
667 CurrentSectionHeader->PointerToRelocations = 0;
668 CurrentSectionHeader->PointerToLinenumbers = 0;
669 CurrentSectionHeader->NumberOfRelocations = 0;
670 CurrentSectionHeader->NumberOfLinenumbers = 0;
671 CurrentSectionHeader->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
672 | IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_TYPE_NOLOAD;
673 OutOptHeader->SizeOfImage = ROUND_UP(CurrentSectionHeader->VirtualAddress +
674 CurrentSectionHeader->Misc.VirtualSize,
675 OutOptHeader->SectionAlignment);
676 (OutFileHeader->NumberOfSections)++;
677
678 PaddedRosSym = malloc(RosSymFileLength);
679 if (NULL == PaddedRosSym)
680 {
681 fprintf(stderr, "Failed to allocate %lu bytes for padded .rossym\n", RosSymFileLength);
682 return 1;
683 }
684 memcpy(PaddedRosSym, RosSymSection, RosSymLength);
685 memset((char *) PaddedRosSym + RosSymLength, '\0', RosSymFileLength - RosSymLength);
686 }
687 else
688 {
689 PaddedRosSym = NULL;
690 }
691 CheckSum = 0;
692 for (i = 0; i < StartOfRawData / 2; i++)
693 {
694 CheckSum += ((unsigned short*) OutHeader)[i];
695 CheckSum = 0xffff & (CheckSum + (CheckSum >> 16));
696 }
697 Length = StartOfRawData;
698 for (Section = 0; Section < OutFileHeader->NumberOfSections; Section++)
699 {
700 if (OutRelocSection == OutSectionHeaders + Section)
701 {
702 Data = (void *) ProcessedRelocs;
703 }
704 else if (RosSymLength > 0 && Section + 1 == OutFileHeader->NumberOfSections)
705 {
706 Data = (void *) PaddedRosSym;
707 }
708 else
709 {
710 Data = (void *) ((char *) InData + OutSectionHeaders[Section].PointerToRawData);
711 }
712 for (i = 0; i < OutSectionHeaders[Section].SizeOfRawData / 2; i++)
713 {
714 CheckSum += ((unsigned short*) Data)[i];
715 CheckSum = 0xffff & (CheckSum + (CheckSum >> 16));
716 }
717 Length += OutSectionHeaders[Section].SizeOfRawData;
718 }
719 CheckSum += Length;
720 OutOptHeader->CheckSum = CheckSum;
721
722 if (fwrite(OutHeader, 1, StartOfRawData, OutFile) != StartOfRawData)
723 {
724 perror("Error writing output header\n");
725 free(OutHeader);
726 return 1;
727 }
728
729 for (Section = 0; Section < OutFileHeader->NumberOfSections; Section++)
730 {
731 if (0 != OutSectionHeaders[Section].SizeOfRawData)
732 {
733 fseek(OutFile, OutSectionHeaders[Section].PointerToRawData, SEEK_SET);
734 if (OutRelocSection == OutSectionHeaders + Section)
735 {
736 Data = (void *) ProcessedRelocs;
737 }
738 else if (RosSymLength > 0 && Section + 1 == OutFileHeader->NumberOfSections)
739 {
740 Data = (void *) PaddedRosSym;
741 }
742 else
743 {
744 Data = (void *) ((char *) InData + OutSectionHeaders[Section].PointerToRawData);
745 }
746 if (fwrite(Data, 1, OutSectionHeaders[Section].SizeOfRawData, OutFile) !=
747 OutSectionHeaders[Section].SizeOfRawData)
748 {
749 perror("Error writing section data\n");
750 free(PaddedRosSym);
751 free(OutHeader);
752 return 1;
753 }
754 }
755 }
756
757 if (PaddedRosSym)
758 {
759 free(PaddedRosSym);
760 }
761 free(OutHeader);
762
763 return 0;
764 }
765
766 int main(int argc, char* argv[])
767 {
768 PSYMBOLFILE_HEADER SymbolFileHeader;
769 PIMAGE_DOS_HEADER PEDosHeader;
770 PIMAGE_FILE_HEADER PEFileHeader;
771 PIMAGE_OPTIONAL_HEADER PEOptHeader;
772 PIMAGE_SECTION_HEADER PESectionHeaders;
773 ULONG ImageBase;
774 void *StabBase;
775 ULONG StabsLength;
776 void *StabStringBase;
777 ULONG StabStringsLength;
778 void *CoffBase = NULL;
779 ULONG CoffsLength;
780 void *CoffStringBase = NULL;
781 ULONG CoffStringsLength;
782 char* path1;
783 char* path2;
784 FILE* out;
785 void *StringBase;
786 ULONG StringsLength;
787 ULONG StabSymbolsCount;
788 PROSSYM_ENTRY StabSymbols;
789 ULONG CoffSymbolsCount;
790 PROSSYM_ENTRY CoffSymbols;
791 ULONG MergedSymbolsCount;
792 PROSSYM_ENTRY MergedSymbols;
793 size_t FileSize;
794 void *FileData;
795 ULONG RosSymLength;
796 void *RosSymSection;
797 char elfhdr[4] = { '\177', 'E', 'L', 'F' };
798
799 if (3 != argc)
800 {
801 fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
802 exit(1);
803 }
804
805 path1 = convert_path(argv[1]);
806 path2 = convert_path(argv[2]);
807
808 FileData = load_file ( path1, &FileSize );
809 if ( !FileData )
810 {
811 fprintf ( stderr, "An error occured loading '%s'\n", path1 );
812 exit(1);
813 }
814
815 /* Check if MZ header exists */
816 PEDosHeader = (PIMAGE_DOS_HEADER) FileData;
817 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
818 {
819 /* Ignore elf */
820 if (!memcmp(PEDosHeader, elfhdr, sizeof(elfhdr)))
821 exit(0);
822 perror("Input file is not a PE image.\n");
823 free(FileData);
824 exit(1);
825 }
826
827 /* Locate PE file header */
828 /* sizeof(ULONG) = sizeof(MAGIC) */
829 PEFileHeader = (PIMAGE_FILE_HEADER)((char *) FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
830
831 /* Locate optional header */
832 assert(sizeof(ULONG) == 4);
833 PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
834 ImageBase = PEOptHeader->ImageBase;
835
836 /* Locate PE section headers */
837 PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
838
839 if (GetStabInfo(FileData, PEFileHeader, PESectionHeaders, &StabsLength, &StabBase,
840 &StabStringsLength, &StabStringBase))
841 {
842 free(FileData);
843 exit(1);
844 }
845
846 if (GetCoffInfo(FileData, PEFileHeader, PESectionHeaders, &CoffsLength, &CoffBase,
847 &CoffStringsLength, &CoffStringBase))
848 {
849 free(FileData);
850 exit(1);
851 }
852
853 StringBase = malloc(1 + StabStringsLength + CoffStringsLength +
854 (CoffsLength / sizeof(ROSSYM_ENTRY)) * (E_SYMNMLEN + 1));
855 if (NULL == StringBase)
856 {
857 free(FileData);
858 fprintf(stderr, "Failed to allocate memory for strings table\n");
859 exit(1);
860 }
861 /* Make offset 0 into an empty string */
862 *((char *) StringBase) = '\0';
863 StringsLength = 1;
864
865 if (ConvertStabs(&StabSymbolsCount, &StabSymbols, &StringsLength, StringBase,
866 StabsLength, StabBase, StabStringsLength, StabStringBase,
867 ImageBase, PEFileHeader, PESectionHeaders))
868 {
869 free(StringBase);
870 free(FileData);
871 fprintf(stderr, "Failed to allocate memory for strings table\n");
872 exit(1);
873 }
874
875 if (ConvertCoffs(&CoffSymbolsCount, &CoffSymbols, &StringsLength, StringBase,
876 CoffsLength, CoffBase, CoffStringsLength, CoffStringBase,
877 ImageBase, PEFileHeader, PESectionHeaders))
878 {
879 if (StabSymbols)
880 {
881 free(StabSymbols);
882 }
883 free(StringBase);
884 free(FileData);
885 exit(1);
886 }
887
888 if (MergeStabsAndCoffs(&MergedSymbolsCount, &MergedSymbols,
889 StabSymbolsCount, StabSymbols,
890 CoffSymbolsCount, CoffSymbols))
891 {
892 if (CoffSymbols)
893 {
894 free(CoffSymbols);
895 }
896 if (StabSymbols)
897 {
898 free(StabSymbols);
899 }
900 free(StringBase);
901 free(FileData);
902 exit(1);
903 }
904
905 if (CoffSymbols)
906 {
907 free(CoffSymbols);
908 }
909 if (StabSymbols)
910 {
911 free(StabSymbols);
912 }
913 if (MergedSymbolsCount == 0)
914 {
915 RosSymLength = 0;
916 RosSymSection = NULL;
917 }
918 else
919 {
920 RosSymLength = sizeof(SYMBOLFILE_HEADER) + MergedSymbolsCount * sizeof(ROSSYM_ENTRY)
921 + StringsLength;
922 RosSymSection = malloc(RosSymLength);
923 if (NULL == RosSymSection)
924 {
925 free(MergedSymbols);
926 free(StringBase);
927 free(FileData);
928 fprintf(stderr, "Unable to allocate memory for .rossym section\n");
929 exit(1);
930 }
931 memset(RosSymSection, '\0', RosSymLength);
932
933 SymbolFileHeader = (PSYMBOLFILE_HEADER) RosSymSection;
934 SymbolFileHeader->SymbolsOffset = sizeof(SYMBOLFILE_HEADER);
935 SymbolFileHeader->SymbolsLength = MergedSymbolsCount * sizeof(ROSSYM_ENTRY);
936 SymbolFileHeader->StringsOffset = SymbolFileHeader->SymbolsOffset + SymbolFileHeader->SymbolsLength;
937 SymbolFileHeader->StringsLength = StringsLength;
938
939 memcpy((char *) RosSymSection + SymbolFileHeader->SymbolsOffset, MergedSymbols,
940 SymbolFileHeader->SymbolsLength);
941 memcpy((char *) RosSymSection + SymbolFileHeader->StringsOffset, StringBase,
942 SymbolFileHeader->StringsLength);
943
944 free(MergedSymbols);
945 }
946 free(StringBase);
947 out = fopen(path2, "wb");
948 if (out == NULL)
949 {
950 perror("Cannot open output file");
951 free(RosSymSection);
952 free(FileData);
953 exit(1);
954 }
955
956 if (CreateOutputFile(out, FileData, PEDosHeader, PEFileHeader, PEOptHeader,
957 PESectionHeaders, RosSymLength, RosSymSection))
958 {
959 fclose(out);
960 if (RosSymSection)
961 {
962 free(RosSymSection);
963 }
964 free(FileData);
965 exit(1);
966 }
967
968 fclose(out);
969 if (RosSymSection)
970 {
971 free(RosSymSection);
972 }
973 free(FileData);
974
975 return 0;
976 }
977
978 /* EOF */