owar.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Peter Vreman
  4. Contains the stuff for writing .a files directly
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit owar;
  19. interface
  20. uses
  21. cobjects,owbase;
  22. type
  23. tarhdr=packed record
  24. name : array[0..15] of char;
  25. date : array[0..11] of char;
  26. uid : array[0..5] of char;
  27. gid : array[0..5] of char;
  28. mode : array[0..7] of char;
  29. size : array[0..9] of char;
  30. fmag : array[0..1] of char;
  31. end;
  32. parobjectwriter=^tarobjectwriter;
  33. tarobjectwriter=object(tobjectwriter)
  34. constructor Init(const Aarfn:string);
  35. destructor Done;virtual;
  36. procedure create(const fn:string);virtual;
  37. procedure close;virtual;
  38. procedure writesym(sym:string);virtual;
  39. procedure write(var b;len:longint);virtual;
  40. private
  41. arfn : string;
  42. arhdr : tarhdr;
  43. symreloc,
  44. symstr,
  45. lfnstr,
  46. ardata,
  47. objdata : PDynamicArray;
  48. objfixup : longint;
  49. objfn : string;
  50. timestamp : string[12];
  51. procedure createarhdr(fn:string;size:longint;const gid,uid,mode:string);
  52. procedure writear;
  53. end;
  54. implementation
  55. uses
  56. verbose,
  57. {$ifdef Delphi}
  58. dmisc,
  59. {$endif Delphi}
  60. dos;
  61. const
  62. {$ifdef TP}
  63. symrelocbufsize = 32;
  64. symstrbufsize = 256;
  65. lfnstrbufsize = 256;
  66. arbufsize = 256;
  67. objbufsize = 256;
  68. {$else}
  69. symrelocbufsize = 1024;
  70. symstrbufsize = 8192;
  71. lfnstrbufsize = 4096;
  72. arbufsize = 65536;
  73. objbufsize = 16384;
  74. {$endif}
  75. {*****************************************************************************
  76. Helpers
  77. *****************************************************************************}
  78. const
  79. C1970=2440588;
  80. D0=1461;
  81. D1=146097;
  82. D2=1721119;
  83. Function Gregorian2Julian(DT:DateTime):LongInt;
  84. Var
  85. Century,XYear,Month : LongInt;
  86. Begin
  87. Month:=DT.Month;
  88. If Month<=2 Then
  89. Begin
  90. Dec(DT.Year);
  91. Inc(Month,12);
  92. End;
  93. Dec(Month,3);
  94. Century:=(longint(DT.Year Div 100)*D1) shr 2;
  95. XYear:=(longint(DT.Year Mod 100)*D0) shr 2;
  96. Gregorian2Julian:=((((Month*153)+2) div 5)+DT.Day)+D2+XYear+Century;
  97. End;
  98. function DT2Unix(DT:DateTime):LongInt;
  99. Begin
  100. DT2Unix:=(Gregorian2Julian(DT)-C1970)*86400+(LongInt(DT.Hour)*3600)+(DT.Min*60)+DT.Sec;
  101. end;
  102. {*****************************************************************************
  103. TArObjectWriter
  104. *****************************************************************************}
  105. constructor tarobjectwriter.init(const Aarfn:string);
  106. var
  107. time : datetime;
  108. dummy : word;
  109. begin
  110. arfn:=Aarfn;
  111. new(arData,init(1,arbufsize));
  112. new(symreloc,init(4,symrelocbufsize));
  113. new(symstr,init(1,symstrbufsize));
  114. new(lfnstr,init(1,lfnstrbufsize));
  115. { create timestamp }
  116. getdate(time.year,time.month,time.day,dummy);
  117. gettime(time.hour,time.min,time.sec,dummy);
  118. Str(DT2Unix(time),timestamp);
  119. end;
  120. destructor tarobjectwriter.done;
  121. begin
  122. if Errorcount=0 then
  123. writear;
  124. dispose(arData,done);
  125. dispose(symreloc,done);
  126. dispose(symstr,done);
  127. dispose(lfnstr,done);
  128. end;
  129. procedure tarobjectwriter.createarhdr(fn:string;size:longint;const gid,uid,mode:string);
  130. var
  131. tmp : string[9];
  132. begin
  133. fillchar(arhdr,sizeof(tarhdr),' ');
  134. { create ar header }
  135. fn:=fn+'/';
  136. if length(fn)>16 then
  137. begin
  138. arhdr.name[0]:='/';
  139. str(lfnstr^.usedsize,tmp);
  140. move(tmp[1],arhdr.name[1],length(tmp));
  141. fn:=fn+#10;
  142. lfnstr^.write(fn[1],length(fn));
  143. end
  144. else
  145. move(fn[1],arhdr.name,length(fn));
  146. { don't write a date if also no gid/uid/mode is specified }
  147. if gid<>'' then
  148. move(timestamp[1],arhdr.date,sizeof(timestamp));
  149. str(size,tmp);
  150. move(tmp[1],arhdr.size,length(tmp));
  151. move(gid[1],arhdr.gid,length(gid));
  152. move(uid[1],arhdr.uid,length(uid));
  153. move(mode[1],arhdr.mode,length(mode));
  154. arhdr.fmag:='`'#10;
  155. end;
  156. procedure tarobjectwriter.create(const fn:string);
  157. begin
  158. objfn:=fn;
  159. objfixup:=ardata^.usedsize;
  160. { reset size }
  161. new(objdata,init(1,objbufsize));
  162. end;
  163. procedure tarobjectwriter.close;
  164. begin
  165. objdata^.align(2);
  166. { fix the size in the header }
  167. createarhdr(objfn,objdata^.usedsize,'42','42','644');
  168. { write the header }
  169. ardata^.write(arhdr,sizeof(tarhdr));
  170. { write the data of this objfile }
  171. ardata^.write(objdata^.data^,objdata^.usedsize);
  172. { free this object }
  173. dispose(objdata,done);
  174. end;
  175. procedure tarobjectwriter.writesym(sym:string);
  176. begin
  177. sym:=sym+#0;
  178. symreloc^.write(objfixup,1);
  179. symstr^.write(sym[1],length(sym));
  180. end;
  181. procedure tarobjectwriter.write(var b;len:longint);
  182. begin
  183. objdata^.write(b,len);
  184. end;
  185. procedure tarobjectwriter.writear;
  186. function lsb2msb(l:longint):longint;
  187. type
  188. bytearr=array[0..3] of byte;
  189. var
  190. l1 : longint;
  191. begin
  192. bytearr(l1)[0]:=bytearr(l)[3];
  193. bytearr(l1)[1]:=bytearr(l)[2];
  194. bytearr(l1)[2]:=bytearr(l)[1];
  195. bytearr(l1)[3]:=bytearr(l)[0];
  196. lsb2msb:=l1;
  197. end;
  198. const
  199. armagic:array[1..8] of char='!<arch>'#10;
  200. type
  201. plongint=^longint;
  202. var
  203. arf : file;
  204. fixup,
  205. relocs,i : longint;
  206. begin
  207. assign(arf,arfn);
  208. {$I-}
  209. rewrite(arf,1);
  210. {$I+}
  211. if ioresult<>0 then
  212. exit;
  213. blockwrite(arf,armagic,sizeof(armagic));
  214. { align first, because we need the size for the fixups of the symbol reloc }
  215. if lfnstr^.usedsize>0 then
  216. lfnstr^.align(2);
  217. if symreloc^.usedsize>0 then
  218. begin
  219. symstr^.align(2);
  220. fixup:=12+sizeof(tarhdr)+symreloc^.usedsize+symstr^.usedsize;
  221. if lfnstr^.usedsize>0 then
  222. inc(fixup,lfnstr^.usedsize+sizeof(tarhdr));
  223. relocs:=symreloc^.count;
  224. for i:=0to relocs-1 do
  225. plongint(@symreloc^.data[i*4])^:=lsb2msb(plongint(@symreloc^.data[i*4])^+fixup);
  226. createarhdr('',4+symreloc^.usedsize+symstr^.usedsize,'0','0','0');
  227. blockwrite(arf,arhdr,sizeof(tarhdr));
  228. relocs:=lsb2msb(relocs);
  229. blockwrite(arf,relocs,4);
  230. blockwrite(arf,symreloc^.data^,symreloc^.usedsize);
  231. blockwrite(arf,symstr^.data^,symstr^.usedsize);
  232. end;
  233. if lfnstr^.usedsize>0 then
  234. begin
  235. createarhdr('/',lfnstr^.usedsize,'','','');
  236. blockwrite(arf,arhdr,sizeof(tarhdr));
  237. blockwrite(arf,lfnstr^.data^,lfnstr^.usedsize);
  238. end;
  239. blockwrite(arf,ardata^.data^,ardata^.usedsize);
  240. system.close(arf);
  241. end;
  242. end.
  243. {
  244. $Log$
  245. Revision 1.3 1999-05-09 11:38:06 peter
  246. * don't write .o and link if errors occure during assembling
  247. Revision 1.2 1999/05/04 21:44:53 florian
  248. * changes to compile it with Delphi 4.0
  249. Revision 1.1 1999/05/01 13:24:26 peter
  250. * merged nasm compiler
  251. * old asm moved to oldasm/
  252. Revision 1.1 1999/03/18 20:30:51 peter
  253. + .a writer
  254. }