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