2
0

t_os2.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Daniel Mantione
  4. Portions Copyright (c) 1992-96 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. interface
  28. uses
  29. import,link;
  30. type
  31. pimportlibos2=^timportlibos2;
  32. timportlibos2=object(timportlib)
  33. procedure preparelib(const s:string);virtual;
  34. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  35. procedure generatelib;virtual;
  36. end;
  37. plinkeros2=^tlinkeros2;
  38. tlinkeros2=object(tlinker)
  39. private
  40. Function WriteResponseFile(isdll:boolean) : Boolean;
  41. public
  42. constructor Init;
  43. procedure SetDefaultInfo;virtual;
  44. function MakeExecutable:boolean;virtual;
  45. end;
  46. {***************************************************************************}
  47. {***************************************************************************}
  48. implementation
  49. uses
  50. {$ifdef Delphi}
  51. dmisc,
  52. {$else Delphi}
  53. dos,
  54. {$endif Delphi}
  55. globtype,strings,cobjects,comphook,systems,
  56. globals,verbose,files,script;
  57. const profile_flag:boolean=false;
  58. const n_ext = 1;
  59. n_abs = 2;
  60. n_text = 4;
  61. n_data = 6;
  62. n_bss = 8;
  63. n_imp1 = $68;
  64. n_imp2 = $6a;
  65. type reloc=packed record {This is the layout of a relocation table
  66. entry.}
  67. address:longint; {Fixup location}
  68. remaining:longint;
  69. {Meaning of bits for remaining:
  70. 0..23: Symbol number or segment
  71. 24: Self-relative fixup if non-zero
  72. 25..26: Fixup size (0: 1 byte, 1: 2, 2: 4 bytes)
  73. 27: Reference to symbol or segment
  74. 28..31 Not used}
  75. end;
  76. nlist=packed record {This is the layout of a symbol table entry.}
  77. strofs:longint; {Offset in string table}
  78. typ:byte; {Type of the symbol}
  79. other:byte; {Other information}
  80. desc:word; {More information}
  81. value:longint; {Value (address)}
  82. end;
  83. a_out_header=packed record
  84. magic:word; {Magic word, must be $0107}
  85. machtype:byte; {Machine type}
  86. flags:byte; {Flags}
  87. text_size:longint; {Length of text, in bytes}
  88. data_size:longint; {Length of initialized data, in bytes}
  89. bss_size:longint; {Length of uninitialized data, in bytes}
  90. sym_size:longint; {Length of symbol table, in bytes}
  91. entry:longint; {Start address (entry point)}
  92. trsize:longint; {Length of relocation info for text, bytes}
  93. drsize:longint; {Length of relocation info for data, bytes}
  94. end;
  95. ar_hdr=packed record
  96. ar_name:array[0..15] of char;
  97. ar_date:array[0..11] of char;
  98. ar_uid:array[0..5] of char;
  99. ar_gid:array[0..5] of char;
  100. ar_mode:array[0..7] of char;
  101. ar_size:array[0..9] of char;
  102. ar_fmag:array[0..1] of char;
  103. end;
  104. var aout_str_size:longint;
  105. aout_str_tab:array[0..2047] of byte;
  106. aout_sym_count:longint;
  107. aout_sym_tab:array[0..5] of nlist;
  108. aout_text:array[0..63] of byte;
  109. aout_text_size:longint;
  110. aout_treloc_tab:array[0..1] of reloc;
  111. aout_treloc_count:longint;
  112. aout_size:longint;
  113. seq_no:longint;
  114. ar_member_size:longint;
  115. out_file:file;
  116. procedure write_ar(const name:string;size:longint);
  117. var ar:ar_hdr;
  118. time:datetime;
  119. dummy:word;
  120. numtime:longint;
  121. tmp:string[19];
  122. begin
  123. ar_member_size:=size;
  124. fillchar(ar.ar_name,sizeof(ar.ar_name),' ');
  125. move(name[1],ar.ar_name,length(name));
  126. getdate(time.year,time.month,time.day,dummy);
  127. gettime(time.hour,time.min,time.sec,dummy);
  128. packtime(time,numtime);
  129. str(numtime,tmp);
  130. fillchar(ar.ar_date,sizeof(ar.ar_date),' ');
  131. move(tmp[1],ar.ar_date,length(tmp));
  132. ar.ar_uid:='0 ';
  133. ar.ar_gid:='0 ';
  134. ar.ar_mode:='100666'#0#0;
  135. str(size,tmp);
  136. fillchar(ar.ar_size,sizeof(ar.ar_size),' ');
  137. move(tmp[1],ar.ar_size,length(tmp));
  138. ar.ar_fmag:='`'#10;
  139. blockwrite(out_file,ar,sizeof(ar));
  140. end;
  141. procedure finish_ar;
  142. var a:byte;
  143. begin
  144. a:=0;
  145. if odd(ar_member_size) then
  146. blockwrite(out_file,a,1);
  147. end;
  148. procedure aout_init;
  149. begin
  150. aout_str_size:=sizeof(longint);
  151. aout_sym_count:=0;
  152. aout_text_size:=0;
  153. aout_treloc_count:=0;
  154. end;
  155. function aout_sym(const name:string;typ,other:byte;desc:word;
  156. value:longint):longint;
  157. begin
  158. if aout_str_size+length(name)+1>sizeof(aout_str_tab) then
  159. Do_halt($da);
  160. if aout_sym_count>=sizeof(aout_sym_tab) div sizeof(aout_sym_tab[0]) then
  161. Do_halt($da);
  162. aout_sym_tab[aout_sym_count].strofs:=aout_str_size;
  163. aout_sym_tab[aout_sym_count].typ:=typ;
  164. aout_sym_tab[aout_sym_count].other:=other;
  165. aout_sym_tab[aout_sym_count].desc:=desc;
  166. aout_sym_tab[aout_sym_count].value:=value;
  167. strPcopy(@aout_str_tab[aout_str_size],name);
  168. aout_str_size:=aout_str_size+length(name)+1;
  169. aout_sym:=aout_sym_count;
  170. inc(aout_sym_count);
  171. end;
  172. procedure aout_text_byte(b:byte);
  173. begin
  174. if aout_text_size>=sizeof(aout_text) then
  175. Do_halt($da);
  176. aout_text[aout_text_size]:=b;
  177. inc(aout_text_size);
  178. end;
  179. procedure aout_text_dword(d:longint);
  180. type li_ar=array[0..3] of byte;
  181. begin
  182. aout_text_byte(li_ar(d)[0]);
  183. aout_text_byte(li_ar(d)[1]);
  184. aout_text_byte(li_ar(d)[2]);
  185. aout_text_byte(li_ar(d)[3]);
  186. end;
  187. procedure aout_treloc(address,symbolnum,pcrel,len,ext:longint);
  188. begin
  189. if aout_treloc_count>=sizeof(aout_treloc_tab) div sizeof(reloc) then
  190. Do_halt($da);
  191. aout_treloc_tab[aout_treloc_count].address:=address;
  192. aout_treloc_tab[aout_treloc_count].remaining:=symbolnum+pcrel shl 24+
  193. len shl 25+ext shl 27;
  194. inc(aout_treloc_count);
  195. end;
  196. procedure aout_finish;
  197. begin
  198. while (aout_text_size and 3)<>0 do
  199. aout_text_byte ($90);
  200. aout_size:=sizeof(a_out_header)+aout_text_size+aout_treloc_count*
  201. sizeof(reloc)+aout_sym_count*sizeof(aout_sym_tab[0])+aout_str_size;
  202. end;
  203. procedure aout_write;
  204. var ao:a_out_header;
  205. begin
  206. ao.magic:=$0107;
  207. ao.machtype:=0;
  208. ao.flags:=0;
  209. ao.text_size:=aout_text_size;
  210. ao.data_size:=0;
  211. ao.bss_size:=0;
  212. ao.sym_size:=aout_sym_count*sizeof(aout_sym_tab[0]);
  213. ao.entry:=0;
  214. ao.trsize:=aout_treloc_count*sizeof(reloc);
  215. ao.drsize:=0;
  216. blockwrite(out_file,ao,sizeof(ao));
  217. blockwrite(out_file,aout_text,aout_text_size);
  218. blockwrite(out_file,aout_treloc_tab,sizeof(reloc)*aout_treloc_count);
  219. blockwrite(out_file,aout_sym_tab,sizeof(aout_sym_tab[0])*aout_sym_count);
  220. longint((@aout_str_tab)^):=aout_str_size;
  221. blockwrite(out_file,aout_str_tab,aout_str_size);
  222. end;
  223. procedure timportlibos2.preparelib(const s:string);
  224. {This code triggers a lot of bugs in the compiler.
  225. const armag='!<arch>'#10;
  226. ar_magic:array[1..length(armag)] of char=armag;}
  227. const ar_magic:array[1..8] of char='!<arch>'#10;
  228. begin
  229. seq_no:=1;
  230. if not (cs_create_smart in aktmoduleswitches) then
  231. current_module^.linkotherstaticlibs.insert(s,link_allways);
  232. assign(out_file,current_module^.path^+s+'.ao2');
  233. rewrite(out_file,1);
  234. blockwrite(out_file,ar_magic,sizeof(ar_magic));
  235. end;
  236. procedure timportlibos2.importprocedure(const func,module:string;index:longint;const name:string);
  237. {func = Name of function to import.
  238. module = Name of DLL to import from.
  239. index = Index of function in DLL. Use 0 to import by name.
  240. name = Name of function in DLL. Ignored when index=0;}
  241. var tmp1,tmp2,tmp3:string;
  242. sym_mcount,sym_entry,sym_import:longint;
  243. fixup_mcount,fixup_import:longint;
  244. begin
  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.Init;
  293. begin
  294. Inherited Init;
  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 -o $EXE @$RES';
  304. ExeCmd[2]:='emxbind -b $STRIP$PM -k$STACKKB -h$HEAPMB -o $EXE.exe $EXE -aim -s$DOSHEAPKB';
  305. end;
  306. end;
  307. Function TLinkeros2.WriteResponseFile(isdll:boolean) : Boolean;
  308. Var
  309. linkres : TLinkRes;
  310. i : longint;
  311. HPath : PStringQueueItem;
  312. s : string;
  313. begin
  314. WriteResponseFile:=False;
  315. { Open link.res file }
  316. LinkRes.Init(Info.ResName);
  317. { Write path to search libraries }
  318. HPath:=current_module^.locallibrarysearchpath.First;
  319. while assigned(HPath) do
  320. begin
  321. LinkRes.Add('-L'+HPath^.Data^);
  322. HPath:=HPath^.Next;
  323. end;
  324. HPath:=LibrarySearchPath.First;
  325. while assigned(HPath) do
  326. begin
  327. LinkRes.Add('-L'+HPath^.Data^);
  328. HPath:=HPath^.Next;
  329. end;
  330. { add objectfiles, start with prt0 always }
  331. LinkRes.AddFileName(FindObjectFile('prt0'));
  332. while not ObjectFiles.Empty do
  333. begin
  334. s:=ObjectFiles.Get;
  335. if s<>'' then
  336. LinkRes.AddFileName(s);
  337. end;
  338. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  339. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  340. While not SharedLibFiles.Empty do
  341. begin
  342. S:=SharedLibFiles.Get;
  343. i:=Pos(target_os.sharedlibext,S);
  344. if i>0 then
  345. Delete(S,i,255);
  346. LinkRes.Add('-l'+s);
  347. end;
  348. { Write staticlibraries }
  349. While not StaticLibFiles.Empty do
  350. begin
  351. S:=StaticLibFiles.Get;
  352. LinkRes.AddFileName(s)
  353. end;
  354. { Write and Close response }
  355. linkres.writetodisk;
  356. linkres.done;
  357. WriteResponseFile:=True;
  358. end;
  359. function TLinkeros2.MakeExecutable:boolean;
  360. var
  361. binstr,
  362. cmdstr : string;
  363. success : boolean;
  364. i : longint;
  365. PMStr,
  366. StripStr : string[40];
  367. begin
  368. if not(cs_link_extern in aktglobalswitches) then
  369. Message1(exec_i_linking,current_module^.exefilename^);
  370. { Create some replacements }
  371. StripStr:='';
  372. PMStr:='';
  373. if (cs_link_strip in aktglobalswitches) then
  374. StripStr:='-s';
  375. if usewindowapi then
  376. PMStr:='-p';
  377. { Write used files and libraries }
  378. WriteResponseFile(false);
  379. { Call linker }
  380. success:=false;
  381. for i:=1to 2 do
  382. begin
  383. SplitBinCmd(Info.ExeCmd[i],binstr,cmdstr);
  384. if binstr<>'' then
  385. begin
  386. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  387. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  388. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  389. Replace(cmdstr,'$STRIP',StripStr);
  390. Replace(cmdstr,'$HEAPMB',tostr((maxheapsize+1048575) shr 20));
  391. {Size of the stack when an EMX program runs in OS/2.}
  392. Replace(cmdstr,'$STACKKB',tostr((stacksize+1023) shr 10));
  393. {When an EMX program runs in DOS, the heap and stack share the
  394. same memory pool. The heap grows upwards, the stack grows downwards.}
  395. Replace(cmdstr,'$DOSHEAPKB',tostr((stacksize+maxheapsize+1023) shr 10));
  396. Replace(cmdstr,'$PM',PMStr);
  397. success:=DoExec(FindUtil(binstr),cmdstr,(i=1),false);
  398. if not success then
  399. break;
  400. end;
  401. end;
  402. { Remove ReponseFile }
  403. if (success) and not(cs_link_extern in aktglobalswitches) then
  404. RemoveFile(current_module^.outpath^+Info.ResName);
  405. MakeExecutable:=success; { otherwise a recursive call to link method }
  406. end;
  407. end.
  408. {
  409. $Log$
  410. Revision 1.3 1999-11-12 11:03:50 peter
  411. * searchpaths changed to stringqueue object
  412. Revision 1.2 1999/11/04 10:55:31 peter
  413. * TSearchPathString for the string type of the searchpaths, which is
  414. ansistring under FPC/Delphi
  415. Revision 1.1 1999/10/21 14:29:38 peter
  416. * redesigned linker object
  417. + library support for linux (only procedures can be exported)
  418. }