symbols.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  1. /* symbols.c -symbol table-
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
  17. #include "as.h"
  18. #include "safe-ctype.h"
  19. #include "obstack.h" /* For "symbols.h" */
  20. #include "subsegs.h"
  21. #include "write.h"
  22. #include <limits.h>
  23. #ifndef CHAR_BIT
  24. #define CHAR_BIT 8
  25. #endif
  26. struct symbol_flags
  27. {
  28. /* Whether the symbol is a local_symbol. */
  29. unsigned int local_symbol : 1;
  30. /* Weather symbol has been written. */
  31. unsigned int written : 1;
  32. /* Whether symbol value has been completely resolved (used during
  33. final pass over symbol table). */
  34. unsigned int resolved : 1;
  35. /* Whether the symbol value is currently being resolved (used to
  36. detect loops in symbol dependencies). */
  37. unsigned int resolving : 1;
  38. /* Whether the symbol value is used in a reloc. This is used to
  39. ensure that symbols used in relocs are written out, even if they
  40. are local and would otherwise not be. */
  41. unsigned int used_in_reloc : 1;
  42. /* Whether the symbol is used as an operand or in an expression.
  43. NOTE: Not all the backends keep this information accurate;
  44. backends which use this bit are responsible for setting it when
  45. a symbol is used in backend routines. */
  46. unsigned int used : 1;
  47. /* Whether the symbol can be re-defined. */
  48. unsigned int volatil : 1;
  49. /* Whether the symbol is a forward reference, and whether such has
  50. been determined. */
  51. unsigned int forward_ref : 1;
  52. unsigned int forward_resolved : 1;
  53. /* This is set if the symbol is defined in an MRI common section.
  54. We handle such sections as single common symbols, so symbols
  55. defined within them must be treated specially by the relocation
  56. routines. */
  57. unsigned int mri_common : 1;
  58. /* This is set if the symbol is set with a .weakref directive. */
  59. unsigned int weakrefr : 1;
  60. /* This is set when the symbol is referenced as part of a .weakref
  61. directive, but only if the symbol was not in the symbol table
  62. before. It is cleared as soon as any direct reference to the
  63. symbol is present. */
  64. unsigned int weakrefd : 1;
  65. /* Whether the symbol has been marked to be removed by a .symver
  66. directive. */
  67. unsigned int removed : 1;
  68. /* Set when a warning about the symbol containing multibyte characters
  69. is generated. */
  70. unsigned int multibyte_warned : 1;
  71. };
  72. /* A pointer in the symbol may point to either a complete symbol
  73. (struct symbol below) or to a local symbol (struct local_symbol
  74. defined here). The symbol code can detect the case by examining
  75. the first field which is present in both structs.
  76. We do this because we ordinarily only need a small amount of
  77. information for a local symbol. The symbol table takes up a lot of
  78. space, and storing less information for a local symbol can make a
  79. big difference in assembler memory usage when assembling a large
  80. file. */
  81. struct local_symbol
  82. {
  83. /* Symbol flags. Only local_symbol and resolved are relevant. */
  84. struct symbol_flags flags;
  85. /* Hash value calculated from name. */
  86. hashval_t hash;
  87. /* The symbol name. */
  88. const char *name;
  89. /* The symbol frag. */
  90. fragS *frag;
  91. /* The symbol section. */
  92. asection *section;
  93. /* The value of the symbol. */
  94. valueT value;
  95. };
  96. /* The information we keep for a symbol. The symbol table holds
  97. pointers both to this and to local_symbol structures. The first
  98. three fields must be identical to struct local_symbol, and the size
  99. should be the same as or smaller than struct local_symbol.
  100. Fields that don't fit go to an extension structure. */
  101. struct symbol
  102. {
  103. /* Symbol flags. */
  104. struct symbol_flags flags;
  105. /* Hash value calculated from name. */
  106. hashval_t hash;
  107. /* The symbol name. */
  108. const char *name;
  109. /* Pointer to the frag this symbol is attached to, if any.
  110. Otherwise, NULL. */
  111. fragS *frag;
  112. /* BFD symbol */
  113. asymbol *bsym;
  114. /* Extra symbol fields that won't fit. */
  115. struct xsymbol *x;
  116. };
  117. /* Extra fields to make up a full symbol. */
  118. struct xsymbol
  119. {
  120. /* The value of the symbol. */
  121. expressionS value;
  122. /* Forwards and backwards chain pointers. */
  123. struct symbol *next;
  124. struct symbol *previous;
  125. #ifdef OBJ_SYMFIELD_TYPE
  126. OBJ_SYMFIELD_TYPE obj;
  127. #endif
  128. #ifdef TC_SYMFIELD_TYPE
  129. TC_SYMFIELD_TYPE tc;
  130. #endif
  131. };
  132. typedef union symbol_entry
  133. {
  134. struct local_symbol lsy;
  135. struct symbol sy;
  136. } symbol_entry_t;
  137. /* Hash function for a symbol_entry. */
  138. static hashval_t
  139. hash_symbol_entry (const void *e)
  140. {
  141. symbol_entry_t *entry = (symbol_entry_t *) e;
  142. if (entry->sy.hash == 0)
  143. entry->sy.hash = htab_hash_string (entry->sy.name);
  144. return entry->sy.hash;
  145. }
  146. /* Equality function for a symbol_entry. */
  147. static int
  148. eq_symbol_entry (const void *a, const void *b)
  149. {
  150. const symbol_entry_t *ea = (const symbol_entry_t *) a;
  151. const symbol_entry_t *eb = (const symbol_entry_t *) b;
  152. return (ea->sy.hash == eb->sy.hash
  153. && strcmp (ea->sy.name, eb->sy.name) == 0);
  154. }
  155. static void *
  156. symbol_entry_find (htab_t table, const char *name)
  157. {
  158. hashval_t hash = htab_hash_string (name);
  159. symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  160. hash, name, 0, 0, 0 } };
  161. return htab_find_with_hash (table, &needle, hash);
  162. }
  163. /* This is non-zero if symbols are case sensitive, which is the
  164. default. */
  165. int symbols_case_sensitive = 1;
  166. #ifndef WORKING_DOT_WORD
  167. extern int new_broken_words;
  168. #endif
  169. static htab_t sy_hash;
  170. /* Below are commented in "symbols.h". */
  171. symbolS *symbol_rootP;
  172. symbolS *symbol_lastP;
  173. symbolS abs_symbol;
  174. struct xsymbol abs_symbol_x;
  175. symbolS dot_symbol;
  176. struct xsymbol dot_symbol_x;
  177. #ifdef DEBUG_SYMS
  178. #define debug_verify_symchain verify_symbol_chain
  179. #else
  180. #define debug_verify_symchain(root, last) ((void) 0)
  181. #endif
  182. #define DOLLAR_LABEL_CHAR '\001'
  183. #define LOCAL_LABEL_CHAR '\002'
  184. #ifndef TC_LABEL_IS_LOCAL
  185. #define TC_LABEL_IS_LOCAL(name) 0
  186. #endif
  187. struct obstack notes;
  188. #ifdef TE_PE
  189. /* The name of an external symbol which is
  190. used to make weak PE symbol names unique. */
  191. const char * an_external_name;
  192. #endif
  193. /* Return a pointer to a new symbol. Die if we can't make a new
  194. symbol. Fill in the symbol's values. Add symbol to end of symbol
  195. chain.
  196. This function should be called in the general case of creating a
  197. symbol. However, if the output file symbol table has already been
  198. set, and you are certain that this symbol won't be wanted in the
  199. output file, you can call symbol_create. */
  200. symbolS *
  201. symbol_new (const char *name, segT segment, fragS *frag, valueT valu)
  202. {
  203. symbolS *symbolP = symbol_create (name, segment, frag, valu);
  204. /* Link to end of symbol chain. */
  205. symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
  206. return symbolP;
  207. }
  208. /* Save a symbol name on a permanent obstack, and convert it according
  209. to the object file format. */
  210. static const char *
  211. save_symbol_name (const char *name)
  212. {
  213. size_t name_length;
  214. char *ret;
  215. gas_assert (name != NULL);
  216. name_length = strlen (name) + 1; /* +1 for \0. */
  217. obstack_grow (&notes, name, name_length);
  218. ret = (char *) obstack_finish (&notes);
  219. #ifdef tc_canonicalize_symbol_name
  220. ret = tc_canonicalize_symbol_name (ret);
  221. #endif
  222. if (! symbols_case_sensitive)
  223. {
  224. char *s;
  225. for (s = ret; *s != '\0'; s++)
  226. *s = TOUPPER (*s);
  227. }
  228. return ret;
  229. }
  230. static void
  231. symbol_init (symbolS *symbolP, const char *name, asection *sec,
  232. fragS *frag, valueT valu)
  233. {
  234. symbolP->frag = frag;
  235. symbolP->bsym = bfd_make_empty_symbol (stdoutput);
  236. if (symbolP->bsym == NULL)
  237. as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
  238. symbolP->bsym->name = name;
  239. symbolP->bsym->section = sec;
  240. if (multibyte_handling == multibyte_warn_syms
  241. && ! symbolP->flags.local_symbol
  242. && sec != undefined_section
  243. && ! symbolP->flags.multibyte_warned
  244. && scan_for_multibyte_characters ((const unsigned char *) name,
  245. (const unsigned char *) name + strlen (name),
  246. false /* Do not warn. */))
  247. {
  248. as_warn (_("symbol '%s' contains multibyte characters"), name);
  249. symbolP->flags.multibyte_warned = 1;
  250. }
  251. S_SET_VALUE (symbolP, valu);
  252. symbol_clear_list_pointers (symbolP);
  253. obj_symbol_new_hook (symbolP);
  254. #ifdef tc_symbol_new_hook
  255. tc_symbol_new_hook (symbolP);
  256. #endif
  257. }
  258. /* Create a symbol. NAME is copied, the caller can destroy/modify. */
  259. symbolS *
  260. symbol_create (const char *name, segT segment, fragS *frag, valueT valu)
  261. {
  262. const char *preserved_copy_of_name;
  263. symbolS *symbolP;
  264. size_t size;
  265. preserved_copy_of_name = save_symbol_name (name);
  266. size = sizeof (symbolS) + sizeof (struct xsymbol);
  267. symbolP = (symbolS *) obstack_alloc (&notes, size);
  268. /* symbol must be born in some fixed state. This seems as good as any. */
  269. memset (symbolP, 0, size);
  270. symbolP->name = preserved_copy_of_name;
  271. symbolP->x = (struct xsymbol *) (symbolP + 1);
  272. symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
  273. return symbolP;
  274. }
  275. /* Local symbol support. If we can get away with it, we keep only a
  276. small amount of information for local symbols. */
  277. /* Used for statistics. */
  278. static unsigned long local_symbol_count;
  279. static unsigned long local_symbol_conversion_count;
  280. /* Create a local symbol and insert it into the local hash table. */
  281. struct local_symbol *
  282. local_symbol_make (const char *name, segT section, fragS *frag, valueT val)
  283. {
  284. const char *name_copy;
  285. struct local_symbol *ret;
  286. struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
  287. ++local_symbol_count;
  288. name_copy = save_symbol_name (name);
  289. ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
  290. ret->flags = flags;
  291. ret->hash = 0;
  292. ret->name = name_copy;
  293. ret->frag = frag;
  294. ret->section = section;
  295. ret->value = val;
  296. htab_insert (sy_hash, ret, 1);
  297. return ret;
  298. }
  299. /* Convert a local symbol into a real symbol. */
  300. static symbolS *
  301. local_symbol_convert (void *sym)
  302. {
  303. symbol_entry_t *ent = (symbol_entry_t *) sym;
  304. struct xsymbol *xtra;
  305. valueT val;
  306. gas_assert (ent->lsy.flags.local_symbol);
  307. ++local_symbol_conversion_count;
  308. xtra = (struct xsymbol *) obstack_alloc (&notes, sizeof (*xtra));
  309. memset (xtra, 0, sizeof (*xtra));
  310. val = ent->lsy.value;
  311. ent->sy.x = xtra;
  312. /* Local symbols are always either defined or used. */
  313. ent->sy.flags.used = 1;
  314. ent->sy.flags.local_symbol = 0;
  315. symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
  316. symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
  317. return &ent->sy;
  318. }
  319. static void
  320. define_sym_at_dot (symbolS *symbolP)
  321. {
  322. symbolP->frag = frag_now;
  323. S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
  324. S_SET_SEGMENT (symbolP, now_seg);
  325. }
  326. /* We have just seen "<name>:".
  327. Creates a struct symbol unless it already exists.
  328. Gripes if we are redefining a symbol incompatibly (and ignores it). */
  329. symbolS *
  330. colon (/* Just seen "x:" - rattle symbols & frags. */
  331. const char *sym_name /* Symbol name, as a canonical string. */
  332. /* We copy this string: OK to alter later. */)
  333. {
  334. symbolS *symbolP; /* Symbol we are working with. */
  335. /* Sun local labels go out of scope whenever a non-local symbol is
  336. defined. */
  337. if (LOCAL_LABELS_DOLLAR
  338. && !bfd_is_local_label_name (stdoutput, sym_name))
  339. dollar_label_clear ();
  340. #ifndef WORKING_DOT_WORD
  341. if (new_broken_words)
  342. {
  343. struct broken_word *a;
  344. int possible_bytes;
  345. fragS *frag_tmp;
  346. char *frag_opcode;
  347. if (now_seg == absolute_section)
  348. {
  349. as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
  350. return NULL;
  351. }
  352. possible_bytes = (md_short_jump_size
  353. + new_broken_words * md_long_jump_size);
  354. frag_tmp = frag_now;
  355. frag_opcode = frag_var (rs_broken_word,
  356. possible_bytes,
  357. possible_bytes,
  358. (relax_substateT) 0,
  359. (symbolS *) broken_words,
  360. (offsetT) 0,
  361. NULL);
  362. /* We want to store the pointer to where to insert the jump
  363. table in the fr_opcode of the rs_broken_word frag. This
  364. requires a little hackery. */
  365. while (frag_tmp
  366. && (frag_tmp->fr_type != rs_broken_word
  367. || frag_tmp->fr_opcode))
  368. frag_tmp = frag_tmp->fr_next;
  369. know (frag_tmp);
  370. frag_tmp->fr_opcode = frag_opcode;
  371. new_broken_words = 0;
  372. for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
  373. a->dispfrag = frag_tmp;
  374. }
  375. #endif /* WORKING_DOT_WORD */
  376. #ifdef obj_frob_colon
  377. obj_frob_colon (sym_name);
  378. #endif
  379. if ((symbolP = symbol_find (sym_name)) != 0)
  380. {
  381. S_CLEAR_WEAKREFR (symbolP);
  382. #ifdef RESOLVE_SYMBOL_REDEFINITION
  383. if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
  384. return symbolP;
  385. #endif
  386. /* Now check for undefined symbols. */
  387. if (symbolP->flags.local_symbol)
  388. {
  389. struct local_symbol *locsym = (struct local_symbol *) symbolP;
  390. if (locsym->section != undefined_section
  391. && (locsym->frag != frag_now
  392. || locsym->section != now_seg
  393. || locsym->value != frag_now_fix ()))
  394. {
  395. as_bad (_("symbol `%s' is already defined"), sym_name);
  396. return symbolP;
  397. }
  398. locsym->section = now_seg;
  399. locsym->frag = frag_now;
  400. locsym->value = frag_now_fix ();
  401. }
  402. else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
  403. || S_IS_COMMON (symbolP)
  404. || S_IS_VOLATILE (symbolP))
  405. {
  406. if (S_IS_VOLATILE (symbolP))
  407. {
  408. symbolP = symbol_clone (symbolP, 1);
  409. S_SET_VALUE (symbolP, 0);
  410. S_CLEAR_VOLATILE (symbolP);
  411. }
  412. if (S_GET_VALUE (symbolP) == 0)
  413. {
  414. define_sym_at_dot (symbolP);
  415. #ifdef N_UNDF
  416. know (N_UNDF == 0);
  417. #endif /* if we have one, it better be zero. */
  418. }
  419. else
  420. {
  421. /* There are still several cases to check:
  422. A .comm/.lcomm symbol being redefined as initialized
  423. data is OK
  424. A .comm/.lcomm symbol being redefined with a larger
  425. size is also OK
  426. This only used to be allowed on VMS gas, but Sun cc
  427. on the sparc also depends on it. */
  428. if (((!S_IS_DEBUG (symbolP)
  429. && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
  430. && S_IS_EXTERNAL (symbolP))
  431. || S_GET_SEGMENT (symbolP) == bss_section)
  432. && (now_seg == data_section
  433. || now_seg == bss_section
  434. || now_seg == S_GET_SEGMENT (symbolP)))
  435. {
  436. /* Select which of the 2 cases this is. */
  437. if (now_seg != data_section)
  438. {
  439. /* New .comm for prev .comm symbol.
  440. If the new size is larger we just change its
  441. value. If the new size is smaller, we ignore
  442. this symbol. */
  443. if (S_GET_VALUE (symbolP)
  444. < ((unsigned) frag_now_fix ()))
  445. {
  446. S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
  447. }
  448. }
  449. else
  450. {
  451. /* It is a .comm/.lcomm being converted to initialized
  452. data. */
  453. define_sym_at_dot (symbolP);
  454. }
  455. }
  456. else
  457. {
  458. #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
  459. static const char *od_buf = "";
  460. #else
  461. char od_buf[100];
  462. od_buf[0] = '\0';
  463. if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
  464. sprintf (od_buf, "%d.%d.",
  465. S_GET_OTHER (symbolP),
  466. S_GET_DESC (symbolP));
  467. #endif
  468. as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
  469. sym_name,
  470. segment_name (S_GET_SEGMENT (symbolP)),
  471. od_buf,
  472. (long) S_GET_VALUE (symbolP));
  473. }
  474. } /* if the undefined symbol has no value */
  475. }
  476. else
  477. {
  478. /* Don't blow up if the definition is the same. */
  479. if (!(frag_now == symbolP->frag
  480. && S_GET_VALUE (symbolP) == frag_now_fix ()
  481. && S_GET_SEGMENT (symbolP) == now_seg))
  482. {
  483. as_bad (_("symbol `%s' is already defined"), sym_name);
  484. symbolP = symbol_clone (symbolP, 0);
  485. define_sym_at_dot (symbolP);
  486. }
  487. }
  488. }
  489. else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
  490. {
  491. symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
  492. frag_now_fix ());
  493. }
  494. else
  495. {
  496. symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
  497. symbol_table_insert (symbolP);
  498. }
  499. if (mri_common_symbol != NULL)
  500. {
  501. /* This symbol is actually being defined within an MRI common
  502. section. This requires special handling. */
  503. if (symbolP->flags.local_symbol)
  504. symbolP = local_symbol_convert (symbolP);
  505. symbolP->x->value.X_op = O_symbol;
  506. symbolP->x->value.X_add_symbol = mri_common_symbol;
  507. symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
  508. symbolP->frag = &zero_address_frag;
  509. S_SET_SEGMENT (symbolP, expr_section);
  510. symbolP->flags.mri_common = 1;
  511. }
  512. #ifdef tc_frob_label
  513. tc_frob_label (symbolP);
  514. #endif
  515. #ifdef obj_frob_label
  516. obj_frob_label (symbolP);
  517. #endif
  518. return symbolP;
  519. }
  520. /* Die if we can't insert the symbol. */
  521. void
  522. symbol_table_insert (symbolS *symbolP)
  523. {
  524. know (symbolP);
  525. htab_insert (sy_hash, symbolP, 1);
  526. }
  527. /* If a symbol name does not exist, create it as undefined, and insert
  528. it into the symbol table. Return a pointer to it. */
  529. symbolS *
  530. symbol_find_or_make (const char *name)
  531. {
  532. symbolS *symbolP;
  533. symbolP = symbol_find (name);
  534. if (symbolP == NULL)
  535. {
  536. if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
  537. {
  538. symbolP = md_undefined_symbol ((char *) name);
  539. if (symbolP != NULL)
  540. return symbolP;
  541. symbolP = (symbolS *) local_symbol_make (name, undefined_section,
  542. &zero_address_frag, 0);
  543. return symbolP;
  544. }
  545. symbolP = symbol_make (name);
  546. symbol_table_insert (symbolP);
  547. } /* if symbol wasn't found */
  548. return (symbolP);
  549. }
  550. symbolS *
  551. symbol_make (const char *name)
  552. {
  553. symbolS *symbolP;
  554. /* Let the machine description default it, e.g. for register names. */
  555. symbolP = md_undefined_symbol ((char *) name);
  556. if (!symbolP)
  557. symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
  558. return (symbolP);
  559. }
  560. symbolS *
  561. symbol_clone (symbolS *orgsymP, int replace)
  562. {
  563. symbolS *newsymP;
  564. asymbol *bsymorg, *bsymnew;
  565. /* Make sure we never clone the dot special symbol. */
  566. gas_assert (orgsymP != &dot_symbol);
  567. /* When cloning a local symbol it isn't absolutely necessary to
  568. convert the original, but converting makes the code much
  569. simpler to cover this unexpected case. As of 2020-08-21
  570. symbol_clone won't be called on a local symbol. */
  571. if (orgsymP->flags.local_symbol)
  572. orgsymP = local_symbol_convert (orgsymP);
  573. bsymorg = orgsymP->bsym;
  574. newsymP = (symbolS *) obstack_alloc (&notes, (sizeof (symbolS)
  575. + sizeof (struct xsymbol)));
  576. *newsymP = *orgsymP;
  577. newsymP->x = (struct xsymbol *) (newsymP + 1);
  578. *newsymP->x = *orgsymP->x;
  579. bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
  580. if (bsymnew == NULL)
  581. as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
  582. newsymP->bsym = bsymnew;
  583. bsymnew->name = bsymorg->name;
  584. bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
  585. bsymnew->section = bsymorg->section;
  586. bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
  587. bfd_asymbol_bfd (bsymnew), bsymnew);
  588. #ifdef obj_symbol_clone_hook
  589. obj_symbol_clone_hook (newsymP, orgsymP);
  590. #endif
  591. #ifdef tc_symbol_clone_hook
  592. tc_symbol_clone_hook (newsymP, orgsymP);
  593. #endif
  594. if (replace)
  595. {
  596. if (symbol_rootP == orgsymP)
  597. symbol_rootP = newsymP;
  598. else if (orgsymP->x->previous)
  599. {
  600. orgsymP->x->previous->x->next = newsymP;
  601. orgsymP->x->previous = NULL;
  602. }
  603. if (symbol_lastP == orgsymP)
  604. symbol_lastP = newsymP;
  605. else if (orgsymP->x->next)
  606. orgsymP->x->next->x->previous = newsymP;
  607. /* Symbols that won't be output can't be external. */
  608. S_CLEAR_EXTERNAL (orgsymP);
  609. orgsymP->x->previous = orgsymP->x->next = orgsymP;
  610. debug_verify_symchain (symbol_rootP, symbol_lastP);
  611. symbol_table_insert (newsymP);
  612. }
  613. else
  614. {
  615. /* Symbols that won't be output can't be external. */
  616. S_CLEAR_EXTERNAL (newsymP);
  617. newsymP->x->previous = newsymP->x->next = newsymP;
  618. }
  619. return newsymP;
  620. }
  621. /* Referenced symbols, if they are forward references, need to be cloned
  622. (without replacing the original) so that the value of the referenced
  623. symbols at the point of use is saved by the clone. */
  624. #undef symbol_clone_if_forward_ref
  625. symbolS *
  626. symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
  627. {
  628. if (symbolP
  629. && !symbolP->flags.local_symbol
  630. && !symbolP->flags.forward_resolved)
  631. {
  632. symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
  633. symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
  634. symbolS *add_symbol = orig_add_symbol;
  635. symbolS *op_symbol = orig_op_symbol;
  636. if (symbolP->flags.forward_ref)
  637. is_forward = 1;
  638. if (is_forward)
  639. {
  640. /* assign_symbol() clones volatile symbols; pre-existing expressions
  641. hold references to the original instance, but want the current
  642. value. Just repeat the lookup. */
  643. if (add_symbol && S_IS_VOLATILE (add_symbol))
  644. add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
  645. if (op_symbol && S_IS_VOLATILE (op_symbol))
  646. op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
  647. }
  648. /* Re-using resolving here, as this routine cannot get called from
  649. symbol resolution code. */
  650. if ((symbolP->bsym->section == expr_section
  651. || symbolP->flags.forward_ref)
  652. && !symbolP->flags.resolving)
  653. {
  654. symbolP->flags.resolving = 1;
  655. add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
  656. op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
  657. symbolP->flags.resolving = 0;
  658. }
  659. if (symbolP->flags.forward_ref
  660. || add_symbol != orig_add_symbol
  661. || op_symbol != orig_op_symbol)
  662. {
  663. if (symbolP != &dot_symbol)
  664. {
  665. symbolP = symbol_clone (symbolP, 0);
  666. symbolP->flags.resolving = 0;
  667. }
  668. else
  669. {
  670. symbolP = symbol_temp_new_now ();
  671. #ifdef tc_new_dot_label
  672. tc_new_dot_label (symbolP);
  673. #endif
  674. }
  675. }
  676. symbolP->x->value.X_add_symbol = add_symbol;
  677. symbolP->x->value.X_op_symbol = op_symbol;
  678. symbolP->flags.forward_resolved = 1;
  679. }
  680. return symbolP;
  681. }
  682. symbolS *
  683. symbol_temp_new (segT seg, fragS *frag, valueT ofs)
  684. {
  685. return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
  686. }
  687. symbolS *
  688. symbol_temp_new_now (void)
  689. {
  690. return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
  691. }
  692. symbolS *
  693. symbol_temp_new_now_octets (void)
  694. {
  695. return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
  696. }
  697. symbolS *
  698. symbol_temp_make (void)
  699. {
  700. return symbol_make (FAKE_LABEL_NAME);
  701. }
  702. /* Implement symbol table lookup.
  703. In: A symbol's name as a string: '\0' can't be part of a symbol name.
  704. Out: NULL if the name was not in the symbol table, else the address
  705. of a struct symbol associated with that name. */
  706. symbolS *
  707. symbol_find_exact (const char *name)
  708. {
  709. return symbol_find_exact_noref (name, 0);
  710. }
  711. symbolS *
  712. symbol_find_exact_noref (const char *name, int noref)
  713. {
  714. symbolS *sym = symbol_entry_find (sy_hash, name);
  715. /* Any references to the symbol, except for the reference in
  716. .weakref, must clear this flag, such that the symbol does not
  717. turn into a weak symbol. Note that we don't have to handle the
  718. local_symbol case, since a weakrefd is always promoted out of the
  719. local_symbol table when it is turned into a weak symbol. */
  720. if (sym && ! noref)
  721. S_CLEAR_WEAKREFD (sym);
  722. return sym;
  723. }
  724. symbolS *
  725. symbol_find (const char *name)
  726. {
  727. return symbol_find_noref (name, 0);
  728. }
  729. symbolS *
  730. symbol_find_noref (const char *name, int noref)
  731. {
  732. symbolS * result;
  733. char * copy = NULL;
  734. #ifdef tc_canonicalize_symbol_name
  735. {
  736. copy = xstrdup (name);
  737. name = tc_canonicalize_symbol_name (copy);
  738. }
  739. #endif
  740. if (! symbols_case_sensitive)
  741. {
  742. const char *orig;
  743. char *copy2 = NULL;
  744. unsigned char c;
  745. orig = name;
  746. if (copy != NULL)
  747. copy2 = copy;
  748. name = copy = XNEWVEC (char, strlen (name) + 1);
  749. while ((c = *orig++) != '\0')
  750. *copy++ = TOUPPER (c);
  751. *copy = '\0';
  752. free (copy2);
  753. copy = (char *) name;
  754. }
  755. result = symbol_find_exact_noref (name, noref);
  756. free (copy);
  757. return result;
  758. }
  759. /* Once upon a time, symbols were kept in a singly linked list. At
  760. least coff needs to be able to rearrange them from time to time, for
  761. which a doubly linked list is much more convenient. Loic did these
  762. as macros which seemed dangerous to me so they're now functions.
  763. xoxorich. */
  764. /* Link symbol ADDME after symbol TARGET in the chain. */
  765. void
  766. symbol_append (symbolS *addme, symbolS *target,
  767. symbolS **rootPP, symbolS **lastPP)
  768. {
  769. extern int symbol_table_frozen;
  770. if (symbol_table_frozen)
  771. abort ();
  772. if (addme->flags.local_symbol)
  773. abort ();
  774. if (target != NULL && target->flags.local_symbol)
  775. abort ();
  776. if (target == NULL)
  777. {
  778. know (*rootPP == NULL);
  779. know (*lastPP == NULL);
  780. addme->x->next = NULL;
  781. addme->x->previous = NULL;
  782. *rootPP = addme;
  783. *lastPP = addme;
  784. return;
  785. } /* if the list is empty */
  786. if (target->x->next != NULL)
  787. {
  788. target->x->next->x->previous = addme;
  789. }
  790. else
  791. {
  792. know (*lastPP == target);
  793. *lastPP = addme;
  794. } /* if we have a next */
  795. addme->x->next = target->x->next;
  796. target->x->next = addme;
  797. addme->x->previous = target;
  798. debug_verify_symchain (symbol_rootP, symbol_lastP);
  799. }
  800. /* Set the chain pointers of SYMBOL to null. */
  801. void
  802. symbol_clear_list_pointers (symbolS *symbolP)
  803. {
  804. if (symbolP->flags.local_symbol)
  805. abort ();
  806. symbolP->x->next = NULL;
  807. symbolP->x->previous = NULL;
  808. }
  809. /* Remove SYMBOLP from the list. */
  810. void
  811. symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
  812. {
  813. if (symbolP->flags.local_symbol)
  814. abort ();
  815. if (symbolP == *rootPP)
  816. {
  817. *rootPP = symbolP->x->next;
  818. } /* if it was the root */
  819. if (symbolP == *lastPP)
  820. {
  821. *lastPP = symbolP->x->previous;
  822. } /* if it was the tail */
  823. if (symbolP->x->next != NULL)
  824. {
  825. symbolP->x->next->x->previous = symbolP->x->previous;
  826. } /* if not last */
  827. if (symbolP->x->previous != NULL)
  828. {
  829. symbolP->x->previous->x->next = symbolP->x->next;
  830. } /* if not first */
  831. debug_verify_symchain (*rootPP, *lastPP);
  832. }
  833. /* Link symbol ADDME before symbol TARGET in the chain. */
  834. void
  835. symbol_insert (symbolS *addme, symbolS *target,
  836. symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
  837. {
  838. extern int symbol_table_frozen;
  839. if (symbol_table_frozen)
  840. abort ();
  841. if (addme->flags.local_symbol)
  842. abort ();
  843. if (target->flags.local_symbol)
  844. abort ();
  845. if (target->x->previous != NULL)
  846. {
  847. target->x->previous->x->next = addme;
  848. }
  849. else
  850. {
  851. know (*rootPP == target);
  852. *rootPP = addme;
  853. } /* if not first */
  854. addme->x->previous = target->x->previous;
  855. target->x->previous = addme;
  856. addme->x->next = target;
  857. debug_verify_symchain (*rootPP, *lastPP);
  858. }
  859. void
  860. verify_symbol_chain (symbolS *rootP, symbolS *lastP)
  861. {
  862. symbolS *symbolP = rootP;
  863. if (symbolP == NULL)
  864. return;
  865. for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
  866. {
  867. gas_assert (symbolP->bsym != NULL);
  868. gas_assert (symbolP->flags.local_symbol == 0);
  869. gas_assert (symbolP->x->next->x->previous == symbolP);
  870. }
  871. gas_assert (lastP == symbolP);
  872. }
  873. int
  874. symbol_on_chain (symbolS *s, symbolS *rootPP, symbolS *lastPP)
  875. {
  876. return (!s->flags.local_symbol
  877. && ((s->x->next != s
  878. && s->x->next != NULL
  879. && s->x->next->x->previous == s)
  880. || s == lastPP)
  881. && ((s->x->previous != s
  882. && s->x->previous != NULL
  883. && s->x->previous->x->next == s)
  884. || s == rootPP));
  885. }
  886. #ifdef OBJ_COMPLEX_RELC
  887. static int
  888. use_complex_relocs_for (symbolS * symp)
  889. {
  890. switch (symp->x->value.X_op)
  891. {
  892. case O_constant:
  893. return 0;
  894. case O_multiply:
  895. case O_divide:
  896. case O_modulus:
  897. case O_left_shift:
  898. case O_right_shift:
  899. case O_bit_inclusive_or:
  900. case O_bit_or_not:
  901. case O_bit_exclusive_or:
  902. case O_bit_and:
  903. case O_add:
  904. case O_subtract:
  905. case O_eq:
  906. case O_ne:
  907. case O_lt:
  908. case O_le:
  909. case O_ge:
  910. case O_gt:
  911. case O_logical_and:
  912. case O_logical_or:
  913. if ((S_IS_COMMON (symp->x->value.X_op_symbol)
  914. || S_IS_LOCAL (symp->x->value.X_op_symbol))
  915. && S_IS_DEFINED (symp->x->value.X_op_symbol)
  916. && S_GET_SEGMENT (symp->x->value.X_op_symbol) != expr_section)
  917. {
  918. case O_symbol:
  919. case O_symbol_rva:
  920. case O_uminus:
  921. case O_bit_not:
  922. case O_logical_not:
  923. if ((S_IS_COMMON (symp->x->value.X_add_symbol)
  924. || S_IS_LOCAL (symp->x->value.X_add_symbol))
  925. && S_IS_DEFINED (symp->x->value.X_add_symbol)
  926. && S_GET_SEGMENT (symp->x->value.X_add_symbol) != expr_section)
  927. return 0;
  928. }
  929. break;
  930. default:
  931. break;
  932. }
  933. return 1;
  934. }
  935. #endif
  936. static void
  937. report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
  938. {
  939. const char *file;
  940. unsigned int line;
  941. segT seg_left = left ? S_GET_SEGMENT (left) : 0;
  942. segT seg_right = S_GET_SEGMENT (right);
  943. const char *opname;
  944. switch (op)
  945. {
  946. default:
  947. abort ();
  948. return;
  949. case O_uminus: opname = "-"; break;
  950. case O_bit_not: opname = "~"; break;
  951. case O_logical_not: opname = "!"; break;
  952. case O_multiply: opname = "*"; break;
  953. case O_divide: opname = "/"; break;
  954. case O_modulus: opname = "%"; break;
  955. case O_left_shift: opname = "<<"; break;
  956. case O_right_shift: opname = ">>"; break;
  957. case O_bit_inclusive_or: opname = "|"; break;
  958. case O_bit_or_not: opname = "|~"; break;
  959. case O_bit_exclusive_or: opname = "^"; break;
  960. case O_bit_and: opname = "&"; break;
  961. case O_add: opname = "+"; break;
  962. case O_subtract: opname = "-"; break;
  963. case O_eq: opname = "=="; break;
  964. case O_ne: opname = "!="; break;
  965. case O_lt: opname = "<"; break;
  966. case O_le: opname = "<="; break;
  967. case O_ge: opname = ">="; break;
  968. case O_gt: opname = ">"; break;
  969. case O_logical_and: opname = "&&"; break;
  970. case O_logical_or: opname = "||"; break;
  971. }
  972. if (expr_symbol_where (symp, &file, &line))
  973. {
  974. if (left)
  975. as_bad_where (file, line,
  976. _("invalid operands (%s and %s sections) for `%s'"),
  977. seg_left->name, seg_right->name, opname);
  978. else
  979. as_bad_where (file, line,
  980. _("invalid operand (%s section) for `%s'"),
  981. seg_right->name, opname);
  982. }
  983. else
  984. {
  985. const char *sname = S_GET_NAME (symp);
  986. if (left)
  987. as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
  988. seg_left->name, seg_right->name, opname, sname);
  989. else
  990. as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
  991. seg_right->name, opname, sname);
  992. }
  993. }
  994. /* Resolve the value of a symbol. This is called during the final
  995. pass over the symbol table to resolve any symbols with complex
  996. values. */
  997. valueT
  998. resolve_symbol_value (symbolS *symp)
  999. {
  1000. int resolved;
  1001. valueT final_val;
  1002. segT final_seg;
  1003. if (symp->flags.local_symbol)
  1004. {
  1005. struct local_symbol *locsym = (struct local_symbol *) symp;
  1006. final_val = locsym->value;
  1007. if (locsym->flags.resolved)
  1008. return final_val;
  1009. /* Symbols whose section has SEC_ELF_OCTETS set,
  1010. resolve to octets instead of target bytes. */
  1011. if (locsym->section->flags & SEC_OCTETS)
  1012. final_val += locsym->frag->fr_address;
  1013. else
  1014. final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
  1015. if (finalize_syms)
  1016. {
  1017. locsym->value = final_val;
  1018. locsym->flags.resolved = 1;
  1019. }
  1020. return final_val;
  1021. }
  1022. if (symp->flags.resolved)
  1023. {
  1024. final_val = 0;
  1025. while (symp->x->value.X_op == O_symbol)
  1026. {
  1027. final_val += symp->x->value.X_add_number;
  1028. symp = symp->x->value.X_add_symbol;
  1029. if (symp->flags.local_symbol)
  1030. {
  1031. struct local_symbol *locsym = (struct local_symbol *) symp;
  1032. final_val += locsym->value;
  1033. return final_val;
  1034. }
  1035. if (!symp->flags.resolved)
  1036. return 0;
  1037. }
  1038. if (symp->x->value.X_op == O_constant)
  1039. final_val += symp->x->value.X_add_number;
  1040. else
  1041. final_val = 0;
  1042. return final_val;
  1043. }
  1044. resolved = 0;
  1045. final_seg = S_GET_SEGMENT (symp);
  1046. if (symp->flags.resolving)
  1047. {
  1048. if (finalize_syms)
  1049. as_bad (_("symbol definition loop encountered at `%s'"),
  1050. S_GET_NAME (symp));
  1051. final_val = 0;
  1052. resolved = 1;
  1053. }
  1054. #ifdef OBJ_COMPLEX_RELC
  1055. else if (final_seg == expr_section
  1056. && use_complex_relocs_for (symp))
  1057. {
  1058. symbolS * relc_symbol = NULL;
  1059. char * relc_symbol_name = NULL;
  1060. relc_symbol_name = symbol_relc_make_expr (& symp->x->value);
  1061. /* For debugging, print out conversion input & output. */
  1062. #ifdef DEBUG_SYMS
  1063. print_expr (& symp->x->value);
  1064. if (relc_symbol_name)
  1065. fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
  1066. #endif
  1067. if (relc_symbol_name != NULL)
  1068. relc_symbol = symbol_new (relc_symbol_name, undefined_section,
  1069. &zero_address_frag, 0);
  1070. if (relc_symbol == NULL)
  1071. {
  1072. as_bad (_("cannot convert expression symbol %s to complex relocation"),
  1073. S_GET_NAME (symp));
  1074. resolved = 0;
  1075. }
  1076. else
  1077. {
  1078. symbol_table_insert (relc_symbol);
  1079. /* S_CLEAR_EXTERNAL (relc_symbol); */
  1080. if (symp->bsym->flags & BSF_SRELC)
  1081. relc_symbol->bsym->flags |= BSF_SRELC;
  1082. else
  1083. relc_symbol->bsym->flags |= BSF_RELC;
  1084. /* symp->bsym->flags |= BSF_RELC; */
  1085. copy_symbol_attributes (symp, relc_symbol);
  1086. symp->x->value.X_op = O_symbol;
  1087. symp->x->value.X_add_symbol = relc_symbol;
  1088. symp->x->value.X_add_number = 0;
  1089. resolved = 1;
  1090. }
  1091. final_val = 0;
  1092. final_seg = undefined_section;
  1093. goto exit_dont_set_value;
  1094. }
  1095. #endif
  1096. else
  1097. {
  1098. symbolS *add_symbol, *op_symbol;
  1099. offsetT left, right;
  1100. segT seg_left, seg_right;
  1101. operatorT op;
  1102. int move_seg_ok;
  1103. symp->flags.resolving = 1;
  1104. /* Help out with CSE. */
  1105. add_symbol = symp->x->value.X_add_symbol;
  1106. op_symbol = symp->x->value.X_op_symbol;
  1107. final_val = symp->x->value.X_add_number;
  1108. op = symp->x->value.X_op;
  1109. switch (op)
  1110. {
  1111. default:
  1112. BAD_CASE (op);
  1113. break;
  1114. case O_absent:
  1115. final_val = 0;
  1116. /* Fall through. */
  1117. case O_constant:
  1118. /* Symbols whose section has SEC_ELF_OCTETS set,
  1119. resolve to octets instead of target bytes. */
  1120. if (symp->bsym->section->flags & SEC_OCTETS)
  1121. final_val += symp->frag->fr_address;
  1122. else
  1123. final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
  1124. if (final_seg == expr_section)
  1125. final_seg = absolute_section;
  1126. /* Fall through. */
  1127. case O_register:
  1128. resolved = 1;
  1129. break;
  1130. case O_symbol:
  1131. case O_symbol_rva:
  1132. case O_secidx:
  1133. left = resolve_symbol_value (add_symbol);
  1134. seg_left = S_GET_SEGMENT (add_symbol);
  1135. if (finalize_syms)
  1136. symp->x->value.X_op_symbol = NULL;
  1137. do_symbol:
  1138. if (S_IS_WEAKREFR (symp))
  1139. {
  1140. gas_assert (final_val == 0);
  1141. if (S_IS_WEAKREFR (add_symbol))
  1142. {
  1143. gas_assert (add_symbol->x->value.X_op == O_symbol
  1144. && add_symbol->x->value.X_add_number == 0);
  1145. add_symbol = add_symbol->x->value.X_add_symbol;
  1146. gas_assert (! S_IS_WEAKREFR (add_symbol));
  1147. symp->x->value.X_add_symbol = add_symbol;
  1148. }
  1149. }
  1150. if (symp->flags.mri_common)
  1151. {
  1152. /* This is a symbol inside an MRI common section. The
  1153. relocation routines are going to handle it specially.
  1154. Don't change the value. */
  1155. resolved = symbol_resolved_p (add_symbol);
  1156. break;
  1157. }
  1158. /* Don't leave symbol loops. */
  1159. if (finalize_syms
  1160. && !add_symbol->flags.local_symbol
  1161. && add_symbol->flags.resolving)
  1162. break;
  1163. if (finalize_syms && final_val == 0
  1164. #ifdef OBJ_XCOFF
  1165. /* Avoid changing symp's "within" when dealing with
  1166. AIX debug symbols. For some storage classes, "within"
  1167. have a special meaning.
  1168. C_DWARF should behave like on Linux, thus this check
  1169. isn't done to be closer. */
  1170. && ((symbol_get_bfdsym (symp)->flags & BSF_DEBUGGING) == 0
  1171. || (S_GET_STORAGE_CLASS (symp) == C_DWARF))
  1172. #endif
  1173. )
  1174. {
  1175. if (add_symbol->flags.local_symbol)
  1176. add_symbol = local_symbol_convert (add_symbol);
  1177. copy_symbol_attributes (symp, add_symbol);
  1178. }
  1179. /* If we have equated this symbol to an undefined or common
  1180. symbol, keep X_op set to O_symbol, and don't change
  1181. X_add_number. This permits the routine which writes out
  1182. relocation to detect this case, and convert the
  1183. relocation to be against the symbol to which this symbol
  1184. is equated. */
  1185. if (seg_left == undefined_section
  1186. || bfd_is_com_section (seg_left)
  1187. #if defined (OBJ_COFF) && defined (TE_PE)
  1188. || S_IS_WEAK (add_symbol)
  1189. #endif
  1190. || (finalize_syms
  1191. && ((final_seg == expr_section
  1192. && seg_left != expr_section
  1193. && seg_left != absolute_section)
  1194. || symbol_shadow_p (symp))))
  1195. {
  1196. if (finalize_syms)
  1197. {
  1198. symp->x->value.X_op = O_symbol;
  1199. symp->x->value.X_add_symbol = add_symbol;
  1200. symp->x->value.X_add_number = final_val;
  1201. /* Use X_op_symbol as a flag. */
  1202. symp->x->value.X_op_symbol = add_symbol;
  1203. }
  1204. final_seg = seg_left;
  1205. final_val += symp->frag->fr_address + left;
  1206. resolved = symbol_resolved_p (add_symbol);
  1207. symp->flags.resolving = 0;
  1208. if (op == O_secidx && seg_left != undefined_section)
  1209. {
  1210. final_val = 0;
  1211. break;
  1212. }
  1213. goto exit_dont_set_value;
  1214. }
  1215. else
  1216. {
  1217. final_val += symp->frag->fr_address + left;
  1218. if (final_seg == expr_section || final_seg == undefined_section)
  1219. final_seg = seg_left;
  1220. }
  1221. resolved = symbol_resolved_p (add_symbol);
  1222. if (S_IS_WEAKREFR (symp))
  1223. {
  1224. symp->flags.resolving = 0;
  1225. goto exit_dont_set_value;
  1226. }
  1227. break;
  1228. case O_uminus:
  1229. case O_bit_not:
  1230. case O_logical_not:
  1231. left = resolve_symbol_value (add_symbol);
  1232. seg_left = S_GET_SEGMENT (add_symbol);
  1233. /* By reducing these to the relevant dyadic operator, we get
  1234. !S -> S == 0 permitted on anything,
  1235. -S -> 0 - S only permitted on absolute
  1236. ~S -> S ^ ~0 only permitted on absolute */
  1237. if (op != O_logical_not && seg_left != absolute_section
  1238. && finalize_syms)
  1239. report_op_error (symp, NULL, op, add_symbol);
  1240. if (final_seg == expr_section || final_seg == undefined_section)
  1241. final_seg = absolute_section;
  1242. if (op == O_uminus)
  1243. left = -left;
  1244. else if (op == O_logical_not)
  1245. left = !left;
  1246. else
  1247. left = ~left;
  1248. final_val += left + symp->frag->fr_address;
  1249. resolved = symbol_resolved_p (add_symbol);
  1250. break;
  1251. case O_multiply:
  1252. case O_divide:
  1253. case O_modulus:
  1254. case O_left_shift:
  1255. case O_right_shift:
  1256. case O_bit_inclusive_or:
  1257. case O_bit_or_not:
  1258. case O_bit_exclusive_or:
  1259. case O_bit_and:
  1260. case O_add:
  1261. case O_subtract:
  1262. case O_eq:
  1263. case O_ne:
  1264. case O_lt:
  1265. case O_le:
  1266. case O_ge:
  1267. case O_gt:
  1268. case O_logical_and:
  1269. case O_logical_or:
  1270. left = resolve_symbol_value (add_symbol);
  1271. right = resolve_symbol_value (op_symbol);
  1272. seg_left = S_GET_SEGMENT (add_symbol);
  1273. seg_right = S_GET_SEGMENT (op_symbol);
  1274. /* Simplify addition or subtraction of a constant by folding the
  1275. constant into X_add_number. */
  1276. if (op == O_add)
  1277. {
  1278. if (seg_right == absolute_section)
  1279. {
  1280. final_val += right;
  1281. goto do_symbol;
  1282. }
  1283. else if (seg_left == absolute_section)
  1284. {
  1285. final_val += left;
  1286. add_symbol = op_symbol;
  1287. left = right;
  1288. seg_left = seg_right;
  1289. goto do_symbol;
  1290. }
  1291. }
  1292. else if (op == O_subtract)
  1293. {
  1294. if (seg_right == absolute_section)
  1295. {
  1296. final_val -= right;
  1297. goto do_symbol;
  1298. }
  1299. }
  1300. move_seg_ok = 1;
  1301. /* Equality and non-equality tests are permitted on anything.
  1302. Subtraction, and other comparison operators are permitted if
  1303. both operands are in the same section. Otherwise, both
  1304. operands must be absolute. We already handled the case of
  1305. addition or subtraction of a constant above. This will
  1306. probably need to be changed for an object file format which
  1307. supports arbitrary expressions. */
  1308. if (!(seg_left == absolute_section
  1309. && seg_right == absolute_section)
  1310. && !(op == O_eq || op == O_ne)
  1311. && !((op == O_subtract
  1312. || op == O_lt || op == O_le || op == O_ge || op == O_gt)
  1313. && seg_left == seg_right
  1314. && (seg_left != undefined_section
  1315. || add_symbol == op_symbol)))
  1316. {
  1317. /* Don't emit messages unless we're finalizing the symbol value,
  1318. otherwise we may get the same message multiple times. */
  1319. if (finalize_syms)
  1320. report_op_error (symp, add_symbol, op, op_symbol);
  1321. /* However do not move the symbol into the absolute section
  1322. if it cannot currently be resolved - this would confuse
  1323. other parts of the assembler into believing that the
  1324. expression had been evaluated to zero. */
  1325. else
  1326. move_seg_ok = 0;
  1327. }
  1328. if (move_seg_ok
  1329. && (final_seg == expr_section || final_seg == undefined_section))
  1330. final_seg = absolute_section;
  1331. /* Check for division by zero. */
  1332. if ((op == O_divide || op == O_modulus) && right == 0)
  1333. {
  1334. /* If seg_right is not absolute_section, then we've
  1335. already issued a warning about using a bad symbol. */
  1336. if (seg_right == absolute_section && finalize_syms)
  1337. {
  1338. const char *file;
  1339. unsigned int line;
  1340. if (expr_symbol_where (symp, &file, &line))
  1341. as_bad_where (file, line, _("division by zero"));
  1342. else
  1343. as_bad (_("division by zero when setting `%s'"),
  1344. S_GET_NAME (symp));
  1345. }
  1346. right = 1;
  1347. }
  1348. if ((op == O_left_shift || op == O_right_shift)
  1349. && (valueT) right >= sizeof (valueT) * CHAR_BIT)
  1350. {
  1351. as_warn_value_out_of_range (_("shift count"), right, 0,
  1352. sizeof (valueT) * CHAR_BIT - 1,
  1353. NULL, 0);
  1354. left = right = 0;
  1355. }
  1356. switch (symp->x->value.X_op)
  1357. {
  1358. case O_multiply: left *= right; break;
  1359. case O_divide: left /= right; break;
  1360. case O_modulus: left %= right; break;
  1361. case O_left_shift:
  1362. left = (valueT) left << (valueT) right; break;
  1363. case O_right_shift:
  1364. left = (valueT) left >> (valueT) right; break;
  1365. case O_bit_inclusive_or: left |= right; break;
  1366. case O_bit_or_not: left |= ~right; break;
  1367. case O_bit_exclusive_or: left ^= right; break;
  1368. case O_bit_and: left &= right; break;
  1369. case O_add: left += right; break;
  1370. case O_subtract: left -= right; break;
  1371. case O_eq:
  1372. case O_ne:
  1373. left = (left == right && seg_left == seg_right
  1374. && (seg_left != undefined_section
  1375. || add_symbol == op_symbol)
  1376. ? ~ (offsetT) 0 : 0);
  1377. if (symp->x->value.X_op == O_ne)
  1378. left = ~left;
  1379. break;
  1380. case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
  1381. case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
  1382. case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
  1383. case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
  1384. case O_logical_and: left = left && right; break;
  1385. case O_logical_or: left = left || right; break;
  1386. case O_illegal:
  1387. case O_absent:
  1388. case O_constant:
  1389. /* See PR 20895 for a reproducer. */
  1390. as_bad (_("Invalid operation on symbol"));
  1391. goto exit_dont_set_value;
  1392. default:
  1393. abort ();
  1394. }
  1395. final_val += symp->frag->fr_address + left;
  1396. if (final_seg == expr_section || final_seg == undefined_section)
  1397. {
  1398. if (seg_left == undefined_section
  1399. || seg_right == undefined_section)
  1400. final_seg = undefined_section;
  1401. else if (seg_left == absolute_section)
  1402. final_seg = seg_right;
  1403. else
  1404. final_seg = seg_left;
  1405. }
  1406. resolved = (symbol_resolved_p (add_symbol)
  1407. && symbol_resolved_p (op_symbol));
  1408. break;
  1409. case O_big:
  1410. case O_illegal:
  1411. /* Give an error (below) if not in expr_section. We don't
  1412. want to worry about expr_section symbols, because they
  1413. are fictional (they are created as part of expression
  1414. resolution), and any problems may not actually mean
  1415. anything. */
  1416. break;
  1417. }
  1418. symp->flags.resolving = 0;
  1419. }
  1420. if (finalize_syms)
  1421. S_SET_VALUE (symp, final_val);
  1422. exit_dont_set_value:
  1423. /* Always set the segment, even if not finalizing the value.
  1424. The segment is used to determine whether a symbol is defined. */
  1425. S_SET_SEGMENT (symp, final_seg);
  1426. /* Don't worry if we can't resolve an expr_section symbol. */
  1427. if (finalize_syms)
  1428. {
  1429. if (resolved)
  1430. symp->flags.resolved = 1;
  1431. else if (S_GET_SEGMENT (symp) != expr_section)
  1432. {
  1433. as_bad (_("can't resolve value for symbol `%s'"),
  1434. S_GET_NAME (symp));
  1435. symp->flags.resolved = 1;
  1436. }
  1437. }
  1438. return final_val;
  1439. }
  1440. /* A static function passed to hash_traverse. */
  1441. static int
  1442. resolve_local_symbol (void **slot, void *arg ATTRIBUTE_UNUSED)
  1443. {
  1444. symbol_entry_t *entry = *((symbol_entry_t **) slot);
  1445. if (entry->sy.flags.local_symbol)
  1446. resolve_symbol_value (&entry->sy);
  1447. return 1;
  1448. }
  1449. /* Resolve all local symbols. */
  1450. void
  1451. resolve_local_symbol_values (void)
  1452. {
  1453. htab_traverse (sy_hash, resolve_local_symbol, NULL);
  1454. }
  1455. /* Obtain the current value of a symbol without changing any
  1456. sub-expressions used. */
  1457. int
  1458. snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
  1459. {
  1460. symbolS *symbolP = *symbolPP;
  1461. if (symbolP->flags.local_symbol)
  1462. {
  1463. struct local_symbol *locsym = (struct local_symbol *) symbolP;
  1464. *valueP = locsym->value;
  1465. *segP = locsym->section;
  1466. *fragPP = locsym->frag;
  1467. }
  1468. else
  1469. {
  1470. expressionS exp = symbolP->x->value;
  1471. if (!symbolP->flags.resolved && exp.X_op != O_illegal)
  1472. {
  1473. int resolved;
  1474. if (symbolP->flags.resolving)
  1475. return 0;
  1476. symbolP->flags.resolving = 1;
  1477. resolved = resolve_expression (&exp);
  1478. symbolP->flags.resolving = 0;
  1479. if (!resolved)
  1480. return 0;
  1481. switch (exp.X_op)
  1482. {
  1483. case O_constant:
  1484. case O_register:
  1485. if (!symbol_equated_p (symbolP))
  1486. break;
  1487. /* Fallthru. */
  1488. case O_symbol:
  1489. case O_symbol_rva:
  1490. symbolP = exp.X_add_symbol;
  1491. break;
  1492. default:
  1493. return 0;
  1494. }
  1495. }
  1496. *symbolPP = symbolP;
  1497. /* A bogus input file can result in resolve_expression()
  1498. generating a local symbol, so we have to check again. */
  1499. if (symbolP->flags.local_symbol)
  1500. {
  1501. struct local_symbol *locsym = (struct local_symbol *) symbolP;
  1502. *valueP = locsym->value;
  1503. *segP = locsym->section;
  1504. *fragPP = locsym->frag;
  1505. }
  1506. else
  1507. {
  1508. *valueP = exp.X_add_number;
  1509. *segP = symbolP->bsym->section;
  1510. *fragPP = symbolP->frag;
  1511. }
  1512. if (*segP == expr_section)
  1513. switch (exp.X_op)
  1514. {
  1515. case O_constant: *segP = absolute_section; break;
  1516. case O_register: *segP = reg_section; break;
  1517. default: break;
  1518. }
  1519. }
  1520. return 1;
  1521. }
  1522. /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
  1523. They are *really* local. That is, they go out of scope whenever we see a
  1524. label that isn't local. Also, like fb labels, there can be multiple
  1525. instances of a dollar label. Therefor, we name encode each instance with
  1526. the instance number, keep a list of defined symbols separate from the real
  1527. symbol table, and we treat these buggers as a sparse array. */
  1528. typedef unsigned int dollar_ent;
  1529. static dollar_ent *dollar_labels;
  1530. static dollar_ent *dollar_label_instances;
  1531. static char *dollar_label_defines;
  1532. static size_t dollar_label_count;
  1533. static size_t dollar_label_max;
  1534. int
  1535. dollar_label_defined (unsigned int label)
  1536. {
  1537. dollar_ent *i;
  1538. know ((dollar_labels != NULL) || (dollar_label_count == 0));
  1539. for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
  1540. if (*i == label)
  1541. return dollar_label_defines[i - dollar_labels];
  1542. /* If we get here, label isn't defined. */
  1543. return 0;
  1544. }
  1545. static unsigned int
  1546. dollar_label_instance (unsigned int label)
  1547. {
  1548. dollar_ent *i;
  1549. know ((dollar_labels != NULL) || (dollar_label_count == 0));
  1550. for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
  1551. if (*i == label)
  1552. return (dollar_label_instances[i - dollar_labels]);
  1553. /* If we get here, we haven't seen the label before.
  1554. Therefore its instance count is zero. */
  1555. return 0;
  1556. }
  1557. void
  1558. dollar_label_clear (void)
  1559. {
  1560. if (dollar_label_count)
  1561. memset (dollar_label_defines, '\0', dollar_label_count);
  1562. }
  1563. #define DOLLAR_LABEL_BUMP_BY 10
  1564. void
  1565. define_dollar_label (unsigned int label)
  1566. {
  1567. dollar_ent *i;
  1568. for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
  1569. if (*i == label)
  1570. {
  1571. ++dollar_label_instances[i - dollar_labels];
  1572. dollar_label_defines[i - dollar_labels] = 1;
  1573. return;
  1574. }
  1575. /* If we get to here, we don't have label listed yet. */
  1576. if (dollar_labels == NULL)
  1577. {
  1578. dollar_labels = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
  1579. dollar_label_instances = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
  1580. dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
  1581. dollar_label_max = DOLLAR_LABEL_BUMP_BY;
  1582. dollar_label_count = 0;
  1583. }
  1584. else if (dollar_label_count == dollar_label_max)
  1585. {
  1586. dollar_label_max += DOLLAR_LABEL_BUMP_BY;
  1587. dollar_labels = XRESIZEVEC (dollar_ent, dollar_labels,
  1588. dollar_label_max);
  1589. dollar_label_instances = XRESIZEVEC (dollar_ent,
  1590. dollar_label_instances,
  1591. dollar_label_max);
  1592. dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
  1593. dollar_label_max);
  1594. } /* if we needed to grow */
  1595. dollar_labels[dollar_label_count] = label;
  1596. dollar_label_instances[dollar_label_count] = 1;
  1597. dollar_label_defines[dollar_label_count] = 1;
  1598. ++dollar_label_count;
  1599. }
  1600. /* Caller must copy returned name: we re-use the area for the next name.
  1601. The mth occurrence of label n: is turned into the symbol "Ln^Am"
  1602. where n is the label number and m is the instance number. "L" makes
  1603. it a label discarded unless debugging and "^A"('\1') ensures no
  1604. ordinary symbol SHOULD get the same name as a local label
  1605. symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
  1606. fb labels get the same treatment, except that ^B is used in place
  1607. of ^A.
  1608. AUGEND is 0 for current instance, 1 for new instance. */
  1609. char *
  1610. dollar_label_name (unsigned int n, unsigned int augend)
  1611. {
  1612. /* Returned to caller, then copied. Used for created names ("4f"). */
  1613. static char symbol_name_build[24];
  1614. char *p = symbol_name_build;
  1615. #ifdef LOCAL_LABEL_PREFIX
  1616. *p++ = LOCAL_LABEL_PREFIX;
  1617. #endif
  1618. sprintf (p, "L%u%c%u",
  1619. n, DOLLAR_LABEL_CHAR, dollar_label_instance (n) + augend);
  1620. return symbol_name_build;
  1621. }
  1622. /* Somebody else's idea of local labels. They are made by "n:" where n
  1623. is any decimal digit. Refer to them with
  1624. "nb" for previous (backward) n:
  1625. or "nf" for next (forward) n:.
  1626. We do a little better and let n be any number, not just a single digit, but
  1627. since the other guy's assembler only does ten, we treat the first ten
  1628. specially.
  1629. Like someone else's assembler, we have one set of local label counters for
  1630. entire assembly, not one set per (sub)segment like in most assemblers. This
  1631. implies that one can refer to a label in another segment, and indeed some
  1632. crufty compilers have done just that.
  1633. Since there could be a LOT of these things, treat them as a sparse
  1634. array. */
  1635. #define FB_LABEL_SPECIAL (10)
  1636. typedef unsigned int fb_ent;
  1637. static fb_ent fb_low_counter[FB_LABEL_SPECIAL];
  1638. static fb_ent *fb_labels;
  1639. static fb_ent *fb_label_instances;
  1640. static size_t fb_label_count;
  1641. static size_t fb_label_max;
  1642. /* This must be more than FB_LABEL_SPECIAL. */
  1643. #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
  1644. static void
  1645. fb_label_init (void)
  1646. {
  1647. memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
  1648. }
  1649. /* Add one to the instance number of this fb label. */
  1650. void
  1651. fb_label_instance_inc (unsigned int label)
  1652. {
  1653. fb_ent *i;
  1654. if (label < FB_LABEL_SPECIAL)
  1655. {
  1656. ++fb_low_counter[label];
  1657. return;
  1658. }
  1659. if (fb_labels != NULL)
  1660. {
  1661. for (i = fb_labels + FB_LABEL_SPECIAL;
  1662. i < fb_labels + fb_label_count; ++i)
  1663. {
  1664. if (*i == label)
  1665. {
  1666. ++fb_label_instances[i - fb_labels];
  1667. return;
  1668. } /* if we find it */
  1669. } /* for each existing label */
  1670. }
  1671. /* If we get to here, we don't have label listed yet. */
  1672. if (fb_labels == NULL)
  1673. {
  1674. fb_labels = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
  1675. fb_label_instances = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
  1676. fb_label_max = FB_LABEL_BUMP_BY;
  1677. fb_label_count = FB_LABEL_SPECIAL;
  1678. }
  1679. else if (fb_label_count == fb_label_max)
  1680. {
  1681. fb_label_max += FB_LABEL_BUMP_BY;
  1682. fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
  1683. fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
  1684. fb_label_max);
  1685. } /* if we needed to grow */
  1686. fb_labels[fb_label_count] = label;
  1687. fb_label_instances[fb_label_count] = 1;
  1688. ++fb_label_count;
  1689. }
  1690. static unsigned int
  1691. fb_label_instance (unsigned int label)
  1692. {
  1693. fb_ent *i;
  1694. if (label < FB_LABEL_SPECIAL)
  1695. return (fb_low_counter[label]);
  1696. if (fb_labels != NULL)
  1697. {
  1698. for (i = fb_labels + FB_LABEL_SPECIAL;
  1699. i < fb_labels + fb_label_count; ++i)
  1700. {
  1701. if (*i == label)
  1702. return (fb_label_instances[i - fb_labels]);
  1703. }
  1704. }
  1705. /* We didn't find the label, so this must be a reference to the
  1706. first instance. */
  1707. return 0;
  1708. }
  1709. /* Caller must copy returned name: we re-use the area for the next name.
  1710. The mth occurrence of label n: is turned into the symbol "Ln^Bm"
  1711. where n is the label number and m is the instance number. "L" makes
  1712. it a label discarded unless debugging and "^B"('\2') ensures no
  1713. ordinary symbol SHOULD get the same name as a local label
  1714. symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
  1715. dollar labels get the same treatment, except that ^A is used in
  1716. place of ^B.
  1717. AUGEND is 0 for nb, 1 for n:, nf. */
  1718. char *
  1719. fb_label_name (unsigned int n, unsigned int augend)
  1720. {
  1721. /* Returned to caller, then copied. Used for created names ("4f"). */
  1722. static char symbol_name_build[24];
  1723. char *p = symbol_name_build;
  1724. #ifdef TC_MMIX
  1725. know (augend <= 2 /* See mmix_fb_label. */);
  1726. #else
  1727. know (augend <= 1);
  1728. #endif
  1729. #ifdef LOCAL_LABEL_PREFIX
  1730. *p++ = LOCAL_LABEL_PREFIX;
  1731. #endif
  1732. sprintf (p, "L%u%c%u",
  1733. n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
  1734. return symbol_name_build;
  1735. }
  1736. /* Decode name that may have been generated by foo_label_name() above.
  1737. If the name wasn't generated by foo_label_name(), then return it
  1738. unaltered. This is used for error messages. */
  1739. char *
  1740. decode_local_label_name (char *s)
  1741. {
  1742. char *p;
  1743. char *symbol_decode;
  1744. int label_number;
  1745. int instance_number;
  1746. const char *type;
  1747. const char *message_format;
  1748. int lindex = 0;
  1749. #ifdef LOCAL_LABEL_PREFIX
  1750. if (s[lindex] == LOCAL_LABEL_PREFIX)
  1751. ++lindex;
  1752. #endif
  1753. if (s[lindex] != 'L')
  1754. return s;
  1755. for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
  1756. label_number = (10 * label_number) + *p - '0';
  1757. if (*p == DOLLAR_LABEL_CHAR)
  1758. type = "dollar";
  1759. else if (*p == LOCAL_LABEL_CHAR)
  1760. type = "fb";
  1761. else
  1762. return s;
  1763. for (instance_number = 0, p++; ISDIGIT (*p); ++p)
  1764. instance_number = (10 * instance_number) + *p - '0';
  1765. message_format = _("\"%d\" (instance number %d of a %s label)");
  1766. symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
  1767. sprintf (symbol_decode, message_format, label_number, instance_number, type);
  1768. return symbol_decode;
  1769. }
  1770. /* Get the value of a symbol. */
  1771. valueT
  1772. S_GET_VALUE (symbolS *s)
  1773. {
  1774. if (s->flags.local_symbol)
  1775. return resolve_symbol_value (s);
  1776. if (!s->flags.resolved)
  1777. {
  1778. valueT val = resolve_symbol_value (s);
  1779. if (!finalize_syms)
  1780. return val;
  1781. }
  1782. if (S_IS_WEAKREFR (s))
  1783. return S_GET_VALUE (s->x->value.X_add_symbol);
  1784. if (s->x->value.X_op != O_constant)
  1785. {
  1786. if (! s->flags.resolved
  1787. || s->x->value.X_op != O_symbol
  1788. || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
  1789. as_bad (_("attempt to get value of unresolved symbol `%s'"),
  1790. S_GET_NAME (s));
  1791. }
  1792. return (valueT) s->x->value.X_add_number;
  1793. }
  1794. /* Set the value of a symbol. */
  1795. void
  1796. S_SET_VALUE (symbolS *s, valueT val)
  1797. {
  1798. if (s->flags.local_symbol)
  1799. {
  1800. ((struct local_symbol *) s)->value = val;
  1801. return;
  1802. }
  1803. s->x->value.X_op = O_constant;
  1804. s->x->value.X_add_number = (offsetT) val;
  1805. s->x->value.X_unsigned = 0;
  1806. S_CLEAR_WEAKREFR (s);
  1807. }
  1808. void
  1809. copy_symbol_attributes (symbolS *dest, symbolS *src)
  1810. {
  1811. if (dest->flags.local_symbol)
  1812. dest = local_symbol_convert (dest);
  1813. if (src->flags.local_symbol)
  1814. src = local_symbol_convert (src);
  1815. /* In an expression, transfer the settings of these flags.
  1816. The user can override later, of course. */
  1817. #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
  1818. | BSF_GNU_INDIRECT_FUNCTION)
  1819. dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
  1820. #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
  1821. OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
  1822. #endif
  1823. #ifdef TC_COPY_SYMBOL_ATTRIBUTES
  1824. TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
  1825. #endif
  1826. }
  1827. int
  1828. S_IS_FUNCTION (symbolS *s)
  1829. {
  1830. flagword flags;
  1831. if (s->flags.local_symbol)
  1832. return 0;
  1833. flags = s->bsym->flags;
  1834. return (flags & BSF_FUNCTION) != 0;
  1835. }
  1836. int
  1837. S_IS_EXTERNAL (symbolS *s)
  1838. {
  1839. flagword flags;
  1840. if (s->flags.local_symbol)
  1841. return 0;
  1842. flags = s->bsym->flags;
  1843. /* Sanity check. */
  1844. if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
  1845. abort ();
  1846. return (flags & BSF_GLOBAL) != 0;
  1847. }
  1848. int
  1849. S_IS_WEAK (symbolS *s)
  1850. {
  1851. if (s->flags.local_symbol)
  1852. return 0;
  1853. /* Conceptually, a weakrefr is weak if the referenced symbol is. We
  1854. could probably handle a WEAKREFR as always weak though. E.g., if
  1855. the referenced symbol has lost its weak status, there's no reason
  1856. to keep handling the weakrefr as if it was weak. */
  1857. if (S_IS_WEAKREFR (s))
  1858. return S_IS_WEAK (s->x->value.X_add_symbol);
  1859. return (s->bsym->flags & BSF_WEAK) != 0;
  1860. }
  1861. int
  1862. S_IS_WEAKREFR (symbolS *s)
  1863. {
  1864. if (s->flags.local_symbol)
  1865. return 0;
  1866. return s->flags.weakrefr != 0;
  1867. }
  1868. int
  1869. S_IS_WEAKREFD (symbolS *s)
  1870. {
  1871. if (s->flags.local_symbol)
  1872. return 0;
  1873. return s->flags.weakrefd != 0;
  1874. }
  1875. int
  1876. S_IS_COMMON (symbolS *s)
  1877. {
  1878. if (s->flags.local_symbol)
  1879. return 0;
  1880. return bfd_is_com_section (s->bsym->section);
  1881. }
  1882. int
  1883. S_IS_DEFINED (symbolS *s)
  1884. {
  1885. if (s->flags.local_symbol)
  1886. return ((struct local_symbol *) s)->section != undefined_section;
  1887. return s->bsym->section != undefined_section;
  1888. }
  1889. #ifndef EXTERN_FORCE_RELOC
  1890. #define EXTERN_FORCE_RELOC IS_ELF
  1891. #endif
  1892. /* Return true for symbols that should not be reduced to section
  1893. symbols or eliminated from expressions, because they may be
  1894. overridden by the linker. */
  1895. int
  1896. S_FORCE_RELOC (symbolS *s, int strict)
  1897. {
  1898. segT sec;
  1899. if (s->flags.local_symbol)
  1900. sec = ((struct local_symbol *) s)->section;
  1901. else
  1902. {
  1903. if ((strict
  1904. && ((s->bsym->flags & BSF_WEAK) != 0
  1905. || (EXTERN_FORCE_RELOC
  1906. && (s->bsym->flags & BSF_GLOBAL) != 0)))
  1907. || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
  1908. return true;
  1909. sec = s->bsym->section;
  1910. }
  1911. return bfd_is_und_section (sec) || bfd_is_com_section (sec);
  1912. }
  1913. int
  1914. S_IS_DEBUG (symbolS *s)
  1915. {
  1916. if (s->flags.local_symbol)
  1917. return 0;
  1918. if (s->bsym->flags & BSF_DEBUGGING)
  1919. return 1;
  1920. return 0;
  1921. }
  1922. int
  1923. S_IS_LOCAL (symbolS *s)
  1924. {
  1925. flagword flags;
  1926. const char *name;
  1927. if (s->flags.local_symbol)
  1928. return 1;
  1929. flags = s->bsym->flags;
  1930. /* Sanity check. */
  1931. if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
  1932. abort ();
  1933. if (bfd_asymbol_section (s->bsym) == reg_section)
  1934. return 1;
  1935. if (flag_strip_local_absolute
  1936. /* Keep BSF_FILE symbols in order to allow debuggers to identify
  1937. the source file even when the object file is stripped. */
  1938. && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
  1939. && bfd_asymbol_section (s->bsym) == absolute_section)
  1940. return 1;
  1941. name = S_GET_NAME (s);
  1942. return (name != NULL
  1943. && ! S_IS_DEBUG (s)
  1944. && (strchr (name, DOLLAR_LABEL_CHAR)
  1945. || strchr (name, LOCAL_LABEL_CHAR)
  1946. #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
  1947. || strchr (name, FAKE_LABEL_CHAR)
  1948. #endif
  1949. || TC_LABEL_IS_LOCAL (name)
  1950. || (! flag_keep_locals
  1951. && (bfd_is_local_label (stdoutput, s->bsym)
  1952. || (flag_mri
  1953. && name[0] == '?'
  1954. && name[1] == '?')))));
  1955. }
  1956. int
  1957. S_IS_STABD (symbolS *s)
  1958. {
  1959. return S_GET_NAME (s) == 0;
  1960. }
  1961. int
  1962. S_CAN_BE_REDEFINED (const symbolS *s)
  1963. {
  1964. if (s->flags.local_symbol)
  1965. return (((struct local_symbol *) s)->frag
  1966. == &predefined_address_frag);
  1967. /* Permit register names to be redefined. */
  1968. return s->bsym->section == reg_section;
  1969. }
  1970. int
  1971. S_IS_VOLATILE (const symbolS *s)
  1972. {
  1973. if (s->flags.local_symbol)
  1974. return 0;
  1975. return s->flags.volatil;
  1976. }
  1977. int
  1978. S_IS_FORWARD_REF (const symbolS *s)
  1979. {
  1980. if (s->flags.local_symbol)
  1981. return 0;
  1982. return s->flags.forward_ref;
  1983. }
  1984. const char *
  1985. S_GET_NAME (symbolS *s)
  1986. {
  1987. return s->name;
  1988. }
  1989. segT
  1990. S_GET_SEGMENT (symbolS *s)
  1991. {
  1992. if (s->flags.local_symbol)
  1993. return ((struct local_symbol *) s)->section;
  1994. return s->bsym->section;
  1995. }
  1996. void
  1997. S_SET_SEGMENT (symbolS *s, segT seg)
  1998. {
  1999. if (s->flags.local_symbol)
  2000. {
  2001. ((struct local_symbol *) s)->section = seg;
  2002. return;
  2003. }
  2004. /* Don't reassign section symbols. The direct reason is to prevent seg
  2005. faults assigning back to const global symbols such as *ABS*, but it
  2006. shouldn't happen anyway. */
  2007. if (s->bsym->flags & BSF_SECTION_SYM)
  2008. {
  2009. if (s->bsym->section != seg)
  2010. abort ();
  2011. }
  2012. else
  2013. {
  2014. if (multibyte_handling == multibyte_warn_syms
  2015. && ! s->flags.local_symbol
  2016. && seg != undefined_section
  2017. && ! s->flags.multibyte_warned
  2018. && scan_for_multibyte_characters ((const unsigned char *) s->name,
  2019. (const unsigned char *) s->name + strlen (s->name),
  2020. false))
  2021. {
  2022. as_warn (_("symbol '%s' contains multibyte characters"), s->name);
  2023. s->flags.multibyte_warned = 1;
  2024. }
  2025. s->bsym->section = seg;
  2026. }
  2027. }
  2028. void
  2029. S_SET_EXTERNAL (symbolS *s)
  2030. {
  2031. if (s->flags.local_symbol)
  2032. s = local_symbol_convert (s);
  2033. if ((s->bsym->flags & BSF_WEAK) != 0)
  2034. {
  2035. /* Let .weak override .global. */
  2036. return;
  2037. }
  2038. if (s->bsym->flags & BSF_SECTION_SYM)
  2039. {
  2040. /* Do not reassign section symbols. */
  2041. as_warn (_("can't make section symbol global"));
  2042. return;
  2043. }
  2044. #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
  2045. if (S_GET_SEGMENT (s) == reg_section)
  2046. {
  2047. as_bad (_("can't make register symbol global"));
  2048. return;
  2049. }
  2050. #endif
  2051. s->bsym->flags |= BSF_GLOBAL;
  2052. s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
  2053. #ifdef TE_PE
  2054. if (! an_external_name && S_GET_NAME(s)[0] != '.')
  2055. an_external_name = S_GET_NAME (s);
  2056. #endif
  2057. }
  2058. void
  2059. S_CLEAR_EXTERNAL (symbolS *s)
  2060. {
  2061. if (s->flags.local_symbol)
  2062. return;
  2063. if ((s->bsym->flags & BSF_WEAK) != 0)
  2064. {
  2065. /* Let .weak override. */
  2066. return;
  2067. }
  2068. s->bsym->flags |= BSF_LOCAL;
  2069. s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
  2070. }
  2071. void
  2072. S_SET_WEAK (symbolS *s)
  2073. {
  2074. if (s->flags.local_symbol)
  2075. s = local_symbol_convert (s);
  2076. #ifdef obj_set_weak_hook
  2077. obj_set_weak_hook (s);
  2078. #endif
  2079. s->bsym->flags |= BSF_WEAK;
  2080. s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
  2081. }
  2082. void
  2083. S_SET_WEAKREFR (symbolS *s)
  2084. {
  2085. if (s->flags.local_symbol)
  2086. s = local_symbol_convert (s);
  2087. s->flags.weakrefr = 1;
  2088. /* If the alias was already used, make sure we mark the target as
  2089. used as well, otherwise it might be dropped from the symbol
  2090. table. This may have unintended side effects if the alias is
  2091. later redirected to another symbol, such as keeping the unused
  2092. previous target in the symbol table. Since it will be weak, it's
  2093. not a big deal. */
  2094. if (s->flags.used)
  2095. symbol_mark_used (s->x->value.X_add_symbol);
  2096. }
  2097. void
  2098. S_CLEAR_WEAKREFR (symbolS *s)
  2099. {
  2100. if (s->flags.local_symbol)
  2101. return;
  2102. s->flags.weakrefr = 0;
  2103. }
  2104. void
  2105. S_SET_WEAKREFD (symbolS *s)
  2106. {
  2107. if (s->flags.local_symbol)
  2108. s = local_symbol_convert (s);
  2109. s->flags.weakrefd = 1;
  2110. S_SET_WEAK (s);
  2111. }
  2112. void
  2113. S_CLEAR_WEAKREFD (symbolS *s)
  2114. {
  2115. if (s->flags.local_symbol)
  2116. return;
  2117. if (s->flags.weakrefd)
  2118. {
  2119. s->flags.weakrefd = 0;
  2120. /* If a weakref target symbol is weak, then it was never
  2121. referenced directly before, not even in a .global directive,
  2122. so decay it to local. If it remains undefined, it will be
  2123. later turned into a global, like any other undefined
  2124. symbol. */
  2125. if (s->bsym->flags & BSF_WEAK)
  2126. {
  2127. #ifdef obj_clear_weak_hook
  2128. obj_clear_weak_hook (s);
  2129. #endif
  2130. s->bsym->flags &= ~BSF_WEAK;
  2131. s->bsym->flags |= BSF_LOCAL;
  2132. }
  2133. }
  2134. }
  2135. void
  2136. S_SET_THREAD_LOCAL (symbolS *s)
  2137. {
  2138. if (s->flags.local_symbol)
  2139. s = local_symbol_convert (s);
  2140. if (bfd_is_com_section (s->bsym->section)
  2141. && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
  2142. return;
  2143. s->bsym->flags |= BSF_THREAD_LOCAL;
  2144. if ((s->bsym->flags & BSF_FUNCTION) != 0)
  2145. as_bad (_("Accessing function `%s' as thread-local object"),
  2146. S_GET_NAME (s));
  2147. else if (! bfd_is_und_section (s->bsym->section)
  2148. && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
  2149. as_bad (_("Accessing `%s' as thread-local object"),
  2150. S_GET_NAME (s));
  2151. }
  2152. void
  2153. S_SET_NAME (symbolS *s, const char *name)
  2154. {
  2155. s->name = name;
  2156. if (s->flags.local_symbol)
  2157. return;
  2158. s->bsym->name = name;
  2159. }
  2160. void
  2161. S_SET_VOLATILE (symbolS *s)
  2162. {
  2163. if (s->flags.local_symbol)
  2164. s = local_symbol_convert (s);
  2165. s->flags.volatil = 1;
  2166. }
  2167. void
  2168. S_CLEAR_VOLATILE (symbolS *s)
  2169. {
  2170. if (!s->flags.local_symbol)
  2171. s->flags.volatil = 0;
  2172. }
  2173. void
  2174. S_SET_FORWARD_REF (symbolS *s)
  2175. {
  2176. if (s->flags.local_symbol)
  2177. s = local_symbol_convert (s);
  2178. s->flags.forward_ref = 1;
  2179. }
  2180. /* Return the previous symbol in a chain. */
  2181. symbolS *
  2182. symbol_previous (symbolS *s)
  2183. {
  2184. if (s->flags.local_symbol)
  2185. abort ();
  2186. return s->x->previous;
  2187. }
  2188. /* Return the next symbol in a chain. */
  2189. symbolS *
  2190. symbol_next (symbolS *s)
  2191. {
  2192. if (s->flags.local_symbol)
  2193. abort ();
  2194. return s->x->next;
  2195. }
  2196. /* Return a pointer to the value of a symbol as an expression. */
  2197. expressionS *
  2198. symbol_get_value_expression (symbolS *s)
  2199. {
  2200. if (s->flags.local_symbol)
  2201. s = local_symbol_convert (s);
  2202. return &s->x->value;
  2203. }
  2204. /* Set the value of a symbol to an expression. */
  2205. void
  2206. symbol_set_value_expression (symbolS *s, const expressionS *exp)
  2207. {
  2208. if (s->flags.local_symbol)
  2209. s = local_symbol_convert (s);
  2210. s->x->value = *exp;
  2211. S_CLEAR_WEAKREFR (s);
  2212. }
  2213. /* Return whether 2 symbols are the same. */
  2214. int
  2215. symbol_same_p (symbolS *s1, symbolS *s2)
  2216. {
  2217. return s1 == s2;
  2218. }
  2219. /* Return a pointer to the X_add_number component of a symbol. */
  2220. offsetT *
  2221. symbol_X_add_number (symbolS *s)
  2222. {
  2223. if (s->flags.local_symbol)
  2224. return (offsetT *) &((struct local_symbol *) s)->value;
  2225. return &s->x->value.X_add_number;
  2226. }
  2227. /* Set the value of SYM to the current position in the current segment. */
  2228. void
  2229. symbol_set_value_now (symbolS *sym)
  2230. {
  2231. S_SET_SEGMENT (sym, now_seg);
  2232. S_SET_VALUE (sym, frag_now_fix ());
  2233. symbol_set_frag (sym, frag_now);
  2234. }
  2235. /* Set the frag of a symbol. */
  2236. void
  2237. symbol_set_frag (symbolS *s, fragS *f)
  2238. {
  2239. if (s->flags.local_symbol)
  2240. {
  2241. ((struct local_symbol *) s)->frag = f;
  2242. return;
  2243. }
  2244. s->frag = f;
  2245. S_CLEAR_WEAKREFR (s);
  2246. }
  2247. /* Return the frag of a symbol. */
  2248. fragS *
  2249. symbol_get_frag (symbolS *s)
  2250. {
  2251. if (s->flags.local_symbol)
  2252. return ((struct local_symbol *) s)->frag;
  2253. return s->frag;
  2254. }
  2255. /* Mark a symbol as having been used. */
  2256. void
  2257. symbol_mark_used (symbolS *s)
  2258. {
  2259. if (s->flags.local_symbol)
  2260. return;
  2261. s->flags.used = 1;
  2262. if (S_IS_WEAKREFR (s))
  2263. symbol_mark_used (s->x->value.X_add_symbol);
  2264. }
  2265. /* Clear the mark of whether a symbol has been used. */
  2266. void
  2267. symbol_clear_used (symbolS *s)
  2268. {
  2269. if (s->flags.local_symbol)
  2270. s = local_symbol_convert (s);
  2271. s->flags.used = 0;
  2272. }
  2273. /* Return whether a symbol has been used. */
  2274. int
  2275. symbol_used_p (symbolS *s)
  2276. {
  2277. if (s->flags.local_symbol)
  2278. return 1;
  2279. return s->flags.used;
  2280. }
  2281. /* Mark a symbol as having been used in a reloc. */
  2282. void
  2283. symbol_mark_used_in_reloc (symbolS *s)
  2284. {
  2285. if (s->flags.local_symbol)
  2286. s = local_symbol_convert (s);
  2287. s->flags.used_in_reloc = 1;
  2288. }
  2289. /* Clear the mark of whether a symbol has been used in a reloc. */
  2290. void
  2291. symbol_clear_used_in_reloc (symbolS *s)
  2292. {
  2293. if (s->flags.local_symbol)
  2294. return;
  2295. s->flags.used_in_reloc = 0;
  2296. }
  2297. /* Return whether a symbol has been used in a reloc. */
  2298. int
  2299. symbol_used_in_reloc_p (symbolS *s)
  2300. {
  2301. if (s->flags.local_symbol)
  2302. return 0;
  2303. return s->flags.used_in_reloc;
  2304. }
  2305. /* Mark a symbol as an MRI common symbol. */
  2306. void
  2307. symbol_mark_mri_common (symbolS *s)
  2308. {
  2309. if (s->flags.local_symbol)
  2310. s = local_symbol_convert (s);
  2311. s->flags.mri_common = 1;
  2312. }
  2313. /* Clear the mark of whether a symbol is an MRI common symbol. */
  2314. void
  2315. symbol_clear_mri_common (symbolS *s)
  2316. {
  2317. if (s->flags.local_symbol)
  2318. return;
  2319. s->flags.mri_common = 0;
  2320. }
  2321. /* Return whether a symbol is an MRI common symbol. */
  2322. int
  2323. symbol_mri_common_p (symbolS *s)
  2324. {
  2325. if (s->flags.local_symbol)
  2326. return 0;
  2327. return s->flags.mri_common;
  2328. }
  2329. /* Mark a symbol as having been written. */
  2330. void
  2331. symbol_mark_written (symbolS *s)
  2332. {
  2333. if (s->flags.local_symbol)
  2334. return;
  2335. s->flags.written = 1;
  2336. }
  2337. /* Clear the mark of whether a symbol has been written. */
  2338. void
  2339. symbol_clear_written (symbolS *s)
  2340. {
  2341. if (s->flags.local_symbol)
  2342. return;
  2343. s->flags.written = 0;
  2344. }
  2345. /* Return whether a symbol has been written. */
  2346. int
  2347. symbol_written_p (symbolS *s)
  2348. {
  2349. if (s->flags.local_symbol)
  2350. return 0;
  2351. return s->flags.written;
  2352. }
  2353. /* Mark a symbol as to be removed. */
  2354. void
  2355. symbol_mark_removed (symbolS *s)
  2356. {
  2357. if (s->flags.local_symbol)
  2358. return;
  2359. s->flags.removed = 1;
  2360. }
  2361. /* Return whether a symbol has been marked to be removed. */
  2362. int
  2363. symbol_removed_p (symbolS *s)
  2364. {
  2365. if (s->flags.local_symbol)
  2366. return 0;
  2367. return s->flags.removed;
  2368. }
  2369. /* Mark a symbol has having been resolved. */
  2370. void
  2371. symbol_mark_resolved (symbolS *s)
  2372. {
  2373. s->flags.resolved = 1;
  2374. }
  2375. /* Return whether a symbol has been resolved. */
  2376. int
  2377. symbol_resolved_p (symbolS *s)
  2378. {
  2379. return s->flags.resolved;
  2380. }
  2381. /* Return whether a symbol is a section symbol. */
  2382. int
  2383. symbol_section_p (symbolS *s)
  2384. {
  2385. if (s->flags.local_symbol)
  2386. return 0;
  2387. return (s->bsym->flags & BSF_SECTION_SYM) != 0;
  2388. }
  2389. /* Return whether a symbol is equated to another symbol. */
  2390. int
  2391. symbol_equated_p (symbolS *s)
  2392. {
  2393. if (s->flags.local_symbol)
  2394. return 0;
  2395. return s->x->value.X_op == O_symbol;
  2396. }
  2397. /* Return whether a symbol is equated to another symbol, and should be
  2398. treated specially when writing out relocs. */
  2399. int
  2400. symbol_equated_reloc_p (symbolS *s)
  2401. {
  2402. if (s->flags.local_symbol)
  2403. return 0;
  2404. /* X_op_symbol, normally not used for O_symbol, is set by
  2405. resolve_symbol_value to flag expression syms that have been
  2406. equated. */
  2407. return (s->x->value.X_op == O_symbol
  2408. #if defined (OBJ_COFF) && defined (TE_PE)
  2409. && ! S_IS_WEAK (s)
  2410. #endif
  2411. && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
  2412. || ! S_IS_DEFINED (s)
  2413. || S_IS_COMMON (s)));
  2414. }
  2415. /* Return whether a symbol has a constant value. */
  2416. int
  2417. symbol_constant_p (symbolS *s)
  2418. {
  2419. if (s->flags.local_symbol)
  2420. return 1;
  2421. return s->x->value.X_op == O_constant;
  2422. }
  2423. /* Return whether a symbol was cloned and thus removed from the global
  2424. symbol list. */
  2425. int
  2426. symbol_shadow_p (symbolS *s)
  2427. {
  2428. if (s->flags.local_symbol)
  2429. return 0;
  2430. return s->x->next == s;
  2431. }
  2432. /* If S is a struct symbol return S, otherwise return NULL. */
  2433. symbolS *
  2434. symbol_symbolS (symbolS *s)
  2435. {
  2436. if (s->flags.local_symbol)
  2437. return NULL;
  2438. return s;
  2439. }
  2440. /* Return the BFD symbol for a symbol. */
  2441. asymbol *
  2442. symbol_get_bfdsym (symbolS *s)
  2443. {
  2444. if (s->flags.local_symbol)
  2445. s = local_symbol_convert (s);
  2446. return s->bsym;
  2447. }
  2448. /* Set the BFD symbol for a symbol. */
  2449. void
  2450. symbol_set_bfdsym (symbolS *s, asymbol *bsym)
  2451. {
  2452. if (s->flags.local_symbol)
  2453. s = local_symbol_convert (s);
  2454. /* Usually, it is harmless to reset a symbol to a BFD section
  2455. symbol. For example, obj_elf_change_section sets the BFD symbol
  2456. of an old symbol with the newly created section symbol. But when
  2457. we have multiple sections with the same name, the newly created
  2458. section may have the same name as an old section. We check if the
  2459. old symbol has been already marked as a section symbol before
  2460. resetting it. */
  2461. if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
  2462. s->bsym = bsym;
  2463. /* else XXX - What do we do now ? */
  2464. }
  2465. #ifdef OBJ_SYMFIELD_TYPE
  2466. /* Get a pointer to the object format information for a symbol. */
  2467. OBJ_SYMFIELD_TYPE *
  2468. symbol_get_obj (symbolS *s)
  2469. {
  2470. if (s->flags.local_symbol)
  2471. s = local_symbol_convert (s);
  2472. return &s->x->obj;
  2473. }
  2474. /* Set the object format information for a symbol. */
  2475. void
  2476. symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
  2477. {
  2478. if (s->flags.local_symbol)
  2479. s = local_symbol_convert (s);
  2480. s->x->obj = *o;
  2481. }
  2482. #endif /* OBJ_SYMFIELD_TYPE */
  2483. #ifdef TC_SYMFIELD_TYPE
  2484. /* Get a pointer to the processor information for a symbol. */
  2485. TC_SYMFIELD_TYPE *
  2486. symbol_get_tc (symbolS *s)
  2487. {
  2488. if (s->flags.local_symbol)
  2489. s = local_symbol_convert (s);
  2490. return &s->x->tc;
  2491. }
  2492. /* Set the processor information for a symbol. */
  2493. void
  2494. symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
  2495. {
  2496. if (s->flags.local_symbol)
  2497. s = local_symbol_convert (s);
  2498. s->x->tc = *o;
  2499. }
  2500. #endif /* TC_SYMFIELD_TYPE */
  2501. void
  2502. symbol_begin (void)
  2503. {
  2504. symbol_lastP = NULL;
  2505. symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
  2506. sy_hash = htab_create_alloc (16, hash_symbol_entry, eq_symbol_entry,
  2507. NULL, xcalloc, free);
  2508. #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
  2509. abs_symbol.bsym = bfd_abs_section_ptr->symbol;
  2510. #endif
  2511. abs_symbol.x = &abs_symbol_x;
  2512. abs_symbol.x->value.X_op = O_constant;
  2513. abs_symbol.frag = &zero_address_frag;
  2514. if (LOCAL_LABELS_FB)
  2515. fb_label_init ();
  2516. }
  2517. void
  2518. dot_symbol_init (void)
  2519. {
  2520. dot_symbol.name = ".";
  2521. dot_symbol.flags.forward_ref = 1;
  2522. dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
  2523. if (dot_symbol.bsym == NULL)
  2524. as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
  2525. dot_symbol.bsym->name = ".";
  2526. dot_symbol.x = &dot_symbol_x;
  2527. dot_symbol.x->value.X_op = O_constant;
  2528. }
  2529. int indent_level;
  2530. /* Maximum indent level.
  2531. Available for modification inside a gdb session. */
  2532. static int max_indent_level = 8;
  2533. void
  2534. print_symbol_value_1 (FILE *file, symbolS *sym)
  2535. {
  2536. const char *name = S_GET_NAME (sym);
  2537. if (!name || !name[0])
  2538. name = "(unnamed)";
  2539. fprintf (file, "sym ");
  2540. fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
  2541. fprintf (file, " %s", name);
  2542. if (sym->flags.local_symbol)
  2543. {
  2544. struct local_symbol *locsym = (struct local_symbol *) sym;
  2545. if (locsym->frag != &zero_address_frag
  2546. && locsym->frag != NULL)
  2547. {
  2548. fprintf (file, " frag ");
  2549. fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) locsym->frag));
  2550. }
  2551. if (locsym->flags.resolved)
  2552. fprintf (file, " resolved");
  2553. fprintf (file, " local");
  2554. }
  2555. else
  2556. {
  2557. if (sym->frag != &zero_address_frag)
  2558. {
  2559. fprintf (file, " frag ");
  2560. fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->frag));
  2561. }
  2562. if (sym->flags.written)
  2563. fprintf (file, " written");
  2564. if (sym->flags.resolved)
  2565. fprintf (file, " resolved");
  2566. else if (sym->flags.resolving)
  2567. fprintf (file, " resolving");
  2568. if (sym->flags.used_in_reloc)
  2569. fprintf (file, " used-in-reloc");
  2570. if (sym->flags.used)
  2571. fprintf (file, " used");
  2572. if (S_IS_LOCAL (sym))
  2573. fprintf (file, " local");
  2574. if (S_IS_EXTERNAL (sym))
  2575. fprintf (file, " extern");
  2576. if (S_IS_WEAK (sym))
  2577. fprintf (file, " weak");
  2578. if (S_IS_DEBUG (sym))
  2579. fprintf (file, " debug");
  2580. if (S_IS_DEFINED (sym))
  2581. fprintf (file, " defined");
  2582. }
  2583. if (S_IS_WEAKREFR (sym))
  2584. fprintf (file, " weakrefr");
  2585. if (S_IS_WEAKREFD (sym))
  2586. fprintf (file, " weakrefd");
  2587. fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
  2588. if (symbol_resolved_p (sym))
  2589. {
  2590. segT s = S_GET_SEGMENT (sym);
  2591. if (s != undefined_section
  2592. && s != expr_section)
  2593. fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
  2594. }
  2595. else if (indent_level < max_indent_level
  2596. && S_GET_SEGMENT (sym) != undefined_section)
  2597. {
  2598. indent_level++;
  2599. fprintf (file, "\n%*s<", indent_level * 4, "");
  2600. if (sym->flags.local_symbol)
  2601. fprintf (file, "constant %lx",
  2602. (unsigned long) ((struct local_symbol *) sym)->value);
  2603. else
  2604. print_expr_1 (file, &sym->x->value);
  2605. fprintf (file, ">");
  2606. indent_level--;
  2607. }
  2608. fflush (file);
  2609. }
  2610. void
  2611. print_symbol_value (symbolS *sym)
  2612. {
  2613. indent_level = 0;
  2614. print_symbol_value_1 (stderr, sym);
  2615. fprintf (stderr, "\n");
  2616. }
  2617. static void
  2618. print_binary (FILE *file, const char *name, expressionS *exp)
  2619. {
  2620. indent_level++;
  2621. fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
  2622. print_symbol_value_1 (file, exp->X_add_symbol);
  2623. fprintf (file, ">\n%*s<", indent_level * 4, "");
  2624. print_symbol_value_1 (file, exp->X_op_symbol);
  2625. fprintf (file, ">");
  2626. indent_level--;
  2627. }
  2628. void
  2629. print_expr_1 (FILE *file, expressionS *exp)
  2630. {
  2631. fprintf (file, "expr ");
  2632. fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
  2633. fprintf (file, " ");
  2634. switch (exp->X_op)
  2635. {
  2636. case O_illegal:
  2637. fprintf (file, "illegal");
  2638. break;
  2639. case O_absent:
  2640. fprintf (file, "absent");
  2641. break;
  2642. case O_constant:
  2643. fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
  2644. break;
  2645. case O_symbol:
  2646. indent_level++;
  2647. fprintf (file, "symbol\n%*s<", indent_level * 4, "");
  2648. print_symbol_value_1 (file, exp->X_add_symbol);
  2649. fprintf (file, ">");
  2650. maybe_print_addnum:
  2651. if (exp->X_add_number)
  2652. fprintf (file, "\n%*s%lx", indent_level * 4, "",
  2653. (unsigned long) exp->X_add_number);
  2654. indent_level--;
  2655. break;
  2656. case O_register:
  2657. fprintf (file, "register #%d", (int) exp->X_add_number);
  2658. break;
  2659. case O_big:
  2660. fprintf (file, "big");
  2661. break;
  2662. case O_uminus:
  2663. fprintf (file, "uminus -<");
  2664. indent_level++;
  2665. print_symbol_value_1 (file, exp->X_add_symbol);
  2666. fprintf (file, ">");
  2667. goto maybe_print_addnum;
  2668. case O_bit_not:
  2669. fprintf (file, "bit_not");
  2670. break;
  2671. case O_multiply:
  2672. print_binary (file, "multiply", exp);
  2673. break;
  2674. case O_divide:
  2675. print_binary (file, "divide", exp);
  2676. break;
  2677. case O_modulus:
  2678. print_binary (file, "modulus", exp);
  2679. break;
  2680. case O_left_shift:
  2681. print_binary (file, "lshift", exp);
  2682. break;
  2683. case O_right_shift:
  2684. print_binary (file, "rshift", exp);
  2685. break;
  2686. case O_bit_inclusive_or:
  2687. print_binary (file, "bit_ior", exp);
  2688. break;
  2689. case O_bit_exclusive_or:
  2690. print_binary (file, "bit_xor", exp);
  2691. break;
  2692. case O_bit_and:
  2693. print_binary (file, "bit_and", exp);
  2694. break;
  2695. case O_eq:
  2696. print_binary (file, "eq", exp);
  2697. break;
  2698. case O_ne:
  2699. print_binary (file, "ne", exp);
  2700. break;
  2701. case O_lt:
  2702. print_binary (file, "lt", exp);
  2703. break;
  2704. case O_le:
  2705. print_binary (file, "le", exp);
  2706. break;
  2707. case O_ge:
  2708. print_binary (file, "ge", exp);
  2709. break;
  2710. case O_gt:
  2711. print_binary (file, "gt", exp);
  2712. break;
  2713. case O_logical_and:
  2714. print_binary (file, "logical_and", exp);
  2715. break;
  2716. case O_logical_or:
  2717. print_binary (file, "logical_or", exp);
  2718. break;
  2719. case O_add:
  2720. indent_level++;
  2721. fprintf (file, "add\n%*s<", indent_level * 4, "");
  2722. print_symbol_value_1 (file, exp->X_add_symbol);
  2723. fprintf (file, ">\n%*s<", indent_level * 4, "");
  2724. print_symbol_value_1 (file, exp->X_op_symbol);
  2725. fprintf (file, ">");
  2726. goto maybe_print_addnum;
  2727. case O_subtract:
  2728. indent_level++;
  2729. fprintf (file, "subtract\n%*s<", indent_level * 4, "");
  2730. print_symbol_value_1 (file, exp->X_add_symbol);
  2731. fprintf (file, ">\n%*s<", indent_level * 4, "");
  2732. print_symbol_value_1 (file, exp->X_op_symbol);
  2733. fprintf (file, ">");
  2734. goto maybe_print_addnum;
  2735. default:
  2736. fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
  2737. break;
  2738. }
  2739. fflush (stdout);
  2740. }
  2741. void
  2742. print_expr (expressionS *exp)
  2743. {
  2744. print_expr_1 (stderr, exp);
  2745. fprintf (stderr, "\n");
  2746. }
  2747. void
  2748. symbol_print_statistics (FILE *file)
  2749. {
  2750. htab_print_statistics (file, "symbol table", sy_hash);
  2751. fprintf (file, "%lu mini local symbols created, %lu converted\n",
  2752. local_symbol_count, local_symbol_conversion_count);
  2753. }
  2754. #ifdef OBJ_COMPLEX_RELC
  2755. /* Convert given symbol to a new complex-relocation symbol name. This
  2756. may be a recursive function, since it might be called for non-leaf
  2757. nodes (plain symbols) in the expression tree. The caller owns the
  2758. returning string, so should free it eventually. Errors are
  2759. indicated via as_bad and a NULL return value. The given symbol
  2760. is marked with used_in_reloc. */
  2761. char *
  2762. symbol_relc_make_sym (symbolS * sym)
  2763. {
  2764. char * terminal = NULL;
  2765. const char * sname;
  2766. char typetag;
  2767. int sname_len;
  2768. gas_assert (sym != NULL);
  2769. /* Recurse to symbol_relc_make_expr if this symbol
  2770. is defined as an expression or a plain value. */
  2771. if ( S_GET_SEGMENT (sym) == expr_section
  2772. || S_GET_SEGMENT (sym) == absolute_section)
  2773. return symbol_relc_make_expr (symbol_get_value_expression (sym));
  2774. /* This may be a "fake symbol", referring to ".".
  2775. Write out a special null symbol to refer to this position. */
  2776. if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
  2777. return xstrdup (".");
  2778. /* We hope this is a plain leaf symbol. Construct the encoding
  2779. as {S,s}II...:CCCCCCC....
  2780. where 'S'/'s' means section symbol / plain symbol
  2781. III is decimal for the symbol name length
  2782. CCC is the symbol name itself. */
  2783. symbol_mark_used_in_reloc (sym);
  2784. sname = S_GET_NAME (sym);
  2785. sname_len = strlen (sname);
  2786. typetag = symbol_section_p (sym) ? 'S' : 's';
  2787. terminal = XNEWVEC (char, (1 /* S or s */
  2788. + 8 /* sname_len in decimal */
  2789. + 1 /* _ spacer */
  2790. + sname_len /* name itself */
  2791. + 1 /* \0 */ ));
  2792. sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
  2793. return terminal;
  2794. }
  2795. /* Convert given value to a new complex-relocation symbol name. This
  2796. is a non-recursive function, since it is be called for leaf nodes
  2797. (plain values) in the expression tree. The caller owns the
  2798. returning string, so should free() it eventually. No errors. */
  2799. char *
  2800. symbol_relc_make_value (offsetT val)
  2801. {
  2802. char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
  2803. terminal[0] = '#';
  2804. bfd_sprintf_vma (stdoutput, terminal + 1, val);
  2805. return terminal;
  2806. }
  2807. /* Convert given expression to a new complex-relocation symbol name.
  2808. This is a recursive function, since it traverses the entire given
  2809. expression tree. The caller owns the returning string, so should
  2810. free() it eventually. Errors are indicated via as_bad() and a NULL
  2811. return value. */
  2812. char *
  2813. symbol_relc_make_expr (expressionS * exp)
  2814. {
  2815. const char * opstr = NULL; /* Operator prefix string. */
  2816. int arity = 0; /* Arity of this operator. */
  2817. char * operands[3]; /* Up to three operands. */
  2818. char * concat_string = NULL;
  2819. operands[0] = operands[1] = operands[2] = NULL;
  2820. gas_assert (exp != NULL);
  2821. /* Match known operators -> fill in opstr, arity, operands[] and fall
  2822. through to construct subexpression fragments; may instead return
  2823. string directly for leaf nodes. */
  2824. /* See expr.h for the meaning of all these enums. Many operators
  2825. have an unnatural arity (X_add_number implicitly added). The
  2826. conversion logic expands them to explicit "+" subexpressions. */
  2827. switch (exp->X_op)
  2828. {
  2829. default:
  2830. as_bad ("Unknown expression operator (enum %d)", exp->X_op);
  2831. break;
  2832. /* Leaf nodes. */
  2833. case O_constant:
  2834. return symbol_relc_make_value (exp->X_add_number);
  2835. case O_symbol:
  2836. if (exp->X_add_number)
  2837. {
  2838. arity = 2;
  2839. opstr = "+";
  2840. operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
  2841. operands[1] = symbol_relc_make_value (exp->X_add_number);
  2842. break;
  2843. }
  2844. else
  2845. return symbol_relc_make_sym (exp->X_add_symbol);
  2846. /* Helper macros for nesting nodes. */
  2847. #define HANDLE_XADD_OPT1(str_) \
  2848. if (exp->X_add_number) \
  2849. { \
  2850. arity = 2; \
  2851. opstr = "+:" str_; \
  2852. operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
  2853. operands[1] = symbol_relc_make_value (exp->X_add_number); \
  2854. break; \
  2855. } \
  2856. else \
  2857. { \
  2858. arity = 1; \
  2859. opstr = str_; \
  2860. operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
  2861. } \
  2862. break
  2863. #define HANDLE_XADD_OPT2(str_) \
  2864. if (exp->X_add_number) \
  2865. { \
  2866. arity = 3; \
  2867. opstr = "+:" str_; \
  2868. operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
  2869. operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
  2870. operands[2] = symbol_relc_make_value (exp->X_add_number); \
  2871. } \
  2872. else \
  2873. { \
  2874. arity = 2; \
  2875. opstr = str_; \
  2876. operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
  2877. operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
  2878. } \
  2879. break
  2880. /* Nesting nodes. */
  2881. case O_uminus: HANDLE_XADD_OPT1 ("0-");
  2882. case O_bit_not: HANDLE_XADD_OPT1 ("~");
  2883. case O_logical_not: HANDLE_XADD_OPT1 ("!");
  2884. case O_multiply: HANDLE_XADD_OPT2 ("*");
  2885. case O_divide: HANDLE_XADD_OPT2 ("/");
  2886. case O_modulus: HANDLE_XADD_OPT2 ("%");
  2887. case O_left_shift: HANDLE_XADD_OPT2 ("<<");
  2888. case O_right_shift: HANDLE_XADD_OPT2 (">>");
  2889. case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
  2890. case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
  2891. case O_bit_and: HANDLE_XADD_OPT2 ("&");
  2892. case O_add: HANDLE_XADD_OPT2 ("+");
  2893. case O_subtract: HANDLE_XADD_OPT2 ("-");
  2894. case O_eq: HANDLE_XADD_OPT2 ("==");
  2895. case O_ne: HANDLE_XADD_OPT2 ("!=");
  2896. case O_lt: HANDLE_XADD_OPT2 ("<");
  2897. case O_le: HANDLE_XADD_OPT2 ("<=");
  2898. case O_ge: HANDLE_XADD_OPT2 (">=");
  2899. case O_gt: HANDLE_XADD_OPT2 (">");
  2900. case O_logical_and: HANDLE_XADD_OPT2 ("&&");
  2901. case O_logical_or: HANDLE_XADD_OPT2 ("||");
  2902. }
  2903. /* Validate & reject early. */
  2904. if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
  2905. opstr = NULL;
  2906. if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
  2907. opstr = NULL;
  2908. if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
  2909. opstr = NULL;
  2910. if (opstr == NULL)
  2911. concat_string = NULL;
  2912. else if (arity == 0)
  2913. concat_string = xstrdup (opstr);
  2914. else if (arity == 1)
  2915. concat_string = concat (opstr, ":", operands[0], (char *) NULL);
  2916. else if (arity == 2)
  2917. concat_string = concat (opstr, ":", operands[0], ":", operands[1],
  2918. (char *) NULL);
  2919. else
  2920. concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
  2921. operands[2], (char *) NULL);
  2922. /* Free operand strings (not opstr). */
  2923. if (arity >= 1) xfree (operands[0]);
  2924. if (arity >= 2) xfree (operands[1]);
  2925. if (arity >= 3) xfree (operands[2]);
  2926. return concat_string;
  2927. }
  2928. #endif