t_go32v2.pas 11 KB

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