og386cff.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman and Pierre Muller
  4. Contains the 386 binary coff writer
  5. * This code was inspired by the NASM sources
  6. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit og386cff;
  22. {
  23. Notes on COFF:
  24. (0) When I say `standard COFF' below, I mean `COFF as output and
  25. used by DJGPP'. I assume DJGPP gets it right.
  26. (1) Win32 appears to interpret the term `relative relocation'
  27. differently from standard COFF. Standard COFF understands a
  28. relative relocation to mean that during relocation you add the
  29. address of the symbol you're referencing, and subtract the base
  30. address of the section you're in. Win32 COFF, by contrast, seems
  31. to add the address of the symbol and then subtract the address
  32. of THE BYTE AFTER THE RELOCATED DWORD. Hence the two formats are
  33. subtly incompatible.
  34. (2) Win32 doesn't bother putting any flags in the header flags
  35. field (at offset 0x12 into the file).
  36. (3) Win32 uses some extra flags into the section header table:
  37. it defines flags 0x80000000 (writable), 0x40000000 (readable)
  38. and 0x20000000 (executable), and uses them in the expected
  39. combinations. It also defines 0x00100000 through 0x00700000 for
  40. section alignments of 1 through 64 bytes.
  41. (4) Both standard COFF and Win32 COFF seem to use the DWORD
  42. field directly after the section name in the section header
  43. table for something strange: they store what the address of the
  44. section start point _would_ be, if you laid all the sections end
  45. to end starting at zero. Dunno why. Microsoft's documentation
  46. lists this field as "Virtual Size of Section", which doesn't
  47. seem to fit at all. In fact, Win32 even includes non-linked
  48. sections such as .drectve in this calculation.
  49. (5) Standard COFF does something very strange to common
  50. variables: the relocation point for a common variable is as far
  51. _before_ the variable as its size stretches out _after_ it. So
  52. we must fix up common variable references. Win32 seems to be
  53. sensible on this one.
  54. }
  55. interface
  56. uses
  57. cobjects,
  58. systems,cpubase,aasm,og386;
  59. type
  60. preloc = ^treloc;
  61. treloc = packed record
  62. next : preloc;
  63. address : longint;
  64. symbol : pasmsymbol;
  65. section : tsection; { only used if symbol=nil }
  66. relative : relative_type;
  67. end;
  68. psymbol = ^tsymbol;
  69. tsymbol = packed record
  70. name : string[8];
  71. strpos : longint;
  72. section : tsection;
  73. value : longint;
  74. typ : TAsmsymtype;
  75. end;
  76. pcoffsection = ^tcoffsection;
  77. tcoffsection = object
  78. index : tsection;
  79. secidx : longint;
  80. data : PDynamicArray;
  81. size,
  82. fillsize,
  83. mempos,
  84. len,
  85. datapos,
  86. relocpos,
  87. nrelocs,
  88. align,
  89. flags : longint;
  90. relochead : PReloc;
  91. reloctail : ^PReloc;
  92. constructor init(sec:TSection;Aflags:longint);
  93. destructor done;
  94. procedure write(var d;l:longint);
  95. procedure alloc(l:longint);
  96. procedure addsymreloc(ofs:longint;p:pasmsymbol;relative:relative_type);
  97. procedure addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  98. end;
  99. pgenericcoffoutput = ^tgenericcoffoutput;
  100. tgenericcoffoutput = object(tobjectoutput)
  101. win32 : boolean;
  102. sects : array[TSection] of PCoffSection;
  103. strs,
  104. syms : Pdynamicarray;
  105. initsym : longint;
  106. constructor init(smart:boolean);
  107. destructor done;virtual;
  108. procedure initwriting(Aplace:tcutplace);virtual;
  109. procedure donewriting;virtual;
  110. procedure setsectionsizes(var s:tsecsize);virtual;
  111. procedure writebytes(var data;len:longint);virtual;
  112. procedure writealloc(len:longint);virtual;
  113. procedure writealign(len:longint);virtual;
  114. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  115. procedure writesymbol(p:pasmsymbol);virtual;
  116. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  117. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  118. nidx,nother,line:longint;reloc:boolean);virtual;
  119. function text_flags : longint;virtual;
  120. function data_flags : longint;virtual;
  121. function bss_flags : longint;virtual;
  122. function idata_flags : longint;virtual;
  123. function edata_flags : longint;virtual;
  124. private
  125. procedure createsection(sec:tsection);
  126. procedure write_relocs(s:pcoffsection);
  127. procedure write_symbol(const name:string;strpos,value,section,typ,aux:longint);
  128. procedure write_symbols;
  129. procedure writetodisk;
  130. end;
  131. pdjgppcoffoutput = ^tdjgppcoffoutput;
  132. tdjgppcoffoutput = object(tgenericcoffoutput)
  133. constructor init(smart:boolean);
  134. function text_flags : longint;virtual;
  135. function data_flags : longint;virtual;
  136. function bss_flags : longint;virtual;
  137. end;
  138. pwin32coffoutput = ^twin32coffoutput;
  139. twin32coffoutput = object(tgenericcoffoutput)
  140. constructor init(smart:boolean);
  141. function text_flags : longint;virtual;
  142. function data_flags : longint;virtual;
  143. function bss_flags : longint;virtual;
  144. function idata_flags : longint;virtual;
  145. function edata_flags : longint;virtual;
  146. end;
  147. implementation
  148. uses
  149. strings,verbose,
  150. globtype,globals,files;
  151. type
  152. { Structures which are written directly to the output file }
  153. coffheader=packed record
  154. mach : word;
  155. nsects : word;
  156. time : longint;
  157. sympos : longint;
  158. syms : longint;
  159. opthdr : word;
  160. flag : word;
  161. end;
  162. coffsechdr=packed record
  163. name : array[0..7] of char;
  164. vsize : longint;
  165. rvaofs : longint;
  166. datalen : longint;
  167. datapos : longint;
  168. relocpos : longint;
  169. lineno1 : longint;
  170. nrelocs : word;
  171. lineno2 : word;
  172. flags : longint;
  173. end;
  174. coffsectionrec=packed record
  175. len : longint;
  176. nrelocs : word;
  177. empty : array[0..11] of char;
  178. end;
  179. coffreloc=packed record
  180. address : longint;
  181. sym : longint;
  182. relative : word;
  183. end;
  184. coffsymbol=packed record
  185. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  186. strpos : longint;
  187. value : longint;
  188. section : integer;
  189. empty : integer;
  190. typ : byte;
  191. aux : byte;
  192. end;
  193. pcoffstab=^coffstab;
  194. coffstab=packed record
  195. strpos : longint;
  196. ntype : byte;
  197. nother : byte;
  198. ndesc : word;
  199. nvalue : longint;
  200. end;
  201. {****************************************************************************
  202. TSection
  203. ****************************************************************************}
  204. constructor tcoffsection.init(sec:TSection;Aflags:longint);
  205. begin
  206. index:=sec;
  207. secidx:=0;
  208. flags:=AFlags;
  209. { alignment after section }
  210. case sec of
  211. sec_code,
  212. sec_data,
  213. sec_bss :
  214. align:=4;
  215. else
  216. align:=1;
  217. end;
  218. { filled after pass 1 }
  219. size:=0;
  220. fillsize:=0;
  221. mempos:=0;
  222. { pass 2 data }
  223. relocHead:=nil;
  224. relocTail:=@relocHead;
  225. Len:=0;
  226. NRelocs:=0;
  227. if sec=sec_bss then
  228. data:=nil
  229. else
  230. new(Data,Init(1,8192));
  231. end;
  232. destructor tcoffsection.done;
  233. begin
  234. if assigned(Data) then
  235. dispose(Data,done);
  236. end;
  237. procedure tcoffsection.write(var d;l:longint);
  238. begin
  239. if not assigned(Data) then
  240. Internalerror(3334441);
  241. Data^.write(d,l);
  242. inc(len,l);
  243. end;
  244. procedure tcoffsection.alloc(l:longint);
  245. begin
  246. if assigned(Data) then
  247. Internalerror(3334442);
  248. inc(len,l);
  249. end;
  250. procedure tcoffsection.addsymreloc(ofs:longint;p:pasmsymbol;relative:relative_type);
  251. var
  252. r : PReloc;
  253. begin
  254. new(r);
  255. reloctail^:=r;
  256. reloctail:=@r^.next;
  257. r^.next:=nil;
  258. r^.address:=ofs+mempos;
  259. r^.symbol:=p;
  260. r^.section:=sec_none;
  261. r^.relative:=relative;
  262. inc(nrelocs);
  263. end;
  264. procedure tcoffsection.addsectionreloc(ofs:longint;sec:tsection;relative:relative_type);
  265. var
  266. r : PReloc;
  267. begin
  268. new(r);
  269. reloctail^:=r;
  270. reloctail:=@r^.next;
  271. r^.next:=nil;
  272. r^.address:=ofs+mempos;
  273. r^.symbol:=nil;
  274. r^.section:=sec;
  275. r^.relative:=relative;
  276. inc(nrelocs);
  277. end;
  278. {****************************************************************************
  279. Genericcoffoutput
  280. ****************************************************************************}
  281. const
  282. {$ifdef TP}
  283. symbolresize = 50;
  284. strsresize = 200;
  285. {$else}
  286. symbolresize = 200;
  287. strsresize = 8192;
  288. {$endif}
  289. constructor tgenericcoffoutput.init(smart:boolean);
  290. begin
  291. inherited init(smart);
  292. end;
  293. destructor tgenericcoffoutput.done;
  294. begin
  295. inherited done;
  296. end;
  297. procedure tgenericcoffoutput.initwriting(Aplace:tcutplace);
  298. var
  299. s : string;
  300. begin
  301. inherited initwriting(Aplace);
  302. { reset }
  303. initsym:=0;
  304. new(syms,init(sizeof(TSymbol),symbolresize));
  305. new(strs,init(1,strsresize));
  306. FillChar(Sects,sizeof(Sects),0);
  307. { we need at least the following 3 sections }
  308. createsection(sec_code);
  309. createsection(sec_data);
  310. createsection(sec_bss);
  311. if (cs_gdb_lineinfo in aktglobalswitches) or
  312. (cs_debuginfo in aktmoduleswitches) then
  313. begin
  314. createsection(sec_stab);
  315. createsection(sec_stabstr);
  316. writestabs(sec_none,0,nil,0,0,0,false);
  317. { write zero pchar and name together (PM) }
  318. s:=#0+SplitFileName(current_module^.mainsource^)+#0;
  319. sects[sec_stabstr]^.write(s[1],length(s));
  320. end;
  321. end;
  322. procedure tgenericcoffoutput.donewriting;
  323. var
  324. sec : tsection;
  325. begin
  326. { Only write the .o if there are no errors }
  327. if errorcount=0 then
  328. writetodisk;
  329. dispose(syms,done);
  330. dispose(strs,done);
  331. for sec:=low(tsection) to high(tsection) do
  332. if assigned(sects[sec]) then
  333. dispose(sects[sec],done);
  334. inherited donewriting;
  335. end;
  336. function tgenericcoffoutput.text_flags : longint;
  337. begin
  338. text_flags:=0;
  339. end;
  340. function tgenericcoffoutput.data_flags : longint;
  341. begin
  342. data_flags:=0;
  343. end;
  344. function tgenericcoffoutput.bss_flags : longint;
  345. begin
  346. bss_flags:=0;
  347. end;
  348. function tgenericcoffoutput.edata_flags : longint;
  349. begin
  350. edata_flags:=0;
  351. end;
  352. function tgenericcoffoutput.idata_flags : longint;
  353. begin
  354. idata_flags:=0;
  355. end;
  356. procedure tgenericcoffoutput.createsection(sec:TSection);
  357. var
  358. Aflags : longint;
  359. begin
  360. Aflags:=0;
  361. case sec of
  362. sec_code :
  363. Aflags:=text_flags;
  364. sec_data :
  365. Aflags:=data_flags;
  366. sec_bss :
  367. Aflags:=bss_flags;
  368. sec_idata2,
  369. sec_idata4,
  370. sec_idata5,
  371. sec_idata6,
  372. sec_idata7 :
  373. Aflags:=idata_flags;
  374. sec_edata :
  375. Aflags:=edata_flags;
  376. else
  377. Aflags:=0;
  378. end;
  379. sects[sec]:=new(PcoffSection,init(Sec,Aflags));
  380. end;
  381. procedure tgenericcoffoutput.writesymbol(p:pasmsymbol);
  382. var
  383. pos : longint;
  384. sym : tsymbol;
  385. s : string;
  386. begin
  387. { already written ? }
  388. if p^.idx<>-1 then
  389. exit;
  390. { be sure that the section will exists }
  391. if (p^.section<>sec_none) and not(assigned(sects[p^.section])) then
  392. createsection(p^.section);
  393. { symbolname }
  394. pos:=strs^.usedsize+4;
  395. s:=p^.name;
  396. if length(s)>8 then
  397. begin
  398. s:=s+#0;
  399. strs^.write(s[1],length(s));
  400. end
  401. else
  402. pos:=-1;
  403. FillChar(sym,sizeof(sym),0);
  404. sym.strpos:=pos;
  405. if pos=-1 then
  406. sym.name:=s;
  407. sym.value:=p^.size;
  408. sym.typ:=p^.typ;
  409. { if local of global then set the section value to the address
  410. of the symbol }
  411. if p^.typ in [AS_LOCAL,AS_GLOBAL] then
  412. begin
  413. sym.section:=p^.section;
  414. sym.value:=p^.address+sects[p^.section]^.mempos;
  415. end;
  416. { update the asmsymbol index }
  417. p^.idx:=syms^.count;
  418. { store the symbol, but not the local ones (PM) }
  419. if (p^.typ<>AS_LOCAL) or ((copy(s,1,2)<>'.L') and
  420. ((copy(s,1,1)<>'L') or not win32)) then
  421. syms^.write(sym,1);
  422. { make the exported syms known to the objectwriter
  423. (needed for .a generation) }
  424. if (p^.typ=AS_GLOBAL) or
  425. ((p^.typ=AS_EXTERNAL) and (sym.value=p^.size) and (sym.value>0)) then
  426. writer^.writesym(p^.name);
  427. end;
  428. procedure tgenericcoffoutput.writebytes(var data;len:longint);
  429. begin
  430. if not assigned(sects[currsec]) then
  431. createsection(currsec);
  432. sects[currsec]^.write(data,len);
  433. end;
  434. procedure tgenericcoffoutput.writealloc(len:longint);
  435. begin
  436. if not assigned(sects[currsec]) then
  437. createsection(currsec);
  438. sects[currsec]^.alloc(len);
  439. end;
  440. procedure tgenericcoffoutput.writealign(len:longint);
  441. var modulo : longint;
  442. begin
  443. if not assigned(sects[currsec]) then
  444. createsection(currsec);
  445. modulo:=sects[currsec]^.len mod len;
  446. if modulo > 0 then
  447. sects[currsec]^.alloc(len-modulo);
  448. end;
  449. procedure tgenericcoffoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  450. var
  451. symaddr : longint;
  452. begin
  453. if not assigned(sects[currsec]) then
  454. createsection(currsec);
  455. if assigned(p) then
  456. begin
  457. { real address of the symbol }
  458. symaddr:=p^.address;
  459. if p^.section<>sec_none then
  460. inc(symaddr,sects[p^.section]^.mempos);
  461. { no symbol relocation need inside a section }
  462. if p^.section=currsec then
  463. begin
  464. case relative of
  465. relative_false :
  466. begin
  467. sects[currsec]^.addsectionreloc(sects[currsec]^.len,currsec,relative_false);
  468. inc(data,symaddr);
  469. end;
  470. relative_true :
  471. begin
  472. inc(data,symaddr-len-sects[currsec]^.len);
  473. end;
  474. relative_rva :
  475. begin
  476. { don't know if this can happens !! }
  477. { does this work ?? }
  478. sects[currsec]^.addsectionreloc(sects[currsec]^.len,currsec,relative_rva);
  479. inc(data,symaddr);
  480. end;
  481. end;
  482. end
  483. else
  484. begin
  485. writesymbol(p);
  486. if (p^.section<>sec_none) and (relative<>relative_true) then
  487. sects[currsec]^.addsectionreloc(sects[currsec]^.len,p^.section,relative)
  488. else
  489. sects[currsec]^.addsymreloc(sects[currsec]^.len,p,relative);
  490. if not win32 then {seems wrong to me (PM) }
  491. inc(data,symaddr)
  492. else
  493. if (relative<>relative_true) and (p^.section<>sec_none) then
  494. inc(data,symaddr);
  495. if relative=relative_true then
  496. begin
  497. if win32 then
  498. dec(data,len-4)
  499. else
  500. dec(data,len+sects[currsec]^.len);
  501. end;
  502. end;
  503. end;
  504. sects[currsec]^.write(data,len);
  505. end;
  506. procedure tgenericcoffoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc : boolean);
  507. var
  508. stab : coffstab;
  509. s : tsection;
  510. begin
  511. { This is wrong because
  512. sec_none is used only for external bss
  513. if section=sec_none then
  514. s:=currsec
  515. else }
  516. s:=section;
  517. { local var can be at offset -1 !! PM }
  518. if reloc then
  519. begin
  520. if (offset=-1) then
  521. begin
  522. if s=sec_none then
  523. offset:=0
  524. else
  525. offset:=sects[s]^.len;
  526. end;
  527. if (s<>sec_none) then
  528. inc(offset,sects[s]^.mempos);
  529. end;
  530. fillchar(stab,sizeof(coffstab),0);
  531. if assigned(p) and (p[0]<>#0) then
  532. begin
  533. stab.strpos:=sects[sec_stabstr]^.len;
  534. sects[sec_stabstr]^.write(p^,strlen(p)+1);
  535. end;
  536. stab.ntype:=nidx;
  537. stab.ndesc:=line;
  538. stab.nother:=nother;
  539. stab.nvalue:=offset;
  540. sects[sec_stab]^.write(stab,sizeof(stab));
  541. { when the offset is not 0 then write a relocation, take also the
  542. hdrstab into account with the offset }
  543. if reloc then
  544. if DLLSource and RelocSection then
  545. { avoid relocation in the .stab section
  546. because it ends up in the .reloc section instead }
  547. sects[sec_stab]^.addsectionreloc(sects[sec_stab]^.len-4,s,relative_rva)
  548. else
  549. sects[sec_stab]^.addsectionreloc(sects[sec_stab]^.len-4,s,relative_false);
  550. end;
  551. procedure tgenericcoffoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  552. nidx,nother,line:longint;reloc:boolean);
  553. var
  554. stab : coffstab;
  555. s : tsection;
  556. begin
  557. { This is wrong because
  558. sec_none is used only for external bss
  559. if section=sec_none then
  560. s:=currsec
  561. else }
  562. s:=section;
  563. { do not use the size stored in offset field
  564. this is DJGPP specific ! PM }
  565. if win32 then
  566. offset:=0;
  567. { local var can be at offset -1 !! PM }
  568. if reloc then
  569. begin
  570. if (offset=-1) then
  571. begin
  572. if s=sec_none then
  573. offset:=0
  574. else
  575. offset:=sects[s]^.len;
  576. end;
  577. if (s<>sec_none) then
  578. inc(offset,sects[s]^.mempos);
  579. end;
  580. fillchar(stab,sizeof(coffstab),0);
  581. if assigned(p) and (p[0]<>#0) then
  582. begin
  583. stab.strpos:=sects[sec_stabstr]^.len;
  584. sects[sec_stabstr]^.write(p^,strlen(p)+1);
  585. end;
  586. stab.ntype:=nidx;
  587. stab.ndesc:=line;
  588. stab.nother:=nother;
  589. stab.nvalue:=offset;
  590. sects[sec_stab]^.write(stab,sizeof(stab));
  591. { when the offset is not 0 then write a relocation, take also the
  592. hdrstab into account with the offset }
  593. if reloc then
  594. if DLLSource and RelocSection then
  595. { avoid relocation in the .stab section
  596. because it ends up in the .reloc section instead }
  597. sects[sec_stab]^.addsymreloc(sects[sec_stab]^.len-4,ps,relative_rva)
  598. else
  599. sects[sec_stab]^.addsymreloc(sects[sec_stab]^.len-4,ps,relative_false);
  600. end;
  601. procedure tgenericcoffoutput.write_relocs(s:pcoffsection);
  602. var
  603. rel : coffreloc;
  604. hr,r : preloc;
  605. begin
  606. r:=s^.relochead;
  607. while assigned(r) do
  608. begin
  609. rel.address:=r^.address;
  610. if assigned(r^.symbol) then
  611. begin
  612. if (r^.symbol^.typ=AS_LOCAL) then
  613. rel.sym:=2*sects[r^.symbol^.section]^.secidx
  614. else
  615. begin
  616. if r^.symbol^.idx=-1 then
  617. internalerror(4321);
  618. rel.sym:=r^.symbol^.idx+initsym;
  619. end;
  620. end
  621. else if r^.section<>sec_none then
  622. rel.sym:=2*sects[r^.section]^.secidx
  623. else
  624. rel.sym:=0;
  625. case r^.relative of
  626. relative_true : rel.relative:=$14;
  627. relative_false : rel.relative:=$6;
  628. relative_rva : rel.relative:=$7;
  629. end;
  630. writer^.write(rel,sizeof(rel));
  631. { goto next and dispose this reloc }
  632. hr:=r;
  633. r:=r^.next;
  634. dispose(hr);
  635. end;
  636. end;
  637. procedure tgenericcoffoutput.write_symbol(const name:string;strpos,value,section,typ,aux:longint);
  638. var
  639. sym : coffsymbol;
  640. begin
  641. FillChar(sym,sizeof(sym),0);
  642. if strpos=-1 then
  643. move(name[1],sym.name,length(name))
  644. else
  645. sym.strpos:=strpos;
  646. sym.value:=value;
  647. sym.section:=section;
  648. sym.typ:=typ;
  649. sym.aux:=aux;
  650. writer^.write(sym,sizeof(sym));
  651. end;
  652. procedure tgenericcoffoutput.write_symbols;
  653. var
  654. filename : string[18];
  655. sec : tsection;
  656. sectionval,
  657. i : longint;
  658. globalval : byte;
  659. secrec : coffsectionrec;
  660. sym : tsymbol;
  661. begin
  662. { The `.file' record, and the file name auxiliary record. }
  663. write_symbol ('.file', -1, 0, -2, $67, 1);
  664. fillchar(filename,sizeof(filename),0);
  665. filename:=SplitFileName(current_module^.mainsource^);
  666. writer^.write(filename[1],sizeof(filename)-1);
  667. { The section records, with their auxiliaries, also store the
  668. symbol index }
  669. for sec:=low(tsection) to high(tsection) do
  670. if assigned(sects[sec]) then
  671. begin
  672. write_symbol(target_asm.secnames[sec],-1,sects[sec]^.mempos,sects[sec]^.secidx,3,1);
  673. fillchar(secrec,sizeof(secrec),0);
  674. secrec.len:=sects[sec]^.len;
  675. secrec.nrelocs:=sects[sec]^.nrelocs;
  676. writer^.write(secrec,sizeof(secrec));
  677. end;
  678. { The real symbols. }
  679. syms^.seek(0);
  680. for i:=1 to syms^.count do
  681. begin
  682. syms^.read(sym,1);
  683. if sym.typ=AS_LOCAL then
  684. globalval:=3
  685. else
  686. globalval:=2;
  687. if assigned(sects[sym.section]) then
  688. sectionval:=sects[sym.section]^.secidx
  689. else
  690. sectionval:=0;
  691. write_symbol(sym.name,sym.strpos,sym.value,sectionval,globalval,0);
  692. end;
  693. end;
  694. procedure tgenericcoffoutput.setsectionsizes(var s:tsecsize);
  695. var
  696. align,
  697. mempos : longint;
  698. sec : tsection;
  699. begin
  700. { multiply stab with real size }
  701. s[sec_stab]:=s[sec_stab]*sizeof(coffstab);
  702. { if debug then also count header stab }
  703. if (cs_gdb_lineinfo in aktglobalswitches) or
  704. (cs_debuginfo in aktmoduleswitches) then
  705. begin
  706. inc(s[sec_stab],sizeof(coffstab));
  707. inc(s[sec_stabstr],length(SplitFileName(current_module^.mainsource^))+2);
  708. end;
  709. { fix all section }
  710. mempos:=0;
  711. for sec:=low(tsection) to high(tsection) do
  712. begin
  713. if (s[sec]>0) and (not assigned(sects[sec])) then
  714. createsection(sec);
  715. if assigned(sects[sec]) then
  716. begin
  717. sects[sec]^.size:=s[sec];
  718. sects[sec]^.mempos:=mempos;
  719. { calculate the alignment }
  720. align:=sects[sec]^.align;
  721. sects[sec]^.fillsize:=align-(sects[sec]^.size and (align-1));
  722. if sects[sec]^.fillsize=align then
  723. sects[sec]^.fillsize:=0;
  724. { next section position, not for win32 which uses
  725. relative addresses }
  726. if not win32 then
  727. inc(mempos,sects[sec]^.size+sects[sec]^.fillsize);
  728. end;
  729. end;
  730. end;
  731. procedure tgenericcoffoutput.writetodisk;
  732. var
  733. datapos,secidx,
  734. nsects,sympos,i : longint;
  735. gotreloc : boolean;
  736. sec : tsection;
  737. header : coffheader;
  738. sechdr : coffsechdr;
  739. empty : array[0..15] of byte;
  740. begin
  741. { calc amount of sections we have and align sections at 4 bytes }
  742. fillchar(empty,sizeof(empty),0);
  743. nsects:=0;
  744. for sec:=low(tsection) to high(tsection) do
  745. if assigned(sects[sec]) then
  746. begin
  747. {$ifdef EXTDEBUG}
  748. { check if the section is still the same size }
  749. if (sects[sec]^.len<>sects[sec]^.size) then
  750. Comment(V_Warning,'Size of section changed '+
  751. tostr(sects[sec]^.size)+'->'+tostr(sects[sec]^.len)+
  752. ' ['+target_asm.secnames[sec]+']');
  753. {$endif EXTDEBUG}
  754. { fill with zero }
  755. if sects[sec]^.fillsize>0 then
  756. begin
  757. if assigned(sects[sec]^.data) then
  758. sects[sec]^.write(empty,sects[sec]^.fillsize)
  759. else
  760. sects[sec]^.alloc(sects[sec]^.fillsize);
  761. end;
  762. inc(nsects);
  763. end;
  764. { Calculate the filepositions }
  765. datapos:=sizeof(coffheader)+sizeof(coffsechdr)*nsects;
  766. initsym:=2; { 2 for the file }
  767. { sections first }
  768. secidx:=0;
  769. for sec:=low(tsection) to high(tsection) do
  770. if assigned(sects[sec]) then
  771. begin
  772. inc(secidx);
  773. sects[sec]^.secidx:=secidx;
  774. sects[sec]^.datapos:=datapos;
  775. if assigned(sects[sec]^.data) then
  776. inc(datapos,sects[sec]^.len);
  777. inc(initsym,2); { 2 for each section }
  778. end;
  779. { relocs }
  780. gotreloc:=false;
  781. for sec:=low(tsection) to high(tsection) do
  782. if assigned(sects[sec]) then
  783. begin
  784. sects[sec]^.relocpos:=datapos;
  785. inc(datapos,10*sects[sec]^.nrelocs);
  786. if (not gotreloc) and (sects[sec]^.nrelocs>0) then
  787. gotreloc:=true;
  788. end;
  789. { symbols }
  790. sympos:=datapos;
  791. { COFF header }
  792. fillchar(header,sizeof(coffheader),0);
  793. header.mach:=$14c;
  794. header.nsects:=nsects;
  795. header.sympos:=sympos;
  796. header.syms:=syms^.count+initsym;
  797. if gotreloc then
  798. header.flag:=$104
  799. else
  800. header.flag:=$105;
  801. writer^.write(header,sizeof(header));
  802. { Section headers }
  803. for sec:=low(tsection) to high(tsection) do
  804. if assigned(sects[sec]) then
  805. begin
  806. fillchar(sechdr,sizeof(sechdr),0);
  807. move(target_asm.secnames[sec][1],sechdr.name,length(target_asm.secnames[sec]));
  808. if not win32 then
  809. begin
  810. sechdr.rvaofs:=sects[sec]^.mempos;
  811. sechdr.vsize:=sects[sec]^.mempos;
  812. end
  813. else
  814. begin
  815. if sec=sec_bss then
  816. sechdr.vsize:=sects[sec]^.len;
  817. end;
  818. sechdr.datalen:=sects[sec]^.len;
  819. if (sects[sec]^.len>0) and assigned(sects[sec]^.data) then
  820. sechdr.datapos:=sects[sec]^.datapos;
  821. sechdr.relocpos:=sects[sec]^.relocpos;
  822. sechdr.nrelocs:=sects[sec]^.nrelocs;
  823. sechdr.flags:=sects[sec]^.flags;
  824. writer^.write(sechdr,sizeof(sechdr));
  825. end;
  826. { Sections }
  827. for sec:=low(tsection) to high(tsection) do
  828. if assigned(sects[sec]) and
  829. assigned(sects[sec]^.data) then
  830. begin
  831. { For the stab section we need an HdrSym which can now be
  832. calculated more easily }
  833. if sec=sec_stab then
  834. begin
  835. pcoffstab(sects[sec_stab]^.data^.data)^.nvalue:=sects[sec_stabstr]^.len;
  836. pcoffstab(sects[sec_stab]^.data^.data)^.strpos:=1;
  837. pcoffstab(sects[sec_stab]^.data^.data)^.ndesc:=
  838. (sects[sec_stab]^.len div sizeof(coffstab))-1{+1 according to gas output PM};
  839. end;
  840. writer^.write(sects[sec]^.data^.data^,sects[sec]^.data^.usedsize);
  841. end;
  842. { Relocs }
  843. for sec:=low(tsection) to high(tsection) do
  844. if assigned(sects[sec]) then
  845. write_relocs(sects[sec]);
  846. { Symbols }
  847. write_symbols;
  848. { Strings }
  849. i:=strs^.usedsize+4;
  850. writer^.write(i,4);
  851. writer^.write(strs^.data^,strs^.usedsize);
  852. end;
  853. {****************************************************************************
  854. DJGppcoffoutput
  855. ****************************************************************************}
  856. constructor tdjgppcoffoutput.init(smart:boolean);
  857. begin
  858. inherited init(smart);
  859. win32:=false;
  860. end;
  861. function tdjgppcoffoutput.text_flags : longint;
  862. begin
  863. text_flags:=$20;
  864. end;
  865. function tdjgppcoffoutput.data_flags : longint;
  866. begin
  867. data_flags:=$40;
  868. end;
  869. function tdjgppcoffoutput.bss_flags : longint;
  870. begin
  871. bss_flags:=$80;
  872. end;
  873. {****************************************************************************
  874. Win32coffoutput
  875. ****************************************************************************}
  876. constructor twin32coffoutput.init(smart:boolean);
  877. begin
  878. inherited init(smart);
  879. win32:=true;
  880. end;
  881. function twin32coffoutput.text_flags : longint;
  882. begin
  883. text_flags:=$60000020; { same as as 2.9.1 }
  884. end;
  885. function twin32coffoutput.data_flags : longint;
  886. begin
  887. data_flags:=$c0300040;
  888. end;
  889. function twin32coffoutput.bss_flags : longint;
  890. begin
  891. bss_flags:=$c0300080;
  892. end;
  893. function twin32coffoutput.edata_flags : longint;
  894. begin
  895. edata_flags:=$c0300040;
  896. end;
  897. function twin32coffoutput.idata_flags : longint;
  898. begin
  899. idata_flags:=$40000000;
  900. end;
  901. end.
  902. {
  903. $Log$
  904. Revision 1.23 2000-04-12 12:42:29 pierre
  905. * fix the -g-l option
  906. Revision 1.22 2000/03/10 16:05:28 pierre
  907. * check that symbol is in object
  908. Revision 1.21 2000/03/10 09:15:54 pierre
  909. * rva relocation bug fixed
  910. Revision 1.20 2000/03/09 14:29:47 pierre
  911. * fix for the stab section size changes with smartlinking
  912. Revision 1.19 2000/02/09 13:22:54 peter
  913. * log truncated
  914. Revision 1.18 2000/01/12 10:38:18 peter
  915. * smartlinking fixes for binary writer
  916. * release alignreg code and moved instruction writing align to cpuasm,
  917. but it doesn't use the specified register yet
  918. Revision 1.17 2000/01/07 01:14:27 peter
  919. * updated copyright to 2000
  920. Revision 1.16 1999/12/20 22:29:26 pierre
  921. * relocation with debug info in rva (only with internal compiler)
  922. Revision 1.15 1999/11/30 10:40:43 peter
  923. + ttype, tsymlist
  924. Revision 1.14 1999/11/06 14:34:21 peter
  925. * truncated log to 20 revs
  926. Revision 1.13 1999/11/02 15:06:57 peter
  927. * import library fixes for win32
  928. * alignment works again
  929. Revision 1.12 1999/08/16 15:35:25 pierre
  930. * fix for DLL relocation problems
  931. * external bss vars had wrong stabs for pecoff
  932. + -WB11000000 to specify default image base, allows to
  933. load several DLLs with debugging info included
  934. (relocatable DLL are stripped because the relocation
  935. of the .Stab section is misplaced by ldw)
  936. Revision 1.11 1999/08/11 17:17:38 peter
  937. * fixed rva writting for section relocs
  938. * fixed section flags for edata and idata
  939. Revision 1.10 1999/08/04 00:23:05 florian
  940. * renamed i386asm and i386base to cpuasm and cpubase
  941. }