t_os2.pas 15 KB

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