t_os2.pas 16 KB

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