t_go32v2.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Go32v2 target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_go32v2;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutils,cclasses,
  25. globtype,globals,systems,verbose,script,
  26. fmodule,i_go32v2,
  27. link,ogcoff;
  28. type
  29. TInternalLinkerGo32v2=class(TInternallinker)
  30. constructor create;override;
  31. procedure DefaultLinkScript;override;
  32. end;
  33. TExternalLinkerGo32v2=class(texternallinker)
  34. private
  35. Function WriteResponseFile(isdll:boolean) : Boolean;
  36. Function WriteScript(isdll:boolean) : Boolean;
  37. public
  38. constructor Create;override;
  39. procedure SetDefaultInfo;override;
  40. function MakeExecutable:boolean;override;
  41. end;
  42. {****************************************************************************
  43. TCoffLinker
  44. ****************************************************************************}
  45. constructor TInternalLinkerGo32v2.Create;
  46. begin
  47. inherited Create;
  48. CExeoutput:=TDJCoffexeoutput;
  49. CObjInput:=TDJCoffObjInput;
  50. end;
  51. procedure TInternalLinkerGo32v2.DefaultLinkScript;
  52. begin
  53. end;
  54. {****************************************************************************
  55. TExternalLinkerGo32v2
  56. ****************************************************************************}
  57. Constructor TExternalLinkerGo32v2.Create;
  58. begin
  59. Inherited Create;
  60. { allow duplicated libs (PM) }
  61. SharedLibFiles.doubles:=true;
  62. StaticLibFiles.doubles:=true;
  63. end;
  64. procedure TExternalLinkerGo32v2.SetDefaultInfo;
  65. begin
  66. with Info do
  67. begin
  68. ExeCmd[1]:='ld $SCRIPT $OPT $STRIP -o $EXE $RES';
  69. end;
  70. end;
  71. Function TExternalLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  72. Var
  73. linkres : TLinkRes;
  74. i : longint;
  75. s : string;
  76. linklibc : boolean;
  77. begin
  78. WriteResponseFile:=False;
  79. { Open link.res file }
  80. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  81. { Write staticlibraries }
  82. if not StaticLibFiles.Empty then
  83. begin
  84. LinkRes.Add('-(');
  85. While not StaticLibFiles.Empty do
  86. begin
  87. S:=StaticLibFiles.GetFirst;
  88. LinkRes.AddFileName(GetShortName(s))
  89. end;
  90. LinkRes.Add('-)');
  91. end;
  92. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  93. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  94. linklibc:=false;
  95. While not SharedLibFiles.Empty do
  96. begin
  97. S:=SharedLibFiles.GetFirst;
  98. if s<>'c' then
  99. begin
  100. i:=Pos(target_info.sharedlibext,S);
  101. if i>0 then
  102. Delete(S,i,255);
  103. LinkRes.Add('-l'+s);
  104. end
  105. else
  106. begin
  107. LinkRes.Add('-l'+s);
  108. linklibc:=true;
  109. end;
  110. end;
  111. { be sure that libc&libgcc is the last lib }
  112. if linklibc then
  113. begin
  114. LinkRes.Add('-lc');
  115. LinkRes.Add('-lgcc');
  116. end;
  117. { Write and Close response }
  118. linkres.writetodisk;
  119. LinkRes.Free;
  120. WriteResponseFile:=True;
  121. end;
  122. Function TExternalLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  123. Var
  124. scriptres : TLinkRes;
  125. HPath : TCmdStrListItem;
  126. s : string;
  127. begin
  128. WriteScript:=False;
  129. { Open link.res file }
  130. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  131. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  132. ScriptRes.Add('ENTRY(start)');
  133. ScriptRes.Add('SECTIONS');
  134. ScriptRes.Add('{');
  135. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  136. ScriptRes.Add(' . = ALIGN(16);');
  137. { add objectfiles, start with prt0 always }
  138. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  139. while not ObjectFiles.Empty do
  140. begin
  141. s:=ObjectFiles.GetFirst;
  142. if s<>'' then
  143. begin
  144. ScriptRes.Add(' . = ALIGN(16);');
  145. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  146. end;
  147. end;
  148. ScriptRes.Add(' *(.text)');
  149. ScriptRes.Add(' etext = . ; _etext = .;');
  150. ScriptRes.Add(' . = ALIGN(0x200);');
  151. ScriptRes.Add(' }');
  152. ScriptRes.Add(' .data ALIGN(0x200) : {');
  153. ScriptRes.Add(' djgpp_first_ctor = . ;');
  154. ScriptRes.Add(' *(.ctor)');
  155. ScriptRes.Add(' djgpp_last_ctor = . ;');
  156. ScriptRes.Add(' djgpp_first_dtor = . ;');
  157. ScriptRes.Add(' *(.dtor)');
  158. ScriptRes.Add(' djgpp_last_dtor = . ;');
  159. ScriptRes.Add(' *(.data)');
  160. ScriptRes.Add(' *(.fpc*)');
  161. ScriptRes.Add(' *(.gcc_exc)');
  162. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  163. ScriptRes.Add(' *(.eh_fram)');
  164. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  165. ScriptRes.Add(' LONG(0)');
  166. ScriptRes.Add(' edata = . ; _edata = .;');
  167. ScriptRes.Add(' . = ALIGN(0x200);');
  168. ScriptRes.Add(' }');
  169. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  170. ScriptRes.Add(' {');
  171. ScriptRes.Add(' _object.2 = . ;');
  172. ScriptRes.Add(' . += 24 ;');
  173. ScriptRes.Add(' *(.bss)');
  174. ScriptRes.Add(' *(COMMON)');
  175. ScriptRes.Add(' end = . ; _end = .;');
  176. ScriptRes.Add(' . = ALIGN(0x200);');
  177. ScriptRes.Add(' }');
  178. ScriptRes.Add(' }');
  179. { Write path to search libraries }
  180. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  181. while assigned(HPath) do
  182. begin
  183. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  184. HPath:=TCmdStrListItem(HPath.Next);
  185. end;
  186. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  187. while assigned(HPath) do
  188. begin
  189. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  190. HPath:=TCmdStrListItem(HPath.Next);
  191. end;
  192. { Write and Close response }
  193. ScriptRes.WriteToDisk;
  194. ScriptRes.Free;
  195. WriteScript:=True;
  196. end;
  197. function TExternalLinkerGo32v2.MakeExecutable:boolean;
  198. var
  199. binstr,
  200. cmdstr : TCmdStr;
  201. success : boolean;
  202. StripStr : string[40];
  203. begin
  204. if not(cs_link_nolink in current_settings.globalswitches) then
  205. Message1(exec_i_linking,current_module.exefilename^);
  206. { Create some replacements }
  207. StripStr:='';
  208. if (cs_link_strip in current_settings.globalswitches) then
  209. StripStr:='-s';
  210. { Write used files and libraries and our own ld script }
  211. WriteScript(false);
  212. WriteResponsefile(false);
  213. { Call linker }
  214. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  215. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  216. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  217. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  218. (* Potential issues with older ld version??? *)
  219. Replace(cmdstr,'$STRIP',StripStr);
  220. Replace(cmdstr,'$SCRIPT','--script='+maybequoted(outputexedir+Info.ScriptName));
  221. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  222. { Remove ReponseFile }
  223. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  224. begin
  225. DeleteFile(outputexedir+Info.ResName);
  226. DeleteFile(outputexedir+Info.ScriptName);
  227. end;
  228. MakeExecutable:=success; { otherwise a recursive call to link method }
  229. end;
  230. {$ifdef notnecessary}
  231. procedure TExternalLinkerGo32v2.postprocessexecutable(const n : string);
  232. type
  233. tcoffheader=packed record
  234. mach : word;
  235. nsects : word;
  236. time : longint;
  237. sympos : longint;
  238. syms : longint;
  239. opthdr : word;
  240. flag : word;
  241. end;
  242. tcoffsechdr=packed record
  243. name : array[0..7] of char;
  244. vsize : longint;
  245. rvaofs : longint;
  246. datalen : longint;
  247. datapos : longint;
  248. relocpos : longint;
  249. lineno1 : longint;
  250. nrelocs : word;
  251. lineno2 : word;
  252. flags : longint;
  253. end;
  254. psecfill=^TSecfill;
  255. TSecfill=record
  256. fillpos,
  257. fillsize : longint;
  258. next : psecfill;
  259. end;
  260. var
  261. f : file;
  262. coffheader : tcoffheader;
  263. firstsecpos,
  264. maxfillsize,
  265. l : longint;
  266. coffsec : tcoffsechdr;
  267. secroot,hsecroot : psecfill;
  268. zerobuf : pointer;
  269. begin
  270. { when -s is used quit, because there is no .exe }
  271. if cs_link_nolink in current_settings.globalswitches then
  272. exit;
  273. { open file }
  274. assign(f,n);
  275. {$I-}
  276. reset(f,1);
  277. if ioresult<>0 then
  278. Message1(execinfo_f_cant_open_executable,n);
  279. { read headers }
  280. seek(f,2048);
  281. blockread(f,coffheader,sizeof(tcoffheader));
  282. { read section info }
  283. maxfillsize:=0;
  284. firstsecpos:=0;
  285. secroot:=nil;
  286. for l:=1to coffheader.nSects do
  287. begin
  288. blockread(f,coffsec,sizeof(tcoffsechdr));
  289. if coffsec.datapos>0 then
  290. begin
  291. if secroot=nil then
  292. firstsecpos:=coffsec.datapos;
  293. new(hsecroot);
  294. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  295. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  296. hsecroot^.next:=secroot;
  297. secroot:=hsecroot;
  298. if secroot^.fillsize>maxfillsize then
  299. maxfillsize:=secroot^.fillsize;
  300. end;
  301. end;
  302. if firstsecpos>0 then
  303. begin
  304. l:=firstsecpos-filepos(f);
  305. if l>maxfillsize then
  306. maxfillsize:=l;
  307. end
  308. else
  309. l:=0;
  310. { get zero buffer }
  311. getmem(zerobuf,maxfillsize);
  312. fillchar(zerobuf^,maxfillsize,0);
  313. { zero from sectioninfo until first section }
  314. blockwrite(f,zerobuf^,l);
  315. { zero section alignments }
  316. while assigned(secroot) do
  317. begin
  318. seek(f,secroot^.fillpos);
  319. blockwrite(f,zerobuf^,secroot^.fillsize);
  320. hsecroot:=secroot;
  321. secroot:=secroot^.next;
  322. dispose(hsecroot);
  323. end;
  324. freemem(zerobuf,maxfillsize);
  325. close(f);
  326. {$I+}
  327. i:=ioresult;
  328. postprocessexecutable:=true;
  329. end;
  330. {$endif}
  331. {*****************************************************************************
  332. Initialize
  333. *****************************************************************************}
  334. initialization
  335. RegisterExternalLinker(system_i386_go32v2_info,TExternalLinkerGo32v2);
  336. // RegisterInternalLinker(system_i386_go32v2_info,TInternalLinkerGo32v2);
  337. RegisterTarget(system_i386_go32v2_info);
  338. end.