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