t_go32v2.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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,cfileutl,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 $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. { Add all options to link.res instead of passing them via command line:
  82. DOS command line is limited to 126 characters! }
  83. LinkRes.Add('--script='+maybequoted(outputexedir+Info.ScriptName));
  84. if info.ExtraOptions<>'' then
  85. LinkRes.Add(Info.ExtraOptions);
  86. (* Potential issues with older ld version??? *)
  87. if (cs_link_strip in current_settings.globalswitches) then
  88. LinkRes.Add('-s');
  89. LinkRes.Add('-o '+maybequoted(current_module.exefilename^));
  90. { Write staticlibraries }
  91. if not StaticLibFiles.Empty then
  92. begin
  93. LinkRes.Add('-(');
  94. While not StaticLibFiles.Empty do
  95. begin
  96. S:=StaticLibFiles.GetFirst;
  97. LinkRes.AddFileName(GetShortName(s))
  98. end;
  99. LinkRes.Add('-)');
  100. end;
  101. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  102. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  103. linklibc:=false;
  104. While not SharedLibFiles.Empty do
  105. begin
  106. S:=SharedLibFiles.GetFirst;
  107. if s<>'c' then
  108. begin
  109. i:=Pos(target_info.sharedlibext,S);
  110. if i>0 then
  111. Delete(S,i,255);
  112. LinkRes.Add('-l'+s);
  113. end
  114. else
  115. begin
  116. LinkRes.Add('-l'+s);
  117. linklibc:=true;
  118. end;
  119. end;
  120. { be sure that libc&libgcc is the last lib }
  121. if linklibc then
  122. begin
  123. LinkRes.Add('-lc');
  124. LinkRes.Add('-lgcc');
  125. end;
  126. { Write and Close response }
  127. linkres.writetodisk;
  128. LinkRes.Free;
  129. WriteResponseFile:=True;
  130. end;
  131. Function TExternalLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  132. Var
  133. scriptres : TLinkRes;
  134. HPath : TCmdStrListItem;
  135. s : string;
  136. begin
  137. WriteScript:=False;
  138. { Open link.res file }
  139. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  140. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  141. ScriptRes.Add('ENTRY(start)');
  142. ScriptRes.Add('SECTIONS');
  143. ScriptRes.Add('{');
  144. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  145. ScriptRes.Add(' . = ALIGN(16);');
  146. { add objectfiles, start with prt0 always }
  147. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  148. while not ObjectFiles.Empty do
  149. begin
  150. s:=ObjectFiles.GetFirst;
  151. if s<>'' then
  152. begin
  153. ScriptRes.Add(' . = ALIGN(16);');
  154. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  155. end;
  156. end;
  157. ScriptRes.Add(' *(.text)');
  158. ScriptRes.Add(' etext = . ; _etext = .;');
  159. ScriptRes.Add(' . = ALIGN(0x200);');
  160. ScriptRes.Add(' }');
  161. ScriptRes.Add(' .data ALIGN(0x200) : {');
  162. ScriptRes.Add(' djgpp_first_ctor = . ;');
  163. ScriptRes.Add(' *(.ctor)');
  164. ScriptRes.Add(' djgpp_last_ctor = . ;');
  165. ScriptRes.Add(' djgpp_first_dtor = . ;');
  166. ScriptRes.Add(' *(.dtor)');
  167. ScriptRes.Add(' djgpp_last_dtor = . ;');
  168. ScriptRes.Add(' *(.data)');
  169. ScriptRes.Add(' *(.fpc*)');
  170. ScriptRes.Add(' *(.gcc_exc)');
  171. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  172. ScriptRes.Add(' *(.eh_fram)');
  173. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  174. ScriptRes.Add(' LONG(0)');
  175. ScriptRes.Add(' edata = . ; _edata = .;');
  176. ScriptRes.Add(' . = ALIGN(0x200);');
  177. ScriptRes.Add(' }');
  178. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  179. ScriptRes.Add(' {');
  180. ScriptRes.Add(' _object.2 = . ;');
  181. ScriptRes.Add(' . += 24 ;');
  182. ScriptRes.Add(' *(.bss)');
  183. ScriptRes.Add(' *(COMMON)');
  184. ScriptRes.Add(' end = . ; _end = .;');
  185. ScriptRes.Add(' . = ALIGN(0x200);');
  186. ScriptRes.Add(' }');
  187. ScriptRes.Add(' }');
  188. { Write path to search libraries }
  189. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  190. while assigned(HPath) do
  191. begin
  192. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  193. HPath:=TCmdStrListItem(HPath.Next);
  194. end;
  195. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  196. while assigned(HPath) do
  197. begin
  198. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  199. HPath:=TCmdStrListItem(HPath.Next);
  200. end;
  201. { Write and Close response }
  202. ScriptRes.WriteToDisk;
  203. ScriptRes.Free;
  204. WriteScript:=True;
  205. end;
  206. function TExternalLinkerGo32v2.MakeExecutable:boolean;
  207. var
  208. binstr,
  209. cmdstr : TCmdStr;
  210. success : boolean;
  211. begin
  212. if not(cs_link_nolink in current_settings.globalswitches) then
  213. Message1(exec_i_linking,current_module.exefilename^);
  214. { Write used files and libraries and our own ld script }
  215. WriteScript(false);
  216. WriteResponsefile(false);
  217. { Call linker }
  218. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  219. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  220. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  221. { Remove ReponseFile }
  222. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  223. begin
  224. DeleteFile(outputexedir+Info.ResName);
  225. DeleteFile(outputexedir+Info.ScriptName);
  226. end;
  227. MakeExecutable:=success; { otherwise a recursive call to link method }
  228. end;
  229. {$ifdef notnecessary}
  230. procedure TExternalLinkerGo32v2.postprocessexecutable(const n : string);
  231. type
  232. tcoffheader=packed record
  233. mach : word;
  234. nsects : word;
  235. time : longint;
  236. sympos : longint;
  237. syms : longint;
  238. opthdr : word;
  239. flag : word;
  240. end;
  241. tcoffsechdr=packed record
  242. name : array[0..7] of char;
  243. vsize : longint;
  244. rvaofs : longint;
  245. datalen : longint;
  246. datapos : longint;
  247. relocpos : longint;
  248. lineno1 : longint;
  249. nrelocs : word;
  250. lineno2 : word;
  251. flags : longint;
  252. end;
  253. psecfill=^TSecfill;
  254. TSecfill=record
  255. fillpos,
  256. fillsize : longint;
  257. next : psecfill;
  258. end;
  259. var
  260. f : file;
  261. coffheader : tcoffheader;
  262. firstsecpos,
  263. maxfillsize,
  264. l : longint;
  265. coffsec : tcoffsechdr;
  266. secroot,hsecroot : psecfill;
  267. zerobuf : pointer;
  268. begin
  269. { when -s is used quit, because there is no .exe }
  270. if cs_link_nolink in current_settings.globalswitches then
  271. exit;
  272. { open file }
  273. assign(f,n);
  274. {$I-}
  275. reset(f,1);
  276. if ioresult<>0 then
  277. Message1(execinfo_f_cant_open_executable,n);
  278. { read headers }
  279. seek(f,2048);
  280. blockread(f,coffheader,sizeof(tcoffheader));
  281. { read section info }
  282. maxfillsize:=0;
  283. firstsecpos:=0;
  284. secroot:=nil;
  285. for l:=1to coffheader.nSects do
  286. begin
  287. blockread(f,coffsec,sizeof(tcoffsechdr));
  288. if coffsec.datapos>0 then
  289. begin
  290. if secroot=nil then
  291. firstsecpos:=coffsec.datapos;
  292. new(hsecroot);
  293. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  294. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  295. hsecroot^.next:=secroot;
  296. secroot:=hsecroot;
  297. if secroot^.fillsize>maxfillsize then
  298. maxfillsize:=secroot^.fillsize;
  299. end;
  300. end;
  301. if firstsecpos>0 then
  302. begin
  303. l:=firstsecpos-filepos(f);
  304. if l>maxfillsize then
  305. maxfillsize:=l;
  306. end
  307. else
  308. l:=0;
  309. { get zero buffer }
  310. getmem(zerobuf,maxfillsize);
  311. fillchar(zerobuf^,maxfillsize,0);
  312. { zero from sectioninfo until first section }
  313. blockwrite(f,zerobuf^,l);
  314. { zero section alignments }
  315. while assigned(secroot) do
  316. begin
  317. seek(f,secroot^.fillpos);
  318. blockwrite(f,zerobuf^,secroot^.fillsize);
  319. hsecroot:=secroot;
  320. secroot:=secroot^.next;
  321. dispose(hsecroot);
  322. end;
  323. freemem(zerobuf,maxfillsize);
  324. close(f);
  325. {$I+}
  326. i:=ioresult;
  327. postprocessexecutable:=true;
  328. end;
  329. {$endif}
  330. {*****************************************************************************
  331. Initialize
  332. *****************************************************************************}
  333. initialization
  334. RegisterExternalLinker(system_i386_go32v2_info,TExternalLinkerGo32v2);
  335. // RegisterInternalLinker(system_i386_go32v2_info,TInternalLinkerGo32v2);
  336. RegisterTarget(system_i386_go32v2_info);
  337. end.