2
0

ogmap.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. cclasses,globtype,systems,
  23. { object writer }
  24. aasmbase,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. globals,verbose;
  49. const
  50. HexTbl : array[0..15] of char='0123456789abcdef';
  51. function sizestr(v:aword):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.Add(const s:string);
  93. begin
  94. writeln(t,s);
  95. end;
  96. procedure TExeMap.AddHeader(const s:string);
  97. begin
  98. Add('');
  99. Add(s);
  100. end;
  101. procedure TExeMap.AddCommonSymbolsHeader;
  102. begin
  103. AddHeader('Allocating common symbols');
  104. Add('Common symbol size file');
  105. Add('');
  106. end;
  107. procedure TExeMap.AddCommonSymbol(p:TObjSymbol);
  108. var
  109. s : string;
  110. begin
  111. { Common symbol size file }
  112. s:=p.name;
  113. if length(s)>20 then
  114. begin
  115. writeln(t,p.name);
  116. s:='';
  117. end;
  118. Add(PadSpace(s,20)+PadSpace(sizestr(p.size),16)+p.objdata.name);
  119. end;
  120. procedure TExeMap.AddMemoryMapHeader(abase:aint);
  121. var
  122. imagebasestr : string;
  123. begin
  124. FImageBase:=abase;
  125. if FImageBase<>0 then
  126. imagebasestr:=' (ImageBase=0x'+HexStr(FImageBase,sizeof(pint)*2)+')'
  127. else
  128. imagebasestr:='';
  129. AddHeader('Memory map'+imagebasestr);
  130. Add('');
  131. end;
  132. procedure TExeMap.AddMemoryMapExeSection(p:texesection);
  133. begin
  134. { .text 0x000018a8 0xd958 }
  135. Add(PadSpace(p.name,15)+PadSpace(' 0x'+HexStr(p.mempos+Fimagebase,sizeof(pint)*2),12)+
  136. ' '+PadSpaceLeft(sizestr(p.size),9));
  137. end;
  138. procedure TExeMap.AddMemoryMapObjectSection(p:TObjSection);
  139. var
  140. secname : string;
  141. begin
  142. { .text 0x000018a8 0xd958 object.o }
  143. secname:=p.name;
  144. if Length(secname)>14 then
  145. begin
  146. Add(' '+secname);
  147. secname:='';
  148. end;
  149. Add(' '+PadSpace(secname,14)+PadSpace(' 0x'+HexStr(p.mempos+FImageBase,sizeof(pint)*2),12)+
  150. ' '+PadSpaceLeft(sizestr(p.size),9)+' '+p.objdata.name);
  151. end;
  152. procedure TExeMap.AddMemoryMapSymbol(p:TObjSymbol);
  153. begin
  154. { 0x00001e30 setup_screens }
  155. Add(Space(16)+PadSpace('0x'+HexStr(p.address+Fimagebase,sizeof(pint)*2),25)+' '+p.name);
  156. end;
  157. end.