t_go32v2.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. implementation
  23. uses
  24. link,
  25. cutils,cclasses,
  26. globtype,globals,systems,verbose,script,fmodule;
  27. type
  28. tlinkergo32v2=class(tlinker)
  29. private
  30. Function WriteResponseFile(isdll:boolean) : Boolean;
  31. Function WriteScript(isdll:boolean) : Boolean;
  32. public
  33. constructor Create;override;
  34. procedure SetDefaultInfo;override;
  35. function MakeExecutable:boolean;override;
  36. end;
  37. {****************************************************************************
  38. TLinkerGo32v2
  39. ****************************************************************************}
  40. Constructor TLinkerGo32v2.Create;
  41. begin
  42. Inherited Create;
  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. ExeCmd[1]:='ld $SCRIPT $OPT $STRIP -o $EXE @$RES';
  52. end;
  53. end;
  54. Function TLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  55. Var
  56. linkres : TLinkRes;
  57. i : longint;
  58. s : string;
  59. linklibc : boolean;
  60. begin
  61. WriteResponseFile:=False;
  62. { Open link.res file }
  63. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  64. { Write staticlibraries }
  65. if not StaticLibFiles.Empty then
  66. begin
  67. LinkRes.Add('-(');
  68. While not StaticLibFiles.Empty do
  69. begin
  70. S:=StaticLibFiles.GetFirst;
  71. LinkRes.AddFileName(GetShortName(s))
  72. end;
  73. LinkRes.Add('-)');
  74. end;
  75. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  76. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  77. linklibc:=false;
  78. While not SharedLibFiles.Empty do
  79. begin
  80. S:=SharedLibFiles.GetFirst;
  81. if s<>'c' then
  82. begin
  83. i:=Pos(target_info.sharedlibext,S);
  84. if i>0 then
  85. Delete(S,i,255);
  86. LinkRes.Add('-l'+s);
  87. end
  88. else
  89. begin
  90. LinkRes.Add('-l'+s);
  91. linklibc:=true;
  92. end;
  93. end;
  94. { be sure that libc&libgcc is the last lib }
  95. if linklibc then
  96. begin
  97. LinkRes.Add('-lc');
  98. LinkRes.Add('-lgcc');
  99. end;
  100. { Write and Close response }
  101. linkres.writetodisk;
  102. LinkRes.Free;
  103. WriteResponseFile:=True;
  104. end;
  105. Function TLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  106. Var
  107. scriptres : TLinkRes;
  108. HPath : TStringListItem;
  109. s : string;
  110. begin
  111. WriteScript:=False;
  112. { Open link.res file }
  113. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  114. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  115. ScriptRes.Add('ENTRY(start)');
  116. ScriptRes.Add('SECTIONS');
  117. ScriptRes.Add('{');
  118. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  119. ScriptRes.Add(' . = ALIGN(16);');
  120. { add objectfiles, start with prt0 always }
  121. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0',''))+'(.text)');
  122. while not ObjectFiles.Empty do
  123. begin
  124. s:=ObjectFiles.GetFirst;
  125. if s<>'' then
  126. begin
  127. ScriptRes.Add(' . = ALIGN(16);');
  128. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  129. end;
  130. end;
  131. ScriptRes.Add(' *(.text)');
  132. ScriptRes.Add(' etext = . ; _etext = .;');
  133. ScriptRes.Add(' . = ALIGN(0x200);');
  134. ScriptRes.Add(' }');
  135. ScriptRes.Add(' .data ALIGN(0x200) : {');
  136. ScriptRes.Add(' djgpp_first_ctor = . ;');
  137. ScriptRes.Add(' *(.ctor)');
  138. ScriptRes.Add(' djgpp_last_ctor = . ;');
  139. ScriptRes.Add(' djgpp_first_dtor = . ;');
  140. ScriptRes.Add(' *(.dtor)');
  141. ScriptRes.Add(' djgpp_last_dtor = . ;');
  142. ScriptRes.Add(' *(.data)');
  143. ScriptRes.Add(' *(.gcc_exc)');
  144. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  145. ScriptRes.Add(' *(.eh_fram)');
  146. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  147. ScriptRes.Add(' LONG(0)');
  148. ScriptRes.Add(' edata = . ; _edata = .;');
  149. ScriptRes.Add(' . = ALIGN(0x200);');
  150. ScriptRes.Add(' }');
  151. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  152. ScriptRes.Add(' {');
  153. ScriptRes.Add(' _object.2 = . ;');
  154. ScriptRes.Add(' . += 24 ;');
  155. ScriptRes.Add(' *(.bss)');
  156. ScriptRes.Add(' *(COMMON)');
  157. ScriptRes.Add(' end = . ; _end = .;');
  158. ScriptRes.Add(' . = ALIGN(0x200);');
  159. ScriptRes.Add(' }');
  160. ScriptRes.Add(' }');
  161. { Write path to search libraries }
  162. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  163. while assigned(HPath) do
  164. begin
  165. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  166. HPath:=TStringListItem(HPath.Next);
  167. end;
  168. HPath:=TStringListItem(LibrarySearchPath.First);
  169. while assigned(HPath) do
  170. begin
  171. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  172. HPath:=TStringListItem(HPath.Next);
  173. end;
  174. { Write and Close response }
  175. ScriptRes.WriteToDisk;
  176. ScriptRes.Free;
  177. WriteScript:=True;
  178. end;
  179. function TLinkerGo32v2.MakeExecutable:boolean;
  180. var
  181. binstr,
  182. cmdstr : string;
  183. success : boolean;
  184. StripStr : string[40];
  185. begin
  186. if not(cs_link_extern in aktglobalswitches) then
  187. Message1(exec_i_linking,current_module.exefilename^);
  188. { Create some replacements }
  189. StripStr:='';
  190. if (cs_link_strip in aktglobalswitches) then
  191. StripStr:='-s';
  192. { Write used files and libraries and our own ld script }
  193. WriteScript(false);
  194. WriteResponsefile(false);
  195. { Call linker }
  196. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  197. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  198. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  199. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  200. Replace(cmdstr,'$STRIP',StripStr);
  201. Replace(cmdstr,'$SCRIPT','--script='+maybequoted(outputexedir+Info.ScriptName));
  202. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  203. { Remove ReponseFile }
  204. if (success) and not(cs_link_extern in aktglobalswitches) then
  205. begin
  206. RemoveFile(outputexedir+Info.ResName);
  207. RemoveFile(outputexedir+Info.ScriptName);
  208. end;
  209. MakeExecutable:=success; { otherwise a recursive call to link method }
  210. end;
  211. {$ifdef notnecessary}
  212. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  213. type
  214. tcoffheader=packed record
  215. mach : word;
  216. nsects : word;
  217. time : longint;
  218. sympos : longint;
  219. syms : longint;
  220. opthdr : word;
  221. flag : word;
  222. end;
  223. tcoffsechdr=packed record
  224. name : array[0..7] of char;
  225. vsize : longint;
  226. rvaofs : longint;
  227. datalen : longint;
  228. datapos : longint;
  229. relocpos : longint;
  230. lineno1 : longint;
  231. nrelocs : word;
  232. lineno2 : word;
  233. flags : longint;
  234. end;
  235. psecfill=^tsecfill;
  236. tsecfill=record
  237. fillpos,
  238. fillsize : longint;
  239. next : psecfill;
  240. end;
  241. var
  242. f : file;
  243. coffheader : tcoffheader;
  244. firstsecpos,
  245. maxfillsize,
  246. l : longint;
  247. coffsec : tcoffsechdr;
  248. secroot,hsecroot : psecfill;
  249. zerobuf : pointer;
  250. begin
  251. { when -s is used quit, because there is no .exe }
  252. if cs_link_extern in aktglobalswitches then
  253. exit;
  254. { open file }
  255. assign(f,n);
  256. {$I-}
  257. reset(f,1);
  258. if ioresult<>0 then
  259. Message1(execinfo_f_cant_open_executable,n);
  260. { read headers }
  261. seek(f,2048);
  262. blockread(f,coffheader,sizeof(tcoffheader));
  263. { read section info }
  264. maxfillsize:=0;
  265. firstsecpos:=0;
  266. secroot:=nil;
  267. for l:=1to coffheader.nSects do
  268. begin
  269. blockread(f,coffsec,sizeof(tcoffsechdr));
  270. if coffsec.datapos>0 then
  271. begin
  272. if secroot=nil then
  273. firstsecpos:=coffsec.datapos;
  274. new(hsecroot);
  275. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  276. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  277. hsecroot^.next:=secroot;
  278. secroot:=hsecroot;
  279. if secroot^.fillsize>maxfillsize then
  280. maxfillsize:=secroot^.fillsize;
  281. end;
  282. end;
  283. if firstsecpos>0 then
  284. begin
  285. l:=firstsecpos-filepos(f);
  286. if l>maxfillsize then
  287. maxfillsize:=l;
  288. end
  289. else
  290. l:=0;
  291. { get zero buffer }
  292. getmem(zerobuf,maxfillsize);
  293. fillchar(zerobuf^,maxfillsize,0);
  294. { zero from sectioninfo until first section }
  295. blockwrite(f,zerobuf^,l);
  296. { zero section alignments }
  297. while assigned(secroot) do
  298. begin
  299. seek(f,secroot^.fillpos);
  300. blockwrite(f,zerobuf^,secroot^.fillsize);
  301. hsecroot:=secroot;
  302. secroot:=secroot^.next;
  303. dispose(hsecroot);
  304. end;
  305. freemem(zerobuf,maxfillsize);
  306. close(f);
  307. {$I+}
  308. i:=ioresult;
  309. postprocessexecutable:=true;
  310. end;
  311. {$endif}
  312. {*****************************************************************************
  313. Initialize
  314. *****************************************************************************}
  315. const
  316. target_i386_go32v2_info : ttargetinfo =
  317. (
  318. target : target_i386_GO32V2;
  319. name : 'GO32 V2 DOS extender';
  320. shortname : 'Go32v2';
  321. flags : [];
  322. cpu : i386;
  323. unit_env : 'GO32V2UNITS';
  324. extradefines : 'DPMI';
  325. sourceext : '.pp';
  326. pasext : '.pas';
  327. exeext : '.exe';
  328. defext : '.def';
  329. scriptext : '.bat';
  330. smartext : '.sl';
  331. unitext : '.ppu';
  332. unitlibext : '.ppl';
  333. asmext : '.s';
  334. objext : '.o';
  335. resext : '.res';
  336. resobjext : '.or';
  337. sharedlibext : '.dll';
  338. staticlibext : '.a';
  339. staticlibprefix : '';
  340. sharedlibprefix : '';
  341. sharedClibext : '.dll';
  342. staticClibext : '.a';
  343. staticClibprefix : '';
  344. sharedClibprefix : '';
  345. Cprefix : '_';
  346. newline : #13#10;
  347. dirsep : '\';
  348. files_case_relevent : false;
  349. assem : as_i386_coff;
  350. assemextern : as_i386_as;
  351. link : ld_i386_go32v2;
  352. linkextern : ld_i386_go32v2;
  353. ar : ar_gnu_ar;
  354. res : res_none;
  355. script : script_dos;
  356. endian : endian_little;
  357. alignment :
  358. (
  359. procalign : 4;
  360. loopalign : 4;
  361. jumpalign : 0;
  362. constalignmin : 0;
  363. constalignmax : 4;
  364. varalignmin : 0;
  365. varalignmax : 4;
  366. localalignmin : 0;
  367. localalignmax : 4;
  368. paraalign : 2;
  369. recordalignmin : 0;
  370. recordalignmax : 2;
  371. maxCrecordalign : 4
  372. );
  373. size_of_pointer : 4;
  374. size_of_longint : 4;
  375. heapsize : 2048*1024;
  376. maxheapsize : 32768*1024;
  377. stacksize : 16384;
  378. DllScanSupported : false;
  379. use_bound_instruction : false;
  380. use_function_relative_addresses : true
  381. );
  382. initialization
  383. RegisterLinker(ld_i386_go32v2,TLinkerGo32v2);
  384. RegisterTarget(target_i386_go32v2_info);
  385. end.
  386. {
  387. $Log$
  388. Revision 1.15 2002-01-29 21:27:34 peter
  389. * default alignment changed to 4 bytes for locals and static const,var
  390. Revision 1.14 2001/09/18 11:32:00 michael
  391. * Fixes win32 linking problems with import libraries
  392. * LINKLIB Libraries are now looked for using C file extensions
  393. * get_exepath fix
  394. Revision 1.13 2001/09/17 21:29:16 peter
  395. * merged netbsd, fpu-overflow from fixes branch
  396. Revision 1.12 2001/08/30 20:08:23 peter
  397. * create script.res and use link.res for commandline
  398. Revision 1.11 2001/08/19 11:22:24 peter
  399. * palmos support from v10 merged
  400. Revision 1.10 2001/08/07 18:47:15 peter
  401. * merged netbsd start
  402. * profile for win32
  403. Revision 1.9 2001/07/10 21:01:35 peter
  404. * fixed crash with writing of the linker script
  405. Revision 1.8 2001/07/01 20:16:20 peter
  406. * alignmentinfo record added
  407. * -Oa argument supports more alignment settings that can be specified
  408. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  409. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  410. required alignment and the maximum usefull alignment. The final
  411. alignment will be choosen per variable size dependent on these
  412. settings
  413. Revision 1.7 2001/06/28 19:46:25 peter
  414. * added override and virtual for constructors
  415. Revision 1.6 2001/06/18 20:36:26 peter
  416. * -Ur switch (merged)
  417. * masm fixes (merged)
  418. * quoted filenames for go32v2 and win32
  419. Revision 1.5 2001/06/03 15:15:31 peter
  420. * dllprt0 stub for linux shared libs
  421. * pass -init and -fini for linux shared libs
  422. * libprefix splitted into staticlibprefix and sharedlibprefix
  423. Revision 1.4 2001/06/02 19:22:44 peter
  424. * extradefines field added
  425. Revision 1.3 2001/04/18 22:02:04 peter
  426. * registration of targets and assemblers
  427. Revision 1.2 2001/04/13 01:22:21 peter
  428. * symtable change to classes
  429. * range check generation and errors fixed, make cycle DEBUG=1 works
  430. * memory leaks fixed
  431. Revision 1.1 2001/02/26 19:43:11 peter
  432. * moved target units to subdir
  433. Revision 1.7 2001/01/27 21:29:35 florian
  434. * behavior -Oa optimized
  435. Revision 1.6 2000/12/25 00:07:30 peter
  436. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  437. tlinkedlist objects)
  438. Revision 1.5 2000/09/24 15:06:31 peter
  439. * use defines.inc
  440. Revision 1.4 2000/08/27 16:11:54 peter
  441. * moved some util functions from globals,cobjects to cutils
  442. * splitted files into finput,fmodule
  443. Revision 1.3 2000/08/16 13:06:07 florian
  444. + support of 64 bit integer constants
  445. Revision 1.2 2000/07/13 11:32:50 michael
  446. + removed logs
  447. }