t_os2.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
  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_os2;
  26. {$i fpcdefs.inc}
  27. interface
  28. implementation
  29. uses
  30. SysUtils,
  31. cutils,cfileutl,cclasses,
  32. globtype,systems,symconst,symdef,
  33. globals,verbose,fmodule,script,
  34. import,link,i_os2,ogbase;
  35. type
  36. timportlibos2=class(timportlib)
  37. procedure generatelib;override;
  38. end;
  39. tlinkeros2=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;
  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(200504245);
  165. if aout_sym_count>=sizeof(aout_sym_tab) div sizeof(aout_sym_tab[0]) then
  166. internalerror(200504246);
  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(200504247);
  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(200504248);
  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: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. (*
  234. var tmp1,tmp2,tmp3:string;
  235. *)
  236. var tmp1,tmp3:string;
  237. sym_mcount,sym_import:longint;
  238. fixup_mcount,fixup_import:longint;
  239. begin
  240. aout_init;
  241. (*
  242. tmp2:=func;
  243. if profile_flag and not (copy(func,1,4)='_16_') then
  244. *)
  245. if profile_flag and not (copy(Name,1,4)='_16_') then
  246. begin
  247. {sym_entry:=aout_sym(func,n_text+n_ext,0,0,aout_text_size);}
  248. sym_mcount:=aout_sym('__mcount',n_ext,0,0,0);
  249. {Use, say, "_$U_DosRead" for "DosRead" to import the
  250. non-profiled function.}
  251. (*
  252. tmp2:='__$U_'+func;
  253. sym_import:=aout_sym(tmp2,n_ext,0,0,0);
  254. *)
  255. sym_import:=aout_sym(name,n_ext,0,0,0);
  256. aout_text_byte($55); {push ebp}
  257. aout_text_byte($89); {mov ebp, esp}
  258. aout_text_byte($e5);
  259. aout_text_byte($e8); {call _mcount}
  260. fixup_mcount:=aout_text_size;
  261. aout_text_dword(0-(aout_text_size+4));
  262. aout_text_byte($5d); {pop ebp}
  263. aout_text_byte($e9); {jmp _$U_DosRead}
  264. fixup_import:=aout_text_size;
  265. aout_text_dword(0-(aout_text_size+4));
  266. aout_treloc(fixup_mcount,sym_mcount,1,2,1);
  267. aout_treloc (fixup_import, sym_import,1,2,1);
  268. end;
  269. str(seq_no,tmp1);
  270. tmp1:='IMPORT#'+tmp1;
  271. (*
  272. if name='' then
  273. *)
  274. if index<>0 then
  275. begin
  276. str(index,tmp3);
  277. (*
  278. tmp3:=func+'='+module+'.'+tmp3;
  279. *)
  280. tmp3:=Name+'='+module+'.'+tmp3;
  281. end
  282. else
  283. tmp3:=Name+'='+module+'.'+name;
  284. (*
  285. tmp3:=func+'='+module+'.'+name;
  286. aout_sym(tmp2,n_imp1+n_ext,0,0,0);
  287. *)
  288. aout_sym(Name,n_imp1+n_ext,0,0,0);
  289. aout_sym(tmp3,n_imp2+n_ext,0,0,0);
  290. aout_finish;
  291. write_ar(tmp1,aout_size);
  292. aout_write;
  293. finish_ar;
  294. inc(seq_no);
  295. end;
  296. procedure timportlibos2.generatelib;
  297. const
  298. ar_magic:array[1..8] of char='!<arch>'#10;
  299. var
  300. libname : string;
  301. i,j : longint;
  302. ImportLibrary : TImportLibrary;
  303. ImportSymbol : TImportSymbol;
  304. begin
  305. LibName:=FixFileName(Current_Module.RealModuleName^ + Target_Info.StaticCLibExt);
  306. seq_no:=1;
  307. current_module.linkotherstaticlibs.add(libname,link_always);
  308. assign(out_file,current_module.outputpath^+libname);
  309. rewrite(out_file,1);
  310. blockwrite(out_file,ar_magic,sizeof(ar_magic));
  311. for i:=0 to current_module.ImportLibraryList.Count-1 do
  312. begin
  313. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  314. { LibName:=FixFileName(ImportLibrary.Name + Target_Info.StaticCLibExt);}
  315. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  316. begin
  317. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  318. AddImport(ChangeFileExt(ExtractFileName(ImportLibrary.Name),''),ImportSymbol.OrdNr,ImportSymbol.Name);
  319. end;
  320. end;
  321. close(out_file);
  322. end;
  323. {****************************************************************************
  324. TLinkeros2
  325. ****************************************************************************}
  326. Constructor TLinkeros2.Create;
  327. begin
  328. Inherited Create;
  329. { allow duplicated libs (PM) }
  330. SharedLibFiles.doubles:=true;
  331. StaticLibFiles.doubles:=true;
  332. end;
  333. procedure TLinkeros2.SetDefaultInfo;
  334. begin
  335. with Info do
  336. begin
  337. ExeCmd[1]:='ld $OPT -o $OUT @$RES';
  338. ExeCmd[2]:='emxbind -b $STRIP $APPTYPE $RSRC -k$STACKKB -h1 -o $EXE $OUT -ai -s8';
  339. if Source_Info.Script = script_dos then
  340. ExeCmd[3]:='del $OUT';
  341. end;
  342. end;
  343. Function TLinkeros2.WriteResponseFile(isdll:boolean) : Boolean;
  344. Var
  345. linkres : TLinkRes;
  346. i : longint;
  347. HPath : TCmdStrListItem;
  348. s : string;
  349. begin
  350. WriteResponseFile:=False;
  351. { Open link.res file }
  352. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  353. { Write path to search libraries }
  354. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  355. while assigned(HPath) do
  356. begin
  357. LinkRes.Add('-L'+HPath.Str);
  358. HPath:=TCmdStrListItem(HPath.Next);
  359. end;
  360. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  361. while assigned(HPath) do
  362. begin
  363. LinkRes.Add('-L'+HPath.Str);
  364. HPath:=TCmdStrListItem(HPath.Next);
  365. end;
  366. { add objectfiles, start with prt0 always }
  367. LinkRes.AddFileName(FindObjectFile('prt0','',false));
  368. while not ObjectFiles.Empty do
  369. begin
  370. s:=ObjectFiles.GetFirst;
  371. if s<>'' then
  372. LinkRes.AddFileName(s);
  373. end;
  374. { Write staticlibraries }
  375. { No group !! This will not work correctly PM }
  376. While not StaticLibFiles.Empty do
  377. begin
  378. S:=StaticLibFiles.GetFirst;
  379. LinkRes.AddFileName(s)
  380. end;
  381. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  382. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  383. While not SharedLibFiles.Empty do
  384. begin
  385. S:=SharedLibFiles.GetFirst;
  386. i:=Pos(target_info.sharedlibext,S);
  387. if i>0 then
  388. Delete(S,i,255);
  389. LinkRes.Add('-l'+s);
  390. end;
  391. { Write and Close response }
  392. linkres.writetodisk;
  393. LinkRes.Free;
  394. WriteResponseFile:=True;
  395. end;
  396. function TLinkeros2.MakeExecutable:boolean;
  397. var
  398. binstr,
  399. cmdstr : TCmdStr;
  400. success : boolean;
  401. i : longint;
  402. AppTypeStr,
  403. StripStr: string[40];
  404. RsrcStr : string;
  405. OutName: TPathStr;
  406. begin
  407. if not(cs_link_nolink in current_settings.globalswitches) then
  408. Message1(exec_i_linking,current_module.exefilename^);
  409. { Create some replacements }
  410. OutName := ChangeFileExt(current_module.exefilename^,'.out');
  411. if (cs_link_strip in current_settings.globalswitches) then
  412. StripStr := '-s'
  413. else
  414. StripStr := '';
  415. if (usewindowapi) or (AppType = app_gui) then
  416. AppTypeStr := '-p'
  417. else if AppType = app_fs then
  418. AppTypeStr := '-f'
  419. else AppTypeStr := '-w';
  420. if not (Current_module.ResourceFiles.Empty) then
  421. RsrcStr := '-r ' + Current_module.ResourceFiles.GetFirst
  422. else
  423. RsrcStr := '';
  424. (* Only one resource file supported, discard everything else
  425. (should be already empty anyway, though). *)
  426. Current_module.ResourceFiles.Clear;
  427. { Write used files and libraries }
  428. WriteResponseFile(false);
  429. { Call linker }
  430. success:=false;
  431. for i:=1 to 3 do
  432. begin
  433. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  434. if binstr<>'' then
  435. begin
  436. { Is this really required? Not anymore according to my EMX docs }
  437. Replace(cmdstr,'$HEAPMB',tostr((1048575) shr 20));
  438. {Size of the stack when an EMX program runs in OS/2.}
  439. Replace(cmdstr,'$STACKKB',tostr((stacksize+1023) shr 10));
  440. {When an EMX program runs in DOS, the heap and stack share the
  441. same memory pool. The heap grows upwards, the stack grows downwards.}
  442. Replace(cmdstr,'$DOSHEAPKB',tostr((stacksize+1023) shr 10));
  443. Replace(cmdstr,'$STRIP',StripStr);
  444. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  445. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  446. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  447. Replace(cmdstr,'$RSRC',RsrcStr);
  448. Replace(cmdstr,'$OUT',maybequoted(OutName));
  449. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  450. if i<>3 then
  451. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false)
  452. else
  453. success:=DoExec(binstr,cmdstr,(i=1),true);
  454. end;
  455. end;
  456. { Remove ReponseFile }
  457. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  458. DeleteFile(outputexedir+Info.ResName);
  459. MakeExecutable:=success; { otherwise a recursive call to link method }
  460. end;
  461. {*****************************************************************************
  462. Initialize
  463. *****************************************************************************}
  464. initialization
  465. RegisterExternalLinker(system_i386_os2_info,TLinkerOS2);
  466. RegisterImport(system_i386_os2,TImportLibOS2);
  467. { RegisterRes(res_emxbind_info);}
  468. RegisterTarget(system_i386_os2_info);
  469. end.