darwin.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693
  1. /* Functions for generic Darwin as target machine for GNU C compiler.
  2. Copyright (C) 1989-2015 Free Software Foundation, Inc.
  3. Contributed by Apple Computer Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "rtl.h"
  21. #include "regs.h"
  22. #include "hard-reg-set.h"
  23. #include "insn-config.h"
  24. #include "conditions.h"
  25. #include "insn-flags.h"
  26. #include "output.h"
  27. #include "insn-attr.h"
  28. #include "flags.h"
  29. #include "hash-set.h"
  30. #include "machmode.h"
  31. #include "vec.h"
  32. #include "double-int.h"
  33. #include "input.h"
  34. #include "alias.h"
  35. #include "symtab.h"
  36. #include "wide-int.h"
  37. #include "inchash.h"
  38. #include "tree.h"
  39. #include "fold-const.h"
  40. #include "stringpool.h"
  41. #include "varasm.h"
  42. #include "stor-layout.h"
  43. #include "hashtab.h"
  44. #include "function.h"
  45. #include "statistics.h"
  46. #include "real.h"
  47. #include "fixed-value.h"
  48. #include "expmed.h"
  49. #include "dojump.h"
  50. #include "explow.h"
  51. #include "calls.h"
  52. #include "emit-rtl.h"
  53. #include "stmt.h"
  54. #include "expr.h"
  55. #include "reload.h"
  56. #include "ggc.h"
  57. #include "langhooks.h"
  58. #include "target.h"
  59. #include "tm_p.h"
  60. #include "diagnostic-core.h"
  61. #include "toplev.h"
  62. #include "dominance.h"
  63. #include "cfg.h"
  64. #include "cfgrtl.h"
  65. #include "cfganal.h"
  66. #include "lcm.h"
  67. #include "cfgbuild.h"
  68. #include "cfgcleanup.h"
  69. #include "predict.h"
  70. #include "basic-block.h"
  71. #include "df.h"
  72. #include "debug.h"
  73. #include "obstack.h"
  74. #include "hash-table.h"
  75. #include "tree-ssa-alias.h"
  76. #include "internal-fn.h"
  77. #include "gimple-fold.h"
  78. #include "tree-eh.h"
  79. #include "gimple-expr.h"
  80. #include "is-a.h"
  81. #include "gimple.h"
  82. #include "gimplify.h"
  83. #include "hash-map.h"
  84. #include "plugin-api.h"
  85. #include "ipa-ref.h"
  86. #include "cgraph.h"
  87. #include "lto-streamer.h"
  88. #include "lto-section-names.h"
  89. /* Darwin supports a feature called fix-and-continue, which is used
  90. for rapid turn around debugging. When code is compiled with the
  91. -mfix-and-continue flag, two changes are made to the generated code
  92. that allow the system to do things that it would normally not be
  93. able to do easily. These changes allow gdb to load in
  94. recompilation of a translation unit that has been changed into a
  95. running program and replace existing functions and methods of that
  96. translation unit with versions of those functions and methods
  97. from the newly compiled translation unit. The new functions access
  98. the existing static symbols from the old translation unit, if the
  99. symbol existed in the unit to be replaced, and from the new
  100. translation unit, otherwise.
  101. The changes are to insert 5 nops at the beginning of all functions
  102. and to use indirection to get at static symbols. The 5 nops
  103. are required by consumers of the generated code. Currently, gdb
  104. uses this to patch in a jump to the overriding function, this
  105. allows all uses of the old name to forward to the replacement,
  106. including existing function pointers and virtual methods. See
  107. rs6000_emit_prologue for the code that handles the nop insertions.
  108. The added indirection allows gdb to redirect accesses to static
  109. symbols from the newly loaded translation unit to the existing
  110. symbol, if any. @code{static} symbols are special and are handled by
  111. setting the second word in the .non_lazy_symbol_pointer data
  112. structure to symbol. See indirect_data for the code that handles
  113. the extra indirection, and machopic_output_indirection and its use
  114. of MACHO_SYMBOL_STATIC for the code that handles @code{static}
  115. symbol indirection. */
  116. /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
  117. branch islands and we no longer need to emit darwin stubs.
  118. However, if we are generating code for earlier systems (or for use in the
  119. kernel) the stubs might still be required, and this will be set true. */
  120. int darwin_emit_branch_islands = false;
  121. typedef struct GTY(()) cdtor_record {
  122. rtx symbol;
  123. int priority; /* [con/de]structor priority */
  124. int position; /* original position */
  125. } cdtor_record;
  126. static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
  127. static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
  128. /* A flag to determine whether we are running c++ or obj-c++. This has to be
  129. settable from non-c-family contexts too (i.e. we can't use the c_dialect_
  130. functions). */
  131. int darwin_running_cxx;
  132. /* Some code-gen now depends on OS major version numbers (at least). */
  133. int generating_for_darwin_version ;
  134. /* Section names. */
  135. section * darwin_sections[NUM_DARWIN_SECTIONS];
  136. /* While we transition to using in-tests instead of ifdef'd code. */
  137. #ifndef HAVE_lo_sum
  138. #define HAVE_lo_sum 0
  139. #define gen_macho_high(a,b) (a)
  140. #define gen_macho_low(a,b,c) (a)
  141. #endif
  142. /* True if we're setting __attribute__ ((ms_struct)). */
  143. int darwin_ms_struct = false;
  144. /* Earlier versions of Darwin as do not recognize an alignment field in
  145. .comm directives, this should be set for versions that allow it. */
  146. int emit_aligned_common = false;
  147. /* A get_unnamed_section callback used to switch to an ObjC section.
  148. DIRECTIVE is as for output_section_asm_op. */
  149. static void
  150. output_objc_section_asm_op (const void *directive)
  151. {
  152. static bool been_here = false;
  153. /* The NeXT ObjC Runtime requires these sections to be present and in
  154. order in the object. The code below implements this by emitting
  155. a section header for each ObjC section the first time that an ObjC
  156. section is requested. */
  157. if (! been_here)
  158. {
  159. section *saved_in_section = in_section;
  160. static const enum darwin_section_enum tomark[] =
  161. {
  162. /* written, cold -> hot */
  163. objc_cat_cls_meth_section,
  164. objc_cat_inst_meth_section,
  165. objc_string_object_section,
  166. objc_constant_string_object_section,
  167. objc_selector_refs_section,
  168. objc_selector_fixup_section,
  169. objc_cls_refs_section,
  170. objc_class_section,
  171. objc_meta_class_section,
  172. /* shared, hot -> cold */
  173. objc_cls_meth_section,
  174. objc_inst_meth_section,
  175. objc_protocol_section,
  176. objc_class_names_section,
  177. objc_meth_var_types_section,
  178. objc_meth_var_names_section,
  179. objc_category_section,
  180. objc_class_vars_section,
  181. objc_instance_vars_section,
  182. objc_module_info_section,
  183. objc_symbols_section,
  184. };
  185. /* ABI=1 */
  186. static const enum darwin_section_enum tomarkv1[] =
  187. {
  188. objc1_protocol_ext_section,
  189. objc1_class_ext_section,
  190. objc1_prop_list_section
  191. } ;
  192. /* ABI=2 */
  193. static const enum darwin_section_enum tomarkv2[] =
  194. {
  195. objc2_message_refs_section,
  196. objc2_classdefs_section,
  197. objc2_metadata_section,
  198. objc2_classrefs_section,
  199. objc2_classlist_section,
  200. objc2_categorylist_section,
  201. objc2_selector_refs_section,
  202. objc2_nonlazy_class_section,
  203. objc2_nonlazy_category_section,
  204. objc2_protocollist_section,
  205. objc2_protocolrefs_section,
  206. objc2_super_classrefs_section,
  207. objc2_image_info_section,
  208. objc2_constant_string_object_section
  209. } ;
  210. size_t i;
  211. been_here = true;
  212. if (flag_objc_abi < 2)
  213. {
  214. for (i = 0; i < ARRAY_SIZE (tomark); i++)
  215. switch_to_section (darwin_sections[tomark[i]]);
  216. if (flag_objc_abi == 1)
  217. for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
  218. switch_to_section (darwin_sections[tomarkv1[i]]);
  219. }
  220. else
  221. for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
  222. switch_to_section (darwin_sections[tomarkv2[i]]);
  223. /* Make sure we don't get varasm.c out of sync with us. */
  224. switch_to_section (saved_in_section);
  225. }
  226. output_section_asm_op (directive);
  227. }
  228. /* Private flag applied to disable section-anchors in a particular section. */
  229. #define SECTION_NO_ANCHOR SECTION_MACH_DEP
  230. /* Implement TARGET_ASM_INIT_SECTIONS. */
  231. void
  232. darwin_init_sections (void)
  233. {
  234. #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
  235. darwin_sections[NAME] = \
  236. get_unnamed_section (FLAGS, (OBJC \
  237. ? output_objc_section_asm_op \
  238. : output_section_asm_op), \
  239. "\t" DIRECTIVE);
  240. #include "config/darwin-sections.def"
  241. #undef DEF_SECTION
  242. readonly_data_section = darwin_sections[const_section];
  243. exception_section = darwin_sections[darwin_exception_section];
  244. eh_frame_section = darwin_sections[darwin_eh_frame_section];
  245. }
  246. int
  247. name_needs_quotes (const char *name)
  248. {
  249. int c;
  250. while ((c = *name++) != '\0')
  251. if (! ISIDNUM (c)
  252. && c != '.' && c != '$' && c != '_' )
  253. return 1;
  254. return 0;
  255. }
  256. /* Return true if SYM_REF can be used without an indirection. */
  257. int
  258. machopic_symbol_defined_p (rtx sym_ref)
  259. {
  260. if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
  261. return true;
  262. /* If a symbol references local and is not an extern to this
  263. file, then the symbol might be able to declared as defined. */
  264. if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
  265. {
  266. /* If the symbol references a variable and the variable is a
  267. common symbol, then this symbol is not defined. */
  268. if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
  269. {
  270. tree decl = SYMBOL_REF_DECL (sym_ref);
  271. if (!decl)
  272. return true;
  273. if (DECL_COMMON (decl))
  274. return false;
  275. }
  276. return true;
  277. }
  278. return false;
  279. }
  280. /* This module assumes that (const (symbol_ref "foo")) is a legal pic
  281. reference, which will not be changed. */
  282. enum machopic_addr_class
  283. machopic_classify_symbol (rtx sym_ref)
  284. {
  285. bool function_p;
  286. function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
  287. if (machopic_symbol_defined_p (sym_ref))
  288. return (function_p
  289. ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
  290. else
  291. return (function_p
  292. ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
  293. }
  294. #ifndef TARGET_FIX_AND_CONTINUE
  295. #define TARGET_FIX_AND_CONTINUE 0
  296. #endif
  297. /* Indicate when fix-and-continue style code generation is being used
  298. and when a reference to data should be indirected so that it can be
  299. rebound in a new translation unit to reference the original instance
  300. of that data. Symbol names that are for code generation local to
  301. the translation unit are bound to the new translation unit;
  302. currently this means symbols that begin with L or _OBJC_;
  303. otherwise, we indicate that an indirect reference should be made to
  304. permit the runtime to rebind new instances of the translation unit
  305. to the original instance of the data. */
  306. static int
  307. indirect_data (rtx sym_ref)
  308. {
  309. int lprefix;
  310. const char *name;
  311. /* If we aren't generating fix-and-continue code, don't do anything
  312. special. */
  313. if (TARGET_FIX_AND_CONTINUE == 0)
  314. return 0;
  315. /* Otherwise, all symbol except symbols that begin with L or _OBJC_
  316. are indirected. Symbols that begin with L and _OBJC_ are always
  317. bound to the current translation unit as they are used for
  318. generated local data of the translation unit. */
  319. name = XSTR (sym_ref, 0);
  320. lprefix = (((name[0] == '*' || name[0] == '&')
  321. && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
  322. || (strncmp (name, "_OBJC_", 6) == 0));
  323. return ! lprefix;
  324. }
  325. static int
  326. machopic_data_defined_p (rtx sym_ref)
  327. {
  328. if (indirect_data (sym_ref))
  329. return 0;
  330. switch (machopic_classify_symbol (sym_ref))
  331. {
  332. case MACHOPIC_DEFINED_DATA:
  333. case MACHOPIC_DEFINED_FUNCTION:
  334. return 1;
  335. default:
  336. return 0;
  337. }
  338. }
  339. void
  340. machopic_define_symbol (rtx mem)
  341. {
  342. rtx sym_ref;
  343. gcc_assert (GET_CODE (mem) == MEM);
  344. sym_ref = XEXP (mem, 0);
  345. SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
  346. }
  347. /* Return either ORIG or:
  348. (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
  349. depending on MACHO_DYNAMIC_NO_PIC_P. */
  350. rtx
  351. machopic_gen_offset (rtx orig)
  352. {
  353. if (MACHO_DYNAMIC_NO_PIC_P)
  354. return orig;
  355. else
  356. {
  357. /* Play games to avoid marking the function as needing pic if we
  358. are being called as part of the cost-estimation process. */
  359. if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
  360. crtl->uses_pic_offset_table = 1;
  361. orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
  362. UNSPEC_MACHOPIC_OFFSET);
  363. return gen_rtx_CONST (Pmode, orig);
  364. }
  365. }
  366. static GTY(()) const char * function_base_func_name;
  367. static GTY(()) int current_pic_label_num;
  368. static GTY(()) int emitted_pic_label_num;
  369. static void
  370. update_pic_label_number_if_needed (void)
  371. {
  372. const char *current_name;
  373. /* When we are generating _get_pc thunks within stubs, there is no current
  374. function. */
  375. if (current_function_decl)
  376. {
  377. current_name =
  378. IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
  379. if (function_base_func_name != current_name)
  380. {
  381. ++current_pic_label_num;
  382. function_base_func_name = current_name;
  383. }
  384. }
  385. else
  386. {
  387. ++current_pic_label_num;
  388. function_base_func_name = "L_machopic_stub_dummy";
  389. }
  390. }
  391. void
  392. machopic_output_function_base_name (FILE *file)
  393. {
  394. /* If dynamic-no-pic is on, we should not get here. */
  395. gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
  396. update_pic_label_number_if_needed ();
  397. fprintf (file, "L%d$pb", current_pic_label_num);
  398. }
  399. char curr_picbasename[32];
  400. const char *
  401. machopic_get_function_picbase (void)
  402. {
  403. /* If dynamic-no-pic is on, we should not get here. */
  404. gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
  405. update_pic_label_number_if_needed ();
  406. snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
  407. return (const char *) curr_picbasename;
  408. }
  409. bool
  410. machopic_should_output_picbase_label (void)
  411. {
  412. update_pic_label_number_if_needed ();
  413. if (current_pic_label_num == emitted_pic_label_num)
  414. return false;
  415. emitted_pic_label_num = current_pic_label_num;
  416. return true;
  417. }
  418. /* The suffix attached to non-lazy pointer symbols. */
  419. #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
  420. /* The suffix attached to stub symbols. */
  421. #define STUB_SUFFIX "$stub"
  422. typedef struct GTY ((for_user)) machopic_indirection
  423. {
  424. /* The SYMBOL_REF for the entity referenced. */
  425. rtx symbol;
  426. /* The name of the stub or non-lazy pointer. */
  427. const char * ptr_name;
  428. /* True iff this entry is for a stub (as opposed to a non-lazy
  429. pointer). */
  430. bool stub_p;
  431. /* True iff this stub or pointer pointer has been referenced. */
  432. bool used;
  433. } machopic_indirection;
  434. struct indirection_hasher : ggc_hasher<machopic_indirection *>
  435. {
  436. typedef const char *compare_type;
  437. static hashval_t hash (machopic_indirection *);
  438. static bool equal (machopic_indirection *, const char *);
  439. };
  440. /* A table mapping stub names and non-lazy pointer names to
  441. SYMBOL_REFs for the stubbed-to and pointed-to entities. */
  442. static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
  443. /* Return a hash value for a SLOT in the indirections hash table. */
  444. hashval_t
  445. indirection_hasher::hash (machopic_indirection *p)
  446. {
  447. return htab_hash_string (p->ptr_name);
  448. }
  449. /* Returns true if the KEY is the same as that associated with
  450. SLOT. */
  451. bool
  452. indirection_hasher::equal (machopic_indirection *s, const char *k)
  453. {
  454. return strcmp (s->ptr_name, k) == 0;
  455. }
  456. /* Return the name of the non-lazy pointer (if STUB_P is false) or
  457. stub (if STUB_B is true) corresponding to the given name. */
  458. const char *
  459. machopic_indirection_name (rtx sym_ref, bool stub_p)
  460. {
  461. char *buffer;
  462. const char *name = XSTR (sym_ref, 0);
  463. size_t namelen = strlen (name);
  464. machopic_indirection *p;
  465. bool needs_quotes;
  466. const char *suffix;
  467. const char *prefix = user_label_prefix;
  468. const char *quote = "";
  469. tree id;
  470. id = maybe_get_identifier (name);
  471. if (id)
  472. {
  473. tree id_orig = id;
  474. while (IDENTIFIER_TRANSPARENT_ALIAS (id))
  475. id = TREE_CHAIN (id);
  476. if (id != id_orig)
  477. {
  478. name = IDENTIFIER_POINTER (id);
  479. namelen = strlen (name);
  480. }
  481. }
  482. if (name[0] == '*')
  483. {
  484. prefix = "";
  485. ++name;
  486. --namelen;
  487. }
  488. needs_quotes = name_needs_quotes (name);
  489. if (needs_quotes)
  490. {
  491. quote = "\"";
  492. }
  493. if (stub_p)
  494. suffix = STUB_SUFFIX;
  495. else
  496. suffix = NON_LAZY_POINTER_SUFFIX;
  497. buffer = XALLOCAVEC (char, strlen ("&L")
  498. + strlen (prefix)
  499. + namelen
  500. + strlen (suffix)
  501. + 2 * strlen (quote)
  502. + 1 /* '\0' */);
  503. /* Construct the name of the non-lazy pointer or stub. */
  504. sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
  505. if (!machopic_indirections)
  506. machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
  507. machopic_indirection **slot
  508. = machopic_indirections->find_slot_with_hash (buffer,
  509. htab_hash_string (buffer),
  510. INSERT);
  511. if (*slot)
  512. {
  513. p = *slot;
  514. }
  515. else
  516. {
  517. p = ggc_alloc<machopic_indirection> ();
  518. p->symbol = sym_ref;
  519. p->ptr_name = xstrdup (buffer);
  520. p->stub_p = stub_p;
  521. p->used = false;
  522. *slot = p;
  523. }
  524. return p->ptr_name;
  525. }
  526. /* Return the name of the stub for the mcount function. */
  527. const char*
  528. machopic_mcount_stub_name (void)
  529. {
  530. rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
  531. return machopic_indirection_name (symbol, /*stub_p=*/true);
  532. }
  533. /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
  534. or non-lazy pointer as used -- and mark the object to which the
  535. pointer/stub refers as used as well, since the pointer/stub will
  536. emit a reference to it. */
  537. void
  538. machopic_validate_stub_or_non_lazy_ptr (const char *name)
  539. {
  540. machopic_indirection *p
  541. = machopic_indirections->find_with_hash (name, htab_hash_string (name));
  542. if (p && ! p->used)
  543. {
  544. const char *real_name;
  545. tree id;
  546. p->used = true;
  547. /* Do what output_addr_const will do when we actually call it. */
  548. if (SYMBOL_REF_DECL (p->symbol))
  549. mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
  550. real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
  551. id = maybe_get_identifier (real_name);
  552. if (id)
  553. mark_referenced (id);
  554. }
  555. }
  556. /* Transform ORIG, which may be any data source, to the corresponding
  557. source using indirections. */
  558. rtx
  559. machopic_indirect_data_reference (rtx orig, rtx reg)
  560. {
  561. rtx ptr_ref = orig;
  562. if (! MACHOPIC_INDIRECT)
  563. return orig;
  564. if (GET_CODE (orig) == SYMBOL_REF)
  565. {
  566. int defined = machopic_data_defined_p (orig);
  567. if (defined && MACHO_DYNAMIC_NO_PIC_P)
  568. {
  569. if (DARWIN_PPC)
  570. {
  571. /* Create a new register for CSE opportunities. */
  572. rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
  573. emit_insn (gen_macho_high (hi_reg, orig));
  574. emit_insn (gen_macho_low (reg, hi_reg, orig));
  575. return reg;
  576. }
  577. else if (DARWIN_X86)
  578. return orig;
  579. else
  580. /* some other cpu -- writeme! */
  581. gcc_unreachable ();
  582. }
  583. else if (defined)
  584. {
  585. rtx offset = NULL;
  586. if (DARWIN_PPC || HAVE_lo_sum)
  587. offset = machopic_gen_offset (orig);
  588. if (DARWIN_PPC)
  589. {
  590. rtx hi_sum_reg = (!can_create_pseudo_p ()
  591. ? reg
  592. : gen_reg_rtx (Pmode));
  593. gcc_assert (reg);
  594. emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
  595. gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
  596. gen_rtx_HIGH (Pmode, offset))));
  597. emit_insn (gen_rtx_SET (Pmode, reg,
  598. gen_rtx_LO_SUM (Pmode, hi_sum_reg,
  599. copy_rtx (offset))));
  600. orig = reg;
  601. }
  602. else if (HAVE_lo_sum)
  603. {
  604. gcc_assert (reg);
  605. emit_insn (gen_rtx_SET (VOIDmode, reg,
  606. gen_rtx_HIGH (Pmode, offset)));
  607. emit_insn (gen_rtx_SET (VOIDmode, reg,
  608. gen_rtx_LO_SUM (Pmode, reg,
  609. copy_rtx (offset))));
  610. emit_use (pic_offset_table_rtx);
  611. orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
  612. }
  613. return orig;
  614. }
  615. ptr_ref = (gen_rtx_SYMBOL_REF
  616. (Pmode,
  617. machopic_indirection_name (orig, /*stub_p=*/false)));
  618. SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
  619. ptr_ref = gen_const_mem (Pmode, ptr_ref);
  620. machopic_define_symbol (ptr_ref);
  621. if (DARWIN_X86
  622. && reg
  623. && MACHO_DYNAMIC_NO_PIC_P)
  624. {
  625. emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
  626. ptr_ref = reg;
  627. }
  628. return ptr_ref;
  629. }
  630. else if (GET_CODE (orig) == CONST)
  631. {
  632. /* If "(const (plus ...", walk the PLUS and return that result.
  633. PLUS processing (below) will restore the "(const ..." if
  634. appropriate. */
  635. if (GET_CODE (XEXP (orig, 0)) == PLUS)
  636. return machopic_indirect_data_reference (XEXP (orig, 0), reg);
  637. else
  638. return orig;
  639. }
  640. else if (GET_CODE (orig) == MEM)
  641. {
  642. XEXP (ptr_ref, 0) =
  643. machopic_indirect_data_reference (XEXP (orig, 0), reg);
  644. return ptr_ref;
  645. }
  646. else if (GET_CODE (orig) == PLUS)
  647. {
  648. rtx base, result;
  649. /* When the target is i386, this code prevents crashes due to the
  650. compiler's ignorance on how to move the PIC base register to
  651. other registers. (The reload phase sometimes introduces such
  652. insns.) */
  653. if (GET_CODE (XEXP (orig, 0)) == REG
  654. && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
  655. /* Prevent the same register from being erroneously used
  656. as both the base and index registers. */
  657. && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
  658. && reg)
  659. {
  660. emit_move_insn (reg, XEXP (orig, 0));
  661. XEXP (ptr_ref, 0) = reg;
  662. return ptr_ref;
  663. }
  664. /* Legitimize both operands of the PLUS. */
  665. base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
  666. orig = machopic_indirect_data_reference (XEXP (orig, 1),
  667. (base == reg ? 0 : reg));
  668. if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
  669. result = plus_constant (Pmode, base, INTVAL (orig));
  670. else
  671. result = gen_rtx_PLUS (Pmode, base, orig);
  672. if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
  673. {
  674. if (reg)
  675. {
  676. emit_move_insn (reg, result);
  677. result = reg;
  678. }
  679. else
  680. {
  681. result = force_reg (GET_MODE (result), result);
  682. }
  683. }
  684. return result;
  685. }
  686. return ptr_ref;
  687. }
  688. /* Transform TARGET (a MEM), which is a function call target, to the
  689. corresponding symbol_stub if necessary. Return a new MEM. */
  690. rtx
  691. machopic_indirect_call_target (rtx target)
  692. {
  693. if (! darwin_emit_branch_islands)
  694. return target;
  695. if (GET_CODE (target) != MEM)
  696. return target;
  697. if (MACHOPIC_INDIRECT
  698. && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
  699. && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
  700. & MACHO_SYMBOL_FLAG_DEFINED))
  701. {
  702. rtx sym_ref = XEXP (target, 0);
  703. const char *stub_name = machopic_indirection_name (sym_ref,
  704. /*stub_p=*/true);
  705. machine_mode mode = GET_MODE (sym_ref);
  706. XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
  707. SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
  708. MEM_READONLY_P (target) = 1;
  709. MEM_NOTRAP_P (target) = 1;
  710. }
  711. return target;
  712. }
  713. rtx
  714. machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
  715. {
  716. rtx pic_ref = orig;
  717. if (! MACHOPIC_INDIRECT)
  718. return orig;
  719. /* First handle a simple SYMBOL_REF or LABEL_REF */
  720. if (GET_CODE (orig) == LABEL_REF
  721. || (GET_CODE (orig) == SYMBOL_REF
  722. ))
  723. {
  724. /* addr(foo) = &func+(foo-func) */
  725. orig = machopic_indirect_data_reference (orig, reg);
  726. if (GET_CODE (orig) == PLUS
  727. && GET_CODE (XEXP (orig, 0)) == REG)
  728. {
  729. if (reg == 0)
  730. return force_reg (mode, orig);
  731. emit_move_insn (reg, orig);
  732. return reg;
  733. }
  734. if (GET_CODE (orig) == MEM)
  735. {
  736. if (reg == 0)
  737. {
  738. gcc_assert (!reload_in_progress);
  739. reg = gen_reg_rtx (Pmode);
  740. }
  741. #if HAVE_lo_sum
  742. if (MACHO_DYNAMIC_NO_PIC_P
  743. && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
  744. || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
  745. {
  746. #if defined (TARGET_TOC) /* ppc */
  747. rtx temp_reg = (!can_create_pseudo_p ()
  748. ? reg :
  749. gen_reg_rtx (Pmode));
  750. rtx asym = XEXP (orig, 0);
  751. rtx mem;
  752. emit_insn (gen_macho_high (temp_reg, asym));
  753. mem = gen_const_mem (GET_MODE (orig),
  754. gen_rtx_LO_SUM (Pmode, temp_reg,
  755. copy_rtx (asym)));
  756. emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
  757. #else
  758. /* Some other CPU -- WriteMe! but right now there are no other
  759. platforms that can use dynamic-no-pic */
  760. gcc_unreachable ();
  761. #endif
  762. pic_ref = reg;
  763. }
  764. else
  765. if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
  766. || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
  767. {
  768. rtx offset = machopic_gen_offset (XEXP (orig, 0));
  769. #if defined (TARGET_TOC) /* i.e., PowerPC */
  770. /* Generating a new reg may expose opportunities for
  771. common subexpression elimination. */
  772. rtx hi_sum_reg = (!can_create_pseudo_p ()
  773. ? reg
  774. : gen_reg_rtx (Pmode));
  775. rtx mem;
  776. rtx insn;
  777. rtx sum;
  778. sum = gen_rtx_HIGH (Pmode, offset);
  779. if (! MACHO_DYNAMIC_NO_PIC_P)
  780. sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
  781. emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
  782. mem = gen_const_mem (GET_MODE (orig),
  783. gen_rtx_LO_SUM (Pmode,
  784. hi_sum_reg,
  785. copy_rtx (offset)));
  786. insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
  787. set_unique_reg_note (insn, REG_EQUAL, pic_ref);
  788. pic_ref = reg;
  789. #else
  790. emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
  791. emit_insn (gen_rtx_SET (VOIDmode, reg,
  792. gen_rtx_HIGH (Pmode,
  793. gen_rtx_CONST (Pmode,
  794. offset))));
  795. emit_insn (gen_rtx_SET (VOIDmode, reg,
  796. gen_rtx_LO_SUM (Pmode, reg,
  797. gen_rtx_CONST (Pmode,
  798. copy_rtx (offset)))));
  799. pic_ref = gen_rtx_PLUS (Pmode,
  800. pic_offset_table_rtx, reg);
  801. #endif
  802. }
  803. else
  804. #endif /* HAVE_lo_sum */
  805. {
  806. rtx pic = pic_offset_table_rtx;
  807. if (GET_CODE (pic) != REG)
  808. {
  809. emit_move_insn (reg, pic);
  810. pic = reg;
  811. }
  812. #if 0
  813. emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
  814. #endif
  815. if (reload_in_progress)
  816. df_set_regs_ever_live (REGNO (pic), true);
  817. pic_ref = gen_rtx_PLUS (Pmode, pic,
  818. machopic_gen_offset (XEXP (orig, 0)));
  819. }
  820. #if !defined (TARGET_TOC)
  821. emit_move_insn (reg, pic_ref);
  822. pic_ref = gen_const_mem (GET_MODE (orig), reg);
  823. #endif
  824. }
  825. else
  826. {
  827. #if HAVE_lo_sum
  828. if (GET_CODE (orig) == SYMBOL_REF
  829. || GET_CODE (orig) == LABEL_REF)
  830. {
  831. rtx offset = machopic_gen_offset (orig);
  832. #if defined (TARGET_TOC) /* i.e., PowerPC */
  833. rtx hi_sum_reg;
  834. if (reg == 0)
  835. {
  836. gcc_assert (!reload_in_progress);
  837. reg = gen_reg_rtx (Pmode);
  838. }
  839. hi_sum_reg = reg;
  840. emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
  841. (MACHO_DYNAMIC_NO_PIC_P)
  842. ? gen_rtx_HIGH (Pmode, offset)
  843. : gen_rtx_PLUS (Pmode,
  844. pic_offset_table_rtx,
  845. gen_rtx_HIGH (Pmode,
  846. offset))));
  847. emit_insn (gen_rtx_SET (VOIDmode, reg,
  848. gen_rtx_LO_SUM (Pmode,
  849. hi_sum_reg,
  850. copy_rtx (offset))));
  851. pic_ref = reg;
  852. #else
  853. emit_insn (gen_rtx_SET (VOIDmode, reg,
  854. gen_rtx_HIGH (Pmode, offset)));
  855. emit_insn (gen_rtx_SET (VOIDmode, reg,
  856. gen_rtx_LO_SUM (Pmode, reg,
  857. copy_rtx (offset))));
  858. pic_ref = gen_rtx_PLUS (Pmode,
  859. pic_offset_table_rtx, reg);
  860. #endif
  861. }
  862. else
  863. #endif /* HAVE_lo_sum */
  864. {
  865. if (REG_P (orig)
  866. || GET_CODE (orig) == SUBREG)
  867. {
  868. return orig;
  869. }
  870. else
  871. {
  872. rtx pic = pic_offset_table_rtx;
  873. if (GET_CODE (pic) != REG)
  874. {
  875. emit_move_insn (reg, pic);
  876. pic = reg;
  877. }
  878. #if 0
  879. emit_use (pic_offset_table_rtx);
  880. #endif
  881. if (reload_in_progress)
  882. df_set_regs_ever_live (REGNO (pic), true);
  883. pic_ref = gen_rtx_PLUS (Pmode,
  884. pic,
  885. machopic_gen_offset (orig));
  886. }
  887. }
  888. }
  889. if (GET_CODE (pic_ref) != REG)
  890. {
  891. if (reg != 0)
  892. {
  893. emit_move_insn (reg, pic_ref);
  894. return reg;
  895. }
  896. else
  897. {
  898. return force_reg (mode, pic_ref);
  899. }
  900. }
  901. else
  902. {
  903. return pic_ref;
  904. }
  905. }
  906. else if (GET_CODE (orig) == SYMBOL_REF)
  907. return orig;
  908. else if (GET_CODE (orig) == PLUS
  909. && (GET_CODE (XEXP (orig, 0)) == MEM
  910. || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
  911. || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
  912. && XEXP (orig, 0) != pic_offset_table_rtx
  913. && GET_CODE (XEXP (orig, 1)) != REG)
  914. {
  915. rtx base;
  916. int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
  917. base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
  918. orig = machopic_legitimize_pic_address (XEXP (orig, 1),
  919. Pmode, (base == reg ? 0 : reg));
  920. if (GET_CODE (orig) == CONST_INT)
  921. {
  922. pic_ref = plus_constant (Pmode, base, INTVAL (orig));
  923. is_complex = 1;
  924. }
  925. else
  926. pic_ref = gen_rtx_PLUS (Pmode, base, orig);
  927. if (reg && is_complex)
  928. {
  929. emit_move_insn (reg, pic_ref);
  930. pic_ref = reg;
  931. }
  932. /* Likewise, should we set special REG_NOTEs here? */
  933. }
  934. else if (GET_CODE (orig) == CONST)
  935. {
  936. return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
  937. }
  938. else if (GET_CODE (orig) == MEM
  939. && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
  940. {
  941. rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
  942. addr = replace_equiv_address (orig, addr);
  943. emit_move_insn (reg, addr);
  944. pic_ref = reg;
  945. }
  946. return pic_ref;
  947. }
  948. /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
  949. DATA is the FILE* for assembly output. Called from
  950. htab_traverse. */
  951. int
  952. machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
  953. {
  954. machopic_indirection *p = *slot;
  955. rtx symbol;
  956. const char *sym_name;
  957. const char *ptr_name;
  958. if (!p->used)
  959. return 1;
  960. symbol = p->symbol;
  961. sym_name = XSTR (symbol, 0);
  962. ptr_name = p->ptr_name;
  963. if (p->stub_p)
  964. {
  965. char *sym;
  966. char *stub;
  967. tree id;
  968. id = maybe_get_identifier (sym_name);
  969. if (id)
  970. {
  971. tree id_orig = id;
  972. while (IDENTIFIER_TRANSPARENT_ALIAS (id))
  973. id = TREE_CHAIN (id);
  974. if (id != id_orig)
  975. sym_name = IDENTIFIER_POINTER (id);
  976. }
  977. sym = XALLOCAVEC (char, strlen (sym_name) + 2);
  978. if (sym_name[0] == '*' || sym_name[0] == '&')
  979. strcpy (sym, sym_name + 1);
  980. else if (sym_name[0] == '-' || sym_name[0] == '+')
  981. strcpy (sym, sym_name);
  982. else
  983. sprintf (sym, "%s%s", user_label_prefix, sym_name);
  984. stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
  985. if (ptr_name[0] == '*' || ptr_name[0] == '&')
  986. strcpy (stub, ptr_name + 1);
  987. else
  988. sprintf (stub, "%s%s", user_label_prefix, ptr_name);
  989. machopic_output_stub (asm_out_file, sym, stub);
  990. }
  991. else if (! indirect_data (symbol)
  992. && (machopic_symbol_defined_p (symbol)
  993. || SYMBOL_REF_LOCAL_P (symbol)))
  994. {
  995. switch_to_section (data_section);
  996. assemble_align (GET_MODE_ALIGNMENT (Pmode));
  997. assemble_label (asm_out_file, ptr_name);
  998. assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
  999. GET_MODE_SIZE (Pmode),
  1000. GET_MODE_ALIGNMENT (Pmode), 1);
  1001. }
  1002. else
  1003. {
  1004. rtx init = const0_rtx;
  1005. switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
  1006. /* Mach-O symbols are passed around in code through indirect
  1007. references and the original symbol_ref hasn't passed through
  1008. the generic handling and reference-catching in
  1009. output_operand, so we need to manually mark weak references
  1010. as such. */
  1011. if (SYMBOL_REF_WEAK (symbol))
  1012. {
  1013. tree decl = SYMBOL_REF_DECL (symbol);
  1014. gcc_assert (DECL_P (decl));
  1015. if (decl != NULL_TREE
  1016. && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
  1017. /* Handle only actual external-only definitions, not
  1018. e.g. extern inline code or variables for which
  1019. storage has been allocated. */
  1020. && !TREE_STATIC (decl))
  1021. {
  1022. fputs ("\t.weak_reference ", asm_out_file);
  1023. assemble_name (asm_out_file, sym_name);
  1024. fputc ('\n', asm_out_file);
  1025. }
  1026. }
  1027. assemble_name (asm_out_file, ptr_name);
  1028. fprintf (asm_out_file, ":\n");
  1029. fprintf (asm_out_file, "\t.indirect_symbol ");
  1030. assemble_name (asm_out_file, sym_name);
  1031. fprintf (asm_out_file, "\n");
  1032. /* Variables that are marked with MACHO_SYMBOL_STATIC need to
  1033. have their symbol name instead of 0 in the second entry of
  1034. the non-lazy symbol pointer data structure when they are
  1035. defined. This allows the runtime to rebind newer instances
  1036. of the translation unit with the original instance of the
  1037. symbol. */
  1038. if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
  1039. && machopic_symbol_defined_p (symbol))
  1040. init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
  1041. assemble_integer (init, GET_MODE_SIZE (Pmode),
  1042. GET_MODE_ALIGNMENT (Pmode), 1);
  1043. }
  1044. return 1;
  1045. }
  1046. void
  1047. machopic_finish (FILE *asm_out_file)
  1048. {
  1049. if (machopic_indirections)
  1050. machopic_indirections
  1051. ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
  1052. }
  1053. int
  1054. machopic_operand_p (rtx op)
  1055. {
  1056. if (MACHOPIC_JUST_INDIRECT)
  1057. return (GET_CODE (op) == SYMBOL_REF
  1058. && machopic_symbol_defined_p (op));
  1059. else
  1060. return (GET_CODE (op) == CONST
  1061. && GET_CODE (XEXP (op, 0)) == UNSPEC
  1062. && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
  1063. }
  1064. /* This function records whether a given name corresponds to a defined
  1065. or undefined function or variable, for machopic_classify_ident to
  1066. use later. */
  1067. void
  1068. darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
  1069. {
  1070. rtx sym_ref;
  1071. /* Do the standard encoding things first. */
  1072. default_encode_section_info (decl, rtl, first);
  1073. if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
  1074. return;
  1075. sym_ref = XEXP (rtl, 0);
  1076. if (TREE_CODE (decl) == VAR_DECL)
  1077. SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
  1078. if (!DECL_EXTERNAL (decl)
  1079. && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
  1080. && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
  1081. && ((TREE_STATIC (decl)
  1082. && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
  1083. || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
  1084. && DECL_INITIAL (decl) != error_mark_node)))
  1085. SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
  1086. if (! TREE_PUBLIC (decl))
  1087. SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
  1088. }
  1089. void
  1090. darwin_mark_decl_preserved (const char *name)
  1091. {
  1092. /* Actually we shouldn't mark any local symbol this way, but for now
  1093. this only happens with ObjC meta-data. */
  1094. if (darwin_label_is_anonymous_local_objc_name (name))
  1095. return;
  1096. fprintf (asm_out_file, "\t.no_dead_strip ");
  1097. assemble_name (asm_out_file, name);
  1098. fputc ('\n', asm_out_file);
  1099. }
  1100. static section *
  1101. darwin_rodata_section (int weak, bool zsize)
  1102. {
  1103. return (weak
  1104. ? darwin_sections[const_coal_section]
  1105. : (zsize ? darwin_sections[zobj_const_section]
  1106. : darwin_sections[const_section]));
  1107. }
  1108. static section *
  1109. darwin_mergeable_string_section (tree exp,
  1110. unsigned HOST_WIDE_INT align)
  1111. {
  1112. /* Darwin's ld expects to see non-writable string literals in the .cstring
  1113. section. Later versions of ld check and complain when CFStrings are
  1114. enabled. Therefore we shall force the strings into .cstring since we
  1115. don't support writable ones anyway. */
  1116. if ((darwin_constant_cfstrings || flag_merge_constants)
  1117. && TREE_CODE (exp) == STRING_CST
  1118. && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
  1119. && align <= 256
  1120. && (int_size_in_bytes (TREE_TYPE (exp))
  1121. == TREE_STRING_LENGTH (exp))
  1122. && ((size_t) TREE_STRING_LENGTH (exp)
  1123. == strlen (TREE_STRING_POINTER (exp)) + 1))
  1124. return darwin_sections[cstring_section];
  1125. if (DARWIN_SECTION_ANCHORS && flag_section_anchors
  1126. && TREE_CODE (exp) == STRING_CST
  1127. && TREE_STRING_LENGTH (exp) == 0)
  1128. return darwin_sections[zobj_const_section];
  1129. return readonly_data_section;
  1130. }
  1131. #ifndef HAVE_GAS_LITERAL16
  1132. #define HAVE_GAS_LITERAL16 0
  1133. #endif
  1134. static section *
  1135. darwin_mergeable_constant_section (tree exp,
  1136. unsigned HOST_WIDE_INT align,
  1137. bool zsize)
  1138. {
  1139. machine_mode mode = DECL_MODE (exp);
  1140. unsigned int modesize = GET_MODE_BITSIZE (mode);
  1141. if (DARWIN_SECTION_ANCHORS
  1142. && flag_section_anchors
  1143. && zsize)
  1144. return darwin_sections[zobj_const_section];
  1145. if (flag_merge_constants
  1146. && mode != VOIDmode
  1147. && mode != BLKmode
  1148. && modesize <= align
  1149. && align >= 8
  1150. && align <= 256
  1151. && (align & (align -1)) == 0)
  1152. {
  1153. tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
  1154. if (TREE_CODE (size) == INTEGER_CST)
  1155. {
  1156. if (wi::eq_p (size, 4))
  1157. return darwin_sections[literal4_section];
  1158. else if (wi::eq_p (size, 8))
  1159. return darwin_sections[literal8_section];
  1160. else if (HAVE_GAS_LITERAL16
  1161. && TARGET_64BIT
  1162. && wi::eq_p (size, 16))
  1163. return darwin_sections[literal16_section];
  1164. }
  1165. }
  1166. return readonly_data_section;
  1167. }
  1168. section *
  1169. darwin_tm_clone_table_section (void)
  1170. {
  1171. return get_named_section (NULL,
  1172. "__DATA,__tm_clone_table,regular,no_dead_strip",
  1173. 3);
  1174. }
  1175. int
  1176. machopic_reloc_rw_mask (void)
  1177. {
  1178. return MACHOPIC_INDIRECT ? 3 : 0;
  1179. }
  1180. /* We have to deal with ObjC/C++ metadata section placement in the common
  1181. code, since it will also be called from LTO.
  1182. Return metadata attributes, if present (searching for ABI=2 first)
  1183. Return NULL_TREE if no such attributes are found. */
  1184. static tree
  1185. is_objc_metadata (tree decl)
  1186. {
  1187. if (DECL_P (decl)
  1188. && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
  1189. && DECL_ATTRIBUTES (decl))
  1190. {
  1191. tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
  1192. if (meta)
  1193. return meta;
  1194. meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
  1195. if (meta)
  1196. return meta;
  1197. }
  1198. return NULL_TREE;
  1199. }
  1200. static int classes_seen;
  1201. static int objc_metadata_seen;
  1202. /* Return the section required for Objective C ABI 2 metadata. */
  1203. static section *
  1204. darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
  1205. {
  1206. const char *p;
  1207. tree ident = TREE_VALUE (meta);
  1208. gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
  1209. p = IDENTIFIER_POINTER (ident);
  1210. gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
  1211. objc_metadata_seen = 1;
  1212. if (base == data_section)
  1213. base = darwin_sections[objc2_metadata_section];
  1214. /* Most of the OBJC2 META-data end up in the base section, so check it
  1215. first. */
  1216. if (!strncmp (p, "V2_BASE", 7))
  1217. return base;
  1218. else if (!strncmp (p, "V2_STRG", 7))
  1219. return darwin_sections[cstring_section];
  1220. else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
  1221. return darwin_sections[objc2_classdefs_section];
  1222. else if (!strncmp (p, "V2_MREF", 7))
  1223. return darwin_sections[objc2_message_refs_section];
  1224. else if (!strncmp (p, "V2_CLRF", 7))
  1225. return darwin_sections[objc2_classrefs_section];
  1226. else if (!strncmp (p, "V2_SURF", 7))
  1227. return darwin_sections[objc2_super_classrefs_section];
  1228. else if (!strncmp (p, "V2_NLCL", 7))
  1229. return darwin_sections[objc2_nonlazy_class_section];
  1230. else if (!strncmp (p, "V2_CLAB", 7))
  1231. {
  1232. classes_seen = 1;
  1233. return darwin_sections[objc2_classlist_section];
  1234. }
  1235. else if (!strncmp (p, "V2_SRFS", 7))
  1236. return darwin_sections[objc2_selector_refs_section];
  1237. else if (!strncmp (p, "V2_NLCA", 7))
  1238. return darwin_sections[objc2_nonlazy_category_section];
  1239. else if (!strncmp (p, "V2_CALA", 7))
  1240. return darwin_sections[objc2_categorylist_section];
  1241. else if (!strncmp (p, "V2_PLST", 7))
  1242. return darwin_sections[objc2_protocollist_section];
  1243. else if (!strncmp (p, "V2_PRFS", 7))
  1244. return darwin_sections[objc2_protocolrefs_section];
  1245. else if (!strncmp (p, "V2_INFO", 7))
  1246. return darwin_sections[objc2_image_info_section];
  1247. else if (!strncmp (p, "V2_EHTY", 7))
  1248. return darwin_sections[data_coal_section];
  1249. else if (!strncmp (p, "V2_CSTR", 7))
  1250. return darwin_sections[objc2_constant_string_object_section];
  1251. /* Not recognized, default. */
  1252. return base;
  1253. }
  1254. /* Return the section required for Objective C ABI 0/1 metadata. */
  1255. static section *
  1256. darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
  1257. {
  1258. const char *p;
  1259. tree ident = TREE_VALUE (meta);
  1260. gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
  1261. p = IDENTIFIER_POINTER (ident);
  1262. gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
  1263. objc_metadata_seen = 1;
  1264. /* String sections first, cos there are lots of strings. */
  1265. if (!strncmp (p, "V1_STRG", 7))
  1266. return darwin_sections[cstring_section];
  1267. else if (!strncmp (p, "V1_CLSN", 7))
  1268. return darwin_sections[objc_class_names_section];
  1269. else if (!strncmp (p, "V1_METN", 7))
  1270. return darwin_sections[objc_meth_var_names_section];
  1271. else if (!strncmp (p, "V1_METT", 7))
  1272. return darwin_sections[objc_meth_var_types_section];
  1273. else if (!strncmp (p, "V1_CLAS", 7))
  1274. {
  1275. classes_seen = 1;
  1276. return darwin_sections[objc_class_section];
  1277. }
  1278. else if (!strncmp (p, "V1_META", 7))
  1279. return darwin_sections[objc_meta_class_section];
  1280. else if (!strncmp (p, "V1_CATG", 7))
  1281. return darwin_sections[objc_category_section];
  1282. else if (!strncmp (p, "V1_PROT", 7))
  1283. return darwin_sections[objc_protocol_section];
  1284. else if (!strncmp (p, "V1_CLCV", 7))
  1285. return darwin_sections[objc_class_vars_section];
  1286. else if (!strncmp (p, "V1_CLIV", 7))
  1287. return darwin_sections[objc_instance_vars_section];
  1288. else if (!strncmp (p, "V1_CLCM", 7))
  1289. return darwin_sections[objc_cls_meth_section];
  1290. else if (!strncmp (p, "V1_CLIM", 7))
  1291. return darwin_sections[objc_inst_meth_section];
  1292. else if (!strncmp (p, "V1_CACM", 7))
  1293. return darwin_sections[objc_cat_cls_meth_section];
  1294. else if (!strncmp (p, "V1_CAIM", 7))
  1295. return darwin_sections[objc_cat_inst_meth_section];
  1296. else if (!strncmp (p, "V1_PNSM", 7))
  1297. return darwin_sections[objc_cat_inst_meth_section];
  1298. else if (!strncmp (p, "V1_PCLM", 7))
  1299. return darwin_sections[objc_cat_cls_meth_section];
  1300. else if (!strncmp (p, "V1_CLPR", 7))
  1301. return darwin_sections[objc_cat_cls_meth_section];
  1302. else if (!strncmp (p, "V1_CAPR", 7))
  1303. return darwin_sections[objc_category_section]; /* ??? CHECK me. */
  1304. else if (!strncmp (p, "V1_PRFS", 7))
  1305. return darwin_sections[objc_cat_cls_meth_section];
  1306. else if (!strncmp (p, "V1_CLRF", 7))
  1307. return darwin_sections[objc_cls_refs_section];
  1308. else if (!strncmp (p, "V1_SRFS", 7))
  1309. return darwin_sections[objc_selector_refs_section];
  1310. else if (!strncmp (p, "V1_MODU", 7))
  1311. return darwin_sections[objc_module_info_section];
  1312. else if (!strncmp (p, "V1_SYMT", 7))
  1313. return darwin_sections[objc_symbols_section];
  1314. else if (!strncmp (p, "V1_INFO", 7))
  1315. return darwin_sections[objc_image_info_section];
  1316. else if (!strncmp (p, "V1_PLST", 7))
  1317. return darwin_sections[objc1_prop_list_section];
  1318. else if (!strncmp (p, "V1_PEXT", 7))
  1319. return darwin_sections[objc1_protocol_ext_section];
  1320. else if (!strncmp (p, "V1_CEXT", 7))
  1321. return darwin_sections[objc1_class_ext_section];
  1322. else if (!strncmp (p, "V2_CSTR", 7))
  1323. return darwin_sections[objc_constant_string_object_section];
  1324. return base;
  1325. }
  1326. section *
  1327. machopic_select_section (tree decl,
  1328. int reloc,
  1329. unsigned HOST_WIDE_INT align)
  1330. {
  1331. bool zsize, one, weak, ro;
  1332. section *base_section = NULL;
  1333. weak = (DECL_P (decl)
  1334. && DECL_WEAK (decl)
  1335. && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
  1336. zsize = (DECL_P (decl)
  1337. && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
  1338. && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
  1339. one = DECL_P (decl)
  1340. && TREE_CODE (decl) == VAR_DECL
  1341. && DECL_COMDAT_GROUP (decl);
  1342. ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
  1343. switch (categorize_decl_for_section (decl, reloc))
  1344. {
  1345. case SECCAT_TEXT:
  1346. gcc_unreachable ();
  1347. break;
  1348. case SECCAT_RODATA:
  1349. case SECCAT_SRODATA:
  1350. base_section = darwin_rodata_section (weak, zsize);
  1351. break;
  1352. case SECCAT_RODATA_MERGE_STR:
  1353. base_section = darwin_mergeable_string_section (decl, align);
  1354. break;
  1355. case SECCAT_RODATA_MERGE_STR_INIT:
  1356. base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
  1357. break;
  1358. case SECCAT_RODATA_MERGE_CONST:
  1359. base_section = darwin_mergeable_constant_section (decl, align, zsize);
  1360. break;
  1361. case SECCAT_DATA:
  1362. case SECCAT_DATA_REL:
  1363. case SECCAT_DATA_REL_LOCAL:
  1364. case SECCAT_DATA_REL_RO:
  1365. case SECCAT_DATA_REL_RO_LOCAL:
  1366. case SECCAT_SDATA:
  1367. case SECCAT_TDATA:
  1368. if (weak || one)
  1369. {
  1370. if (ro)
  1371. base_section = darwin_sections[const_data_coal_section];
  1372. else
  1373. base_section = darwin_sections[data_coal_section];
  1374. }
  1375. else if (DARWIN_SECTION_ANCHORS
  1376. && flag_section_anchors
  1377. && zsize)
  1378. {
  1379. /* If we're doing section anchors, then punt zero-sized objects into
  1380. their own sections so that they don't interfere with offset
  1381. computation for the remaining vars. This does not need to be done
  1382. for stuff in mergeable sections, since these are ineligible for
  1383. anchors. */
  1384. if (ro)
  1385. base_section = darwin_sections[zobj_const_data_section];
  1386. else
  1387. base_section = darwin_sections[zobj_data_section];
  1388. }
  1389. else if (ro)
  1390. base_section = darwin_sections[const_data_section];
  1391. else
  1392. base_section = data_section;
  1393. break;
  1394. case SECCAT_BSS:
  1395. case SECCAT_SBSS:
  1396. case SECCAT_TBSS:
  1397. if (weak || one)
  1398. base_section = darwin_sections[data_coal_section];
  1399. else
  1400. {
  1401. if (!TREE_PUBLIC (decl))
  1402. base_section = lcomm_section;
  1403. else if (bss_noswitch_section)
  1404. base_section = bss_noswitch_section;
  1405. else
  1406. base_section = data_section;
  1407. }
  1408. break;
  1409. default:
  1410. gcc_unreachable ();
  1411. }
  1412. /* Darwin weird special cases.
  1413. a) OBJC Meta-data. */
  1414. if (DECL_P (decl)
  1415. && (TREE_CODE (decl) == VAR_DECL
  1416. || TREE_CODE (decl) == CONST_DECL)
  1417. && DECL_ATTRIBUTES (decl))
  1418. {
  1419. tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
  1420. if (meta)
  1421. return darwin_objc2_section (decl, meta, base_section);
  1422. meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
  1423. if (meta)
  1424. return darwin_objc1_section (decl, meta, base_section);
  1425. meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
  1426. if (meta)
  1427. return base_section; /* GNU runtime is happy with it all in one pot. */
  1428. }
  1429. /* b) Constant string objects. */
  1430. if (TREE_CODE (decl) == CONSTRUCTOR
  1431. && TREE_TYPE (decl)
  1432. && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
  1433. && TYPE_NAME (TREE_TYPE (decl)))
  1434. {
  1435. tree name = TYPE_NAME (TREE_TYPE (decl));
  1436. if (TREE_CODE (name) == TYPE_DECL)
  1437. name = DECL_NAME (name);
  1438. if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
  1439. {
  1440. if (flag_next_runtime)
  1441. {
  1442. if (flag_objc_abi == 2)
  1443. return darwin_sections[objc2_constant_string_object_section];
  1444. else
  1445. return darwin_sections[objc_constant_string_object_section];
  1446. }
  1447. else
  1448. return darwin_sections[objc_string_object_section];
  1449. }
  1450. else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
  1451. return darwin_sections[cfstring_constant_object_section];
  1452. else
  1453. return base_section;
  1454. }
  1455. /* c) legacy meta-data selection. */
  1456. else if (TREE_CODE (decl) == VAR_DECL
  1457. && DECL_NAME (decl)
  1458. && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
  1459. && IDENTIFIER_POINTER (DECL_NAME (decl))
  1460. && flag_next_runtime
  1461. && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
  1462. {
  1463. const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
  1464. static bool warned_objc_46 = false;
  1465. /* We shall assert that zero-sized objects are an error in ObjC
  1466. meta-data. */
  1467. gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
  1468. /* ??? This mechanism for determining the metadata section is
  1469. broken when LTO is in use, since the frontend that generated
  1470. the data is not identified. We will keep the capability for
  1471. the short term - in case any non-Objective-C programs are using
  1472. it to place data in specified sections. */
  1473. if (!warned_objc_46)
  1474. {
  1475. location_t loc = DECL_SOURCE_LOCATION (decl);
  1476. warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
  1477. " to select meta-data sections is deprecated at 4.6"
  1478. " and will be removed in 4.7");
  1479. warned_objc_46 = true;
  1480. }
  1481. if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
  1482. return darwin_sections[objc_cls_meth_section];
  1483. else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
  1484. return darwin_sections[objc_inst_meth_section];
  1485. else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
  1486. return darwin_sections[objc_cat_cls_meth_section];
  1487. else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
  1488. return darwin_sections[objc_cat_inst_meth_section];
  1489. else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
  1490. return darwin_sections[objc_class_vars_section];
  1491. else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
  1492. return darwin_sections[objc_instance_vars_section];
  1493. else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
  1494. return darwin_sections[objc_cat_cls_meth_section];
  1495. else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
  1496. return darwin_sections[objc_class_names_section];
  1497. else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
  1498. return darwin_sections[objc_meth_var_names_section];
  1499. else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
  1500. return darwin_sections[objc_meth_var_types_section];
  1501. else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
  1502. return darwin_sections[objc_cls_refs_section];
  1503. else if (!strncmp (name, "_OBJC_CLASS_", 12))
  1504. return darwin_sections[objc_class_section];
  1505. else if (!strncmp (name, "_OBJC_METACLASS_", 16))
  1506. return darwin_sections[objc_meta_class_section];
  1507. else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
  1508. return darwin_sections[objc_category_section];
  1509. else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
  1510. return darwin_sections[objc_selector_refs_section];
  1511. else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
  1512. return darwin_sections[objc_selector_fixup_section];
  1513. else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
  1514. return darwin_sections[objc_symbols_section];
  1515. else if (!strncmp (name, "_OBJC_MODULES", 13))
  1516. return darwin_sections[objc_module_info_section];
  1517. else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
  1518. return darwin_sections[objc_image_info_section];
  1519. else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
  1520. return darwin_sections[objc_cat_inst_meth_section];
  1521. else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
  1522. return darwin_sections[objc_cat_cls_meth_section];
  1523. else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
  1524. return darwin_sections[objc_cat_cls_meth_section];
  1525. else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
  1526. return darwin_sections[objc_protocol_section];
  1527. else
  1528. return base_section;
  1529. }
  1530. return base_section;
  1531. }
  1532. /* This can be called with address expressions as "rtx".
  1533. They must go in "const". */
  1534. section *
  1535. machopic_select_rtx_section (machine_mode mode, rtx x,
  1536. unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
  1537. {
  1538. if (GET_MODE_SIZE (mode) == 8
  1539. && (GET_CODE (x) == CONST_INT
  1540. || GET_CODE (x) == CONST_WIDE_INT
  1541. || GET_CODE (x) == CONST_DOUBLE))
  1542. return darwin_sections[literal8_section];
  1543. else if (GET_MODE_SIZE (mode) == 4
  1544. && (GET_CODE (x) == CONST_INT
  1545. || GET_CODE (x) == CONST_WIDE_INT
  1546. || GET_CODE (x) == CONST_DOUBLE))
  1547. return darwin_sections[literal4_section];
  1548. else if (HAVE_GAS_LITERAL16
  1549. && TARGET_64BIT
  1550. && GET_MODE_SIZE (mode) == 16
  1551. && (GET_CODE (x) == CONST_INT
  1552. || GET_CODE (x) == CONST_WIDE_INT
  1553. || GET_CODE (x) == CONST_DOUBLE
  1554. || GET_CODE (x) == CONST_VECTOR))
  1555. return darwin_sections[literal16_section];
  1556. else if (MACHOPIC_INDIRECT
  1557. && (GET_CODE (x) == SYMBOL_REF
  1558. || GET_CODE (x) == CONST
  1559. || GET_CODE (x) == LABEL_REF))
  1560. return darwin_sections[const_data_section];
  1561. else
  1562. return darwin_sections[const_section];
  1563. }
  1564. void
  1565. machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
  1566. {
  1567. cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
  1568. vec_safe_push (ctors, new_elt);
  1569. if (! MACHOPIC_INDIRECT)
  1570. fprintf (asm_out_file, ".reference .constructors_used\n");
  1571. }
  1572. void
  1573. machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
  1574. {
  1575. cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
  1576. vec_safe_push (dtors, new_elt);
  1577. if (! MACHOPIC_INDIRECT)
  1578. fprintf (asm_out_file, ".reference .destructors_used\n");
  1579. }
  1580. static int
  1581. sort_cdtor_records (const void * a, const void * b)
  1582. {
  1583. const cdtor_record *cda = (const cdtor_record *)a;
  1584. const cdtor_record *cdb = (const cdtor_record *)b;
  1585. if (cda->priority > cdb->priority)
  1586. return 1;
  1587. if (cda->priority < cdb->priority)
  1588. return -1;
  1589. if (cda->position > cdb->position)
  1590. return 1;
  1591. if (cda->position < cdb->position)
  1592. return -1;
  1593. return 0;
  1594. }
  1595. static void
  1596. finalize_ctors ()
  1597. {
  1598. unsigned int i;
  1599. cdtor_record *elt;
  1600. if (MACHOPIC_INDIRECT)
  1601. switch_to_section (darwin_sections[mod_init_section]);
  1602. else
  1603. switch_to_section (darwin_sections[constructor_section]);
  1604. if (vec_safe_length (ctors) > 1)
  1605. ctors->qsort (sort_cdtor_records);
  1606. FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
  1607. {
  1608. assemble_align (POINTER_SIZE);
  1609. assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
  1610. }
  1611. }
  1612. static void
  1613. finalize_dtors ()
  1614. {
  1615. unsigned int i;
  1616. cdtor_record *elt;
  1617. if (MACHOPIC_INDIRECT)
  1618. switch_to_section (darwin_sections[mod_term_section]);
  1619. else
  1620. switch_to_section (darwin_sections[destructor_section]);
  1621. if (vec_safe_length (dtors) > 1)
  1622. dtors->qsort (sort_cdtor_records);
  1623. FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
  1624. {
  1625. assemble_align (POINTER_SIZE);
  1626. assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
  1627. }
  1628. }
  1629. void
  1630. darwin_globalize_label (FILE *stream, const char *name)
  1631. {
  1632. if (!!strncmp (name, "_OBJC_", 6))
  1633. default_globalize_label (stream, name);
  1634. }
  1635. /* This routine returns non-zero if 'name' starts with the special objective-c
  1636. anonymous file-scope static name. It accommodates c++'s mangling of such
  1637. symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
  1638. int
  1639. darwin_label_is_anonymous_local_objc_name (const char *name)
  1640. {
  1641. const unsigned char *p = (const unsigned char *) name;
  1642. if (*p != '_')
  1643. return 0;
  1644. if (p[1] == 'Z' && p[2] == 'L')
  1645. {
  1646. p += 3;
  1647. while (*p >= '0' && *p <= '9')
  1648. p++;
  1649. }
  1650. return (!strncmp ((const char *)p, "_OBJC_", 6));
  1651. }
  1652. /* LTO support for Mach-O.
  1653. This version uses three mach-o sections to encapsulate the (unlimited
  1654. number of) lto sections.
  1655. __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
  1656. __GNU_LTO, __section_names contains the GNU LTO section names.
  1657. __GNU_LTO, __section_index contains an array of values that index these.
  1658. Indexed thus:
  1659. <section offset from the start of __GNU_LTO, __lto_sections>,
  1660. <section length>
  1661. <name offset from the start of __GNU_LTO, __section_names,
  1662. <name length>.
  1663. At present, for both m32 and m64 mach-o files each of these fields is
  1664. represented by a uint32_t. This is because, AFAICT, a mach-o object
  1665. cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
  1666. uint32_t offset;
  1667. "offset An integer specifying the offset to this section in the file." */
  1668. /* Count lto section numbers. */
  1669. static unsigned int lto_section_num = 0;
  1670. /* A vector of information about LTO sections, at present, we only have
  1671. the name. TODO: see if we can get the data length somehow. */
  1672. typedef struct GTY (()) darwin_lto_section_e {
  1673. const char *sectname;
  1674. } darwin_lto_section_e ;
  1675. static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
  1676. /* Section wrapper scheme (used here to wrap the unlimited number of LTO
  1677. sections into three Mach-O ones).
  1678. NOTE: These names MUST be kept in sync with those in
  1679. libiberty/simple-object-mach-o. */
  1680. #define LTO_SECTS_SECTION "__wrapper_sects"
  1681. #define LTO_NAMES_SECTION "__wrapper_names"
  1682. #define LTO_INDEX_SECTION "__wrapper_index"
  1683. /* File to temporarily store LTO data. This is appended to asm_out_file
  1684. in darwin_end_file. */
  1685. static FILE *lto_asm_out_file, *saved_asm_out_file;
  1686. static char *lto_asm_out_name;
  1687. /* Prepare asm_out_file for LTO output. For darwin, this means hiding
  1688. asm_out_file and switching to an alternative output file. */
  1689. void
  1690. darwin_asm_lto_start (void)
  1691. {
  1692. gcc_assert (! saved_asm_out_file);
  1693. saved_asm_out_file = asm_out_file;
  1694. if (! lto_asm_out_name)
  1695. lto_asm_out_name = make_temp_file (".lto.s");
  1696. lto_asm_out_file = fopen (lto_asm_out_name, "a");
  1697. if (lto_asm_out_file == NULL)
  1698. fatal_error (input_location,
  1699. "failed to open temporary file %s for LTO output",
  1700. lto_asm_out_name);
  1701. asm_out_file = lto_asm_out_file;
  1702. }
  1703. /* Restore asm_out_file. */
  1704. void
  1705. darwin_asm_lto_end (void)
  1706. {
  1707. gcc_assert (saved_asm_out_file);
  1708. fclose (lto_asm_out_file);
  1709. asm_out_file = saved_asm_out_file;
  1710. saved_asm_out_file = NULL;
  1711. }
  1712. static void
  1713. darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
  1714. /* Called for the TARGET_ASM_NAMED_SECTION hook. */
  1715. void
  1716. darwin_asm_named_section (const char *name,
  1717. unsigned int flags,
  1718. tree decl ATTRIBUTE_UNUSED)
  1719. {
  1720. /* LTO sections go in a special section that encapsulates the (unlimited)
  1721. number of GNU LTO sections within a single mach-o one. */
  1722. if (strncmp (name, LTO_SECTION_NAME_PREFIX,
  1723. strlen (LTO_SECTION_NAME_PREFIX)) == 0)
  1724. {
  1725. darwin_lto_section_e e;
  1726. /* We expect certain flags to be set... */
  1727. gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
  1728. == (SECTION_DEBUG | SECTION_NAMED));
  1729. /* Switch to our combined section. */
  1730. fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
  1731. LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
  1732. /* Output a label for the start of this sub-section. */
  1733. fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
  1734. lto_section_num, name);
  1735. /* We have to jump through hoops to get the values of the intra-section
  1736. offsets... */
  1737. fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
  1738. lto_section_num, lto_section_num);
  1739. fprintf (asm_out_file,
  1740. "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
  1741. lto_section_num, lto_section_num+1, lto_section_num);
  1742. lto_section_num++;
  1743. e.sectname = xstrdup (name);
  1744. /* Keep the names, we'll need to make a table later.
  1745. TODO: check that we do not revisit sections, that would break
  1746. the assumption of how this is done. */
  1747. if (lto_section_names == NULL)
  1748. vec_alloc (lto_section_names, 16);
  1749. vec_safe_push (lto_section_names, e);
  1750. }
  1751. else if (strncmp (name, "__DWARF,", 8) == 0)
  1752. darwin_asm_dwarf_section (name, flags, decl);
  1753. else
  1754. fprintf (asm_out_file, "\t.section %s\n", name);
  1755. }
  1756. void
  1757. darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
  1758. {
  1759. /* Darwin does not use unique sections. */
  1760. }
  1761. /* Handle __attribute__ ((apple_kext_compatibility)).
  1762. This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
  1763. vtable for classes with this attribute (and their descendants) by not
  1764. outputting the new 3.0 nondeleting destructor. This means that such
  1765. objects CANNOT be allocated on the stack or as globals UNLESS they have
  1766. a completely empty `operator delete'.
  1767. Luckily, this fits in with the Darwin kext model.
  1768. This attribute also disables gcc3's potential overlaying of derived
  1769. class data members on the padding at the end of the base class. */
  1770. tree
  1771. darwin_handle_kext_attribute (tree *node, tree name,
  1772. tree args ATTRIBUTE_UNUSED,
  1773. int flags ATTRIBUTE_UNUSED,
  1774. bool *no_add_attrs)
  1775. {
  1776. /* APPLE KEXT stuff -- only applies with pure static C++ code. */
  1777. if (! TARGET_KEXTABI)
  1778. {
  1779. warning (0, "%qE 2.95 vtable-compatibility attribute applies "
  1780. "only when compiling a kext", name);
  1781. *no_add_attrs = true;
  1782. }
  1783. else if (TREE_CODE (*node) != RECORD_TYPE)
  1784. {
  1785. warning (0, "%qE 2.95 vtable-compatibility attribute applies "
  1786. "only to C++ classes", name);
  1787. *no_add_attrs = true;
  1788. }
  1789. return NULL_TREE;
  1790. }
  1791. /* Handle a "weak_import" attribute; arguments as in
  1792. struct attribute_spec.handler. */
  1793. tree
  1794. darwin_handle_weak_import_attribute (tree *node, tree name,
  1795. tree ARG_UNUSED (args),
  1796. int ARG_UNUSED (flags),
  1797. bool * no_add_attrs)
  1798. {
  1799. if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
  1800. {
  1801. warning (OPT_Wattributes, "%qE attribute ignored",
  1802. name);
  1803. *no_add_attrs = true;
  1804. }
  1805. else
  1806. declare_weak (*node);
  1807. return NULL_TREE;
  1808. }
  1809. /* Emit a label for an FDE, making it global and/or weak if appropriate.
  1810. The third parameter is nonzero if this is for exception handling.
  1811. The fourth parameter is nonzero if this is just a placeholder for an
  1812. FDE that we are omitting. */
  1813. void
  1814. darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
  1815. {
  1816. char *lab ;
  1817. char buf[32];
  1818. static int invok_count = 0;
  1819. static tree last_fun_decl = NULL_TREE;
  1820. /* We use the linker to emit the .eh labels for Darwin 9 and above. */
  1821. if (! for_eh || generating_for_darwin_version >= 9)
  1822. return;
  1823. /* FIXME: This only works when the eh for all sections of a function is
  1824. emitted at the same time. If that changes, we would need to use a lookup
  1825. table of some form to determine what to do. Also, we should emit the
  1826. unadorned label for the partition containing the public label for a
  1827. function. This is of limited use, probably, since we do not currently
  1828. enable partitioning. */
  1829. strcpy (buf, ".eh");
  1830. if (decl && TREE_CODE (decl) == FUNCTION_DECL)
  1831. {
  1832. if (decl == last_fun_decl)
  1833. {
  1834. invok_count++;
  1835. snprintf (buf, 31, "$$part$$%d.eh", invok_count);
  1836. }
  1837. else
  1838. {
  1839. last_fun_decl = decl;
  1840. invok_count = 0;
  1841. }
  1842. }
  1843. lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
  1844. if (TREE_PUBLIC (decl))
  1845. {
  1846. targetm.asm_out.globalize_label (file, lab);
  1847. if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
  1848. {
  1849. fputs ("\t.private_extern ", file);
  1850. assemble_name (file, lab);
  1851. fputc ('\n', file);
  1852. }
  1853. }
  1854. if (DECL_WEAK (decl))
  1855. {
  1856. fputs ("\t.weak_definition ", file);
  1857. assemble_name (file, lab);
  1858. fputc ('\n', file);
  1859. }
  1860. assemble_name (file, lab);
  1861. if (empty)
  1862. {
  1863. fputs (" = 0\n", file);
  1864. /* Mark the absolute .eh and .eh1 style labels as needed to
  1865. ensure that we don't dead code strip them and keep such
  1866. labels from another instantiation point until we can fix this
  1867. properly with group comdat support. */
  1868. darwin_mark_decl_preserved (lab);
  1869. }
  1870. else
  1871. fputs (":\n", file);
  1872. free (lab);
  1873. }
  1874. static GTY(()) unsigned long except_table_label_num;
  1875. void
  1876. darwin_emit_except_table_label (FILE *file)
  1877. {
  1878. char section_start_label[30];
  1879. ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
  1880. except_table_label_num++);
  1881. ASM_OUTPUT_LABEL (file, section_start_label);
  1882. }
  1883. /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
  1884. void
  1885. darwin_non_lazy_pcrel (FILE *file, rtx addr)
  1886. {
  1887. const char *nlp_name;
  1888. gcc_assert (GET_CODE (addr) == SYMBOL_REF);
  1889. nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
  1890. fputs ("\t.long\t", file);
  1891. ASM_OUTPUT_LABELREF (file, nlp_name);
  1892. fputs ("-.", file);
  1893. }
  1894. /* If this is uncommented, details of each allocation will be printed
  1895. in the asm right before the actual code. WARNING - this will cause some
  1896. test-suite fails (since the printout will contain items that some tests
  1897. are not expecting) -- so don't leave it on by default (it bloats the
  1898. asm too). */
  1899. /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
  1900. /* The first two of these routines are ostensibly just intended to put
  1901. names into the asm. However, they are both hijacked in order to ensure
  1902. that zero-sized items do not make their way into the output. Consequently,
  1903. we also need to make these participate in provisions for dealing with
  1904. such items in section anchors. */
  1905. /* The implementation of ASM_DECLARE_OBJECT_NAME. */
  1906. /* The RTTI data (e.g., __ti4name) is common and public (and static),
  1907. but it does need to be referenced via indirect PIC data pointers.
  1908. The machopic_define_symbol calls are telling the machopic subsystem
  1909. that the name *is* defined in this module, so it doesn't need to
  1910. make them indirect. */
  1911. void
  1912. darwin_asm_declare_object_name (FILE *file,
  1913. const char *nam, tree decl)
  1914. {
  1915. const char *xname = nam;
  1916. unsigned HOST_WIDE_INT size;
  1917. bool local_def, weak;
  1918. weak = (DECL_P (decl)
  1919. && DECL_WEAK (decl)
  1920. && !lookup_attribute ("weak_import",
  1921. DECL_ATTRIBUTES (decl)));
  1922. local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
  1923. && (!DECL_COMMON (decl)
  1924. || !TREE_PUBLIC (decl)));
  1925. if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
  1926. xname = IDENTIFIER_POINTER (DECL_NAME (decl));
  1927. if (local_def)
  1928. {
  1929. (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
  1930. if (!weak)
  1931. machopic_define_symbol (DECL_RTL (decl));
  1932. }
  1933. size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
  1934. #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
  1935. fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
  1936. " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
  1937. xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
  1938. (unsigned long long)size, DECL_ALIGN (decl), local_def,
  1939. DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
  1940. TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
  1941. (unsigned long)DECL_INITIAL (decl));
  1942. #endif
  1943. /* Darwin needs help to support local zero-sized objects.
  1944. They must be made at least one byte, and the section containing must be
  1945. marked as unsuitable for section-anchors (see storage allocators below).
  1946. For non-zero objects this output is handled by varasm.c.
  1947. */
  1948. if (!size)
  1949. {
  1950. unsigned int l2align = 0;
  1951. /* The align must be honored, even for zero-sized. */
  1952. if (DECL_ALIGN (decl))
  1953. {
  1954. l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
  1955. fprintf (file, "\t.align\t%u\n", l2align);
  1956. }
  1957. ASM_OUTPUT_LABEL (file, xname);
  1958. size = 1;
  1959. fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
  1960. /* Check that we've correctly picked up the zero-sized item and placed it
  1961. properly. */
  1962. gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
  1963. || (in_section
  1964. && (in_section->common.flags & SECTION_NO_ANCHOR)));
  1965. }
  1966. else
  1967. ASM_OUTPUT_LABEL (file, xname);
  1968. }
  1969. /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
  1970. void
  1971. darwin_asm_declare_constant_name (FILE *file, const char *name,
  1972. const_tree exp ATTRIBUTE_UNUSED,
  1973. HOST_WIDE_INT size)
  1974. {
  1975. assemble_label (file, name);
  1976. /* As for other items, we need at least one byte. */
  1977. if (!size)
  1978. {
  1979. fputs ("\t.space\t1\n", file);
  1980. /* Check that we've correctly picked up the zero-sized item and placed it
  1981. properly. */
  1982. gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
  1983. || (in_section
  1984. && (in_section->common.flags & SECTION_NO_ANCHOR)));
  1985. }
  1986. }
  1987. /* Darwin storage allocators.
  1988. Zerofill sections are desirable for large blank data since, otherwise, these
  1989. data bloat objects (PR33210).
  1990. However, section anchors don't work in .zerofill sections (one cannot switch
  1991. to a zerofill section). Ergo, for Darwin targets using section anchors we need
  1992. to put (at least some) data into 'normal' switchable sections.
  1993. Here we set a relatively arbitrary value for the size of an object to trigger
  1994. zerofill when section anchors are enabled (anything bigger than a page for
  1995. current Darwin implementations). FIXME: there ought to be some objective way
  1996. to make this choice.
  1997. When section anchor are off this is ignored anyway. */
  1998. #define BYTES_ZFILL 4096
  1999. /* Emit a chunk of data for items coalesced by the linker. */
  2000. static void
  2001. darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
  2002. unsigned HOST_WIDE_INT size,
  2003. unsigned int align)
  2004. {
  2005. /* Since the sections used here are coalesed, they will not be eligible
  2006. for section anchors, and therefore we don't need to break that out. */
  2007. if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
  2008. switch_to_section (darwin_sections[const_data_coal_section]);
  2009. else
  2010. switch_to_section (darwin_sections[data_coal_section]);
  2011. /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
  2012. the align info for zero-sized items... but do it here otherwise. */
  2013. if (size && align)
  2014. fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
  2015. if (TREE_PUBLIC (decl))
  2016. darwin_globalize_label (fp, name);
  2017. /* ... and we let it deal with outputting one byte of zero for them too. */
  2018. darwin_asm_declare_object_name (fp, name, decl);
  2019. if (size)
  2020. assemble_zeros (size);
  2021. }
  2022. /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
  2023. static void
  2024. darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
  2025. unsigned HOST_WIDE_INT size,
  2026. unsigned int align, tree meta)
  2027. {
  2028. section *ocs = data_section;
  2029. if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
  2030. ocs = darwin_objc2_section (decl, meta, ocs);
  2031. else
  2032. ocs = darwin_objc1_section (decl, meta, ocs);
  2033. switch_to_section (ocs);
  2034. /* We shall declare that zero-sized meta-data are not valid (yet). */
  2035. gcc_assert (size);
  2036. fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
  2037. /* ... and we let it deal with outputting one byte of zero for them too. */
  2038. darwin_asm_declare_object_name (fp, name, decl);
  2039. assemble_zeros (size);
  2040. }
  2041. /* This routine emits 'local' storage:
  2042. When Section Anchors are off this routine emits .zerofill commands in
  2043. sections named for their alignment.
  2044. When Section Anchors are on, smaller (non-zero-sized) items are placed in
  2045. the .static_data section so that the section anchoring system can see them.
  2046. Larger items are still placed in .zerofill sections, addressing PR33210.
  2047. The routine has no checking - it is all assumed to be done by the caller.
  2048. */
  2049. static void
  2050. darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
  2051. unsigned HOST_WIDE_INT size,
  2052. unsigned int l2align)
  2053. {
  2054. /* FIXME: We have a fudge to make this work with Java even when the target does
  2055. not use sections anchors -- Java seems to need at least one small item in a
  2056. non-zerofill segment. */
  2057. if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
  2058. || (size && size <= 2))
  2059. {
  2060. /* Put smaller objects in _static_data, where the section anchors system
  2061. can get them.
  2062. However, if they are zero-sized punt them to yet a different section
  2063. (that is not allowed to participate in anchoring). */
  2064. if (!size)
  2065. {
  2066. fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
  2067. in_section = darwin_sections[zobj_bss_section];
  2068. size = 1;
  2069. }
  2070. else
  2071. {
  2072. fputs ("\t.static_data\n", fp);
  2073. in_section = darwin_sections[static_data_section];
  2074. }
  2075. if (l2align)
  2076. fprintf (fp, "\t.align\t%u\n", l2align);
  2077. assemble_name (fp, name);
  2078. fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
  2079. }
  2080. else
  2081. {
  2082. /* When we are on a non-section anchor target, we can get zero-sized
  2083. items here. However, all we need to do is to bump them to one byte
  2084. and the section alignment will take care of the rest. */
  2085. char secnam[64];
  2086. unsigned int flags ;
  2087. snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
  2088. (unsigned) l2align);
  2089. /* We can't anchor (yet, if ever) in zerofill sections, because we can't
  2090. switch to them and emit a label. */
  2091. flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
  2092. in_section = get_section (secnam, flags, NULL);
  2093. fprintf (fp, "\t.zerofill %s,", secnam);
  2094. assemble_name (fp, name);
  2095. if (!size)
  2096. size = 1;
  2097. if (l2align)
  2098. fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
  2099. size, (unsigned) l2align);
  2100. else
  2101. fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
  2102. }
  2103. (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
  2104. /* This is defined as a file-scope var, so we know to notify machopic. */
  2105. machopic_define_symbol (DECL_RTL (decl));
  2106. }
  2107. /* Emit a chunk of common. */
  2108. static void
  2109. darwin_emit_common (FILE *fp, const char *name,
  2110. unsigned HOST_WIDE_INT size, unsigned int align)
  2111. {
  2112. unsigned HOST_WIDE_INT rounded;
  2113. unsigned int l2align;
  2114. /* Earlier systems complain if the alignment exceeds the page size.
  2115. The magic number is 4096 * 8 - hard-coded for legacy systems. */
  2116. if (!emit_aligned_common && (align > 32768UL))
  2117. align = 4096UL; /* In units. */
  2118. else
  2119. align /= BITS_PER_UNIT;
  2120. /* Make sure we have a meaningful align. */
  2121. if (!align)
  2122. align = 1;
  2123. /* For earlier toolchains, we need to emit the var as a rounded size to
  2124. tell ld the alignment. */
  2125. if (size < align)
  2126. rounded = align;
  2127. else
  2128. rounded = (size + (align-1)) & ~(align-1);
  2129. l2align = floor_log2 (align);
  2130. gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
  2131. in_section = comm_section;
  2132. /* We mustn't allow multiple public symbols to share an address when using
  2133. the normal OSX toolchain. */
  2134. if (!size)
  2135. {
  2136. /* Put at least one byte. */
  2137. size = 1;
  2138. /* This section can no longer participate in section anchoring. */
  2139. comm_section->common.flags |= SECTION_NO_ANCHOR;
  2140. }
  2141. fputs ("\t.comm\t", fp);
  2142. assemble_name (fp, name);
  2143. fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
  2144. emit_aligned_common?size:rounded);
  2145. if (l2align && emit_aligned_common)
  2146. fprintf (fp, ",%u", l2align);
  2147. fputs ("\n", fp);
  2148. }
  2149. /* Output a var which is all zero - into aligned BSS sections, common, lcomm
  2150. or coalescable data sections (for weak or comdat) as appropriate. */
  2151. void
  2152. darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
  2153. unsigned HOST_WIDE_INT size, unsigned int align)
  2154. {
  2155. unsigned int l2align;
  2156. bool one, pub, weak;
  2157. tree meta;
  2158. pub = TREE_PUBLIC (decl);
  2159. one = DECL_ONE_ONLY (decl);
  2160. weak = (DECL_P (decl)
  2161. && DECL_WEAK (decl)
  2162. && !lookup_attribute ("weak_import",
  2163. DECL_ATTRIBUTES (decl)));
  2164. #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
  2165. fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
  2166. " pub %d weak %d one %d init %lx\n",
  2167. name, (long long)size, (int)align, TREE_READONLY (decl),
  2168. TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
  2169. pub, weak, one, (unsigned long)DECL_INITIAL (decl));
  2170. #endif
  2171. /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
  2172. before the target has a chance to comment. */
  2173. if ((meta = is_objc_metadata (decl)))
  2174. {
  2175. darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
  2176. return;
  2177. }
  2178. /* Check that any initializer is valid. */
  2179. gcc_assert ((DECL_INITIAL (decl) == NULL)
  2180. || (DECL_INITIAL (decl) == error_mark_node)
  2181. || initializer_zerop (DECL_INITIAL (decl)));
  2182. gcc_assert (DECL_SECTION_NAME (decl) == NULL);
  2183. gcc_assert (!DECL_COMMON (decl));
  2184. /* Pick up the correct alignment. */
  2185. if (!size || !align)
  2186. align = DECL_ALIGN (decl);
  2187. l2align = floor_log2 (align / BITS_PER_UNIT);
  2188. gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
  2189. last_assemble_variable_decl = decl;
  2190. /* We would rather not have to check this here - but it seems that we might
  2191. be passed a decl that should be in coalesced space. */
  2192. if (one || weak)
  2193. {
  2194. /* Weak or COMDAT objects are put in mergeable sections. */
  2195. darwin_emit_weak_or_comdat (fp, decl, name, size,
  2196. DECL_ALIGN (decl));
  2197. return;
  2198. }
  2199. /* If this is not public, then emit according to local rules. */
  2200. if (!pub)
  2201. {
  2202. darwin_emit_local_bss (fp, decl, name, size, l2align);
  2203. return;
  2204. }
  2205. /* So we have a public symbol (small item fudge for Java, see above). */
  2206. if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
  2207. || (size && size <= 2))
  2208. {
  2209. /* Put smaller objects in data, where the section anchors system can get
  2210. them. However, if they are zero-sized punt them to yet a different
  2211. section (that is not allowed to participate in anchoring). */
  2212. if (!size)
  2213. {
  2214. fputs ("\t.section\t__DATA,__zobj_data\n", fp);
  2215. in_section = darwin_sections[zobj_data_section];
  2216. size = 1;
  2217. }
  2218. else
  2219. {
  2220. fputs ("\t.data\n", fp);
  2221. in_section = data_section;
  2222. }
  2223. if (l2align)
  2224. fprintf (fp, "\t.align\t%u\n", l2align);
  2225. assemble_name (fp, name);
  2226. fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
  2227. }
  2228. else
  2229. {
  2230. char secnam[64];
  2231. unsigned int flags ;
  2232. /* When we are on a non-section anchor target, we can get zero-sized
  2233. items here. However, all we need to do is to bump them to one byte
  2234. and the section alignment will take care of the rest. */
  2235. snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
  2236. /* We can't anchor in zerofill sections, because we can't switch
  2237. to them and emit a label. */
  2238. flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
  2239. in_section = get_section (secnam, flags, NULL);
  2240. fprintf (fp, "\t.zerofill %s,", secnam);
  2241. assemble_name (fp, name);
  2242. if (!size)
  2243. size = 1;
  2244. if (l2align)
  2245. fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
  2246. else
  2247. fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
  2248. }
  2249. (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
  2250. }
  2251. /* Output a chunk of common, with alignment specified (where the target
  2252. supports this). */
  2253. void
  2254. darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
  2255. unsigned HOST_WIDE_INT size,
  2256. unsigned int align)
  2257. {
  2258. unsigned int l2align;
  2259. bool one, weak;
  2260. tree meta;
  2261. /* No corresponding var. */
  2262. if (decl==NULL)
  2263. {
  2264. #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
  2265. fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
  2266. #endif
  2267. darwin_emit_common (fp, name, size, align);
  2268. return;
  2269. }
  2270. one = DECL_ONE_ONLY (decl);
  2271. weak = (DECL_P (decl)
  2272. && DECL_WEAK (decl)
  2273. && !lookup_attribute ("weak_import",
  2274. DECL_ATTRIBUTES (decl)));
  2275. #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
  2276. fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
  2277. " weak %d one %d init %lx\n",
  2278. name, (long long)size, (int)align, TREE_READONLY (decl),
  2279. TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
  2280. TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
  2281. #endif
  2282. /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
  2283. before the target has a chance to comment. */
  2284. if ((meta = is_objc_metadata (decl)))
  2285. {
  2286. darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
  2287. return;
  2288. }
  2289. /* We shouldn't be messing with this if the decl has a section name. */
  2290. gcc_assert (DECL_SECTION_NAME (decl) == NULL);
  2291. /* We would rather not have to check this here - but it seems that we might
  2292. be passed a decl that should be in coalesced space. */
  2293. if (one || weak)
  2294. {
  2295. /* Weak or COMDAT objects are put in mergable sections. */
  2296. darwin_emit_weak_or_comdat (fp, decl, name, size,
  2297. DECL_ALIGN (decl));
  2298. return;
  2299. }
  2300. /* We should only get here for DECL_COMMON, with a zero init (and, in
  2301. principle, only for public symbols too - although we deal with local
  2302. ones below). */
  2303. /* Check the initializer is OK. */
  2304. gcc_assert (DECL_COMMON (decl)
  2305. && ((DECL_INITIAL (decl) == NULL)
  2306. || (DECL_INITIAL (decl) == error_mark_node)
  2307. || initializer_zerop (DECL_INITIAL (decl))));
  2308. last_assemble_variable_decl = decl;
  2309. if (!size || !align)
  2310. align = DECL_ALIGN (decl);
  2311. l2align = floor_log2 (align / BITS_PER_UNIT);
  2312. /* Check we aren't asking for more aligment than the platform allows. */
  2313. gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
  2314. if (TREE_PUBLIC (decl) != 0)
  2315. darwin_emit_common (fp, name, size, align);
  2316. else
  2317. darwin_emit_local_bss (fp, decl, name, size, l2align);
  2318. }
  2319. /* Output a chunk of BSS with alignment specfied. */
  2320. void
  2321. darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
  2322. unsigned HOST_WIDE_INT size,
  2323. unsigned int align)
  2324. {
  2325. unsigned long l2align;
  2326. bool one, weak;
  2327. tree meta;
  2328. one = DECL_ONE_ONLY (decl);
  2329. weak = (DECL_P (decl)
  2330. && DECL_WEAK (decl)
  2331. && !lookup_attribute ("weak_import",
  2332. DECL_ATTRIBUTES (decl)));
  2333. #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
  2334. fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
  2335. " weak %d init %lx\n",
  2336. name, (long long)size, (int)align, TREE_READONLY (decl),
  2337. TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
  2338. weak , (unsigned long)DECL_INITIAL (decl));
  2339. #endif
  2340. /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
  2341. before the target has a chance to comment. */
  2342. if ((meta = is_objc_metadata (decl)))
  2343. {
  2344. darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
  2345. return;
  2346. }
  2347. /* We shouldn't be messing with this if the decl has a section name. */
  2348. gcc_assert (DECL_SECTION_NAME (decl) == NULL);
  2349. /* We would rather not have to check this here - but it seems that we might
  2350. be passed a decl that should be in coalesced space. */
  2351. if (one || weak)
  2352. {
  2353. /* Weak or COMDAT objects are put in mergable sections. */
  2354. darwin_emit_weak_or_comdat (fp, decl, name, size,
  2355. DECL_ALIGN (decl));
  2356. return;
  2357. }
  2358. /* .. and it should be suitable for placement in local mem. */
  2359. gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
  2360. /* .. and any initializer must be all-zero. */
  2361. gcc_assert ((DECL_INITIAL (decl) == NULL)
  2362. || (DECL_INITIAL (decl) == error_mark_node)
  2363. || initializer_zerop (DECL_INITIAL (decl)));
  2364. last_assemble_variable_decl = decl;
  2365. if (!size || !align)
  2366. align = DECL_ALIGN (decl);
  2367. l2align = floor_log2 (align / BITS_PER_UNIT);
  2368. gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
  2369. darwin_emit_local_bss (fp, decl, name, size, l2align);
  2370. }
  2371. /* Emit an assembler directive to set visibility for a symbol. The
  2372. only supported visibilities are VISIBILITY_DEFAULT and
  2373. VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
  2374. extern". There is no MACH-O equivalent of ELF's
  2375. VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
  2376. void
  2377. darwin_assemble_visibility (tree decl, int vis)
  2378. {
  2379. if (vis == VISIBILITY_DEFAULT)
  2380. ;
  2381. else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
  2382. {
  2383. fputs ("\t.private_extern ", asm_out_file);
  2384. assemble_name (asm_out_file,
  2385. (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
  2386. fputs ("\n", asm_out_file);
  2387. }
  2388. else
  2389. warning (OPT_Wattributes, "protected visibility attribute "
  2390. "not supported in this configuration; ignored");
  2391. }
  2392. /* vec used by darwin_asm_dwarf_section.
  2393. Maybe a hash tab would be better here - but the intention is that this is
  2394. a very short list (fewer than 16 items) and each entry should (ideally,
  2395. eventually) only be presented once.
  2396. A structure to hold a dwarf debug section used entry. */
  2397. typedef struct GTY(()) dwarf_sect_used_entry {
  2398. const char *name;
  2399. unsigned count;
  2400. }
  2401. dwarf_sect_used_entry;
  2402. /* A list of used __DWARF sections. */
  2403. static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
  2404. /* This is called when we are asked to assemble a named section and the
  2405. name begins with __DWARF,. We keep a list of the section names (without
  2406. the __DWARF, prefix) and use this to emit our required start label on the
  2407. first switch to each section. */
  2408. static void
  2409. darwin_asm_dwarf_section (const char *name, unsigned int flags,
  2410. tree ARG_UNUSED (decl))
  2411. {
  2412. unsigned i;
  2413. int namelen;
  2414. const char * sname;
  2415. dwarf_sect_used_entry *ref;
  2416. bool found = false;
  2417. gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
  2418. == (SECTION_DEBUG | SECTION_NAMED));
  2419. /* We know that the name starts with __DWARF, */
  2420. sname = name + 8;
  2421. namelen = strchr (sname, ',') - sname;
  2422. gcc_assert (namelen);
  2423. if (dwarf_sect_names_table == NULL)
  2424. vec_alloc (dwarf_sect_names_table, 16);
  2425. else
  2426. for (i = 0;
  2427. dwarf_sect_names_table->iterate (i, &ref);
  2428. i++)
  2429. {
  2430. if (!ref)
  2431. break;
  2432. if (!strcmp (ref->name, sname))
  2433. {
  2434. found = true;
  2435. ref->count++;
  2436. break;
  2437. }
  2438. }
  2439. fprintf (asm_out_file, "\t.section %s\n", name);
  2440. if (!found)
  2441. {
  2442. dwarf_sect_used_entry e;
  2443. fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
  2444. e.count = 1;
  2445. e.name = xstrdup (sname);
  2446. vec_safe_push (dwarf_sect_names_table, e);
  2447. }
  2448. }
  2449. /* Output a difference of two labels that will be an assembly time
  2450. constant if the two labels are local. (.long lab1-lab2 will be
  2451. very different if lab1 is at the boundary between two sections; it
  2452. will be relocated according to the second section, not the first,
  2453. so one ends up with a difference between labels in different
  2454. sections, which is bad in the dwarf2 eh context for instance.) */
  2455. static int darwin_dwarf_label_counter;
  2456. void
  2457. darwin_asm_output_dwarf_delta (FILE *file, int size,
  2458. const char *lab1, const char *lab2)
  2459. {
  2460. int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
  2461. && lab2[0] == '*' && lab2[1] == 'L');
  2462. const char *directive = (size == 8 ? ".quad" : ".long");
  2463. if (islocaldiff)
  2464. fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
  2465. else
  2466. fprintf (file, "\t%s\t", directive);
  2467. assemble_name_raw (file, lab1);
  2468. fprintf (file, "-");
  2469. assemble_name_raw (file, lab2);
  2470. if (islocaldiff)
  2471. fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
  2472. }
  2473. /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
  2474. offsets are not represented using relocs in .o files; either the
  2475. section never leaves the .o file, or the linker or other tool is
  2476. responsible for parsing the DWARF and updating the offsets. */
  2477. void
  2478. darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
  2479. section *base)
  2480. {
  2481. char sname[64];
  2482. int namelen;
  2483. gcc_assert (base->common.flags & SECTION_NAMED);
  2484. gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
  2485. gcc_assert (strchr (base->named.name + 8, ','));
  2486. namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
  2487. sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
  2488. darwin_asm_output_dwarf_delta (file, size, lab, sname);
  2489. }
  2490. /* Called from the within the TARGET_ASM_FILE_START for each target. */
  2491. void
  2492. darwin_file_start (void)
  2493. {
  2494. /* Nothing to do. */
  2495. }
  2496. /* Called for the TARGET_ASM_FILE_END hook.
  2497. Emit the mach-o pic indirection data, the lto data and, finally a flag
  2498. to tell the linker that it can break the file object into sections and
  2499. move those around for efficiency. */
  2500. void
  2501. darwin_file_end (void)
  2502. {
  2503. if (!vec_safe_is_empty (ctors))
  2504. finalize_ctors ();
  2505. if (!vec_safe_is_empty (dtors))
  2506. finalize_dtors ();
  2507. /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
  2508. some) then we output the fix-and-continue marker (Image Info).
  2509. This applies to Objective C, Objective C++ and LTO with either language
  2510. as part of the input. */
  2511. if (flag_next_runtime && objc_metadata_seen)
  2512. {
  2513. unsigned int flags = 0;
  2514. if (flag_objc_abi >= 2)
  2515. {
  2516. flags = 16;
  2517. output_section_asm_op
  2518. (darwin_sections[objc2_image_info_section]->unnamed.data);
  2519. }
  2520. else
  2521. output_section_asm_op
  2522. (darwin_sections[objc_image_info_section]->unnamed.data);
  2523. ASM_OUTPUT_ALIGN (asm_out_file, 2);
  2524. fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
  2525. flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
  2526. flags |= flag_objc_gc ? 2 : 0;
  2527. fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
  2528. }
  2529. machopic_finish (asm_out_file);
  2530. if (lang_GNU_CXX ())
  2531. {
  2532. switch_to_section (darwin_sections[constructor_section]);
  2533. switch_to_section (darwin_sections[destructor_section]);
  2534. ASM_OUTPUT_ALIGN (asm_out_file, 1);
  2535. }
  2536. /* If there was LTO assembler output, append it to asm_out_file. */
  2537. if (lto_asm_out_name)
  2538. {
  2539. int n;
  2540. char *buf, *lto_asm_txt;
  2541. /* Shouldn't be here if we failed to switch back. */
  2542. gcc_assert (! saved_asm_out_file);
  2543. lto_asm_out_file = fopen (lto_asm_out_name, "r");
  2544. if (lto_asm_out_file == NULL)
  2545. fatal_error (input_location,
  2546. "failed to open temporary file %s with LTO output",
  2547. lto_asm_out_name);
  2548. fseek (lto_asm_out_file, 0, SEEK_END);
  2549. n = ftell (lto_asm_out_file);
  2550. if (n > 0)
  2551. {
  2552. fseek (lto_asm_out_file, 0, SEEK_SET);
  2553. lto_asm_txt = buf = (char *) xmalloc (n + 1);
  2554. while (fgets (lto_asm_txt, n, lto_asm_out_file))
  2555. fputs (lto_asm_txt, asm_out_file);
  2556. /* Put a termination label. */
  2557. fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
  2558. LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
  2559. fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
  2560. lto_section_num);
  2561. /* Make sure our termination label stays in this section. */
  2562. fputs ("\t.space\t1\n", asm_out_file);
  2563. }
  2564. /* Remove the temporary file. */
  2565. fclose (lto_asm_out_file);
  2566. unlink_if_ordinary (lto_asm_out_name);
  2567. free (lto_asm_out_name);
  2568. }
  2569. /* Output the names and indices. */
  2570. if (lto_section_names && lto_section_names->length ())
  2571. {
  2572. int count;
  2573. darwin_lto_section_e *ref;
  2574. /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
  2575. the latter up ourselves. */
  2576. const char *op = integer_asm_op (4,0);
  2577. /* Emit the names. */
  2578. fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
  2579. LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
  2580. FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
  2581. {
  2582. fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
  2583. /* We have to jump through hoops to get the values of the intra-section
  2584. offsets... */
  2585. fprintf (asm_out_file,
  2586. "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
  2587. count, count);
  2588. fprintf (asm_out_file,
  2589. "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
  2590. count, count+1, count);
  2591. fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
  2592. }
  2593. fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
  2594. /* make sure our termination label stays in this section. */
  2595. fputs ("\t.space\t1\n", asm_out_file);
  2596. /* Emit the Index. */
  2597. fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
  2598. LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
  2599. fputs ("\t.align\t2\n", asm_out_file);
  2600. fputs ("# Section offset, Section length, Name offset, Name length\n",
  2601. asm_out_file);
  2602. FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
  2603. {
  2604. fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
  2605. op, count, ref->sectname);
  2606. fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
  2607. fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
  2608. fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
  2609. }
  2610. }
  2611. /* If we have section anchors, then we must prevent the linker from
  2612. re-arranging data. */
  2613. if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
  2614. fprintf (asm_out_file, "\t.subsections_via_symbols\n");
  2615. }
  2616. /* TODO: Add a language hook for identifying if a decl is a vtable. */
  2617. #define DARWIN_VTABLE_P(DECL) 0
  2618. /* Cross-module name binding. Darwin does not support overriding
  2619. functions at dynamic-link time, except for vtables in kexts. */
  2620. bool
  2621. darwin_binds_local_p (const_tree decl)
  2622. {
  2623. return default_binds_local_p_1 (decl,
  2624. TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
  2625. }
  2626. /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
  2627. anchor relative to ".", the current section position. We cannot use
  2628. the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
  2629. void
  2630. darwin_asm_output_anchor (rtx symbol)
  2631. {
  2632. fprintf (asm_out_file, "\t.set\t");
  2633. assemble_name (asm_out_file, XSTR (symbol, 0));
  2634. fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
  2635. SYMBOL_REF_BLOCK_OFFSET (symbol));
  2636. }
  2637. /* Disable section anchoring on any section containing a zero-sized
  2638. object. */
  2639. bool
  2640. darwin_use_anchors_for_symbol_p (const_rtx symbol)
  2641. {
  2642. if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
  2643. {
  2644. section *sect;
  2645. /* If the section contains a zero-sized object it's ineligible. */
  2646. sect = SYMBOL_REF_BLOCK (symbol)->sect;
  2647. /* This should have the effect of disabling anchors for vars that follow
  2648. any zero-sized one, in a given section. */
  2649. if (sect->common.flags & SECTION_NO_ANCHOR)
  2650. return false;
  2651. /* Also check the normal reasons for suppressing. */
  2652. return default_use_anchors_for_symbol_p (symbol);
  2653. }
  2654. else
  2655. return false;
  2656. }
  2657. /* Set the darwin specific attributes on TYPE. */
  2658. void
  2659. darwin_set_default_type_attributes (tree type)
  2660. {
  2661. if (darwin_ms_struct
  2662. && TREE_CODE (type) == RECORD_TYPE)
  2663. TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
  2664. NULL_TREE,
  2665. TYPE_ATTRIBUTES (type));
  2666. }
  2667. /* True, iff we're generating code for loadable kernel extensions. */
  2668. bool
  2669. darwin_kextabi_p (void) {
  2670. return flag_apple_kext;
  2671. }
  2672. void
  2673. darwin_override_options (void)
  2674. {
  2675. /* Keep track of which (major) version we're generating code for. */
  2676. if (darwin_macosx_version_min)
  2677. {
  2678. if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
  2679. generating_for_darwin_version = 10;
  2680. else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
  2681. generating_for_darwin_version = 9;
  2682. /* Earlier versions are not specifically accounted, until required. */
  2683. }
  2684. /* In principle, this should be c-family only. However, we really need to
  2685. set sensible defaults for LTO as well, since the section selection stuff
  2686. should check for correctness re. the ABI. TODO: check and provide the
  2687. flags (runtime & ABI) from the lto wrapper). */
  2688. /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
  2689. if (!global_options_set.x_flag_objc_abi)
  2690. global_options.x_flag_objc_abi
  2691. = (!flag_next_runtime)
  2692. ? 0
  2693. : (TARGET_64BIT ? 2
  2694. : (generating_for_darwin_version >= 9) ? 1
  2695. : 0);
  2696. /* Objective-C family ABI 2 is only valid for next/m64 at present. */
  2697. if (global_options_set.x_flag_objc_abi && flag_next_runtime)
  2698. {
  2699. if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
  2700. error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
  2701. " used for %<-m64%> targets with"
  2702. " %<-fnext-runtime%>");
  2703. if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
  2704. error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
  2705. " supported on %<-m32%> targets with"
  2706. " %<-fnext-runtime%>");
  2707. }
  2708. /* Don't emit DWARF3/4 unless specifically selected. This is a
  2709. workaround for tool bugs. */
  2710. if (!global_options_set.x_dwarf_strict)
  2711. dwarf_strict = 1;
  2712. if (!global_options_set.x_dwarf_version)
  2713. dwarf_version = 2;
  2714. /* Do not allow unwind tables to be generated by default for m32.
  2715. fnon-call-exceptions will override this, regardless of what we do. */
  2716. if (generating_for_darwin_version < 10
  2717. && !global_options_set.x_flag_asynchronous_unwind_tables
  2718. && !TARGET_64BIT)
  2719. global_options.x_flag_asynchronous_unwind_tables = 0;
  2720. /* Disable -freorder-blocks-and-partition when unwind tables are being
  2721. emitted for Darwin < 9 (OSX 10.5).
  2722. The strategy is, "Unless the User has specifically set/unset an unwind
  2723. flag we will switch off -freorder-blocks-and-partition when unwind tables
  2724. will be generated". If the User specifically sets flags... we assume
  2725. (s)he knows why... */
  2726. if (generating_for_darwin_version < 9
  2727. && global_options_set.x_flag_reorder_blocks_and_partition
  2728. && ((global_options.x_flag_exceptions /* User, c++, java */
  2729. && !global_options_set.x_flag_exceptions) /* User specified... */
  2730. || (global_options.x_flag_unwind_tables
  2731. && !global_options_set.x_flag_unwind_tables)
  2732. || (global_options.x_flag_non_call_exceptions
  2733. && !global_options_set.x_flag_non_call_exceptions)
  2734. || (global_options.x_flag_asynchronous_unwind_tables
  2735. && !global_options_set.x_flag_asynchronous_unwind_tables)))
  2736. {
  2737. inform (input_location,
  2738. "-freorder-blocks-and-partition does not work with exceptions "
  2739. "on this architecture");
  2740. flag_reorder_blocks_and_partition = 0;
  2741. flag_reorder_blocks = 1;
  2742. }
  2743. /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
  2744. one valid choice of exception scheme for each runtime. */
  2745. if (!global_options_set.x_flag_objc_sjlj_exceptions)
  2746. global_options.x_flag_objc_sjlj_exceptions =
  2747. flag_next_runtime && !TARGET_64BIT;
  2748. /* FIXME: and this could be eliminated then too. */
  2749. if (!global_options_set.x_flag_exceptions
  2750. && flag_objc_exceptions
  2751. && TARGET_64BIT)
  2752. flag_exceptions = 1;
  2753. if (flag_mkernel || flag_apple_kext)
  2754. {
  2755. /* -mkernel implies -fapple-kext for C++ */
  2756. if (lang_GNU_CXX ())
  2757. flag_apple_kext = 1;
  2758. flag_no_common = 1;
  2759. /* No EH in kexts. */
  2760. flag_exceptions = 0;
  2761. /* No -fnon-call-exceptions data in kexts. */
  2762. flag_non_call_exceptions = 0;
  2763. /* so no tables either.. */
  2764. flag_unwind_tables = 0;
  2765. flag_asynchronous_unwind_tables = 0;
  2766. /* We still need to emit branch islands for kernel context. */
  2767. darwin_emit_branch_islands = true;
  2768. }
  2769. if (flag_var_tracking_uninit == 0
  2770. && generating_for_darwin_version >= 9
  2771. && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
  2772. : (debug_info_level >= DINFO_LEVEL_NORMAL))
  2773. && write_symbols == DWARF2_DEBUG)
  2774. flag_var_tracking_uninit = flag_var_tracking;
  2775. if (MACHO_DYNAMIC_NO_PIC_P)
  2776. {
  2777. if (flag_pic)
  2778. warning_at (UNKNOWN_LOCATION, 0,
  2779. "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
  2780. " %<-fpie%> or %<-fPIE%>");
  2781. flag_pic = 0;
  2782. }
  2783. else if (flag_pic == 1)
  2784. {
  2785. /* Darwin's -fpic is -fPIC. */
  2786. flag_pic = 2;
  2787. }
  2788. /* It is assumed that branch island stubs are needed for earlier systems. */
  2789. if (generating_for_darwin_version < 9)
  2790. darwin_emit_branch_islands = true;
  2791. else
  2792. emit_aligned_common = true; /* Later systems can support aligned common. */
  2793. /* The c_dialect...() macros are not available to us here. */
  2794. darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
  2795. }
  2796. #if DARWIN_PPC
  2797. /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
  2798. static void
  2799. darwin_patch_builtin (enum built_in_function fncode)
  2800. {
  2801. tree fn = builtin_decl_explicit (fncode);
  2802. tree sym;
  2803. char *newname;
  2804. if (!fn)
  2805. return;
  2806. sym = DECL_ASSEMBLER_NAME (fn);
  2807. newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
  2808. set_user_assembler_name (fn, newname);
  2809. fn = builtin_decl_implicit (fncode);
  2810. if (fn)
  2811. set_user_assembler_name (fn, newname);
  2812. }
  2813. void
  2814. darwin_patch_builtins (void)
  2815. {
  2816. if (LONG_DOUBLE_TYPE_SIZE != 128)
  2817. return;
  2818. #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
  2819. #define PATCH_BUILTIN_NO64(fncode) \
  2820. if (!TARGET_64BIT) \
  2821. darwin_patch_builtin (fncode);
  2822. #define PATCH_BUILTIN_VARIADIC(fncode) \
  2823. if (!TARGET_64BIT \
  2824. && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
  2825. darwin_patch_builtin (fncode);
  2826. #include "darwin-ppc-ldouble-patch.def"
  2827. #undef PATCH_BUILTIN
  2828. #undef PATCH_BUILTIN_NO64
  2829. #undef PATCH_BUILTIN_VARIADIC
  2830. }
  2831. #endif
  2832. /* CFStrings implementation. */
  2833. static GTY(()) tree cfstring_class_reference = NULL_TREE;
  2834. static GTY(()) tree cfstring_type_node = NULL_TREE;
  2835. static GTY(()) tree ccfstring_type_node = NULL_TREE;
  2836. static GTY(()) tree pccfstring_type_node = NULL_TREE;
  2837. static GTY(()) tree pcint_type_node = NULL_TREE;
  2838. static GTY(()) tree pcchar_type_node = NULL_TREE;
  2839. static enum built_in_function darwin_builtin_cfstring;
  2840. /* Store all constructed constant CFStrings in a hash table so that
  2841. they get uniqued properly. */
  2842. typedef struct GTY ((for_user)) cfstring_descriptor {
  2843. /* The string literal. */
  2844. tree literal;
  2845. /* The resulting constant CFString. */
  2846. tree constructor;
  2847. } cfstring_descriptor;
  2848. struct cfstring_hasher : ggc_hasher<cfstring_descriptor *>
  2849. {
  2850. static hashval_t hash (cfstring_descriptor *);
  2851. static bool equal (cfstring_descriptor *, cfstring_descriptor *);
  2852. };
  2853. static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
  2854. static tree
  2855. add_builtin_field_decl (tree type, const char *name, tree **chain)
  2856. {
  2857. tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
  2858. get_identifier (name), type);
  2859. if (*chain != NULL)
  2860. **chain = field;
  2861. *chain = &DECL_CHAIN (field);
  2862. return field;
  2863. }
  2864. tree
  2865. darwin_init_cfstring_builtins (unsigned builtin_cfstring)
  2866. {
  2867. tree cfsfun, fields, pccfstring_ftype_pcchar;
  2868. tree *chain = NULL;
  2869. darwin_builtin_cfstring =
  2870. (enum built_in_function) builtin_cfstring;
  2871. /* struct __builtin_CFString {
  2872. const int *isa; (will point at
  2873. int flags; __CFConstantStringClassReference)
  2874. const char *str;
  2875. long length;
  2876. }; */
  2877. pcint_type_node = build_pointer_type
  2878. (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
  2879. pcchar_type_node = build_pointer_type
  2880. (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
  2881. cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
  2882. /* Have to build backwards for finish struct. */
  2883. fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
  2884. add_builtin_field_decl (pcchar_type_node, "str", &chain);
  2885. add_builtin_field_decl (integer_type_node, "flags", &chain);
  2886. add_builtin_field_decl (pcint_type_node, "isa", &chain);
  2887. finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
  2888. fields, NULL_TREE);
  2889. /* const struct __builtin_CFstring *
  2890. __builtin___CFStringMakeConstantString (const char *); */
  2891. ccfstring_type_node = build_qualified_type
  2892. (cfstring_type_node, TYPE_QUAL_CONST);
  2893. pccfstring_type_node = build_pointer_type (ccfstring_type_node);
  2894. pccfstring_ftype_pcchar = build_function_type_list
  2895. (pccfstring_type_node, pcchar_type_node, NULL_TREE);
  2896. cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
  2897. get_identifier ("__builtin___CFStringMakeConstantString"),
  2898. pccfstring_ftype_pcchar);
  2899. TREE_PUBLIC (cfsfun) = 1;
  2900. DECL_EXTERNAL (cfsfun) = 1;
  2901. DECL_ARTIFICIAL (cfsfun) = 1;
  2902. /* Make a lang-specific section - dup_lang_specific_decl makes a new node
  2903. in place of the existing, which may be NULL. */
  2904. DECL_LANG_SPECIFIC (cfsfun) = NULL;
  2905. (*lang_hooks.dup_lang_specific_decl) (cfsfun);
  2906. DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
  2907. DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
  2908. lang_hooks.builtin_function (cfsfun);
  2909. /* extern int __CFConstantStringClassReference[]; */
  2910. cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
  2911. get_identifier ("__CFConstantStringClassReference"),
  2912. build_array_type (integer_type_node, NULL_TREE));
  2913. TREE_PUBLIC (cfstring_class_reference) = 1;
  2914. DECL_ARTIFICIAL (cfstring_class_reference) = 1;
  2915. (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
  2916. DECL_EXTERNAL (cfstring_class_reference) = 1;
  2917. rest_of_decl_compilation (cfstring_class_reference, 0, 0);
  2918. /* Initialize the hash table used to hold the constant CFString objects. */
  2919. cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
  2920. return cfstring_type_node;
  2921. }
  2922. tree
  2923. darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
  2924. bool ARG_UNUSED (ignore))
  2925. {
  2926. unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
  2927. if (fcode == darwin_builtin_cfstring)
  2928. {
  2929. if (!darwin_constant_cfstrings)
  2930. {
  2931. error ("built-in function %qD requires the"
  2932. " %<-mconstant-cfstrings%> flag", fndecl);
  2933. return error_mark_node;
  2934. }
  2935. if (n_args != 1)
  2936. {
  2937. error ("built-in function %qD takes one argument only", fndecl);
  2938. return error_mark_node;
  2939. }
  2940. return darwin_build_constant_cfstring (*argp);
  2941. }
  2942. return NULL_TREE;
  2943. }
  2944. void
  2945. darwin_rename_builtins (void)
  2946. {
  2947. /* The system ___divdc3 routine in libSystem on darwin10 is not
  2948. accurate to 1ulp, ours is, so we avoid ever using the system name
  2949. for this routine and instead install a non-conflicting name that
  2950. is accurate.
  2951. When -ffast-math or -funsafe-math-optimizations is given, we can
  2952. use the faster version. */
  2953. if (!flag_unsafe_math_optimizations)
  2954. {
  2955. enum built_in_function dcode
  2956. = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
  2957. + DCmode - MIN_MODE_COMPLEX_FLOAT);
  2958. tree fn = builtin_decl_explicit (dcode);
  2959. /* Fortran and c call TARGET_INIT_BUILTINS and
  2960. TARGET_INIT_LIBFUNCS at different times, so we have to put a
  2961. call into each to ensure that at least one of them is called
  2962. after build_common_builtin_nodes. A better fix is to add a
  2963. new hook to run after build_common_builtin_nodes runs. */
  2964. if (fn)
  2965. set_user_assembler_name (fn, "___ieee_divdc3");
  2966. fn = builtin_decl_implicit (dcode);
  2967. if (fn)
  2968. set_user_assembler_name (fn, "___ieee_divdc3");
  2969. }
  2970. }
  2971. bool
  2972. darwin_libc_has_function (enum function_class fn_class)
  2973. {
  2974. if (fn_class == function_sincos)
  2975. return false;
  2976. if (fn_class == function_c99_math_complex
  2977. || fn_class == function_c99_misc)
  2978. return (TARGET_64BIT
  2979. || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
  2980. return true;
  2981. }
  2982. hashval_t
  2983. cfstring_hasher::hash (cfstring_descriptor *ptr)
  2984. {
  2985. tree str = ptr->literal;
  2986. const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
  2987. int i, len = TREE_STRING_LENGTH (str);
  2988. hashval_t h = len;
  2989. for (i = 0; i < len; i++)
  2990. h = ((h * 613) + p[i]);
  2991. return h;
  2992. }
  2993. bool
  2994. cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
  2995. {
  2996. tree str1 = ptr1->literal;
  2997. tree str2 = ptr2->literal;
  2998. int len1 = TREE_STRING_LENGTH (str1);
  2999. return (len1 == TREE_STRING_LENGTH (str2)
  3000. && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
  3001. len1));
  3002. }
  3003. tree
  3004. darwin_build_constant_cfstring (tree str)
  3005. {
  3006. struct cfstring_descriptor *desc, key;
  3007. tree addr;
  3008. if (!str)
  3009. {
  3010. error ("CFString literal is missing");
  3011. return error_mark_node;
  3012. }
  3013. STRIP_NOPS (str);
  3014. if (TREE_CODE (str) == ADDR_EXPR)
  3015. str = TREE_OPERAND (str, 0);
  3016. if (TREE_CODE (str) != STRING_CST)
  3017. {
  3018. error ("CFString literal expression is not a string constant");
  3019. return error_mark_node;
  3020. }
  3021. /* Perhaps we already constructed a constant CFString just like this one? */
  3022. key.literal = str;
  3023. cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
  3024. desc = *loc;
  3025. if (!desc)
  3026. {
  3027. tree var, constructor, field;
  3028. vec<constructor_elt, va_gc> *v = NULL;
  3029. int length = TREE_STRING_LENGTH (str) - 1;
  3030. if (darwin_warn_nonportable_cfstrings)
  3031. {
  3032. const char *s = TREE_STRING_POINTER (str);
  3033. int l = 0;
  3034. for (l = 0; l < length; l++)
  3035. if (!s[l] || !isascii (s[l]))
  3036. {
  3037. warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
  3038. s[l] ? "non-ASCII character" : "embedded NUL");
  3039. break;
  3040. }
  3041. }
  3042. *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
  3043. desc->literal = str;
  3044. /* isa *. */
  3045. field = TYPE_FIELDS (ccfstring_type_node);
  3046. CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
  3047. build1 (ADDR_EXPR, TREE_TYPE (field),
  3048. cfstring_class_reference));
  3049. /* flags */
  3050. field = DECL_CHAIN (field);
  3051. CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
  3052. build_int_cst (TREE_TYPE (field), 0x000007c8));
  3053. /* string *. */
  3054. field = DECL_CHAIN (field);
  3055. CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
  3056. build1 (ADDR_EXPR, TREE_TYPE (field), str));
  3057. /* length */
  3058. field = DECL_CHAIN (field);
  3059. CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
  3060. build_int_cst (TREE_TYPE (field), length));
  3061. constructor = build_constructor (ccfstring_type_node, v);
  3062. TREE_READONLY (constructor) = 1;
  3063. TREE_CONSTANT (constructor) = 1;
  3064. TREE_STATIC (constructor) = 1;
  3065. /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
  3066. to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
  3067. being built without any knowledge of C++ tree accessors; hence, we shall
  3068. use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
  3069. if (darwin_running_cxx)
  3070. TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
  3071. /* Create an anonymous global variable for this CFString. */
  3072. var = build_decl (input_location, CONST_DECL,
  3073. NULL, TREE_TYPE (constructor));
  3074. DECL_ARTIFICIAL (var) = 1;
  3075. TREE_STATIC (var) = 1;
  3076. DECL_INITIAL (var) = constructor;
  3077. /* FIXME: This should use a translation_unit_decl to indicate file scope. */
  3078. DECL_CONTEXT (var) = NULL_TREE;
  3079. desc->constructor = var;
  3080. }
  3081. addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
  3082. TREE_CONSTANT (addr) = 1;
  3083. return addr;
  3084. }
  3085. bool
  3086. darwin_cfstring_p (tree str)
  3087. {
  3088. struct cfstring_descriptor key;
  3089. if (!str)
  3090. return false;
  3091. STRIP_NOPS (str);
  3092. if (TREE_CODE (str) == ADDR_EXPR)
  3093. str = TREE_OPERAND (str, 0);
  3094. if (TREE_CODE (str) != STRING_CST)
  3095. return false;
  3096. key.literal = str;
  3097. cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
  3098. if (loc)
  3099. return true;
  3100. return false;
  3101. }
  3102. void
  3103. darwin_enter_string_into_cfstring_table (tree str)
  3104. {
  3105. struct cfstring_descriptor key;
  3106. key.literal = str;
  3107. cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
  3108. if (!*loc)
  3109. {
  3110. *loc = ggc_cleared_alloc<cfstring_descriptor> ();
  3111. ((struct cfstring_descriptor *)*loc)->literal = str;
  3112. }
  3113. }
  3114. /* Choose named function section based on its frequency. */
  3115. section *
  3116. darwin_function_section (tree decl, enum node_frequency freq,
  3117. bool startup, bool exit)
  3118. {
  3119. /* Decide if we need to put this in a coalescable section. */
  3120. bool weak = (decl
  3121. && DECL_WEAK (decl)
  3122. && (!DECL_ATTRIBUTES (decl)
  3123. || !lookup_attribute ("weak_import",
  3124. DECL_ATTRIBUTES (decl))));
  3125. /* If there is a specified section name, we should not be trying to
  3126. override. */
  3127. if (decl && DECL_SECTION_NAME (decl) != NULL)
  3128. return get_named_section (decl, NULL, 0);
  3129. /* We always put unlikely executed stuff in the cold section. */
  3130. if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
  3131. return (weak) ? darwin_sections[text_cold_coal_section]
  3132. : darwin_sections[text_cold_section];
  3133. /* If we have LTO *and* feedback information, then let LTO handle
  3134. the function ordering, it makes a better job (for normal, hot,
  3135. startup and exit - hence the bailout for cold above). */
  3136. if (in_lto_p && flag_profile_values)
  3137. goto default_function_sections;
  3138. /* Non-cold startup code should go to startup subsection. */
  3139. if (startup)
  3140. return (weak) ? darwin_sections[text_startup_coal_section]
  3141. : darwin_sections[text_startup_section];
  3142. /* Similarly for exit. */
  3143. if (exit)
  3144. return (weak) ? darwin_sections[text_exit_coal_section]
  3145. : darwin_sections[text_exit_section];
  3146. /* Place hot code. */
  3147. if (freq == NODE_FREQUENCY_HOT)
  3148. return (weak) ? darwin_sections[text_hot_coal_section]
  3149. : darwin_sections[text_hot_section];
  3150. /* Otherwise, default to the 'normal' non-reordered sections. */
  3151. default_function_sections:
  3152. return (weak) ? darwin_sections[text_coal_section]
  3153. : text_section;
  3154. }
  3155. /* When a function is partitioned between sections, we need to insert a label
  3156. at the start of each new chunk - so that it may become a valid 'atom' for
  3157. eh and debug purposes. Without this the linker will emit warnings if one
  3158. tries to add line location information (since the switched fragment will
  3159. be anonymous). */
  3160. void
  3161. darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
  3162. {
  3163. char buf[128];
  3164. snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
  3165. IDENTIFIER_POINTER (DECL_NAME (decl)));
  3166. /* Make sure we pick up all the relevant quotes etc. */
  3167. assemble_name_raw (fp, (const char *) buf);
  3168. fputs (":\n", fp);
  3169. }
  3170. #include "gt-darwin.h"