os2_targ.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. {***************************************************************************}
  37. {***************************************************************************}
  38. implementation
  39. uses globtype,dos,strings,globals,link,files;
  40. const profile_flag:boolean=false;
  41. const n_ext = 1;
  42. n_abs = 2;
  43. n_text = 4;
  44. n_data = 6;
  45. n_bss = 8;
  46. n_imp1 = $68;
  47. n_imp2 = $6a;
  48. type reloc=packed record {This is the layout of a relocation table
  49. entry.}
  50. address:longint; {Fixup location}
  51. remaining:longint;
  52. {Meaning of bits for remaining:
  53. 0..23: Symbol number or segment
  54. 24: Self-relative fixup if non-zero
  55. 25..26: Fixup size (0: 1 byte, 1: 2, 2: 4 bytes)
  56. 27: Reference to symbol or segment
  57. 28..31 Not used}
  58. end;
  59. nlist=packed record {This is the layout of a symbol table entry.}
  60. strofs:longint; {Offset in string table}
  61. typ:byte; {Type of the symbol}
  62. other:byte; {Other information}
  63. desc:word; {More information}
  64. value:longint; {Value (address)}
  65. end;
  66. a_out_header=packed record
  67. magic:word; {Magic word, must be $0107}
  68. machtype:byte; {Machine type}
  69. flags:byte; {Flags}
  70. text_size:longint; {Length of text, in bytes}
  71. data_size:longint; {Length of initialized data, in bytes}
  72. bss_size:longint; {Length of uninitialized data, in bytes}
  73. sym_size:longint; {Length of symbol table, in bytes}
  74. entry:longint; {Start address (entry point)}
  75. trsize:longint; {Length of relocation info for text, bytes}
  76. drsize:longint; {Length of relocation info for data, bytes}
  77. end;
  78. ar_hdr=packed record
  79. ar_name:array[0..15] of char;
  80. ar_date:array[0..11] of char;
  81. ar_uid:array[0..5] of char;
  82. ar_gid:array[0..5] of char;
  83. ar_mode:array[0..7] of char;
  84. ar_size:array[0..9] of char;
  85. ar_fmag:array[0..1] of char;
  86. end;
  87. var aout_str_size:longint;
  88. aout_str_tab:array[0..2047] of byte;
  89. aout_sym_count:longint;
  90. aout_sym_tab:array[0..5] of nlist;
  91. aout_text:array[0..63] of byte;
  92. aout_text_size:longint;
  93. aout_treloc_tab:array[0..1] of reloc;
  94. aout_treloc_count:longint;
  95. aout_size:longint;
  96. seq_no:longint;
  97. ar_member_size:longint;
  98. out_file:file;
  99. procedure write_ar(const name:string;size:longint);
  100. var ar:ar_hdr;
  101. time:datetime;
  102. dummy:word;
  103. numtime:longint;
  104. tmp:string[19];
  105. begin
  106. ar_member_size:=size;
  107. fillchar(ar.ar_name,sizeof(ar.ar_name),' ');
  108. move(name[1],ar.ar_name,length(name));
  109. getdate(time.year,time.month,time.day,dummy);
  110. gettime(time.hour,time.min,time.sec,dummy);
  111. packtime(time,numtime);
  112. str(numtime,tmp);
  113. fillchar(ar.ar_date,sizeof(ar.ar_date),' ');
  114. move(tmp[1],ar.ar_date,length(tmp));
  115. ar.ar_uid:='0 ';
  116. ar.ar_gid:='0 ';
  117. ar.ar_mode:='100666'#0#0;
  118. str(size,tmp);
  119. fillchar(ar.ar_size,sizeof(ar.ar_size),' ');
  120. move(tmp[1],ar.ar_size,length(tmp));
  121. ar.ar_fmag:='`'#10;
  122. blockwrite(out_file,ar,sizeof(ar));
  123. end;
  124. procedure finish_ar;
  125. var a:byte;
  126. begin
  127. a:=0;
  128. if odd(ar_member_size) then
  129. blockwrite(out_file,a,1);
  130. end;
  131. procedure aout_init;
  132. begin
  133. aout_str_size:=sizeof(longint);
  134. aout_sym_count:=0;
  135. aout_text_size:=0;
  136. aout_treloc_count:=0;
  137. end;
  138. function aout_sym(const name:string;typ,other:byte;desc:word;
  139. value:longint):longint;
  140. begin
  141. if aout_str_size+length(name)+1>sizeof(aout_str_tab) then
  142. runerror($da);
  143. if aout_sym_count>=sizeof(aout_sym_tab) div sizeof(aout_sym_tab[0]) then
  144. runerror($da);
  145. aout_sym_tab[aout_sym_count].strofs:=aout_str_size;
  146. aout_sym_tab[aout_sym_count].typ:=typ;
  147. aout_sym_tab[aout_sym_count].other:=other;
  148. aout_sym_tab[aout_sym_count].desc:=desc;
  149. aout_sym_tab[aout_sym_count].value:=value;
  150. strPcopy(@aout_str_tab[aout_str_size],name);
  151. aout_str_size:=aout_str_size+length(name)+1;
  152. aout_sym:=aout_sym_count;
  153. inc(aout_sym_count);
  154. end;
  155. procedure aout_text_byte(b:byte);
  156. begin
  157. if aout_text_size>=sizeof(aout_text) then
  158. runerror($da);
  159. aout_text[aout_text_size]:=b;
  160. inc(aout_text_size);
  161. end;
  162. procedure aout_text_dword(d:longint);
  163. type li_ar=array[0..3] of byte;
  164. begin
  165. aout_text_byte(li_ar(d)[0]);
  166. aout_text_byte(li_ar(d)[1]);
  167. aout_text_byte(li_ar(d)[2]);
  168. aout_text_byte(li_ar(d)[3]);
  169. end;
  170. procedure aout_treloc(address,symbolnum,pcrel,len,ext:longint);
  171. begin
  172. if aout_treloc_count>=sizeof(aout_treloc_tab) div sizeof(reloc) then
  173. runerror($da);
  174. aout_treloc_tab[aout_treloc_count].address:=address;
  175. aout_treloc_tab[aout_treloc_count].remaining:=symbolnum+pcrel shl 24+
  176. len shl 25+ext shl 27;
  177. inc(aout_treloc_count);
  178. end;
  179. procedure aout_finish;
  180. begin
  181. while (aout_text_size and 3)<>0 do
  182. aout_text_byte ($90);
  183. aout_size:=sizeof(a_out_header)+aout_text_size+aout_treloc_count*
  184. sizeof(reloc)+aout_sym_count*sizeof(aout_sym_tab[0])+aout_str_size;
  185. end;
  186. procedure aout_write;
  187. var ao:a_out_header;
  188. begin
  189. ao.magic:=$0107;
  190. ao.machtype:=0;
  191. ao.flags:=0;
  192. ao.text_size:=aout_text_size;
  193. ao.data_size:=0;
  194. ao.bss_size:=0;
  195. ao.sym_size:=aout_sym_count*sizeof(aout_sym_tab[0]);
  196. ao.entry:=0;
  197. ao.trsize:=aout_treloc_count*sizeof(reloc);
  198. ao.drsize:=0;
  199. blockwrite(out_file,ao,sizeof(ao));
  200. blockwrite(out_file,aout_text,aout_text_size);
  201. blockwrite(out_file,aout_treloc_tab,sizeof(reloc)*aout_treloc_count);
  202. blockwrite(out_file,aout_sym_tab,sizeof(aout_sym_tab[0])*aout_sym_count);
  203. longint((@aout_str_tab)^):=aout_str_size;
  204. blockwrite(out_file,aout_str_tab,aout_str_size);
  205. end;
  206. procedure timportlibos2.preparelib(const s:string);
  207. {This code triggers a lot of bugs in the compiler.
  208. const armag='!<arch>'#10;
  209. ar_magic:array[1..length(armag)] of char=armag;}
  210. const ar_magic:array[1..8] of char='!<arch>'#10;
  211. begin
  212. seq_no:=1;
  213. if not (cs_smartlink in aktmoduleswitches) then
  214. current_module^.linkstaticlibs.insert(s);
  215. assign(out_file,current_module^.path^+s+'.ao2');
  216. rewrite(out_file,1);
  217. blockwrite(out_file,ar_magic,sizeof(ar_magic));
  218. end;
  219. procedure timportlibos2.importprocedure(const func,module:string;index:longint;const name:string);
  220. {func = Name of function to import.
  221. module = Name of DLL to import from.
  222. index = Index of function in DLL. Use 0 to import by name.
  223. name = Name of function in DLL. Ignored when index=0;}
  224. var tmp1,tmp2,tmp3:string;
  225. sym_mcount,sym_entry,sym_import:longint;
  226. fixup_mcount,fixup_import:longint;
  227. begin
  228. aout_init;
  229. tmp2:=func;
  230. if profile_flag and not (copy(func,1,4)='_16_') then
  231. begin
  232. sym_entry:=aout_sym(func,n_text+n_ext,0,0,aout_text_size);
  233. sym_mcount:=aout_sym('__mcount',n_ext,0,0,0);
  234. {Use, say, "_$U_DosRead" for "DosRead" to import the
  235. non-profiled function.}
  236. tmp2:='__$U_'+func;
  237. sym_import:=aout_sym(tmp2,n_ext,0,0,0);
  238. aout_text_byte($55); {push ebp}
  239. aout_text_byte($89); {mov ebp, esp}
  240. aout_text_byte($e5);
  241. aout_text_byte($e8); {call _mcount}
  242. fixup_mcount:=aout_text_size;
  243. aout_text_dword(0-(aout_text_size+4));
  244. aout_text_byte($5d); {pop ebp}
  245. aout_text_byte($e9); {jmp _$U_DosRead}
  246. fixup_import:=aout_text_size;
  247. aout_text_dword(0-(aout_text_size+4));
  248. aout_treloc(fixup_mcount,sym_mcount,1,2,1);
  249. aout_treloc (fixup_import, sym_import,1,2,1);
  250. end;
  251. str(seq_no,tmp1);
  252. tmp1:='IMPORT#'+tmp1;
  253. if name='' then
  254. begin
  255. str(index,tmp3);
  256. tmp3:=func+'='+module+'.'+tmp3;
  257. end
  258. else
  259. tmp3:=func+'='+module+'.'+name;
  260. aout_sym(tmp2,n_imp1+n_ext,0,0,0);
  261. aout_sym(tmp3,n_imp2+n_ext,0,0,0);
  262. aout_finish;
  263. write_ar(tmp1,aout_size);
  264. aout_write;
  265. finish_ar;
  266. inc(seq_no);
  267. end;
  268. procedure timportlibos2.generatelib;
  269. begin
  270. close(out_file);
  271. end;
  272. end.
  273. {
  274. $Log$
  275. Revision 1.6 1998-12-11 00:03:25 peter
  276. + globtype,tokens,version unit splitted from globals
  277. Revision 1.5 1998/10/16 14:20:53 daniel
  278. * Faster keyword scanning.
  279. * Import library and smartlink library in one file.
  280. Revision 1.4 1998/06/17 14:10:14 peter
  281. * small os2 fixes
  282. * fixed interdependent units with newppu (remake3 under linux works now)
  283. Revision 1.3 1998/06/04 23:51:48 peter
  284. * m68k compiles
  285. + .def file creation moved to gendef.pas so it could also be used
  286. for win32
  287. Revision 1.2 1998/05/04 17:54:27 peter
  288. + smartlinking works (only case jumptable left todo)
  289. * redesign of systems.pas to support assemblers and linkers
  290. + Unitname is now also in the PPU-file, increased version to 14
  291. }