ogcoff.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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,aasmtai;
  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','.tls',
  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','.debug_info','.debug_line','.debug_abbrev',
  512. '.fpc',
  513. ''
  514. );
  515. var
  516. secname : string;
  517. begin
  518. secname:=secnames[atype];
  519. if use_smartlink_section and
  520. (aname<>'') then
  521. result:=secname+'$'+aname
  522. else
  523. result:=secname;
  524. end;
  525. procedure tcoffobjectdata.writesymbol(p:tasmsymbol);
  526. begin
  527. if currsec=nil then
  528. internalerror(200403071);
  529. { already written ? }
  530. if p.indexnr<>-1 then
  531. exit;
  532. { calculate symbol index }
  533. if (p.currbind<>AB_LOCAL) then
  534. begin
  535. { insert the symbol in the local index, the indexarray
  536. will take care of the numbering }
  537. symbols.insert(p);
  538. end
  539. else
  540. p.indexnr:=-2; { local }
  541. end;
  542. procedure tcoffobjectdata.writereloc(data,len:aint;p:tasmsymbol;relative:TAsmRelocationType);
  543. var
  544. curraddr,
  545. symaddr : longint;
  546. begin
  547. if currsec=nil then
  548. internalerror(200403072);
  549. if assigned(p) then
  550. begin
  551. { current address }
  552. curraddr:=currsec.mempos+currsec.datasize;
  553. { external/common symbols don't have a fixed memory position yet }
  554. if (p.currbind=AB_COMMON) then
  555. begin
  556. { For go32v2 we need to use the size as address }
  557. if not win32 then
  558. symaddr:=p.size
  559. else
  560. symaddr:=0;
  561. end
  562. else
  563. begin
  564. symaddr:=p.address;
  565. if assigned(p.section) then
  566. inc(symaddr,p.section.mempos);
  567. end;
  568. { no symbol relocation need inside a section }
  569. if (p.section=currsec) and
  570. (p.currbind<>AB_COMMON) then
  571. begin
  572. case relative of
  573. RELOC_ABSOLUTE :
  574. begin
  575. currsec.addsectionreloc(curraddr,currsec,RELOC_ABSOLUTE);
  576. inc(data,symaddr);
  577. end;
  578. RELOC_RELATIVE :
  579. begin
  580. inc(data,symaddr-len-currsec.datasize);
  581. end;
  582. RELOC_RVA :
  583. begin
  584. currsec.addsectionreloc(curraddr,currsec,RELOC_RVA);
  585. inc(data,symaddr);
  586. end;
  587. end;
  588. end
  589. else
  590. begin
  591. writesymbol(p);
  592. if (p.section<>nil) and
  593. (p.currbind<>AB_COMMON) and
  594. (relative<>RELOC_RELATIVE) then
  595. currsec.addsectionreloc(curraddr,p.section,relative)
  596. else
  597. currsec.addsymreloc(curraddr,p,relative);
  598. if (not win32) or
  599. ((relative<>RELOC_RELATIVE) and (p.section<>nil)) then
  600. inc(data,symaddr);
  601. if relative=RELOC_RELATIVE then
  602. begin
  603. if win32 then
  604. dec(data,len-4)
  605. else
  606. dec(data,len+currsec.datasize);
  607. end;
  608. end;
  609. end;
  610. currsec.write(data,len);
  611. end;
  612. procedure tcoffobjectdata.writestab(offset:aint;ps:tasmsymbol;nidx,nother,line:longint;p:pchar);
  613. var
  614. stab : coffstab;
  615. curraddr : longint;
  616. begin
  617. { Win32 does not need an offset if a symbol relocation is used }
  618. if win32 and
  619. assigned(ps) and
  620. (ps.currbind<>AB_LOCAL) then
  621. offset:=0;
  622. if assigned(p) and (p[0]<>#0) then
  623. begin
  624. stab.strpos:=StabStrSec.datasize;
  625. StabStrSec.write(p^,strlen(p)+1);
  626. end
  627. else
  628. stab.strpos:=0;
  629. stab.ntype:=nidx;
  630. stab.ndesc:=line;
  631. stab.nother:=nother;
  632. stab.nvalue:=offset;
  633. StabsSec.write(stab,sizeof(stab));
  634. if assigned(ps) then
  635. begin
  636. writesymbol(ps);
  637. { current address }
  638. curraddr:=StabsSec.mempos+StabsSec.datasize;
  639. if DLLSource and RelocSection then
  640. { avoid relocation in the .stab section
  641. because it ends up in the .reloc section instead }
  642. StabsSec.addsymreloc(curraddr-4,ps,RELOC_RVA)
  643. else
  644. StabsSec.addsymreloc(curraddr-4,ps,RELOC_ABSOLUTE);
  645. end;
  646. end;
  647. procedure tcoffobjectdata.section_mempos(p:tnamedindexitem;arg:pointer);
  648. begin
  649. tcoffobjectsection(p).memsize:=tcoffobjectsection(p).datasize;
  650. { memory position is in arg }
  651. if not win32 then
  652. begin
  653. tcoffobjectsection(p).mempos:=plongint(arg)^;
  654. inc(plongint(arg)^,align(tcoffobjectsection(p).memsize,tcoffobjectsection(p).addralign));
  655. end;
  656. end;
  657. procedure tcoffobjectdata.beforealloc;
  658. begin
  659. { create stabs sections if debugging }
  660. if (cs_debuginfo in aktmoduleswitches) then
  661. begin
  662. StabsSec.Alloc(sizeof(coffstab));
  663. StabStrSec.Alloc(length(SplitFileName(current_module.mainsource^))+2);
  664. end;
  665. end;
  666. procedure tcoffobjectdata.beforewrite;
  667. var
  668. s : string;
  669. begin
  670. { create stabs sections if debugging }
  671. if (cs_debuginfo in aktmoduleswitches) then
  672. begin
  673. writestab(0,nil,0,0,0,nil);
  674. { write zero pchar and name together (PM) }
  675. s:=#0+SplitFileName(current_module.mainsource^)+#0;
  676. stabstrsec.write(s[1],length(s));
  677. end;
  678. end;
  679. procedure tcoffobjectdata.afteralloc;
  680. var
  681. mempos : longint;
  682. begin
  683. { if debug then also count header stab }
  684. if (cs_debuginfo in aktmoduleswitches) then
  685. begin
  686. StabsSec.Alloc(sizeof(coffstab));
  687. StabStrSec.Alloc(length(SplitFileName(current_module.mainsource^))+2);
  688. end;
  689. { calc mempos }
  690. mempos:=0;
  691. sects.foreach(@section_mempos,@mempos);
  692. end;
  693. {****************************************************************************
  694. tdjcoffobjectdata
  695. ****************************************************************************}
  696. constructor tdjcoffobjectdata.create(const n:string);
  697. begin
  698. inherited createcoff(n,false,tdjcoffobjectsection);
  699. end;
  700. {****************************************************************************
  701. tpecoffobjectdata
  702. ****************************************************************************}
  703. constructor tpecoffobjectdata.create(const n:string);
  704. begin
  705. inherited createcoff(n,true,tpecoffobjectsection);
  706. end;
  707. {****************************************************************************
  708. tcoffobjectoutput
  709. ****************************************************************************}
  710. constructor tcoffobjectoutput.createdjgpp(smart:boolean);
  711. begin
  712. inherited create(smart);
  713. win32:=false;
  714. end;
  715. constructor tcoffobjectoutput.createwin32(smart:boolean);
  716. begin
  717. inherited create(smart);
  718. win32:=true;
  719. end;
  720. function tcoffobjectoutput.newobjectdata(const n:string):TAsmObjectData;
  721. begin
  722. if win32 then
  723. result:=tpecoffobjectdata.create(n)
  724. else
  725. result:=tdjcoffobjectdata.create(n);
  726. end;
  727. procedure tcoffobjectoutput.write_symbol(const name:string;value,section,typ,aux:longint);
  728. var
  729. sym : coffsymbol;
  730. begin
  731. FillChar(sym,sizeof(sym),0);
  732. { symbolname }
  733. if length(name)>8 then
  734. begin
  735. sym.strpos:=FCoffStrs.size+4;
  736. FCoffStrs.writestr(name);
  737. FCoffStrs.writestr(#0);
  738. end
  739. else
  740. move(name[1],sym.name,length(name));
  741. sym.value:=value;
  742. sym.section:=section;
  743. sym.typ:=typ;
  744. sym.aux:=aux;
  745. FWriter.write(sym,sizeof(sym));
  746. end;
  747. procedure tcoffobjectoutput.section_write_symbol(p:tnamedindexitem;arg:pointer);
  748. var
  749. secrec : coffsectionrec;
  750. begin
  751. write_symbol(tasmsection(p).name,tasmsection(p).mempos,tasmsection(p).secsymidx,3,1);
  752. fillchar(secrec,sizeof(secrec),0);
  753. secrec.len:=tasmsection(p).aligneddatasize;
  754. secrec.nrelocs:=tasmsection(p).relocations.count;
  755. FWriter.write(secrec,sizeof(secrec));
  756. end;
  757. procedure tcoffobjectoutput.section_write_relocs(p:tnamedindexitem;arg:pointer);
  758. var
  759. rel : coffreloc;
  760. r : TAsmRelocation;
  761. begin
  762. r:=TasmRelocation(tasmsection(p).relocations.first);
  763. while assigned(r) do
  764. begin
  765. rel.address:=r.address;
  766. if assigned(r.symbol) then
  767. begin
  768. if (r.symbol.currbind=AB_LOCAL) then
  769. rel.sym:=2*r.symbol.section.secsymidx
  770. else
  771. begin
  772. if r.symbol.indexnr=-1 then
  773. internalerror(4321);
  774. { indexnr starts with 1, coff starts with 0 }
  775. rel.sym:=r.symbol.indexnr+initsym-1;
  776. end;
  777. end
  778. else
  779. begin
  780. if r.section<>nil then
  781. rel.sym:=2*r.section.secsymidx
  782. else
  783. rel.sym:=0;
  784. end;
  785. case r.typ of
  786. RELOC_RELATIVE :
  787. rel.relative:=$14;
  788. RELOC_ABSOLUTE :
  789. rel.relative:=$6;
  790. RELOC_RVA :
  791. rel.relative:=$7;
  792. end;
  793. FWriter.write(rel,sizeof(rel));
  794. r:=TAsmRelocation(r.next);
  795. end;
  796. end;
  797. procedure tcoffobjectoutput.write_symbols(data:TAsmObjectData);
  798. var
  799. filename : string[18];
  800. sectionval,
  801. globalval,
  802. value : longint;
  803. p : tasmsymbol;
  804. begin
  805. with tcoffobjectdata(data) do
  806. begin
  807. { The `.file' record, and the file name auxiliary record }
  808. write_symbol('.file', 0, -2, $67, 1);
  809. fillchar(filename,sizeof(filename),0);
  810. filename:=SplitFileName(current_module.mainsource^);
  811. FWriter.write(filename[1],sizeof(filename)-1);
  812. { The section records, with their auxiliaries, also store the
  813. symbol index }
  814. Sects.foreach(@section_write_symbol,nil);
  815. { The symbols used }
  816. p:=Tasmsymbol(symbols.First);
  817. while assigned(p) do
  818. begin
  819. if assigned(p.section) and
  820. (p.currbind<>AB_COMMON) then
  821. sectionval:=p.section.secsymidx
  822. else
  823. sectionval:=0;
  824. if p.currbind=AB_LOCAL then
  825. globalval:=3
  826. else
  827. globalval:=2;
  828. { if local of global then set the section value to the address
  829. of the symbol }
  830. if p.currbind in [AB_LOCAL,AB_GLOBAL] then
  831. value:=p.address+p.section.mempos
  832. else
  833. value:=p.size;
  834. { symbolname }
  835. write_symbol(p.name,value,sectionval,globalval,0);
  836. p:=tasmsymbol(p.indexnext);
  837. end;
  838. end;
  839. end;
  840. procedure tcoffobjectoutput.section_set_secsymidx(p:tnamedindexitem;arg:pointer);
  841. begin
  842. inc(plongint(arg)^);
  843. tasmsection(p).secsymidx:=plongint(arg)^;
  844. end;
  845. procedure tcoffobjectoutput.section_set_datapos(p:tnamedindexitem;arg:pointer);
  846. begin
  847. tasmsection(p).datapos:=plongint(arg)^;
  848. if not(aso_alloconly in tasmsection(p).secoptions) then
  849. inc(plongint(arg)^,tasmsection(p).aligneddatasize);
  850. end;
  851. procedure tcoffobjectoutput.section_set_reloc_datapos(p:tnamedindexitem;arg:pointer);
  852. begin
  853. TCoffObjectSection(p).coffrelocpos:=plongint(arg)^;
  854. inc(plongint(arg)^,sizeof(coffreloc)*tasmsection(p).relocations.count);
  855. end;
  856. procedure tcoffobjectoutput.section_write_header(p:tnamedindexitem;arg:pointer);
  857. var
  858. sechdr : coffsechdr;
  859. s : string;
  860. strpos : longint;
  861. begin
  862. fillchar(sechdr,sizeof(sechdr),0);
  863. s:=tasmsection(p).name;
  864. if length(s)>8 then
  865. begin
  866. strpos:=FCoffStrs.size+4;
  867. FCoffStrs.writestr(s);
  868. FCoffStrs.writestr(#0);
  869. s:='/'+ToStr(strpos);
  870. end;
  871. move(s[1],sechdr.name,length(s));
  872. if not win32 then
  873. begin
  874. sechdr.rvaofs:=tasmsection(p).mempos;
  875. sechdr.vsize:=tasmsection(p).mempos;
  876. end
  877. else
  878. begin
  879. if tasmsection(p).sectype=sec_bss then
  880. sechdr.vsize:=tasmsection(p).aligneddatasize;
  881. end;
  882. sechdr.datasize:=tasmsection(p).aligneddatasize;
  883. if (tasmsection(p).datasize>0) and
  884. not(aso_alloconly in tasmsection(p).secoptions) then
  885. sechdr.datapos:=tasmsection(p).datapos;
  886. sechdr.nrelocs:=tasmsection(p).relocations.count;
  887. sechdr.relocpos:=TCoffObjectSection(p).coffrelocpos;
  888. sechdr.flags:=TCoffObjectSection(p).flags;
  889. FWriter.write(sechdr,sizeof(sechdr));
  890. end;
  891. procedure tcoffobjectoutput.section_write_data(p:tnamedindexitem;arg:pointer);
  892. var
  893. hp : pdynamicblock;
  894. begin
  895. if (aso_alloconly in tasmsection(p).secoptions) then
  896. exit;
  897. if tasmsection(p).data=nil then
  898. internalerror(200403073);
  899. tasmsection(p).alignsection;
  900. hp:=tasmsection(p).data.firstblock;
  901. while assigned(hp) do
  902. begin
  903. FWriter.write(hp^.data,hp^.used);
  904. hp:=hp^.next;
  905. end;
  906. end;
  907. function tcoffobjectoutput.writedata(data:TAsmObjectData):boolean;
  908. var
  909. orgdatapos,
  910. datapos,
  911. nsects,
  912. sympos,i : longint;
  913. hstab : coffstab;
  914. gotreloc : boolean;
  915. header : coffheader;
  916. empty : array[0..15] of byte;
  917. hp : pdynamicblock;
  918. s : string;
  919. begin
  920. result:=false;
  921. FCoffStrs:=TDynamicArray.Create(strsresize);
  922. with tcoffobjectdata(data) do
  923. begin
  924. { calc amount of sections we have }
  925. fillchar(empty,sizeof(empty),0);
  926. nsects:=0;
  927. Sects.foreach(@section_set_secsymidx,@nsects);
  928. initsym:=2+nsects*2; { 2 for the file }
  929. { For the stab section we need an HdrSym which can now be
  930. calculated more easily }
  931. if StabsSec<>nil then
  932. begin
  933. { header stab }
  934. s:=#0+SplitFileName(current_module.mainsource^)+#0;
  935. stabstrsec.write(s[1],length(s));
  936. hstab.strpos:=1;
  937. hstab.ntype:=0;
  938. hstab.nother:=0;
  939. hstab.ndesc:=(StabsSec.datasize div sizeof(coffstab))-1{+1 according to gas output PM};
  940. hstab.nvalue:=StabStrSec.datasize;
  941. StabsSec.data.seek(0);
  942. StabsSec.data.write(hstab,sizeof(hstab));
  943. end;
  944. { Calculate the filepositions }
  945. datapos:=sizeof(coffheader)+sizeof(coffsechdr)*nsects;
  946. { sections first }
  947. Sects.foreach(@section_set_datapos,@datapos);
  948. { relocs }
  949. orgdatapos:=datapos;
  950. Sects.foreach(@section_set_reloc_datapos,@datapos);
  951. gotreloc:=(orgdatapos<>datapos);
  952. { symbols }
  953. sympos:=datapos;
  954. { COFF header }
  955. fillchar(header,sizeof(coffheader),0);
  956. {$ifdef i386}
  957. header.mach:=$14c;
  958. {$endif i386}
  959. {$ifdef arm}
  960. header.mach:=$1c0;
  961. {$endif arm}
  962. header.nsects:=nsects;
  963. header.sympos:=sympos;
  964. header.syms:=symbols.count+initsym;
  965. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_NOLINES;
  966. if not gotreloc then
  967. header.flag:=header.flag or COFF_FLAG_NORELOCS;
  968. FWriter.write(header,sizeof(header));
  969. { Section headers }
  970. Sects.foreach(@section_write_header,nil);
  971. { Sections }
  972. Sects.foreach(@section_write_data,nil);
  973. { Relocs }
  974. Sects.foreach(@section_write_relocs,nil);
  975. { Symbols }
  976. write_symbols(data);
  977. { Strings }
  978. i:=FCoffStrs.size+4;
  979. FWriter.write(i,4);
  980. hp:=FCoffStrs.firstblock;
  981. while assigned(hp) do
  982. begin
  983. FWriter.write(hp^.data,hp^.used);
  984. hp:=hp^.next;
  985. end;
  986. end;
  987. FCoffStrs.Free;
  988. end;
  989. {****************************************************************************
  990. tcoffexeoutput
  991. ****************************************************************************}
  992. constructor tcoffexeoutput.createdjgpp;
  993. begin
  994. inherited create;
  995. win32:=false;
  996. end;
  997. constructor tcoffexeoutput.createwin32;
  998. begin
  999. inherited create;
  1000. win32:=true;
  1001. end;
  1002. function tcoffexeoutput.newobjectinput:tobjectinput;
  1003. begin
  1004. if win32 then
  1005. result:=tcoffobjectinput.createwin32
  1006. else
  1007. result:=tcoffobjectinput.createdjgpp;
  1008. end;
  1009. procedure tcoffexeoutput.write_symbol(const name:string;value,section,typ,aux:longint);
  1010. var
  1011. sym : coffsymbol;
  1012. begin
  1013. FillChar(sym,sizeof(sym),0);
  1014. if length(name)>8 then
  1015. begin
  1016. sym.strpos:=FCoffStrs.size+4;
  1017. FCoffStrs.writestr(name);
  1018. FCoffStrs.writestr(#0);
  1019. end
  1020. else
  1021. move(name[1],sym.name,length(name));
  1022. sym.value:=value;
  1023. sym.section:=section;
  1024. sym.typ:=typ;
  1025. sym.aux:=aux;
  1026. FWriter.write(sym,sizeof(sym));
  1027. end;
  1028. procedure tcoffexeoutput.write_symbols;
  1029. var
  1030. value,
  1031. sectionval,
  1032. globalval : byte;
  1033. objdata : TAsmObjectData;
  1034. p : tasmsymbol;
  1035. begin
  1036. {$ifdef internallinker}
  1037. objdata:=TAsmObjectData(objdatalist.first);
  1038. while assigned(objdata) do
  1039. begin
  1040. with tcoffobjectdata(objdata) do
  1041. begin
  1042. { The symbols used }
  1043. p:=Tasmsymbol(symbols.First);
  1044. while assigned(p) do
  1045. begin
  1046. if p.section=sec_common then
  1047. sectionval:=sections[sec_bss].secsymidx
  1048. else
  1049. sectionval:=sections[p.section.name].secsymidx;
  1050. if p.currbind=AB_LOCAL then
  1051. globalval:=3
  1052. else
  1053. globalval:=2;
  1054. { if local of global then set the section value to the address
  1055. of the symbol }
  1056. if p.currbind in [AB_LOCAL,AB_GLOBAL] then
  1057. value:=p.address
  1058. else
  1059. value:=p.size;
  1060. { symbolname }
  1061. s:=p.name;
  1062. if length(s)>8 then
  1063. begin
  1064. nameidx:=FCoffStrs.size+4;
  1065. FCoffStrs.writestr(s);
  1066. FCoffStrs.writestr(#0);
  1067. end
  1068. else
  1069. begin
  1070. nameidx:=-1;
  1071. namestr:=s;
  1072. end;
  1073. write_symbol(namestr,nameidx,value,sectionval,globalval,0);
  1074. p:=tasmsymbol(p.indexnext);
  1075. end;
  1076. end;
  1077. objdata:=TAsmObjectData(objdata.next);
  1078. end;
  1079. {$endif internallinker}
  1080. end;
  1081. procedure tcoffexeoutput.CalculateMemoryMap;
  1082. var
  1083. objdata : TAsmObjectData;
  1084. secsymidx,
  1085. mempos,
  1086. datapos : longint;
  1087. begin
  1088. {$ifdef internallinker}
  1089. { retrieve amount of sections }
  1090. nsects:=0;
  1091. secsymidx:=0;
  1092. for sec:=low(TSection) to high(TSection) do
  1093. begin
  1094. if sections[sec].available then
  1095. begin
  1096. inc(nsects);
  1097. inc(secsymidx);
  1098. sections[sec].secsymidx:=secsymidx;
  1099. end;
  1100. end;
  1101. { calculate start positions after the headers }
  1102. datapos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
  1103. mempos:=sizeof(coffheader)+sizeof(coffoptheader)+sizeof(coffsechdr)*nsects;
  1104. if not win32 then
  1105. inc(mempos,sizeof(go32v2stub)+$1000);
  1106. { add sections }
  1107. MapObjectdata(datapos,mempos);
  1108. { end symbol }
  1109. AddGlobalSym('_etext',sections[sec_code].mempos+sections[sec_code].memsize);
  1110. AddGlobalSym('_edata',sections[sec_data].mempos+sections[sec_data].memsize);
  1111. AddGlobalSym('end',mempos);
  1112. { symbols }
  1113. nsyms:=0;
  1114. sympos:=0;
  1115. if not(cs_link_strip in aktglobalswitches) then
  1116. begin
  1117. sympos:=datapos;
  1118. objdata:=TAsmObjectData(objdatalist.first);
  1119. while assigned(objdata) do
  1120. begin
  1121. inc(nsyms,objdata.symbols.count);
  1122. objdata:=TAsmObjectData(objdata.next);
  1123. end;
  1124. end;
  1125. {$endif internallinker}
  1126. end;
  1127. function tcoffexeoutput.writedata:boolean;
  1128. var
  1129. i : longint;
  1130. header : coffheader;
  1131. optheader : coffoptheader;
  1132. sechdr : coffsechdr;
  1133. hp : pdynamicblock;
  1134. objdata : TAsmObjectData;
  1135. hsym : tasmsymbol;
  1136. begin
  1137. result:=false;
  1138. {$ifdef internallinker}
  1139. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1140. FCoffStrs:=TDynamicArray.Create(strsresize);
  1141. { Stub }
  1142. if not win32 then
  1143. FWriter.write(go32v2stub,sizeof(go32v2stub));
  1144. { COFF header }
  1145. fillchar(header,sizeof(header),0);
  1146. header.mach:=$14c;
  1147. header.nsects:=nsects;
  1148. header.sympos:=sympos;
  1149. header.syms:=nsyms;
  1150. header.opthdr:=sizeof(coffoptheader);
  1151. header.flag:=COFF_FLAG_AR32WR or COFF_FLAG_EXE or COFF_FLAG_NORELOCS or COFF_FLAG_NOLINES;
  1152. FWriter.write(header,sizeof(header));
  1153. { Optional COFF Header }
  1154. fillchar(optheader,sizeof(optheader),0);
  1155. optheader.magic:=$10b;
  1156. optheader.tsize:=sections[sec_code].memsize;
  1157. optheader.dsize:=sections[sec_data].memsize;
  1158. optheader.bsize:=sections[sec_bss].memsize;
  1159. hsym:=tasmsymbol(globalsyms.search('start'));
  1160. if not assigned(hsym) then
  1161. begin
  1162. Comment(V_Error,'Entrypoint "start" not defined');
  1163. exit;
  1164. end;
  1165. optheader.entry:=hsym.address;
  1166. optheader.text_start:=sections[sec_code].mempos;
  1167. optheader.data_start:=sections[sec_data].mempos;
  1168. FWriter.write(optheader,sizeof(optheader));
  1169. { Section headers }
  1170. for sec:=low(TSection) to high(TSection) do
  1171. if sections[sec].available then
  1172. begin
  1173. fillchar(sechdr,sizeof(sechdr),0);
  1174. move(target_asm.secnames[sec][1],sechdr.name,length(target_asm.secnames[sec]));
  1175. if not win32 then
  1176. begin
  1177. sechdr.rvaofs:=sections[sec].mempos;
  1178. sechdr.vsize:=sections[sec].mempos;
  1179. end
  1180. else
  1181. begin
  1182. if sec=sec_bss then
  1183. sechdr.vsize:=sections[sec].memsize;
  1184. end;
  1185. if sec=sec_bss then
  1186. sechdr.datasize:=sections[sec].memsize
  1187. else
  1188. begin
  1189. sechdr.datasize:=sections[sec].datasize;
  1190. sechdr.datapos:=sections[sec].datapos;
  1191. end;
  1192. sechdr.nrelocs:=0;
  1193. sechdr.relocpos:=0;
  1194. sechdr.flags:=sections[sec].flags;
  1195. FWriter.write(sechdr,sizeof(sechdr));
  1196. end;
  1197. { Sections }
  1198. for sec:=low(TSection) to high(TSection) do
  1199. if sections[sec].available then
  1200. begin
  1201. { update objectfiles }
  1202. objdata:=TAsmObjectData(objdatalist.first);
  1203. while assigned(objdata) do
  1204. begin
  1205. if assigned(objdata.sects[sec]) and
  1206. assigned(objdata.sects[sec].data) then
  1207. begin
  1208. FWriter.WriteZeros(objdata.sects[sec].dataalignbytes);
  1209. hp:=objdata.sects[sec].data.firstblock;
  1210. while assigned(hp) do
  1211. begin
  1212. FWriter.write(hp^.data,hp^.used);
  1213. hp:=hp^.next;
  1214. end;
  1215. end;
  1216. objdata:=TAsmObjectData(objdata.next);
  1217. end;
  1218. end;
  1219. { Optional symbols }
  1220. if not(cs_link_strip in aktglobalswitches) then
  1221. begin
  1222. { Symbols }
  1223. write_symbols;
  1224. { Strings }
  1225. i:=FCoffStrs.size+4;
  1226. FWriter.write(i,4);
  1227. hp:=FCoffStrs.firstblock;
  1228. while assigned(hp) do
  1229. begin
  1230. FWriter.write(hp^.data,hp^.used);
  1231. hp:=hp^.next;
  1232. end;
  1233. end;
  1234. { Release }
  1235. FCoffStrs.Free;
  1236. FCoffSyms.Free;
  1237. result:=true;
  1238. {$endif internallinker}
  1239. end;
  1240. procedure tcoffexeoutput.GenerateExecutable(const fn:string);
  1241. begin
  1242. AddGlobalSym('_etext',0);
  1243. AddGlobalSym('_edata',0);
  1244. AddGlobalSym('end',0);
  1245. if not CalculateSymbols then
  1246. exit;
  1247. CalculateMemoryMap;
  1248. FixupSymbols;
  1249. FixupRelocations;
  1250. writeexefile(fn);
  1251. end;
  1252. {****************************************************************************
  1253. tcoffobjectinput
  1254. ****************************************************************************}
  1255. constructor tcoffobjectinput.createdjgpp;
  1256. begin
  1257. inherited create;
  1258. win32:=false;
  1259. end;
  1260. constructor tcoffobjectinput.createwin32;
  1261. begin
  1262. inherited create;
  1263. win32:=true;
  1264. end;
  1265. function tcoffobjectinput.newobjectdata(const n:string):TAsmObjectData;
  1266. begin
  1267. if win32 then
  1268. result:=tpecoffobjectdata.create(n)
  1269. else
  1270. result:=tdjcoffobjectdata.create(n);
  1271. end;
  1272. procedure tcoffobjectinput.read_relocs(s:TCoffObjectSection);
  1273. var
  1274. rel : coffreloc;
  1275. rel_type : TAsmRelocationType;
  1276. i : longint;
  1277. p : tasmsymbol;
  1278. begin
  1279. for i:=1 to s.coffrelocs do
  1280. begin
  1281. FReader.read(rel,sizeof(rel));
  1282. case rel.relative of
  1283. $14 : rel_type:=RELOC_RELATIVE;
  1284. $06 : rel_type:=RELOC_ABSOLUTE;
  1285. $07 : rel_type:=RELOC_RVA;
  1286. else
  1287. begin
  1288. Comment(V_Error,'Error reading coff file');
  1289. exit;
  1290. end;
  1291. end;
  1292. p:=FSymTbl^[rel.sym].sym;
  1293. if assigned(p) then
  1294. begin
  1295. s.addsymsizereloc(rel.address-s.mempos,p,FSymTbl^[rel.sym].orgsize,rel_type);
  1296. end
  1297. else
  1298. begin
  1299. Comment(V_Error,'Error reading coff file');
  1300. exit;
  1301. end;
  1302. end;
  1303. end;
  1304. procedure tcoffobjectinput.handle_symbols(data:TAsmObjectData);
  1305. var
  1306. size,
  1307. address,
  1308. i,nsyms,
  1309. symidx : longint;
  1310. sym : coffsymbol;
  1311. strname : string;
  1312. p : tasmsymbol;
  1313. bind : Tasmsymbind;
  1314. auxrec : array[0..17] of byte;
  1315. begin
  1316. {$ifdef internallinker}
  1317. with tcoffobjectdata(data) do
  1318. begin
  1319. nsyms:=FCoffSyms.Size div sizeof(CoffSymbol);
  1320. { Allocate memory for symidx -> tasmsymbol table }
  1321. GetMem(FSymTbl,nsyms*sizeof(ttasmsymbolrec));
  1322. FillChar(FSymTbl^,nsyms*sizeof(ttasmsymbolrec),0);
  1323. { Loop all symbols }
  1324. FCoffSyms.Seek(0);
  1325. symidx:=0;
  1326. while (symidx<nsyms) do
  1327. begin
  1328. FCoffSyms.Read(sym,sizeof(sym));
  1329. if plongint(@sym.name)^<>0 then
  1330. begin
  1331. move(sym.name,strname[1],8);
  1332. strname[9]:=#0;
  1333. end
  1334. else
  1335. begin
  1336. FCoffStrs.Seek(sym.strpos-4);
  1337. FCoffStrs.Read(strname[1],255);
  1338. strname[255]:=#0;
  1339. end;
  1340. strname[0]:=chr(strlen(@strname[1]));
  1341. if strname='' then
  1342. Internalerror(200205172);
  1343. bind:=AB_EXTERNAL;
  1344. sec:=sec_none;
  1345. size:=0;
  1346. address:=0;
  1347. case sym.typ of
  1348. COFF_SYM_GLOBAL :
  1349. begin
  1350. if sym.section=0 then
  1351. begin
  1352. if sym.value=0 then
  1353. bind:=AB_EXTERNAL
  1354. else
  1355. begin
  1356. bind:=AB_COMMON;
  1357. size:=sym.value;
  1358. end;
  1359. end
  1360. else
  1361. begin
  1362. bind:=AB_GLOBAL;
  1363. sec:=Fidx2sec[sym.section];
  1364. if assigned(sects[sec]) then
  1365. begin
  1366. if sym.value>=sects[sec].mempos then
  1367. address:=sym.value-sects[sec].mempos
  1368. else
  1369. internalerror(432432432);
  1370. end
  1371. else
  1372. internalerror(34243214);
  1373. end;
  1374. p:=TAsmSymbol.Create(strname,bind,AT_FUNCTION);
  1375. p.SetAddress(0,sec,address,size);
  1376. p.objectdata:=data;
  1377. symbols.insert(p);
  1378. end;
  1379. COFF_SYM_LABEL,
  1380. COFF_SYM_LOCAL :
  1381. begin
  1382. { do not add constants (section=-1) }
  1383. if sym.section<>-1 then
  1384. begin
  1385. bind:=AB_LOCAL;
  1386. sec:=Fidx2sec[sym.section];
  1387. if assigned(sects[sec]) then
  1388. begin
  1389. if sym.value>=sects[sec].mempos then
  1390. address:=sym.value-sects[sec].mempos
  1391. else
  1392. internalerror(432432432);
  1393. end
  1394. else
  1395. internalerror(34243214);
  1396. p:=TAsmSymbol.Create(strname,bind,AT_FUNCTION);
  1397. p.SetAddress(0,sec,address,size);
  1398. p.objectdata:=data;
  1399. symbols.insert(p);
  1400. end;
  1401. end;
  1402. COFF_SYM_SECTION,
  1403. COFF_SYM_FUNCTION,
  1404. COFF_SYM_FILE :
  1405. ;
  1406. else
  1407. internalerror(4342343);
  1408. end;
  1409. FSymTbl^[symidx].sym:=p;
  1410. FSymTbl^[symidx].orgsize:=size;
  1411. { read aux records }
  1412. for i:=1 to sym.aux do
  1413. begin
  1414. FCoffSyms.Read(auxrec,sizeof(auxrec));
  1415. inc(symidx);
  1416. end;
  1417. inc(symidx);
  1418. end;
  1419. end;
  1420. {$endif internallinker}
  1421. end;
  1422. function tcoffobjectinput.readobjectdata(data:TAsmObjectData):boolean;
  1423. var
  1424. strsize,
  1425. i : longint;
  1426. header : coffheader;
  1427. sechdr : coffsechdr;
  1428. secname : array[0..15] of char;
  1429. begin
  1430. result:=false;
  1431. {$ifdef internallinker}
  1432. FCoffSyms:=TDynamicArray.Create(symbolresize);
  1433. FCoffStrs:=TDynamicArray.Create(strsresize);
  1434. with tcoffobjectdata(data) do
  1435. begin
  1436. FillChar(Fidx2sec,sizeof(Fidx2sec),0);
  1437. { Read COFF header }
  1438. if not reader.read(header,sizeof(coffheader)) then
  1439. begin
  1440. Comment(V_Error,'Error reading coff file');
  1441. exit;
  1442. end;
  1443. {$ifdef i386}
  1444. if header.mach<>$14c then
  1445. {$endif i386}
  1446. {$ifdef arm}
  1447. if header.mach<>$1c0 then
  1448. {$endif arm}
  1449. begin
  1450. Comment(V_Error,'Not a coff file');
  1451. exit;
  1452. end;
  1453. if header.nsects>255 then
  1454. begin
  1455. Comment(V_Error,'To many sections');
  1456. exit;
  1457. end;
  1458. { Section headers }
  1459. for i:=1 to header.nsects do
  1460. begin
  1461. if not reader.read(sechdr,sizeof(sechdr)) then
  1462. begin
  1463. Comment(V_Error,'Error reading coff file');
  1464. exit;
  1465. end;
  1466. move(sechdr.name,secname,8);
  1467. secname[8]:=#0;
  1468. sec:=str2sec(strpas(secname));
  1469. if sec<>sec_none then
  1470. begin
  1471. Fidx2sec[i]:=sec;
  1472. createsection(sec);
  1473. if not win32 then
  1474. sects[sec].mempos:=sechdr.rvaofs;
  1475. TCoffObjectSection(sects[sec]).coffrelocs:=sechdr.nrelocs;
  1476. TCoffObjectSection(sects[sec]).coffrelocpos:=sechdr.relocpos;
  1477. sects[sec].datapos:=sechdr.datapos;
  1478. sects[sec].datasize:=sechdr.datasize;
  1479. sects[sec].memsize:=sechdr.datasize;
  1480. TCoffObjectSection(sects[sec]).orgmempos:=sects[sec].mempos;
  1481. sects[sec].flags:=sechdr.flags;
  1482. end
  1483. else
  1484. Comment(V_Warning,'skipping unsupported section '+strpas(sechdr.name));
  1485. end;
  1486. { Symbols }
  1487. Reader.Seek(header.sympos);
  1488. if not Reader.ReadArray(FCoffSyms,header.syms*sizeof(CoffSymbol)) then
  1489. begin
  1490. Comment(V_Error,'Error reading coff file');
  1491. exit;
  1492. end;
  1493. { Strings }
  1494. if not Reader.Read(strsize,4) then
  1495. begin
  1496. Comment(V_Error,'Error reading coff file');
  1497. exit;
  1498. end;
  1499. if strsize<4 then
  1500. begin
  1501. Comment(V_Error,'Error reading coff file');
  1502. exit;
  1503. end;
  1504. if not Reader.ReadArray(FCoffStrs,Strsize-4) then
  1505. begin
  1506. Comment(V_Error,'Error reading coff file');
  1507. exit;
  1508. end;
  1509. { Insert all symbols }
  1510. handle_symbols(data);
  1511. { Sections }
  1512. for sec:=low(TSection) to high(TSection) do
  1513. if assigned(sects[sec]) and
  1514. (sec<>sec_bss) then
  1515. begin
  1516. Reader.Seek(sects[sec].datapos);
  1517. if not Reader.ReadArray(sects[sec].data,sects[sec].datasize) then
  1518. begin
  1519. Comment(V_Error,'Error reading coff file');
  1520. exit;
  1521. end;
  1522. end;
  1523. { Relocs }
  1524. for sec:=low(TSection) to high(TSection) do
  1525. if assigned(sects[sec]) and
  1526. (TCoffObjectSection(sects[sec]).coffrelocs>0) then
  1527. begin
  1528. Reader.Seek(TCoffObjectSection(sects[sec]).coffrelocpos);
  1529. read_relocs(TCoffObjectSection(sects[sec]));
  1530. end;
  1531. end;
  1532. FCoffStrs.Free;
  1533. FCoffSyms.Free;
  1534. result:=true;
  1535. {$endif internallinker}
  1536. end;
  1537. {****************************************************************************
  1538. TCoffAssembler
  1539. ****************************************************************************}
  1540. constructor TCoffAssembler.Create(smart:boolean);
  1541. begin
  1542. inherited Create(smart);
  1543. objectoutput:=tcoffobjectoutput.createdjgpp(smart);
  1544. end;
  1545. {****************************************************************************
  1546. TPECoffAssembler
  1547. ****************************************************************************}
  1548. constructor TPECoffAssembler.Create(smart:boolean);
  1549. begin
  1550. inherited Create(smart);
  1551. objectoutput:=tcoffobjectoutput.createwin32(smart);
  1552. end;
  1553. {****************************************************************************
  1554. TCoffLinker
  1555. ****************************************************************************}
  1556. constructor TCoffLinker.Create;
  1557. begin
  1558. inherited Create;
  1559. exeoutput:=tcoffexeoutput.createdjgpp;
  1560. end;
  1561. {*****************************************************************************
  1562. Initialize
  1563. *****************************************************************************}
  1564. const
  1565. as_i386_coff_info : tasminfo =
  1566. (
  1567. id : as_i386_coff;
  1568. idtxt : 'COFF';
  1569. asmbin : '';
  1570. asmcmd : '';
  1571. supported_target : system_i386_go32v2;
  1572. flags : [af_outputbinary];
  1573. labelprefix : '.L';
  1574. comment : '';
  1575. );
  1576. as_i386_pecoff_info : tasminfo =
  1577. (
  1578. id : as_i386_pecoff;
  1579. idtxt : 'PECOFF';
  1580. asmbin : '';
  1581. asmcmd : '';
  1582. supported_target : system_i386_win32;
  1583. flags : [af_outputbinary,af_smartlink_sections];
  1584. labelprefix : '.L';
  1585. comment : '';
  1586. );
  1587. as_i386_pecoffwdosx_info : tasminfo =
  1588. (
  1589. id : as_i386_pecoffwdosx;
  1590. idtxt : 'PEWDOSX';
  1591. asmbin : '';
  1592. asmcmd : '';
  1593. supported_target : system_i386_wdosx;
  1594. flags : [af_outputbinary];
  1595. labelprefix : '.L';
  1596. comment : '';
  1597. );
  1598. as_i386_pecoffwince_info : tasminfo =
  1599. (
  1600. id : as_i386_pecoffwince;
  1601. idtxt : 'PECOFFWINCE';
  1602. asmbin : '';
  1603. asmcmd : '';
  1604. supported_target : system_i386_wince;
  1605. flags : [af_outputbinary];
  1606. labelprefix : '.L';
  1607. comment : '';
  1608. );
  1609. as_arm_pecoffwince_info : tasminfo =
  1610. (
  1611. id : as_arm_pecoffwince;
  1612. idtxt : 'PECOFFWINCE';
  1613. asmbin : '';
  1614. asmcmd : '';
  1615. supported_target : system_arm_wince;
  1616. flags : [af_outputbinary];
  1617. labelprefix : '.L';
  1618. comment : '';
  1619. );
  1620. initialization
  1621. {$ifdef i386}
  1622. RegisterAssembler(as_i386_coff_info,TCoffAssembler);
  1623. RegisterAssembler(as_i386_pecoff_info,TPECoffAssembler);
  1624. RegisterAssembler(as_i386_pecoffwdosx_info,TPECoffAssembler);
  1625. RegisterAssembler(as_i386_pecoffwince_info,TPECoffAssembler);
  1626. {$endif i386}
  1627. {$ifdef arm}
  1628. RegisterAssembler(as_arm_pecoffwince_info,TPECoffAssembler);
  1629. {$endif arm}
  1630. end.