1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25: #include "as.h"
26:
27: #include "safe-ctype.h"
28: #include "obstack.h"
29: #include "subsegs.h"
30:
31: #include "struc-symbol.h"
32:
33:
34:
35: int symbols_case_sensitive = 1;
36:
37: #ifndef WORKING_DOT_WORD
38: extern int new_broken_words;
39: #endif
40:
41:
42: static struct hash_control *sy_hash;
43:
44:
45: static struct hash_control *local_hash;
46:
47:
48: symbolS *symbol_rootP;
49: symbolS *symbol_lastP;
50: symbolS abs_symbol;
51:
52: #ifdef DEBUG_SYMS
53: #define debug_verify_symchain verify_symbol_chain
54: #else
55: #define debug_verify_symchain(root, last) ((void) 0)
56: #endif
57:
58: #define DOLLAR_LABEL_CHAR '\001'
59: #define LOCAL_LABEL_CHAR '\002'
60:
61: struct obstack notes;
62: #ifdef USE_UNIQUE
63:
64:
65: const char * an_external_name;
66: #endif
67:
68: static char *save_symbol_name (const char *);
69: static void fb_label_init (void);
70: static long dollar_label_instance (long);
71: static long fb_label_instance (long);
72:
73: static void print_binary (FILE *, const char *, expressionS *);
74: static void report_op_error (symbolS *, symbolS *, symbolS *);
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85: symbolS *
86: symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
87: {
88: symbolS *symbolP = symbol_create (name, segment, valu, frag);
89:
90:
91: {
92: extern int symbol_table_frozen;
93: if (symbol_table_frozen)
94: abort ();
95: }
96: symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
97:
98: return symbolP;
99: }
100:
101:
102:
103:
104: static char *
105: save_symbol_name (const char *name)
106: {
107: unsigned int name_length;
108: char *ret;
109:
110: name_length = strlen (name) + 1;
111: obstack_grow (¬es, name, name_length);
112: ret = obstack_finish (¬es);
113:
114: #ifdef tc_canonicalize_symbol_name
115: ret = tc_canonicalize_symbol_name (ret);
116: #endif
117:
118: if (! symbols_case_sensitive)
119: {
120: char *s;
121:
122: for (s = ret; *s != '\0'; s++)
123: *s = TOUPPER (*s);
124: }
125:
126: return ret;
127: }
128:
129: symbolS *
130: symbol_create (const char *name,
131: segT segment,
132: valueT valu,
133: fragS *frag )
134: {
135: char *preserved_copy_of_name;
136: symbolS *symbolP;
137:
138: preserved_copy_of_name = save_symbol_name (name);
139:
140: symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
141:
142:
143: memset (symbolP, 0, sizeof (symbolS));
144:
145: symbolP->bsym = bfd_make_empty_symbol (stdoutput);
146: if (symbolP->bsym == NULL)
147: as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
148: S_SET_NAME (symbolP, preserved_copy_of_name);
149:
150: S_SET_SEGMENT (symbolP, segment);
151: S_SET_VALUE (symbolP, valu);
152: symbol_clear_list_pointers (symbolP);
153:
154: symbolP->sy_frag = frag;
155:
156: obj_symbol_new_hook (symbolP);
157:
158: #ifdef tc_symbol_new_hook
159: tc_symbol_new_hook (symbolP);
160: #endif
161:
162: return symbolP;
163: }
164: ^L
165:
166:
167:
168:
169: static symbolS *local_symbol_convert (struct local_symbol *);
170:
171:
172:
173: static unsigned long local_symbol_count;
174: static unsigned long local_symbol_conversion_count;
175:
176:
177:
178:
179:
180: #define LOCAL_SYMBOL_CHECK(s) \
181: (s->bsym == NULL \
182: ? (local_symbol_converted_p ((struct local_symbol *) s) \
183: ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
184: 0) \
185: : 1) \
186: : 0)
187:
188:
189:
190: static struct local_symbol *
191: local_symbol_make (const char *name, segT section, valueT value, fragS *frag)
192: {
193: char *name_copy;
194: struct local_symbol *ret;
195:
196: ++local_symbol_count;
197:
198: name_copy = save_symbol_name (name);
199:
200: ret = (struct local_symbol *) obstack_alloc (¬es, sizeof *ret);
201: ret->lsy_marker = NULL;
202: ret->lsy_name = name_copy;
203: ret->lsy_section = section;
204: local_symbol_set_frag (ret, frag);
205: ret->lsy_value = value;
206:
207: hash_jam (local_hash, name_copy, (PTR) ret);
208:
209: return ret;
210: }
211:
212:
213:
214:
215: static symbolS *
216: local_symbol_convert (struct local_symbol *locsym)
217: {
218: symbolS *ret;
219:
220: assert (locsym->lsy_marker == NULL);
221: if (local_symbol_converted_p (locsym))
222: return local_symbol_get_real_symbol (locsym);
223:
224: ++local_symbol_conversion_count;
225:
226: ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
227: local_symbol_get_frag (locsym));
228:
229: if (local_symbol_resolved_p (locsym))
230: ret->sy_resolved = 1;
231:
232:
233: ret->sy_used = 1;
234:
235: #ifdef TC_LOCAL_SYMFIELD_CONVERT
236: TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
237: #endif
238:
239: symbol_table_insert (ret);
240:
241: local_symbol_mark_converted (locsym);
242: local_symbol_set_real_symbol (locsym, ret);
243:
244: hash_jam (local_hash, locsym->lsy_name, NULL);
245:
246: return ret;
247: }
248: ^L
249:
250:
251:
252:
253:
254: symbolS *
255: colon (
256: const char *sym_name
257: )
258: {
259: register symbolS *symbolP;
260:
261:
262:
263: if (LOCAL_LABELS_DOLLAR
264: && !bfd_is_local_label_name (stdoutput, sym_name))
265: dollar_label_clear ();
266:
267: #ifndef WORKING_DOT_WORD
268: if (new_broken_words)
269: {
270: struct broken_word *a;
271: int possible_bytes;
272: fragS *frag_tmp;
273: char *frag_opcode;
274:
275: if (now_seg == absolute_section)
276: {
277: as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
278: return NULL;
279: }
280:
281: possible_bytes = (md_short_jump_size
282: + new_broken_words * md_long_jump_size);
283:
284: frag_tmp = frag_now;
285: frag_opcode = frag_var (rs_broken_word,
286: possible_bytes,
287: possible_bytes,
288: (relax_substateT) 0,
289: (symbolS *) broken_words,
290: (offsetT) 0,
291: NULL);
292:
293:
294:
295:
296: while (frag_tmp
297: && (frag_tmp->fr_type != rs_broken_word
298: || frag_tmp->fr_opcode))
299: frag_tmp = frag_tmp->fr_next;
300: know (frag_tmp);
301: frag_tmp->fr_opcode = frag_opcode;
302: new_broken_words = 0;
303:
304: for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
305: a->dispfrag = frag_tmp;
306: }
307: #endif
308:
309: if ((symbolP = symbol_find (sym_name)) != 0)
310: {
311: S_CLEAR_WEAKREFR (symbolP);
312: #ifdef RESOLVE_SYMBOL_REDEFINITION
313: if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
314: return symbolP;
315: #endif
316:
317: if (LOCAL_SYMBOL_CHECK (symbolP))
318: {
319: struct local_symbol *locsym = (struct local_symbol *) symbolP;
320:
321: if (locsym->lsy_section != undefined_section
322: && (local_symbol_get_frag (locsym) != frag_now
323: || locsym->lsy_section != now_seg
324: || locsym->lsy_value != frag_now_fix ()))
325: {
326: as_bad (_("symbol `%s' is already defined"), sym_name);
327: return symbolP;
328: }
329:
330: locsym->lsy_section = now_seg;
331: local_symbol_set_frag (locsym, frag_now);
332: locsym->lsy_value = frag_now_fix ();
333: }
334: else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
335: || S_IS_COMMON (symbolP)
336: || S_IS_VOLATILE (symbolP))
337: {
338: if (S_IS_VOLATILE (symbolP))
339: {
340: symbolP = symbol_clone (symbolP, 1);
341: S_SET_VALUE (symbolP, 0);
342: S_CLEAR_VOLATILE (symbolP);
343: }
344: if (S_GET_VALUE (symbolP) == 0)
345: {
346: symbolP->sy_frag = frag_now;
347: #ifdef OBJ_VMS
348: S_SET_OTHER (symbolP, const_flag);
349: #endif
350: S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
351: S_SET_SEGMENT (symbolP, now_seg);
352: #ifdef N_UNDF
353: know (N_UNDF == 0);
354: #endif
355:
356: }
357: else
358: {
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370: if (((!S_IS_DEBUG (symbolP)
371: && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
372: && S_IS_EXTERNAL (symbolP))
373: || S_GET_SEGMENT (symbolP) == bss_section)
374: && (now_seg == data_section
375: || now_seg == bss_section
376: || now_seg == S_GET_SEGMENT (symbolP)))
377: {
378:
379: if (now_seg != data_section)
380: {
381:
382:
383:
384:
385:
386: if (S_GET_VALUE (symbolP)
387: < ((unsigned) frag_now_fix ()))
388: {
389: S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
390: }
391: }
392: else
393: {
394:
395:
396: symbolP->sy_frag = frag_now;
397: #ifdef OBJ_VMS
398: S_SET_OTHER (symbolP, const_flag);
399: #endif
400: S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
401: S_SET_SEGMENT (symbolP, now_seg);
402: }
403: }
404: else
405: {
406: #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
407: && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
408: static const char *od_buf = "";
409: #else
410: char od_buf[100];
411: od_buf[0] = '\0';
412: if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
413: sprintf (od_buf, "%d.%d.",
414: S_GET_OTHER (symbolP),
415: S_GET_DESC (symbolP));
416: #endif
417: as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
418: sym_name,
419: segment_name (S_GET_SEGMENT (symbolP)),
420: od_buf,
421: (long) S_GET_VALUE (symbolP));
422: }
423: }
424: }
425: else
426: {
427:
428: if (!(frag_now == symbolP->sy_frag
429: && S_GET_VALUE (symbolP) == frag_now_fix ()
430: && S_GET_SEGMENT (symbolP) == now_seg))
431: {
432: as_bad (_("symbol `%s' is already defined"), sym_name);
433: symbolP = symbol_clone (symbolP, 0);
434: }
435: }
436:
437: }
438: else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
439: {
440: symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
441: (valueT) frag_now_fix (),
442: frag_now);
443: }
444: else
445: {
446: symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
447: frag_now);
448: #ifdef OBJ_VMS
449: S_SET_OTHER (symbolP, const_flag);
450: #endif
451:
452: symbol_table_insert (symbolP);
453: }
454:
455: if (mri_common_symbol != NULL)
456: {
457:
458:
459: if (LOCAL_SYMBOL_CHECK (symbolP))
460: symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
461: symbolP->sy_value.X_op = O_symbol;
462: symbolP->sy_value.X_add_symbol = mri_common_symbol;
463: symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
464: symbolP->sy_frag = &zero_address_frag;
465: S_SET_SEGMENT (symbolP, expr_section);
466: symbolP->sy_mri_common = 1;
467: }
468:
469: #ifdef tc_frob_label
470: tc_frob_label (symbolP);
471: #endif
472: #ifdef obj_frob_label
473: obj_frob_label (symbolP);
474: #endif
475:
476: return symbolP;
477: }
478: ^L
479:
480:
481: void
482: symbol_table_insert (symbolS *symbolP)
483: {
484: register const char *error_string;
485:
486: know (symbolP);
487: know (S_GET_NAME (symbolP));
488:
489: if (LOCAL_SYMBOL_CHECK (symbolP))
490: {
491: error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
492: (PTR) symbolP);
493: if (error_string != NULL)
494: as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
495: S_GET_NAME (symbolP), error_string);
496: return;
497: }
498:
499: if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
500: {
501: as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
502: S_GET_NAME (symbolP), error_string);
503: }
504: }
505: ^L
506:
507:
508:
509: symbolS *
510: symbol_find_or_make (const char *name)
511: {
512: register symbolS *symbolP;
513:
514: symbolP = symbol_find (name);
515:
516: if (symbolP == NULL)
517: {
518: if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
519: {
520: symbolP = md_undefined_symbol ((char *) name);
521: if (symbolP != NULL)
522: return symbolP;
523:
524: symbolP = (symbolS *) local_symbol_make (name, undefined_section,
525: (valueT) 0,
526: &zero_address_frag);
527: return symbolP;
528: }
529:
530: symbolP = symbol_make (name);
531:
532: symbol_table_insert (symbolP);
533: }
534:
535: return (symbolP);
536: }
537:
538: symbolS *
539: symbol_make (const char *name)
540: {
541: symbolS *symbolP;
542:
543:
544: symbolP = md_undefined_symbol ((char *) name);
545:
546: if (!symbolP)
547: symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
548:
549: return (symbolP);
550: }
551:
552: symbolS *
553: symbol_clone (symbolS *orgsymP, int replace)
554: {
555: symbolS *newsymP;
556: asymbol *bsymorg, *bsymnew;
557:
558:
559:
560:
561:
562: if (LOCAL_SYMBOL_CHECK (orgsymP))
563: orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
564: bsymorg = orgsymP->bsym;
565:
566: newsymP = obstack_alloc (¬es, sizeof (*newsymP));
567: *newsymP = *orgsymP;
568: bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
569: if (bsymnew == NULL)
570: as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
571: newsymP->bsym = bsymnew;
572: bsymnew->name = bsymorg->name;
573: bsymnew->flags = bsymorg->flags;
574: bsymnew->section = bsymorg->section;
575: bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
576: bfd_asymbol_bfd (bsymnew), bsymnew);
577:
578: #ifdef obj_symbol_clone_hook
579: obj_symbol_clone_hook (newsymP, orgsymP);
580: #endif
581:
582: #ifdef tc_symbol_clone_hook
583: tc_symbol_clone_hook (newsymP, orgsymP);
584: #endif
585:
586: if (replace)
587: {
588: if (symbol_rootP == orgsymP)
589: symbol_rootP = newsymP;
590: else if (orgsymP->sy_previous)
591: {
592: orgsymP->sy_previous->sy_next = newsymP