dump.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* dump.c: Routines to dump the internal data to a file
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2005 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "dump.h"
  16. #ifdef ENABLE_DUMP
  17. #include "integers.h"
  18. #include "manip.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #define FixIndent \
  22. SIZE NewLen = (Indent ? strlen (Indent) : 0) + 2; \
  23. char NextIndent[NewLen+1]; \
  24. if (!Indent) \
  25. Indent = ""; \
  26. strcpy (NextIndent, Indent); \
  27. NextIndent [NewLen - 2] = ' '; \
  28. NextIndent [NewLen - 1] = ' '; \
  29. NextIndent [NewLen] = 0;
  30. static void PrintOffset (FILE *File, OFFSET Offset)
  31. {
  32. if (Offset)
  33. {
  34. if (Offset > 0)
  35. fprintf (File, " + %ld", (long) Offset);
  36. else
  37. fprintf (File, " - %ld", (long) (-Offset));
  38. }
  39. }
  40. void DumpProgram (FILE *File, const char *Indent, const PROGRAM *Program)
  41. {
  42. const SECTION *Section;
  43. const GLOBAL_IMPORT *GlobalImport;
  44. FixIndent;
  45. if (Program->Frozen)
  46. fprintf (File, "%sFrozen\n", Indent);
  47. for_each (Section, Program->Sections)
  48. {
  49. fprintf (File, "%sSection\n", Indent);
  50. DumpSection (File, NextIndent, Section);
  51. }
  52. if (!(IsEmpty (Program->GlobalImports)))
  53. {
  54. fprintf (File, "%sGlobal Imports\n", Indent);
  55. for_each (GlobalImport, Program->GlobalImports)
  56. fprintf (File, "%s %s\n", Indent, GlobalImport->SymbolName);
  57. }
  58. }
  59. void DumpSection (FILE *File, const char *Indent, const SECTION *Section)
  60. {
  61. FixIndent;
  62. if (Section->FileName)
  63. fprintf (File, "%sFile: %s\n", Indent, Section->FileName);
  64. if (Section->StartupNumber)
  65. fprintf (File, "%sStartup Section: %ld\n", Indent, (long) (Section->StartupNumber));
  66. if (Section->Essential)
  67. fprintf (File, "%sEssential\n", Indent);
  68. if (Section->Frozen)
  69. fprintf (File, "%sFrozen\n", Indent);
  70. if (Section->CanCutRanges)
  71. fprintf (File, "%sCan Cut Ranges\n", Indent);
  72. if (Section->Mergeable)
  73. fprintf (File, "%sMergeable\n", Indent);
  74. if (Section->Unaligned)
  75. fprintf (File, "%sUnaligned\n", Indent);
  76. fprintf (File, Section->Code ? "%sCode\n" : "%sData\n", Indent);
  77. {
  78. const SEGMENT *NextSegment = GetFirst (Section->Segments);
  79. const SYMBOL *NextSymbol = GetFirst (Section->Symbols);
  80. const RELOC *NextReloc = GetFirst (Section->Relocs);
  81. const ROM_CALL *NextROMCall = GetFirst (Section->ROMCalls);
  82. const RAM_CALL *NextRAMCall = GetFirst (Section->RAMCalls);
  83. const LIB_CALL *NextLibCall = GetFirst (Section->LibCalls);
  84. OFFSET CurPos = 0;
  85. SIZE SectionSize = Section->Size;
  86. while ((CurPos < SectionSize) || NextSegment || NextSymbol || NextReloc || NextROMCall || NextRAMCall || NextLibCall)
  87. {
  88. OFFSET NextPos = MAX_OFFSET;
  89. OFFSET StartPos;
  90. const char *CheckPos (void)
  91. {
  92. return ((CurPos == StartPos) && (CurPos <= SectionSize) ? "" : " (!)");
  93. }
  94. // Segments
  95. while (NextSegment && (CurPos >= (StartPos = NextSegment->Location.Start->Location)))
  96. {
  97. fprintf (File, "%s%s Segment: %s (%s%s)\n", Indent, CheckPos (), NextSegment->FileName ? : "", NextSegment->Code ? "Code" : "Data", NextSegment->CanCutRanges ? ", Can Cut Ranges" : "");
  98. NextSegment = GetNext (NextSegment);
  99. }
  100. if (NextSegment && (NextPos > NextSegment->Location.Start->Location))
  101. NextPos = NextSegment->Location.Start->Location;
  102. // Symbols
  103. while (NextSymbol && (CurPos >= (StartPos = NextSymbol->Location)))
  104. {
  105. fprintf (File, "%s%s %s:%s\n", Indent, CheckPos (), NextSymbol->Name, NextSymbol->Exported ? "" : " (local)");
  106. NextSymbol = GetNext (NextSymbol);
  107. }
  108. if (NextSymbol && (NextPos > NextSymbol->Location))
  109. NextPos = NextSymbol->Location;
  110. // Relocs
  111. if (NextReloc && (CurPos >= (StartPos = NextReloc->Location)))
  112. {
  113. OFFSET EndPos = StartPos + NextReloc->Size;
  114. fprintf (File, "%s 0x%.06lX%s: <%ldB%s: %s%s", NextIndent, (long) StartPos, CheckPos (), (long) NextReloc->Size, NextReloc->Unoptimizable ? " U" : "", NextReloc->Target.SymbolName, NextReloc->Target.Symbol ? (NextReloc->Target.Symbol->Parent == NextReloc->Parent ? "" : " (->)") : " (?)");
  115. if (NextReloc->Target.Offset)
  116. fprintf (File, "%+ld", (long) (NextReloc->Target.Offset));
  117. if (NextReloc->Relative)
  118. {
  119. if (NextReloc->Relation)
  120. {
  121. fprintf (File, " - %s%s%s", NextReloc->Relation->Offset ? "(" : "", NextReloc->Relation->SymbolName, NextReloc->Relation->Symbol ? (NextReloc->Relation->Symbol->Parent == NextReloc->Parent ? "" : " (->)") : " (?)");
  122. if (NextReloc->Relation->Offset)
  123. fprintf (File, "%+ld)", (long) (NextReloc->Relation->Offset));
  124. }
  125. else
  126. fprintf (File, " (rel)");
  127. }
  128. PrintOffset (File, NextReloc->FixedOffset);
  129. fprintf (File, ">%s\n", IsZeroDataRange (Section, StartPos, EndPos) ? "" : " (!)");
  130. if (CurPos < EndPos)
  131. CurPos = EndPos;
  132. NextReloc = GetNext (NextReloc);
  133. }
  134. if (NextReloc && (NextPos > NextReloc->Location))
  135. NextPos = NextReloc->Location;
  136. // ROM Calls
  137. if (NextROMCall && (CurPos >= (StartPos = NextROMCall->Location)))
  138. {
  139. OFFSET EndPos = StartPos + NextROMCall->Size;
  140. fprintf (File, "%s 0x%.06lX%s: <%ldB: ROM Call 0x%lX", NextIndent, (long) StartPos, CheckPos (), (long) NextROMCall->Size, (long) (NextROMCall->Number));
  141. PrintOffset (File, NextROMCall->FixedOffset);
  142. fprintf (File, ">%s\n", IsZeroDataRange (Section, StartPos, EndPos) ? "" : " (!)");
  143. if (CurPos < EndPos)
  144. CurPos = EndPos;
  145. NextROMCall = GetNext (NextROMCall);
  146. }
  147. if (NextROMCall && (NextPos > NextROMCall->Location))
  148. NextPos = NextROMCall->Location;
  149. // RAM Calls
  150. if (NextRAMCall && (CurPos >= (StartPos = NextRAMCall->Location)))
  151. {
  152. OFFSET EndPos = StartPos + NextRAMCall->Size;
  153. fprintf (File, "%s 0x%.06lX%s: <%ldB: RAM Call 0x%lX", NextIndent, (long) StartPos, CheckPos (), (long) NextRAMCall->Size, (long) (NextRAMCall->Number));
  154. PrintOffset (File, NextRAMCall->FixedOffset);
  155. fprintf (File, ">%s\n", IsZeroDataRange (Section, StartPos, EndPos) ? "" : " (!)");
  156. if (CurPos < EndPos)
  157. CurPos = EndPos;
  158. NextRAMCall = GetNext (NextRAMCall);
  159. }
  160. if (NextRAMCall && (NextPos > NextRAMCall->Location))
  161. NextPos = NextRAMCall->Location;
  162. // Library Calls
  163. if (NextLibCall && (CurPos >= (StartPos = NextLibCall->Location)))
  164. {
  165. OFFSET EndPos = StartPos + NextLibCall->Size;
  166. fprintf (File, "%s 0x%.06lX%s: <%ldB: Library Call %s::0x%lX", NextIndent, (long) StartPos, CheckPos (), (long) NextLibCall->Size, NextLibCall->Library->Name, (long) (NextLibCall->Number));
  167. PrintOffset (File, NextLibCall->FixedOffset);
  168. fprintf (File, ">%s\n", IsZeroDataRange (Section, StartPos, EndPos) ? "" : " (!)");
  169. if (CurPos < EndPos)
  170. CurPos = EndPos;
  171. NextLibCall = GetNext (NextLibCall);
  172. }
  173. if (NextLibCall && (NextPos > NextLibCall->Location))
  174. NextPos = NextLibCall->Location;
  175. if (CurPos > SectionSize)
  176. fprintf (File, "%s (Section Size Exceeded!)\n", NextIndent);
  177. // Section Data
  178. if (CurPos < NextPos)
  179. {
  180. OFFSET EndPos = NextPos;
  181. if (EndPos > SectionSize)
  182. EndPos = SectionSize;
  183. if (CurPos < EndPos)
  184. {
  185. if (Section->Data)
  186. DumpData (File, NextIndent, Section->Data, CurPos, EndPos);
  187. else
  188. DumpDummyData (File, NextIndent, Section->Initialized ? "00" : "??", CurPos, EndPos);
  189. }
  190. CurPos = NextPos;
  191. }
  192. }
  193. }
  194. }
  195. void DumpData (FILE *File, const char *Indent, const I1 *Data, OFFSET Start, OFFSET End)
  196. {
  197. OFFSET CurPos;
  198. FixIndent;
  199. fprintf (File, "%s 0x%.06lX:", Indent, (long) Start);
  200. for (CurPos = Start; CurPos < End; CurPos++)
  201. fprintf (File, " %.02hX", (short) (Data [CurPos]));
  202. fprintf (File, "\n");
  203. }
  204. void DumpDummyData (FILE *File, const char *Indent, const char *Dummy, OFFSET Start, OFFSET End)
  205. {
  206. OFFSET CurPos;
  207. FixIndent;
  208. fprintf (File, "%s 0x%.06lX:", Indent, (long) Start);
  209. for (CurPos = Start; CurPos < End; CurPos++)
  210. fprintf (File, " %s", Dummy);
  211. fprintf (File, "\n");
  212. }
  213. #endif /* ENABLE_DUMP */