t_go32v2.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  21. interface
  22. uses
  23. link;
  24. type
  25. tlinkergo32v2=class(tlinker)
  26. private
  27. Function WriteResponseFile(isdll:boolean) : Boolean;
  28. Function WriteScript(isdll:boolean) : Boolean;
  29. public
  30. constructor Create;
  31. procedure SetDefaultInfo;override;
  32. function MakeExecutable:boolean;override;
  33. end;
  34. implementation
  35. uses
  36. cutils,cclasses,
  37. globtype,globals,systems,verbose,script,fmodule;
  38. {****************************************************************************
  39. TLinkerGo32v2
  40. ****************************************************************************}
  41. Constructor TLinkerGo32v2.Create;
  42. begin
  43. Inherited Create;
  44. { allow duplicated libs (PM) }
  45. SharedLibFiles.doubles:=true;
  46. StaticLibFiles.doubles:=true;
  47. end;
  48. procedure TLinkerGo32v2.SetDefaultInfo;
  49. begin
  50. with Info do
  51. begin
  52. if cs_align in aktglobalswitches then
  53. ExeCmd[1]:='ld $SCRIPT $OPT $STRIP -o $EXE'
  54. else
  55. ExeCmd[1]:='ld -oformat coff-go32-exe $OPT $STRIP -o $EXE @$RES'
  56. end;
  57. end;
  58. Function TLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  59. Var
  60. linkres : TLinkRes;
  61. i : longint;
  62. HPath : TStringListItem;
  63. s : string;
  64. linklibc : boolean;
  65. begin
  66. WriteResponseFile:=False;
  67. { Open link.res file }
  68. LinkRes.Init(outputexedir+Info.ResName);
  69. { Write path to search libraries }
  70. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  71. while assigned(HPath) do
  72. begin
  73. LinkRes.Add('-L'+GetShortName(HPath.Str));
  74. HPath:=TStringListItem(HPath.Next);
  75. end;
  76. HPath:=TStringListItem(LibrarySearchPath.First);
  77. while assigned(HPath) do
  78. begin
  79. LinkRes.Add('-L'+GetShortName(HPath.Str));
  80. HPath:=TStringListItem(HPath.Next);
  81. end;
  82. { add objectfiles, start with prt0 always }
  83. LinkRes.AddFileName(GetShortName(FindObjectFile('prt0','')));
  84. while not ObjectFiles.Empty do
  85. begin
  86. s:=ObjectFiles.GetFirst;
  87. if s<>'' then
  88. LinkRes.AddFileName(GetShortName(s));
  89. end;
  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_os.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.done;
  129. WriteResponseFile:=True;
  130. end;
  131. Function TLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  132. Var
  133. scriptres : TLinkRes;
  134. i : longint;
  135. s : string;
  136. linklibc : boolean;
  137. begin
  138. WriteScript:=False;
  139. { Open link.res file }
  140. ScriptRes.Init(outputexedir+Info.ResName);
  141. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  142. ScriptRes.Add('ENTRY(start)');
  143. {$ifdef dummy}
  144. { Write path to search libraries }
  145. HPath:=current_module.locallibrarysearchpath.First;
  146. while assigned(HPath) do
  147. begin
  148. ScriptRes.Add('SEARCH_PATH("'+GetShortName(HPath^.Data^)+'")');
  149. HPath:=HPath^.Next;
  150. end;
  151. HPath:=LibrarySearchPath.First;
  152. while assigned(HPath) do
  153. begin
  154. ScriptRes.Add('SEARCH_PATH("'+GetShortName(HPath^.Data^)+'")');
  155. HPath:=HPath^.Next;
  156. end;
  157. {$endif dummy}
  158. ScriptRes.Add('SECTIONS');
  159. ScriptRes.Add('{');
  160. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  161. ScriptRes.Add(' . = ALIGN(16);');
  162. { add objectfiles, start with prt0 always }
  163. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0',''))+'(.text)');
  164. while not ObjectFiles.Empty do
  165. begin
  166. s:=ObjectFiles.GetFirst;
  167. if s<>'' then
  168. begin
  169. ScriptRes.Add(' . = ALIGN(16);');
  170. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  171. end;
  172. end;
  173. ScriptRes.Add(' *(.text)');
  174. ScriptRes.Add(' etext = . ; _etext = .;');
  175. ScriptRes.Add(' . = ALIGN(0x200);');
  176. ScriptRes.Add(' }');
  177. ScriptRes.Add(' .data ALIGN(0x200) : {');
  178. ScriptRes.Add(' djgpp_first_ctor = . ;');
  179. ScriptRes.Add(' *(.ctor)');
  180. ScriptRes.Add(' djgpp_last_ctor = . ;');
  181. ScriptRes.Add(' djgpp_first_dtor = . ;');
  182. ScriptRes.Add(' *(.dtor)');
  183. ScriptRes.Add(' djgpp_last_dtor = . ;');
  184. ScriptRes.Add(' *(.data)');
  185. ScriptRes.Add(' *(.gcc_exc)');
  186. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  187. ScriptRes.Add(' *(.eh_fram)');
  188. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  189. ScriptRes.Add(' LONG(0)');
  190. ScriptRes.Add(' edata = . ; _edata = .;');
  191. ScriptRes.Add(' . = ALIGN(0x200);');
  192. ScriptRes.Add(' }');
  193. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  194. ScriptRes.Add(' {');
  195. ScriptRes.Add(' _object.2 = . ;');
  196. ScriptRes.Add(' . += 24 ;');
  197. ScriptRes.Add(' *(.bss)');
  198. ScriptRes.Add(' *(COMMON)');
  199. ScriptRes.Add(' end = . ; _end = .;');
  200. ScriptRes.Add(' . = ALIGN(0x200);');
  201. ScriptRes.Add(' }');
  202. ScriptRes.Add(' }');
  203. { Write staticlibraries }
  204. if not StaticLibFiles.Empty then
  205. begin
  206. ScriptRes.Add('-(');
  207. While not StaticLibFiles.Empty do
  208. begin
  209. S:=StaticLibFiles.GetFirst;
  210. ScriptRes.AddFileName(GetShortName(s))
  211. end;
  212. ScriptRes.Add('-)');
  213. end;
  214. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  215. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  216. linklibc:=false;
  217. While not SharedLibFiles.Empty do
  218. begin
  219. S:=SharedLibFiles.GetFirst;
  220. if s<>'c' then
  221. begin
  222. i:=Pos(target_os.sharedlibext,S);
  223. if i>0 then
  224. Delete(S,i,255);
  225. ScriptRes.Add('-l'+s);
  226. end
  227. else
  228. begin
  229. ScriptRes.Add('-l'+s);
  230. linklibc:=true;
  231. end;
  232. end;
  233. { be sure that libc&libgcc is the last lib }
  234. if linklibc then
  235. begin
  236. ScriptRes.Add('-lc');
  237. ScriptRes.Add('-lgcc');
  238. end;
  239. { Write and Close response }
  240. ScriptRes.WriteToDisk;
  241. ScriptRes.done;
  242. WriteScript:=True;
  243. end;
  244. function TLinkerGo32v2.MakeExecutable:boolean;
  245. var
  246. binstr,
  247. cmdstr : string;
  248. success : boolean;
  249. StripStr : string[40];
  250. begin
  251. if not(cs_link_extern in aktglobalswitches) then
  252. Message1(exec_i_linking,current_module.exefilename^);
  253. { Create some replacements }
  254. StripStr:='';
  255. if (cs_link_strip in aktglobalswitches) then
  256. StripStr:='-s';
  257. if cs_align in aktglobalswitches then
  258. WriteScript(false)
  259. else
  260. { Write used files and libraries }
  261. WriteResponseFile(false);
  262. { Call linker }
  263. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  264. Replace(cmdstr,'$EXE',current_module.exefilename^);
  265. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  266. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  267. Replace(cmdstr,'$STRIP',StripStr);
  268. Replace(cmdstr,'$SCRIPT','--script='+outputexedir+Info.ResName);
  269. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  270. { Remove ReponseFile }
  271. if (success) and not(cs_link_extern in aktglobalswitches) then
  272. RemoveFile(outputexedir+Info.ResName);
  273. MakeExecutable:=success; { otherwise a recursive call to link method }
  274. end;
  275. {$ifdef notnecessary}
  276. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  277. type
  278. tcoffheader=packed record
  279. mach : word;
  280. nsects : word;
  281. time : longint;
  282. sympos : longint;
  283. syms : longint;
  284. opthdr : word;
  285. flag : word;
  286. end;
  287. tcoffsechdr=packed record
  288. name : array[0..7] of char;
  289. vsize : longint;
  290. rvaofs : longint;
  291. datalen : longint;
  292. datapos : longint;
  293. relocpos : longint;
  294. lineno1 : longint;
  295. nrelocs : word;
  296. lineno2 : word;
  297. flags : longint;
  298. end;
  299. psecfill=^tsecfill;
  300. tsecfill=record
  301. fillpos,
  302. fillsize : longint;
  303. next : psecfill;
  304. end;
  305. var
  306. f : file;
  307. coffheader : tcoffheader;
  308. firstsecpos,
  309. maxfillsize,
  310. l : longint;
  311. coffsec : tcoffsechdr;
  312. secroot,hsecroot : psecfill;
  313. zerobuf : pointer;
  314. begin
  315. { when -s is used quit, because there is no .exe }
  316. if cs_link_extern in aktglobalswitches then
  317. exit;
  318. { open file }
  319. assign(f,n);
  320. {$I-}
  321. reset(f,1);
  322. if ioresult<>0 then
  323. Message1(execinfo_f_cant_open_executable,n);
  324. { read headers }
  325. seek(f,2048);
  326. blockread(f,coffheader,sizeof(tcoffheader));
  327. { read section info }
  328. maxfillsize:=0;
  329. firstsecpos:=0;
  330. secroot:=nil;
  331. for l:=1to coffheader.nSects do
  332. begin
  333. blockread(f,coffsec,sizeof(tcoffsechdr));
  334. if coffsec.datapos>0 then
  335. begin
  336. if secroot=nil then
  337. firstsecpos:=coffsec.datapos;
  338. new(hsecroot);
  339. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  340. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  341. hsecroot^.next:=secroot;
  342. secroot:=hsecroot;
  343. if secroot^.fillsize>maxfillsize then
  344. maxfillsize:=secroot^.fillsize;
  345. end;
  346. end;
  347. if firstsecpos>0 then
  348. begin
  349. l:=firstsecpos-filepos(f);
  350. if l>maxfillsize then
  351. maxfillsize:=l;
  352. end
  353. else
  354. l:=0;
  355. { get zero buffer }
  356. getmem(zerobuf,maxfillsize);
  357. fillchar(zerobuf^,maxfillsize,0);
  358. { zero from sectioninfo until first section }
  359. blockwrite(f,zerobuf^,l);
  360. { zero section alignments }
  361. while assigned(secroot) do
  362. begin
  363. seek(f,secroot^.fillpos);
  364. blockwrite(f,zerobuf^,secroot^.fillsize);
  365. hsecroot:=secroot;
  366. secroot:=secroot^.next;
  367. dispose(hsecroot);
  368. end;
  369. freemem(zerobuf,maxfillsize);
  370. close(f);
  371. {$I+}
  372. i:=ioresult;
  373. postprocessexecutable:=true;
  374. end;
  375. {$endif}
  376. end.
  377. {
  378. $Log$
  379. Revision 1.7 2001-01-27 21:29:35 florian
  380. * behavior -Oa optimized
  381. Revision 1.6 2000/12/25 00:07:30 peter
  382. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  383. tlinkedlist objects)
  384. Revision 1.5 2000/09/24 15:06:31 peter
  385. * use defines.inc
  386. Revision 1.4 2000/08/27 16:11:54 peter
  387. * moved some util functions from globals,cobjects to cutils
  388. * splitted files into finput,fmodule
  389. Revision 1.3 2000/08/16 13:06:07 florian
  390. + support of 64 bit integer constants
  391. Revision 1.2 2000/07/13 11:32:50 michael
  392. + removed logs
  393. }