t_go32v2.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Go32v2 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. unit t_go32v2;
  20. interface
  21. uses
  22. link;
  23. type
  24. plinkergo32v2=^tlinkergo32v2;
  25. tlinkergo32v2=object(tlinker)
  26. private
  27. Function WriteResponseFile(isdll:boolean) : Boolean;
  28. public
  29. constructor Init;
  30. procedure SetDefaultInfo;virtual;
  31. function MakeExecutable:boolean;virtual;
  32. end;
  33. implementation
  34. uses
  35. strings,globtype,globals,cobjects,systems,verbose,script,files;
  36. {****************************************************************************
  37. TLinkerGo32v2
  38. ****************************************************************************}
  39. Constructor TLinkerGo32v2.Init;
  40. begin
  41. Inherited Init;
  42. { allow duplicated libs (PM) }
  43. SharedLibFiles.doubles:=true;
  44. StaticLibFiles.doubles:=true;
  45. end;
  46. procedure TLinkerGo32v2.SetDefaultInfo;
  47. begin
  48. with Info do
  49. begin
  50. ExeCmd[1]:='ld -oformat coff-go32-exe $OPT $STRIP -o $EXE @$RES';
  51. end;
  52. end;
  53. Function TLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  54. Var
  55. linkres : TLinkRes;
  56. i : longint;
  57. {$IFDEF NEWST}
  58. HPath : PStringItem;
  59. {$ELSE}
  60. HPath : PStringQueueItem;
  61. {$ENDIF NEWST}
  62. s : string;
  63. linklibc : boolean;
  64. begin
  65. WriteResponseFile:=False;
  66. { Open link.res file }
  67. LinkRes.Init(outputexedir+Info.ResName);
  68. { Write path to search libraries }
  69. HPath:=current_module^.locallibrarysearchpath.First;
  70. while assigned(HPath) do
  71. begin
  72. LinkRes.Add('-L'+GetShortName(HPath^.Data^));
  73. HPath:=HPath^.Next;
  74. end;
  75. HPath:=LibrarySearchPath.First;
  76. while assigned(HPath) do
  77. begin
  78. LinkRes.Add('-L'+GetShortName(HPath^.Data^));
  79. HPath:=HPath^.Next;
  80. end;
  81. { add objectfiles, start with prt0 always }
  82. LinkRes.AddFileName(GetShortName(FindObjectFile('prt0','')));
  83. while not ObjectFiles.Empty do
  84. begin
  85. s:=ObjectFiles.Get;
  86. if s<>'' then
  87. LinkRes.AddFileName(GetShortName(s));
  88. end;
  89. { Write staticlibraries }
  90. if not StaticLibFiles.Empty then
  91. begin
  92. LinkRes.Add('-(');
  93. While not StaticLibFiles.Empty do
  94. begin
  95. S:=StaticLibFiles.Get;
  96. LinkRes.AddFileName(GetShortName(s))
  97. end;
  98. LinkRes.Add('-)');
  99. end;
  100. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  101. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  102. linklibc:=false;
  103. While not SharedLibFiles.Empty do
  104. begin
  105. S:=SharedLibFiles.Get;
  106. if s<>'c' then
  107. begin
  108. i:=Pos(target_os.sharedlibext,S);
  109. if i>0 then
  110. Delete(S,i,255);
  111. LinkRes.Add('-l'+s);
  112. end
  113. else
  114. begin
  115. LinkRes.Add('-l'+s);
  116. linklibc:=true;
  117. end;
  118. end;
  119. { be sure that libc&libgcc is the last lib }
  120. if linklibc then
  121. begin
  122. LinkRes.Add('-lc');
  123. LinkRes.Add('-lgcc');
  124. end;
  125. { Write and Close response }
  126. linkres.writetodisk;
  127. linkres.done;
  128. WriteResponseFile:=True;
  129. end;
  130. function TLinkerGo32v2.MakeExecutable:boolean;
  131. var
  132. binstr,
  133. cmdstr : string;
  134. success : boolean;
  135. StripStr : string[40];
  136. begin
  137. if not(cs_link_extern in aktglobalswitches) then
  138. Message1(exec_i_linking,current_module^.exefilename^);
  139. { Create some replacements }
  140. StripStr:='';
  141. if (cs_link_strip in aktglobalswitches) then
  142. StripStr:='-s';
  143. { Write used files and libraries }
  144. WriteResponseFile(false);
  145. { Call linker }
  146. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  147. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  148. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  149. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  150. Replace(cmdstr,'$STRIP',StripStr);
  151. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  152. { Remove ReponseFile }
  153. if (success) and not(cs_link_extern in aktglobalswitches) then
  154. RemoveFile(outputexedir+Info.ResName);
  155. MakeExecutable:=success; { otherwise a recursive call to link method }
  156. end;
  157. {$ifdef notnecessary}
  158. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  159. type
  160. tcoffheader=packed record
  161. mach : word;
  162. nsects : word;
  163. time : longint;
  164. sympos : longint;
  165. syms : longint;
  166. opthdr : word;
  167. flag : word;
  168. end;
  169. tcoffsechdr=packed record
  170. name : array[0..7] of char;
  171. vsize : longint;
  172. rvaofs : longint;
  173. datalen : longint;
  174. datapos : longint;
  175. relocpos : longint;
  176. lineno1 : longint;
  177. nrelocs : word;
  178. lineno2 : word;
  179. flags : longint;
  180. end;
  181. psecfill=^tsecfill;
  182. tsecfill=record
  183. fillpos,
  184. fillsize : longint;
  185. next : psecfill;
  186. end;
  187. var
  188. f : file;
  189. coffheader : tcoffheader;
  190. firstsecpos,
  191. maxfillsize,
  192. l : longint;
  193. coffsec : tcoffsechdr;
  194. secroot,hsecroot : psecfill;
  195. zerobuf : pointer;
  196. begin
  197. { when -s is used quit, because there is no .exe }
  198. if cs_link_extern in aktglobalswitches then
  199. exit;
  200. { open file }
  201. assign(f,n);
  202. {$I-}
  203. reset(f,1);
  204. if ioresult<>0 then
  205. Message1(execinfo_f_cant_open_executable,n);
  206. { read headers }
  207. seek(f,2048);
  208. blockread(f,coffheader,sizeof(tcoffheader));
  209. { read section info }
  210. maxfillsize:=0;
  211. firstsecpos:=0;
  212. secroot:=nil;
  213. for l:=1to coffheader.nSects do
  214. begin
  215. blockread(f,coffsec,sizeof(tcoffsechdr));
  216. if coffsec.datapos>0 then
  217. begin
  218. if secroot=nil then
  219. firstsecpos:=coffsec.datapos;
  220. new(hsecroot);
  221. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  222. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  223. hsecroot^.next:=secroot;
  224. secroot:=hsecroot;
  225. if secroot^.fillsize>maxfillsize then
  226. maxfillsize:=secroot^.fillsize;
  227. end;
  228. end;
  229. if firstsecpos>0 then
  230. begin
  231. l:=firstsecpos-filepos(f);
  232. if l>maxfillsize then
  233. maxfillsize:=l;
  234. end
  235. else
  236. l:=0;
  237. { get zero buffer }
  238. getmem(zerobuf,maxfillsize);
  239. fillchar(zerobuf^,maxfillsize,0);
  240. { zero from sectioninfo until first section }
  241. blockwrite(f,zerobuf^,l);
  242. { zero section alignments }
  243. while assigned(secroot) do
  244. begin
  245. seek(f,secroot^.fillpos);
  246. blockwrite(f,zerobuf^,secroot^.fillsize);
  247. hsecroot:=secroot;
  248. secroot:=secroot^.next;
  249. dispose(hsecroot);
  250. end;
  251. freemem(zerobuf,maxfillsize);
  252. close(f);
  253. {$I+}
  254. i:=ioresult;
  255. postprocessexecutable:=true;
  256. end;
  257. {$endif}
  258. end.
  259. {
  260. $Log$
  261. Revision 1.2 2000-07-13 11:32:50 michael
  262. + removed logs
  263. }