os2_targ.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 os2_targ;
  27. interface
  28. uses import;
  29. type
  30. pimportlibos2=^timportlibos2;
  31. timportlibos2=object(timportlib)
  32. procedure preparelib(const s:string);virtual;
  33. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  34. procedure generatelib;virtual;
  35. end;
  36. procedure write_def_file;
  37. {***************************************************************************}
  38. {***************************************************************************}
  39. implementation
  40. uses dos,strings,globals,link,files;
  41. const profile_flag:boolean=false;
  42. const n_ext = 1;
  43. n_abs = 2;
  44. n_text = 4;
  45. n_data = 6;
  46. n_bss = 8;
  47. n_imp1 = $68;
  48. n_imp2 = $6a;
  49. type reloc=packed record {This is the layout of a relocation table
  50. entry.}
  51. address:longint; {Fixup location}
  52. remaining:longint;
  53. {Meaning of bits for remaining:
  54. 0..23: Symbol number or segment
  55. 24: Self-relative fixup if non-zero
  56. 25..26: Fixup size (0: 1 byte, 1: 2, 2: 4 bytes)
  57. 27: Reference to symbol or segment
  58. 28..31 Not used}
  59. end;
  60. nlist=packed record {This is the layout of a symbol table entry.}
  61. strofs:longint; {Offset in string table}
  62. typ:byte; {Type of the symbol}
  63. other:byte; {Other information}
  64. desc:word; {More information}
  65. value:longint; {Value (address)}
  66. end;
  67. a_out_header=packed record
  68. magic:word; {Magic word, must be $0107}
  69. machtype:byte; {Machine type}
  70. flags:byte; {Flags}
  71. text_size:longint; {Length of text, in bytes}
  72. data_size:longint; {Length of initialized data, in bytes}
  73. bss_size:longint; {Length of uninitialized data, in bytes}
  74. sym_size:longint; {Length of symbol table, in bytes}
  75. entry:longint; {Start address (entry point)}
  76. trsize:longint; {Length of relocation info for text, bytes}
  77. drsize:longint; {Length of relocation info for data, bytes}
  78. end;
  79. ar_hdr=packed record
  80. ar_name:array[0..15] of char;
  81. ar_date:array[0..11] of char;
  82. ar_uid:array[0..5] of char;
  83. ar_gid:array[0..5] of char;
  84. ar_mode:array[0..7] of char;
  85. ar_size:array[0..9] of char;
  86. ar_fmag:array[0..1] of char;
  87. end;
  88. var aout_str_size:longint;
  89. aout_str_tab:array[0..2047] of byte;
  90. aout_sym_count:longint;
  91. aout_sym_tab:array[0..5] of nlist;
  92. aout_text:array[0..63] of byte;
  93. aout_text_size:longint;
  94. aout_treloc_tab:array[0..1] of reloc;
  95. aout_treloc_count:longint;
  96. aout_size:longint;
  97. seq_no:longint;
  98. ar_member_size:longint;
  99. out_file:file;
  100. procedure write_ar(const name:string;size:longint);
  101. var ar:ar_hdr;
  102. time:datetime;
  103. dummy:word;
  104. numtime:longint;
  105. tmp:string[19];
  106. begin
  107. ar_member_size:=size;
  108. fillchar(ar.ar_name,sizeof(ar.ar_name),' ');
  109. move(name[1],ar.ar_name,length(name));
  110. getdate(time.year,time.month,time.day,dummy);
  111. gettime(time.hour,time.min,time.sec,dummy);
  112. packtime(time,numtime);
  113. str(numtime,tmp);
  114. fillchar(ar.ar_date,sizeof(ar.ar_date),' ');
  115. move(tmp[1],ar.ar_date,length(tmp));
  116. ar.ar_uid:='0 ';
  117. ar.ar_gid:='0 ';
  118. ar.ar_mode:='100666'#0#0;
  119. str(size,tmp);
  120. fillchar(ar.ar_size,sizeof(ar.ar_size),' ');
  121. move(tmp[1],ar.ar_size,length(tmp));
  122. ar.ar_fmag:='`'#10;
  123. blockwrite(out_file,ar,sizeof(ar));
  124. end;
  125. procedure finish_ar;
  126. var a:byte;
  127. begin
  128. a:=0;
  129. if odd(ar_member_size) then
  130. blockwrite(out_file,a,1);
  131. end;
  132. procedure aout_init;
  133. begin
  134. aout_str_size:=sizeof(longint);
  135. aout_sym_count:=0;
  136. aout_text_size:=0;
  137. aout_treloc_count:=0;
  138. end;
  139. function aout_sym(const name:string;typ,other:byte;desc:word;
  140. value:longint):longint;
  141. begin
  142. if aout_str_size+length(name)+1>sizeof(aout_str_tab) then
  143. runerror($da);
  144. if aout_sym_count>=sizeof(aout_sym_tab) div sizeof(aout_sym_tab[0]) then
  145. runerror($da);
  146. aout_sym_tab[aout_sym_count].strofs:=aout_str_size;
  147. aout_sym_tab[aout_sym_count].typ:=typ;
  148. aout_sym_tab[aout_sym_count].other:=other;
  149. aout_sym_tab[aout_sym_count].desc:=desc;
  150. aout_sym_tab[aout_sym_count].value:=value;
  151. strPcopy(@aout_str_tab[aout_str_size],name);
  152. aout_str_size:=aout_str_size+length(name)+1;
  153. aout_sym:=aout_sym_count;
  154. inc(aout_sym_count);
  155. end;
  156. procedure aout_text_byte(b:byte);
  157. begin
  158. if aout_text_size>=sizeof(aout_text) then
  159. runerror($da);
  160. aout_text[aout_text_size]:=b;
  161. inc(aout_text_size);
  162. end;
  163. procedure aout_text_dword(d:longint);
  164. type li_ar=array[0..3] of byte;
  165. begin
  166. aout_text_byte(li_ar(d)[0]);
  167. aout_text_byte(li_ar(d)[1]);
  168. aout_text_byte(li_ar(d)[2]);
  169. aout_text_byte(li_ar(d)[3]);
  170. end;
  171. procedure aout_treloc(address,symbolnum,pcrel,len,ext:longint);
  172. begin
  173. if aout_treloc_count>=sizeof(aout_treloc_tab) div sizeof(reloc) then
  174. runerror($da);
  175. aout_treloc_tab[aout_treloc_count].address:=address;
  176. aout_treloc_tab[aout_treloc_count].remaining:=symbolnum+pcrel shl 24+
  177. len shl 25+ext shl 27;
  178. inc(aout_treloc_count);
  179. end;
  180. procedure aout_finish;
  181. begin
  182. while (aout_text_size and 3)<>0 do
  183. aout_text_byte ($90);
  184. aout_size:=sizeof(a_out_header)+aout_text_size+aout_treloc_count*
  185. sizeof(reloc)+aout_sym_count*sizeof(aout_sym_tab[0])+aout_str_size;
  186. end;
  187. procedure aout_write;
  188. var ao:a_out_header;
  189. begin
  190. ao.magic:=$0107;
  191. ao.machtype:=0;
  192. ao.flags:=0;
  193. ao.text_size:=aout_text_size;
  194. ao.data_size:=0;
  195. ao.bss_size:=0;
  196. ao.sym_size:=aout_sym_count*sizeof(aout_sym_tab[0]);
  197. ao.entry:=0;
  198. ao.trsize:=aout_treloc_count*sizeof(reloc);
  199. ao.drsize:=0;
  200. blockwrite(out_file,ao,sizeof(ao));
  201. blockwrite(out_file,aout_text,aout_text_size);
  202. blockwrite(out_file,aout_treloc_tab,sizeof(reloc)*aout_treloc_count);
  203. blockwrite(out_file,aout_sym_tab,sizeof(aout_sym_tab[0])*aout_sym_count);
  204. longint((@aout_str_tab)^):=aout_str_size;
  205. blockwrite(out_file,aout_str_tab,aout_str_size);
  206. end;
  207. procedure timportlibos2.preparelib(const s:string);
  208. {This code triggers a lot of bugs in the compiler.
  209. const armag='!<arch>'#10;
  210. ar_magic:array[1..length(armag)] of char=armag;}
  211. const ar_magic:array[1..8] of char='!<arch>'#10;
  212. begin
  213. seq_no:=1;
  214. Linker.AddLibraryFile(s+'.dll');
  215. current_module^.linkofiles.insert(s+'.dll');
  216. assign(out_file,s+'.ao2');
  217. rewrite(out_file,1);
  218. blockwrite(out_file,ar_magic,sizeof(ar_magic));
  219. end;
  220. procedure timportlibos2.importprocedure(const func,module:string;index:longint;const name:string);
  221. {func = Name of function to import.
  222. module = Name of DLL to import from.
  223. index = Index of function in DLL. Use 0 to import by name.
  224. name = Name of function in DLL. Ignored when index=0;}
  225. var tmp1,tmp2,tmp3:string;
  226. sym_mcount,sym_entry,sym_import:longint;
  227. fixup_mcount,fixup_import:longint;
  228. begin
  229. aout_init;
  230. tmp2:=func;
  231. if profile_flag and not (copy(func,1,4)='_16_') then
  232. begin
  233. sym_entry:=aout_sym(func,n_text+n_ext,0,0,aout_text_size);
  234. sym_mcount:=aout_sym('__mcount',n_ext,0,0,0);
  235. {Use, say, "_$U_DosRead" for "DosRead" to import the
  236. non-profiled function.}
  237. tmp2:='__$U_'+func;
  238. sym_import:=aout_sym(tmp2,n_ext,0,0,0);
  239. aout_text_byte($55); {push ebp}
  240. aout_text_byte($89); {mov ebp, esp}
  241. aout_text_byte($e5);
  242. aout_text_byte($e8); {call _mcount}
  243. fixup_mcount:=aout_text_size;
  244. aout_text_dword(0-(aout_text_size+4));
  245. aout_text_byte($5d); {pop ebp}
  246. aout_text_byte($e9); {jmp _$U_DosRead}
  247. fixup_import:=aout_text_size;
  248. aout_text_dword(0-(aout_text_size+4));
  249. aout_treloc(fixup_mcount,sym_mcount,1,2,1);
  250. aout_treloc (fixup_import, sym_import,1,2,1);
  251. end;
  252. str(seq_no,tmp1);
  253. tmp1:='IMPORT#'+tmp1;
  254. if name='' then
  255. begin
  256. str(index,tmp3);
  257. tmp3:=func+'='+module+'.'+tmp3;
  258. end
  259. else
  260. tmp3:=func+'='+module+'.'+name;
  261. aout_sym(tmp2,n_imp1+n_ext,0,0,0);
  262. aout_sym(tmp3,n_imp2+n_ext,0,0,0);
  263. aout_finish;
  264. write_ar(tmp1,aout_size);
  265. aout_write;
  266. finish_ar;
  267. inc(seq_no);
  268. end;
  269. procedure timportlibos2.generatelib;
  270. begin
  271. close(out_file);
  272. end;
  273. procedure write_def_file;
  274. begin
  275. assign(deffile,inputdir+inputfile+'.DEF');
  276. {$I+}
  277. rewrite(deffile);
  278. {$I-}
  279. if ioresult=0 then
  280. begin
  281. write(deffile,'NAME '+inputfile);
  282. if genpm then
  283. write(deffile,' WINDOWAPI');
  284. writeln(deffile,#13#10#13#10'PROTMODE'#13#10);
  285. writeln(deffile,'DESCRIPTION '+''''+description+''''#13#10);
  286. writeln(deffile,'DATA'#9'MULTIPLE'#13#10);
  287. writeln(deffile,'STACKSIZE'#9+tostr(stacksize));
  288. writeln(deffile,'HEAPSIZE'#9+tostr(heapsize)+#13#10);
  289. write(deffile,'EXPORTS');
  290. end
  291. else
  292. gendeffile:=false;
  293. end;
  294. end.
  295. {
  296. $Log$
  297. Revision 1.1 1998-03-25 11:18:15 root
  298. Initial revision
  299. Revision 1.15 1998/03/10 01:17:21 peter
  300. * all files have the same header
  301. * messages are fully implemented, EXTDEBUG uses Comment()
  302. + AG... files for the Assembler generation
  303. Revision 1.14 1998/03/02 23:08:41 florian
  304. * the concatcopy bug removed (solves problems when compilg sysatari!)
  305. Revision 1.13 1998/03/02 13:38:40 peter
  306. + importlib object
  307. * doesn't crash on a systemunit anymore
  308. * updated makefile and depend
  309. Revision 1.11 1998/02/28 00:20:27 florian
  310. * more changes to get import libs for Win32 working
  311. }