ogmap.pas 5.0 KB

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