ogbase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. texesectioninfo = record
  59. available : boolean;
  60. datasize,
  61. datapos,
  62. memsize,
  63. mempos : longint;
  64. flags : longint;
  65. end;
  66. tobjectsection = class
  67. name : string[32];
  68. secsymidx : longint; { index for the section in symtab }
  69. addralign : longint;
  70. { size of the data and in the file }
  71. data : TDynamicArray;
  72. datasize : longint;
  73. datapos : longint;
  74. { size and position in memory, set by setsectionsize }
  75. memsize,
  76. mempos : longint;
  77. { relocation }
  78. nrelocs : longint;
  79. relochead : POutputReloc;
  80. reloctail : ^POutputReloc;
  81. constructor create(const Aname:string;Aalign:longint;alloconly:boolean);
  82. destructor destroy;override;
  83. function write(var d;l:longint):longint;
  84. function writestr(const s:string):longint;
  85. procedure writealign(l:longint);
  86. function aligneddatasize:longint;
  87. procedure alignsection;
  88. procedure alloc(l:longint);
  89. procedure addsymreloc(ofs:longint;p:tasmsymbol;relative:relative_type);
  90. procedure addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  91. procedure fixuprelocs;
  92. end;
  93. tobjectdata = class(tlinkedlistitem)
  94. { section }
  95. currsec : tsection;
  96. sects : array[TSection] of tobjectsection;
  97. localsyms : tdictionary;
  98. constructor create;
  99. destructor destroy;override;
  100. procedure createsection(sec:tsection);virtual;
  101. procedure defaultsection(sec:tsection);
  102. function sectionsize(s:tsection):longint;
  103. function currsectionsize:longint;
  104. procedure setsectionsizes(var s:tsecsize);virtual;
  105. procedure alloc(len:longint);
  106. procedure allocalign(len:longint);
  107. procedure writebytes(var data;len:longint);
  108. procedure writereloc(data,len:longint;p:tasmsymbol;relative:relative_type);virtual;abstract;
  109. procedure writesymbol(p:tasmsymbol);virtual;abstract;
  110. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  111. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:tasmsymbol;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  112. procedure addsymbol(p:tasmsymbol);
  113. end;
  114. tobjectalloc = class
  115. currsec : tsection;
  116. secsize : tsecsize;
  117. constructor create;
  118. destructor destroy;override;
  119. procedure setsection(sec:tsection);
  120. function sectionsize:longint;
  121. procedure sectionalloc(l:longint);
  122. procedure sectionalign(l:longint);
  123. procedure staballoc(p:pchar);
  124. procedure resetsections;
  125. end;
  126. tobjectoutput = class
  127. protected
  128. { writer }
  129. FWriter : tobjectwriter;
  130. { section }
  131. FData : tobjectdata;
  132. procedure writetodisk;virtual;
  133. public
  134. constructor create(smart:boolean);
  135. destructor destroy;override;
  136. function initwriting(const fn:string):boolean;virtual;
  137. procedure donewriting;virtual;
  138. procedure exportsymbol(p:tasmsymbol);
  139. property Data:TObjectData read FData write FData;
  140. property Writer:TObjectWriter read FWriter;
  141. end;
  142. tobjectinput = class
  143. protected
  144. FObjFile : string;
  145. { writer }
  146. FReader : tobjectreader;
  147. protected
  148. { section }
  149. FData : tobjectdata;
  150. function str2sec(const s:string):tsection;
  151. public
  152. constructor create(const fn:string);
  153. destructor destroy;override;
  154. function initreading:boolean;virtual;
  155. procedure donereading;virtual;
  156. procedure readfromdisk;virtual;
  157. property Data:TObjectData read FData write FData;
  158. property Reader:TObjectReader read FReader;
  159. end;
  160. var
  161. { current object data, used in ag386bin/cpuasm }
  162. objectdata : tobjectdata;
  163. { current object allocator }
  164. objectalloc : tobjectalloc;
  165. { current object writer used }
  166. objectoutput : tobjectoutput;
  167. { globals }
  168. externalsyms : tsinglelist;
  169. globalsyms : tdictionary;
  170. { list of all data of the object files to link }
  171. objdatasections : array[tsection] of texesectioninfo;
  172. objdatalist : tlinkedlist;
  173. implementation
  174. uses
  175. cutils,globtype,globals,verbose,fmodule;
  176. {****************************************************************************
  177. tobjectalloc
  178. ****************************************************************************}
  179. constructor tobjectalloc.create;
  180. begin
  181. end;
  182. destructor tobjectalloc.destroy;
  183. begin
  184. end;
  185. procedure tobjectalloc.setsection(sec:tsection);
  186. begin
  187. currsec:=sec;
  188. end;
  189. procedure tobjectalloc.resetsections;
  190. begin
  191. FillChar(secsize,sizeof(secsize),0);
  192. end;
  193. procedure tobjectalloc.sectionalloc(l:longint);
  194. begin
  195. inc(secsize[currsec],l);
  196. end;
  197. procedure tobjectalloc.sectionalign(l:longint);
  198. begin
  199. if (secsize[currsec] mod l)<>0 then
  200. inc(secsize[currsec],l-(secsize[currsec] mod l));
  201. end;
  202. procedure tobjectalloc.staballoc(p:pchar);
  203. begin
  204. inc(secsize[sec_stab]);
  205. if assigned(p) and (p[0]<>#0) then
  206. inc(secsize[sec_stabstr],strlen(p)+1);
  207. end;
  208. function tobjectalloc.sectionsize:longint;
  209. begin
  210. sectionsize:=secsize[currsec];
  211. end;
  212. {****************************************************************************
  213. TSectionOutput
  214. ****************************************************************************}
  215. constructor tobjectsection.create(const Aname:string;Aalign:longint;alloconly:boolean);
  216. begin
  217. name:=Aname;
  218. secsymidx:=0;
  219. addralign:=Aalign;
  220. { data }
  221. datasize:=0;
  222. datapos:=0;
  223. if alloconly then
  224. data:=nil
  225. else
  226. Data:=TDynamicArray.Create(8192);
  227. { position }
  228. mempos:=0;
  229. memsize:=0;
  230. { relocation }
  231. NRelocs:=0;
  232. relocHead:=nil;
  233. relocTail:=@relocHead;
  234. end;
  235. destructor tobjectsection.destroy;
  236. begin
  237. if assigned(Data) then
  238. Data.Free;
  239. end;
  240. function tobjectsection.write(var d;l:longint):longint;
  241. begin
  242. write:=datasize;
  243. if not assigned(Data) then
  244. Internalerror(3334441);
  245. Data.write(d,l);
  246. inc(datasize,l);
  247. end;
  248. function tobjectsection.writestr(const s:string):longint;
  249. begin
  250. writestr:=datasize;
  251. if not assigned(Data) then
  252. Internalerror(3334441);
  253. Data.write(s[1],length(s));
  254. inc(datasize,length(s));
  255. end;
  256. procedure tobjectsection.writealign(l:longint);
  257. var
  258. i : longint;
  259. empty : array[0..63] of char;
  260. begin
  261. { no alignment needed for 0 or 1 }
  262. if l<=1 then
  263. exit;
  264. i:=datasize mod l;
  265. if i>0 then
  266. begin
  267. if assigned(data) then
  268. begin
  269. fillchar(empty,sizeof(empty),0);
  270. Data.write(empty,l-i);
  271. end;
  272. inc(datasize,l-i);
  273. end;
  274. end;
  275. function tobjectsection.aligneddatasize:longint;
  276. begin
  277. aligneddatasize:=align(datasize,addralign);
  278. end;
  279. procedure tobjectsection.alignsection;
  280. begin
  281. writealign(addralign);
  282. end;
  283. procedure tobjectsection.alloc(l:longint);
  284. begin
  285. if assigned(Data) then
  286. Internalerror(3334442);
  287. inc(datasize,l);
  288. end;
  289. procedure tobjectsection.addsymreloc(ofs:longint;p:tasmsymbol;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:=p;
  299. r^.section:=sec_none;
  300. r^.typ:=relative;
  301. inc(nrelocs);
  302. end;
  303. procedure tobjectsection.addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  304. var
  305. r : POutputReloc;
  306. begin
  307. new(r);
  308. reloctail^:=r;
  309. reloctail:=@r^.next;
  310. r^.next:=nil;
  311. r^.address:=ofs;
  312. r^.symbol:=nil;
  313. r^.section:=sec;
  314. r^.typ:=relative;
  315. inc(nrelocs);
  316. end;
  317. procedure tobjectsection.fixuprelocs;
  318. var
  319. hr,r : poutputreloc;
  320. address,
  321. relocval : longint;
  322. begin
  323. r:=relochead;
  324. while assigned(r) do
  325. begin
  326. if assigned(r^.symbol) then
  327. relocval:=r^.symbol.address
  328. else
  329. relocval:=r^.address;
  330. case r^.typ of
  331. relative_true :
  332. begin
  333. data.Seek(r^.address);
  334. data.Read(address,4);
  335. data.Seek(r^.address);
  336. inc(address,relocval);
  337. end;
  338. relative_false :
  339. address:=relocval;
  340. relative_rva :
  341. address:=relocval;
  342. end;
  343. data.Write(address,4);
  344. { goto next reloc }
  345. r:=r^.next;
  346. end;
  347. end;
  348. {****************************************************************************
  349. tobjectdata
  350. ****************************************************************************}
  351. constructor tobjectdata.create;
  352. begin
  353. inherited create;
  354. { reset }
  355. FillChar(Sects,sizeof(Sects),0);
  356. localsyms:=tdictionary.create;
  357. localsyms.usehash;
  358. end;
  359. destructor tobjectdata.destroy;
  360. var
  361. sec : tsection;
  362. begin
  363. { free memory }
  364. for sec:=low(tsection) to high(tsection) do
  365. if assigned(sects[sec]) then
  366. sects[sec].free;
  367. localsyms.free;
  368. end;
  369. procedure tobjectdata.createsection(sec:tsection);
  370. begin
  371. sects[sec]:=tobjectsection.create(target_asm.secnames[sec],1,(sec=sec_bss));
  372. end;
  373. function tobjectdata.sectionsize(s:tsection):longint;
  374. begin
  375. if assigned(sects[s]) then
  376. sectionsize:=sects[s].datasize
  377. else
  378. sectionsize:=0;
  379. end;
  380. function tobjectdata.currsectionsize:longint;
  381. begin
  382. if assigned(sects[currsec]) then
  383. currsectionsize:=sects[currsec].datasize
  384. else
  385. currsectionsize:=0;
  386. end;
  387. procedure tobjectdata.setsectionsizes(var s:tsecsize);
  388. begin
  389. end;
  390. procedure tobjectdata.defaultsection(sec:tsection);
  391. begin
  392. currsec:=sec;
  393. end;
  394. procedure tobjectdata.writebytes(var data;len:longint);
  395. begin
  396. if not assigned(sects[currsec]) then
  397. createsection(currsec);
  398. sects[currsec].write(data,len);
  399. end;
  400. procedure tobjectdata.alloc(len:longint);
  401. begin
  402. if not assigned(sects[currsec]) then
  403. createsection(currsec);
  404. sects[currsec].alloc(len);
  405. end;
  406. procedure tobjectdata.allocalign(len:longint);
  407. var
  408. modulo : longint;
  409. begin
  410. if not assigned(sects[currsec]) then
  411. createsection(currsec);
  412. modulo:=sects[currsec].datasize mod len;
  413. if modulo > 0 then
  414. sects[currsec].alloc(len-modulo);
  415. end;
  416. procedure tobjectdata.addsymbol(p:tasmsymbol);
  417. begin
  418. if (p.bind=AB_LOCAL) then
  419. localsyms.insert(p)
  420. else
  421. globalsyms.insert(p);
  422. end;
  423. {****************************************************************************
  424. tobjectoutput
  425. ****************************************************************************}
  426. constructor tobjectoutput.create(smart:boolean);
  427. begin
  428. { init writer }
  429. if smart and
  430. not(cs_asm_leave in aktglobalswitches) then
  431. FWriter:=tarobjectwriter.create(current_module.staticlibfilename^)
  432. else
  433. FWriter:=tobjectwriter.create;
  434. end;
  435. destructor tobjectoutput.destroy;
  436. begin
  437. FWriter.free;
  438. end;
  439. procedure tobjectoutput.writetodisk;
  440. begin
  441. end;
  442. function tobjectoutput.initwriting(const fn:string):boolean;
  443. begin
  444. { the data should be set by the real output like coffoutput }
  445. FData:=nil;
  446. initwriting:=FWriter.createfile(fn);
  447. end;
  448. procedure tobjectoutput.donewriting;
  449. begin
  450. { Only write the .o if there are no errors }
  451. if errorcount=0 then
  452. writetodisk;
  453. { close the writer }
  454. FWriter.closefile;
  455. { free data }
  456. FData.free;
  457. FData:=nil;
  458. end;
  459. procedure tobjectoutput.exportsymbol(p:tasmsymbol);
  460. begin
  461. { export globals and common symbols, this is needed
  462. for .a files }
  463. if p.bind in [AB_GLOBAL,AB_COMMON] then
  464. FWriter.writesym(p.name);
  465. end;
  466. {****************************************************************************
  467. tobjectinput
  468. ****************************************************************************}
  469. constructor tobjectinput.create(const fn:string);
  470. begin
  471. FObjfile:=fn;
  472. FData:=nil;
  473. { init reader }
  474. FReader:=tobjectreader.create;
  475. end;
  476. destructor tobjectinput.destroy;
  477. begin
  478. FReader.free;
  479. end;
  480. function tobjectinput.initreading:boolean;
  481. begin
  482. { the data should be set by the real output like coffoutput }
  483. FData:=nil;
  484. { open the reader }
  485. initreading:=FReader.openfile(FObjfile);
  486. end;
  487. procedure tobjectinput.donereading;
  488. begin
  489. { close the writer }
  490. FReader.closefile;
  491. { free data }
  492. FData.free;
  493. FData:=nil;
  494. end;
  495. procedure tobjectinput.readfromdisk;
  496. begin
  497. end;
  498. function tobjectinput.str2sec(const s:string):tsection;
  499. var
  500. t : tsection;
  501. begin
  502. for t:=low(tsection) to high(tsection) do
  503. begin
  504. if (s=target_asm.secnames[t]) then
  505. begin
  506. str2sec:=t;
  507. exit;
  508. end;
  509. end;
  510. str2sec:=sec_none;
  511. end;
  512. end.
  513. {
  514. $Log$
  515. Revision 1.11 2002-05-18 13:34:10 peter
  516. * readded missing revisions
  517. Revision 1.9 2002/05/14 19:34:43 peter
  518. * removed old logs and updated copyright year
  519. }