t_go32v2.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. {
  2. $Id$
  3. Copyright (c) 1999 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. HPath : PStringQueueItem;
  58. s : string;
  59. linklibc : boolean;
  60. begin
  61. WriteResponseFile:=False;
  62. { Open link.res file }
  63. LinkRes.Init(Info.ResName);
  64. { Write path to search libraries }
  65. HPath:=current_module^.locallibrarysearchpath.First;
  66. while assigned(HPath) do
  67. begin
  68. LinkRes.Add('-L'+HPath^.Data^);
  69. HPath:=HPath^.Next;
  70. end;
  71. HPath:=LibrarySearchPath.First;
  72. while assigned(HPath) do
  73. begin
  74. LinkRes.Add('-L'+HPath^.Data^);
  75. HPath:=HPath^.Next;
  76. end;
  77. { add objectfiles, start with prt0 always }
  78. LinkRes.AddFileName(FindObjectFile('prt0'));
  79. while not ObjectFiles.Empty do
  80. begin
  81. s:=ObjectFiles.Get;
  82. if s<>'' then
  83. LinkRes.AddFileName(s);
  84. end;
  85. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  86. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  87. linklibc:=false;
  88. While not SharedLibFiles.Empty do
  89. begin
  90. S:=SharedLibFiles.Get;
  91. if s<>'c' then
  92. begin
  93. i:=Pos(target_os.sharedlibext,S);
  94. if i>0 then
  95. Delete(S,i,255);
  96. LinkRes.Add('-l'+s);
  97. end
  98. else
  99. begin
  100. LinkRes.Add('-l'+s);
  101. linklibc:=true;
  102. end;
  103. end;
  104. { be sure that libc&libgcc is the last lib }
  105. if linklibc then
  106. begin
  107. LinkRes.Add('-lc');
  108. LinkRes.Add('-lgcc');
  109. end;
  110. { Write staticlibraries }
  111. if not StaticLibFiles.Empty then
  112. begin
  113. LinkRes.Add('-(');
  114. While not StaticLibFiles.Empty do
  115. begin
  116. S:=StaticLibFiles.Get;
  117. LinkRes.AddFileName(s)
  118. end;
  119. LinkRes.Add('-)');
  120. end;
  121. { Write and Close response }
  122. linkres.writetodisk;
  123. linkres.done;
  124. WriteResponseFile:=True;
  125. end;
  126. function TLinkerGo32v2.MakeExecutable:boolean;
  127. var
  128. binstr,
  129. cmdstr : string;
  130. success : boolean;
  131. StripStr : string[40];
  132. begin
  133. if not(cs_link_extern in aktglobalswitches) then
  134. Message1(exec_i_linking,current_module^.exefilename^);
  135. { Create some replacements }
  136. StripStr:='';
  137. if (cs_link_strip in aktglobalswitches) then
  138. StripStr:='-s';
  139. { Write used files and libraries }
  140. WriteResponseFile(false);
  141. { Call linker }
  142. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  143. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  144. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  145. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  146. Replace(cmdstr,'$STRIP',StripStr);
  147. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  148. { Remove ReponseFile }
  149. if (success) and not(cs_link_extern in aktglobalswitches) then
  150. RemoveFile(current_module^.outpath^+Info.ResName);
  151. MakeExecutable:=success; { otherwise a recursive call to link method }
  152. end;
  153. {$ifdef notnecessary}
  154. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  155. type
  156. tcoffheader=packed record
  157. mach : word;
  158. nsects : word;
  159. time : longint;
  160. sympos : longint;
  161. syms : longint;
  162. opthdr : word;
  163. flag : word;
  164. end;
  165. tcoffsechdr=packed record
  166. name : array[0..7] of char;
  167. vsize : longint;
  168. rvaofs : longint;
  169. datalen : longint;
  170. datapos : longint;
  171. relocpos : longint;
  172. lineno1 : longint;
  173. nrelocs : word;
  174. lineno2 : word;
  175. flags : longint;
  176. end;
  177. psecfill=^tsecfill;
  178. tsecfill=record
  179. fillpos,
  180. fillsize : longint;
  181. next : psecfill;
  182. end;
  183. var
  184. f : file;
  185. coffheader : tcoffheader;
  186. firstsecpos,
  187. maxfillsize,
  188. l : longint;
  189. coffsec : tcoffsechdr;
  190. secroot,hsecroot : psecfill;
  191. zerobuf : pointer;
  192. begin
  193. { when -s is used quit, because there is no .exe }
  194. if cs_link_extern in aktglobalswitches then
  195. exit;
  196. { open file }
  197. assign(f,n);
  198. {$I-}
  199. reset(f,1);
  200. if ioresult<>0 then
  201. Message1(execinfo_f_cant_open_executable,n);
  202. { read headers }
  203. seek(f,2048);
  204. blockread(f,coffheader,sizeof(tcoffheader));
  205. { read section info }
  206. maxfillsize:=0;
  207. firstsecpos:=0;
  208. secroot:=nil;
  209. for l:=1to coffheader.nSects do
  210. begin
  211. blockread(f,coffsec,sizeof(tcoffsechdr));
  212. if coffsec.datapos>0 then
  213. begin
  214. if secroot=nil then
  215. firstsecpos:=coffsec.datapos;
  216. new(hsecroot);
  217. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  218. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  219. hsecroot^.next:=secroot;
  220. secroot:=hsecroot;
  221. if secroot^.fillsize>maxfillsize then
  222. maxfillsize:=secroot^.fillsize;
  223. end;
  224. end;
  225. if firstsecpos>0 then
  226. begin
  227. l:=firstsecpos-filepos(f);
  228. if l>maxfillsize then
  229. maxfillsize:=l;
  230. end
  231. else
  232. l:=0;
  233. { get zero buffer }
  234. getmem(zerobuf,maxfillsize);
  235. fillchar(zerobuf^,maxfillsize,0);
  236. { zero from sectioninfo until first section }
  237. blockwrite(f,zerobuf^,l);
  238. { zero section alignments }
  239. while assigned(secroot) do
  240. begin
  241. seek(f,secroot^.fillpos);
  242. blockwrite(f,zerobuf^,secroot^.fillsize);
  243. hsecroot:=secroot;
  244. secroot:=secroot^.next;
  245. dispose(hsecroot);
  246. end;
  247. freemem(zerobuf,maxfillsize);
  248. close(f);
  249. {$I+}
  250. i:=ioresult;
  251. postprocessexecutable:=true;
  252. end;
  253. {$endif}
  254. end.
  255. {
  256. $Log$
  257. Revision 1.4 1999-11-12 11:03:50 peter
  258. * searchpaths changed to stringqueue object
  259. Revision 1.3 1999/11/04 10:55:31 peter
  260. * TSearchPathString for the string type of the searchpaths, which is
  261. ansistring under FPC/Delphi
  262. Revision 1.2 1999/10/22 14:42:40 peter
  263. * reset linklibc
  264. Revision 1.1 1999/10/21 14:29:38 peter
  265. * redesigned linker object
  266. + library support for linux (only procedures can be exported)
  267. }