symbols.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,503 行 · 第 1/4 页
C
2,503 行
voiddefine_dollar_label (label) long label;{ long *i; for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i) if (*i == label) { ++dollar_label_instances[i - dollar_labels]; dollar_label_defines[i - dollar_labels] = 1; return; } /* If we get to here, we don't have label listed yet. */ if (dollar_labels == NULL) { dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long)); dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long)); dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY); dollar_label_max = DOLLAR_LABEL_BUMP_BY; dollar_label_count = 0; } else if (dollar_label_count == dollar_label_max) { dollar_label_max += DOLLAR_LABEL_BUMP_BY; dollar_labels = (long *) xrealloc ((char *) dollar_labels, dollar_label_max * sizeof (long)); dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances, dollar_label_max * sizeof (long)); dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max); } /* if we needed to grow */ dollar_labels[dollar_label_count] = label; dollar_label_instances[dollar_label_count] = 1; dollar_label_defines[dollar_label_count] = 1; ++dollar_label_count;}/* Caller must copy returned name: we re-use the area for the next name. The mth occurence of label n: is turned into the symbol "Ln^Am" where n is the label number and m is the instance number. "L" makes it a label discarded unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the same name as a local label symbol. The first "4:" is "L4^A1" - the m numbers begin at 1. fb labels get the same treatment, except that ^B is used in place of ^A. */char * /* Return local label name. */dollar_label_name (n, augend) register long n; /* we just saw "n$:" : n a number. */ register int augend; /* 0 for current instance, 1 for new instance. */{ long i; /* Returned to caller, then copied. Used for created names ("4f"). */ static char symbol_name_build[24]; register char *p; register char *q; char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */ know (n >= 0); know (augend == 0 || augend == 1); p = symbol_name_build;#ifdef LOCAL_LABEL_PREFIX *p++ = LOCAL_LABEL_PREFIX;#endif *p++ = 'L'; /* Next code just does sprintf( {}, "%d", n); */ /* Label number. */ q = symbol_name_temporary; for (*q++ = 0, i = n; i; ++q) { *q = i % 10 + '0'; i /= 10; } while ((*p = *--q) != '\0') ++p; *p++ = DOLLAR_LABEL_CHAR; /* ^A */ /* Instance number. */ q = symbol_name_temporary; for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q) { *q = i % 10 + '0'; i /= 10; } while ((*p++ = *--q) != '\0');; /* The label, as a '\0' ended string, starts at symbol_name_build. */ return symbol_name_build;}/* Sombody else's idea of local labels. They are made by "n:" where n is any decimal digit. Refer to them with "nb" for previous (backward) n: or "nf" for next (forward) n:. We do a little better and let n be any number, not just a single digit, but since the other guy's assembler only does ten, we treat the first ten specially. Like someone else's assembler, we have one set of local label counters for entire assembly, not one set per (sub)segment like in most assemblers. This implies that one can refer to a label in another segment, and indeed some crufty compilers have done just that. Since there could be a LOT of these things, treat them as a sparse array. */#define FB_LABEL_SPECIAL (10)static long fb_low_counter[FB_LABEL_SPECIAL];static long *fb_labels;static long *fb_label_instances;static long fb_label_count;static long fb_label_max;/* This must be more than FB_LABEL_SPECIAL. */#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)static voidfb_label_init (){ memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));}/* Add one to the instance number of this fb label. */voidfb_label_instance_inc (label) long label;{ long *i; if (label < FB_LABEL_SPECIAL) { ++fb_low_counter[label]; return; } if (fb_labels != NULL) { for (i = fb_labels + FB_LABEL_SPECIAL; i < fb_labels + fb_label_count; ++i) { if (*i == label) { ++fb_label_instances[i - fb_labels]; return; } /* if we find it */ } /* for each existing label */ } /* If we get to here, we don't have label listed yet. */ if (fb_labels == NULL) { fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long)); fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long)); fb_label_max = FB_LABEL_BUMP_BY; fb_label_count = FB_LABEL_SPECIAL; } else if (fb_label_count == fb_label_max) { fb_label_max += FB_LABEL_BUMP_BY; fb_labels = (long *) xrealloc ((char *) fb_labels, fb_label_max * sizeof (long)); fb_label_instances = (long *) xrealloc ((char *) fb_label_instances, fb_label_max * sizeof (long)); } /* if we needed to grow */ fb_labels[fb_label_count] = label; fb_label_instances[fb_label_count] = 1; ++fb_label_count;}static longfb_label_instance (label) long label;{ long *i; if (label < FB_LABEL_SPECIAL) { return (fb_low_counter[label]); } if (fb_labels != NULL) { for (i = fb_labels + FB_LABEL_SPECIAL; i < fb_labels + fb_label_count; ++i) { if (*i == label) { return (fb_label_instances[i - fb_labels]); } /* if we find it */ } /* for each existing label */ } /* We didn't find the label, so this must be a reference to the first instance. */ return 0;}/* Caller must copy returned name: we re-use the area for the next name. The mth occurence of label n: is turned into the symbol "Ln^Bm" where n is the label number and m is the instance number. "L" makes it a label discarded unless debugging and "^B"('\2') ensures no ordinary symbol SHOULD get the same name as a local label symbol. The first "4:" is "L4^B1" - the m numbers begin at 1. dollar labels get the same treatment, except that ^A is used in place of ^B. */char * /* Return local label name. */fb_label_name (n, augend) long n; /* We just saw "n:", "nf" or "nb" : n a number. */ long augend; /* 0 for nb, 1 for n:, nf. */{ long i; /* Returned to caller, then copied. Used for created names ("4f"). */ static char symbol_name_build[24]; register char *p; register char *q; char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */ know (n >= 0); know (augend == 0 || augend == 1); p = symbol_name_build;#ifdef LOCAL_LABEL_PREFIX *p++ = LOCAL_LABEL_PREFIX;#endif *p++ = 'L'; /* Next code just does sprintf( {}, "%d", n); */ /* Label number. */ q = symbol_name_temporary; for (*q++ = 0, i = n; i; ++q) { *q = i % 10 + '0'; i /= 10; } while ((*p = *--q) != '\0') ++p; *p++ = LOCAL_LABEL_CHAR; /* ^B */ /* Instance number. */ q = symbol_name_temporary; for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q) { *q = i % 10 + '0'; i /= 10; } while ((*p++ = *--q) != '\0');; /* The label, as a '\0' ended string, starts at symbol_name_build. */ return (symbol_name_build);}/* Decode name that may have been generated by foo_label_name() above. If the name wasn't generated by foo_label_name(), then return it unaltered. This is used for error messages. */char *decode_local_label_name (s) char *s;{ char *p; char *symbol_decode; int label_number; int instance_number; char *type; const char *message_format; int index = 0;#ifdef LOCAL_LABEL_PREFIX if (s[index] == LOCAL_LABEL_PREFIX) ++index;#endif if (s[index] != 'L') return s; for (label_number = 0, p = s + index + 1; isdigit ((unsigned char) *p); ++p) label_number = (10 * label_number) + *p - '0'; if (*p == DOLLAR_LABEL_CHAR) type = "dollar"; else if (*p == LOCAL_LABEL_CHAR) type = "fb"; else return s; for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p) instance_number = (10 * instance_number) + *p - '0'; message_format = _("\"%d\" (instance number %d of a %s label)"); symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30); sprintf (symbol_decode, message_format, label_number, instance_number, type); return symbol_decode;}/* Get the value of a symbol. */valueTS_GET_VALUE (s) symbolS *s;{#ifdef BFD_ASSEMBLER if (LOCAL_SYMBOL_CHECK (s)) return ((struct local_symbol *) s)->lsy_offset;#endif if (!s->sy_resolved && s->sy_value.X_op != O_constant) resolve_symbol_value (s, 1); if (s->sy_value.X_op != O_constant) { static symbolS *recur; /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON may call S_GET_VALUE. We use a static symbol to avoid the immediate recursion. */ if (recur == s) return (valueT) s->sy_value.X_add_number; recur = s; if (! s->sy_resolved || s->sy_value.X_op != O_symbol || (S_IS_DEFINED (s) && ! S_IS_COMMON (s))) as_bad (_("Attempt to get value of unresolved symbol %s"), S_GET_NAME (s)); recur = NULL; } return (valueT) s->sy_value.X_add_number;}/* Set the value of a symbol. */voidS_SET_VALUE (s, val) symbolS *s; valueT val;{#ifdef BFD_ASSEMBLER if (LOCAL_SYMBOL_CHECK (s)) { ((struct local_symbol *) s)->lsy_offset = val; return; }#endif s->sy_value.X_op = O_constant; s->sy_value.X_add_number = (offsetT) val; s->sy_value.X_unsigned = 0;}voidcopy_symbol_attributes (dest, src) symbolS *dest, *src;{ if (LOCAL_SYMBOL_CHECK (dest)) dest = local_symbol_convert ((struct local_symbol *) dest); if (LOCAL_SYMBOL_CHECK (src)) src = local_symbol_convert ((struct local_symbol *) src);#ifdef BFD_ASSEMBLER /* In an expression, transfer the settings of these flags. The user can override later, of course. */#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT) dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;#endif#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);#endif}#ifdef BFD_ASSEMBLERintS_IS_FUNCTION (s) symbolS *s;{ flagword flags; if (LOCAL_SYMBOL_CHECK (s)) return 0; flags = s->bsym->flags; return (flags & BSF_FUNCTION) != 0;}intS_IS_EXTERNAL (s) symbolS *s;{ flagword flags; if (LOCAL_SYMBOL_CHECK (s)) return 0; flags = s->bsym->flags; /* Sanity check. */ if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL)) abort (); return (flags & BSF_GLOBAL) != 0;}intS_IS_WEAK (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return 0; return (s->bsym->flags & BSF_WEAK) != 0;}intS_IS_COMMON (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return 0; return bfd_is_com_section (s->bsym->section);}intS_IS_DEFINED (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return ((struct local_symbol *) s)->lsy_section != undefined_section; return s->bsym->section != undefined_section;}intS_IS_DEBUG (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return 0; if (s->bsym->flags & BSF_DEBUGGING) return 1; return 0;}intS_IS_LOCAL (s) symbolS *s;{ flagword flags; const char *name; if (LOCAL_SYMBOL_CHECK (s)) return 1; flags = s->bsym->flags; /* Sanity check. */ if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL)) abort (); if (bfd_get_section (s->bsym) == reg_section) return 1; if (flag_strip_local_absolute && (flags & BSF_GLOBAL) == 0 && bfd_get_section (s->bsym) == absolute_section) return 1; name = S_GET_NAME (s); return (name != NULL && ! S_IS_DEBUG (s) && (strchr (name, DOLLAR_LABEL_CHAR) || strchr (name, LOCAL_LABEL_CHAR) || (! flag_keep_locals && (bfd_is_local_label (stdoutput, s->bsym) || (flag_mri && name[0] == '?' && name[1] == '?')))));}intS_IS_EXTERN (s) symbolS *s;{ return S_IS_EXTERNAL (s);}intS_IS_STABD (s) symbolS *s;{ return S_GET_NAME (s) == 0;}CONST char *S_GET_NAME (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return ((struct local_symbol *) s)->lsy_name; return s->bsym->name;}segTS_GET_SEGMENT (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return ((struct local_symbol *) s)->lsy_section; return s->bsym->section;}voidS_SET_SEGMENT (s, seg) symbolS *s; segT seg;{ /* Don't reassign section symbols. The direct reason is to prevent seg faults assigning back to const global symbols such as *ABS*, but it shouldn't happen anyway. */ if (LOCAL_SYMBOL_CHECK (s)) { if (seg == reg_section) s = local_symbol_convert ((struct local_symbol *) s); else { ((struct local_symbol *) s)->lsy_section = seg; return; } } if (s->bsym->flags & BSF_SECTION_SYM) { if (s->bsym->section != seg) abort (); } else s->bsym->section = seg;}voidS_SET_EXTERNAL (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) s = local_symbol_convert ((struct local_symbol *) s); if ((s->bsym->flags & BSF_WEAK) != 0) { /* Let .weak override .global. */ return; } if (s->bsym->flags & BSF_SECTION_SYM) { char * file; unsigned int line; /* Do not reassign section symbols. */ as_where (& file, & line); as_warn_where (file, line, _("Section symbols are already global")); return; } s->bsym->flags |= BSF_GLOBAL; s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);}voidS_CLEAR_EXTERNAL (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) return; if ((s->bsym->flags & BSF_WEAK) != 0) { /* Let .weak override. */ return; } s->bsym->flags |= BSF_LOCAL; s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);}voidS_SET_WEAK (s) symbolS *s;{ if (LOCAL_SYMBOL_CHECK (s)) s = local_symbol_convert ((struct local_symbol *) s); s->bsym->flags |= BSF_WEAK; s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);}voidS_SET_NAME (s, name) symbolS *s; char *name;{ if (LOCAL_SYMBOL_CHECK (s)) { ((struct local_symbol *) s)->lsy_name = name; return; } s->bsym->name = name;}#endif /* BFD_ASSEMBLER */#ifdef SYMBOLS_NEED_BACKPOINTERS/* Return the previous symbol in a chain. */symbolS *symbol_previous (s)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?