t_go32v2.pas 7.1 KB

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