postwin32.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Pavel Ozerski and Pierre Muller
  4. This program implements support post processing
  5. for the (i386) Win32 target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. program post_process_win32_executable;
  20. uses
  21. globtype,globals,t_win32,strings;
  22. const
  23. execinfo_f_cant_open_executable='Cannot open file ';
  24. execinfo_x_codesize='Code size: ';
  25. execinfo_x_initdatasize='Size of Initialized Data: ';
  26. execinfo_x_uninitdatasize='Size of Uninitialized Data: ';
  27. execinfo_f_cant_process_executable='Cannot process file ';
  28. execinfo_x_stackreserve='Size of Stack Reserve: ';
  29. execinfo_x_stackcommit='Size of Stack Commit: ';
  30. var
  31. verbose:longbool;
  32. ii,jj,p:longint;
  33. x:single;
  34. code:integer;
  35. procedure Message1(const info,fn:string);
  36. var
  37. e:longbool;
  38. begin
  39. e:=pos('Cannot',info)=1;
  40. if verbose or e then
  41. writeln(info,fn);
  42. if e then
  43. halt(1);
  44. end;
  45. var
  46. l:tlinkerwin32;
  47. fn,s:string;
  48. isDll:boolean;
  49. function GetSwitchValue(const key,shortkey,default:string;const PossibleValues:array of pchar):string;
  50. var
  51. i,j,k:longint;
  52. x:double;
  53. s1,s2:string;
  54. code:integer;
  55. procedure Error;
  56. begin
  57. writeln('Error: unrecognized option ',paramstr(i),' ',s1);
  58. halt(1);
  59. end;
  60. begin
  61. for i:=1 to paramcount do
  62. if(paramstr(i)=key)or(paramstr(i)=shortkey)then
  63. begin
  64. s1:=paramstr(succ(i));
  65. for j:=0 to high(PossibleValues)do
  66. begin
  67. s2:=strpas(PossibleValues[j]);
  68. if(length(s2)>1)and(s2[1]='*')then
  69. case s2[2]of
  70. 'i':
  71. begin
  72. val(s1,k,code);
  73. if code<>0 then
  74. error;
  75. GetSwitchValue:=s1;
  76. exit;
  77. end;
  78. 'r':
  79. begin
  80. val(s1,x,code);
  81. if code<>0 then
  82. error;
  83. GetSwitchValue:=s1;
  84. exit;
  85. end;
  86. 's':
  87. begin
  88. GetSwitchValue:=s1;
  89. exit;
  90. end;
  91. end
  92. else if s1=s2 then
  93. begin
  94. GetSwitchValue:=s1;
  95. exit;
  96. end;
  97. end;
  98. error;
  99. end;
  100. GetSwitchValue:=default;
  101. end;
  102. procedure help_info;
  103. begin
  104. fn:=paramstr(0);
  105. for jj:=length(fn)downto 1 do
  106. if fn[jj] in [':','\','/']then
  107. begin
  108. fn:=copy(fn,succ(jj),255);
  109. break;
  110. end;
  111. writeln('Usage: ',fn,' [options]');
  112. writeln('Options:');
  113. writeln('-i | --input <file> - set input file;');
  114. writeln('-m | --subsystem <console | gui> - set Win32 subsystem;');
  115. writeln('-s | --stack <size> - set stack size;');
  116. writeln('-t | --type <exe | dll> - define binary type;');
  117. writeln('-V | --version <n.n> - set image version;');
  118. writeln('-v | --verbose - show info while processing;');
  119. writeln('-h | --help | -? - show this screen');
  120. halt;
  121. end;
  122. begin
  123. aktglobalswitches:=[];
  124. verbose:=false;
  125. if paramcount=0 then
  126. help_info;
  127. for ii:=1 to paramcount do
  128. if(paramstr(ii)='-h')or(paramstr(ii)='--help')or(paramstr(ii)='-?')then
  129. help_info
  130. else if(paramstr(ii)='-v')or(paramstr(ii)='--verbose')then
  131. begin
  132. verbose:=true;
  133. break;
  134. end;
  135. fn:=GetSwitchValue('--input','-i','',['*s']);
  136. val(GetSwitchValue('--stack','-s','33554432',['*i']),stacksize,code);
  137. {value from
  138. systems.pas
  139. for Win32 target}
  140. s:=GetSwitchValue('--subsystem','-m','console',['gui','console']);
  141. if s='gui' then
  142. apptype:=at_GUI
  143. else
  144. apptype:=at_cui;
  145. dllversion:=GetSwitchValue('--version','-V','1.0',['*r']);
  146. { val(dllversion,x,code);
  147. dllmajor:=trunc(x);
  148. dllminor:=trunc(frac(x)*10);
  149. This does not work for 1.12 !! PM }
  150. p:=pos('.',dllversion);
  151. if p=0 then
  152. begin
  153. dllminor:=0;
  154. val(dllversion,ddlmajor,code);
  155. end
  156. else
  157. begin
  158. val(copy(dllversion,1,p-1),ddlmajor,code);
  159. val(copy(dllversion,p+1,255),ddlminor,code);
  160. end;
  161. isDll:=GetSwitchValue('--type','-t','exe',['exe','dll'])='dll';
  162. { if isDLL then
  163. aktglobalswitches:=[cs_link_extern];
  164. no because otherwise you don't change anything to a dll !!
  165. by the way why not simply use the suffix ?? PM }
  166. l.init;
  167. l.PostProcessExecutable(fn,isdll);
  168. end.