symbols.c 83 KB

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