t_emx.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. {
  2. Copyright (c) 1998-2002 by Daniel Mantione
  3. Portions Copyright (c) 1998-2002 Eberhard Mattes
  4. Unit to write out import libraries and def files for OS/2 via EMX
  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. {
  19. A lot of code in this unit has been ported from C to Pascal from the
  20. emximp utility, part of the EMX development system. Emximp is copyrighted
  21. by Eberhard Mattes. Note: Eberhard doesn't know much about the Pascal
  22. port, please send questions to Tomas Hajny <[email protected]> or
  23. Daniel Mantione <[email protected]>.
  24. }
  25. unit t_emx;
  26. {$i fpcdefs.inc}
  27. interface
  28. implementation
  29. uses
  30. SysUtils,
  31. cutils,cfileutl,cclasses,
  32. globtype,comphook,systems,symconst,symsym,symdef,
  33. globals,verbose,fmodule,cscript,ogbase,
  34. comprsrc,import,link,i_emx,ppu;
  35. type
  36. TImportLibEMX=class(timportlib)
  37. procedure generatelib;override;
  38. end;
  39. TLinkerEMX=class(texternallinker)
  40. private
  41. Function WriteResponseFile(isdll:boolean) : Boolean;
  42. public
  43. constructor Create;override;
  44. procedure SetDefaultInfo;override;
  45. function MakeExecutable:boolean;override;
  46. end;
  47. const profile_flag:boolean=false;
  48. const n_ext = 1;
  49. n_abs = 2;
  50. n_text = 4;
  51. n_data = 6;
  52. n_bss = 8;
  53. n_imp1 = $68;
  54. n_imp2 = $6a;
  55. type reloc=packed record {This is the layout of a relocation table
  56. entry.}
  57. address:longint; {Fixup location}
  58. remaining:longint;
  59. {Meaning of bits for remaining:
  60. 0..23: Symbol number or segment
  61. 24: Self-relative fixup if non-zero
  62. 25..26: Fixup size (0: 1 byte, 1: 2, 2: 4 bytes)
  63. 27: Reference to symbol or segment
  64. 28..31 Not used}
  65. end;
  66. nlist=packed record {This is the layout of a symbol table entry.}
  67. strofs:longint; {Offset in string table}
  68. typ:byte; {Type of the symbol}
  69. other:byte; {Other information}
  70. desc:word; {More information}
  71. value:longint; {Value (address)}
  72. end;
  73. a_out_header=packed record
  74. magic:word; {Magic word, must be $0107}
  75. machtype:byte; {Machine type}
  76. flags:byte; {Flags}
  77. text_size:longint; {Length of text, in bytes}
  78. data_size:longint; {Length of initialized data, in bytes}
  79. bss_size:longint; {Length of uninitialized data, in bytes}
  80. sym_size:longint; {Length of symbol table, in bytes}
  81. entry:longint; {Start address (entry point)}
  82. trsize:longint; {Length of relocation info for text, bytes}
  83. drsize:longint; {Length of relocation info for data, bytes}
  84. end;
  85. ar_hdr=packed record
  86. ar_name:array[0..15] of char;
  87. ar_date:array[0..11] of char;
  88. ar_uid:array[0..5] of char;
  89. ar_gid:array[0..5] of char;
  90. ar_mode:array[0..7] of char;
  91. ar_size:array[0..9] of char;
  92. ar_fmag:array[0..1] of char;
  93. end;
  94. var aout_str_size:longint;
  95. aout_str_tab:array[0..2047] of char;
  96. aout_sym_count:longint;
  97. aout_sym_tab:array[0..5] of nlist;
  98. aout_text:array[0..63] of byte;
  99. aout_text_size:longint;
  100. aout_treloc_tab:array[0..1] of reloc;
  101. aout_treloc_count:longint;
  102. aout_size:longint;
  103. seq_no:longint;
  104. ar_member_size:longint;
  105. out_file:file;
  106. procedure PackTime (var T: TSystemTime; var P: longint);
  107. var zs:longint;
  108. begin
  109. p:=-1980;
  110. p:=p+t.year and 127;
  111. p:=p shl 4;
  112. p:=p+t.month;
  113. p:=p shl 5;
  114. p:=p+t.day;
  115. p:=p shl 16;
  116. zs:=t.hour;
  117. zs:=zs shl 6;
  118. zs:=zs+t.minute;
  119. zs:=zs shl 5;
  120. zs:=zs+t.second div 2;
  121. p:=p+(zs and $ffff);
  122. end;
  123. procedure write_ar(const name:string;size:longint);
  124. var ar:ar_hdr; {PackTime is platform independent}
  125. time:TSystemTime;
  126. numtime:longint;
  127. tmp:string[19];
  128. begin
  129. ar_member_size:=size;
  130. fillchar(ar.ar_name,sizeof(ar.ar_name),' ');
  131. move(name[1],ar.ar_name,length(name));
  132. GetLocalTime(time);
  133. packtime(time,numtime);
  134. str(numtime,tmp);
  135. fillchar(ar.ar_date,sizeof(ar.ar_date),' ');
  136. move(tmp[1],ar.ar_date,length(tmp));
  137. ar.ar_uid:='0 ';
  138. ar.ar_gid:='0 ';
  139. ar.ar_mode:='100666'#0#0;
  140. str(size,tmp);
  141. fillchar(ar.ar_size,sizeof(ar.ar_size),' ');
  142. move(tmp[1],ar.ar_size,length(tmp));
  143. ar.ar_fmag:='`'#10;
  144. blockwrite(out_file,ar,sizeof(ar));
  145. end;
  146. procedure finish_ar;
  147. var a:byte;
  148. begin
  149. a:=0;
  150. if odd(ar_member_size) then
  151. blockwrite(out_file,a,1);
  152. end;
  153. procedure aout_init;
  154. begin
  155. aout_str_size:=sizeof(longint);
  156. aout_sym_count:=0;
  157. aout_text_size:=0;
  158. aout_treloc_count:=0;
  159. end;
  160. function aout_sym(const name:string;typ,other:byte;desc:word;
  161. value:longint):longint;
  162. begin
  163. if aout_str_size+length(name)+1>sizeof(aout_str_tab) then
  164. internalerror(200504241);
  165. if aout_sym_count>=sizeof(aout_sym_tab) div sizeof(aout_sym_tab[0]) then
  166. internalerror(200504242);
  167. aout_sym_tab[aout_sym_count].strofs:=aout_str_size;
  168. aout_sym_tab[aout_sym_count].typ:=typ;
  169. aout_sym_tab[aout_sym_count].other:=other;
  170. aout_sym_tab[aout_sym_count].desc:=desc;
  171. aout_sym_tab[aout_sym_count].value:=value;
  172. strPcopy(@aout_str_tab[aout_str_size],name);
  173. aout_str_size:=aout_str_size+length(name)+1;
  174. aout_sym:=aout_sym_count;
  175. inc(aout_sym_count);
  176. end;
  177. procedure aout_text_byte(b:byte);
  178. begin
  179. if aout_text_size>=sizeof(aout_text) then
  180. internalerror(200504243);
  181. aout_text[aout_text_size]:=b;
  182. inc(aout_text_size);
  183. end;
  184. procedure aout_text_dword(d:longint);
  185. type li_ar=array[0..3] of byte;
  186. begin
  187. aout_text_byte(li_ar(d)[0]);
  188. aout_text_byte(li_ar(d)[1]);
  189. aout_text_byte(li_ar(d)[2]);
  190. aout_text_byte(li_ar(d)[3]);
  191. end;
  192. procedure aout_treloc(address,symbolnum,pcrel,len,ext:longint);
  193. begin
  194. if aout_treloc_count>=sizeof(aout_treloc_tab) div sizeof(reloc) then
  195. internalerror(200504244);
  196. aout_treloc_tab[aout_treloc_count].address:=address;
  197. aout_treloc_tab[aout_treloc_count].remaining:=symbolnum+pcrel shl 24+
  198. len shl 25+ext shl 27;
  199. inc(aout_treloc_count);
  200. end;
  201. procedure aout_finish;
  202. begin
  203. while (aout_text_size and 3)<>0 do
  204. aout_text_byte ($90);
  205. aout_size:=sizeof(a_out_header)+aout_text_size+aout_treloc_count*
  206. sizeof(reloc)+aout_sym_count*sizeof(aout_sym_tab[0])+aout_str_size;
  207. end;
  208. procedure aout_write;
  209. var ao:a_out_header;
  210. begin
  211. ao.magic:=$0107;
  212. ao.machtype:=0;
  213. ao.flags:=0;
  214. ao.text_size:=aout_text_size;
  215. ao.data_size:=0;
  216. ao.bss_size:=0;
  217. ao.sym_size:=aout_sym_count*sizeof(aout_sym_tab[0]);
  218. ao.entry:=0;
  219. ao.trsize:=aout_treloc_count*sizeof(reloc);
  220. ao.drsize:=0;
  221. blockwrite(out_file,ao,sizeof(ao));
  222. blockwrite(out_file,aout_text,aout_text_size);
  223. blockwrite(out_file,aout_treloc_tab,sizeof(reloc)*aout_treloc_count);
  224. blockwrite(out_file,aout_sym_tab,sizeof(aout_sym_tab[0])*aout_sym_count);
  225. plongint(@aout_str_tab)^:=aout_str_size;
  226. blockwrite(out_file,aout_str_tab,aout_str_size);
  227. end;
  228. procedure AddImport(const module:string;index:longint;const name,mangledname:string);
  229. {func = Name of function to import.
  230. module = Name of DLL to import from.
  231. index = Index of function in DLL. Use 0 to import by name.
  232. name = Name of function in DLL. Ignored when index=0;}
  233. var tmp1,tmp2,tmp3:string;
  234. sym_mcount,sym_import:longint;
  235. fixup_mcount,fixup_import:longint;
  236. func : string;
  237. begin
  238. aout_init;
  239. func:=mangledname;
  240. tmp2:=func;
  241. if profile_flag and not (copy(func,1,4)='_16_') then
  242. begin
  243. {sym_entry:=aout_sym(func,n_text+n_ext,0,0,aout_text_size);}
  244. sym_mcount:=aout_sym('__mcount',n_ext,0,0,0);
  245. {Use, say, "_$U_DosRead" for "DosRead" to import the
  246. non-profiled function.}
  247. tmp2:='__$U_'+func;
  248. sym_import:=aout_sym(tmp2,n_ext,0,0,0);
  249. aout_text_byte($55); {push ebp}
  250. aout_text_byte($89); {mov ebp, esp}
  251. aout_text_byte($e5);
  252. aout_text_byte($e8); {call _mcount}
  253. fixup_mcount:=aout_text_size;
  254. aout_text_dword(0-(aout_text_size+4));
  255. aout_text_byte($5d); {pop ebp}
  256. aout_text_byte($e9); {jmp _$U_DosRead}
  257. fixup_import:=aout_text_size;
  258. aout_text_dword(0-(aout_text_size+4));
  259. aout_treloc(fixup_mcount,sym_mcount,1,2,1);
  260. aout_treloc (fixup_import, sym_import,1,2,1);
  261. end;
  262. str(seq_no,tmp1);
  263. tmp1:='IMPORT#'+tmp1;
  264. if name='' then
  265. begin
  266. str(index,tmp3);
  267. tmp3:=func+'='+module+'.'+tmp3;
  268. end
  269. else
  270. tmp3:=func+'='+module+'.'+name;
  271. aout_sym(tmp2,n_imp1+n_ext,0,0,0);
  272. aout_sym(tmp3,n_imp2+n_ext,0,0,0);
  273. aout_finish;
  274. write_ar(tmp1,aout_size);
  275. aout_write;
  276. finish_ar;
  277. inc(seq_no);
  278. end;
  279. procedure TImportLibEMX.GenerateLib;
  280. const
  281. ar_magic:array[1..8] of char='!<arch>'#10;
  282. var
  283. libname : string;
  284. i,j : longint;
  285. ImportLibrary : TImportLibrary;
  286. ImportSymbol : TImportSymbol;
  287. begin
  288. for i:=0 to current_module.ImportLibraryList.Count-1 do
  289. begin
  290. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  291. LibName:=FixFileName(ImportLibrary.Name + Target_Info.StaticCLibExt);
  292. seq_no:=1;
  293. current_module.linkotherstaticlibs.add(libname,link_always);
  294. assign(out_file,current_module.outputpath+libname);
  295. rewrite(out_file,1);
  296. blockwrite(out_file,ar_magic,sizeof(ar_magic));
  297. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  298. begin
  299. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  300. AddImport(ImportLibrary.Name,ImportSymbol.OrdNr,
  301. ImportSymbol.Name,ImportSymbol.MangledName);
  302. end;
  303. close(out_file);
  304. end;
  305. end;
  306. {****************************************************************************
  307. TLinkerEMX
  308. ****************************************************************************}
  309. Constructor TLinkerEMX.Create;
  310. begin
  311. Inherited Create;
  312. { allow duplicated libs (PM) }
  313. SharedLibFiles.doubles:=true;
  314. StaticLibFiles.doubles:=true;
  315. end;
  316. procedure TLinkerEMX.SetDefaultInfo;
  317. begin
  318. with Info do
  319. begin
  320. ExeCmd[1]:='ld $OPT -o $OUT @$RES';
  321. ExeCmd[2]:='emxbind -b $STRIP $MAP $APPTYPE $RSRC -k$STACKKB -h$HEAPMB -o $EXE $OUT -aim -s$DOSHEAPKB';
  322. if Source_Info.Script = script_dos then
  323. ExeCmd[3]:='del $OUT';
  324. end;
  325. end;
  326. Function TLinkerEMX.WriteResponseFile(isdll:boolean) : Boolean;
  327. Var
  328. linkres : TLinkRes;
  329. i : longint;
  330. HPath : TCmdStrListItem;
  331. s : string;
  332. begin
  333. WriteResponseFile:=False;
  334. { Open link.res file }
  335. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  336. { Write path to search libraries }
  337. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  338. while assigned(HPath) do
  339. begin
  340. LinkRes.Add('-L'+HPath.Str);
  341. HPath:=TCmdStrListItem(HPath.Next);
  342. end;
  343. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  344. while assigned(HPath) do
  345. begin
  346. LinkRes.Add('-L'+HPath.Str);
  347. HPath:=TCmdStrListItem(HPath.Next);
  348. end;
  349. { add objectfiles, start with prt0 always }
  350. LinkRes.AddFileName(FindObjectFile('prt0','',false));
  351. while not ObjectFiles.Empty do
  352. begin
  353. s:=ObjectFiles.GetFirst;
  354. if s<>'' then
  355. LinkRes.AddFileName(s);
  356. end;
  357. { Write staticlibraries }
  358. { No group !! This will not work correctly PM }
  359. While not StaticLibFiles.Empty do
  360. begin
  361. S:=StaticLibFiles.GetFirst;
  362. LinkRes.AddFileName(s)
  363. end;
  364. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  365. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  366. While not SharedLibFiles.Empty do
  367. begin
  368. S:=SharedLibFiles.GetFirst;
  369. i:=Pos(target_info.sharedlibext,S);
  370. if i>0 then
  371. Delete(S,i,255);
  372. LinkRes.Add('-l'+s);
  373. end;
  374. { Write and Close response }
  375. linkres.writetodisk;
  376. LinkRes.Free;
  377. WriteResponseFile:=True;
  378. end;
  379. function TLinkerEMX.MakeExecutable:boolean;
  380. var
  381. binstr,
  382. cmdstr : TCmdStr;
  383. success : boolean;
  384. i : longint;
  385. AppTypeStr,
  386. StripStr: string[3];
  387. MapStr: shortstring;
  388. BaseFilename: TPathStr;
  389. RsrcStr : string;
  390. OutName: TPathStr;
  391. begin
  392. if not(cs_link_nolink in current_settings.globalswitches) then
  393. Message1(exec_i_linking,current_module.exefilename);
  394. { Create some replacements }
  395. BaseFilename := ChangeFileExt(current_module.exefilename,'');
  396. OutName := BaseFilename + '.out';
  397. if (cs_link_strip in current_settings.globalswitches) then
  398. StripStr := '-s '
  399. else
  400. StripStr := '';
  401. if (cs_link_map in current_settings.globalswitches) then
  402. MapStr := '-m' + BaseFileName + ' '
  403. else
  404. MapStr := '';
  405. if (usewindowapi) or (AppType = app_gui) then
  406. AppTypeStr := '-p'
  407. else if AppType = app_fs then
  408. AppTypeStr := '-f'
  409. else AppTypeStr := '-w';
  410. if not (Current_module.ResourceFiles.Empty) then
  411. RsrcStr := '-r ' + Current_module.ResourceFiles.GetFirst + ' '
  412. else
  413. RsrcStr := '';
  414. (* Only one resource file supported, discard everything else
  415. (should be already empty anyway, though). *)
  416. Current_module.ResourceFiles.Clear;
  417. { Write used files and libraries }
  418. WriteResponseFile(false);
  419. { Call linker }
  420. success:=false;
  421. for i:=1 to 3 do
  422. begin
  423. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  424. if binstr<>'' then
  425. begin
  426. { Is this really required? Not anymore according to my EMX docs }
  427. Replace(cmdstr,'$HEAPMB',tostr((1048575) shr 20));
  428. {Size of the stack when an EMX program runs in OS/2.}
  429. Replace(cmdstr,'$STACKKB',tostr((stacksize+1023) shr 10));
  430. {When an EMX program runs in DOS, the heap and stack share the
  431. same memory pool. The heap grows upwards, the stack grows downwards.}
  432. Replace(cmdstr,'$DOSHEAPKB',tostr((stacksize+1023) shr 10));
  433. Replace(cmdstr,'$STRIP ', StripStr);
  434. Replace(cmdstr,'$MAP ', MapStr);
  435. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  436. (*
  437. Arrgh!!! The ancient EMX LD.EXE simply dies without saying anything
  438. if the full pathname to link.res is quoted!!!!! @#$@@^%@#$^@#$^@^#$
  439. This means that name of the output directory cannot contain spaces,
  440. but at least it works otherwise...
  441. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  442. *)
  443. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  444. Replace(cmdstr,'$OPT ',Info.ExtraOptions);
  445. Replace(cmdstr,'$RSRC ',RsrcStr);
  446. Replace(cmdstr,'$OUT',maybequoted(OutName));
  447. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  448. if i<>3 then
  449. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false)
  450. else
  451. success:=DoExec(binstr,cmdstr,(i=1),true);
  452. end;
  453. end;
  454. { Remove ReponseFile }
  455. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  456. DeleteFile(outputexedir+Info.ResName);
  457. MakeExecutable:=success; { otherwise a recursive call to link method }
  458. end;
  459. {*****************************************************************************
  460. Initialize
  461. *****************************************************************************}
  462. initialization
  463. RegisterLinker(ld_emx,TLinkerEMX);
  464. RegisterImport(system_i386_emx,TImportLibEMX);
  465. RegisterRes(res_wrc_os2_info,TResourceFile);
  466. RegisterTarget(system_i386_emx_info);
  467. end.