ogcoff.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman and Pierre Muller
  3. Contains the binary coff reader and writer
  4. * This code was inspired by the NASM sources
  5. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit ogcoff;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. { common }
  25. cclasses,globtype,
  26. { target }
  27. systems,
  28. { assembler }
  29. cpuinfo,cpubase,aasmbase,assemble,link,
  30. { output }
  31. ogbase,ogmap;
  32. type
  33. TCoffObjectSection = class(TAsmSection)
  34. private
  35. orgmempos,
  36. coffrelocs,
  37. coffrelocpos : longint;
  38. public
  39. flags : cardinal;
  40. constructor create(const Aname:string;Atype:TAsmSectionType;Aalign:longint;Aoptions:TAsmSectionOptions);override;
  41. procedure addsymsizereloc(ofs:longint;p:tasmsymbol;size:longint;relative:TAsmRelocationType);
  42. procedure fixuprelocs;override;
  43. end;
  44. TDjCoffObjectSection = class(TCoffObjectSection)
  45. constructor create(const Aname:string;Atype:TAsmSectionType;Aalign:longint;Aoptions:TAsmSectionOptions);override;
  46. end;
  47. TPECoffObjectSection = class(TCoffObjectSection)
  48. constructor create(const Aname:string;Atype:TAsmSectionType;Aalign:longint;Aoptions:TAsmSectionOptions);override;
  49. end;
  50. tcoffobjectdata = class(TAsmObjectData)
  51. private
  52. win32 : boolean;
  53. procedure section_mempos(p:tnamedindexitem;arg:pointer);
  54. public
  55. constructor createcoff(const n:string;awin32:boolean;acasmsection:TAsmSectionClass);
  56. destructor destroy;override;
  57. function sectionname(atype:tasmsectiontype;const aname:string):string;override;
  58. procedure writereloc(data,len:aint;p:tasmsymbol;relative:TAsmRelocationType);override;
  59. procedure writesymbol(p:tasmsymbol);override;
  60. procedure writestab(offset:aint;ps:tasmsymbol;nidx,nother,line:longint;p:pchar);override;
  61. procedure beforealloc;override;
  62. procedure beforewrite;override;
  63. procedure afteralloc;override;
  64. end;
  65. tdjcoffobjectdata = class(TCoffObjectData)
  66. constructor create(const n:string);override;
  67. end;
  68. tpecoffobjectdata = class(TCoffObjectData)
  69. constructor create(const n:string);override;
  70. end;
  71. tcoffobjectoutput = class(tobjectoutput)
  72. private
  73. win32 : boolean;
  74. initsym : longint;
  75. FCoffStrs : tdynamicarray;
  76. procedure write_symbol(const name:string;value,section,typ,aux:longint);
  77. procedure section_write_symbol(p:tnamedindexitem;arg:pointer);
  78. procedure section_write_relocs(p:tnamedindexitem;arg:pointer);
  79. procedure write_symbols(data:TAsmObjectData);
  80. procedure section_set_secsymidx(p:tnamedindexitem;arg:pointer);
  81. procedure section_set_datapos(p:tnamedindexitem;arg:pointer);
  82. procedure section_set_reloc_datapos(p:tnamedindexitem;arg:pointer);
  83. procedure section_write_header(p:tnamedindexitem;arg:pointer);
  84. procedure section_write_data(p:tnamedindexitem;arg:pointer);
  85. protected
  86. function writedata(data:TAsmObjectData):boolean;override;
  87. public
  88. constructor createdjgpp(smart:boolean);
  89. constructor createwin32(smart:boolean);
  90. function newobjectdata(const n:string):TAsmObjectData;override;
  91. end;
  92. tcoffexeoutput = class(texeoutput)
  93. private
  94. FCoffsyms,
  95. FCoffStrs : tdynamicarray;
  96. win32 : boolean;
  97. nsects,
  98. nsyms,
  99. sympos : longint;
  100. procedure write_symbol(const name:string;value,section,typ,aux:longint);
  101. procedure write_symbols;
  102. protected
  103. function writedata:boolean;override;
  104. public
  105. constructor createdjgpp;
  106. constructor createwin32;
  107. function newobjectinput:tobjectinput;override;
  108. procedure CalculateMemoryMap;override;
  109. procedure GenerateExecutable(const fn:string);override;
  110. end;
  111. ttasmsymbolrec = record
  112. sym : tasmsymbol;
  113. orgsize : longint;
  114. end;
  115. ttasmsymbolarray = array[0..high(word)] of ttasmsymbolrec;
  116. tcoffobjectinput = class(tobjectinput)
  117. private
  118. Fidx2sec : array[0..255] of TAsmSection;
  119. FCoffsyms,
  120. FCoffStrs : tdynamicarray;
  121. FSymTbl : ^ttasmsymbolarray;
  122. win32 : boolean;
  123. procedure read_relocs(s:TCoffObjectSection);
  124. procedure handle_symbols(data:TAsmObjectData);
  125. protected
  126. function readobjectdata(data:TAsmObjectData):boolean;override;
  127. public
  128. constructor createdjgpp;
  129. constructor createwin32;
  130. function newobjectdata(const n:string):TAsmObjectData;override;
  131. end;
  132. tcoffassembler = class(tinternalassembler)
  133. constructor create(smart:boolean);override;
  134. end;
  135. tpecoffassembler = class(tinternalassembler)
  136. constructor create(smart:boolean);override;
  137. end;
  138. tcofflinker = class(tinternallinker)
  139. constructor create;override;
  140. end;
  141. implementation
  142. uses
  143. strings,
  144. cutils,verbose,
  145. globals,fmodule;
  146. const
  147. COFF_FLAG_NORELOCS = $0001;
  148. COFF_FLAG_EXE = $0002;
  149. COFF_FLAG_NOLINES = $0004;
  150. COFF_FLAG_NOLSYMS = $0008;
  151. COFF_FLAG_AR16WR = $0080; { 16bit little endian }
  152. COFF_FLAG_AR32WR = $0100; { 32bit little endian }
  153. COFF_FLAG_AR32W = $0200; { 32bit big endian }
  154. COFF_FLAG_DLL = $2000;
  155. COFF_SYM_GLOBAL = 2;
  156. COFF_SYM_LOCAL = 3;
  157. COFF_SYM_LABEL = 6;
  158. COFF_SYM_FUNCTION = 101;
  159. COFF_SYM_FILE = 103;
  160. COFF_SYM_SECTION = 104;
  161. type
  162. { Structures which are written directly to the output file }
  163. coffheader=packed record
  164. mach : word;
  165. nsects : word;
  166. time : longint;
  167. sympos : longint;
  168. syms : longint;
  169. opthdr : word;
  170. flag : word;
  171. end;
  172. coffoptheader=packed record
  173. magic : word;
  174. vstamp : word;
  175. tsize : longint;
  176. dsize : longint;
  177. bsize : longint;
  178. entry : longint;
  179. text_start : longint;
  180. data_start : longint;
  181. end;
  182. coffsechdr=packed record
  183. name : array[0..7] of char;
  184. vsize : longint;
  185. rvaofs : longint;
  186. datasize : longint;
  187. datapos : longint;
  188. relocpos : longint;
  189. lineno1 : longint;
  190. nrelocs : word;
  191. lineno2 : word;
  192. flags : cardinal;
  193. end;
  194. coffsectionrec=packed record
  195. len : longint;
  196. nrelocs : word;
  197. empty : array[0..11] of char;
  198. end;
  199. coffreloc=packed record
  200. address : longint;
  201. sym : longint;
  202. relative : word;
  203. end;
  204. coffsymbol=packed record
  205. name : array[0..3] of char; { real is [0..7], which overlaps the strpos ! }
  206. strpos : longint;
  207. value : longint;
  208. section : smallint;
  209. empty : smallint;
  210. typ : byte;
  211. aux : byte;
  212. end;
  213. coffstab=packed record
  214. strpos : longint;
  215. ntype : byte;
  216. nother : byte;
  217. ndesc : word;
  218. nvalue : longint;
  219. end;
  220. const
  221. symbolresize = 200*sizeof(coffsymbol);
  222. strsresize = 8192;
  223. const go32v2stub : array[0..2047] of byte=(
  224. $4D,$5A,$00,$00,$04,$00,$00,$00,$20,$00,$27,$00,$FF,$FF,$00,
  225. $00,$60,$07,$00,$00,$54,$00,$00,$00,$00,$00,$00,$00,$0D,$0A,
  226. $73,$74,$75,$62,$2E,$68,$20,$67,$65,$6E,$65,$72,$61,$74,$65,
  227. $64,$20,$66,$72,$6F,$6D,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,
  228. $20,$62,$79,$20,$64,$6A,$61,$73,$6D,$2C,$20,$6F,$6E,$20,$54,
  229. $68,$75,$20,$44,$65,$63,$20,$20,$39,$20,$31,$30,$3A,$35,$39,
  230. $3A,$33,$31,$20,$31,$39,$39,$39,$0D,$0A,$54,$68,$65,$20,$53,
  231. $54,$55,$42,$2E,$45,$58,$45,$20,$73,$74,$75,$62,$20,$6C,$6F,
  232. $61,$64,$65,$72,$20,$69,$73,$20,$43,$6F,$70,$79,$72,$69,$67,
  233. $68,$74,$20,$28,$43,$29,$20,$31,$39,$39,$33,$2D,$31,$39,$39,
  234. $35,$20,$44,$4A,$20,$44,$65,$6C,$6F,$72,$69,$65,$2E,$20,$0D,
  235. $0A,$50,$65,$72,$6D,$69,$73,$73,$69,$6F,$6E,$20,$67,$72,$61,
  236. $6E,$74,$65,$64,$20,$74,$6F,$20,$75,$73,$65,$20,$66,$6F,$72,
  237. $20,$61,$6E,$79,$20,$70,$75,$72,$70,$6F,$73,$65,$20,$70,$72,
  238. $6F,$76,$69,$64,$65,$64,$20,$74,$68,$69,$73,$20,$63,$6F,$70,
  239. $79,$72,$69,$67,$68,$74,$20,$0D,$0A,$72,$65,$6D,$61,$69,$6E,
  240. $73,$20,$70,$72,$65,$73,$65,$6E,$74,$20,$61,$6E,$64,$20,$75,
  241. $6E,$6D,$6F,$64,$69,$66,$69,$65,$64,$2E,$20,$0D,$0A,$54,$68,
  242. $69,$73,$20,$6F,$6E,$6C,$79,$20,$61,$70,$70,$6C,$69,$65,$73,
  243. $20,$74,$6F,$20,$74,$68,$65,$20,$73,$74,$75,$62,$2C,$20,$61,
  244. $6E,$64,$20,$6E,$6F,$74,$20,$6E,$65,$63,$65,$73,$73,$61,$72,
  245. $69,$6C,$79,$20,$74,$68,$65,$20,$77,$68,$6F,$6C,$65,$20,$70,
  246. $72,$6F,$67,$72,$61,$6D,$2E,$0A,$0D,$0A,$24,$49,$64,$3A,$20,
  247. $73,$74,$75,$62,$2E,$61,$73,$6D,$20,$62,$75,$69,$6C,$74,$20,
  248. $31,$32,$2F,$30,$39,$2F,$39,$39,$20,$31,$30,$3A,$35,$39,$3A,
  249. $33,$31,$20,$62,$79,$20,$64,$6A,$61,$73,$6D,$20,$24,$0A,$0D,
  250. $0A,$40,$28,$23,$29,$20,$73,$74,$75,$62,$2E,$61,$73,$6D,$20,
  251. $62,$75,$69,$6C,$74,$20,$31,$32,$2F,$30,$39,$2F,$39,$39,$20,
  252. $31,$30,$3A,$35,$39,$3A,$33,$31,$20,$62,$79,$20,$64,$6A,$61,
  253. $73,$6D,$0A,$0D,$0A,$1A,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  254. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  255. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  256. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  257. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  258. $00,$00,$67,$6F,$33,$32,$73,$74,$75,$62,$2C,$20,$76,$20,$32,
  259. $2E,$30,$32,$54,$00,$00,$00,$00,$00,$08,$00,$00,$00,$00,$00,
  260. $00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  261. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  262. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$43,$57,$53,$44,$50,
  263. $4D,$49,$2E,$45,$58,$45,$00,$00,$00,$00,$00,$0E,$1F,$8C,$1E,
  264. $24,$00,$8C,$06,$60,$07,$FC,$B4,$30,$CD,$21,$3C,$03,$73,$08,
  265. $B0,$6D,$BA,$A7,$05,$E9,$D4,$03,$A2,$69,$08,$BE,$20,$00,$8B,
  266. $04,$09,$C0,$75,$02,$B4,$FE,$BB,$70,$08,$39,$C3,$73,$02,$89,
  267. $C3,$89,$1C,$FE,$C7,$B9,$04,$FF,$D3,$EB,$B4,$4A,$CD,$21,$73,
  268. $08,$D3,$E3,$FE,$CF,$89,$1C,$EB,$D8,$26,$8E,$06,$2C,$00,$31,
  269. $FF,$30,$C0,$A9,$F2,$AE,$26,$81,$3D,$50,$41,$75,$15,$AF,$26,
  270. $81,$3D,$54,$48,$75,$0D,$AF,$26,$80,$3D,$3D,$75,$06,$47,$89,
  271. $3E,$8C,$04,$4F,$AE,$75,$DF,$AF,$B4,$3E,$BB,$13,$00,$CD,$21,
  272. $B4,$3E,$BB,$12,$00,$CD,$21,$06,$57,$31,$C9,$74,$12,$B0,$6E,
  273. $BA,$7E,$05,$E9,$5E,$03,$09,$C9,$75,$F4,$41,$E8,$A1,$03,$72,
  274. $EE,$B8,$87,$16,$CD,$2F,$09,$C0,$75,$ED,$80,$E3,$01,$74,$E8,
  275. $89,$3E,$00,$06,$8C,$06,$02,$06,$89,$36,$04,$06,$5F,$07,$E8,
  276. $D3,$02,$89,$3E,$2A,$00,$89,$36,$62,$07,$80,$3E,$2C,$00,$00,
  277. $74,$23,$B9,$08,$00,$BF,$2C,$00,$8A,$05,$47,$08,$C0,$74,$05,
  278. $88,$07,$43,$E2,$F4,$66,$C7,$07,$2E,$45,$58,$45,$83,$C3,$04,
  279. $C6,$07,$00,$89,$1E,$62,$07,$B8,$00,$3D,$BA,$64,$07,$CD,$21,
  280. $0F,$82,$B3,$02,$A3,$06,$06,$89,$C3,$B9,$06,$00,$BA,$B5,$07,
  281. $B4,$3F,$CD,$21,$31,$D2,$31,$C9,$A1,$B5,$07,$3D,$4C,$01,$74,
  282. $1B,$3D,$4D,$5A,$0F,$85,$98,$02,$8B,$16,$B9,$07,$C1,$E2,$09,
  283. $8B,$1E,$B7,$07,$09,$DB,$74,$05,$80,$EE,$02,$01,$DA,$89,$16,
  284. $BB,$07,$89,$0E,$BD,$07,$B8,$00,$42,$8B,$1E,$06,$06,$CD,$21,
  285. $B9,$A8,$00,$BA,$BF,$07,$B4,$3F,$CD,$21,$3D,$A8,$00,$75,$06,
  286. $81,$3E,$BF,$07,$4C,$01,$0F,$85,$61,$02,$66,$A1,$E3,$07,$66,
  287. $A3,$10,$06,$66,$8B,$0E,$BB,$07,$66,$A1,$03,$08,$66,$01,$C8,
  288. $66,$A3,$08,$06,$66,$A1,$2B,$08,$66,$01,$C8,$66,$A3,$0C,$06,
  289. $66,$8B,$1E,$4B,$08,$66,$A1,$4F,$08,$66,$01,$C3,$66,$B8,$01,
  290. $00,$01,$00,$66,$39,$C3,$73,$03,$66,$89,$C3,$66,$81,$C3,$FF,
  291. $FF,$00,$00,$31,$DB,$66,$89,$1E,$1C,$00,$E8,$F5,$02,$8B,$1E,
  292. $04,$06,$09,$DB,$74,$0A,$B4,$48,$CD,$21,$0F,$82,$15,$02,$8E,
  293. $C0,$E8,$08,$03,$B8,$01,$00,$FF,$1E,$00,$06,$0F,$82,$0F,$02,
  294. $8C,$06,$26,$00,$8C,$0E,$28,$00,$8C,$D8,$A3,$22,$00,$8E,$C0,
  295. $31,$C0,$B9,$01,$00,$CD,$31,$72,$07,$A3,$14,$06,$31,$C0,$CD,
  296. $31,$0F,$82,$F3,$01,$A3,$16,$06,$66,$8B,$0E,$1C,$00,$B8,$01,
  297. $05,$8B,$1E,$1E,$00,$CD,$31,$0F,$82,$E5,$01,$89,$1E,$1A,$06,
  298. $89,$0E,$18,$06,$89,$36,$1A,$00,$89,$3E,$18,$00,$B8,$07,$00,
  299. $8B,$1E,$14,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,$CD,$31,$B8,
  300. $09,$00,$8C,$C9,$83,$E1,$03,$C1,$E1,$05,$51,$81,$C9,$9B,$C0,
  301. $CD,$31,$B8,$08,$00,$8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,
  302. $B8,$07,$00,$8B,$1E,$16,$06,$8B,$0E,$1A,$06,$8B,$16,$18,$06,
  303. $CD,$31,$B8,$09,$00,$59,$81,$C9,$93,$C0,$CD,$31,$B8,$08,$00,
  304. $8B,$0E,$1E,$00,$49,$BA,$FF,$FF,$CD,$31,$B8,$00,$01,$BB,$00,
  305. $0F,$CD,$31,$73,$10,$3D,$08,$00,$0F,$85,$73,$01,$B8,$00,$01,
  306. $CD,$31,$0F,$82,$6A,$01,$A3,$1C,$06,$89,$16,$1E,$06,$C1,$E3,
  307. $04,$89,$1E,$20,$06,$66,$8B,$36,$08,$06,$66,$8B,$3E,$FB,$07,
  308. $66,$8B,$0E,$FF,$07,$E8,$49,$00,$66,$8B,$36,$0C,$06,$66,$8B,
  309. $3E,$23,$08,$66,$8B,$0E,$27,$08,$E8,$37,$00,$8E,$06,$16,$06,
  310. $66,$8B,$3E,$4B,$08,$66,$8B,$0E,$4F,$08,$66,$31,$C0,$66,$C1,
  311. $E9,$02,$67,$F3,$66,$AB,$B4,$3E,$8B,$1E,$06,$06,$CD,$21,$B8,
  312. $01,$01,$8B,$16,$1E,$06,$CD,$31,$1E,$0F,$A1,$8E,$1E,$16,$06,
  313. $66,$64,$FF,$2E,$10,$06,$66,$89,$F0,$66,$25,$FF,$01,$00,$00,
  314. $66,$01,$C1,$29,$C6,$66,$29,$C7,$66,$89,$0E,$26,$06,$66,$89,
  315. $3E,$22,$06,$E8,$0F,$01,$89,$36,$3E,$06,$66,$C1,$EE,$10,$89,
  316. $36,$42,$06,$8B,$1E,$06,$06,$89,$1E,$3A,$06,$C7,$06,$46,$06,
  317. $00,$42,$E8,$03,$01,$A1,$1C,$06,$A3,$4E,$06,$C7,$06,$3E,$06,
  318. $00,$00,$C6,$06,$47,$06,$3F,$A1,$28,$06,$09,$C0,$75,$09,$A1,
  319. $26,$06,$3B,$06,$20,$06,$76,$03,$A1,$20,$06,$A3,$42,$06,$E8,
  320. $D9,$00,$66,$31,$C9,$8B,$0E,$46,$06,$66,$8B,$3E,$22,$06,$66,
  321. $01,$0E,$22,$06,$66,$29,$0E,$26,$06,$66,$31,$F6,$C1,$E9,$02,
  322. $1E,$06,$8E,$06,$16,$06,$8E,$1E,$1E,$06,$67,$F3,$66,$A5,$07,
  323. $1F,$66,$03,$0E,$26,$06,$75,$AF,$C3,$3C,$3A,$74,$06,$3C,$2F,
  324. $74,$02,$3C,$5C,$C3,$BE,$64,$07,$89,$F3,$26,$8A,$05,$47,$88,
  325. $04,$38,$E0,$74,$0E,$08,$C0,$74,$0A,$46,$E8,$DE,$FF,$75,$EC,
  326. $89,$F3,$74,$E8,$C3,$B0,$66,$BA,$48,$05,$EB,$0C,$B0,$67,$BA,
  327. $55,$05,$EB,$05,$B0,$68,$BA,$5F,$05,$52,$8B,$1E,$62,$07,$C6,
  328. $07,$24,$BB,$64,$07,$EB,$28,$E8,$F5,$00,$B0,$69,$BA,$99,$05,
  329. $EB,$1A,$B0,$6A,$BA,$B2,$05,$EB,$13,$B0,$6B,$BA,$C4,$05,$EB,
  330. $0C,$B0,$6C,$BA,$D6,$05,$EB,$05,$B0,$69,$BA,$99,$05,$52,$BB,
  331. $3B,$05,$E8,$15,$00,$5B,$E8,$11,$00,$BB,$67,$04,$E8,$0B,$00,
  332. $B4,$4C,$CD,$21,$43,$50,$B4,$02,$CD,$21,$58,$8A,$17,$80,$FA,
  333. $24,$75,$F2,$C3,$0D,$0A,$24,$50,$51,$57,$31,$C0,$BF,$2A,$06,
  334. $B9,$19,$00,$F3,$AB,$5F,$59,$58,$C3,$B8,$00,$03,$BB,$21,$00,
  335. $31,$C9,$66,$BF,$2A,$06,$00,$00,$CD,$31,$C3,$00,$00,$30,$E4,
  336. $E8,$4E,$FF,$89,$DE,$8B,$3E,$8C,$04,$EB,$17,$B4,$3B,$E8,$41,
  337. $FF,$81,$FE,$64,$07,$74,$12,$8A,$44,$FF,$E8,$2A,$FF,$74,$04,
  338. $C6,$04,$5C,$46,$E8,$03,$00,$72,$E4,$C3,$E8,$34,$00,$BB,$44,
  339. $00,$8A,$07,$88,$04,$43,$46,$08,$C0,$75,$F6,$06,$57,$1E,$07,
  340. $E8,$9B,$FF,$BB,$2A,$06,$8C,$5F,$04,$89,$5F,$02,$BA,$64,$07,
  341. $B8,$00,$4B,$CD,$21,$5F,$07,$72,$09,$B4,$4D,$CD,$21,$2D,$00,
  342. $03,$F7,$D8,$EB,$28,$80,$3E,$69,$08,$05,$72,$20,$B8,$00,$58,
  343. $CD,$21,$A2,$67,$08,$B8,$02,$58,$CD,$21,$A2,$68,$08,$B8,$01,
  344. $58,$BB,$80,$00,$CD,$21,$B8,$03,$58,$BB,$01,$00,$CD,$21,$C3,
  345. $9C,$80,$3E,$69,$08,$05,$72,$1A,$50,$53,$B8,$03,$58,$8A,$1E,
  346. $68,$08,$30,$FF,$CD,$21,$B8,$01,$58,$8A,$1E,$67,$08,$30,$FF,
  347. $CD,$21,$5B,$58,$9D,$C3,$4C,$6F,$61,$64,$20,$65,$72,$72,$6F,
  348. $72,$3A,$20,$24,$3A,$20,$63,$61,$6E,$27,$74,$20,$6F,$70,$65,
  349. $6E,$24,$3A,$20,$6E,$6F,$74,$20,$45,$58,$45,$24,$3A,$20,$6E,
  350. $6F,$74,$20,$43,$4F,$46,$46,$20,$28,$43,$68,$65,$63,$6B,$20,
  351. $66,$6F,$72,$20,$76,$69,$72,$75,$73,$65,$73,$29,$24,$6E,$6F,
  352. $20,$44,$50,$4D,$49,$20,$2D,$20,$47,$65,$74,$20,$63,$73,$64,
  353. $70,$6D,$69,$2A,$62,$2E,$7A,$69,$70,$24,$6E,$6F,$20,$44,$4F,
  354. $53,$20,$6D,$65,$6D,$6F,$72,$79,$24,$6E,$65,$65,$64,$20,$44,
  355. $4F,$53,$20,$33,$24,$63,$61,$6E,$27,$74,$20,$73,$77,$69,$74,
  356. $63,$68,$20,$6D,$6F,$64,$65,$24,$6E,$6F,$20,$44,$50,$4D,$49,
  357. $20,$73,$65,$6C,$65,$63,$74,$6F,$72,$73,$24,$6E,$6F,$20,$44,
  358. $50,$4D,$49,$20,$6D,$65,$6D,$6F,$72,$79,$24,$90,$90,$90,$90,
  359. $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,
  360. $90,$90,$90,$90,$90,$90,$90,$90);
  361. {****************************************************************************
  362. TCoffObjectSection
  363. ****************************************************************************}
  364. constructor tcoffobjectsection.create(const aname:string;atype:tasmsectiontype;aalign:longint;aoptions:TAsmSectionOptions);
  365. begin
  366. inherited create(aname,atype,aalign,aoptions);
  367. Flags:=0;
  368. end;
  369. procedure TCoffObjectSection.addsymsizereloc(ofs:longint;p:tasmsymbol;size:longint;relative:TAsmRelocationType);
  370. begin
  371. relocations.concat(tasmrelocation.createsymbolsize(ofs,p,size,relative));
  372. end;
  373. procedure TCoffObjectSection.fixuprelocs;
  374. var
  375. r : TAsmRelocation;
  376. address,
  377. relocval : longint;
  378. begin
  379. r:=TAsmRelocation(relocations.first);
  380. if assigned(r) and
  381. (not assigned(data)) then
  382. internalerror(200205183);
  383. while assigned(r) do
  384. begin
  385. if assigned(r.symbol) then
  386. relocval:=r.symbol.address
  387. else
  388. internalerror(200205183);
  389. data.Seek(r.address);
  390. data.Read(address,4);
  391. case r.typ of
  392. RELOC_RELATIVE :
  393. begin
  394. dec(address,mempos);
  395. inc(address,relocval);
  396. end;
  397. RELOC_RVA,
  398. RELOC_ABSOLUTE :
  399. begin
  400. if r.symbol.section.sectype=sec_common then
  401. dec(address,r.orgsize)
  402. else
  403. begin
  404. { fixup address when the symbol was known in defined object }
  405. if (r.symbol.section<>nil) and
  406. (r.symbol.owner=owner) then
  407. dec(address,TCoffObjectSection(r.symbol.section).orgmempos);
  408. end;
  409. inc(address,relocval);
  410. end;
  411. end;
  412. data.Seek(r.address);
  413. data.Write(address,4);
  414. { goto next reloc }
  415. r:=TAsmRelocation(r.next);
  416. end;
  417. end;
  418. {****************************************************************************
  419. TDjCoffObjectSection
  420. ****************************************************************************}
  421. constructor tdjcoffobjectsection.create(const aname:string;atype:tasmsectiontype;aalign:longint;aoptions:TAsmSectionOptions);
  422. begin
  423. inherited create(aname,atype,aalign,aoptions);
  424. case atype of
  425. sec_code :
  426. begin
  427. Flags:=$20;
  428. addralign:=16;
  429. end;
  430. sec_data :
  431. begin
  432. Flags:=$40;
  433. addralign:=16;
  434. end;
  435. sec_bss :
  436. begin
  437. Flags:=$80;
  438. addralign:=16;
  439. end;
  440. end;
  441. end;
  442. {****************************************************************************
  443. TPECoffObjectSection
  444. ****************************************************************************}
  445. constructor tpecoffobjectsection.create(const aname:string;atype:tasmsectiontype;aalign:longint;aoptions:TAsmSectionOptions);
  446. begin
  447. inherited create(aname,atype,aalign,aoptions);
  448. case atype of
  449. sec_code :
  450. begin
  451. Flags:=$60000020;
  452. addralign:=16;
  453. end;
  454. sec_data :
  455. begin
  456. Flags:=$c0300040;
  457. addralign:=16;
  458. end;
  459. sec_bss :
  460. begin
  461. Flags:=$c0300080;
  462. addralign:=16;
  463. end;
  464. sec_idata2,
  465. sec_idata4,
  466. sec_idata5,
  467. sec_idata6,
  468. sec_idata7 :
  469. begin
  470. Flags:=$40000000;
  471. end;
  472. sec_edata :
  473. begin
  474. Flags:=$c0300040;
  475. end;
  476. end;
  477. end;
  478. {****************************************************************************
  479. tcoffobjectdata
  480. ****************************************************************************}
  481. constructor tcoffobjectdata.createcoff(const n:string;awin32:boolean;acasmsection:TAsmSectionClass);
  482. begin
  483. inherited create(n);
  484. CAsmSection:=ACAsmSection;
  485. win32:=awin32;
  486. { we need at least the following 3 sections }
  487. createsection(sec_code,'',0,[]);
  488. createsection(sec_data,'',0,[]);
  489. createsection(sec_bss,'',0,[]);
  490. if (cs_use_lineinfo in aktglobalswitches) or
  491. (cs_debuginfo in aktmoduleswitches) then
  492. begin
  493. stabssec:=createsection(sec_stab,'',0,[]);
  494. stabstrsec:=createsection(sec_stabstr,'',0,[]);
  495. end;
  496. end;
  497. destructor tcoffobjectdata.destroy;
  498. begin
  499. inherited destroy;
  500. end;
  501. function TCoffObjectData.sectionname(atype:tasmsectiontype;const aname:string):string;
  502. const
  503. secnames : array[tasmsectiontype] of string[16] = ('',
  504. '.text','.data','.data','.bss','.threadvar',
  505. 'common',
  506. '.note',
  507. '.text',
  508. '.stab','.stabstr',
  509. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  510. '.eh_frame',
  511. '.debug_frame',
  512. '.fpc'
  513. );
  514. begin
  515. { No support for named sections, because section names are limited to 8 chars }
  516. result:=secnames[atype];
  517. end;
  518. procedure tcoffobjectdata.writesymbol(p:tasmsymbol);
  519. begin
  520. if currsec=nil then
  521. internalerror(200403071);
  522. { already written ? }
  523. if p.indexnr<>-1 then
  524. exit;
  525. { calculate symbol index }
  526. if (p.currbind<>AB_LOCAL) then
  527. begin
  528. { insert the symbol in the local index, the indexarray
  529. will take care of the numbering }
  530. symbols.insert(p);
  531. end
  532. else
  533. p.indexnr:=-2; { local }
  534. end;
  535. procedure tcoffobjectdata.writereloc(data,len:aint;p:tasmsymbol;relative:TAsmRelocationType);
  536. var
  537. curraddr,
  538. symaddr : longint;
  539. begin
  540. if currsec=nil then
  541. internalerror(200403072);
  542. if assigned(p) then
  543. begin
  544. { current address }
  545. curraddr:=currsec.mempos+currsec.datasize;
  546. { external/common symbols don't have a fixed memory position yet }
  547. if (p.currbind=AB_COMMON) then
  548. begin
  549. { For go32v2 we need to use the size as address }
  550. if not win32 then
  551. symaddr:=p.size
  552. else
  553. symaddr:=0;
  554. end
  555. else
  556. begin
  557. symaddr:=p.address;
  558. if assigned(p.section) then
  559. inc(symaddr,p.section.mempos);
  560. end;
  561. { no symbol relocation need inside a section }
  562. if (p.section=currsec) and
  563. (p.currbind<>AB_COMMON) then
  564. begin
  565. case relative of
  566. RELOC_ABSOLUTE :
  567. begin
  568. currsec.addsectionreloc(curraddr,currsec,RELOC_ABSOLUTE);
  569. inc(data,symaddr);
  570. end;
  571. RELOC_RELATIVE :
  572. begin
  573. inc(data,symaddr-len-currsec.datasize);
  574. end;
  575. RELOC_RVA :
  576. begin
  577. currsec.addsectionreloc(curraddr,currsec,RELOC_RVA);
  578. inc(data,symaddr);
  579. end;
  580. end;
  581. end
  582. else
  583. begin
  584. writesymbol(p);
  585. if (p.section<>nil) and
  586. (p.currbind<>AB_COMMON) and
  587. (relative<>RELOC_RELATIVE) then
  588. currsec.addsectionreloc(curraddr,p.section,relative)
  589. else
  590. currsec.addsymreloc(curraddr,p,relative);
  591. if (not win32) or
  592. ((relative<>RELOC_RELATIVE) and (p.section<>nil)) then
  593. inc(data,symaddr);
  594. if relative=RELOC_RELATIVE then
  595. begin
  596. if win32 then
  597. dec(data,len-4)
  598. else
  599. dec(data,len+currsec.datasize);
  600. end;
  601. end;
  602. end;
  603. currsec.write(data,len);
  604. end;
  605. procedure tcoffobjectdata.writestab(offset:aint;ps:tasmsymbol;nidx,nother,line:longint;p:pchar);
  606. var
  607. stab : coffstab;
  608. curraddr : longint;
  609. begin
  610. { Win32 does not need an offset if a symbol is provided }
  611. if win32 and assigned(ps) then
  612. offset:=0;
  613. if assigned(p) and (p[0]<>#0) then
  614. begin
  615. stab.strpos:=StabStrSec.datasize;
  616. StabStrSec.write(p^,strlen(p)+1);
  617. end
  618. else
  619. stab.strpos:=0;
  620. stab.ntype:=nidx;
  621. stab.ndesc:=line;
  622. stab.nother:=nother;
  623. stab.nvalue:=offset;
  624. StabsSec.write(stab,sizeof(stab));
  625. if assigned(ps) then
  626. begin
  627. { current address }
  628. curraddr:=StabsSec.mempos+StabsSec.datasize;
  629. if DLLSource and RelocSection then
  630. { avoid relocation in the .stab section
  631. because it ends up in the .reloc section instead }
  632. StabsSec.addsymreloc(curraddr-4,ps,RELOC_RVA)
  633. else
  634. StabsSec.addsymreloc(curraddr-4,ps,RELOC_ABSOLUTE);
  635. end;
  636. end;
  637. procedure tcoffobjectdata.section_mempos(p:tnamedindexitem;arg:pointer);
  638. begin
  639. tcoffobjectsection(p).memsize:=tcoffobjectsection(p).datasize;
  640. { memory position is in arg }
  641. if not win32 then
  642. begin
  643. tcoffobjectsection(p).mempos:=plongint(arg)^;
  644. inc(plongint(arg)^,align(tcoffobjectsection(p).memsize,tcoffobjectsection(p).addralign));
  645. end;
  646. end;
  647. procedure tcoffobjectdata.beforealloc;
  648. begin
  649. { create stabs sections if debugging }
  650. if (cs_debuginfo in aktmoduleswitches) then
  651. begin
  652. StabsSec.Alloc(sizeof(coffstab));
  653. StabStrSec.Alloc(length(SplitFileName(current_module.mainsource^))+2);
  654. end;
  655. end;
  656. procedure tcoffobjectdata.beforewrite;
  657. var
  658. s : string;
  659. begin
  660. { create stabs sections if debugging }
  661. if (cs_debuginfo in aktmoduleswitches) then
  662. begin
  663. writestab(0,nil,0,0,0,nil);
  664. { write zero pchar and name together (PM) }
  665. s:=#0+SplitFileName(current_module.mainsource^)+#0;
  666. stabstrsec.write(s[1],length(s));
  667. end;
  668. end;
  669. procedure tcoffobjectdata.afteralloc;
  670. var
  671. mempos : longint;
  672. begin
  673. { if debug then also count header stab }
  674. if (cs_debuginfo in aktmoduleswitches) then
  675. begin
  676. StabsSec.Alloc(sizeof(coffstab));
  677. StabStrSec.Alloc(length(SplitFileName(current_module.mainsource^))+2);
  678. end;
  679. { calc mempos }
  680. mempos:=0;
  681. sects.foreach(@section_mempos,@mempos);
  682. end;
  683. {****************************************************************************
  684. tdjcoffobjectdata
  685. ****************************************************************************}
  686. constructor tdjcoffobjectdata.create(const n:string);
  687. begin
  688. inherited createcoff(n,false,tdjcoffobjectsection);
  689. end;
  690. {****************************************************************************
  691. tpecoffobjectdata
  692. ****************************************************************************}
  693. constructor tpecoffobjectdata.create(const n:string);
  694. begin
  695. inherited createcoff(n,true,tpecoffobjectsection);
  696. end;
  697. {****************************************************************************
  698. tcoffobjectoutput
  699. ****************************************************************************}
  700. constructor tcoffobjectoutput.createdjgpp(smart:boolean);
  701. begin
  702. inherited create(smart);
  703. win32:=false;
  704. end;
  705. constructor tcoffobjectoutput.createwin32(smart:boolean);
  706. begin
  707. inherited create(smart);
  708. win32:=true;
  709. end;
  710. function tcoffobjectoutput.newobjectdata(const n:string):TAsmObjectData;
  711. begin
  712. if win32 then
  713. result:=tpecoffobjectdata.create(n)
  714. else
  715. result:=tdjcoffobjectdata.create(n);
  716. end;
  717. procedure tcoffobjectoutput.write_symbol(const name:string;value,section,typ,aux:longint);
  718. var
  719. sym : coffsymbol;
  720. begin
  721. FillChar(sym,sizeof(sym),0);
  722. { symbolname }
  723. if length(name)>8 then
  724. begin
  725. sym.strpos:=FCoffStrs.size+4;
  726. FCoffStrs.writestr(name);
  727. FCoffStrs.writestr(#0);
  728. end
  729. else
  730. move(name[1],sym.name,length(name));
  731. sym.value:=value;
  732. sym.section:=section;
  733. sym.typ:=typ;
  734. sym.aux:=aux;
  735. FWriter.write(sym,sizeof(sym));
  736. end;
  737. procedure tcoffobjectoutput.section_write_symbol(p:tnamedindexitem;arg:pointer);
  738. var
  739. secrec : coffsectionrec;
  740. begin
  741. write_symbol(tasmsection(p).name,tasmsection(p).mempos,tasmsection(p).secsymidx,3,1);
  742. fillchar(secrec,sizeof(secrec),0);
  743. secrec.len:=tasmsection(p).aligneddatasize;
  744. secrec.nrelocs:=tasmsection(p).relocations.count;
  745. FWriter.write(secrec,sizeof(secrec));
  746. end;
  747. procedure tcoffobjectoutput.section_write_relocs(p:tnamedindexitem;arg:pointer);
  748. var
  749. rel : coffreloc;
  750. r : TAsmRelocation;
  751. begin
  752. r:=TasmRelocation(tasmsection(p).relocations.first);
  753. while assigned(r) do
  754. begin
  755. rel.address:=r.address;
  756. if assigned(r.symbol) then
  757. begin
  758. if (r.symbol.currbind=AB_LOCAL) then
  759. rel.sym:=2*r.symbol.section.secsymidx
  760. else
  761. begin
  762. if r.symbol.indexnr=-1 then
  763. internalerror(4321);
  764. { indexnr starts with 1, coff starts with 0 }
  765. rel.sym:=r.symbol.indexnr+initsym-1;
  766. end;
  767. end
  768. else
  769. begin
  770. if r.section<>nil then
  771. rel.sym:=2*r.section.secsymidx
  772. else
  773. rel.sym:=0;
  774. end;
  775. case r.typ of
  776. RELOC_RELATIVE :
  777. rel.relative:=$14;
  778. RELOC_ABSOLUTE :
  779. rel.relative:=$6;
  780. RELOC_RVA :
  781. rel.relative:=$7;
  782. end;
  783. FWriter.write(rel,sizeof(rel));
  784. r:=TAsmRelocation(r.next);
  785. end;
  786. end;
  787. procedure tcoffobjectoutput.write_symbols(data:TAsmObjectData);
  788. var
  789. filename : string[18];
  790. value : longint;
  791. sectionval,
  792. globalval : byte;
  793. p : tasmsymbol;
  794. begin
  795. with tcoffobjectdata(data) do
  796. begin
  797. { The `.file' record, and the file name auxiliary record }
  798. write_symbol('.file', 0, -2, $67, 1);
  799. fillchar(filename,sizeof(filename),0);
  800. filename:=SplitFileName(current_module.mainsource^);
  801. FWriter.write(filename[1],sizeof(filename)-1);
  802. { The section records, with their auxiliaries, also store the
  803. symbol index }
  804. Sects.foreach(@section_write_symbol,nil);
  805. { The symbols used }
  806. p:=Tasmsymbol(symbols.First);
  807. while assigned(p) do
  808. begin
  809. if assigned(p.section) and
  810. (p.currbind<>AB_COMMON) then
  811. sectionval:=p.section.secsymidx
  812. else
  813. sectionval:=0;
  814. if p.currbind=AB_LOCAL then
  815. globalval:=3
  816. else
  817. globalval:=2;
  818. { if local of global then set the section value to the address
  819. of the symbol }
  820. if p.currbind in [AB_LOCAL,AB_GLOBAL] then
  821. value:=p.address+p.section.mempos
  822. else
  823. value:=p.size;
  824. { symbolname }
  825. write_symbol(p.name,value,sectionval,globalval,0);
  826. p:=tasmsymbol(p.indexnext);
  827. end;
  828. end;
  829. end;
  830. procedure tcoffobjectoutput.section_set_secsymidx(p:tnamedindexitem;arg:pointer);
  831. begin
  832. inc(plongint(arg)^);
  833. tasmsection(p).secsymidx:=plongint(arg)^;
  834. end;
  835. procedure tcoffobjectoutput.section_set_datapos(p:tnamedindexitem;arg:pointer);
  836. begin
  837. tasmsection(p).datapos:=plongint(arg)^;
  838. if not(aso_alloconly in tasmsection(p).secoptions) then
  839. inc(plongint(arg)^,tasmsection(p).aligneddatasize);
  840. end;
  841. procedure tcoffobjectoutput.section_set_reloc_datapos(p:tnamedindexitem;arg:pointer);
  842. begin
  843. TCoffObjectSection(p).coffrelocpos:=plongint(arg)^;
  844. inc(plongint(arg)^,sizeof(coffreloc)*tasmsection(p).relocations.count);
  845. end;
  846. procedure tcoffobjectoutput.section_write_header(p:tnamedindexitem;arg:pointer);
  847. var
  848. sechdr : coffsechdr;
  849. s : string;
  850. begin
  851. fillchar(sechdr,sizeof(sechdr),0);
  852. s:=tasmsection(p).name;
  853. { section names are limited to 8 chars }
  854. if length(s)>8 then
  855. internalerror(200403312);
  856. move(s[1],sechdr.name,length(s));
  857. if not win32 then
  858. begin
  859. sechdr.rvaofs:=tasmsection(p).mempos;
  860. sechdr.vsize:=tasmsection(p).mempos;
  861. end
  862. else
  863. begin
  864. if tasmsection(p).sectype=sec_bss then
  865. sechdr.vsize:=tasmsection(p).aligneddatasize;
  866. end;
  867. sechdr.datasize:=tasmsection(p).aligneddatasize;
  868. if (tasmsection(p).datasize>0) and
  869. not(aso_alloconly in tasmsection(p).secoptions) then
  870. sechdr.datapos:=tasmsection(p).datapos;
  871. sechdr.nrelocs:=tasmsection(p).relocations.count;
  872. sechdr.relocpos:=TCoffObjectSection(p).coffrelocpos;
  873. sechdr.flags:=TCoffObjectSection(p).flags;
  874. FWriter.write(sechdr,sizeof(sechdr));
  875. end;
  876. procedure tcoffobjectoutput.section_write_data(p:tnamedindexitem;arg:pointer);
  877. var
  878. hp : pdynamicblock;
  879. begin
  880. if (aso_alloconly in tasmsection(p).secoptions) then
  881. exit;
  882. if tasmsection(p).data=nil then
  883. internalerror(200403073);
  884. tasmsection(p).alignsection;
  885. hp:=tasmsection(p).data.firstblock;
  886. while assigned(hp) do
  887. begin
  888. FWriter.write(hp^.data,hp^.used);
  889. hp:=hp^.next;
  890. end;
  891. end;
  892. function tcoffobjectoutput.writedata(data:TAsmObjectData):boolean;
  893. var
  894. orgdatapos,
  895. datapos,
  896. nsects,
  897. sympos,i : longint;
  898. hstab : coffstab;
  899. gotreloc : boolean;
  900. header : coffheader;
  901. empty : array[0..15] of byte;
  902. hp : pdynamicblock;
  903. s : string;
  904. begin
  905. result:=false;
  906. FCoffStrs:=TDynamicArray.Create(strsresize);
  907. with tcoffobjectdata(data) do
  908. begin
  909. { calc amount of sections we have }
  910. fillchar(empty,sizeof(empty),0);
  911. nsects:=0;
  912. Sects.foreach(@section_set_secsymidx,@nsects);
  913. initsym:=2+nsects*2; { 2 for the file }
  914. { For the stab section we need an HdrSym which can now be
  915. calculated more easily }
  916. if StabsSec<>nil then
  917. begin
  918. { first stabs for main source }
  919. writestab(0,nil,0,0,0,nil);
  920. s:=#0+SplitFileName(current_module.mainsource^)+#0;
  921. stabstrsec.write(s[1],length(s));
  922. { header stab }
  923. hstab.strpos:=1;
  924. hstab.ntype:=0;
  925. hstab.nother:=0;
  926. hstab.ndesc:=(StabsSec.datasize div sizeof(coffstab))-1{+1 according to gas output PM};
  927. hstab.nvalue:=StabStrSec.datasize;
  928. StabsSec.data.seek(0);
  929. StabsSec.data.write(hstab,sizeof(hstab));
  930. end;
  931. { Calculate the filepositions }
  932. datapos:=sizeof(coffheader)+sizeof(coffsechdr)*nsects;
  933. { sections first }
  934. Sects.foreach(@section_set_datapos,@datapos);
  935. { relocs }
  936. orgdatapos:=datapos;
  937. Sects.foreach(@section_set_reloc_datapos,@datapos);
  938. gotreloc:=(orgdatapos<>datapos);
  939. { symbols }
  940. sympos:=datapos;
  941. { COFF header }
  942. fillchar(header,sizeof(coffheader),0);
  943. {$ifdef i386}
  944. header.mach:=$14c;
  945. {$endif i386}
  946. {$ifdef arm}
  947. header.mach:=$1c0;
  948. {$endif arm}
  949. header.nsects:=nsects;
  950. header.sympos:=sympos;
  951. header.syms:=symbols.count+initsym;
  952. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES;
  953. if not gotreloc then
  954. header.flag:=header.flag or COFF_FLAG_NORELOCS;
  955. FWriter.write(header,sizeof(header));
  956. { Section headers }
  957. Sects.foreach(@section_write_header,nil);
  958. { Sections }
  959. Sects.foreach(@section_write_data,nil);
  960. { Relocs }
  961. Sects.foreach(@section_write_relocs,nil);
  962. { Symbols }
  963. write_symbols(data);
  964. { Strings }
  965. i:=FCoffStrs.size+4;
  966. FWriter.write(i,4);
  967. hp:=FCoffStrs.firstblock;
  968. while assigned(hp) do
  969. begin
  970. FWriter.write(hp^.data,hp^.used);
  971. hp:=hp^.next;
  972. end;
  973. end;
  974. FCoffStrs.Free;
  975. end;
  976. {****************************************************************************
  977. tcoffexeoutput
  978. ****************************************************************************}
  979. constructor tcoffexeoutput.createdjgpp;
  980. begin
  981. inherited create;
  982. win32:=false;
  983. end;
  984. constructor tcoffexeoutput.createwin32;
  985. begin
  986. inherited create;
  987. win32:=true;
  988. end;
  989. function tcoffexeoutput.newobjectinput:tobjectinput;
  990. begin
  991. if win32 then
  992. result:=tcoffobjectinput.createwin32
  993. else
  994. result:=tcoffobjectinput.createdjgpp;
  995. end;
  996. procedure tcoffexeoutput.write_symbol(const name:string;value,section,typ,aux:longint);
  997. var
  998. sym : coffsymbol;
  999. begin
  1000. FillChar(sym,sizeof(sym),0);
  1001. if length(name)>8 then
  1002. begin
  1003. sym.strpos:=FCoffStrs.size+4;
  1004. FCoffStrs.writestr(name);
  1005. FCoffStrs.writestr(#0);
  1006. end
  1007. else
  1008. move(name[1],sym.name,length(name));
  1009. sym.value:=value;
  1010. sym.section:=section;
  1011. sym.typ:=typ;
  1012. sym.aux:=aux;
  1013. FWriter.write(sym,sizeof(sym));
  1014. end;
  1015. procedure tcoffexeoutput.write_symbols;
  1016. var
  1017. value,
  1018. sectionval,
  1019. globalval : byte;
  1020. objdata : TAsmObjectData;
  1021. p : tasmsymbol;
  1022. begin
  1023. {$ifdef internallinker}
  1024. objdata:=TAsmObjectData(objdatalist.first);
  1025. while assigned(objdata) do
  1026. begin
  1027. with tcoffobjectdata(objdata) do
  1028. begin
  1029. { The symbols used }
  1030. p:=Tasmsymbol(symbols.First);
  1031. while assigned(p) do
  1032. begin
  1033. if p.section=sec_common then
  1034. sectionval:=sections[sec_bss].secsymidx
  1035. else
  1036. sectionval:=sections[p.section.name].secsymidx;
  1037. if p.currbind=AB_LOCAL then
  1038. globalval:=3
  1039. else
  1040. globalval:=2;
  1041. { if local of global then set the section value to the address
  1042. of the symbol }
  1043. if p.currbind in [AB_LOCAL,AB_GLOBAL] then
  1044. value:=p.address
  1045. else
  1046. value:=p.size;
  1047. { symbolname }
  1048. s:=p.name;
  1049. if length(s)>8 then
  1050. begin
  1051. nameidx:=FCoffStrs.size+4;
  1052. FCoffStrs.writestr(s);
  1053. FCoffStrs.writestr(#0);
  1054. end
  1055. else
  1056. begin
  1057. nameidx:=-1;
  1058. namestr:=s;
  1059. end;
  1060. write_symbol(namestr,nameidx,value,sectionval,globalval,0);
  1061. p:=tasmsymbol(p.indexnext);
  1062. end;
  1063. end;
  1064. objdata:=TAsmObjectData(objdata.next);
  1065. end;
  1066. {$endif internallinker}
  1067. end;
  1068. procedure tcoffexeoutput.CalculateMemoryMap;
  1069. var
  1070. objdata : TAsmObjectData;
  1071. secsymidx,
  1072. mempos,
  1073. datapos : longint;
  1074. begin
  1075. {$ifdef internallinker}
  1076. { retrieve amount of sections }
  1077. nsects:=0;
  1078. secsymidx:=0;
  1079. for sec:=low(TSection) to high(TSection) do
  1080. begin
  1081. if sections[sec].available then
  1082. begin
  1083. inc(nsects);
  1084. inc(secsymidx);
  1085. sections[sec].secsymidx:=secsymidx;
  1086. end;
  1087. end;
  1088. { calculate start positions after the headers }
  1089. datapos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
  1090. mempos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
  1091. if not win32 then
  1092. inc(mempos,sizeof(go32v2stub)+$1000);
  1093. { add sections }
  1094. MapObjectdata(datapos,mempos);
  1095. { end symbol }
  1096. AddGlobalSym('_etext',sections[sec_code].mempos+sections[sec_code].memsize);
  1097. AddGlobalSym('_edata',sections[sec_data].mempos+sections[sec_data].memsize);
  1098. AddGlobalSym('end',mempos);
  1099. { symbols }
  1100. nsyms:=0;
  1101. sympos:=0;
  1102. if not(cs_link_strip in aktglobalswitches) then
  1103. begin
  1104. sympos:=datapos;
  1105. objdata:=TAsmObjectData(objdatalist.first);
  1106. while assigned(objdata) do
  1107. begin
  1108. inc(nsyms,objdata.symbols.count);
  1109. objdata:=TAsmObjectData(objdata.next);
  1110. end;
  1111. end;
  1112. {$endif internallinker}
  1113. end;
  1114. function tcoffexeoutput.writedata:boolean;
  1115. var
  1116. i : longint;
  1117. header : coffheader;
  1118. optheader : coffoptheader;
  1119. sechdr : coffsechdr;
  1120. hp : pdynamicblock;
  1121. objdata : TAsmObjectData;
  1122. hsym : tasmsymbol;
  1123. begin
  1124. result:=false;
  1125. {$ifdef internallinker}
  1126. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1127. FCoffStrs:=TDynamicArray.Create(strsresize);
  1128. { Stub }
  1129. if not win32 then
  1130. FWriter.write(go32v2stub,sizeof(go32v2stub));
  1131. { COFF header }
  1132. fillchar(header,sizeof(header),0);
  1133. header.mach:=$14c;
  1134. header.nsects:=nsects;
  1135. header.sympos:=sympos;
  1136. header.syms:=nsyms;
  1137. header.opthdr:=sizeof(coffoptheader);
  1138. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  1139. FWriter.write(header,sizeof(header));
  1140. { Optional COFF Header }
  1141. fillchar(optheader,sizeof(optheader),0);
  1142. optheader.magic:=$10b;
  1143. optheader.tsize:=sections[sec_code].memsize;
  1144. optheader.dsize:=sections[sec_data].memsize;
  1145. optheader.bsize:=sections[sec_bss].memsize;
  1146. hsym:=tasmsymbol(globalsyms.search('start'));
  1147. if not assigned(hsym) then
  1148. begin
  1149. Comment(V_Error,'Entrypoint "start" not defined');
  1150. exit;
  1151. end;
  1152. optheader.entry:=hsym.address;
  1153. optheader.text_start:=sections[sec_code].mempos;
  1154. optheader.data_start:=sections[sec_data].mempos;
  1155. FWriter.write(optheader,sizeof(optheader));
  1156. { Section headers }
  1157. for sec:=low(TSection) to high(TSection) do
  1158. if sections[sec].available then
  1159. begin
  1160. fillchar(sechdr,sizeof(sechdr),0);
  1161. move(target_asm.secnames[sec][1],sechdr.name,length(target_asm.secnames[sec]));
  1162. if not win32 then
  1163. begin
  1164. sechdr.rvaofs:=sections[sec].mempos;
  1165. sechdr.vsize:=sections[sec].mempos;
  1166. end
  1167. else
  1168. begin
  1169. if sec=sec_bss then
  1170. sechdr.vsize:=sections[sec].memsize;
  1171. end;
  1172. if sec=sec_bss then
  1173. sechdr.datasize:=sections[sec].memsize
  1174. else
  1175. begin
  1176. sechdr.datasize:=sections[sec].datasize;
  1177. sechdr.datapos:=sections[sec].datapos;
  1178. end;
  1179. sechdr.nrelocs:=0;
  1180. sechdr.relocpos:=0;
  1181. sechdr.flags:=sections[sec].flags;
  1182. FWriter.write(sechdr,sizeof(sechdr));
  1183. end;
  1184. { Sections }
  1185. for sec:=low(TSection) to high(TSection) do
  1186. if sections[sec].available then
  1187. begin
  1188. { update objectfiles }
  1189. objdata:=TAsmObjectData(objdatalist.first);
  1190. while assigned(objdata) do
  1191. begin
  1192. if assigned(objdata.sects[sec]) and
  1193. assigned(objdata.sects[sec].data) then
  1194. begin
  1195. FWriter.WriteZeros(objdata.sects[sec].dataalignbytes);
  1196. hp:=objdata.sects[sec].data.firstblock;
  1197. while assigned(hp) do
  1198. begin
  1199. FWriter.write(hp^.data,hp^.used);
  1200. hp:=hp^.next;
  1201. end;
  1202. end;
  1203. objdata:=TAsmObjectData(objdata.next);
  1204. end;
  1205. end;
  1206. { Optional symbols }
  1207. if not(cs_link_strip in aktglobalswitches) then
  1208. begin
  1209. { Symbols }
  1210. write_symbols;
  1211. { Strings }
  1212. i:=FCoffStrs.size+4;
  1213. FWriter.write(i,4);
  1214. hp:=FCoffStrs.firstblock;
  1215. while assigned(hp) do
  1216. begin
  1217. FWriter.write(hp^.data,hp^.used);
  1218. hp:=hp^.next;
  1219. end;
  1220. end;
  1221. { Release }
  1222. FCoffStrs.Free;
  1223. FCoffSyms.Free;
  1224. result:=true;
  1225. {$endif internallinker}
  1226. end;
  1227. procedure tcoffexeoutput.GenerateExecutable(const fn:string);
  1228. begin
  1229. AddGlobalSym('_etext',0);
  1230. AddGlobalSym('_edata',0);
  1231. AddGlobalSym('end',0);
  1232. if not CalculateSymbols then
  1233. exit;
  1234. CalculateMemoryMap;
  1235. FixupSymbols;
  1236. FixupRelocations;
  1237. writeexefile(fn);
  1238. end;
  1239. {****************************************************************************
  1240. tcoffobjectinput
  1241. ****************************************************************************}
  1242. constructor tcoffobjectinput.createdjgpp;
  1243. begin
  1244. inherited create;
  1245. win32:=false;
  1246. end;
  1247. constructor tcoffobjectinput.createwin32;
  1248. begin
  1249. inherited create;
  1250. win32:=true;
  1251. end;
  1252. function tcoffobjectinput.newobjectdata(const n:string):TAsmObjectData;
  1253. begin
  1254. if win32 then
  1255. result:=tpecoffobjectdata.create(n)
  1256. else
  1257. result:=tdjcoffobjectdata.create(n);
  1258. end;
  1259. procedure tcoffobjectinput.read_relocs(s:TCoffObjectSection);
  1260. var
  1261. rel : coffreloc;
  1262. rel_type : TAsmRelocationType;
  1263. i : longint;
  1264. p : tasmsymbol;
  1265. begin
  1266. for i:=1 to s.coffrelocs do
  1267. begin
  1268. FReader.read(rel,sizeof(rel));
  1269. case rel.relative of
  1270. $14 : rel_type:=RELOC_RELATIVE;
  1271. $06 : rel_type:=RELOC_ABSOLUTE;
  1272. $07 : rel_type:=RELOC_RVA;
  1273. else
  1274. begin
  1275. Comment(V_Error,'Error reading coff file');
  1276. exit;
  1277. end;
  1278. end;
  1279. p:=FSymTbl^[rel.sym].sym;
  1280. if assigned(p) then
  1281. begin
  1282. s.addsymsizereloc(rel.address-s.mempos,p,FSymTbl^[rel.sym].orgsize,rel_type);
  1283. end
  1284. else
  1285. begin
  1286. Comment(V_Error,'Error reading coff file');
  1287. exit;
  1288. end;
  1289. end;
  1290. end;
  1291. procedure tcoffobjectinput.handle_symbols(data:TAsmObjectData);
  1292. var
  1293. size,
  1294. address,
  1295. i,nsyms,
  1296. symidx : longint;
  1297. sym : coffsymbol;
  1298. strname : string;
  1299. p : tasmsymbol;
  1300. bind : Tasmsymbind;
  1301. auxrec : array[0..17] of byte;
  1302. begin
  1303. {$ifdef internallinker}
  1304. with tcoffobjectdata(data) do
  1305. begin
  1306. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1307. { Allocate memory for symidx -> tasmsymbol table }
  1308. GetMem(FSymTbl,nsyms*sizeof(ttasmsymbolrec));
  1309. FillChar(FSymTbl^,nsyms*sizeof(ttasmsymbolrec),0);
  1310. { Loop all symbols }
  1311. FCoffSyms.Seek(0);
  1312. symidx:=0;
  1313. while (symidx<nsyms) do
  1314. begin
  1315. FCoffSyms.Read(sym,sizeof(sym));
  1316. if plongint(@sym.name)^<>0 then
  1317. begin
  1318. move(sym.name,strname[1],8);
  1319. strname[9]:=#0;
  1320. end
  1321. else
  1322. begin
  1323. FCoffStrs.Seek(sym.strpos-4);
  1324. FCoffStrs.Read(strname[1],255);
  1325. strname[255]:=#0;
  1326. end;
  1327. strname[0]:=chr(strlen(@strname[1]));
  1328. if strname='' then
  1329. Internalerror(200205172);
  1330. bind:=AB_EXTERNAL;
  1331. sec:=sec_none;
  1332. size:=0;
  1333. address:=0;
  1334. case sym.typ of
  1335. COFF_SYM_GLOBAL :
  1336. begin
  1337. if sym.section=0 then
  1338. begin
  1339. if sym.value=0 then
  1340. bind:=AB_EXTERNAL
  1341. else
  1342. begin
  1343. bind:=AB_COMMON;
  1344. size:=sym.value;
  1345. end;
  1346. end
  1347. else
  1348. begin
  1349. bind:=AB_GLOBAL;
  1350. sec:=Fidx2sec[sym.section];
  1351. if assigned(sects[sec]) then
  1352. begin
  1353. if sym.value>=sects[sec].mempos then
  1354. address:=sym.value-sects[sec].mempos
  1355. else
  1356. internalerror(432432432);
  1357. end
  1358. else
  1359. internalerror(34243214);
  1360. end;
  1361. p:=TAsmSymbol.Create(strname,bind,AT_FUNCTION);
  1362. p.SetAddress(0,sec,address,size);
  1363. p.objectdata:=data;
  1364. symbols.insert(p);
  1365. end;
  1366. COFF_SYM_LABEL,
  1367. COFF_SYM_LOCAL :
  1368. begin
  1369. { do not add constants (section=-1) }
  1370. if sym.section<>-1 then
  1371. begin
  1372. bind:=AB_LOCAL;
  1373. sec:=Fidx2sec[sym.section];
  1374. if assigned(sects[sec]) then
  1375. begin
  1376. if sym.value>=sects[sec].mempos then
  1377. address:=sym.value-sects[sec].mempos
  1378. else
  1379. internalerror(432432432);
  1380. end
  1381. else
  1382. internalerror(34243214);
  1383. p:=TAsmSymbol.Create(strname,bind,AT_FUNCTION);
  1384. p.SetAddress(0,sec,address,size);
  1385. p.objectdata:=data;
  1386. symbols.insert(p);
  1387. end;
  1388. end;
  1389. COFF_SYM_SECTION,
  1390. COFF_SYM_FUNCTION,
  1391. COFF_SYM_FILE :
  1392. ;
  1393. else
  1394. internalerror(4342343);
  1395. end;
  1396. FSymTbl^[symidx].sym:=p;
  1397. FSymTbl^[symidx].orgsize:=size;
  1398. { read aux records }
  1399. for i:=1 to sym.aux do
  1400. begin
  1401. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1402. inc(symidx);
  1403. end;
  1404. inc(symidx);
  1405. end;
  1406. end;
  1407. {$endif internallinker}
  1408. end;
  1409. function tcoffobjectinput.readobjectdata(data:TAsmObjectData):boolean;
  1410. var
  1411. strsize,
  1412. i : longint;
  1413. header : coffheader;
  1414. sechdr : coffsechdr;
  1415. secname : array[0..15] of char;
  1416. begin
  1417. result:=false;
  1418. {$ifdef internallinker}
  1419. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1420. FCoffStrs:=TDynamicArray.Create(strsresize);
  1421. with tcoffobjectdata(data) do
  1422. begin
  1423. FillChar(Fidx2sec,sizeof(Fidx2sec),0);
  1424. { Read COFF header }
  1425. if not reader.read(header,sizeof(coffheader)) then
  1426. begin
  1427. Comment(V_Error,'Error reading coff file');
  1428. exit;
  1429. end;
  1430. {$ifdef i386}
  1431. if header.mach<>$14c then
  1432. {$endif i386}
  1433. {$ifdef arm}
  1434. if header.mach<>$1c0 then
  1435. {$endif arm}
  1436. begin
  1437. Comment(V_Error,'Not a coff file');
  1438. exit;
  1439. end;
  1440. if header.nsects>255 then
  1441. begin
  1442. Comment(V_Error,'To many sections');
  1443. exit;
  1444. end;
  1445. { Section headers }
  1446. for i:=1 to header.nsects do
  1447. begin
  1448. if not reader.read(sechdr,sizeof(sechdr)) then
  1449. begin
  1450. Comment(V_Error,'Error reading coff file');
  1451. exit;
  1452. end;
  1453. move(sechdr.name,secname,8);
  1454. secname[8]:=#0;
  1455. sec:=str2sec(strpas(secname));
  1456. if sec<>sec_none then
  1457. begin
  1458. Fidx2sec[i]:=sec;
  1459. createsection(sec);
  1460. if not win32 then
  1461. sects[sec].mempos:=sechdr.rvaofs;
  1462. TCoffObjectSection(sects[sec]).coffrelocs:=sechdr.nrelocs;
  1463. TCoffObjectSection(sects[sec]).coffrelocpos:=sechdr.relocpos;
  1464. sects[sec].datapos:=sechdr.datapos;
  1465. sects[sec].datasize:=sechdr.datasize;
  1466. sects[sec].memsize:=sechdr.datasize;
  1467. TCoffObjectSection(sects[sec]).orgmempos:=sects[sec].mempos;
  1468. sects[sec].flags:=sechdr.flags;
  1469. end
  1470. else
  1471. Comment(V_Warning,'skipping unsupported section '+strpas(sechdr.name));
  1472. end;
  1473. { Symbols }
  1474. Reader.Seek(header.sympos);
  1475. if not Reader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1476. begin
  1477. Comment(V_Error,'Error reading coff file');
  1478. exit;
  1479. end;
  1480. { Strings }
  1481. if not Reader.Read(strsize,4) then
  1482. begin
  1483. Comment(V_Error,'Error reading coff file');
  1484. exit;
  1485. end;
  1486. if strsize<4 then
  1487. begin
  1488. Comment(V_Error,'Error reading coff file');
  1489. exit;
  1490. end;
  1491. if not Reader.ReadArray(FCoffStrs,Strsize-4) then
  1492. begin
  1493. Comment(V_Error,'Error reading coff file');
  1494. exit;
  1495. end;
  1496. { Insert all symbols }
  1497. handle_symbols(data);
  1498. { Sections }
  1499. for sec:=low(TSection) to high(TSection) do
  1500. if assigned(sects[sec]) and
  1501. (sec<>sec_bss) then
  1502. begin
  1503. Reader.Seek(sects[sec].datapos);
  1504. if not Reader.ReadArray(sects[sec].data,sects[sec].datasize) then
  1505. begin
  1506. Comment(V_Error,'Error reading coff file');
  1507. exit;
  1508. end;
  1509. end;
  1510. { Relocs }
  1511. for sec:=low(TSection) to high(TSection) do
  1512. if assigned(sects[sec]) and
  1513. (TCoffObjectSection(sects[sec]).coffrelocs>0) then
  1514. begin
  1515. Reader.Seek(TCoffObjectSection(sects[sec]).coffrelocpos);
  1516. read_relocs(TCoffObjectSection(sects[sec]));
  1517. end;
  1518. end;
  1519. FCoffStrs.Free;
  1520. FCoffSyms.Free;
  1521. result:=true;
  1522. {$endif internallinker}
  1523. end;
  1524. {****************************************************************************
  1525. TCoffAssembler
  1526. ****************************************************************************}
  1527. constructor TCoffAssembler.Create(smart:boolean);
  1528. begin
  1529. inherited Create(smart);
  1530. objectoutput:=tcoffobjectoutput.createdjgpp(smart);
  1531. end;
  1532. {****************************************************************************
  1533. TPECoffAssembler
  1534. ****************************************************************************}
  1535. constructor TPECoffAssembler.Create(smart:boolean);
  1536. begin
  1537. inherited Create(smart);
  1538. objectoutput:=tcoffobjectoutput.createwin32(smart);
  1539. end;
  1540. {****************************************************************************
  1541. TCoffLinker
  1542. ****************************************************************************}
  1543. constructor TCoffLinker.Create;
  1544. begin
  1545. inherited Create;
  1546. exeoutput:=tcoffexeoutput.createdjgpp;
  1547. end;
  1548. {*****************************************************************************
  1549. Initialize
  1550. *****************************************************************************}
  1551. const
  1552. as_i386_coff_info : tasminfo =
  1553. (
  1554. id : as_i386_coff;
  1555. idtxt : 'COFF';
  1556. asmbin : '';
  1557. asmcmd : '';
  1558. supported_target : system_i386_go32v2;
  1559. flags : [af_outputbinary];
  1560. labelprefix : '.L';
  1561. comment : '';
  1562. );
  1563. as_i386_pecoff_info : tasminfo =
  1564. (
  1565. id : as_i386_pecoff;
  1566. idtxt : 'PECOFF';
  1567. asmbin : '';
  1568. asmcmd : '';
  1569. supported_target : system_i386_win32;
  1570. flags : [af_outputbinary];
  1571. labelprefix : '.L';
  1572. comment : '';
  1573. );
  1574. as_i386_pecoffwdosx_info : tasminfo =
  1575. (
  1576. id : as_i386_pecoffwdosx;
  1577. idtxt : 'PEWDOSX';
  1578. asmbin : '';
  1579. asmcmd : '';
  1580. supported_target : system_i386_wdosx;
  1581. flags : [af_outputbinary];
  1582. labelprefix : '.L';
  1583. comment : '';
  1584. );
  1585. as_i386_pecoffwince_info : tasminfo =
  1586. (
  1587. id : as_i386_pecoffwince;
  1588. idtxt : 'PECOFFWINCE';
  1589. asmbin : '';
  1590. asmcmd : '';
  1591. supported_target : system_i386_wince;
  1592. flags : [af_outputbinary];
  1593. labelprefix : '.L';
  1594. comment : '';
  1595. );
  1596. as_arm_pecoffwince_info : tasminfo =
  1597. (
  1598. id : as_arm_pecoffwince;
  1599. idtxt : 'PECOFFWINCE';
  1600. asmbin : '';
  1601. asmcmd : '';
  1602. supported_target : system_arm_wince;
  1603. flags : [af_outputbinary];
  1604. labelprefix : '.L';
  1605. comment : '';
  1606. );
  1607. initialization
  1608. {$ifdef i386}
  1609. RegisterAssembler(as_i386_coff_info,TCoffAssembler);
  1610. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  1611. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  1612. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  1613. {$endif i386}
  1614. {$ifdef arm}
  1615. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  1616. {$endif arm}
  1617. end.