ogbase.pas 16 KB

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