t_os2.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. if Source_Info.Script = script_dos then
  306. ExeCmd[3]:='del $OUT';
  307. end;
  308. end;
  309. Function TLinkeros2.WriteResponseFile(isdll:boolean) : Boolean;
  310. Var
  311. linkres : TLinkRes;
  312. i : longint;
  313. HPath : TStringListItem;
  314. s : string;
  315. begin
  316. WriteResponseFile:=False;
  317. { Open link.res file }
  318. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  319. { Write path to search libraries }
  320. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  321. while assigned(HPath) do
  322. begin
  323. LinkRes.Add('-L'+HPath.Str);
  324. HPath:=TStringListItem(HPath.Next);
  325. end;
  326. HPath:=TStringListItem(LibrarySearchPath.First);
  327. while assigned(HPath) do
  328. begin
  329. LinkRes.Add('-L'+HPath.Str);
  330. HPath:=TStringListItem(HPath.Next);
  331. end;
  332. { add objectfiles, start with prt0 always }
  333. LinkRes.AddFileName(FindObjectFile('prt0','',false));
  334. while not ObjectFiles.Empty do
  335. begin
  336. s:=ObjectFiles.GetFirst;
  337. if s<>'' then
  338. LinkRes.AddFileName(s);
  339. end;
  340. { Write staticlibraries }
  341. { No group !! This will not work correctly PM }
  342. While not StaticLibFiles.Empty do
  343. begin
  344. S:=StaticLibFiles.GetFirst;
  345. LinkRes.AddFileName(s)
  346. end;
  347. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  348. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  349. While not SharedLibFiles.Empty do
  350. begin
  351. S:=SharedLibFiles.GetFirst;
  352. i:=Pos(target_info.sharedlibext,S);
  353. if i>0 then
  354. Delete(S,i,255);
  355. LinkRes.Add('-l'+s);
  356. end;
  357. { Write and Close response }
  358. linkres.writetodisk;
  359. LinkRes.Free;
  360. WriteResponseFile:=True;
  361. end;
  362. function TLinkeros2.MakeExecutable:boolean;
  363. var
  364. binstr : String;
  365. cmdstr : TCmdStr;
  366. success : boolean;
  367. i : longint;
  368. AppTypeStr,
  369. StripStr: string[40];
  370. RsrcStr : string;
  371. DS: DirStr;
  372. NS: NameStr;
  373. ES: ExtStr;
  374. OutName: PathStr;
  375. begin
  376. if not(cs_link_extern in aktglobalswitches) then
  377. Message1(exec_i_linking,current_module.exefilename^);
  378. { Create some replacements }
  379. FSplit (current_module.exefilename^, DS, NS, ES);
  380. OutName := DS + NS + '.out';
  381. if (cs_link_strip in aktglobalswitches) then
  382. StripStr := '-s'
  383. else
  384. StripStr := '';
  385. if (usewindowapi) or (AppType = app_gui) then
  386. AppTypeStr := '-p'
  387. else if AppType = app_fs then
  388. AppTypeStr := '-f'
  389. else AppTypeStr := '-w';
  390. if not (Current_module.ResourceFiles.Empty) then
  391. RsrcStr := '-r ' + Current_module.ResourceFiles.GetFirst
  392. else
  393. RsrcStr := '';
  394. (* Only one resource file supported, discard everything else
  395. (should be already empty anyway, though). *)
  396. Current_module.ResourceFiles.Clear;
  397. { Write used files and libraries }
  398. WriteResponseFile(false);
  399. { Call linker }
  400. success:=false;
  401. for i:=1 to 3 do
  402. begin
  403. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  404. if binstr<>'' then
  405. begin
  406. { Is this really required? Not anymore according to my EMX docs }
  407. Replace(cmdstr,'$HEAPMB',tostr((1048575) shr 20));
  408. {Size of the stack when an EMX program runs in OS/2.}
  409. Replace(cmdstr,'$STACKKB',tostr((stacksize+1023) shr 10));
  410. {When an EMX program runs in DOS, the heap and stack share the
  411. same memory pool. The heap grows upwards, the stack grows downwards.}
  412. Replace(cmdstr,'$DOSHEAPKB',tostr((stacksize+1023) shr 10));
  413. Replace(cmdstr,'$STRIP',StripStr);
  414. Replace(cmdstr,'$APPTYPE',AppTypeStr);
  415. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  416. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  417. Replace(cmdstr,'$RSRC',RsrcStr);
  418. Replace(cmdstr,'$OUT',maybequoted(OutName));
  419. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  420. if i<>3 then
  421. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,(i=1),false)
  422. else
  423. success:=DoExec(binstr,cmdstr,(i=1),true);
  424. end;
  425. end;
  426. { Remove ReponseFile }
  427. if (success) and not(cs_link_extern in aktglobalswitches) then
  428. RemoveFile(outputexedir+Info.ResName);
  429. MakeExecutable:=success; { otherwise a recursive call to link method }
  430. end;
  431. {*****************************************************************************
  432. Initialize
  433. *****************************************************************************}
  434. initialization
  435. RegisterExternalLinker(system_i386_os2_info,TLinkerOS2);
  436. RegisterImport(system_i386_os2,TImportLibOS2);
  437. { RegisterRes(res_emxbind_info);}
  438. RegisterTarget(system_i386_os2_info);
  439. end.
  440. {
  441. $Log$
  442. Revision 1.20 2005-02-14 17:13:10 peter
  443. * truncate log
  444. }