t_go32v2.pas 10 KB

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