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