t_go32v2.pas 11 KB

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