os2_targ.pas 11 KB

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