]>
Commit | Line | Data |
---|---|---|
fecd2382 | 1 | /* symbols.c -symbol table- |
dacf29ea KR |
2 | Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994 |
3 | Free Software Foundation, Inc. | |
6efd877d | 4 | |
a39116f1 | 5 | This file is part of GAS, the GNU Assembler. |
6efd877d | 6 | |
a39116f1 RP |
7 | GAS is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
6efd877d | 11 | |
a39116f1 RP |
12 | GAS is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
6efd877d | 16 | |
a39116f1 RP |
17 | You should have received a copy of the GNU General Public License |
18 | along with GAS; see the file COPYING. If not, write to | |
1356d77d | 19 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
fecd2382 | 20 | |
58d4951d | 21 | /* #define DEBUG_SYMS / * to debug symbol list maintenance */ |
2b68b820 | 22 | |
6efd877d KR |
23 | #include <ctype.h> |
24 | ||
fecd2382 RP |
25 | #include "as.h" |
26 | ||
27 | #include "obstack.h" /* For "symbols.h" */ | |
28 | #include "subsegs.h" | |
29 | ||
1356d77d ILT |
30 | /* This is non-zero if symbols are case sensitive, which is the |
31 | default. */ | |
32 | int symbols_case_sensitive = 1; | |
33 | ||
fecd2382 RP |
34 | #ifndef WORKING_DOT_WORD |
35 | extern int new_broken_words; | |
36 | #endif | |
fecd2382 | 37 | |
2b68b820 KR |
38 | /* symbol-name => struct symbol pointer */ |
39 | static struct hash_control *sy_hash; | |
fecd2382 | 40 | |
a39116f1 | 41 | /* Below are commented in "symbols.h". */ |
6efd877d KR |
42 | symbolS *symbol_rootP; |
43 | symbolS *symbol_lastP; | |
44 | symbolS abs_symbol; | |
fecd2382 | 45 | |
dacf29ea KR |
46 | #ifdef DEBUG_SYMS |
47 | #define debug_verify_symchain verify_symbol_chain | |
48 | #else | |
49 | #define debug_verify_symchain (void) | |
50 | #endif | |
51 | ||
6efd877d | 52 | struct obstack notes; |
fecd2382 | 53 | |
85825401 | 54 | static void fb_label_init PARAMS ((void)); |
fecd2382 | 55 | |
dacf29ea KR |
56 | /* symbol_new() |
57 | ||
58 | Return a pointer to a new symbol. Die if we can't make a new | |
59 | symbol. Fill in the symbol's values. Add symbol to end of symbol | |
60 | chain. | |
61 | ||
62 | This function should be called in the general case of creating a | |
63 | symbol. However, if the output file symbol table has already been | |
64 | set, and you are certain that this symbol won't be wanted in the | |
65 | output file, you can call symbol_create. */ | |
fecd2382 | 66 | |
6efd877d | 67 | symbolS * |
604633ae | 68 | symbol_new (name, segment, valu, frag) |
dacf29ea KR |
69 | const char *name; |
70 | segT segment; | |
71 | valueT valu; | |
72 | fragS *frag; | |
73 | { | |
74 | symbolS *symbolP = symbol_create (name, segment, valu, frag); | |
75 | ||
76 | /* | |
77 | * Link to end of symbol chain. | |
78 | */ | |
79 | #ifdef BFD_ASSEMBLER | |
80 | { | |
81 | extern int symbol_table_frozen; | |
82 | if (symbol_table_frozen) | |
83 | abort (); | |
84 | } | |
85 | #endif | |
86 | symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP); | |
87 | debug_verify_symchain (symbol_rootP, symbol_lastP); | |
88 | ||
89 | return symbolP; | |
90 | } | |
91 | ||
92 | symbolS * | |
93 | symbol_create (name, segment, valu, frag) | |
94 | const char *name; /* It is copied, the caller can destroy/modify */ | |
6efd877d | 95 | segT segment; /* Segment identifier (SEG_<something>) */ |
604633ae | 96 | valueT valu; /* Symbol value */ |
6efd877d | 97 | fragS *frag; /* Associated fragment */ |
fecd2382 | 98 | { |
6efd877d KR |
99 | unsigned int name_length; |
100 | char *preserved_copy_of_name; | |
101 | symbolS *symbolP; | |
102 | ||
103 | name_length = strlen (name) + 1; /* +1 for \0 */ | |
104 | obstack_grow (¬es, name, name_length); | |
105 | preserved_copy_of_name = obstack_finish (¬es); | |
2b68b820 KR |
106 | #ifdef STRIP_UNDERSCORE |
107 | if (preserved_copy_of_name[0] == '_') | |
108 | preserved_copy_of_name++; | |
109 | #endif | |
dacf29ea KR |
110 | |
111 | #ifdef tc_canonicalize_symbol_name | |
112 | preserved_copy_of_name = | |
113 | tc_canonicalize_symbol_name (preserved_copy_of_name); | |
114 | #endif | |
115 | ||
1356d77d ILT |
116 | if (! symbols_case_sensitive) |
117 | { | |
118 | unsigned char *s; | |
119 | ||
120 | for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++) | |
121 | if (islower (*s)) | |
122 | *s = toupper (*s); | |
123 | } | |
124 | ||
6efd877d KR |
125 | symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS)); |
126 | ||
127 | /* symbol must be born in some fixed state. This seems as good as any. */ | |
128 | memset (symbolP, 0, sizeof (symbolS)); | |
129 | ||
2b68b820 KR |
130 | #ifdef BFD_ASSEMBLER |
131 | symbolP->bsym = bfd_make_empty_symbol (stdoutput); | |
132 | assert (symbolP->bsym != 0); | |
87e48495 | 133 | symbolP->bsym->udata.p = (PTR) symbolP; |
2b68b820 | 134 | #endif |
6efd877d | 135 | S_SET_NAME (symbolP, preserved_copy_of_name); |
6efd877d KR |
136 | |
137 | S_SET_SEGMENT (symbolP, segment); | |
604633ae | 138 | S_SET_VALUE (symbolP, valu); |
dacf29ea | 139 | symbol_clear_list_pointers (symbolP); |
6efd877d KR |
140 | |
141 | symbolP->sy_frag = frag; | |
2b68b820 | 142 | #ifndef BFD_ASSEMBLER |
6efd877d | 143 | symbolP->sy_number = ~0; |
0cafaab1 | 144 | symbolP->sy_name_offset = (unsigned int) ~0; |
2b68b820 | 145 | #endif |
6efd877d | 146 | |
6efd877d KR |
147 | obj_symbol_new_hook (symbolP); |
148 | ||
dacf29ea KR |
149 | #ifdef tc_symbol_new_hook |
150 | tc_symbol_new_hook (symbolP); | |
151 | #endif | |
fecd2382 | 152 | |
2b68b820 KR |
153 | return symbolP; |
154 | } | |
fecd2382 | 155 | \f |
6efd877d | 156 | |
fecd2382 RP |
157 | /* |
158 | * colon() | |
159 | * | |
160 | * We have just seen "<name>:". | |
161 | * Creates a struct symbol unless it already exists. | |
162 | * | |
163 | * Gripes if we are redefining a symbol incompatibly (and ignores it). | |
164 | * | |
165 | */ | |
1356d77d | 166 | symbolS * |
6efd877d | 167 | colon (sym_name) /* just seen "x:" - rattle symbols & frags */ |
2a0f64a5 | 168 | const char *sym_name; /* symbol name, as a cannonical string */ |
6efd877d | 169 | /* We copy this string: OK to alter later. */ |
fecd2382 | 170 | { |
6efd877d KR |
171 | register symbolS *symbolP; /* symbol we are working with */ |
172 | ||
2b68b820 KR |
173 | /* Sun local labels go out of scope whenever a non-local symbol is |
174 | defined. */ | |
56dc989a | 175 | if (LOCAL_LABELS_DOLLAR && *sym_name != 'L') |
6efd877d | 176 | dollar_label_clear (); |
6efd877d | 177 | |
fecd2382 | 178 | #ifndef WORKING_DOT_WORD |
6efd877d KR |
179 | if (new_broken_words) |
180 | { | |
181 | struct broken_word *a; | |
182 | int possible_bytes; | |
183 | fragS *frag_tmp; | |
184 | char *frag_opcode; | |
185 | ||
2b68b820 KR |
186 | extern const int md_short_jump_size; |
187 | extern const int md_long_jump_size; | |
188 | possible_bytes = (md_short_jump_size | |
189 | + new_broken_words * md_long_jump_size); | |
6efd877d KR |
190 | |
191 | frag_tmp = frag_now; | |
192 | frag_opcode = frag_var (rs_broken_word, | |
193 | possible_bytes, | |
194 | possible_bytes, | |
195 | (relax_substateT) 0, | |
196 | (symbolS *) broken_words, | |
197 | 0L, | |
198 | NULL); | |
199 | ||
200 | /* We want to store the pointer to where to insert the jump table in the | |
2b68b820 KR |
201 | fr_opcode of the rs_broken_word frag. This requires a little |
202 | hackery. */ | |
203 | while (frag_tmp | |
204 | && (frag_tmp->fr_type != rs_broken_word | |
205 | || frag_tmp->fr_opcode)) | |
6efd877d KR |
206 | frag_tmp = frag_tmp->fr_next; |
207 | know (frag_tmp); | |
208 | frag_tmp->fr_opcode = frag_opcode; | |
209 | new_broken_words = 0; | |
210 | ||
211 | for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word) | |
212 | a->dispfrag = frag_tmp; | |
213 | } | |
214 | #endif /* WORKING_DOT_WORD */ | |
215 | ||
216 | if ((symbolP = symbol_find (sym_name)) != 0) | |
217 | { | |
2b68b820 KR |
218 | #ifdef RESOLVE_SYMBOL_REDEFINITION |
219 | if (RESOLVE_SYMBOL_REDEFINITION (symbolP)) | |
1356d77d | 220 | return symbolP; |
2b68b820 | 221 | #endif |
6efd877d | 222 | /* |
2b68b820 KR |
223 | * Now check for undefined symbols |
224 | */ | |
6efd877d KR |
225 | if (!S_IS_DEFINED (symbolP)) |
226 | { | |
227 | if (S_GET_VALUE (symbolP) == 0) | |
228 | { | |
229 | symbolP->sy_frag = frag_now; | |
2b68b820 KR |
230 | #ifdef OBJ_VMS |
231 | S_GET_OTHER(symbolP) = const_flag; | |
fecd2382 | 232 | #endif |
87e48495 | 233 | S_SET_VALUE (symbolP, (valueT) frag_now_fix ()); |
6efd877d | 234 | S_SET_SEGMENT (symbolP, now_seg); |
fecd2382 | 235 | #ifdef N_UNDF |
6efd877d | 236 | know (N_UNDF == 0); |
fecd2382 | 237 | #endif /* if we have one, it better be zero. */ |
6efd877d KR |
238 | |
239 | } | |
240 | else | |
241 | { | |
242 | /* | |
2b68b820 KR |
243 | * There are still several cases to check: |
244 | * A .comm/.lcomm symbol being redefined as | |
245 | * initialized data is OK | |
246 | * A .comm/.lcomm symbol being redefined with | |
247 | * a larger size is also OK | |
248 | * | |
249 | * This only used to be allowed on VMS gas, but Sun cc | |
250 | * on the sparc also depends on it. | |
251 | */ | |
252 | ||
253 | if (((!S_IS_DEBUG (symbolP) | |
254 | && !S_IS_DEFINED (symbolP) | |
255 | && S_IS_EXTERNAL (symbolP)) | |
256 | || S_GET_SEGMENT (symbolP) == bss_section) | |
257 | && (now_seg == data_section | |
258 | || now_seg == S_GET_SEGMENT (symbolP))) | |
6efd877d KR |
259 | { |
260 | /* | |
2b68b820 KR |
261 | * Select which of the 2 cases this is |
262 | */ | |
263 | if (now_seg != data_section) | |
6efd877d KR |
264 | { |
265 | /* | |
2b68b820 KR |
266 | * New .comm for prev .comm symbol. |
267 | * If the new size is larger we just | |
268 | * change its value. If the new size | |
269 | * is smaller, we ignore this symbol | |
270 | */ | |
6efd877d | 271 | if (S_GET_VALUE (symbolP) |
2b68b820 | 272 | < ((unsigned) frag_now_fix ())) |
6efd877d | 273 | { |
2b68b820 | 274 | S_SET_VALUE (symbolP, (valueT) frag_now_fix ()); |
6efd877d KR |
275 | } |
276 | } | |
277 | else | |
278 | { | |
85825401 ILT |
279 | /* It is a .comm/.lcomm being converted to initialized |
280 | data. */ | |
6efd877d | 281 | symbolP->sy_frag = frag_now; |
2b68b820 KR |
282 | #ifdef OBJ_VMS |
283 | S_GET_OTHER(symbolP) = const_flag; | |
284 | #endif /* OBJ_VMS */ | |
285 | S_SET_VALUE (symbolP, (valueT) frag_now_fix ()); | |
6efd877d KR |
286 | S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */ |
287 | } | |
288 | } | |
289 | else | |
290 | { | |
2b68b820 KR |
291 | #if defined (S_GET_OTHER) && defined (S_GET_DESC) |
292 | as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.", | |
6efd877d KR |
293 | sym_name, |
294 | segment_name (S_GET_SEGMENT (symbolP)), | |
2b68b820 KR |
295 | S_GET_OTHER (symbolP), S_GET_DESC (symbolP), |
296 | (long) S_GET_VALUE (symbolP)); | |
297 | #else | |
58d4951d | 298 | as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.", |
6efd877d KR |
299 | sym_name, |
300 | segment_name (S_GET_SEGMENT (symbolP)), | |
58d4951d | 301 | (long) S_GET_VALUE (symbolP)); |
2b68b820 | 302 | #endif |
6efd877d KR |
303 | } |
304 | } /* if the undefined symbol has no value */ | |
305 | } | |
306 | else | |
307 | { | |
308 | /* Don't blow up if the definition is the same */ | |
309 | if (!(frag_now == symbolP->sy_frag | |
87e48495 | 310 | && S_GET_VALUE (symbolP) == frag_now_fix () |
6efd877d KR |
311 | && S_GET_SEGMENT (symbolP) == now_seg)) |
312 | as_fatal ("Symbol %s already defined.", sym_name); | |
313 | } /* if this symbol is not yet defined */ | |
314 | ||
315 | } | |
316 | else | |
317 | { | |
87e48495 | 318 | symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (), |
6efd877d | 319 | frag_now); |
2b68b820 | 320 | #ifdef OBJ_VMS |
6efd877d | 321 | S_SET_OTHER (symbolP, const_flag); |
2b68b820 | 322 | #endif /* OBJ_VMS */ |
fecd2382 | 323 | |
6efd877d KR |
324 | symbol_table_insert (symbolP); |
325 | } /* if we have seen this symbol before */ | |
1e9cf565 | 326 | |
1356d77d ILT |
327 | if (mri_common_symbol != NULL) |
328 | { | |
329 | /* This symbol is actually being defined within an MRI common | |
330 | section. This requires special handling. */ | |
331 | symbolP->sy_value.X_op = O_symbol; | |
332 | symbolP->sy_value.X_add_symbol = mri_common_symbol; | |
333 | symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol); | |
334 | symbolP->sy_frag = &zero_address_frag; | |
335 | S_SET_SEGMENT (symbolP, expr_section); | |
336 | symbolP->sy_mri_common = 1; | |
337 | } | |
338 | ||
1e9cf565 ILT |
339 | #ifdef tc_frob_label |
340 | tc_frob_label (symbolP); | |
2b68b820 | 341 | #endif |
1356d77d ILT |
342 | |
343 | return symbolP; | |
1ecd6c4a | 344 | } |
fecd2382 | 345 | \f |
6efd877d | 346 | |
fecd2382 RP |
347 | /* |
348 | * symbol_table_insert() | |
349 | * | |
350 | * Die if we can't insert the symbol. | |
351 | * | |
352 | */ | |
353 | ||
6efd877d KR |
354 | void |
355 | symbol_table_insert (symbolP) | |
356 | symbolS *symbolP; | |
fecd2382 | 357 | { |
604633ae | 358 | register const char *error_string; |
6efd877d KR |
359 | |
360 | know (symbolP); | |
361 | know (S_GET_NAME (symbolP)); | |
362 | ||
0cafaab1 | 363 | if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP))) |
6efd877d KR |
364 | { |
365 | as_fatal ("Inserting \"%s\" into symbol table failed: %s", | |
366 | S_GET_NAME (symbolP), error_string); | |
367 | } /* on error */ | |
368 | } /* symbol_table_insert() */ | |
fecd2382 RP |
369 | \f |
370 | /* | |
371 | * symbol_find_or_make() | |
372 | * | |
373 | * If a symbol name does not exist, create it as undefined, and insert | |
374 | * it into the symbol table. Return a pointer to it. | |
375 | */ | |
6efd877d KR |
376 | symbolS * |
377 | symbol_find_or_make (name) | |
378 | char *name; | |
fecd2382 | 379 | { |
6efd877d KR |
380 | register symbolS *symbolP; |
381 | ||
382 | symbolP = symbol_find (name); | |
383 | ||
384 | if (symbolP == NULL) | |
385 | { | |
386 | symbolP = symbol_make (name); | |
387 | ||
388 | symbol_table_insert (symbolP); | |
389 | } /* if symbol wasn't found */ | |
390 | ||
391 | return (symbolP); | |
392 | } /* symbol_find_or_make() */ | |
393 | ||
394 | symbolS * | |
395 | symbol_make (name) | |
2b68b820 | 396 | CONST char *name; |
fecd2382 | 397 | { |
6efd877d KR |
398 | symbolS *symbolP; |
399 | ||
400 | /* Let the machine description default it, e.g. for register names. */ | |
2b68b820 | 401 | symbolP = md_undefined_symbol ((char *) name); |
6efd877d KR |
402 | |
403 | if (!symbolP) | |
2b68b820 | 404 | symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag); |
6efd877d KR |
405 | |
406 | return (symbolP); | |
407 | } /* symbol_make() */ | |
fecd2382 RP |
408 | |
409 | /* | |
410 | * symbol_find() | |
6efd877d | 411 | * |
fecd2382 RP |
412 | * Implement symbol table lookup. |
413 | * In: A symbol's name as a string: '\0' can't be part of a symbol name. | |
414 | * Out: NULL if the name was not in the symbol table, else the address | |
415 | * of a struct symbol associated with that name. | |
416 | */ | |
417 | ||
6efd877d KR |
418 | symbolS * |
419 | symbol_find (name) | |
2b68b820 | 420 | CONST char *name; |
fecd2382 | 421 | { |
a6c6eaf8 | 422 | #ifdef STRIP_UNDERSCORE |
6efd877d | 423 | return (symbol_find_base (name, 1)); |
a6c6eaf8 | 424 | #else /* STRIP_UNDERSCORE */ |
6efd877d | 425 | return (symbol_find_base (name, 0)); |
fecd2382 | 426 | #endif /* STRIP_UNDERSCORE */ |
6efd877d | 427 | } /* symbol_find() */ |
fecd2382 | 428 | |
6efd877d KR |
429 | symbolS * |
430 | symbol_find_base (name, strip_underscore) | |
2b68b820 | 431 | CONST char *name; |
6efd877d | 432 | int strip_underscore; |
fecd2382 | 433 | { |
6efd877d KR |
434 | if (strip_underscore && *name == '_') |
435 | name++; | |
dacf29ea KR |
436 | |
437 | #ifdef tc_canonicalize_symbol_name | |
438 | { | |
439 | char *copy; | |
440 | ||
441 | copy = (char *) alloca (strlen (name) + 1); | |
442 | strcpy (copy, name); | |
443 | name = tc_canonicalize_symbol_name (copy); | |
444 | } | |
445 | #endif | |
446 | ||
1356d77d ILT |
447 | if (! symbols_case_sensitive) |
448 | { | |
449 | unsigned char *copy; | |
450 | ||
451 | copy = (unsigned char *) alloca (strlen (name) + 1); | |
452 | name = (const char *) copy; | |
453 | for (; *copy != '\0'; copy++) | |
454 | if (islower (*copy)) | |
455 | *copy = toupper (*copy); | |
456 | } | |
457 | ||
6efd877d | 458 | return ((symbolS *) hash_find (sy_hash, name)); |
fecd2382 RP |
459 | } |
460 | ||
461 | /* | |
462 | * Once upon a time, symbols were kept in a singly linked list. At | |
463 | * least coff needs to be able to rearrange them from time to time, for | |
464 | * which a doubly linked list is much more convenient. Loic did these | |
465 | * as macros which seemed dangerous to me so they're now functions. | |
466 | * xoxorich. | |
467 | */ | |
468 | ||
469 | /* Link symbol ADDME after symbol TARGET in the chain. */ | |
6efd877d KR |
470 | void |
471 | symbol_append (addme, target, rootPP, lastPP) | |
472 | symbolS *addme; | |
473 | symbolS *target; | |
474 | symbolS **rootPP; | |
475 | symbolS **lastPP; | |
fecd2382 | 476 | { |
6efd877d KR |
477 | if (target == NULL) |
478 | { | |
479 | know (*rootPP == NULL); | |
480 | know (*lastPP == NULL); | |
481 | *rootPP = addme; | |
482 | *lastPP = addme; | |
483 | return; | |
484 | } /* if the list is empty */ | |
485 | ||
486 | if (target->sy_next != NULL) | |
487 | { | |
fecd2382 | 488 | #ifdef SYMBOLS_NEED_BACKPOINTERS |
6efd877d | 489 | target->sy_next->sy_previous = addme; |
fecd2382 | 490 | #endif /* SYMBOLS_NEED_BACKPOINTERS */ |
6efd877d KR |
491 | } |
492 | else | |
493 | { | |
494 | know (*lastPP == target); | |
495 | *lastPP = addme; | |
496 | } /* if we have a next */ | |
497 | ||
498 | addme->sy_next = target->sy_next; | |
499 | target->sy_next = addme; | |
500 | ||
fecd2382 | 501 | #ifdef SYMBOLS_NEED_BACKPOINTERS |
6efd877d | 502 | addme->sy_previous = target; |
fecd2382 | 503 | #endif /* SYMBOLS_NEED_BACKPOINTERS */ |
2b68b820 | 504 | } |
fecd2382 | 505 | |
dacf29ea KR |
506 | /* Set the chain pointers of SYMBOL to null. */ |
507 | void | |
508 | symbol_clear_list_pointers (symbolP) | |
509 | symbolS *symbolP; | |
510 | { | |
511 | symbolP->sy_next = NULL; | |
512 | #ifdef SYMBOLS_NEED_BACKPOINTERS | |
513 | symbolP->sy_previous = NULL; | |
514 | #endif | |
515 | } | |
516 | ||
fecd2382 RP |
517 | #ifdef SYMBOLS_NEED_BACKPOINTERS |
518 | /* Remove SYMBOLP from the list. */ | |
6efd877d KR |
519 | void |
520 | symbol_remove (symbolP, rootPP, lastPP) | |
521 | symbolS *symbolP; | |
522 | symbolS **rootPP; | |
523 | symbolS **lastPP; | |
fecd2382 | 524 | { |
6efd877d KR |
525 | if (symbolP == *rootPP) |
526 | { | |
527 | *rootPP = symbolP->sy_next; | |
528 | } /* if it was the root */ | |
529 | ||
530 | if (symbolP == *lastPP) | |
531 | { | |
532 | *lastPP = symbolP->sy_previous; | |
533 | } /* if it was the tail */ | |
534 | ||
535 | if (symbolP->sy_next != NULL) | |
536 | { | |
537 | symbolP->sy_next->sy_previous = symbolP->sy_previous; | |
538 | } /* if not last */ | |
539 | ||
540 | if (symbolP->sy_previous != NULL) | |
541 | { | |
542 | symbolP->sy_previous->sy_next = symbolP->sy_next; | |
543 | } /* if not first */ | |
544 | ||
dacf29ea | 545 | debug_verify_symchain (*rootPP, *lastPP); |
2b68b820 | 546 | } |
fecd2382 RP |
547 | |
548 | /* Link symbol ADDME before symbol TARGET in the chain. */ | |
6efd877d KR |
549 | void |
550 | symbol_insert (addme, target, rootPP, lastPP) | |
551 | symbolS *addme; | |
552 | symbolS *target; | |
553 | symbolS **rootPP; | |
554 | symbolS **lastPP; | |
fecd2382 | 555 | { |
6efd877d KR |
556 | if (target->sy_previous != NULL) |
557 | { | |
558 | target->sy_previous->sy_next = addme; | |
559 | } | |
560 | else | |
561 | { | |
562 | know (*rootPP == target); | |
563 | *rootPP = addme; | |
564 | } /* if not first */ | |
565 | ||
566 | addme->sy_previous = target->sy_previous; | |
567 | target->sy_previous = addme; | |
568 | addme->sy_next = target; | |
569 | ||
dacf29ea | 570 | debug_verify_symchain (*rootPP, *lastPP); |
2b68b820 | 571 | } |
6efd877d | 572 | |
fecd2382 RP |
573 | #endif /* SYMBOLS_NEED_BACKPOINTERS */ |
574 | ||
6efd877d KR |
575 | void |
576 | verify_symbol_chain (rootP, lastP) | |
577 | symbolS *rootP; | |
578 | symbolS *lastP; | |
fecd2382 | 579 | { |
6efd877d KR |
580 | symbolS *symbolP = rootP; |
581 | ||
582 | if (symbolP == NULL) | |
2b68b820 | 583 | return; |
6efd877d KR |
584 | |
585 | for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP)) | |
586 | { | |
fecd2382 | 587 | #ifdef SYMBOLS_NEED_BACKPOINTERS |
6efd877d | 588 | know (symbolP->sy_next->sy_previous == symbolP); |
2b68b820 KR |
589 | #else |
590 | /* Walk the list anyways, to make sure pointers are still good. */ | |
604633ae | 591 | ; |
fecd2382 | 592 | #endif /* SYMBOLS_NEED_BACKPOINTERS */ |
2b68b820 | 593 | } |
6efd877d | 594 | |
2b68b820 KR |
595 | assert (lastP == symbolP); |
596 | } | |
6efd877d | 597 | |
2b68b820 KR |
598 | void |
599 | verify_symbol_chain_2 (sym) | |
600 | symbolS *sym; | |
601 | { | |
602 | symbolS *p = sym, *n = sym; | |
603 | #ifdef SYMBOLS_NEED_BACKPOINTERS | |
604 | while (symbol_previous (p)) | |
605 | p = symbol_previous (p); | |
606 | #endif | |
607 | while (symbol_next (n)) | |
608 | n = symbol_next (n); | |
609 | verify_symbol_chain (p, n); | |
610 | } | |
6efd877d | 611 | |
5868b1fe ILT |
612 | /* Resolve the value of a symbol. This is called during the final |
613 | pass over the symbol table to resolve any symbols with complex | |
614 | values. */ | |
615 | ||
616 | void | |
617 | resolve_symbol_value (symp) | |
618 | symbolS *symp; | |
619 | { | |
0cafaab1 ILT |
620 | int resolved; |
621 | ||
5868b1fe ILT |
622 | if (symp->sy_resolved) |
623 | return; | |
624 | ||
0cafaab1 ILT |
625 | resolved = 0; |
626 | ||
5868b1fe ILT |
627 | if (symp->sy_resolving) |
628 | { | |
629 | as_bad ("Symbol definition loop encountered at %s", | |
630 | S_GET_NAME (symp)); | |
631 | S_SET_VALUE (symp, (valueT) 0); | |
0cafaab1 | 632 | resolved = 1; |
5868b1fe ILT |
633 | } |
634 | else | |
635 | { | |
5ac34ac3 ILT |
636 | offsetT left, right, val; |
637 | segT seg_left, seg_right; | |
638 | ||
5868b1fe ILT |
639 | symp->sy_resolving = 1; |
640 | ||
e702f6e6 | 641 | reduce: |
5ac34ac3 | 642 | switch (symp->sy_value.X_op) |
5868b1fe | 643 | { |
5ac34ac3 ILT |
644 | case O_absent: |
645 | S_SET_VALUE (symp, 0); | |
646 | /* Fall through. */ | |
647 | case O_constant: | |
648 | S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address); | |
649 | if (S_GET_SEGMENT (symp) == expr_section) | |
650 | S_SET_SEGMENT (symp, absolute_section); | |
0cafaab1 | 651 | resolved = 1; |
5ac34ac3 ILT |
652 | break; |
653 | ||
654 | case O_symbol: | |
5868b1fe ILT |
655 | resolve_symbol_value (symp->sy_value.X_add_symbol); |
656 | ||
1356d77d ILT |
657 | if (symp->sy_mri_common) |
658 | { | |
659 | /* This is a symbol inside an MRI common section. The | |
660 | relocation routines are going to handle it specially. | |
661 | Don't change the value. */ | |
662 | S_SET_VALUE (symp, symp->sy_value.X_add_number); | |
663 | resolved = symp->sy_value.X_add_symbol->sy_resolved; | |
664 | break; | |
665 | } | |
666 | ||
87e48495 KR |
667 | #if 0 /* I thought this was needed for some of the i386-svr4 PIC |
668 | support, but it appears I was wrong, and it breaks rs6000 | |
669 | support. */ | |
e702f6e6 KR |
670 | if (S_GET_SEGMENT (symp->sy_value.X_add_symbol) != undefined_section |
671 | && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section) | |
87e48495 | 672 | #endif |
e702f6e6 KR |
673 | { |
674 | if (symp->sy_value.X_add_number == 0) | |
675 | copy_symbol_attributes (symp, symp->sy_value.X_add_symbol); | |
676 | ||
677 | S_SET_VALUE (symp, | |
678 | (symp->sy_value.X_add_number | |
679 | + symp->sy_frag->fr_address | |
680 | + S_GET_VALUE (symp->sy_value.X_add_symbol))); | |
681 | if (S_GET_SEGMENT (symp) == expr_section | |
682 | || S_GET_SEGMENT (symp) == undefined_section) | |
683 | S_SET_SEGMENT (symp, | |
684 | S_GET_SEGMENT (symp->sy_value.X_add_symbol)); | |
685 | } | |
0cafaab1 | 686 | resolved = symp->sy_value.X_add_symbol->sy_resolved; |
5ac34ac3 ILT |
687 | break; |
688 | ||
689 | case O_uminus: | |
690 | case O_bit_not: | |
691 | resolve_symbol_value (symp->sy_value.X_add_symbol); | |
692 | if (symp->sy_value.X_op == O_uminus) | |
693 | val = - S_GET_VALUE (symp->sy_value.X_add_symbol); | |
694 | else | |
695 | val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol); | |
696 | S_SET_VALUE (symp, | |
697 | (val | |
698 | + symp->sy_value.X_add_number | |
699 | + symp->sy_frag->fr_address)); | |
700 | if (S_GET_SEGMENT (symp) == expr_section | |
701 | || S_GET_SEGMENT (symp) == undefined_section) | |
702 | S_SET_SEGMENT (symp, absolute_section); | |
0cafaab1 | 703 | resolved = symp->sy_value.X_add_symbol->sy_resolved; |
5ac34ac3 ILT |
704 | break; |
705 | ||
e702f6e6 | 706 | case O_add: |
e702f6e6 KR |
707 | resolve_symbol_value (symp->sy_value.X_add_symbol); |
708 | resolve_symbol_value (symp->sy_value.X_op_symbol); | |
709 | seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol); | |
710 | seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol); | |
711 | /* This case comes up with PIC support. */ | |
712 | { | |
713 | symbolS *s_left = symp->sy_value.X_add_symbol; | |
714 | symbolS *s_right = symp->sy_value.X_op_symbol; | |
715 | ||
716 | if (seg_left == absolute_section) | |
717 | { | |
718 | symbolS *t; | |
719 | segT ts; | |
720 | t = s_left; | |
721 | s_left = s_right; | |
722 | s_right = t; | |
723 | ts = seg_left; | |
724 | seg_left = seg_right; | |
725 | seg_right = ts; | |
726 | } | |
727 | if (seg_right == absolute_section | |
728 | && s_right->sy_resolved) | |
729 | { | |
730 | symp->sy_value.X_add_number += S_GET_VALUE (s_right); | |
731 | symp->sy_value.X_op_symbol = 0; | |
732 | symp->sy_value.X_add_symbol = s_left; | |
733 | symp->sy_value.X_op = O_symbol; | |
734 | goto reduce; | |
735 | } | |
736 | } | |
e702f6e6 KR |
737 | /* fall through */ |
738 | ||
5ac34ac3 ILT |
739 | case O_multiply: |
740 | case O_divide: | |
741 | case O_modulus: | |
742 | case O_left_shift: | |
743 | case O_right_shift: | |
744 | case O_bit_inclusive_or: | |
745 | case O_bit_or_not: | |
746 | case O_bit_exclusive_or: | |
747 | case O_bit_and: | |
5ac34ac3 | 748 | case O_subtract: |
1356d77d ILT |
749 | case O_eq: |
750 | case O_ne: | |
751 | case O_lt: | |
752 | case O_le: | |
753 | case O_ge: | |
754 | case O_gt: | |
c978e704 | 755 | resolve_symbol_value (symp->sy_value.X_add_symbol); |
5ac34ac3 ILT |
756 | resolve_symbol_value (symp->sy_value.X_op_symbol); |
757 | seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol); | |
758 | seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol); | |
759 | if (seg_left != seg_right | |
760 | && seg_left != undefined_section | |
761 | && seg_right != undefined_section) | |
762 | as_bad ("%s is operation on symbols in different sections", | |
c978e704 | 763 | S_GET_NAME (symp)); |
5ac34ac3 ILT |
764 | if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol) |
765 | != absolute_section) | |
766 | && symp->sy_value.X_op != O_subtract) | |
767 | as_bad ("%s is illegal operation on non-absolute symbols", | |
768 | S_GET_NAME (symp)); | |
769 | left = S_GET_VALUE (symp->sy_value.X_add_symbol); | |
770 | right = S_GET_VALUE (symp->sy_value.X_op_symbol); | |
771 | switch (symp->sy_value.X_op) | |
772 | { | |
773 | case O_multiply: val = left * right; break; | |
774 | case O_divide: val = left / right; break; | |
775 | case O_modulus: val = left % right; break; | |
776 | case O_left_shift: val = left << right; break; | |
777 | case O_right_shift: val = left >> right; break; | |
778 | case O_bit_inclusive_or: val = left | right; break; | |
779 | case O_bit_or_not: val = left |~ right; break; | |
780 | case O_bit_exclusive_or: val = left ^ right; break; | |
781 | case O_bit_and: val = left & right; break; | |
782 | case O_add: val = left + right; break; | |
783 | case O_subtract: val = left - right; break; | |
1356d77d ILT |
784 | case O_eq: val = left == right ? ~ (offsetT) 0 : 0; |
785 | case O_ne: val = left != right ? ~ (offsetT) 0 : 0; | |
786 | case O_lt: val = left < right ? ~ (offsetT) 0 : 0; | |
787 | case O_le: val = left <= right ? ~ (offsetT) 0 : 0; | |
788 | case O_ge: val = left >= right ? ~ (offsetT) 0 : 0; | |
789 | case O_gt: val = left > right ? ~ (offsetT) 0 : 0; | |
5ac34ac3 ILT |
790 | default: abort (); |
791 | } | |
c978e704 ILT |
792 | S_SET_VALUE (symp, |
793 | (symp->sy_value.X_add_number | |
794 | + symp->sy_frag->fr_address | |
5ac34ac3 ILT |
795 | + val)); |
796 | if (S_GET_SEGMENT (symp) == expr_section | |
797 | || S_GET_SEGMENT (symp) == undefined_section) | |
798 | S_SET_SEGMENT (symp, absolute_section); | |
0cafaab1 ILT |
799 | resolved = (symp->sy_value.X_add_symbol->sy_resolved |
800 | && symp->sy_value.X_op_symbol->sy_resolved); | |
5ac34ac3 ILT |
801 | break; |
802 | ||
803 | case O_register: | |
804 | case O_big: | |
805 | case O_illegal: | |
0cafaab1 ILT |
806 | /* Give an error (below) if not in expr_section. We don't |
807 | want to worry about expr_section symbols, because they | |
808 | are fictional (they are created as part of expression | |
809 | resolution), and any problems may not actually mean | |
810 | anything. */ | |
5ac34ac3 | 811 | break; |
5868b1fe ILT |
812 | } |
813 | } | |
814 | ||
0cafaab1 ILT |
815 | /* Don't worry if we can't resolve an expr_section symbol. */ |
816 | if (resolved) | |
817 | symp->sy_resolved = 1; | |
818 | else if (S_GET_SEGMENT (symp) != expr_section) | |
819 | { | |
820 | as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp)); | |
821 | symp->sy_resolved = 1; | |
822 | } | |
5868b1fe ILT |
823 | } |
824 | ||
6efd877d KR |
825 | /* Dollar labels look like a number followed by a dollar sign. Eg, "42$". |
826 | They are *really* local. That is, they go out of scope whenever we see a | |
827 | label that isn't local. Also, like fb labels, there can be multiple | |
828 | instances of a dollar label. Therefor, we name encode each instance with | |
829 | the instance number, keep a list of defined symbols separate from the real | |
830 | symbol table, and we treat these buggers as a sparse array. */ | |
831 | ||
2b68b820 KR |
832 | static long *dollar_labels; |
833 | static long *dollar_label_instances; | |
834 | static char *dollar_label_defines; | |
835 | static long dollar_label_count; | |
604633ae | 836 | static unsigned long dollar_label_max; |
6efd877d KR |
837 | |
838 | int | |
839 | dollar_label_defined (label) | |
840 | long label; | |
841 | { | |
842 | long *i; | |
843 | ||
844 | know ((dollar_labels != NULL) || (dollar_label_count == 0)); | |
845 | ||
846 | for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i) | |
2b68b820 KR |
847 | if (*i == label) |
848 | return dollar_label_defines[i - dollar_labels]; | |
6efd877d KR |
849 | |
850 | /* if we get here, label isn't defined */ | |
2b68b820 | 851 | return 0; |
6efd877d KR |
852 | } /* dollar_label_defined() */ |
853 | ||
854 | static int | |
855 | dollar_label_instance (label) | |
856 | long label; | |
857 | { | |
858 | long *i; | |
859 | ||
860 | know ((dollar_labels != NULL) || (dollar_label_count == 0)); | |
861 | ||
862 | for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i) | |
2b68b820 KR |
863 | if (*i == label) |
864 | return (dollar_label_instances[i - dollar_labels]); | |
6efd877d | 865 | |
2b68b820 KR |
866 | /* If we get here, we haven't seen the label before, therefore its instance |
867 | count is zero. */ | |
868 | return 0; | |
869 | } | |
6efd877d KR |
870 | |
871 | void | |
872 | dollar_label_clear () | |
873 | { | |
604633ae | 874 | memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count); |
2b68b820 | 875 | } |
6efd877d KR |
876 | |
877 | #define DOLLAR_LABEL_BUMP_BY 10 | |
878 | ||
879 | void | |
880 | define_dollar_label (label) | |
881 | long label; | |
882 | { | |
883 | long *i; | |
884 | ||
885 | for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i) | |
2b68b820 KR |
886 | if (*i == label) |
887 | { | |
888 | ++dollar_label_instances[i - dollar_labels]; | |
889 | dollar_label_defines[i - dollar_labels] = 1; | |
890 | return; | |
891 | } | |
6efd877d KR |
892 | |
893 | /* if we get to here, we don't have label listed yet. */ | |
894 | ||
895 | if (dollar_labels == NULL) | |
896 | { | |
897 | dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long)); | |
898 | dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long)); | |
899 | dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY); | |
900 | dollar_label_max = DOLLAR_LABEL_BUMP_BY; | |
901 | dollar_label_count = 0; | |
6efd877d KR |
902 | } |
903 | else if (dollar_label_count == dollar_label_max) | |
904 | { | |
905 | dollar_label_max += DOLLAR_LABEL_BUMP_BY; | |
906 | dollar_labels = (long *) xrealloc ((char *) dollar_labels, | |
907 | dollar_label_max * sizeof (long)); | |
908 | dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances, | |
909 | dollar_label_max * sizeof (long)); | |
910 | dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max); | |
911 | } /* if we needed to grow */ | |
912 | ||
913 | dollar_labels[dollar_label_count] = label; | |
914 | dollar_label_instances[dollar_label_count] = 1; | |
915 | dollar_label_defines[dollar_label_count] = 1; | |
916 | ++dollar_label_count; | |
2b68b820 | 917 | } |
6efd877d KR |
918 | |
919 | /* | |
920 | * dollar_label_name() | |
921 | * | |
922 | * Caller must copy returned name: we re-use the area for the next name. | |
923 | * | |
2b68b820 KR |
924 | * The mth occurence of label n: is turned into the symbol "Ln^Am" |
925 | * where n is the label number and m is the instance number. "L" makes | |
926 | * it a label discarded unless debugging and "^A"('\1') ensures no | |
927 | * ordinary symbol SHOULD get the same name as a local label | |
928 | * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1. | |
6efd877d KR |
929 | * |
930 | * fb labels get the same treatment, except that ^B is used in place of ^A. | |
931 | */ | |
932 | ||
933 | char * /* Return local label name. */ | |
934 | dollar_label_name (n, augend) | |
935 | register long n; /* we just saw "n$:" : n a number */ | |
936 | register int augend; /* 0 for current instance, 1 for new instance */ | |
937 | { | |
938 | long i; | |
939 | /* Returned to caller, then copied. used for created names ("4f") */ | |
940 | static char symbol_name_build[24]; | |
941 | register char *p; | |
942 | register char *q; | |
943 | char symbol_name_temporary[20]; /* build up a number, BACKWARDS */ | |
944 | ||
945 | know (n >= 0); | |
946 | know (augend == 0 || augend == 1); | |
947 | p = symbol_name_build; | |
948 | *p++ = 'L'; | |
949 | ||
950 | /* Next code just does sprintf( {}, "%d", n); */ | |
951 | /* label number */ | |
952 | q = symbol_name_temporary; | |
953 | for (*q++ = 0, i = n; i; ++q) | |
954 | { | |
955 | *q = i % 10 + '0'; | |
956 | i /= 10; | |
957 | } | |
958 | while ((*p = *--q) != '\0') | |
959 | ++p; | |
960 | ||
961 | *p++ = 1; /* ^A */ | |
962 | ||
963 | /* instance number */ | |
964 | q = symbol_name_temporary; | |
965 | for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q) | |
966 | { | |
967 | *q = i % 10 + '0'; | |
968 | i /= 10; | |
969 | } | |
970 | while ((*p++ = *--q) != '\0');; | |
971 | ||
972 | /* The label, as a '\0' ended string, starts at symbol_name_build. */ | |
2b68b820 KR |
973 | return symbol_name_build; |
974 | } | |
6efd877d | 975 | |
6efd877d KR |
976 | /* |
977 | * Sombody else's idea of local labels. They are made by "n:" where n | |
978 | * is any decimal digit. Refer to them with | |
979 | * "nb" for previous (backward) n: | |
980 | * or "nf" for next (forward) n:. | |
981 | * | |
982 | * We do a little better and let n be any number, not just a single digit, but | |
983 | * since the other guy's assembler only does ten, we treat the first ten | |
984 | * specially. | |
985 | * | |
986 | * Like someone else's assembler, we have one set of local label counters for | |
987 | * entire assembly, not one set per (sub)segment like in most assemblers. This | |
988 | * implies that one can refer to a label in another segment, and indeed some | |
989 | * crufty compilers have done just that. | |
990 | * | |
991 | * Since there could be a LOT of these things, treat them as a sparse array. | |
992 | */ | |
993 | ||
994 | #define FB_LABEL_SPECIAL (10) | |
995 | ||
996 | static long fb_low_counter[FB_LABEL_SPECIAL]; | |
997 | static long *fb_labels; | |
998 | static long *fb_label_instances; | |
eec0de3f KR |
999 | static long fb_label_count; |
1000 | static long fb_label_max; | |
6efd877d KR |
1001 | |
1002 | /* this must be more than FB_LABEL_SPECIAL */ | |
1003 | #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6) | |
1004 | ||
1005 | static void | |
1006 | fb_label_init () | |
1007 | { | |
1008 | memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter)); | |
6efd877d KR |
1009 | } /* fb_label_init() */ |
1010 | ||
1011 | /* add one to the instance number of this fb label */ | |
1012 | void | |
1013 | fb_label_instance_inc (label) | |
1014 | long label; | |
1015 | { | |
1016 | long *i; | |
1017 | ||
1018 | if (label < FB_LABEL_SPECIAL) | |
1019 | { | |
1020 | ++fb_low_counter[label]; | |
1021 | return; | |
1022 | } | |
1023 | ||
e154ecf4 | 1024 | if (fb_labels != NULL) |
6efd877d | 1025 | { |
e154ecf4 ILT |
1026 | for (i = fb_labels + FB_LABEL_SPECIAL; |
1027 | i < fb_labels + fb_label_count; ++i) | |
6efd877d | 1028 | { |
e154ecf4 ILT |
1029 | if (*i == label) |
1030 | { | |
1031 | ++fb_label_instances[i - fb_labels]; | |
1032 | return; | |
1033 | } /* if we find it */ | |
1034 | } /* for each existing label */ | |
1035 | } | |
6efd877d KR |
1036 | |
1037 | /* if we get to here, we don't have label listed yet. */ | |
1038 | ||
1039 | if (fb_labels == NULL) | |
1040 | { | |
1041 | fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long)); | |
1042 | fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long)); | |
1043 | fb_label_max = FB_LABEL_BUMP_BY; | |
1044 | fb_label_count = FB_LABEL_SPECIAL; | |
1045 | ||
1046 | } | |
1047 | else if (fb_label_count == fb_label_max) | |
1048 | { | |
1049 | fb_label_max += FB_LABEL_BUMP_BY; | |
1050 | fb_labels = (long *) xrealloc ((char *) fb_labels, | |
1051 | fb_label_max * sizeof (long)); | |
1052 | fb_label_instances = (long *) xrealloc ((char *) fb_label_instances, | |
1053 | fb_label_max * sizeof (long)); | |
1054 | } /* if we needed to grow */ | |
1055 | ||
1056 | fb_labels[fb_label_count] = label; | |
1057 | fb_label_instances[fb_label_count] = 1; | |
1058 | ++fb_label_count; | |
1ecd6c4a | 1059 | } |
6efd877d KR |
1060 | |
1061 | static long | |
1062 | fb_label_instance (label) | |
1063 | long label; | |
1064 | { | |
1065 | long *i; | |
1066 | ||
1067 | if (label < FB_LABEL_SPECIAL) | |
1068 | { | |
1069 | return (fb_low_counter[label]); | |
1070 | } | |
1071 | ||
e154ecf4 | 1072 | if (fb_labels != NULL) |
6efd877d | 1073 | { |
e154ecf4 ILT |
1074 | for (i = fb_labels + FB_LABEL_SPECIAL; |
1075 | i < fb_labels + fb_label_count; ++i) | |
6efd877d | 1076 | { |
e154ecf4 ILT |
1077 | if (*i == label) |
1078 | { | |
1079 | return (fb_label_instances[i - fb_labels]); | |
1080 | } /* if we find it */ | |
1081 | } /* for each existing label */ | |
1082 | } | |
6efd877d | 1083 | |
e154ecf4 ILT |
1084 | /* We didn't find the label, so this must be a reference to the |
1085 | first instance. */ | |
1086 | return 0; | |
2b68b820 | 1087 | } |
6efd877d KR |
1088 | |
1089 | /* | |
1090 | * fb_label_name() | |
1091 | * | |
1092 | * Caller must copy returned name: we re-use the area for the next name. | |
1093 | * | |
2b68b820 KR |
1094 | * The mth occurence of label n: is turned into the symbol "Ln^Bm" |
1095 | * where n is the label number and m is the instance number. "L" makes | |
1096 | * it a label discarded unless debugging and "^B"('\2') ensures no | |
1097 | * ordinary symbol SHOULD get the same name as a local label | |
1098 | * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1. | |
6efd877d | 1099 | * |
2b68b820 | 1100 | * dollar labels get the same treatment, except that ^A is used in place of ^B. */ |
6efd877d KR |
1101 | |
1102 | char * /* Return local label name. */ | |
1103 | fb_label_name (n, augend) | |
1104 | long n; /* we just saw "n:", "nf" or "nb" : n a number */ | |
1105 | long augend; /* 0 for nb, 1 for n:, nf */ | |
1106 | { | |
1107 | long i; | |
1108 | /* Returned to caller, then copied. used for created names ("4f") */ | |
1109 | static char symbol_name_build[24]; | |
1110 | register char *p; | |
1111 | register char *q; | |
1112 | char symbol_name_temporary[20]; /* build up a number, BACKWARDS */ | |
1113 | ||
1114 | know (n >= 0); | |
1115 | know (augend == 0 || augend == 1); | |
1116 | p = symbol_name_build; | |
1117 | *p++ = 'L'; | |
1118 | ||
1119 | /* Next code just does sprintf( {}, "%d", n); */ | |
1120 | /* label number */ | |
1121 | q = symbol_name_temporary; | |
1122 | for (*q++ = 0, i = n; i; ++q) | |
1123 | { | |
1124 | *q = i % 10 + '0'; | |
1125 | i /= 10; | |
1126 | } | |
1127 | while ((*p = *--q) != '\0') | |
1128 | ++p; | |
1129 | ||
1130 | *p++ = 2; /* ^B */ | |
1131 | ||
1132 | /* instance number */ | |
1133 | q = symbol_name_temporary; | |
1134 | for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q) | |
1135 | { | |
1136 | *q = i % 10 + '0'; | |
1137 | i /= 10; | |
1138 | } | |
1139 | while ((*p++ = *--q) != '\0');; | |
1140 | ||
1141 | /* The label, as a '\0' ended string, starts at symbol_name_build. */ | |
1142 | return (symbol_name_build); | |
1143 | } /* fb_label_name() */ | |
1144 | ||
a6c6eaf8 | 1145 | /* |
6efd877d KR |
1146 | * decode name that may have been generated by foo_label_name() above. If |
1147 | * the name wasn't generated by foo_label_name(), then return it unaltered. | |
a6c6eaf8 RP |
1148 | * This is used for error messages. |
1149 | */ | |
a39116f1 | 1150 | |
6efd877d KR |
1151 | char * |
1152 | decode_local_label_name (s) | |
1153 | char *s; | |
a6c6eaf8 | 1154 | { |
6efd877d KR |
1155 | char *p; |
1156 | char *symbol_decode; | |
1157 | int label_number; | |
1158 | int instance_number; | |
1159 | char *type; | |
1160 | const char *message_format = "\"%d\" (instance number %d of a %s label)"; | |
1161 | ||
1162 | if (s[0] != 'L') | |
56dc989a | 1163 | return s; |
6efd877d KR |
1164 | |
1165 | for (label_number = 0, p = s + 1; isdigit (*p); ++p) | |
56dc989a | 1166 | label_number = (10 * label_number) + *p - '0'; |
6efd877d KR |
1167 | |
1168 | if (*p == 1) | |
56dc989a | 1169 | type = "dollar"; |
6efd877d | 1170 | else if (*p == 2) |
56dc989a | 1171 | type = "fb"; |
6efd877d | 1172 | else |
56dc989a | 1173 | return s; |
6efd877d | 1174 | |
56dc989a ILT |
1175 | for (instance_number = 0, p++; isdigit (*p); ++p) |
1176 | instance_number = (10 * instance_number) + *p - '0'; | |
6efd877d KR |
1177 | |
1178 | symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30); | |
56dc989a | 1179 | sprintf (symbol_decode, message_format, label_number, instance_number, type); |
6efd877d | 1180 | |
56dc989a ILT |
1181 | return symbol_decode; |
1182 | } | |
a6c6eaf8 | 1183 | |
85051959 ILT |
1184 | /* Get the value of a symbol. */ |
1185 | ||
1186 | valueT | |
1187 | S_GET_VALUE (s) | |
1188 | symbolS *s; | |
1189 | { | |
e702f6e6 KR |
1190 | if (!s->sy_resolved && !s->sy_resolving && s->sy_value.X_op != O_constant) |
1191 | resolve_symbol_value (s); | |
5ac34ac3 | 1192 | if (s->sy_value.X_op != O_constant) |
5868b1fe | 1193 | as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s)); |
85051959 ILT |
1194 | return (valueT) s->sy_value.X_add_number; |
1195 | } | |
1196 | ||
1197 | /* Set the value of a symbol. */ | |
1198 | ||
1199 | void | |
1200 | S_SET_VALUE (s, val) | |
1201 | symbolS *s; | |
1202 | valueT val; | |
1203 | { | |
5ac34ac3 | 1204 | s->sy_value.X_op = O_constant; |
85051959 | 1205 | s->sy_value.X_add_number = (offsetT) val; |
0cafaab1 | 1206 | s->sy_value.X_unsigned = 0; |
85051959 ILT |
1207 | } |
1208 | ||
e702f6e6 KR |
1209 | void |
1210 | copy_symbol_attributes (dest, src) | |
1211 | symbolS *dest, *src; | |
1212 | { | |
1213 | #ifdef BFD_ASSEMBLER | |
1214 | /* In an expression, transfer the settings of these flags. | |
1215 | The user can override later, of course. */ | |
1216 | #define COPIED_SYMFLAGS (BSF_FUNCTION) | |
1217 | dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS; | |
1218 | #endif | |
1219 | ||
1220 | #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES | |
1221 | OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src); | |
1222 | #endif | |
1223 | } | |
1224 | ||
2b68b820 KR |
1225 | #ifdef BFD_ASSEMBLER |
1226 | ||
1227 | int | |
1228 | S_IS_EXTERNAL (s) | |
1229 | symbolS *s; | |
1230 | { | |
1231 | flagword flags = s->bsym->flags; | |
1232 | ||
1233 | /* sanity check */ | |
0cafaab1 | 1234 | if (flags & BSF_LOCAL && flags & BSF_GLOBAL) |
2b68b820 KR |
1235 | abort (); |
1236 | ||
0cafaab1 | 1237 | return (flags & BSF_GLOBAL) != 0; |
2b68b820 KR |
1238 | } |
1239 | ||
1240 | int | |
1241 | S_IS_COMMON (s) | |
1242 | symbolS *s; | |
1243 | { | |
0cafaab1 | 1244 | return bfd_is_com_section (s->bsym->section); |
2b68b820 KR |
1245 | } |
1246 | ||
1247 | int | |
1248 | S_IS_DEFINED (s) | |
1249 | symbolS *s; | |
1250 | { | |
1251 | return s->bsym->section != undefined_section; | |
1252 | } | |
1253 | ||
1254 | int | |
1255 | S_IS_DEBUG (s) | |
1256 | symbolS *s; | |
1257 | { | |
1258 | if (s->bsym->flags & BSF_DEBUGGING) | |
1259 | return 1; | |
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | int | |
1264 | S_IS_LOCAL (s) | |
1265 | symbolS *s; | |
1266 | { | |
1267 | flagword flags = s->bsym->flags; | |
1356d77d | 1268 | const char *name; |
2b68b820 KR |
1269 | |
1270 | /* sanity check */ | |
0cafaab1 | 1271 | if (flags & BSF_LOCAL && flags & BSF_GLOBAL) |
2b68b820 KR |
1272 | abort (); |
1273 | ||
2a0f64a5 ILT |
1274 | if (bfd_get_section (s->bsym) == reg_section) |
1275 | return 1; | |
1276 | ||
1356d77d ILT |
1277 | name = S_GET_NAME (s); |
1278 | return (name != NULL | |
2b68b820 | 1279 | && ! S_IS_DEBUG (s) |
1356d77d ILT |
1280 | && (strchr (name, '\001') |
1281 | || strchr (name, '\002') | |
1282 | || (! flag_keep_locals | |
1283 | && (LOCAL_LABEL (name) | |
1284 | || (flag_mri | |
1285 | && name[0] == '?' | |
1286 | && name[1] == '?'))))); | |
2b68b820 KR |
1287 | } |
1288 | ||
1289 | int | |
1290 | S_IS_EXTERN (s) | |
1291 | symbolS *s; | |
1292 | { | |
1293 | return S_IS_EXTERNAL (s); | |
1294 | } | |
1295 | ||
1296 | int | |
1297 | S_IS_STABD (s) | |
1298 | symbolS *s; | |
1299 | { | |
1300 | return S_GET_NAME (s) == 0; | |
1301 | } | |
1302 | ||
2b68b820 KR |
1303 | CONST char * |
1304 | S_GET_NAME (s) | |
1305 | symbolS *s; | |
1306 | { | |
1307 | return s->bsym->name; | |
1308 | } | |
1309 | ||
1310 | segT | |
1311 | S_GET_SEGMENT (s) | |
1312 | symbolS *s; | |
1313 | { | |
1314 | return s->bsym->section; | |
1315 | } | |
1316 | ||
2b68b820 KR |
1317 | void |
1318 | S_SET_SEGMENT (s, seg) | |
1319 | symbolS *s; | |
1320 | segT seg; | |
1321 | { | |
1322 | s->bsym->section = seg; | |
1323 | } | |
1324 | ||
1325 | void | |
1326 | S_SET_EXTERNAL (s) | |
1327 | symbolS *s; | |
1328 | { | |
56dc989a ILT |
1329 | if ((s->bsym->flags & BSF_WEAK) != 0) |
1330 | as_warn ("%s already declared as weak", S_GET_NAME (s)); | |
1ecd6c4a KR |
1331 | s->bsym->flags |= BSF_GLOBAL; |
1332 | s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK); | |
2b68b820 KR |
1333 | } |
1334 | ||
1335 | void | |
1336 | S_CLEAR_EXTERNAL (s) | |
1337 | symbolS *s; | |
1338 | { | |
56dc989a ILT |
1339 | if ((s->bsym->flags & BSF_WEAK) != 0) |
1340 | as_warn ("%s already declared as weak", S_GET_NAME (s)); | |
2b68b820 | 1341 | s->bsym->flags |= BSF_LOCAL; |
1ecd6c4a KR |
1342 | s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK); |
1343 | } | |
1344 | ||
1345 | void | |
1346 | S_SET_WEAK (s) | |
1347 | symbolS *s; | |
1348 | { | |
56dc989a ILT |
1349 | if ((s->bsym->flags & BSF_GLOBAL) != 0) |
1350 | as_warn ("%s already declared as global", S_GET_NAME (s)); | |
1ecd6c4a KR |
1351 | s->bsym->flags |= BSF_WEAK; |
1352 | s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL); | |
2b68b820 KR |
1353 | } |
1354 | ||
1355 | void | |
1356 | S_SET_NAME (s, name) | |
1357 | symbolS *s; | |
1358 | char *name; | |
1359 | { | |
1360 | s->bsym->name = name; | |
1361 | } | |
1362 | #endif /* BFD_ASSEMBLER */ | |
1363 | ||
eec0de3f KR |
1364 | void |
1365 | symbol_begin () | |
1366 | { | |
1367 | symbol_lastP = NULL; | |
1368 | symbol_rootP = NULL; /* In case we have 0 symbols (!!) */ | |
1369 | sy_hash = hash_new (); | |
dacf29ea | 1370 | |
eec0de3f KR |
1371 | memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol)); |
1372 | #ifdef BFD_ASSEMBLER | |
dacf29ea | 1373 | #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL) |
eec0de3f | 1374 | abs_symbol.bsym = bfd_abs_section.symbol; |
dacf29ea | 1375 | #endif |
eec0de3f KR |
1376 | #else |
1377 | /* Can't initialise a union. Sigh. */ | |
1378 | S_SET_SEGMENT (&abs_symbol, absolute_section); | |
1379 | #endif | |
dacf29ea | 1380 | abs_symbol.sy_value.X_op = O_constant; |
db317fe7 | 1381 | abs_symbol.sy_frag = &zero_address_frag; |
dacf29ea | 1382 | |
56dc989a ILT |
1383 | if (LOCAL_LABELS_FB) |
1384 | fb_label_init (); | |
eec0de3f KR |
1385 | } |
1386 | ||
e702f6e6 KR |
1387 | \f |
1388 | int indent_level; | |
dacf29ea | 1389 | |
56dc989a ILT |
1390 | #if 0 |
1391 | ||
dacf29ea KR |
1392 | static void |
1393 | indent () | |
1394 | { | |
1395 | printf ("%*s", indent_level * 4, ""); | |
1396 | } | |
1397 | ||
56dc989a ILT |
1398 | #endif |
1399 | ||
dacf29ea KR |
1400 | void print_expr_1 PARAMS ((FILE *, expressionS *)); |
1401 | void print_symbol_value_1 PARAMS ((FILE *, symbolS *)); | |
1402 | ||
1403 | void | |
1404 | print_symbol_value_1 (file, sym) | |
1405 | FILE *file; | |
1406 | symbolS *sym; | |
1407 | { | |
1408 | const char *name = S_GET_NAME (sym); | |
1409 | if (!name || !name[0]) | |
1410 | name = "(unnamed)"; | |
56dc989a | 1411 | fprintf (file, "sym %lx %s", (unsigned long) sym, name); |
e702f6e6 KR |
1412 | if (sym->sy_frag != &zero_address_frag) |
1413 | fprintf (file, " frag %lx", (long) sym->sy_frag); | |
dacf29ea KR |
1414 | if (sym->written) |
1415 | fprintf (file, " written"); | |
1416 | if (sym->sy_resolved) | |
1417 | fprintf (file, " resolved"); | |
e702f6e6 | 1418 | else if (sym->sy_resolving) |
dacf29ea KR |
1419 | fprintf (file, " resolving"); |
1420 | if (sym->sy_used_in_reloc) | |
1421 | fprintf (file, " used-in-reloc"); | |
1422 | if (sym->sy_used) | |
1423 | fprintf (file, " used"); | |
87e48495 KR |
1424 | if (S_IS_LOCAL (sym)) |
1425 | fprintf (file, " local"); | |
1426 | if (S_IS_EXTERN (sym)) | |
1427 | fprintf (file, " extern"); | |
1428 | if (S_IS_DEBUG (sym)) | |
1429 | fprintf (file, " debug"); | |
1430 | if (S_IS_DEFINED (sym)) | |
1431 | fprintf (file, " defined"); | |
e702f6e6 | 1432 | fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym))); |
dacf29ea KR |
1433 | if (sym->sy_resolved) |
1434 | { | |
e702f6e6 KR |
1435 | segT s = S_GET_SEGMENT (sym); |
1436 | ||
1437 | if (s != undefined_section | |
1438 | && s != expr_section) | |
1439 | fprintf (file, " %lx", (long) S_GET_VALUE (sym)); | |
dacf29ea | 1440 | } |
e702f6e6 | 1441 | else if (indent_level < 8 && S_GET_SEGMENT (sym) != undefined_section) |
dacf29ea KR |
1442 | { |
1443 | indent_level++; | |
1444 | fprintf (file, "\n%*s<", indent_level * 4, ""); | |
1445 | print_expr_1 (file, &sym->sy_value); | |
1446 | fprintf (file, ">"); | |
1447 | indent_level--; | |
1448 | } | |
1449 | fflush (file); | |
1450 | } | |
1451 | ||
1452 | void | |
1453 | print_symbol_value (sym) | |
1454 | symbolS *sym; | |
1455 | { | |
1456 | indent_level = 0; | |
1457 | print_symbol_value_1 (stderr, sym); | |
1458 | fprintf (stderr, "\n"); | |
1459 | } | |
1460 | ||
1461 | void | |
1462 | print_expr_1 (file, exp) | |
1463 | FILE *file; | |
1464 | expressionS *exp; | |
1465 | { | |
1466 | fprintf (file, "expr %lx ", (long) exp); | |
1467 | switch (exp->X_op) | |
1468 | { | |
1469 | case O_illegal: | |
1470 | fprintf (file, "illegal"); | |
1471 | break; | |
1472 | case O_absent: | |
1473 | fprintf (file, "absent"); | |
1474 | break; | |
1475 | case O_constant: | |
1476 | fprintf (file, "constant %lx", (long) exp->X_add_number); | |
1477 | break; | |
1478 | case O_symbol: | |
1479 | indent_level++; | |
1480 | fprintf (file, "symbol\n%*s<", indent_level * 4, ""); | |
1481 | print_symbol_value_1 (file, exp->X_add_symbol); | |
1482 | fprintf (file, ">"); | |
1483 | maybe_print_addnum: | |
dacf29ea | 1484 | if (exp->X_add_number) |
e702f6e6 | 1485 | fprintf (file, "\n%*s%lx", indent_level * 4, "", |
dacf29ea | 1486 | (long) exp->X_add_number); |
e702f6e6 | 1487 | indent_level--; |
dacf29ea KR |
1488 | break; |
1489 | case O_register: | |
1490 | fprintf (file, "register #%d", (int) exp->X_add_number); | |
1491 | break; | |
1492 | case O_big: | |
1493 | fprintf (file, "big"); | |
1494 | break; | |
1495 | case O_uminus: | |
1496 | fprintf (file, "uminus -<"); | |
1497 | indent_level++; | |
1498 | print_symbol_value_1 (file, exp->X_add_symbol); | |
1499 | fprintf (file, ">"); | |
1500 | goto maybe_print_addnum; | |
1501 | case O_bit_not: | |
1502 | fprintf (file, "bit_not"); | |
1503 | break; | |
1504 | case O_multiply: | |
1505 | fprintf (file, "multiply"); | |
1506 | break; | |
1507 | case O_divide: | |
1508 | fprintf (file, "divide"); | |
1509 | break; | |
1510 | case O_modulus: | |
1511 | fprintf (file, "modulus"); | |
1512 | break; | |
1513 | case O_left_shift: | |
1514 | fprintf (file, "lshift"); | |
1515 | break; | |
1516 | case O_right_shift: | |
1517 | fprintf (file, "rshift"); | |
1518 | break; | |
1519 | case O_bit_inclusive_or: | |
1520 | fprintf (file, "bit_ior"); | |
1521 | break; | |
1522 | case O_bit_exclusive_or: | |
1523 | fprintf (file, "bit_xor"); | |
1524 | break; | |
1525 | case O_bit_and: | |
1526 | fprintf (file, "bit_and"); | |
1527 | break; | |
1356d77d ILT |
1528 | case O_eq: |
1529 | fprintf (file, "eq"); | |
1530 | break; | |
1531 | case O_ne: | |
1532 | fprintf (file, "ne"); | |
1533 | break; | |
1534 | case O_lt: | |
1535 | fprintf (file, "lt"); | |
1536 | break; | |
1537 | case O_le: | |
1538 | fprintf (file, "le"); | |
1539 | break; | |
1540 | case O_ge: | |
1541 | fprintf (file, "ge"); | |
1542 | break; | |
1543 | case O_gt: | |
1544 | fprintf (file, "gt"); | |
1545 | break; | |
dacf29ea KR |
1546 | case O_add: |
1547 | indent_level++; | |
1548 | fprintf (file, "add\n%*s<", indent_level * 4, ""); | |
1549 | print_symbol_value_1 (file, exp->X_add_symbol); | |
1550 | fprintf (file, ">\n%*s<", indent_level * 4, ""); | |
1551 | print_symbol_value_1 (file, exp->X_op_symbol); | |
1552 | fprintf (file, ">"); | |
1553 | goto maybe_print_addnum; | |
1554 | case O_subtract: | |
1555 | indent_level++; | |
1556 | fprintf (file, "subtract\n%*s<", indent_level * 4, ""); | |
1557 | print_symbol_value_1 (file, exp->X_add_symbol); | |
1558 | fprintf (file, ">\n%*s<", indent_level * 4, ""); | |
1559 | print_symbol_value_1 (file, exp->X_op_symbol); | |
1560 | fprintf (file, ">"); | |
1561 | goto maybe_print_addnum; | |
1562 | default: | |
1563 | fprintf (file, "{unknown opcode %d}", (int) exp->X_op); | |
1564 | break; | |
1565 | } | |
1566 | fflush (stdout); | |
1567 | } | |
1568 | ||
1569 | void | |
1570 | print_expr (exp) | |
1571 | expressionS *exp; | |
1572 | { | |
1573 | print_expr_1 (stderr, exp); | |
1574 | fprintf (stderr, "\n"); | |
1575 | } | |
1576 | ||
8b228fe9 | 1577 | /* end of symbols.c */ |