os2_targ.pas 11 KB

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