ogmap.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {
  2. Copyright (c) 2001-2002 by Peter Vreman
  3. Contains the class for generating a map file
  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 of the License, or
  7. (at your option) 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
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogmap;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. globtype,
  23. { object writer }
  24. ogbase
  25. ;
  26. type
  27. texemap = class
  28. private
  29. t : text;
  30. FImageBase : qword;
  31. public
  32. constructor Create(const s:string);
  33. destructor Destroy;override;
  34. procedure Flush;
  35. procedure Add(const s:string);
  36. procedure AddHeader(const s:string);
  37. procedure AddCommonSymbolsHeader;
  38. procedure AddCommonSymbol(p:TObjSymbol);
  39. procedure AddMemoryMapHeader(abase:qword);
  40. procedure AddMemoryMapExeSection(p:texesection);
  41. procedure AddMemoryMapObjectSection(p:TObjSection);
  42. procedure AddMemoryMapSymbol(p:TObjSymbol);
  43. end;
  44. var
  45. exemap : texemap;
  46. implementation
  47. uses
  48. cutils,cfileutl;
  49. const
  50. HexTbl : array[0..15] of char='0123456789abcdef';
  51. function sizestr(v:qword):string;
  52. var
  53. tmp:array [0..19] of char;
  54. i:longint;
  55. begin
  56. if v=0 then
  57. result:='0x0'
  58. else
  59. begin
  60. i:=high(tmp);
  61. while (v>0) do
  62. begin
  63. tmp[i]:=hextbl[v and $f];
  64. v:=v shr 4;
  65. dec(i);
  66. end;
  67. tmp[i]:='x';
  68. tmp[i-1]:='0';
  69. setstring(result,@tmp[i-1],high(tmp)+2-i);
  70. end;
  71. end;
  72. function PadSpaceLeft(const s:string;len:longint):string;
  73. begin
  74. if length(s)<len then
  75. result:=Space(len-length(s))+s
  76. else
  77. result:=s;
  78. end;
  79. {****************************************************************************
  80. TExeMap
  81. ****************************************************************************}
  82. constructor TExeMap.Create(const s:string);
  83. begin
  84. Assign(t,FixFileName(s));
  85. Rewrite(t);
  86. FImageBase:=0;
  87. end;
  88. destructor TExeMap.Destroy;
  89. begin
  90. Close(t);
  91. end;
  92. procedure TExeMap.Flush;
  93. begin
  94. System.Flush(t);
  95. end;
  96. procedure TExeMap.Add(const s:string);
  97. begin
  98. writeln(t,s);
  99. end;
  100. procedure TExeMap.AddHeader(const s:string);
  101. begin
  102. Add('');
  103. Add(s);
  104. end;
  105. procedure TExeMap.AddCommonSymbolsHeader;
  106. begin
  107. AddHeader('Allocating common symbols');
  108. Add('Common symbol size file');
  109. Add('');
  110. end;
  111. procedure TExeMap.AddCommonSymbol(p:TObjSymbol);
  112. var
  113. s : string;
  114. begin
  115. { Common symbol size file }
  116. s:=p.name;
  117. if length(s)>20 then
  118. begin
  119. writeln(t,p.name);
  120. s:='';
  121. end;
  122. Add(PadSpace(s,20)+PadSpace(sizestr(p.size),16)+p.objdata.name);
  123. end;
  124. procedure TExeMap.AddMemoryMapHeader(abase:qword);
  125. var
  126. imagebasestr : string;
  127. begin
  128. FImageBase:=abase;
  129. if FImageBase<>0 then
  130. imagebasestr:=' (ImageBase=0x'+HexStr(FImageBase,sizeof(pint)*2)+')'
  131. else
  132. imagebasestr:='';
  133. AddHeader('Memory map'+imagebasestr);
  134. Add('');
  135. end;
  136. procedure TExeMap.AddMemoryMapExeSection(p:texesection);
  137. begin
  138. { .text 0x000018a8 0xd958 }
  139. Add(PadSpace(p.name,15)+PadSpace(' '+p.MemPosStr(Fimagebase),12)+
  140. ' '+PadSpaceLeft(sizestr(p.size),9));
  141. end;
  142. procedure TExeMap.AddMemoryMapObjectSection(p:TObjSection);
  143. var
  144. secname : string;
  145. begin
  146. { .text 0x000018a8 0xd958 object.o }
  147. secname:=p.name;
  148. if Length(secname)>14 then
  149. begin
  150. Add(' '+secname);
  151. secname:='';
  152. end;
  153. Add(' '+PadSpace(secname,14)+PadSpace(' '+p.MemPosStr(FImageBase),12)+
  154. ' '+PadSpaceLeft(sizestr(p.size),9)+' '+p.objdata.name);
  155. end;
  156. procedure TExeMap.AddMemoryMapSymbol(p:TObjSymbol);
  157. begin
  158. { 0x00001e30 setup_screens }
  159. Add(Space(16)+PadSpace(p.AddressStr(FImageBase),25)+' '+p.name);
  160. end;
  161. end.