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