t_go32v2.pas 7.0 KB

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