1 /*****************************************************************************/
5 /* Dump subroutines for the od65 object file dump utility */
9 /* (C) 2002-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 #include "scopedefs.h"
59 /*****************************************************************************/
61 /*****************************************************************************/
65 static void DestroyStrPool (Collection* C)
66 /* Free all strings in the given pool plus the item pointers. Note: The
67 ** collection may not be reused later.
71 for (I = 0; I < CollCount (C); ++I) {
72 xfree (CollAtUnchecked (C, I));
79 static const char* GetString (const Collection* C, unsigned Index)
80 /* Get a string from a collection. In fact, this function calls CollConstAt,
81 ** but will print a somewhat more readable error message if the index is out
85 if (Index >= CollCount (C)) {
86 Error ("Invalid string index (%u) - file corrupt!", Index);
88 return CollConstAt (C, Index);
93 static void DumpObjHeaderSection (const char* Name,
96 /* Dump a header section */
98 printf (" %s:\n", Name);
99 printf (" Offset:%24lu\n", Offset);
100 printf (" Size: %24lu\n", Size);
105 static char* TimeToStr (unsigned long Time)
106 /* Convert the time into a string and return it */
108 /* Get the time and convert to string */
109 time_t T = (time_t) Time;
110 char* S = asctime (localtime (&T));
112 /* Remove the trailing newline */
113 unsigned Len = strlen (S);
114 if (Len > 0 && S[Len-1] == '\n') {
118 /* Return the time string */
124 static void SkipLineInfoList (FILE* F)
125 /* Skip a line info list from the given file */
127 /* Count preceeds the list */
128 unsigned long Count = ReadVar (F);
138 static void SkipSpanList (FILE* F)
139 /* Skip a span list from the given file */
141 /* Count preceeds the list */
142 unsigned long Count = ReadVar (F);
152 static void SkipExpr (FILE* F)
153 /* Skip an expression from the given file */
155 /* Read the node tag and handle NULL nodes */
156 unsigned char Op = Read8 (F);
157 if (Op == EXPR_NULL) {
161 /* Check the tag and handle the different expression types */
162 if (EXPR_IS_LEAF (Op)) {
166 (void) Read32Signed (F);
170 /* Read the import number */
176 /* Read the segment number */
181 Error ("Invalid expression op: %02X", Op);
187 /* Not a leaf node */
195 static const char* GetExportFlags (unsigned Flags, const unsigned char* ConDes)
196 /* Get the export flags as a (static) string */
199 static char TypeDesc[256];
207 switch (Flags & SYM_MASK_TYPE) {
208 case SYM_STD: strcat (TypeDesc, "SYM_STD"); break;
209 case SYM_CHEAP_LOCAL: strcat (TypeDesc, "SYM_CHEAP_LOCAL"); break;
213 switch (Flags & SYM_MASK_LABEL) {
214 case SYM_EQUATE: strcat (TypeDesc, ",SYM_EQUATE"); break;
215 case SYM_LABEL: strcat (TypeDesc, ",SYM_LABEL"); break;
218 /* Type of expression */
219 switch (Flags & SYM_MASK_VAL) {
220 case SYM_CONST: strcat (TypeDesc, ",SYM_CONST"); break;
221 case SYM_EXPR: strcat (TypeDesc, ",SYM_EXPR"); break;
224 /* Size available? */
225 if (SYM_HAS_SIZE (Flags)) {
226 strcat (TypeDesc, ",SYM_SIZE");
230 /* Constructor/destructor declarations */
231 T = TypeDesc + strlen (TypeDesc);
232 Count = SYM_GET_CONDES_COUNT (Flags);
233 if (Count > 0 && ConDes) {
234 T += sprintf (T, ",SYM_CONDES=");
235 for (I = 0; I < Count; ++I) {
236 unsigned Type = CD_GET_TYPE (ConDes[I]);
237 unsigned Prio = CD_GET_PRIO (ConDes[I]);
241 T += sprintf (T, "[%u,%u]", Type, Prio);
245 /* Return the result */
251 static const char* GetScopeType (unsigned Type)
252 /* Return the name of a scope type */
255 case SCOPE_GLOBAL: return "Global scope";
256 case SCOPE_FILE: return "File scope";
257 case SCOPE_SCOPE: return ".SCOPE or .PROC";
258 case SCOPE_STRUCT: return ".STRUCT";
259 case SCOPE_ENUM: return ".ENUM";
260 case SCOPE_UNDEF: return "Undefined";
261 default: return "Unknown scope type";
267 void DumpObjHeader (FILE* F, unsigned long Offset)
268 /* Dump the header of the given object file */
272 /* Seek to the header position */
273 FileSetPos (F, Offset);
275 /* Read the header */
276 ReadObjHeader (F, &H);
278 /* Now dump the information */
280 /* Output a header */
281 printf (" Header:\n");
284 printf (" Magic:%17s0x%08lX\n", "", H.Magic);
287 printf (" Version:%25u\n", H.Version);
290 printf (" Flags:%21s0x%04X (", "", H.Flags);
291 if (H.Flags & OBJ_FLAGS_DBGINFO) {
292 printf ("OBJ_FLAGS_DBGINFO");
297 DumpObjHeaderSection ("Options", H.OptionOffs, H.OptionSize);
300 DumpObjHeaderSection ("Files", H.FileOffs, H.FileSize);
303 DumpObjHeaderSection ("Segments", H.SegOffs, H.SegSize);
306 DumpObjHeaderSection ("Imports", H.ImportOffs, H.ImportSize);
309 DumpObjHeaderSection ("Exports", H.ExportOffs, H.ExportSize);
312 DumpObjHeaderSection ("Debug symbols", H.DbgSymOffs, H.DbgSymSize);
315 DumpObjHeaderSection ("Line infos", H.LineInfoOffs, H.LineInfoSize);
318 DumpObjHeaderSection ("String pool", H.StrPoolOffs, H.StrPoolSize);
321 DumpObjHeaderSection ("Assertions", H.AssertOffs, H.AssertSize);
324 DumpObjHeaderSection ("Scopes", H.ScopeOffs, H.ScopeSize);
329 void DumpObjOptions (FILE* F, unsigned long Offset)
330 /* Dump the file options */
333 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
337 /* Seek to the header position and read the header */
338 FileSetPos (F, Offset);
339 ReadObjHeader (F, &H);
341 /* Seek to the start of the string pool and read it */
342 FileSetPos (F, Offset + H.StrPoolOffs);
343 ReadStrPool (F, &StrPool);
345 /* Seek to the start of the options */
346 FileSetPos (F, Offset + H.OptionOffs);
348 /* Output a header */
349 printf (" Options:\n");
351 /* Read the number of options and print it */
353 printf (" Count:%27u\n", Count);
355 /* Read and print all options */
356 for (I = 0; I < Count; ++I) {
361 /* Read the type of the option and the value */
362 unsigned char Type = Read8 (F);
363 unsigned long Val = ReadVar (F);
365 /* Get the type of the argument */
366 unsigned char ArgType = Type & OPT_ARGMASK;
368 /* Determine which option follows */
369 const char* TypeDesc;
371 case OPT_COMMENT: TypeDesc = "OPT_COMMENT"; break;
372 case OPT_AUTHOR: TypeDesc = "OPT_AUTHOR"; break;
373 case OPT_TRANSLATOR:TypeDesc = "OPT_TRANSLATOR"; break;
374 case OPT_COMPILER: TypeDesc = "OPT_COMPILER"; break;
375 case OPT_OS: TypeDesc = "OPT_OS"; break;
376 case OPT_DATETIME: TypeDesc = "OPT_DATETIME"; break;
377 default: TypeDesc = "OPT_UNKNOWN"; break;
380 /* Print the header */
381 printf (" Index:%27u\n", I);
384 printf (" Type:%22s0x%02X (%s)\n", "", Type, TypeDesc);
388 ArgStr = GetString (&StrPool, Val);
389 ArgLen = strlen (ArgStr);
390 printf (" Data:%*s\"%s\"\n", (int)(24-ArgLen), "", ArgStr);
394 printf (" Data:%26lu", Val);
395 if (Type == OPT_DATETIME) {
396 /* Print the time as a string */
397 printf (" (%s)", TimeToStr (Val));
403 /* Unknown argument type. This means that we cannot determine
404 ** the option length, so we cannot proceed.
406 Error ("Unknown option type: 0x%02X", Type);
411 /* Destroy the string pool */
412 DestroyStrPool (&StrPool);
417 void DumpObjFiles (FILE* F, unsigned long Offset)
418 /* Dump the source files */
421 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
425 /* Seek to the header position and read the header */
426 FileSetPos (F, Offset);
427 ReadObjHeader (F, &H);
429 /* Seek to the start of the string pool and read it */
430 FileSetPos (F, Offset + H.StrPoolOffs);
431 ReadStrPool (F, &StrPool);
433 /* Seek to the start of the source files */
434 FileSetPos (F, Offset + H.FileOffs);
436 /* Output a header */
437 printf (" Files:\n");
439 /* Read the number of files and print it */
441 printf (" Count:%27u\n", Count);
443 /* Read and print all files */
444 for (I = 0; I < Count; ++I) {
446 /* Read the data for one file */
447 const char* Name = GetString (&StrPool, ReadVar (F));
448 unsigned long MTime = Read32 (F);
449 unsigned long Size = ReadVar (F);
450 unsigned Len = strlen (Name);
452 /* Print the header */
453 printf (" Index:%27u\n", I);
456 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
457 printf (" Size:%26lu\n", Size);
458 printf (" Modification time:%13lu (%s)\n", MTime, TimeToStr (MTime));
461 /* Destroy the string pool */
462 DestroyStrPool (&StrPool);
467 void DumpObjSegments (FILE* F, unsigned long Offset)
468 /* Dump the segments in the object file */
471 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
475 /* Seek to the header position and read the header */
476 FileSetPos (F, Offset);
477 ReadObjHeader (F, &H);
479 /* Seek to the start of the string pool and read it */
480 FileSetPos (F, Offset + H.StrPoolOffs);
481 ReadStrPool (F, &StrPool);
483 /* Seek to the start of the segments */
484 FileSetPos (F, Offset + H.SegOffs);
486 /* Output a header */
487 printf (" Segments:\n");
489 /* Read the number of segments and print it */
491 printf (" Count:%27u\n", Count);
493 /* Read and print all segments */
494 for (I = 0; I < Count; ++I) {
496 /* Read the data for one segments */
497 unsigned long DataSize = Read32 (F);
498 unsigned long NextSeg = ftell (F) + DataSize;
499 const char* Name = GetString (&StrPool, ReadVar (F));
500 unsigned Len = strlen (Name);
501 unsigned Flags = ReadVar (F);
502 unsigned long Size = ReadVar (F);
503 unsigned long Align = ReadVar (F);
504 unsigned char AddrSize = Read8 (F);
505 unsigned long FragCount = ReadVar (F);
507 /* Print the header */
508 printf (" Index:%27u\n", I);
511 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
512 printf (" Flags:%25u\n", Flags);
513 printf (" Size:%26lu\n", Size);
514 printf (" Alignment:%21lu\n", Align);
515 printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize,
516 AddrSizeToStr (AddrSize));
517 printf (" Fragment count:%16lu\n", FragCount);
519 /* Seek to the end of the segment data (start of next) */
520 FileSetPos (F, NextSeg);
523 /* Destroy the string pool */
524 DestroyStrPool (&StrPool);
529 void DumpObjImports (FILE* F, unsigned long Offset)
530 /* Dump the imports in the object file */
533 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
537 /* Seek to the header position and read the header */
538 FileSetPos (F, Offset);
539 ReadObjHeader (F, &H);
541 /* Seek to the start of the string pool and read it */
542 FileSetPos (F, Offset + H.StrPoolOffs);
543 ReadStrPool (F, &StrPool);
545 /* Seek to the start of the imports */
546 FileSetPos (F, Offset + H.ImportOffs);
548 /* Output a header */
549 printf (" Imports:\n");
551 /* Read the number of imports and print it */
553 printf (" Count:%27u\n", Count);
555 /* Read and print all imports */
556 for (I = 0; I < Count; ++I) {
558 /* Read the data for one import */
559 unsigned char AddrSize = Read8 (F);
560 const char* Name = GetString (&StrPool, ReadVar (F));
561 unsigned Len = strlen (Name);
563 /* Skip both line info lists */
564 SkipLineInfoList (F);
565 SkipLineInfoList (F);
567 /* Print the header */
568 printf (" Index:%27u\n", I);
571 printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize,
572 AddrSizeToStr (AddrSize));
573 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
576 /* Destroy the string pool */
577 DestroyStrPool (&StrPool);
582 void DumpObjExports (FILE* F, unsigned long Offset)
583 /* Dump the exports in the object file */
586 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
590 /* Seek to the header position and read the header */
591 FileSetPos (F, Offset);
592 ReadObjHeader (F, &H);
594 /* Seek to the start of the string pool and read it */
595 FileSetPos (F, Offset + H.StrPoolOffs);
596 ReadStrPool (F, &StrPool);
598 /* Seek to the start of the exports */
599 FileSetPos (F, Offset + H.ExportOffs);
601 /* Output a header */
602 printf (" Exports:\n");
604 /* Read the number of exports and print it */
606 printf (" Count:%27u\n", Count);
608 /* Read and print all exports */
609 for (I = 0; I < Count; ++I) {
611 unsigned long Value = 0;
612 unsigned long Size = 0;
613 unsigned char ConDes[CD_TYPE_COUNT];
618 /* Read the data for one export */
619 unsigned Type = ReadVar (F);
620 unsigned char AddrSize = Read8 (F);
621 ReadData (F, ConDes, SYM_GET_CONDES_COUNT (Type));
622 Name = GetString (&StrPool, ReadVar (F));
624 if (SYM_IS_CONST (Type)) {
629 if (SYM_HAS_SIZE (Type)) {
633 /* Skip both line infos lists */
634 SkipLineInfoList (F);
635 SkipLineInfoList (F);
637 /* Print the header */
638 printf (" Index:%27u\n", I);
641 printf (" Type:%22s0x%02X (%s)\n", "", Type, GetExportFlags (Type, ConDes));
642 printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize,
643 AddrSizeToStr (AddrSize));
644 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
645 if (SYM_IS_CONST (Type)) {
646 printf (" Value:%15s0x%08lX (%lu)\n", "", Value, Value);
648 if (SYM_HAS_SIZE (Type)) {
649 printf (" Size:%16s0x%04lX (%lu)\n", "", Size, Size);
653 /* Destroy the string pool */
654 DestroyStrPool (&StrPool);
659 void DumpObjDbgSyms (FILE* F, unsigned long Offset)
660 /* Dump the debug symbols from an object file */
663 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
667 /* Seek to the header position and read the header */
668 FileSetPos (F, Offset);
669 ReadObjHeader (F, &H);
671 /* Seek to the start of the string pool and read it */
672 FileSetPos (F, Offset + H.StrPoolOffs);
673 ReadStrPool (F, &StrPool);
675 /* Seek to the start of the debug syms */
676 FileSetPos (F, Offset + H.DbgSymOffs);
678 /* Output a header */
679 printf (" Debug symbols:\n");
681 /* Check if the object file was compiled with debug info */
682 if ((H.Flags & OBJ_FLAGS_DBGINFO) == 0) {
683 /* Print that there no debug symbols and bail out */
684 printf (" Count:%27u\n", 0);
688 /* Read the number of exports and print it */
690 printf (" Count:%27u\n", Count);
692 /* Read and print all debug symbols */
693 for (I = 0; I < Count; ++I) {
695 unsigned long Value = 0;
696 unsigned long Size = 0;
697 unsigned ImportId = 0;
698 unsigned ExportId = 0;
700 /* Read the data for one symbol */
701 unsigned Type = ReadVar (F);
702 unsigned char AddrSize = Read8 (F);
703 unsigned long Owner = ReadVar (F);
704 const char* Name = GetString (&StrPool, ReadVar (F));
705 unsigned Len = strlen (Name);
706 if (SYM_IS_CONST (Type)) {
711 if (SYM_HAS_SIZE (Type)) {
714 if (SYM_IS_IMPORT (Type)) {
715 ImportId = ReadVar (F);
717 if (SYM_IS_EXPORT (Type)) {
718 ExportId = ReadVar (F);
721 /* Skip both line info lists */
722 SkipLineInfoList (F);
723 SkipLineInfoList (F);
725 /* Print the header */
726 printf (" Index:%27u\n", I);
729 printf (" Type:%22s0x%02X (%s)\n", "", Type, GetExportFlags (Type, 0));
730 printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize,
731 AddrSizeToStr (AddrSize));
732 printf (" Owner:%25lu\n", Owner);
733 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
734 if (SYM_IS_CONST (Type)) {
735 printf (" Value:%15s0x%08lX (%lu)\n", "", Value, Value);
737 if (SYM_HAS_SIZE (Type)) {
738 printf (" Size:%20s0x%04lX (%lu)\n", "", Size, Size);
740 if (SYM_IS_IMPORT (Type)) {
741 printf (" Import:%24u\n", ImportId);
743 if (SYM_IS_EXPORT (Type)) {
744 printf (" Export:%24u\n", ExportId);
748 /* Destroy the string pool */
749 DestroyStrPool (&StrPool);
754 void DumpObjLineInfo (FILE* F, unsigned long Offset)
755 /* Dump the line info from an object file */
758 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
762 /* Seek to the header position and read the header */
763 FileSetPos (F, Offset);
764 ReadObjHeader (F, &H);
766 /* Seek to the start of the string pool and read it */
767 FileSetPos (F, Offset + H.StrPoolOffs);
768 ReadStrPool (F, &StrPool);
770 /* Seek to the start of line infos */
771 FileSetPos (F, Offset + H.LineInfoOffs);
773 /* Output a header */
774 printf (" Line info:\n");
776 /* Check if the object file was compiled with debug info */
777 if ((H.Flags & OBJ_FLAGS_DBGINFO) == 0) {
778 /* Print that there no line infos and bail out */
779 printf (" Count:%27u\n", 0);
783 /* Read the number of line infos and print it */
785 printf (" Count:%27u\n", Count);
787 /* Read and print all line infos */
788 for (I = 0; I < Count; ++I) {
793 /* File position of line info */
794 ReadFilePos (F, &Pos);
796 /* Type of line info */
802 /* Print the header */
803 printf (" Index:%27u\n", I);
806 printf (" Type:%26u\n", LI_GET_TYPE (Type));
807 printf (" Count:%25u\n", LI_GET_COUNT (Type));
808 printf (" Line:%26u\n", Pos.Line);
809 printf (" Col:%27u\n", Pos.Col);
810 printf (" Name:%26u\n", Pos.Name);
813 /* Destroy the string pool */
814 DestroyStrPool (&StrPool);
819 void DumpObjScopes (FILE* F, unsigned long Offset)
820 /* Dump the scopes from an object file */
823 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
827 /* Seek to the header position and read the header */
828 FileSetPos (F, Offset);
829 ReadObjHeader (F, &H);
831 /* Seek to the start of the string pool and read it */
832 FileSetPos (F, Offset + H.StrPoolOffs);
833 ReadStrPool (F, &StrPool);
835 /* Seek to the start of scopes */
836 FileSetPos (F, Offset + H.ScopeOffs);
838 /* Output a header */
839 printf (" Scopes:\n");
841 /* Check if the object file was compiled with debug info */
842 if ((H.Flags & OBJ_FLAGS_DBGINFO) == 0) {
843 /* Print that there no scopes and bail out */
844 printf (" Count:%27u\n", 0);
848 /* Read the number of scopes and print it */
850 printf (" Count:%27u\n", Count);
852 /* Read and print all scopes */
853 for (I = 0; I < Count; ++I) {
859 unsigned ParentId = ReadVar (F);
860 unsigned LexicalLevel = ReadVar (F);
861 unsigned Flags = ReadVar (F);
862 const char* ScopeType = GetScopeType (ReadVar (F));
864 /* Print the header */
865 printf (" Index:%27u\n", I);
868 printf (" Parent id:%21u\n", ParentId);
869 printf (" Lexical level:%17u\n", LexicalLevel);
870 printf (" Flags:%21s0x%02X\n", "", Flags);
871 printf (" Type:%26s\n", ScopeType);
873 /* Resolve and print the name */
874 Name = GetString (&StrPool, ReadVar (F));
876 printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
879 if (SCOPE_HAS_SIZE (Flags)) {
880 unsigned long Size = ReadVar (F);
881 printf (" Size:%20s0x%04lX (%lu)\n", "", Size, Size);
885 if (SCOPE_HAS_LABEL (Flags)) {
886 unsigned LabelId = ReadVar (F);
887 printf (" Label id:%22u\n", LabelId);
894 /* Destroy the string pool */
895 DestroyStrPool (&StrPool);
900 void DumpObjSegSize (FILE* F, unsigned long Offset)
901 /* Dump the sizes of the segment in the object file */
904 Collection StrPool = AUTO_COLLECTION_INITIALIZER;
907 /* Seek to the header position and read the header */
908 FileSetPos (F, Offset);
909 ReadObjHeader (F, &H);
911 /* Seek to the start of the string pool and read it */
912 FileSetPos (F, Offset + H.StrPoolOffs);
913 ReadStrPool (F, &StrPool);
915 /* Seek to the start of the segments */
916 FileSetPos (F, Offset + H.SegOffs);
918 /* Output a header */
919 printf (" Segment sizes:\n");
921 /* Read the number of segments */
924 /* Read and print the sizes of all segments */
929 /* Read the data for one segment */
930 unsigned long DataSize = Read32 (F);
931 unsigned long NextSeg = ftell (F) + DataSize;
932 const char* Name = GetString (&StrPool, ReadVar (F));
933 unsigned Len = strlen (Name);
935 /* Skip segment flags, read size */
939 /* Skip alignment, type and fragment count */
944 /* Print the size for this segment */
945 printf (" %s:%*s%6lu\n", Name, (int)(24-Len), "", Size);
947 /* Seek to the end of the segment data (start of next) */
948 FileSetPos (F, NextSeg);
951 /* Destroy the string pool */
952 DestroyStrPool (&StrPool);