pmodules.pas 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. procedure proc_unit;
  21. procedure proc_package;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. SysUtils,
  26. globtype,version,systems,tokens,
  27. cutils,cfileutl,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. wpoinfo,
  31. aasmtai,aasmdata,aasmcpu,aasmbase,
  32. cgbase,cgobj,
  33. nbas,ncgutil,
  34. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  35. cresstr,procinfo,
  36. pexports,
  37. objcgutl,
  38. wpobase,
  39. scanner,pbase,pexpr,psystem,psub,pdecsub,ptype,
  40. cpuinfo;
  41. procedure create_objectfile;
  42. var
  43. DLLScanner : TDLLScanner;
  44. s : string;
  45. KeepShared : TCmdStrList;
  46. begin
  47. { try to create import entries from system dlls }
  48. if (tf_has_dllscanner in target_info.flags) and
  49. (not current_module.linkOtherSharedLibs.Empty) then
  50. begin
  51. { Init DLLScanner }
  52. if assigned(CDLLScanner[target_info.system]) then
  53. DLLScanner:=CDLLScanner[target_info.system].Create
  54. else
  55. internalerror(200104121);
  56. KeepShared:=TCmdStrList.Create;
  57. { Walk all shared libs }
  58. While not current_module.linkOtherSharedLibs.Empty do
  59. begin
  60. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  61. if not DLLScanner.scan(s) then
  62. KeepShared.Concat(s);
  63. end;
  64. DLLscanner.Free;
  65. { Recreate import section }
  66. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  67. begin
  68. if assigned(current_asmdata.asmlists[al_imports]) then
  69. current_asmdata.asmlists[al_imports].clear
  70. else
  71. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  72. importlib.generatelib;
  73. end;
  74. { Readd the not processed files }
  75. while not KeepShared.Empty do
  76. begin
  77. s:=KeepShared.GetFirst;
  78. current_module.linkOtherSharedLibs.add(s,link_always);
  79. end;
  80. KeepShared.Free;
  81. end;
  82. { Start and end module debuginfo, at least required for stabs
  83. to insert n_sourcefile lines }
  84. if (cs_debuginfo in current_settings.moduleswitches) or
  85. (cs_use_lineinfo in current_settings.globalswitches) then
  86. current_debuginfo.insertmoduleinfo;
  87. { create the .s file and assemble it }
  88. GenerateAsm(false);
  89. { Also create a smartlinked version ? }
  90. if create_smartlink_library then
  91. begin
  92. GenerateAsm(true);
  93. if (af_needar in target_asm.flags) then
  94. Linker.MakeStaticLibrary;
  95. end;
  96. { resource files }
  97. CompileResourceFiles;
  98. end;
  99. procedure insertobjectfile;
  100. { Insert the used object file for this unit in the used list for this unit }
  101. begin
  102. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  103. current_module.flags:=current_module.flags or uf_static_linked;
  104. if create_smartlink_library then
  105. begin
  106. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  107. current_module.flags:=current_module.flags or uf_smart_linked;
  108. end;
  109. end;
  110. procedure create_dwarf_frame;
  111. begin
  112. { Dwarf conflicts with smartlinking in separate .a files }
  113. if create_smartlink_library then
  114. exit;
  115. { Call frame information }
  116. { MWE: we write our own info, so dwarf asm support is not really needed }
  117. { if (af_supports_dwarf in target_asm.flags) and }
  118. { CFI is currently broken for Darwin }
  119. if not(target_info.system in systems_darwin) and
  120. (
  121. (tf_needs_dwarf_cfi in target_info.flags) or
  122. (paratargetdbg in [dbg_dwarf2, dbg_dwarf3])
  123. ) then
  124. begin
  125. current_asmdata.asmlists[al_dwarf_frame].Free;
  126. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  127. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  128. end;
  129. end;
  130. procedure InsertThreadvarTablesTable;
  131. var
  132. hp : tused_unit;
  133. ltvTables : TAsmList;
  134. count : longint;
  135. begin
  136. if (tf_section_threadvars in target_info.flags) then
  137. exit;
  138. ltvTables:=TAsmList.Create;
  139. count:=0;
  140. hp:=tused_unit(usedunits.first);
  141. while assigned(hp) do
  142. begin
  143. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  144. begin
  145. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  146. inc(count);
  147. end;
  148. hp:=tused_unit(hp.next);
  149. end;
  150. { Add program threadvars, if any }
  151. If (current_module.flags and uf_threadvars)=uf_threadvars then
  152. begin
  153. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  154. inc(count);
  155. end;
  156. { Insert TableCount at start }
  157. ltvTables.insert(Tai_const.Create_32bit(count));
  158. { insert in data segment }
  159. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  160. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(pint));
  161. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  162. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  163. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  164. ltvTables.free;
  165. end;
  166. procedure AddToThreadvarList(p:TObject;arg:pointer);
  167. var
  168. ltvTable : TAsmList;
  169. begin
  170. ltvTable:=TAsmList(arg);
  171. if (tsym(p).typ=staticvarsym) and
  172. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  173. begin
  174. { address of threadvar }
  175. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  176. { size of threadvar }
  177. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  178. end;
  179. end;
  180. procedure InsertThreadvars;
  181. var
  182. s : string;
  183. ltvTable : TAsmList;
  184. begin
  185. if (tf_section_threadvars in target_info.flags) then
  186. exit;
  187. ltvTable:=TAsmList.create;
  188. if assigned(current_module.globalsymtable) then
  189. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  190. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  191. if ltvTable.first<>nil then
  192. begin
  193. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  194. { end of the list marker }
  195. ltvTable.concat(tai_const.create_sym(nil));
  196. { add to datasegment }
  197. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  198. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  199. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  200. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  201. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  202. current_module.flags:=current_module.flags or uf_threadvars;
  203. end;
  204. ltvTable.Free;
  205. end;
  206. procedure InsertWideInits;
  207. var
  208. s: string;
  209. item: TTCInitItem;
  210. begin
  211. item:=TTCInitItem(current_asmdata.WideInits.First);
  212. if item=nil then
  213. exit;
  214. s:=make_mangledname('WIDEINITS',current_module.localsymtable,'');
  215. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  216. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  217. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  218. repeat
  219. { optimize away unused local/static symbols }
  220. if (item.sym.refs>0) or (item.sym.owner.symtabletype=globalsymtable) then
  221. begin
  222. { address to initialize }
  223. current_asmdata.asmlists[al_globals].concat(Tai_const.createname(item.sym.mangledname, item.offset));
  224. { value with which to initialize }
  225. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(item.datalabel));
  226. end;
  227. item:=TTCInitItem(item.Next);
  228. until item=nil;
  229. { end-of-list marker }
  230. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  231. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  232. current_module.flags:=current_module.flags or uf_wideinits;
  233. end;
  234. procedure InsertWideInitsTablesTable;
  235. var
  236. hp: tused_unit;
  237. lwiTables: TAsmList;
  238. count: longint;
  239. begin
  240. lwiTables:=TAsmList.Create;
  241. count:=0;
  242. hp:=tused_unit(usedunits.first);
  243. while assigned(hp) do
  244. begin
  245. if (hp.u.flags and uf_wideinits)=uf_wideinits then
  246. begin
  247. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',hp.u.globalsymtable,''),0));
  248. inc(count);
  249. end;
  250. hp:=tused_unit(hp.next);
  251. end;
  252. { Add program widestring consts, if any }
  253. if (current_module.flags and uf_wideinits)=uf_wideinits then
  254. begin
  255. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',current_module.localsymtable,''),0));
  256. inc(count);
  257. end;
  258. { Insert TableCount at start }
  259. lwiTables.insert(Tai_const.Create_32bit(count));
  260. { insert in data segment }
  261. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  262. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_WIDEINITTABLES',sizeof(pint));
  263. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_WIDEINITTABLES',AT_DATA,0));
  264. current_asmdata.asmlists[al_globals].concatlist(lwiTables);
  265. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_WIDEINITTABLES'));
  266. lwiTables.free;
  267. end;
  268. Function CheckResourcesUsed : boolean;
  269. var
  270. hp : tused_unit;
  271. found : Boolean;
  272. begin
  273. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  274. if not CheckResourcesUsed then exit;
  275. hp:=tused_unit(usedunits.first);
  276. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  277. If not found then
  278. While Assigned(hp) and not found do
  279. begin
  280. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  281. hp:=tused_unit(hp.next);
  282. end;
  283. CheckResourcesUsed:=found;
  284. end;
  285. Procedure InsertResourceInfo(ResourcesUsed : boolean);
  286. var
  287. ResourceInfo : TAsmList;
  288. begin
  289. if (target_res.id in [res_elf,res_macho]) then
  290. begin
  291. ResourceInfo:=TAsmList.Create;
  292. maybe_new_object_file(ResourceInfo);
  293. new_section(ResourceInfo,sec_data,'FPC_RESLOCATION',sizeof(aint));
  294. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  295. if ResourcesUsed then
  296. { Valid pointer to resource information }
  297. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0))
  298. else
  299. { Nil pointer to resource information }
  300. {$IFDEF CPU32}
  301. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  302. {$ELSE}
  303. ResourceInfo.Concat(Tai_const.Create_64bit(0));
  304. {$ENDIF}
  305. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  306. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  307. ResourceInfo.free;
  308. end;
  309. end;
  310. Procedure InsertResourceTablesTable;
  311. var
  312. hp : tmodule;
  313. ResourceStringTables : tasmlist;
  314. count : longint;
  315. begin
  316. ResourceStringTables:=tasmlist.Create;
  317. count:=0;
  318. hp:=tmodule(loaded_units.first);
  319. while assigned(hp) do
  320. begin
  321. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  322. begin
  323. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  324. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  325. inc(count);
  326. end;
  327. hp:=tmodule(hp.next);
  328. end;
  329. { Insert TableCount at start }
  330. ResourceStringTables.insert(Tai_const.Create_pint(count));
  331. { Add to data segment }
  332. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  333. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(pint));
  334. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  335. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  336. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  337. ResourceStringTables.free;
  338. end;
  339. procedure AddToStructInits(p:TObject;arg:pointer);
  340. var
  341. StructList: TFPList absolute arg;
  342. begin
  343. if (tdef(p).typ in [objectdef,recorddef]) and
  344. ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  345. StructList.Add(p);
  346. end;
  347. procedure InsertInitFinalTable;
  348. var
  349. hp : tused_unit;
  350. unitinits : TAsmList;
  351. count : longint;
  352. procedure write_struct_inits(u: tmodule);
  353. var
  354. i: integer;
  355. structlist: TFPList;
  356. pd: tprocdef;
  357. begin
  358. structlist := TFPList.Create;
  359. if assigned(u.globalsymtable) then
  360. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  361. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  362. { write structures }
  363. for i := 0 to structlist.Count - 1 do
  364. begin
  365. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
  366. if assigned(pd) then
  367. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  368. else
  369. unitinits.concat(Tai_const.Create_pint(0));
  370. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
  371. if assigned(pd) then
  372. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  373. else
  374. unitinits.concat(Tai_const.Create_pint(0));
  375. inc(count);
  376. end;
  377. structlist.free;
  378. end;
  379. begin
  380. unitinits:=TAsmList.Create;
  381. count:=0;
  382. hp:=tused_unit(usedunits.first);
  383. while assigned(hp) do
  384. begin
  385. { insert class constructors/destructors of the unit }
  386. if (hp.u.flags and uf_classinits) <> 0 then
  387. write_struct_inits(hp.u);
  388. { call the unit init code and make it external }
  389. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  390. begin
  391. if (hp.u.flags and uf_init)<>0 then
  392. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  393. else
  394. unitinits.concat(Tai_const.Create_sym(nil));
  395. if (hp.u.flags and uf_finalize)<>0 then
  396. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  397. else
  398. unitinits.concat(Tai_const.Create_sym(nil));
  399. inc(count);
  400. end;
  401. hp:=tused_unit(hp.next);
  402. end;
  403. { insert class constructors/destructor of the program }
  404. if (current_module.flags and uf_classinits) <> 0 then
  405. write_struct_inits(current_module);
  406. { Insert initialization/finalization of the program }
  407. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  408. begin
  409. if (current_module.flags and uf_init)<>0 then
  410. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  411. else
  412. unitinits.concat(Tai_const.Create_sym(nil));
  413. if (current_module.flags and uf_finalize)<>0 then
  414. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  415. else
  416. unitinits.concat(Tai_const.Create_sym(nil));
  417. inc(count);
  418. end;
  419. { Insert TableCount,InitCount at start }
  420. unitinits.insert(Tai_const.Create_32bit(0));
  421. unitinits.insert(Tai_const.Create_32bit(count));
  422. { Add to data segment }
  423. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  424. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(pint));
  425. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  426. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  427. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  428. unitinits.free;
  429. end;
  430. procedure InsertMemorySizes;
  431. {$IFDEF POWERPC}
  432. var
  433. stkcookie: string;
  434. {$ENDIF POWERPC}
  435. begin
  436. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  437. { Insert Ident of the compiler in the .fpc.version section }
  438. new_section(current_asmdata.asmlists[al_globals],sec_fpc,'version',const_align(32));
  439. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  440. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  441. if not(tf_no_generic_stackcheck in target_info.flags) then
  442. begin
  443. { stacksize can be specified and is now simulated }
  444. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(pint));
  445. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(pint)));
  446. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(stacksize));
  447. end;
  448. {$IFDEF POWERPC}
  449. { AmigaOS4 "stack cookie" support }
  450. if ( target_info.system = system_powerpc_amiga ) then
  451. begin
  452. { this symbol is needed to ignite powerpc amigaos' }
  453. { stack allocation magic for us with the given stack size. }
  454. { note: won't work for m68k amigaos or morphos. (KB) }
  455. str(stacksize,stkcookie);
  456. stkcookie:='$STACK: '+stkcookie+#0;
  457. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  458. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  459. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  460. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  461. end;
  462. {$ENDIF POWERPC}
  463. { Initial heapsize }
  464. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  465. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(pint));
  466. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(pint)));
  467. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(heapsize));
  468. { Initial heapsize }
  469. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  470. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  471. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  472. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  473. end;
  474. procedure AddUnit(const s:string);
  475. var
  476. hp : tppumodule;
  477. unitsym : tunitsym;
  478. begin
  479. { load unit }
  480. hp:=registerunit(current_module,s,'');
  481. hp.loadppu;
  482. hp.adddependency(current_module);
  483. { add to symtable stack }
  484. symtablestack.push(hp.globalsymtable);
  485. if (m_mac in current_settings.modeswitches) and
  486. assigned(hp.globalmacrosymtable) then
  487. macrosymtablestack.push(hp.globalmacrosymtable);
  488. { insert unitsym }
  489. unitsym:=tunitsym.create(s,hp);
  490. inc(unitsym.refs);
  491. current_module.localsymtable.insert(unitsym);
  492. { add to used units }
  493. current_module.addusedunit(hp,false,unitsym);
  494. end;
  495. procedure maybeloadvariantsunit;
  496. var
  497. hp : tmodule;
  498. begin
  499. { Do we need the variants unit? Skip this
  500. for VarUtils unit for bootstrapping }
  501. if (current_module.flags and uf_uses_variants=0) or
  502. (current_module.modulename^='VARUTILS') then
  503. exit;
  504. { Variants unit already loaded? }
  505. hp:=tmodule(loaded_units.first);
  506. while assigned(hp) do
  507. begin
  508. if hp.modulename^='VARIANTS' then
  509. exit;
  510. hp:=tmodule(hp.next);
  511. end;
  512. { Variants unit is not loaded yet, load it now }
  513. Message(parser_w_implicit_uses_of_variants_unit);
  514. AddUnit('variants');
  515. end;
  516. function MaybeRemoveResUnit : boolean;
  517. var
  518. resources_used : boolean;
  519. hp : tmodule;
  520. uu : tused_unit;
  521. unitname : shortstring;
  522. begin
  523. { We simply remove the unit from:
  524. - usedunit list, so that things like init/finalization table won't
  525. contain references to this unit
  526. - loaded_units list, so that the unit object file doesn't get linked
  527. with the executable. }
  528. { Note: on windows we always need resources! }
  529. resources_used:=(target_info.system in systems_all_windows)
  530. or CheckResourcesUsed;
  531. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  532. begin
  533. { resources aren't used, so we don't need this unit }
  534. if target_res.id=res_ext then
  535. unitname:='FPEXTRES'
  536. else
  537. unitname:='FPINTRES';
  538. Message1(unit_u_unload_resunit,unitname);
  539. { find the module }
  540. hp:=tmodule(loaded_units.first);
  541. while assigned(hp) do
  542. begin
  543. if hp.is_unit and (hp.modulename^=unitname) then break;
  544. hp:=tmodule(hp.next);
  545. end;
  546. if not assigned(hp) then
  547. internalerror(200801071);
  548. { find its tused_unit in the global list }
  549. uu:=tused_unit(usedunits.first);
  550. while assigned(uu) do
  551. begin
  552. if uu.u=hp then break;
  553. uu:=tused_unit(uu.next);
  554. end;
  555. if not assigned(uu) then
  556. internalerror(200801072);
  557. { remove the tused_unit }
  558. usedunits.Remove(uu);
  559. uu.Free;
  560. { remove the module }
  561. loaded_units.Remove(hp);
  562. unloaded_units.Concat(hp);
  563. end;
  564. MaybeRemoveResUnit:=resources_used;
  565. end;
  566. procedure loaddefaultunits;
  567. begin
  568. { we are going to rebuild the symtablestack, clear it first }
  569. symtablestack.clear;
  570. macrosymtablestack.clear;
  571. { macro symtable }
  572. macrosymtablestack.push(initialmacrosymtable);
  573. { are we compiling the system unit? }
  574. if (cs_compilesystem in current_settings.moduleswitches) then
  575. begin
  576. systemunit:=tglobalsymtable(current_module.localsymtable);
  577. { create system defines }
  578. create_intern_symbols;
  579. create_intern_types;
  580. { Set the owner of errorsym and errortype to symtable to
  581. prevent crashes when accessing .owner }
  582. generrorsym.owner:=systemunit;
  583. generrordef.owner:=systemunit;
  584. exit;
  585. end;
  586. { insert the system unit, it is allways the first. Load also the
  587. internal types from the system unit }
  588. AddUnit('system');
  589. systemunit:=tglobalsymtable(symtablestack.top);
  590. load_intern_types;
  591. { Set the owner of errorsym and errortype to symtable to
  592. prevent crashes when accessing .owner }
  593. generrorsym.owner:=systemunit;
  594. generrordef.owner:=systemunit;
  595. { Units only required for main module }
  596. if not(current_module.is_unit) then
  597. begin
  598. { Heaptrc unit, load heaptrace before any other units especially objpas }
  599. if (cs_use_heaptrc in current_settings.globalswitches) then
  600. AddUnit('heaptrc');
  601. { Lineinfo unit }
  602. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  603. if (paratargetdbg = dbg_stabs) then
  604. AddUnit('lineinfo')
  605. else
  606. AddUnit('lnfodwrf');
  607. end;
  608. { Valgrind requires c memory manager }
  609. if (cs_gdb_valgrind in current_settings.globalswitches) then
  610. AddUnit('cmem');
  611. {$ifdef cpufpemu}
  612. { Floating point emulation unit?
  613. softfpu must be in the system unit anyways (FK)
  614. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  615. AddUnit('softfpu');
  616. }
  617. {$endif cpufpemu}
  618. { Which kind of resource support?
  619. Note: if resources aren't used this unit will be removed later,
  620. otherwise we need it here since it must be loaded quite early }
  621. if (tf_has_winlike_resources in target_info.flags) then
  622. if target_res.id=res_ext then
  623. AddUnit('fpextres')
  624. else
  625. AddUnit('fpintres');
  626. end;
  627. { Objpas unit? }
  628. if m_objpas in current_settings.modeswitches then
  629. AddUnit('objpas');
  630. { Macpas unit? }
  631. if m_mac in current_settings.modeswitches then
  632. AddUnit('macpas');
  633. if m_iso in current_settings.modeswitches then
  634. AddUnit('iso7185');
  635. { Objective-C support unit? }
  636. if (m_objectivec1 in current_settings.modeswitches) then
  637. begin
  638. { interface to Objective-C run time }
  639. AddUnit('objc');
  640. loadobjctypes;
  641. { NSObject }
  642. if not(current_module.is_unit) or
  643. (current_module.modulename^<>'OBJCBASE') then
  644. AddUnit('objcbase');
  645. end;
  646. { Profile unit? Needed for go32v2 only }
  647. if (cs_profile in current_settings.moduleswitches) and
  648. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  649. AddUnit('profile');
  650. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  651. begin
  652. AddUnit('fpcylix');
  653. AddUnit('dynlibs');
  654. end;
  655. { CPU targets with microcontroller support can add a controller specific unit }
  656. {$if defined(ARM) or defined(AVR)}
  657. if (target_info.system in systems_embedded) and (current_settings.controllertype<>ct_none) then
  658. AddUnit(controllerunitstr[current_settings.controllertype]);
  659. {$endif ARM}
  660. end;
  661. procedure loadautounits;
  662. var
  663. hs,s : string;
  664. begin
  665. hs:=autoloadunits;
  666. repeat
  667. s:=GetToken(hs,',');
  668. if s='' then
  669. break;
  670. AddUnit(s);
  671. until false;
  672. end;
  673. procedure loadunits;
  674. var
  675. s,sorg : TIDString;
  676. fn : string;
  677. pu : tused_unit;
  678. hp2 : tmodule;
  679. unitsym : tunitsym;
  680. begin
  681. consume(_USES);
  682. repeat
  683. s:=pattern;
  684. sorg:=orgpattern;
  685. consume(_ID);
  686. { support "<unit> in '<file>'" construct, but not for tp7 }
  687. fn:='';
  688. if not(m_tp7 in current_settings.modeswitches) and
  689. try_to_consume(_OP_IN) then
  690. fn:=FixFileName(get_stringconst);
  691. { Give a warning if lineinfo is loaded }
  692. if s='LINEINFO' then begin
  693. Message(parser_w_no_lineinfo_use_switch);
  694. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  695. s := 'LNFODWRF';
  696. sorg := s;
  697. end;
  698. { Give a warning if objpas is loaded }
  699. if s='OBJPAS' then
  700. Message(parser_w_no_objpas_use_mode);
  701. { Using the unit itself is not possible }
  702. if (s<>current_module.modulename^) then
  703. begin
  704. { check if the unit is already used }
  705. hp2:=nil;
  706. pu:=tused_unit(current_module.used_units.first);
  707. while assigned(pu) do
  708. begin
  709. if (pu.u.modulename^=s) then
  710. begin
  711. hp2:=pu.u;
  712. break;
  713. end;
  714. pu:=tused_unit(pu.next);
  715. end;
  716. if not assigned(hp2) then
  717. hp2:=registerunit(current_module,sorg,fn)
  718. else
  719. Message1(sym_e_duplicate_id,s);
  720. { Create unitsym, we need to use the name as specified, we
  721. can not use the modulename because that can be different
  722. when -Un is used }
  723. unitsym:=tunitsym.create(sorg,nil);
  724. current_module.localsymtable.insert(unitsym);
  725. { the current module uses the unit hp2 }
  726. current_module.addusedunit(hp2,true,unitsym);
  727. end
  728. else
  729. Message1(sym_e_duplicate_id,s);
  730. if token=_COMMA then
  731. begin
  732. pattern:='';
  733. consume(_COMMA);
  734. end
  735. else
  736. break;
  737. until false;
  738. { Load the units }
  739. pu:=tused_unit(current_module.used_units.first);
  740. while assigned(pu) do
  741. begin
  742. { Only load the units that are in the current
  743. (interface/implementation) uses clause }
  744. if pu.in_uses and
  745. (pu.in_interface=current_module.in_interface) then
  746. begin
  747. tppumodule(pu.u).loadppu;
  748. { is our module compiled? then we can stop }
  749. if current_module.state=ms_compiled then
  750. exit;
  751. { add this unit to the dependencies }
  752. pu.u.adddependency(current_module);
  753. { save crc values }
  754. pu.checksum:=pu.u.crc;
  755. pu.interface_checksum:=pu.u.interface_crc;
  756. pu.indirect_checksum:=pu.u.indirect_crc;
  757. { connect unitsym to the module }
  758. pu.unitsym.module:=pu.u;
  759. { add to symtable stack }
  760. symtablestack.push(pu.u.globalsymtable);
  761. if (m_mac in current_settings.modeswitches) and
  762. assigned(pu.u.globalmacrosymtable) then
  763. macrosymtablestack.push(pu.u.globalmacrosymtable);
  764. { check hints }
  765. pu.u.check_hints;
  766. end;
  767. pu:=tused_unit(pu.next);
  768. end;
  769. consume(_SEMICOLON);
  770. end;
  771. procedure reset_all_defs;
  772. begin
  773. if assigned(current_module.wpoinfo) then
  774. current_module.wpoinfo.resetdefs;
  775. end;
  776. procedure free_localsymtables(st:TSymtable);
  777. var
  778. i : longint;
  779. def : tstoreddef;
  780. pd : tprocdef;
  781. begin
  782. for i:=0 to st.DefList.Count-1 do
  783. begin
  784. def:=tstoreddef(st.DefList[i]);
  785. if def.typ=procdef then
  786. begin
  787. pd:=tprocdef(def);
  788. if assigned(pd.localst) and
  789. (pd.localst.symtabletype<>staticsymtable) and
  790. not(po_inline in pd.procoptions) then
  791. begin
  792. free_localsymtables(pd.localst);
  793. pd.localst.free;
  794. pd.localst:=nil;
  795. end;
  796. end;
  797. end;
  798. end;
  799. procedure parse_implementation_uses;
  800. begin
  801. if token=_USES then
  802. loadunits;
  803. end;
  804. procedure setupglobalswitches;
  805. begin
  806. if (cs_create_pic in current_settings.moduleswitches) then
  807. begin
  808. def_system_macro('FPC_PIC');
  809. def_system_macro('PIC');
  810. end;
  811. end;
  812. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  813. var
  814. ps : tprocsym;
  815. pd : tprocdef;
  816. begin
  817. { there should be no current_procinfo available }
  818. if assigned(current_procinfo) then
  819. internalerror(200304275);
  820. {Generate a procsym for main}
  821. ps:=tprocsym.create('$'+name);
  822. { main are allways used }
  823. inc(ps.refs);
  824. st.insert(ps);
  825. pd:=tprocdef.create(main_program_level);
  826. include(pd.procoptions,po_global);
  827. pd.procsym:=ps;
  828. ps.ProcdefList.Add(pd);
  829. { set procdef options }
  830. pd.proctypeoption:=potype;
  831. pd.proccalloption:=pocall_default;
  832. include(pd.procoptions,po_hascallingconvention);
  833. pd.forwarddef:=false;
  834. pd.setmangledname(target_info.cprefix+name);
  835. pd.aliasnames.insert(pd.mangledname);
  836. handle_calling_convention(pd);
  837. { We don't need is a local symtable. Change it into the static
  838. symtable }
  839. pd.localst.free;
  840. pd.localst:=st;
  841. { set procinfo and current_procinfo.procdef }
  842. result:=tcgprocinfo(cprocinfo.create(nil));
  843. result.procdef:=pd;
  844. { main proc does always a call e.g. to init system unit }
  845. include(result.flags,pi_do_call);
  846. end;
  847. procedure release_main_proc(pi:tcgprocinfo);
  848. begin
  849. { remove localst as it was replaced by staticsymtable }
  850. pi.procdef.localst:=nil;
  851. { remove procinfo }
  852. current_module.procinfo:=nil;
  853. pi.free;
  854. pi:=nil;
  855. end;
  856. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  857. procedure maybe_load_got;
  858. {$ifdef i386}
  859. var
  860. gotvarsym : tstaticvarsym;
  861. {$endif i386}
  862. begin
  863. {$ifdef i386}
  864. if (cs_create_pic in current_settings.moduleswitches) and
  865. (tf_pic_uses_got in target_info.flags) then
  866. begin
  867. { insert symbol for got access in assembler code}
  868. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  869. vs_value,voidpointertype,[vo_is_external]);
  870. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  871. current_module.localsymtable.insert(gotvarsym);
  872. { avoid unnecessary warnings }
  873. gotvarsym.varstate:=vs_read;
  874. gotvarsym.refs:=1;
  875. end;
  876. {$endif i386}
  877. end;
  878. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  879. begin
  880. { update module flags }
  881. current_module.flags:=current_module.flags or flag;
  882. { create procdef }
  883. case flag of
  884. uf_init :
  885. begin
  886. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  887. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  888. end;
  889. uf_finalize :
  890. begin
  891. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  892. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  893. if (not current_module.is_unit) then
  894. result.procdef.aliasnames.insert('PASCALFINALIZE');
  895. end;
  896. else
  897. internalerror(200304253);
  898. end;
  899. result.code:=cnothingnode.create;
  900. end;
  901. procedure copy_macro(p:TObject; arg:pointer);
  902. begin
  903. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  904. end;
  905. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  906. var
  907. last_is_deprecated:boolean;
  908. begin
  909. try_consume_hintdirective:=false;
  910. repeat
  911. last_is_deprecated:=false;
  912. case idtoken of
  913. _LIBRARY :
  914. begin
  915. include(moduleopt,mo_hint_library);
  916. try_consume_hintdirective:=true;
  917. end;
  918. _DEPRECATED :
  919. begin
  920. include(moduleopt,mo_hint_deprecated);
  921. try_consume_hintdirective:=true;
  922. last_is_deprecated:=true;
  923. end;
  924. _EXPERIMENTAL :
  925. begin
  926. include(moduleopt,mo_hint_experimental);
  927. try_consume_hintdirective:=true;
  928. end;
  929. _PLATFORM :
  930. begin
  931. include(moduleopt,mo_hint_platform);
  932. try_consume_hintdirective:=true;
  933. end;
  934. _UNIMPLEMENTED :
  935. begin
  936. include(moduleopt,mo_hint_unimplemented);
  937. try_consume_hintdirective:=true;
  938. end;
  939. else
  940. break;
  941. end;
  942. consume(Token);
  943. { handle deprecated message }
  944. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  945. begin
  946. if deprecatedmsg<>nil then
  947. internalerror(201001221);
  948. if token=_CSTRING then
  949. deprecatedmsg:=stringdup(cstringpattern)
  950. else
  951. deprecatedmsg:=stringdup(pattern);
  952. consume(token);
  953. include(moduleopt,mo_has_deprecated_msg);
  954. end;
  955. until false;
  956. end;
  957. procedure proc_unit;
  958. function is_assembler_generated:boolean;
  959. var
  960. hal : tasmlisttype;
  961. begin
  962. result:=false;
  963. if Errorcount=0 then
  964. begin
  965. for hal:=low(TasmlistType) to high(TasmlistType) do
  966. if not current_asmdata.asmlists[hal].empty then
  967. begin
  968. result:=true;
  969. exit;
  970. end;
  971. end;
  972. end;
  973. var
  974. main_file: tinputfile;
  975. {$ifdef EXTDEBUG}
  976. store_crc,
  977. {$endif EXTDEBUG}
  978. store_interface_crc,
  979. store_indirect_crc: cardinal;
  980. s1,s2 : ^string; {Saves stack space}
  981. force_init_final : boolean;
  982. init_procinfo,
  983. finalize_procinfo : tcgprocinfo;
  984. unitname8 : string[8];
  985. ag: boolean;
  986. {$ifdef debug_devirt}
  987. i: longint;
  988. {$endif debug_devirt}
  989. begin
  990. init_procinfo:=nil;
  991. finalize_procinfo:=nil;
  992. if m_mac in current_settings.modeswitches then
  993. current_module.mode_switch_allowed:= false;
  994. consume(_UNIT);
  995. if compile_level=1 then
  996. Status.IsExe:=false;
  997. if token=_ID then
  998. begin
  999. { create filenames and unit name }
  1000. main_file := current_scanner.inputfile;
  1001. while assigned(main_file.next) do
  1002. main_file := main_file.next;
  1003. new(s1);
  1004. s1^:=current_module.modulename^;
  1005. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1006. current_module.SetModuleName(orgpattern);
  1007. { check for system unit }
  1008. new(s2);
  1009. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  1010. unitname8:=copy(current_module.modulename^,1,8);
  1011. if (cs_check_unit_name in current_settings.globalswitches) and
  1012. (
  1013. not(
  1014. (current_module.modulename^=s2^) or
  1015. (
  1016. (length(current_module.modulename^)>8) and
  1017. (unitname8=s2^)
  1018. )
  1019. )
  1020. or
  1021. (
  1022. (length(s1^)>8) and
  1023. (s1^<>current_module.modulename^)
  1024. )
  1025. ) then
  1026. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  1027. if (current_module.modulename^='SYSTEM') then
  1028. include(current_settings.moduleswitches,cs_compilesystem);
  1029. dispose(s2);
  1030. dispose(s1);
  1031. end;
  1032. if (target_info.system in systems_unit_program_exports) then
  1033. exportlib.preparelib(current_module.realmodulename^);
  1034. consume(_ID);
  1035. { parse hint directives }
  1036. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  1037. consume(_SEMICOLON);
  1038. consume(_INTERFACE);
  1039. { global switches are read, so further changes aren't allowed }
  1040. current_module.in_global:=false;
  1041. { handle the global switches }
  1042. setupglobalswitches;
  1043. message1(unit_u_loading_interface_units,current_module.modulename^);
  1044. { update status }
  1045. status.currentmodule:=current_module.realmodulename^;
  1046. { maybe turn off m_objpas if we are compiling objpas }
  1047. if (current_module.modulename^='OBJPAS') then
  1048. exclude(current_settings.modeswitches,m_objpas);
  1049. { maybe turn off m_mac if we are compiling macpas }
  1050. if (current_module.modulename^='MACPAS') then
  1051. exclude(current_settings.modeswitches,m_mac);
  1052. parse_only:=true;
  1053. { generate now the global symboltable,
  1054. define first as local to overcome dependency conflicts }
  1055. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  1056. { insert unitsym of this unit to prevent other units having
  1057. the same name }
  1058. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1059. { load default units, like the system unit }
  1060. loaddefaultunits;
  1061. { insert qualifier for the system unit (allows system.writeln) }
  1062. if not(cs_compilesystem in current_settings.moduleswitches) and
  1063. (token=_USES) then
  1064. begin
  1065. loadunits;
  1066. { has it been compiled at a higher level ?}
  1067. if current_module.state=ms_compiled then
  1068. exit;
  1069. end;
  1070. { move the global symtable from the temporary local to global }
  1071. current_module.globalsymtable:=current_module.localsymtable;
  1072. current_module.localsymtable:=nil;
  1073. { number all units, so we know if a unit is used by this unit or
  1074. needs to be added implicitly }
  1075. current_module.updatemaps;
  1076. { create whole program optimisation information (may already be
  1077. updated in the interface, e.g., in case of classrefdef typed
  1078. constants }
  1079. current_module.wpoinfo:=tunitwpoinfo.create;
  1080. { ... parse the declarations }
  1081. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1082. symtablestack.push(current_module.globalsymtable);
  1083. read_interface_declarations;
  1084. symtablestack.pop(current_module.globalsymtable);
  1085. { Export macros defined in the interface for macpas. The macros
  1086. are put in the globalmacrosymtable that will only be used by other
  1087. units. The current unit continues to use the localmacrosymtable }
  1088. if (m_mac in current_settings.modeswitches) then
  1089. begin
  1090. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  1091. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  1092. end;
  1093. { leave when we got an error }
  1094. if (Errorcount>0) and not status.skip_error then
  1095. begin
  1096. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1097. status.skip_error:=true;
  1098. exit;
  1099. end;
  1100. { Our interface is compiled, generate CRC and switch to implementation }
  1101. if not(cs_compilesystem in current_settings.moduleswitches) and
  1102. (Errorcount=0) then
  1103. tppumodule(current_module).getppucrc;
  1104. current_module.in_interface:=false;
  1105. current_module.interface_compiled:=true;
  1106. { First reload all units depending on our interface, we need to do this
  1107. in the implementation part to prevent erroneous circular references }
  1108. tppumodule(current_module).setdefgeneration;
  1109. tppumodule(current_module).reload_flagged_units;
  1110. { Parse the implementation section }
  1111. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1112. current_module.interface_only:=true
  1113. else
  1114. current_module.interface_only:=false;
  1115. parse_only:=false;
  1116. { create static symbol table }
  1117. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1118. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1119. maybe_load_got;
  1120. if not current_module.interface_only then
  1121. begin
  1122. consume(_IMPLEMENTATION);
  1123. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1124. { Read the implementation units }
  1125. parse_implementation_uses;
  1126. end;
  1127. if current_module.state=ms_compiled then
  1128. exit;
  1129. { All units are read, now give them a number }
  1130. current_module.updatemaps;
  1131. symtablestack.push(current_module.globalsymtable);
  1132. symtablestack.push(current_module.localsymtable);
  1133. if not current_module.interface_only then
  1134. begin
  1135. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1136. if current_module.in_interface then
  1137. internalerror(200212285);
  1138. { Compile the unit }
  1139. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  1140. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1141. init_procinfo.parse_body;
  1142. { save file pos for debuginfo }
  1143. current_module.mainfilepos:=init_procinfo.entrypos;
  1144. end;
  1145. { Generate specializations of objectdefs methods }
  1146. generate_specialization_procs;
  1147. { if the unit contains ansi/widestrings, initialization and
  1148. finalization code must be forced }
  1149. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1150. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1151. { should we force unit initialization? }
  1152. { this is a hack, but how can it be done better ? }
  1153. if force_init_final and ((current_module.flags and uf_init)=0) then
  1154. begin
  1155. { first release the not used init procinfo }
  1156. if assigned(init_procinfo) then
  1157. release_main_proc(init_procinfo);
  1158. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1159. end;
  1160. { finalize? }
  1161. if not current_module.interface_only and (token=_FINALIZATION) then
  1162. begin
  1163. { the uf_finalize flag is only set after we checked that it
  1164. wasn't empty }
  1165. { Compile the finalize }
  1166. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1167. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1168. finalize_procinfo.parse_body;
  1169. end
  1170. else if force_init_final then
  1171. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1172. { Now both init and finalize bodies are read and it is known
  1173. which variables are used in both init and finalize we can now
  1174. generate the code. This is required to prevent putting a variable in
  1175. a register that is also used in the finalize body (PFV) }
  1176. if assigned(init_procinfo) then
  1177. begin
  1178. init_procinfo.generate_code;
  1179. init_procinfo.resetprocdef;
  1180. release_main_proc(init_procinfo);
  1181. end;
  1182. if assigned(finalize_procinfo) then
  1183. begin
  1184. finalize_procinfo.generate_code;
  1185. finalize_procinfo.resetprocdef;
  1186. release_main_proc(finalize_procinfo);
  1187. end;
  1188. symtablestack.pop(current_module.localsymtable);
  1189. symtablestack.pop(current_module.globalsymtable);
  1190. { the last char should always be a point }
  1191. consume(_POINT);
  1192. { reset wpo flags for all defs }
  1193. reset_all_defs;
  1194. if (Errorcount=0) then
  1195. begin
  1196. { tests, if all (interface) forwards are resolved }
  1197. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1198. { check if all private fields are used }
  1199. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1200. { test static symtable }
  1201. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1202. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1203. tstoredsymtable(current_module.localsymtable).check_forwards;
  1204. tstoredsymtable(current_module.localsymtable).checklabels;
  1205. { used units }
  1206. current_module.allunitsused;
  1207. end;
  1208. { leave when we got an error }
  1209. if (Errorcount>0) and not status.skip_error then
  1210. begin
  1211. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1212. status.skip_error:=true;
  1213. exit;
  1214. end;
  1215. { if an Objective-C module, generate rtti and module info }
  1216. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1217. { do we need to add the variants unit? }
  1218. maybeloadvariantsunit;
  1219. { generate wrappers for interfaces }
  1220. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable,false);
  1221. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  1222. { generate rtti/init tables }
  1223. write_persistent_type_info(current_module.globalsymtable);
  1224. write_persistent_type_info(current_module.localsymtable);
  1225. { Tables }
  1226. InsertThreadvars;
  1227. { Resource strings }
  1228. GenerateResourceStrings;
  1229. { Widestring typed constants }
  1230. InsertWideInits;
  1231. { generate debuginfo }
  1232. if (cs_debuginfo in current_settings.moduleswitches) then
  1233. current_debuginfo.inserttypeinfo;
  1234. { generate imports }
  1235. if current_module.ImportLibraryList.Count>0 then
  1236. importlib.generatelib;
  1237. { insert own objectfile, or say that it's in a library
  1238. (no check for an .o when loading) }
  1239. ag:=is_assembler_generated;
  1240. if ag then
  1241. insertobjectfile
  1242. else
  1243. begin
  1244. current_module.flags:=current_module.flags or uf_no_link;
  1245. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1246. end;
  1247. if ag then
  1248. begin
  1249. { create callframe info }
  1250. create_dwarf_frame;
  1251. { assemble }
  1252. create_objectfile;
  1253. end;
  1254. { Write out the ppufile after the object file has been created }
  1255. store_interface_crc:=current_module.interface_crc;
  1256. store_indirect_crc:=current_module.indirect_crc;
  1257. {$ifdef EXTDEBUG}
  1258. store_crc:=current_module.crc;
  1259. {$endif EXTDEBUG}
  1260. if (Errorcount=0) then
  1261. tppumodule(current_module).writeppu;
  1262. if not(cs_compilesystem in current_settings.moduleswitches) then
  1263. begin
  1264. if store_interface_crc<>current_module.interface_crc then
  1265. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1266. if store_indirect_crc<>current_module.indirect_crc then
  1267. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1268. end;
  1269. {$ifdef EXTDEBUG}
  1270. if not(cs_compilesystem in current_settings.moduleswitches) then
  1271. if (store_crc<>current_module.crc) and simplify_ppu then
  1272. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1273. {$endif EXTDEBUG}
  1274. { release local symtables that are not needed anymore }
  1275. free_localsymtables(current_module.globalsymtable);
  1276. free_localsymtables(current_module.localsymtable);
  1277. { leave when we got an error }
  1278. if (Errorcount>0) and not status.skip_error then
  1279. begin
  1280. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1281. status.skip_error:=true;
  1282. exit;
  1283. end;
  1284. {$ifdef debug_devirt}
  1285. { print out all instantiated class/object types }
  1286. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1287. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1288. begin
  1289. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1290. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1291. objectdef:
  1292. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1293. odt_object:
  1294. writeln(' (object)');
  1295. odt_class:
  1296. writeln(' (class)');
  1297. else
  1298. internalerror(2008101103);
  1299. end;
  1300. else
  1301. internalerror(2008101104);
  1302. end;
  1303. end;
  1304. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1305. begin
  1306. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1307. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1308. objectdef:
  1309. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1310. odt_class:
  1311. writeln(' (classrefdef)');
  1312. else
  1313. internalerror(2008101105);
  1314. end
  1315. else
  1316. internalerror(2008101102);
  1317. end;
  1318. end;
  1319. {$endif debug_devirt}
  1320. Message1(unit_u_finished_compiling,current_module.modulename^);
  1321. end;
  1322. procedure procexport(const s : string);
  1323. var
  1324. hp : texported_item;
  1325. begin
  1326. hp:=texported_item.create;
  1327. hp.name:=stringdup(s);
  1328. hp.options:=hp.options or eo_name;
  1329. exportlib.exportprocedure(hp);
  1330. end;
  1331. procedure varexport(const s : string);
  1332. var
  1333. hp : texported_item;
  1334. begin
  1335. hp:=texported_item.create;
  1336. hp.name:=stringdup(s);
  1337. hp.options:=hp.options or eo_name;
  1338. exportlib.exportvar(hp);
  1339. end;
  1340. procedure insert_export(sym : TObject;arg:pointer);
  1341. var
  1342. i : longint;
  1343. item : TCmdStrListItem;
  1344. begin
  1345. case TSym(sym).typ of
  1346. { ignore: }
  1347. unitsym,
  1348. syssym,
  1349. constsym,
  1350. enumsym,
  1351. typesym:
  1352. ;
  1353. procsym:
  1354. begin
  1355. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1356. begin
  1357. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1358. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1359. ((tsymtable(arg).symtabletype=globalsymtable) or
  1360. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1361. ) then
  1362. begin
  1363. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1364. { walk through all aliases }
  1365. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1366. while assigned(item) do
  1367. begin
  1368. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1369. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1370. procexport(item.str);
  1371. item:=TCmdStrListItem(item.next);
  1372. end;
  1373. end;
  1374. end;
  1375. end;
  1376. staticvarsym:
  1377. begin
  1378. varexport(tsym(sym).mangledname);
  1379. end;
  1380. else
  1381. begin
  1382. writeln('unknown: ',ord(TSym(sym).typ));
  1383. end;
  1384. end;
  1385. end;
  1386. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1387. Var
  1388. MakeStatic : Boolean;
  1389. Var
  1390. buffer : array[0..$1fff] of byte;
  1391. inppu,
  1392. outppu : tppufile;
  1393. b,
  1394. untilb : byte;
  1395. l,m : longint;
  1396. f : file;
  1397. ext,
  1398. s : string;
  1399. ppuversion : dword;
  1400. begin
  1401. Result:=false;
  1402. MakeStatic:=False;
  1403. inppu:=tppufile.create(PPUFn);
  1404. if not inppu.openfile then
  1405. begin
  1406. inppu.free;
  1407. Comment(V_Error,'Could not open : '+PPUFn);
  1408. Exit;
  1409. end;
  1410. { Check the ppufile }
  1411. if not inppu.CheckPPUId then
  1412. begin
  1413. inppu.free;
  1414. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1415. Exit;
  1416. end;
  1417. ppuversion:=inppu.GetPPUVersion;
  1418. if ppuversion<CurrentPPUVersion then
  1419. begin
  1420. inppu.free;
  1421. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1422. Exit;
  1423. end;
  1424. { No .o file generated for this ppu, just skip }
  1425. if (inppu.header.flags and uf_no_link)<>0 then
  1426. begin
  1427. inppu.free;
  1428. Result:=true;
  1429. Exit;
  1430. end;
  1431. { Already a lib? }
  1432. if (inppu.header.flags and uf_in_library)<>0 then
  1433. begin
  1434. inppu.free;
  1435. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1436. Exit;
  1437. end;
  1438. { We need a static linked unit }
  1439. if (inppu.header.flags and uf_static_linked)=0 then
  1440. begin
  1441. inppu.free;
  1442. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1443. Exit;
  1444. end;
  1445. { Check if shared is allowed }
  1446. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1447. begin
  1448. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1449. MakeStatic:=true;
  1450. end;
  1451. { Create the new ppu }
  1452. if PPUFn=PPLFn then
  1453. outppu:=tppufile.create('ppumove.$$$')
  1454. else
  1455. outppu:=tppufile.create(PPLFn);
  1456. outppu.createfile;
  1457. { Create new header, with the new flags }
  1458. outppu.header:=inppu.header;
  1459. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1460. if MakeStatic then
  1461. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1462. else
  1463. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1464. { read until the object files are found }
  1465. untilb:=iblinkunitofiles;
  1466. repeat
  1467. b:=inppu.readentry;
  1468. if b in [ibendinterface,ibend] then
  1469. begin
  1470. inppu.free;
  1471. outppu.free;
  1472. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1473. Exit;
  1474. end;
  1475. if b<>untilb then
  1476. begin
  1477. repeat
  1478. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1479. outppu.putdata(buffer,l);
  1480. until l<sizeof(buffer);
  1481. outppu.writeentry(b);
  1482. end;
  1483. until (b=untilb);
  1484. { we have now reached the section for the files which need to be added,
  1485. now add them to the list }
  1486. case b of
  1487. iblinkunitofiles :
  1488. begin
  1489. { add all o files, and save the entry when not creating a static
  1490. library to keep staticlinking possible }
  1491. while not inppu.endofentry do
  1492. begin
  1493. s:=inppu.getstring;
  1494. m:=inppu.getlongint;
  1495. if not MakeStatic then
  1496. begin
  1497. outppu.putstring(s);
  1498. outppu.putlongint(m);
  1499. end;
  1500. current_module.linkotherofiles.add(s,link_always);;
  1501. end;
  1502. if not MakeStatic then
  1503. outppu.writeentry(b);
  1504. end;
  1505. { iblinkunitstaticlibs :
  1506. begin
  1507. AddToLinkFiles(ExtractLib(inppu.getstring));
  1508. if not inppu.endofentry then
  1509. begin
  1510. repeat
  1511. inppu.getdatabuf(buffer^,bufsize,l);
  1512. outppu.putdata(buffer^,l);
  1513. until l<bufsize;
  1514. outppu.writeentry(b);
  1515. end;
  1516. end; }
  1517. end;
  1518. { just add a new entry with the new lib }
  1519. if MakeStatic then
  1520. begin
  1521. outppu.putstring('imp'+current_module.realmodulename^);
  1522. outppu.putlongint(link_static);
  1523. outppu.writeentry(iblinkunitstaticlibs)
  1524. end
  1525. else
  1526. begin
  1527. outppu.putstring('imp'+current_module.realmodulename^);
  1528. outppu.putlongint(link_shared);
  1529. outppu.writeentry(iblinkunitsharedlibs);
  1530. end;
  1531. { read all entries until the end and write them also to the new ppu }
  1532. repeat
  1533. b:=inppu.readentry;
  1534. { don't write ibend, that's written automatically }
  1535. if b<>ibend then
  1536. begin
  1537. if b=iblinkothersharedlibs then
  1538. begin
  1539. while not inppu.endofentry do
  1540. begin
  1541. s:=inppu.getstring;
  1542. m:=inppu.getlongint;
  1543. outppu.putstring(s);
  1544. outppu.putlongint(m);
  1545. { strip lib prefix }
  1546. if copy(s,1,3)='lib' then
  1547. delete(s,1,3);
  1548. ext:=ExtractFileExt(s);
  1549. if ext<>'' then
  1550. delete(s,length(s)-length(ext)+1,length(ext));
  1551. current_module.linkOtherSharedLibs.add(s,link_always);
  1552. end;
  1553. end
  1554. else
  1555. repeat
  1556. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1557. outppu.putdata(buffer,l);
  1558. until l<sizeof(buffer);
  1559. outppu.writeentry(b);
  1560. end;
  1561. until b=ibend;
  1562. { write the last stuff and close }
  1563. outppu.flush;
  1564. outppu.writeheader;
  1565. outppu.free;
  1566. inppu.free;
  1567. { rename }
  1568. if PPUFn=PPLFn then
  1569. begin
  1570. {$I-}
  1571. assign(f,PPUFn);
  1572. erase(f);
  1573. assign(f,'ppumove.$$$');
  1574. rename(f,PPUFn);
  1575. {$I+}
  1576. if ioresult<>0 then;
  1577. end;
  1578. Result:=True;
  1579. end;
  1580. procedure createimportlibfromexports;
  1581. var
  1582. hp : texported_item;
  1583. begin
  1584. hp:=texported_item(current_module._exports.first);
  1585. while assigned(hp) do
  1586. begin
  1587. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.name^,hp.index,hp.is_var,false);
  1588. hp:=texported_item(hp.next);
  1589. end;
  1590. end;
  1591. procedure proc_package;
  1592. var
  1593. main_file : tinputfile;
  1594. hp,hp2 : tmodule;
  1595. {finalize_procinfo,
  1596. init_procinfo,
  1597. main_procinfo : tcgprocinfo;}
  1598. force_init_final : boolean;
  1599. uu : tused_unit;
  1600. begin
  1601. Status.IsPackage:=true;
  1602. Status.IsExe:=true;
  1603. parse_only:=false;
  1604. {main_procinfo:=nil;
  1605. init_procinfo:=nil;
  1606. finalize_procinfo:=nil;}
  1607. if not RelocSectionSetExplicitly then
  1608. RelocSection:=true;
  1609. { Relocation works only without stabs under Windows when }
  1610. { external linker (LD) is used. LD generates relocs for }
  1611. { stab sections which is not loaded in memory. It causes }
  1612. { AV error when DLL is loaded and relocation is needed. }
  1613. { Internal linker does not have this problem. }
  1614. if RelocSection and
  1615. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1616. (cs_link_extern in current_settings.globalswitches) then
  1617. begin
  1618. include(current_settings.globalswitches,cs_link_strip);
  1619. { Warning stabs info does not work with reloc section !! }
  1620. if (cs_debuginfo in current_settings.moduleswitches) and
  1621. (target_dbg.id=dbg_stabs) then
  1622. begin
  1623. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1624. Message(parser_w_parser_win32_debug_needs_WN);
  1625. exclude(current_settings.moduleswitches,cs_debuginfo);
  1626. end;
  1627. end;
  1628. { get correct output names }
  1629. main_file := current_scanner.inputfile;
  1630. while assigned(main_file.next) do
  1631. main_file := main_file.next;
  1632. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1633. consume(_ID);
  1634. current_module.setmodulename(orgpattern);
  1635. current_module.ispackage:=true;
  1636. exportlib.preparelib(orgpattern);
  1637. if tf_library_needs_pic in target_info.flags then
  1638. include(current_settings.moduleswitches,cs_create_pic);
  1639. consume(_ID);
  1640. consume(_SEMICOLON);
  1641. { global switches are read, so further changes aren't allowed }
  1642. current_module.in_global:=false;
  1643. { setup things using the switches }
  1644. setupglobalswitches;
  1645. { set implementation flag }
  1646. current_module.in_interface:=false;
  1647. current_module.interface_compiled:=true;
  1648. { insert after the unit symbol tables the static symbol table }
  1649. { of the program }
  1650. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1651. {Load the units used by the program we compile.}
  1652. if token=_REQUIRES then
  1653. begin
  1654. end;
  1655. {Load the units used by the program we compile.}
  1656. if (token=_ID) and (idtoken=_CONTAINS) then
  1657. begin
  1658. consume(_ID);
  1659. while true do
  1660. begin
  1661. if token=_ID then
  1662. AddUnit(pattern);
  1663. consume(_ID);
  1664. if token=_COMMA then
  1665. consume(_COMMA)
  1666. else break;
  1667. end;
  1668. consume(_SEMICOLON);
  1669. end;
  1670. { All units are read, now give them a number }
  1671. current_module.updatemaps;
  1672. {Insert the name of the main program into the symbol table.}
  1673. if current_module.realmodulename^<>'' then
  1674. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1675. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1676. symtablestack.push(current_module.localsymtable);
  1677. { create whole program optimisation information }
  1678. current_module.wpoinfo:=tunitwpoinfo.create;
  1679. { should we force unit initialization? }
  1680. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1681. if force_init_final then
  1682. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1683. { Add symbol to the exports section for win32 so smartlinking a
  1684. DLL will include the edata section }
  1685. if assigned(exportlib) and
  1686. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1687. ((current_module.flags and uf_has_exports)<>0) then
  1688. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1689. { all labels must be defined before generating code }
  1690. if Errorcount=0 then
  1691. tstoredsymtable(current_module.localsymtable).checklabels;
  1692. symtablestack.pop(current_module.localsymtable);
  1693. { consume the last point }
  1694. consume(_END);
  1695. consume(_POINT);
  1696. if (Errorcount=0) then
  1697. begin
  1698. { test static symtable }
  1699. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1700. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1701. tstoredsymtable(current_module.localsymtable).check_forwards;
  1702. current_module.allunitsused;
  1703. end;
  1704. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1705. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1706. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1707. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1708. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1709. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1710. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1711. { leave when we got an error }
  1712. if (Errorcount>0) and not status.skip_error then
  1713. begin
  1714. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1715. status.skip_error:=true;
  1716. exit;
  1717. end;
  1718. { remove all unused units, this happends when units are removed
  1719. from the uses clause in the source and the ppu was already being loaded }
  1720. hp:=tmodule(loaded_units.first);
  1721. while assigned(hp) do
  1722. begin
  1723. hp2:=hp;
  1724. hp:=tmodule(hp.next);
  1725. if hp2.is_unit and
  1726. not assigned(hp2.globalsymtable) then
  1727. loaded_units.remove(hp2);
  1728. end;
  1729. { force exports }
  1730. uu:=tused_unit(usedunits.first);
  1731. while assigned(uu) do
  1732. begin
  1733. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1734. { check localsymtable for exports too to get public symbols }
  1735. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1736. { create special exports }
  1737. if (uu.u.flags and uf_init)<>0 then
  1738. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1739. if (uu.u.flags and uf_finalize)<>0 then
  1740. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1741. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1742. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1743. uu:=tused_unit(uu.next);
  1744. end;
  1745. {$ifdef arm}
  1746. { Insert .pdata section for arm-wince.
  1747. It is needed for exception handling. }
  1748. if target_info.system in [system_arm_wince] then
  1749. InsertPData;
  1750. {$endif arm}
  1751. { generate debuginfo }
  1752. if (cs_debuginfo in current_settings.moduleswitches) then
  1753. current_debuginfo.inserttypeinfo;
  1754. exportlib.generatelib;
  1755. { write all our exports to the import library,
  1756. needs to be done after exportlib.generatelib; }
  1757. createimportlibfromexports;
  1758. { generate imports }
  1759. if current_module.ImportLibraryList.Count>0 then
  1760. importlib.generatelib;
  1761. { Reference all DEBUGINFO sections from the main .fpc section }
  1762. if (cs_debuginfo in current_settings.moduleswitches) then
  1763. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1764. { insert own objectfile }
  1765. insertobjectfile;
  1766. { assemble and link }
  1767. create_objectfile;
  1768. { We might need the symbols info if not using
  1769. the default do_extractsymbolinfo
  1770. which is a dummy function PM }
  1771. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1772. { release all local symtables that are not needed anymore }
  1773. if (not needsymbolinfo) then
  1774. free_localsymtables(current_module.localsymtable);
  1775. { leave when we got an error }
  1776. if (Errorcount>0) and not status.skip_error then
  1777. begin
  1778. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1779. status.skip_error:=true;
  1780. exit;
  1781. end;
  1782. if (not current_module.is_unit) then
  1783. begin
  1784. { finally rewrite all units included into the package }
  1785. uu:=tused_unit(usedunits.first);
  1786. while assigned(uu) do
  1787. begin
  1788. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1789. uu:=tused_unit(uu.next);
  1790. end;
  1791. { create the executable when we are at level 1 }
  1792. if (compile_level=1) then
  1793. begin
  1794. { create global resource file by collecting all resource files }
  1795. CollectResourceFiles;
  1796. { write .def file }
  1797. if (cs_link_deffile in current_settings.globalswitches) then
  1798. deffile.writefile;
  1799. { insert all .o files from all loaded units and
  1800. unload the units, we don't need them anymore.
  1801. Keep the current_module because that is still needed }
  1802. hp:=tmodule(loaded_units.first);
  1803. while assigned(hp) do
  1804. begin
  1805. { the package itself contains no code so far }
  1806. linker.AddModuleFiles(hp);
  1807. hp2:=tmodule(hp.next);
  1808. if (hp<>current_module) and
  1809. (not needsymbolinfo) then
  1810. begin
  1811. loaded_units.remove(hp);
  1812. hp.free;
  1813. end;
  1814. hp:=hp2;
  1815. end;
  1816. linker.MakeSharedLibrary
  1817. end;
  1818. { Give Fatal with error count for linker errors }
  1819. if (Errorcount>0) and not status.skip_error then
  1820. begin
  1821. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1822. status.skip_error:=true;
  1823. end;
  1824. end;
  1825. end;
  1826. procedure proc_program(islibrary : boolean);
  1827. var
  1828. main_file : tinputfile;
  1829. hp,hp2 : tmodule;
  1830. finalize_procinfo,
  1831. init_procinfo,
  1832. main_procinfo : tcgprocinfo;
  1833. force_init_final : boolean;
  1834. resources_used : boolean;
  1835. begin
  1836. DLLsource:=islibrary;
  1837. Status.IsLibrary:=IsLibrary;
  1838. Status.IsPackage:=false;
  1839. Status.IsExe:=true;
  1840. parse_only:=false;
  1841. main_procinfo:=nil;
  1842. init_procinfo:=nil;
  1843. finalize_procinfo:=nil;
  1844. resources_used:=false;
  1845. { DLL defaults to create reloc info }
  1846. if islibrary then
  1847. begin
  1848. if not RelocSectionSetExplicitly then
  1849. RelocSection:=true;
  1850. end;
  1851. { Relocation works only without stabs under Windows when }
  1852. { external linker (LD) is used. LD generates relocs for }
  1853. { stab sections which is not loaded in memory. It causes }
  1854. { AV error when DLL is loaded and relocation is needed. }
  1855. { Internal linker does not have this problem. }
  1856. if RelocSection and
  1857. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1858. (cs_link_extern in current_settings.globalswitches) then
  1859. begin
  1860. include(current_settings.globalswitches,cs_link_strip);
  1861. { Warning stabs info does not work with reloc section !! }
  1862. if (cs_debuginfo in current_settings.moduleswitches) and
  1863. (target_dbg.id=dbg_stabs) then
  1864. begin
  1865. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1866. Message(parser_w_parser_win32_debug_needs_WN);
  1867. exclude(current_settings.moduleswitches,cs_debuginfo);
  1868. end;
  1869. end;
  1870. { get correct output names }
  1871. main_file := current_scanner.inputfile;
  1872. while assigned(main_file.next) do
  1873. main_file := main_file.next;
  1874. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1875. if islibrary then
  1876. begin
  1877. consume(_LIBRARY);
  1878. current_module.setmodulename(orgpattern);
  1879. current_module.islibrary:=true;
  1880. exportlib.preparelib(orgpattern);
  1881. if tf_library_needs_pic in target_info.flags then
  1882. include(current_settings.moduleswitches,cs_create_pic);
  1883. consume(_ID);
  1884. consume(_SEMICOLON);
  1885. end
  1886. else
  1887. { is there an program head ? }
  1888. if token=_PROGRAM then
  1889. begin
  1890. consume(_PROGRAM);
  1891. current_module.setmodulename(orgpattern);
  1892. if (target_info.system in systems_unit_program_exports) then
  1893. exportlib.preparelib(orgpattern);
  1894. consume(_ID);
  1895. if token=_LKLAMMER then
  1896. begin
  1897. consume(_LKLAMMER);
  1898. repeat
  1899. consume(_ID);
  1900. until not try_to_consume(_COMMA);
  1901. consume(_RKLAMMER);
  1902. end;
  1903. consume(_SEMICOLON);
  1904. end
  1905. else if (target_info.system in systems_unit_program_exports) then
  1906. exportlib.preparelib(current_module.realmodulename^);
  1907. { global switches are read, so further changes aren't allowed }
  1908. current_module.in_global:=false;
  1909. { setup things using the switches }
  1910. setupglobalswitches;
  1911. { set implementation flag }
  1912. current_module.in_interface:=false;
  1913. current_module.interface_compiled:=true;
  1914. { insert after the unit symbol tables the static symbol table }
  1915. { of the program }
  1916. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1917. { load standard units (system,objpas,profile unit) }
  1918. loaddefaultunits;
  1919. { Load units provided on the command line }
  1920. loadautounits;
  1921. {Load the units used by the program we compile.}
  1922. if token=_USES then
  1923. loadunits;
  1924. { All units are read, now give them a number }
  1925. current_module.updatemaps;
  1926. {Insert the name of the main program into the symbol table.}
  1927. if current_module.realmodulename^<>'' then
  1928. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1929. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1930. symtablestack.push(current_module.localsymtable);
  1931. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1932. maybe_load_got;
  1933. { create whole program optimisation information }
  1934. current_module.wpoinfo:=tunitwpoinfo.create;
  1935. { The program intialization needs an alias, so it can be called
  1936. from the bootstrap code.}
  1937. if islibrary then
  1938. begin
  1939. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1940. { Win32 startup code needs a single name }
  1941. if not(target_info.system in systems_darwin) then
  1942. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1943. else
  1944. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1945. end
  1946. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1947. begin
  1948. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1949. end
  1950. else
  1951. begin
  1952. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1953. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1954. end;
  1955. main_procinfo.parse_body;
  1956. { save file pos for debuginfo }
  1957. current_module.mainfilepos:=main_procinfo.entrypos;
  1958. { Generate specializations of objectdefs methods }
  1959. generate_specialization_procs;
  1960. { should we force unit initialization? }
  1961. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1962. if force_init_final then
  1963. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1964. { Add symbol to the exports section for win32 so smartlinking a
  1965. DLL will include the edata section }
  1966. if assigned(exportlib) and
  1967. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1968. ((current_module.flags and uf_has_exports)<>0) then
  1969. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1970. { finalize? }
  1971. if token=_FINALIZATION then
  1972. begin
  1973. { the uf_finalize flag is only set after we checked that it
  1974. wasn't empty }
  1975. { Parse the finalize }
  1976. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1977. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1978. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1979. finalize_procinfo.parse_body;
  1980. end
  1981. else
  1982. if force_init_final then
  1983. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1984. { the finalization routine of libraries is generic (and all libraries need to }
  1985. { be finalized, so they can finalize any units they use }
  1986. if (islibrary) then
  1987. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1988. { all labels must be defined before generating code }
  1989. if Errorcount=0 then
  1990. tstoredsymtable(current_module.localsymtable).checklabels;
  1991. { See remark in unit init/final }
  1992. main_procinfo.generate_code;
  1993. main_procinfo.resetprocdef;
  1994. release_main_proc(main_procinfo);
  1995. if assigned(init_procinfo) then
  1996. begin
  1997. init_procinfo.generate_code;
  1998. init_procinfo.resetprocdef;
  1999. release_main_proc(init_procinfo);
  2000. end;
  2001. if assigned(finalize_procinfo) then
  2002. begin
  2003. finalize_procinfo.generate_code;
  2004. finalize_procinfo.resetprocdef;
  2005. release_main_proc(finalize_procinfo);
  2006. end;
  2007. symtablestack.pop(current_module.localsymtable);
  2008. { consume the last point }
  2009. consume(_POINT);
  2010. { reset wpo flags for all defs }
  2011. reset_all_defs;
  2012. if (Errorcount=0) then
  2013. begin
  2014. { test static symtable }
  2015. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2016. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2017. tstoredsymtable(current_module.localsymtable).check_forwards;
  2018. current_module.allunitsused;
  2019. end;
  2020. { leave when we got an error }
  2021. if (Errorcount>0) and not status.skip_error then
  2022. begin
  2023. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2024. status.skip_error:=true;
  2025. exit;
  2026. end;
  2027. { remove all unused units, this happens when units are removed
  2028. from the uses clause in the source and the ppu was already being loaded }
  2029. hp:=tmodule(loaded_units.first);
  2030. while assigned(hp) do
  2031. begin
  2032. hp2:=hp;
  2033. hp:=tmodule(hp.next);
  2034. if hp2.is_unit and
  2035. not assigned(hp2.globalsymtable) then
  2036. begin
  2037. loaded_units.remove(hp2);
  2038. unloaded_units.concat(hp2);
  2039. end;
  2040. end;
  2041. { do we need to add the variants unit? }
  2042. maybeloadvariantsunit;
  2043. { Now that everything has been compiled we know if we need resource
  2044. support. If not, remove the unit. }
  2045. resources_used:=MaybeRemoveResUnit;
  2046. linker.initsysinitunitname;
  2047. if target_info.system in systems_internal_sysinit then
  2048. begin
  2049. { add start/halt unit }
  2050. AddUnit(linker.sysinitunit);
  2051. end;
  2052. {$ifdef arm}
  2053. { Insert .pdata section for arm-wince.
  2054. It is needed for exception handling. }
  2055. if target_info.system in [system_arm_wince] then
  2056. InsertPData;
  2057. {$endif arm}
  2058. InsertThreadvars;
  2059. { generate rtti/init tables }
  2060. write_persistent_type_info(current_module.localsymtable);
  2061. { if an Objective-C module, generate rtti and module info }
  2062. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2063. { generate wrappers for interfaces }
  2064. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  2065. { generate imports }
  2066. if current_module.ImportLibraryList.Count>0 then
  2067. importlib.generatelib;
  2068. { generate debuginfo }
  2069. if (cs_debuginfo in current_settings.moduleswitches) then
  2070. current_debuginfo.inserttypeinfo;
  2071. if islibrary or (target_info.system in systems_unit_program_exports) then
  2072. exportlib.generatelib;
  2073. { Reference all DEBUGINFO sections from the main .fpc section }
  2074. if (cs_debuginfo in current_settings.moduleswitches) then
  2075. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2076. { Resource strings }
  2077. GenerateResourceStrings;
  2078. { Windows widestring needing initialization }
  2079. InsertWideInits;
  2080. { insert Tables and StackLength }
  2081. InsertInitFinalTable;
  2082. InsertThreadvarTablesTable;
  2083. InsertResourceTablesTable;
  2084. InsertWideInitsTablesTable;
  2085. InsertMemorySizes;
  2086. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2087. if target_info.system in systems_interrupt_table then
  2088. InsertInterruptTable;
  2089. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2090. { Insert symbol to resource info }
  2091. InsertResourceInfo(resources_used);
  2092. { create callframe info }
  2093. create_dwarf_frame;
  2094. { insert own objectfile }
  2095. insertobjectfile;
  2096. { assemble and link }
  2097. create_objectfile;
  2098. { We might need the symbols info if not using
  2099. the default do_extractsymbolinfo
  2100. which is a dummy function PM }
  2101. needsymbolinfo:=
  2102. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2103. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2104. { release all local symtables that are not needed anymore }
  2105. if (not needsymbolinfo) then
  2106. free_localsymtables(current_module.localsymtable);
  2107. { leave when we got an error }
  2108. if (Errorcount>0) and not status.skip_error then
  2109. begin
  2110. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2111. status.skip_error:=true;
  2112. exit;
  2113. end;
  2114. if (not current_module.is_unit) then
  2115. begin
  2116. { create the executable when we are at level 1 }
  2117. if (compile_level=1) then
  2118. begin
  2119. { create global resource file by collecting all resource files }
  2120. CollectResourceFiles;
  2121. { write .def file }
  2122. if (cs_link_deffile in current_settings.globalswitches) then
  2123. deffile.writefile;
  2124. { insert all .o files from all loaded units and
  2125. unload the units, we don't need them anymore.
  2126. Keep the current_module because that is still needed }
  2127. hp:=tmodule(loaded_units.first);
  2128. while assigned(hp) do
  2129. begin
  2130. linker.AddModuleFiles(hp);
  2131. hp2:=tmodule(hp.next);
  2132. if (hp<>current_module) and
  2133. (not needsymbolinfo) then
  2134. begin
  2135. loaded_units.remove(hp);
  2136. hp.free;
  2137. end;
  2138. hp:=hp2;
  2139. end;
  2140. { free also unneeded units we didn't free before }
  2141. if not needsymbolinfo then
  2142. unloaded_units.Clear;
  2143. { finally we can create a executable }
  2144. if DLLSource then
  2145. linker.MakeSharedLibrary
  2146. else
  2147. linker.MakeExecutable;
  2148. { collect all necessary information for whole-program optimization }
  2149. wpoinfomanager.extractwpoinfofromprogram;
  2150. end;
  2151. { Give Fatal with error count for linker errors }
  2152. if (Errorcount>0) and not status.skip_error then
  2153. begin
  2154. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2155. status.skip_error:=true;
  2156. end;
  2157. end;
  2158. end;
  2159. end.