ogbase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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,cobjects,
  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 : pasmsymbol;
  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:pasmsymbol;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. constructor create;
  89. destructor destroy;override;
  90. procedure createsection(sec:tsection);virtual;
  91. procedure defaultsection(sec:tsection);
  92. function sectionsize(s:tsection):longint;
  93. procedure setsectionsizes(var s:tsecsize);virtual;
  94. procedure alloc(len:longint);
  95. procedure allocalign(len:longint);
  96. procedure writebytes(var data;len:longint);
  97. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;abstract;
  98. procedure writesymbol(p:pasmsymbol);virtual;abstract;
  99. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  100. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;nidx,nother,line:longint;reloc:boolean);virtual;abstract;
  101. end;
  102. tobjectalloc = class
  103. currsec : tsection;
  104. secsize : tsecsize;
  105. constructor create;
  106. destructor destroy;override;
  107. procedure setsection(sec:tsection);
  108. function sectionsize:longint;
  109. procedure sectionalloc(l:longint);
  110. procedure sectionalign(l:longint);
  111. procedure staballoc(p:pchar);
  112. procedure resetsections;
  113. end;
  114. tobjectoutput = class
  115. protected
  116. path : pathstr;
  117. ObjFile : string;
  118. { smartlinking }
  119. objsmart : boolean;
  120. place : tcutplace;
  121. SmartFilesCount,
  122. SmartHeaderCount : longint;
  123. { writer }
  124. writer : tobjectwriter;
  125. { section }
  126. data : tobjectdata;
  127. { Writing }
  128. procedure NextSmartName;
  129. protected
  130. procedure writetodisk;virtual;
  131. public
  132. constructor create(smart:boolean);
  133. destructor destroy;override;
  134. function initwriting(Aplace:tcutplace):tobjectdata;virtual;
  135. procedure donewriting;virtual;
  136. procedure exportsymbol(p:pasmsymbol);
  137. end;
  138. var
  139. { current object data, used in ag386bin/cpuasm }
  140. objectdata : tobjectdata;
  141. { current object allocator }
  142. objectalloc : tobjectalloc;
  143. { current object writer used }
  144. objectoutput : tobjectoutput;
  145. implementation
  146. uses
  147. comphook,
  148. cutils,globtype,globals,verbose,fmodule;
  149. {****************************************************************************
  150. tobjectalloc
  151. ****************************************************************************}
  152. constructor tobjectalloc.create;
  153. begin
  154. end;
  155. destructor tobjectalloc.destroy;
  156. begin
  157. end;
  158. procedure tobjectalloc.setsection(sec:tsection);
  159. begin
  160. currsec:=sec;
  161. end;
  162. procedure tobjectalloc.resetsections;
  163. begin
  164. FillChar(secsize,sizeof(secsize),0);
  165. end;
  166. procedure tobjectalloc.sectionalloc(l:longint);
  167. begin
  168. inc(secsize[currsec],l);
  169. end;
  170. procedure tobjectalloc.sectionalign(l:longint);
  171. begin
  172. if (secsize[currsec] mod l)<>0 then
  173. inc(secsize[currsec],l-(secsize[currsec] mod l));
  174. end;
  175. procedure tobjectalloc.staballoc(p:pchar);
  176. begin
  177. inc(secsize[sec_stab]);
  178. if assigned(p) and (p[0]<>#0) then
  179. inc(secsize[sec_stabstr],strlen(p)+1);
  180. end;
  181. function tobjectalloc.sectionsize:longint;
  182. begin
  183. sectionsize:=secsize[currsec];
  184. end;
  185. {****************************************************************************
  186. TSectionOutput
  187. ****************************************************************************}
  188. constructor tobjectsection.create(const Aname:string;Aalign:longint;alloconly:boolean);
  189. begin
  190. name:=Aname;
  191. secsymidx:=0;
  192. addralign:=Aalign;
  193. { data }
  194. datasize:=0;
  195. datapos:=0;
  196. if alloconly then
  197. data:=nil
  198. else
  199. Data:=TDynamicArray.Create(8192);
  200. { position }
  201. mempos:=0;
  202. memsize:=0;
  203. { relocation }
  204. NRelocs:=0;
  205. relocHead:=nil;
  206. relocTail:=@relocHead;
  207. end;
  208. destructor tobjectsection.destroy;
  209. begin
  210. if assigned(Data) then
  211. Data.Free;
  212. end;
  213. function tobjectsection.write(var d;l:longint):longint;
  214. begin
  215. write:=datasize;
  216. if not assigned(Data) then
  217. Internalerror(3334441);
  218. Data.write(d,l);
  219. inc(datasize,l);
  220. end;
  221. function tobjectsection.writestr(const s:string):longint;
  222. begin
  223. writestr:=datasize;
  224. if not assigned(Data) then
  225. Internalerror(3334441);
  226. Data.write(s[1],length(s));
  227. inc(datasize,length(s));
  228. end;
  229. procedure tobjectsection.writealign(l:longint);
  230. var
  231. i : longint;
  232. empty : array[0..63] of char;
  233. begin
  234. { no alignment needed for 0 or 1 }
  235. if l<=1 then
  236. exit;
  237. i:=datasize mod l;
  238. if i>0 then
  239. begin
  240. if assigned(data) then
  241. begin
  242. fillchar(empty,sizeof(empty),0);
  243. Data.write(empty,l-i);
  244. end;
  245. inc(datasize,l-i);
  246. end;
  247. end;
  248. function tobjectsection.aligneddatasize:longint;
  249. begin
  250. aligneddatasize:=align(datasize,addralign);
  251. end;
  252. procedure tobjectsection.alignsection;
  253. begin
  254. writealign(addralign);
  255. end;
  256. procedure tobjectsection.alloc(l:longint);
  257. begin
  258. if assigned(Data) then
  259. Internalerror(3334442);
  260. inc(datasize,l);
  261. end;
  262. procedure tobjectsection.addsymreloc(ofs:longint;p:pasmsymbol;relative:relative_type);
  263. var
  264. r : POutputReloc;
  265. begin
  266. new(r);
  267. reloctail^:=r;
  268. reloctail:=@r^.next;
  269. r^.next:=nil;
  270. r^.address:=ofs;
  271. r^.symbol:=p;
  272. r^.section:=sec_none;
  273. r^.typ:=relative;
  274. inc(nrelocs);
  275. end;
  276. procedure tobjectsection.addsectionreloc(ofs:longint;sec:tsection;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:=nil;
  286. r^.section:=sec;
  287. r^.typ:=relative;
  288. inc(nrelocs);
  289. end;
  290. {****************************************************************************
  291. tobjectdata
  292. ****************************************************************************}
  293. constructor tobjectdata.create;
  294. begin
  295. { reset }
  296. FillChar(Sects,sizeof(Sects),0);
  297. end;
  298. destructor tobjectdata.destroy;
  299. var
  300. sec : tsection;
  301. begin
  302. { free memory }
  303. for sec:=low(tsection) to high(tsection) do
  304. if assigned(sects[sec]) then
  305. sects[sec].free;
  306. end;
  307. procedure tobjectdata.createsection(sec:tsection);
  308. begin
  309. sects[sec]:=tobjectsection.create(target_asm.secnames[sec],1,(sec=sec_bss));
  310. end;
  311. function tobjectdata.sectionsize(s:tsection):longint;
  312. begin
  313. if assigned(sects[s]) then
  314. sectionsize:=sects[s].datasize
  315. else
  316. sectionsize:=0;
  317. end;
  318. procedure tobjectdata.setsectionsizes(var s:tsecsize);
  319. begin
  320. end;
  321. procedure tobjectdata.defaultsection(sec:tsection);
  322. begin
  323. currsec:=sec;
  324. end;
  325. procedure tobjectdata.writebytes(var data;len:longint);
  326. begin
  327. if not assigned(sects[currsec]) then
  328. createsection(currsec);
  329. sects[currsec].write(data,len);
  330. end;
  331. procedure tobjectdata.alloc(len:longint);
  332. begin
  333. if not assigned(sects[currsec]) then
  334. createsection(currsec);
  335. sects[currsec].alloc(len);
  336. end;
  337. procedure tobjectdata.allocalign(len:longint);
  338. var
  339. modulo : longint;
  340. begin
  341. if not assigned(sects[currsec]) then
  342. createsection(currsec);
  343. modulo:=sects[currsec].datasize mod len;
  344. if modulo > 0 then
  345. sects[currsec].alloc(len-modulo);
  346. end;
  347. {****************************************************************************
  348. tobjectoutput
  349. ****************************************************************************}
  350. constructor tobjectoutput.create(smart:boolean);
  351. begin
  352. SmartFilesCount:=0;
  353. SmartHeaderCount:=0;
  354. objsmart:=smart;
  355. objfile:=current_module.objfilename^;
  356. { Which path will be used ? }
  357. if objsmart and
  358. (cs_asm_leave in aktglobalswitches) then
  359. begin
  360. path:=current_module.path^+FixFileName(current_module.modulename^)+target_info.smartext;
  361. {$I-}
  362. mkdir(path);
  363. {$I+}
  364. if ioresult<>0 then;
  365. path:=FixPath(path,false);
  366. end
  367. else
  368. path:=current_module.path^;
  369. { init writer }
  370. if objsmart and
  371. not(cs_asm_leave in aktglobalswitches) then
  372. writer:=tarobjectwriter.create(current_module.staticlibfilename^)
  373. else
  374. writer:=tobjectwriter.create;
  375. end;
  376. destructor tobjectoutput.destroy;
  377. begin
  378. writer.free;
  379. end;
  380. procedure tobjectoutput.NextSmartName;
  381. var
  382. s : string;
  383. begin
  384. inc(SmartFilesCount);
  385. if SmartFilesCount>999999 then
  386. Message(asmw_f_too_many_asm_files);
  387. if (cs_asm_leave in aktglobalswitches) then
  388. s:=current_module.asmprefix^
  389. else
  390. s:=current_module.modulename^;
  391. case place of
  392. cut_begin :
  393. begin
  394. inc(SmartHeaderCount);
  395. s:=s+tostr(SmartHeaderCount)+'h';
  396. end;
  397. cut_normal :
  398. s:=s+tostr(SmartHeaderCount)+'s';
  399. cut_end :
  400. s:=s+tostr(SmartHeaderCount)+'t';
  401. end;
  402. ObjFile:=FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  403. end;
  404. procedure tobjectoutput.writetodisk;
  405. begin
  406. end;
  407. function tobjectoutput.initwriting(Aplace:tcutplace):tobjectdata;
  408. begin
  409. place:=Aplace;
  410. { the data should be set by the real output like coffoutput }
  411. data:=nil;
  412. initwriting:=nil;
  413. { open the writer }
  414. if objsmart then
  415. NextSmartName;
  416. writer.createfile(objfile);
  417. end;
  418. procedure tobjectoutput.donewriting;
  419. begin
  420. { Only write the .o if there are no errors }
  421. if errorcount=0 then
  422. writetodisk;
  423. { close the writer }
  424. writer.closefile;
  425. { free data }
  426. data.free;
  427. data:=nil;
  428. end;
  429. procedure tobjectoutput.exportsymbol(p:pasmsymbol);
  430. begin
  431. { export globals and common symbols, this is needed
  432. for .a files }
  433. if p^.bind in [AB_GLOBAL,AB_COMMON] then
  434. writer.writesym(p^.name);
  435. end;
  436. end.
  437. {
  438. $Log$
  439. Revision 1.5 2000-12-25 00:07:26 peter
  440. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  441. tlinkedlist objects)
  442. Revision 1.4 2000/12/24 12:25:31 peter
  443. + cstreams unit
  444. * dynamicarray object to class
  445. Revision 1.3 2000/12/23 19:59:35 peter
  446. * object to class for ow/og objects
  447. * split objectdata from objectoutput
  448. Revision 1.2 2000/11/13 21:56:07 peter
  449. * removed some virtual from methods
  450. * sectionsize method implemented (fixes lineinfo stabs)
  451. Revision 1.1 2000/11/12 22:20:37 peter
  452. * create generic tobjectsection for binary writers
  453. }