ogbase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. Contains the base stuff for binary object file writers
  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 ogbase;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. {$ifdef Delphi}
  23. sysutils,
  24. dmisc,
  25. {$else Delphi}
  26. strings,
  27. dos,
  28. {$endif Delphi}
  29. { common }
  30. cclasses,
  31. { targets }
  32. systems,
  33. { outputwriters }
  34. owbase,owar,
  35. { assembler }
  36. cpubase,aasm;
  37. type
  38. tsecsize = array[tsection] of longint;
  39. relative_type = (relative_false,relative_true,relative_rva);
  40. poutputreloc = ^toutputreloc;
  41. toutputreloc = packed record
  42. next : poutputreloc;
  43. address : longint;
  44. symbol : tasmsymbol;
  45. section : tsection; { only used if symbol=nil }
  46. typ : relative_type;
  47. end;
  48. poutputsymbol = ^toutputsymbol;
  49. toutputsymbol = packed record
  50. namestr : string[8]; { namestr or nameidx will be used }
  51. nameidx : longint;
  52. section : tsection;
  53. value : longint;
  54. bind : TAsmsymbind;
  55. typ : TAsmsymtype;
  56. size : longint;
  57. end;
  58. tobjectsection = class
  59. name : string[32];
  60. secsymidx : longint; { index for the section in symtab }
  61. addralign : longint;
  62. { size of the data and in the file }
  63. data : TDynamicArray;
  64. datasize : longint;
  65. datapos : longint;
  66. { size and position in memory, set by setsectionsize }
  67. memsize,
  68. mempos : longint;
  69. { relocation }
  70. nrelocs : longint;
  71. relochead : POutputReloc;
  72. reloctail : ^POutputReloc;
  73. constructor create(const Aname:string;Aalign:longint;alloconly:boolean);
  74. destructor destroy;override;
  75. function write(var d;l:longint):longint;
  76. function writestr(const s:string):longint;
  77. procedure writealign(l:longint);
  78. function aligneddatasize:longint;
  79. procedure alignsection;
  80. procedure alloc(l:longint);
  81. procedure addsymreloc(ofs:longint;p:tasmsymbol;relative:relative_type);
  82. procedure addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  83. end;
  84. tobjectdata = class
  85. { section }
  86. currsec : tsection;
  87. sects : array[TSection] of tobjectsection;
  88. localsyms : tdictionary;
  89. constructor create;
  90. destructor destroy;override;
  91. procedure createsection(sec:tsection);virtual;
  92. procedure defaultsection(sec:tsection);
  93. function sectionsize(s:tsection):longint;
  94. procedure setsectionsizes(var s:tsecsize);virtual;
  95. procedure alloc(len:longint);
  96. procedure allocalign(len:longint);
  97. procedure writebytes(var data;len:longint);
  98. procedure writereloc(data,len:longint;p:tasmsymbol;relative:relative_type);virtual;abstract;
  99. procedure writesymbol(p:tasmsymbol);virtual;abstract;
  100. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  101. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:tasmsymbol;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  102. procedure addsymbol(p:tasmsymbol);
  103. end;
  104. tobjectalloc = class
  105. currsec : tsection;
  106. secsize : tsecsize;
  107. constructor create;
  108. destructor destroy;override;
  109. procedure setsection(sec:tsection);
  110. function sectionsize:longint;
  111. procedure sectionalloc(l:longint);
  112. procedure sectionalign(l:longint);
  113. procedure staballoc(p:pchar);
  114. procedure resetsections;
  115. end;
  116. tobjectoutput = class
  117. protected
  118. { writer }
  119. FWriter : tobjectwriter;
  120. { section }
  121. FData : tobjectdata;
  122. procedure writetodisk;virtual;
  123. public
  124. constructor create(smart:boolean);
  125. destructor destroy;override;
  126. function initwriting(const fn:string):boolean;virtual;
  127. procedure donewriting;virtual;
  128. procedure exportsymbol(p:tasmsymbol);
  129. property Data:TObjectData read FData write FData;
  130. property Writer:TObjectWriter read FWriter;
  131. end;
  132. tobjectinput = class
  133. protected
  134. FObjFile : string;
  135. { writer }
  136. FReader : tobjectreader;
  137. protected
  138. { section }
  139. FData : tobjectdata;
  140. function str2sec(const s:string):tsection;
  141. public
  142. constructor create(const fn:string);
  143. destructor destroy;override;
  144. function initreading:boolean;virtual;
  145. procedure donereading;virtual;
  146. procedure readfromdisk;virtual;
  147. property Data:TObjectData read FData write FData;
  148. property Reader:TObjectReader read FReader;
  149. end;
  150. var
  151. { current object data, used in ag386bin/cpuasm }
  152. objectdata : tobjectdata;
  153. { current object allocator }
  154. objectalloc : tobjectalloc;
  155. { current object writer used }
  156. objectoutput : tobjectoutput;
  157. { globals }
  158. globalsyms : tdictionary;
  159. implementation
  160. uses
  161. cutils,globtype,globals,verbose,fmodule;
  162. {****************************************************************************
  163. tobjectalloc
  164. ****************************************************************************}
  165. constructor tobjectalloc.create;
  166. begin
  167. end;
  168. destructor tobjectalloc.destroy;
  169. begin
  170. end;
  171. procedure tobjectalloc.setsection(sec:tsection);
  172. begin
  173. currsec:=sec;
  174. end;
  175. procedure tobjectalloc.resetsections;
  176. begin
  177. FillChar(secsize,sizeof(secsize),0);
  178. end;
  179. procedure tobjectalloc.sectionalloc(l:longint);
  180. begin
  181. inc(secsize[currsec],l);
  182. end;
  183. procedure tobjectalloc.sectionalign(l:longint);
  184. begin
  185. if (secsize[currsec] mod l)<>0 then
  186. inc(secsize[currsec],l-(secsize[currsec] mod l));
  187. end;
  188. procedure tobjectalloc.staballoc(p:pchar);
  189. begin
  190. inc(secsize[sec_stab]);
  191. if assigned(p) and (p[0]<>#0) then
  192. inc(secsize[sec_stabstr],strlen(p)+1);
  193. end;
  194. function tobjectalloc.sectionsize:longint;
  195. begin
  196. sectionsize:=secsize[currsec];
  197. end;
  198. {****************************************************************************
  199. TSectionOutput
  200. ****************************************************************************}
  201. constructor tobjectsection.create(const Aname:string;Aalign:longint;alloconly:boolean);
  202. begin
  203. name:=Aname;
  204. secsymidx:=0;
  205. addralign:=Aalign;
  206. { data }
  207. datasize:=0;
  208. datapos:=0;
  209. if alloconly then
  210. data:=nil
  211. else
  212. Data:=TDynamicArray.Create(8192);
  213. { position }
  214. mempos:=0;
  215. memsize:=0;
  216. { relocation }
  217. NRelocs:=0;
  218. relocHead:=nil;
  219. relocTail:=@relocHead;
  220. end;
  221. destructor tobjectsection.destroy;
  222. begin
  223. if assigned(Data) then
  224. Data.Free;
  225. end;
  226. function tobjectsection.write(var d;l:longint):longint;
  227. begin
  228. write:=datasize;
  229. if not assigned(Data) then
  230. Internalerror(3334441);
  231. Data.write(d,l);
  232. inc(datasize,l);
  233. end;
  234. function tobjectsection.writestr(const s:string):longint;
  235. begin
  236. writestr:=datasize;
  237. if not assigned(Data) then
  238. Internalerror(3334441);
  239. Data.write(s[1],length(s));
  240. inc(datasize,length(s));
  241. end;
  242. procedure tobjectsection.writealign(l:longint);
  243. var
  244. i : longint;
  245. empty : array[0..63] of char;
  246. begin
  247. { no alignment needed for 0 or 1 }
  248. if l<=1 then
  249. exit;
  250. i:=datasize mod l;
  251. if i>0 then
  252. begin
  253. if assigned(data) then
  254. begin
  255. fillchar(empty,sizeof(empty),0);
  256. Data.write(empty,l-i);
  257. end;
  258. inc(datasize,l-i);
  259. end;
  260. end;
  261. function tobjectsection.aligneddatasize:longint;
  262. begin
  263. aligneddatasize:=align(datasize,addralign);
  264. end;
  265. procedure tobjectsection.alignsection;
  266. begin
  267. writealign(addralign);
  268. end;
  269. procedure tobjectsection.alloc(l:longint);
  270. begin
  271. if assigned(Data) then
  272. Internalerror(3334442);
  273. inc(datasize,l);
  274. end;
  275. procedure tobjectsection.addsymreloc(ofs:longint;p:tasmsymbol;relative:relative_type);
  276. var
  277. r : POutputReloc;
  278. begin
  279. new(r);
  280. reloctail^:=r;
  281. reloctail:=@r^.next;
  282. r^.next:=nil;
  283. r^.address:=ofs;
  284. r^.symbol:=p;
  285. r^.section:=sec_none;
  286. r^.typ:=relative;
  287. inc(nrelocs);
  288. end;
  289. procedure tobjectsection.addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  290. var
  291. r : POutputReloc;
  292. begin
  293. new(r);
  294. reloctail^:=r;
  295. reloctail:=@r^.next;
  296. r^.next:=nil;
  297. r^.address:=ofs;
  298. r^.symbol:=nil;
  299. r^.section:=sec;
  300. r^.typ:=relative;
  301. inc(nrelocs);
  302. end;
  303. {****************************************************************************
  304. tobjectdata
  305. ****************************************************************************}
  306. constructor tobjectdata.create;
  307. begin
  308. { reset }
  309. FillChar(Sects,sizeof(Sects),0);
  310. localsyms:=tdictionary.create;
  311. localsyms.usehash;
  312. end;
  313. destructor tobjectdata.destroy;
  314. var
  315. sec : tsection;
  316. begin
  317. { free memory }
  318. for sec:=low(tsection) to high(tsection) do
  319. if assigned(sects[sec]) then
  320. sects[sec].free;
  321. localsyms.free;
  322. end;
  323. procedure tobjectdata.createsection(sec:tsection);
  324. begin
  325. sects[sec]:=tobjectsection.create(target_asm.secnames[sec],1,(sec=sec_bss));
  326. end;
  327. function tobjectdata.sectionsize(s:tsection):longint;
  328. begin
  329. if assigned(sects[s]) then
  330. sectionsize:=sects[s].datasize
  331. else
  332. sectionsize:=0;
  333. end;
  334. procedure tobjectdata.setsectionsizes(var s:tsecsize);
  335. begin
  336. end;
  337. procedure tobjectdata.defaultsection(sec:tsection);
  338. begin
  339. currsec:=sec;
  340. end;
  341. procedure tobjectdata.writebytes(var data;len:longint);
  342. begin
  343. if not assigned(sects[currsec]) then
  344. createsection(currsec);
  345. sects[currsec].write(data,len);
  346. end;
  347. procedure tobjectdata.alloc(len:longint);
  348. begin
  349. if not assigned(sects[currsec]) then
  350. createsection(currsec);
  351. sects[currsec].alloc(len);
  352. end;
  353. procedure tobjectdata.allocalign(len:longint);
  354. var
  355. modulo : longint;
  356. begin
  357. if not assigned(sects[currsec]) then
  358. createsection(currsec);
  359. modulo:=sects[currsec].datasize mod len;
  360. if modulo > 0 then
  361. sects[currsec].alloc(len-modulo);
  362. end;
  363. procedure tobjectdata.addsymbol(p:tasmsymbol);
  364. begin
  365. if (p.bind=AB_LOCAL) then
  366. localsyms.insert(p)
  367. else
  368. globalsyms.insert(p);
  369. end;
  370. {****************************************************************************
  371. tobjectoutput
  372. ****************************************************************************}
  373. constructor tobjectoutput.create(smart:boolean);
  374. begin
  375. { init writer }
  376. if smart and
  377. not(cs_asm_leave in aktglobalswitches) then
  378. FWriter:=tarobjectwriter.create(current_module.staticlibfilename^)
  379. else
  380. FWriter:=tobjectwriter.create;
  381. end;
  382. destructor tobjectoutput.destroy;
  383. begin
  384. FWriter.free;
  385. end;
  386. procedure tobjectoutput.writetodisk;
  387. begin
  388. end;
  389. function tobjectoutput.initwriting(const fn:string):boolean;
  390. begin
  391. { the data should be set by the real output like coffoutput }
  392. FData:=nil;
  393. initwriting:=FWriter.createfile(fn);
  394. end;
  395. procedure tobjectoutput.donewriting;
  396. begin
  397. { Only write the .o if there are no errors }
  398. if errorcount=0 then
  399. writetodisk;
  400. { close the writer }
  401. FWriter.closefile;
  402. { free data }
  403. FData.free;
  404. FData:=nil;
  405. end;
  406. procedure tobjectoutput.exportsymbol(p:tasmsymbol);
  407. begin
  408. { export globals and common symbols, this is needed
  409. for .a files }
  410. if p.bind in [AB_GLOBAL,AB_COMMON] then
  411. FWriter.writesym(p.name);
  412. end;
  413. {****************************************************************************
  414. tobjectinput
  415. ****************************************************************************}
  416. constructor tobjectinput.create(const fn:string);
  417. begin
  418. FObjfile:=fn;
  419. FData:=nil;
  420. { init reader }
  421. FReader:=tobjectreader.create;
  422. end;
  423. destructor tobjectinput.destroy;
  424. begin
  425. FReader.free;
  426. end;
  427. function tobjectinput.initreading:boolean;
  428. begin
  429. { the data should be set by the real output like coffoutput }
  430. FData:=nil;
  431. { open the reader }
  432. initreading:=FReader.openfile(FObjfile);
  433. end;
  434. procedure tobjectinput.donereading;
  435. begin
  436. { close the writer }
  437. FReader.closefile;
  438. { free data }
  439. FData.free;
  440. FData:=nil;
  441. end;
  442. procedure tobjectinput.readfromdisk;
  443. begin
  444. end;
  445. function tobjectinput.str2sec(const s:string):tsection;
  446. var
  447. t : tsection;
  448. begin
  449. for t:=low(tsection) to high(tsection) do
  450. begin
  451. if (s=target_asm.secnames[t]) then
  452. begin
  453. str2sec:=t;
  454. exit;
  455. end;
  456. end;
  457. str2sec:=sec_none;
  458. end;
  459. end.
  460. {
  461. $Log$
  462. Revision 1.7 2001-04-13 01:22:10 peter
  463. * symtable change to classes
  464. * range check generation and errors fixed, make cycle DEBUG=1 works
  465. * memory leaks fixed
  466. Revision 1.6 2001/03/05 21:40:38 peter
  467. * more things for tcoffobjectinput
  468. Revision 1.5 2000/12/25 00:07:26 peter
  469. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  470. tlinkedlist objects)
  471. Revision 1.4 2000/12/24 12:25:31 peter
  472. + cstreams unit
  473. * dynamicarray object to class
  474. Revision 1.3 2000/12/23 19:59:35 peter
  475. * object to class for ow/og objects
  476. * split objectdata from objectoutput
  477. Revision 1.2 2000/11/13 21:56:07 peter
  478. * removed some virtual from methods
  479. * sectionsize method implemented (fixes lineinfo stabs)
  480. Revision 1.1 2000/11/12 22:20:37 peter
  481. * create generic tobjectsection for binary writers
  482. }