ag386bin.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements an binary assembler output class
  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 ag386bin;
  19. {$i defines.inc}
  20. {$define MULTIPASS}
  21. interface
  22. uses
  23. cpubase,cobjects,aasm,fmodule,finput,assemble;
  24. type
  25. togtype=(og_none,og_dbg,og_coff,og_pecoff,og_elf);
  26. pi386binasmlist=^ti386binasmlist;
  27. ti386binasmlist=object
  28. SmartAsm : boolean;
  29. constructor init(t:togtype;smart:boolean);
  30. destructor done;
  31. procedure WriteBin;
  32. private
  33. { the aasmoutput lists that need to be processed }
  34. lists : byte;
  35. list : array[1..maxoutputlists] of paasmoutput;
  36. { current processing }
  37. currlistidx : byte;
  38. currlist : paasmoutput;
  39. currpass : byte;
  40. {$ifdef GDB}
  41. n_line : byte; { different types of source lines }
  42. linecount,
  43. includecount : longint;
  44. funcname : pasmsymbol;
  45. stabslastfileinfo : tfileposinfo;
  46. procedure convertstabs(p:pchar);
  47. procedure emitlineinfostabs(nidx,line : longint);
  48. procedure emitstabs(s:string);
  49. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  50. procedure StartFileLineInfo;
  51. procedure EndFileLineInfo;
  52. {$endif}
  53. function MaybeNextList(var hp:pai):boolean;
  54. function TreePass0(hp:pai):pai;
  55. function TreePass1(hp:pai):pai;
  56. function TreePass2(hp:pai):pai;
  57. procedure writetree;
  58. procedure writetreesmart;
  59. end;
  60. implementation
  61. uses
  62. {$ifdef delphi}
  63. sysutils,
  64. {$else}
  65. strings,
  66. {$endif}
  67. cutils,globtype,globals,systems,verbose,
  68. cpuasm,
  69. {$ifdef GDB}
  70. gdb,
  71. {$endif}
  72. ogbase,
  73. ogcoff,ogelf;
  74. {$ifdef GDB}
  75. procedure ti386binasmlist.convertstabs(p:pchar);
  76. var
  77. ofs,
  78. nidx,nother,ii,i,line,j : longint;
  79. code : integer;
  80. hp : pchar;
  81. reloc : boolean;
  82. sec : tsection;
  83. ps : pasmsymbol;
  84. s : string;
  85. begin
  86. ofs:=0;
  87. reloc:=true;
  88. ps:=nil;
  89. sec:=sec_none;
  90. if p[0]='"' then
  91. begin
  92. i:=1;
  93. { we can have \" inside the string !! PM }
  94. while not ((p[i]='"') and (p[i-1]<>'\')) do
  95. inc(i);
  96. p[i]:=#0;
  97. ii:=i;
  98. hp:=@p[1];
  99. s:=StrPas(@P[i+2]);
  100. end
  101. else
  102. begin
  103. hp:=nil;
  104. s:=StrPas(P);
  105. i:=-2; {needed below (PM) }
  106. end;
  107. { When in pass 1 then only alloc and leave }
  108. if currpass=1 then
  109. begin
  110. objectalloc^.staballoc(hp);
  111. if assigned(hp) then
  112. p[i]:='"';
  113. exit;
  114. end;
  115. { Parse the rest of the stabs }
  116. if s='' then
  117. internalerror(33000);
  118. j:=pos(',',s);
  119. if j=0 then
  120. internalerror(33001);
  121. Val(Copy(s,1,j-1),nidx,code);
  122. if code<>0 then
  123. internalerror(33002);
  124. i:=i+2+j;
  125. Delete(s,1,j);
  126. j:=pos(',',s);
  127. if (j=0) then
  128. internalerror(33003);
  129. Val(Copy(s,1,j-1),nother,code);
  130. if code<>0 then
  131. internalerror(33004);
  132. i:=i+j;
  133. Delete(s,1,j);
  134. j:=pos(',',s);
  135. if j=0 then
  136. begin
  137. j:=256;
  138. ofs:=-1;
  139. end;
  140. Val(Copy(s,1,j-1),line,code);
  141. if code<>0 then
  142. internalerror(33005);
  143. if ofs=0 then
  144. begin
  145. Delete(s,1,j);
  146. i:=i+j;
  147. Val(s,ofs,code);
  148. if code=0 then
  149. reloc:=false
  150. else
  151. begin
  152. ofs:=0;
  153. s:=strpas(@p[i]);
  154. { handle asmsymbol or
  155. asmsymbol - asmsymbol }
  156. j:=pos(' ',s);
  157. if j=0 then
  158. j:=pos('-',s);
  159. { single asmsymbol }
  160. if j=0 then
  161. j:=256;
  162. { the symbol can be external
  163. so we must use newasmsymbol and
  164. not getasmsymbol !! PM }
  165. ps:=newasmsymbol(copy(s,1,j-1));
  166. if not assigned(ps) then
  167. internalerror(33006)
  168. else
  169. begin
  170. sec:=ps^.section;
  171. ofs:=ps^.address;
  172. reloc:=true;
  173. UsedAsmSymbolListInsert(ps);
  174. end;
  175. if j<256 then
  176. begin
  177. i:=i+j;
  178. s:=strpas(@p[i]);
  179. if (s<>'') and (s[1]=' ') then
  180. begin
  181. j:=0;
  182. while (s[j+1]=' ') do
  183. inc(j);
  184. i:=i+j;
  185. s:=strpas(@p[i]);
  186. end;
  187. ps:=getasmsymbol(s);
  188. if not assigned(ps) then
  189. internalerror(33007)
  190. else
  191. begin
  192. if ps^.section<>sec then
  193. internalerror(33008);
  194. ofs:=ofs-ps^.address;
  195. reloc:=false;
  196. UsedAsmSymbolListInsert(ps);
  197. end;
  198. end;
  199. end;
  200. end;
  201. { external bss need speical handling (PM) }
  202. if assigned(ps) and (ps^.section=sec_none) then
  203. begin
  204. if currpass=2 then
  205. objectoutput^.writesymbol(ps);
  206. objectoutput^.WriteSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  207. end
  208. else
  209. objectoutput^.WriteStabs(sec,ofs,hp,nidx,nother,line,reloc);
  210. if assigned(hp) then
  211. p[ii]:='"';
  212. end;
  213. procedure ti386binasmlist.emitlineinfostabs(nidx,line : longint);
  214. var
  215. sec : tsection;
  216. begin
  217. if currpass=1 then
  218. begin
  219. objectalloc^.staballoc(nil);
  220. exit;
  221. end;
  222. if (nidx=n_textline) and assigned(funcname) and
  223. (target_os.use_function_relative_addresses) then
  224. objectoutput^.WriteStabs(sec_code,objectoutput^.sectionsize(sec_code)-funcname^.address,
  225. nil,nidx,0,line,false)
  226. else
  227. begin
  228. if nidx=n_textline then
  229. sec:=sec_code
  230. else if nidx=n_dataline then
  231. sec:=sec_data
  232. else
  233. sec:=sec_bss;
  234. objectoutput^.WriteStabs(sec,objectoutput^.sectionsize(sec),
  235. nil,nidx,0,line,true);
  236. end;
  237. end;
  238. procedure ti386binasmlist.emitstabs(s:string);
  239. begin
  240. s:=s+#0;
  241. ConvertStabs(@s[1]);
  242. end;
  243. procedure ti386binasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  244. var
  245. curr_n : byte;
  246. hp : pasmsymbol;
  247. infile : pinputfile;
  248. begin
  249. if not ((cs_debuginfo in aktmoduleswitches) or
  250. (cs_gdb_lineinfo in aktglobalswitches)) then
  251. exit;
  252. { file changed ? (must be before line info) }
  253. if (fileinfo.fileindex<>0) and
  254. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  255. begin
  256. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  257. if includecount=0 then
  258. curr_n:=n_sourcefile
  259. else
  260. curr_n:=n_includefile;
  261. { get symbol for this includefile }
  262. hp:=newasmsymboltype('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  263. if currpass=1 then
  264. begin
  265. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  266. UsedAsmSymbolListInsert(hp);
  267. end
  268. else
  269. objectoutput^.writesymbol(hp);
  270. { emit stabs }
  271. if (infile^.path^<>'') then
  272. EmitStabs('"'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+tostr(curr_n)+
  273. ',0,0,Ltext'+ToStr(IncludeCount));
  274. EmitStabs('"'+lower(FixFileName(infile^.name^))+'",'+tostr(curr_n)+
  275. ',0,0,Ltext'+ToStr(IncludeCount));
  276. inc(includecount);
  277. end;
  278. { line changed ? }
  279. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  280. emitlineinfostabs(n_line,fileinfo.line);
  281. stabslastfileinfo:=fileinfo;
  282. end;
  283. procedure ti386binasmlist.StartFileLineInfo;
  284. var
  285. fileinfo : tfileposinfo;
  286. begin
  287. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  288. n_line:=n_textline;
  289. funcname:=nil;
  290. linecount:=1;
  291. includecount:=0;
  292. fileinfo.fileindex:=1;
  293. fileinfo.line:=1;
  294. WriteFileLineInfo(fileinfo);
  295. end;
  296. procedure ti386binasmlist.EndFileLineInfo;
  297. var
  298. hp : pasmsymbol;
  299. store_sec : tsection;
  300. begin
  301. if not ((cs_debuginfo in aktmoduleswitches) or
  302. (cs_gdb_lineinfo in aktglobalswitches)) then
  303. exit;
  304. store_sec:=objectalloc^.currsec;
  305. objectalloc^.setsection(sec_code);
  306. hp:=newasmsymboltype('Letext',AB_LOCAL,AT_FUNCTION);
  307. if currpass=1 then
  308. begin
  309. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  310. UsedAsmSymbolListInsert(hp);
  311. end
  312. else
  313. objectoutput^.writesymbol(hp);
  314. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  315. objectalloc^.setsection(store_sec);
  316. end;
  317. {$endif GDB}
  318. function ti386binasmlist.MaybeNextList(var hp:pai):boolean;
  319. begin
  320. { maybe end of list }
  321. while not assigned(hp) do
  322. begin
  323. if currlistidx<lists then
  324. begin
  325. inc(currlistidx);
  326. currlist:=list[currlistidx];
  327. hp:=pai(currlist^.first);
  328. end
  329. else
  330. begin
  331. MaybeNextList:=false;
  332. exit;
  333. end;
  334. end;
  335. MaybeNextList:=true;
  336. end;
  337. function ti386binasmlist.TreePass0(hp:pai):pai;
  338. var
  339. l : longint;
  340. begin
  341. while assigned(hp) do
  342. begin
  343. case hp^.typ of
  344. ait_align :
  345. begin
  346. { always use the maximum fillsize in this pass to avoid possible
  347. short jumps to become out of range }
  348. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype;
  349. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  350. end;
  351. ait_datablock :
  352. begin
  353. if not SmartAsm then
  354. begin
  355. if not pai_datablock(hp)^.is_global then
  356. begin
  357. l:=pai_datablock(hp)^.size;
  358. if l>2 then
  359. objectalloc^.sectionalign(4)
  360. else if l>1 then
  361. objectalloc^.sectionalign(2);
  362. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  363. end;
  364. end
  365. else
  366. begin
  367. l:=pai_datablock(hp)^.size;
  368. if l>2 then
  369. objectalloc^.sectionalign(4)
  370. else if l>1 then
  371. objectalloc^.sectionalign(2);
  372. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  373. end;
  374. end;
  375. ait_const_32bit :
  376. objectalloc^.sectionalloc(4);
  377. ait_const_16bit :
  378. objectalloc^.sectionalloc(2);
  379. ait_const_8bit :
  380. objectalloc^.sectionalloc(1);
  381. ait_real_80bit :
  382. objectalloc^.sectionalloc(10);
  383. ait_real_64bit :
  384. objectalloc^.sectionalloc(8);
  385. ait_real_32bit :
  386. objectalloc^.sectionalloc(4);
  387. ait_comp_64bit :
  388. objectalloc^.sectionalloc(8);
  389. ait_const_rva,
  390. ait_const_symbol :
  391. objectalloc^.sectionalloc(4);
  392. ait_section:
  393. objectalloc^.setsection(pai_section(hp)^.sec);
  394. ait_symbol :
  395. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  396. ait_label :
  397. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  398. ait_string :
  399. objectalloc^.sectionalloc(pai_string(hp)^.len);
  400. ait_instruction :
  401. begin
  402. { reset instructions which could change in pass 2 }
  403. paicpu(hp)^.resetpass2;
  404. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  405. end;
  406. ait_cut :
  407. if SmartAsm then
  408. break;
  409. end;
  410. hp:=pai(hp^.next);
  411. end;
  412. TreePass0:=hp;
  413. end;
  414. function ti386binasmlist.TreePass1(hp:pai):pai;
  415. var
  416. i,l : longint;
  417. begin
  418. while assigned(hp) do
  419. begin
  420. {$ifdef GDB}
  421. { write stabs }
  422. if ((cs_debuginfo in aktmoduleswitches) or
  423. (cs_gdb_lineinfo in aktglobalswitches)) then
  424. begin
  425. if (objectalloc^.currsec<>sec_none) and
  426. not(hp^.typ in [
  427. ait_label,
  428. ait_regalloc,ait_tempalloc,
  429. ait_stabn,ait_stabs,ait_section,
  430. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  431. WriteFileLineInfo(hp^.fileinfo);
  432. end;
  433. {$endif GDB}
  434. case hp^.typ of
  435. ait_align :
  436. begin
  437. { here we must determine the fillsize which is used in pass2 }
  438. pai_align(hp)^.fillsize:=align(objectalloc^.sectionsize,pai_align(hp)^.aligntype)-
  439. objectalloc^.sectionsize;
  440. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  441. end;
  442. ait_datablock :
  443. begin
  444. if objectalloc^.currsec<>sec_bss then
  445. Message(asmw_e_alloc_data_only_in_bss);
  446. if not SmartAsm then
  447. begin
  448. if pai_datablock(hp)^.is_global then
  449. begin
  450. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  451. { force to be common/external, must be after setaddress as that would
  452. set it to AS_GLOBAL }
  453. pai_datablock(hp)^.sym^.bind:=AB_COMMON;
  454. end
  455. else
  456. begin
  457. l:=pai_datablock(hp)^.size;
  458. if l>2 then
  459. objectalloc^.sectionalign(4)
  460. else if l>1 then
  461. objectalloc^.sectionalign(2);
  462. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,
  463. pai_datablock(hp)^.size);
  464. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  465. end;
  466. end
  467. else
  468. begin
  469. l:=pai_datablock(hp)^.size;
  470. if l>2 then
  471. objectalloc^.sectionalign(4)
  472. else if l>1 then
  473. objectalloc^.sectionalign(2);
  474. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  475. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  476. end;
  477. UsedAsmSymbolListInsert(pai_datablock(hp)^.sym);
  478. end;
  479. ait_const_32bit :
  480. objectalloc^.sectionalloc(4);
  481. ait_const_16bit :
  482. objectalloc^.sectionalloc(2);
  483. ait_const_8bit :
  484. objectalloc^.sectionalloc(1);
  485. ait_real_80bit :
  486. objectalloc^.sectionalloc(10);
  487. ait_real_64bit :
  488. objectalloc^.sectionalloc(8);
  489. ait_real_32bit :
  490. objectalloc^.sectionalloc(4);
  491. ait_comp_64bit :
  492. objectalloc^.sectionalloc(8);
  493. ait_const_rva,
  494. ait_const_symbol :
  495. begin
  496. objectalloc^.sectionalloc(4);
  497. UsedAsmSymbolListInsert(pai_const_symbol(hp)^.sym);
  498. end;
  499. ait_section:
  500. begin
  501. objectalloc^.setsection(pai_section(hp)^.sec);
  502. {$ifdef GDB}
  503. case pai_section(hp)^.sec of
  504. sec_code : n_line:=n_textline;
  505. sec_data : n_line:=n_dataline;
  506. sec_bss : n_line:=n_bssline;
  507. else
  508. n_line:=n_dataline;
  509. end;
  510. stabslastfileinfo.line:=-1;
  511. {$endif GDB}
  512. end;
  513. {$ifdef GDB}
  514. ait_stabn :
  515. convertstabs(pai_stabn(hp)^.str);
  516. ait_stabs :
  517. convertstabs(pai_stabs(hp)^.str);
  518. ait_stab_function_name :
  519. begin
  520. if assigned(pai_stab_function_name(hp)^.str) then
  521. begin
  522. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str));
  523. UsedAsmSymbolListInsert(funcname);
  524. end
  525. else
  526. funcname:=nil;
  527. end;
  528. ait_force_line :
  529. stabslastfileinfo.line:=0;
  530. {$endif}
  531. ait_symbol :
  532. begin
  533. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  534. UsedAsmSymbolListInsert(pai_symbol(hp)^.sym);
  535. end;
  536. ait_symbol_end :
  537. begin
  538. if target_info.target=target_i386_linux then
  539. begin
  540. pai_symbol(hp)^.sym^.size:=objectalloc^.sectionsize-pai_symbol(hp)^.sym^.address;
  541. UsedAsmSymbolListInsert(pai_symbol(hp)^.sym);
  542. end;
  543. end;
  544. ait_label :
  545. begin
  546. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  547. UsedAsmSymbolListInsert(pai_label(hp)^.l);
  548. end;
  549. ait_string :
  550. objectalloc^.sectionalloc(pai_string(hp)^.len);
  551. ait_instruction :
  552. begin
  553. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  554. { fixup the references }
  555. for i:=1 to paicpu(hp)^.ops do
  556. begin
  557. with paicpu(hp)^.oper[i-1] do
  558. begin
  559. case typ of
  560. top_ref :
  561. begin
  562. if assigned(ref^.symbol) then
  563. UsedAsmSymbolListInsert(ref^.symbol);
  564. end;
  565. top_symbol :
  566. begin
  567. UsedAsmSymbolListInsert(sym);
  568. end;
  569. end;
  570. end;
  571. end;
  572. end;
  573. ait_direct :
  574. Message(asmw_f_direct_not_supported);
  575. ait_cut :
  576. if SmartAsm then
  577. break;
  578. end;
  579. hp:=pai(hp^.next);
  580. end;
  581. TreePass1:=hp;
  582. end;
  583. function ti386binasmlist.TreePass2(hp:pai):pai;
  584. var
  585. l : longint;
  586. {$ifdef I386}
  587. co : comp;
  588. {$endif I386}
  589. begin
  590. { main loop }
  591. while assigned(hp) do
  592. begin
  593. {$ifdef GDB}
  594. { write stabs }
  595. if ((cs_debuginfo in aktmoduleswitches) or
  596. (cs_gdb_lineinfo in aktglobalswitches)) then
  597. begin
  598. if (objectoutput^.currsec<>sec_none) and
  599. not(hp^.typ in [
  600. ait_label,
  601. ait_regalloc,ait_tempalloc,
  602. ait_stabn,ait_stabs,ait_section,
  603. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  604. WriteFileLineInfo(hp^.fileinfo);
  605. end;
  606. {$endif GDB}
  607. case hp^.typ of
  608. ait_align :
  609. objectoutput^.writebytes(pai_align(hp)^.getfillbuf^,pai_align(hp)^.fillsize);
  610. ait_section :
  611. begin
  612. objectoutput^.defaultsection(pai_section(hp)^.sec);
  613. {$ifdef GDB}
  614. case pai_section(hp)^.sec of
  615. sec_code : n_line:=n_textline;
  616. sec_data : n_line:=n_dataline;
  617. sec_bss : n_line:=n_bssline;
  618. else
  619. n_line:=n_dataline;
  620. end;
  621. stabslastfileinfo.line:=-1;
  622. {$endif GDB}
  623. end;
  624. ait_symbol :
  625. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  626. ait_datablock :
  627. begin
  628. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  629. if SmartAsm or (not pai_datablock(hp)^.is_global) then
  630. begin
  631. l:=pai_datablock(hp)^.size;
  632. if l>2 then
  633. objectoutput^.allocalign(4)
  634. else if l>1 then
  635. objectoutput^.allocalign(2);
  636. objectoutput^.alloc(pai_datablock(hp)^.size);
  637. end;
  638. end;
  639. ait_const_32bit :
  640. objectoutput^.writebytes(pai_const(hp)^.value,4);
  641. ait_const_16bit :
  642. objectoutput^.writebytes(pai_const(hp)^.value,2);
  643. ait_const_8bit :
  644. objectoutput^.writebytes(pai_const(hp)^.value,1);
  645. ait_real_80bit :
  646. objectoutput^.writebytes(pai_real_80bit(hp)^.value,10);
  647. ait_real_64bit :
  648. objectoutput^.writebytes(pai_real_64bit(hp)^.value,8);
  649. ait_real_32bit :
  650. objectoutput^.writebytes(pai_real_32bit(hp)^.value,4);
  651. ait_comp_64bit :
  652. begin
  653. {$ifdef FPC}
  654. co:=comp(pai_comp_64bit(hp)^.value);
  655. {$else}
  656. co:=pai_comp_64bit(hp)^.value;
  657. {$endif}
  658. objectoutput^.writebytes(co,8);
  659. end;
  660. ait_string :
  661. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  662. ait_const_rva :
  663. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  664. pai_const_symbol(hp)^.sym,relative_rva);
  665. ait_const_symbol :
  666. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  667. pai_const_symbol(hp)^.sym,relative_false);
  668. ait_label :
  669. objectoutput^.writesymbol(pai_label(hp)^.l);
  670. ait_instruction :
  671. paicpu(hp)^.Pass2;
  672. {$ifdef GDB}
  673. ait_stabn :
  674. convertstabs(pai_stabn(hp)^.str);
  675. ait_stabs :
  676. convertstabs(pai_stabs(hp)^.str);
  677. ait_stab_function_name :
  678. if assigned(pai_stab_function_name(hp)^.str) then
  679. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  680. else
  681. funcname:=nil;
  682. ait_force_line :
  683. stabslastfileinfo.line:=0;
  684. {$endif}
  685. ait_cut :
  686. if SmartAsm then
  687. break;
  688. end;
  689. hp:=pai(hp^.next);
  690. end;
  691. TreePass2:=hp;
  692. end;
  693. procedure ti386binasmlist.writetree;
  694. var
  695. hp : pai;
  696. begin
  697. objectalloc^.resetsections;
  698. objectalloc^.setsection(sec_code);
  699. objectoutput^.initwriting(cut_normal);
  700. objectoutput^.defaultsection(sec_code);
  701. { reset the asmsymbol list }
  702. InitUsedAsmsymbolList;
  703. {$ifdef MULTIPASS}
  704. { Pass 0 }
  705. currpass:=0;
  706. objectalloc^.setsection(sec_code);
  707. { start with list 1 }
  708. currlistidx:=1;
  709. currlist:=list[currlistidx];
  710. hp:=pai(currlist^.first);
  711. while assigned(hp) do
  712. begin
  713. hp:=TreePass0(hp);
  714. MaybeNextList(hp);
  715. end;
  716. { leave if errors have occured }
  717. if errorcount>0 then
  718. exit;
  719. {$endif}
  720. { Pass 1 }
  721. currpass:=1;
  722. objectalloc^.resetsections;
  723. objectalloc^.setsection(sec_code);
  724. {$ifdef GDB}
  725. StartFileLineInfo;
  726. {$endif GDB}
  727. { start with list 1 }
  728. currlistidx:=1;
  729. currlist:=list[currlistidx];
  730. hp:=pai(currlist^.first);
  731. while assigned(hp) do
  732. begin
  733. hp:=TreePass1(hp);
  734. MaybeNextList(hp);
  735. end;
  736. {$ifdef GDB}
  737. EndFileLineInfo;
  738. {$endif GDB}
  739. { check for undefined labels and reset }
  740. UsedAsmSymbolListCheckUndefined;
  741. { set section sizes }
  742. objectoutput^.setsectionsizes(objectalloc^.secsize);
  743. { leave if errors have occured }
  744. if errorcount>0 then
  745. exit;
  746. { Pass 2 }
  747. currpass:=2;
  748. {$ifdef GDB}
  749. StartFileLineInfo;
  750. {$endif GDB}
  751. { start with list 1 }
  752. currlistidx:=1;
  753. currlist:=list[currlistidx];
  754. hp:=pai(currlist^.first);
  755. while assigned(hp) do
  756. begin
  757. hp:=TreePass2(hp);
  758. MaybeNextList(hp);
  759. end;
  760. {$ifdef GDB}
  761. EndFileLineInfo;
  762. {$endif GDB}
  763. { leave if errors have occured }
  764. if errorcount>0 then
  765. exit;
  766. { write last objectfile }
  767. objectoutput^.donewriting;
  768. { reset the used symbols back, must be after the .o has been
  769. written }
  770. UsedAsmsymbolListReset;
  771. DoneUsedAsmsymbolList;
  772. end;
  773. procedure ti386binasmlist.writetreesmart;
  774. var
  775. hp : pai;
  776. startsec : tsection;
  777. place: tcutplace;
  778. begin
  779. objectalloc^.resetsections;
  780. objectalloc^.setsection(sec_code);
  781. objectoutput^.initwriting(cut_normal);
  782. objectoutput^.defaultsection(sec_code);
  783. startsec:=sec_code;
  784. { start with list 1 }
  785. currlistidx:=1;
  786. currlist:=list[currlistidx];
  787. hp:=pai(currlist^.first);
  788. while assigned(hp) do
  789. begin
  790. { reset the asmsymbol list }
  791. InitUsedAsmSymbolList;
  792. {$ifdef MULTIPASS}
  793. { Pass 0 }
  794. currpass:=0;
  795. objectalloc^.resetsections;
  796. objectalloc^.setsection(startsec);
  797. TreePass0(hp);
  798. { leave if errors have occured }
  799. if errorcount>0 then
  800. exit;
  801. {$endif MULTIPASS}
  802. { Pass 1 }
  803. currpass:=1;
  804. objectalloc^.resetsections;
  805. objectalloc^.setsection(startsec);
  806. {$ifdef GDB}
  807. StartFileLineInfo;
  808. {$endif GDB}
  809. TreePass1(hp);
  810. {$ifdef GDB}
  811. EndFileLineInfo;
  812. {$endif GDB}
  813. { check for undefined labels }
  814. UsedAsmSymbolListCheckUndefined;
  815. { set section sizes }
  816. objectoutput^.setsectionsizes(objectalloc^.secsize);
  817. { leave if errors have occured }
  818. if errorcount>0 then
  819. exit;
  820. { Pass 2 }
  821. currpass:=2;
  822. objectoutput^.defaultsection(startsec);
  823. {$ifdef GDB}
  824. StartFileLineInfo;
  825. {$endif GDB}
  826. hp:=TreePass2(hp);
  827. {$ifdef GDB}
  828. EndFileLineInfo;
  829. {$endif GDB}
  830. { leave if errors have occured }
  831. if errorcount>0 then
  832. exit;
  833. { if not end then write the current objectfile }
  834. objectoutput^.donewriting;
  835. { reset the used symbols back, must be after the .o has been
  836. written }
  837. UsedAsmsymbolListReset;
  838. DoneUsedAsmsymbolList;
  839. { end of lists? }
  840. if not MaybeNextList(hp) then
  841. break;
  842. { save section for next loop }
  843. { this leads to a problem if startsec is sec_none !! PM }
  844. startsec:=objectalloc^.currsec;
  845. { we will start a new objectfile so reset everything }
  846. { The place can still change in the next while loop, so don't init }
  847. { the writer yet (JM) }
  848. if (hp^.typ=ait_cut) then
  849. place := pai_cut(hp)^.place
  850. else
  851. place := cut_normal;
  852. { avoid empty files }
  853. while assigned(hp^.next) and
  854. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  855. begin
  856. if pai(hp^.next)^.typ=ait_section then
  857. startsec:=pai_section(hp^.next)^.sec
  858. else if (pai(hp^.next)^.typ=ait_cut) then
  859. place := pai_cut(hp)^.place;
  860. hp:=pai(hp^.next);
  861. end;
  862. objectoutput^.initwriting(place);
  863. hp:=pai(hp^.next);
  864. { there is a problem if startsec is sec_none !! PM }
  865. if startsec=sec_none then
  866. startsec:=sec_code;
  867. if not MaybeNextList(hp) then
  868. break;
  869. end;
  870. end;
  871. procedure ti386binasmlist.writebin;
  872. procedure addlist(p:paasmoutput);
  873. begin
  874. inc(lists);
  875. list[lists]:=p;
  876. end;
  877. begin
  878. if cs_debuginfo in aktmoduleswitches then
  879. addlist(debuglist);
  880. addlist(codesegment);
  881. addlist(datasegment);
  882. addlist(consts);
  883. addlist(rttilist);
  884. if assigned(resourcestringlist) then
  885. addlist(resourcestringlist);
  886. addlist(bsssegment);
  887. if assigned(importssection) then
  888. addlist(importssection);
  889. if assigned(exportssection) and not UseDeffileForExport then
  890. addlist(exportssection);
  891. if assigned(resourcesection) then
  892. addlist(resourcesection);
  893. if SmartAsm then
  894. writetreesmart
  895. else
  896. writetree;
  897. end;
  898. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  899. begin
  900. case t of
  901. og_none :
  902. Message(asmw_f_no_binary_writer_selected);
  903. og_coff :
  904. objectoutput:=new(pcoffoutput,initdjgpp(smart));
  905. og_pecoff :
  906. objectoutput:=new(pcoffoutput,initwin32(smart));
  907. og_elf :
  908. objectoutput:=new(pelf32output,init(smart));
  909. else
  910. internalerror(43243432);
  911. end;
  912. objectalloc:=new(pobjectalloc,init);
  913. SmartAsm:=smart;
  914. currpass:=0;
  915. end;
  916. destructor ti386binasmlist.done;
  917. {$ifdef MEMDEBUG}
  918. var
  919. d : tmemdebug;
  920. {$endif}
  921. begin
  922. {$ifdef MEMDEBUG}
  923. d.init('agbin');
  924. {$endif}
  925. dispose(objectoutput,done);
  926. dispose(objectalloc,done);
  927. {$ifdef MEMDEBUG}
  928. d.done;
  929. {$endif}
  930. end;
  931. end.
  932. {
  933. $Log$
  934. Revision 1.9 2000-11-12 22:20:37 peter
  935. * create generic toutputsection for binary writers
  936. Revision 1.8 2000/09/24 15:06:10 peter
  937. * use defines.inc
  938. Revision 1.7 2000/08/27 16:11:49 peter
  939. * moved some util functions from globals,cobjects to cutils
  940. * splitted files into finput,fmodule
  941. Revision 1.6 2000/08/12 15:34:22 peter
  942. + usedasmsymbollist to check and reset only the used symbols (merged)
  943. Revision 1.5 2000/08/08 19:28:57 peter
  944. * memdebug/memory patches (merged)
  945. * only once illegal directive (merged)
  946. Revision 1.4 2000/08/04 22:00:50 peter
  947. * merges from fixes
  948. Revision 1.3 2000/07/13 12:08:24 michael
  949. + patched to 1.1.0 with former 1.09patch from peter
  950. Revision 1.2 2000/07/13 11:32:29 michael
  951. + removed logs
  952. }