pmodules.pas 90 KB

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