symbols.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* symbols.c -symbol table-
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include "as.h"
  16. #include "hash.h"
  17. #include "obstack.h" /* For "symbols.h" */
  18. #include "struc-symbol.h"
  19. #include "symbols.h"
  20. #include "frags.h"
  21. #ifndef WORKING_DOT_WORD
  22. extern int new_broken_words;
  23. #endif
  24. static
  25. struct hash_control *
  26. sy_hash; /* symbol-name => struct symbol pointer */
  27. /* Below are commented in "symbols.h". */
  28. unsigned int local_bss_counter;
  29. symbolS * symbol_rootP;
  30. symbolS * symbol_lastP;
  31. symbolS abs_symbol;
  32. struct obstack notes;
  33. symbolS * symbol_find(); /* Keep C compiler happy. */
  34. /*
  35. * Un*x idea of local labels. They are made by "n:" where n
  36. * is any decimal digit. Refer to them with
  37. * "nb" for previous (backward) n:
  38. * or "nf" for next (forward) n:.
  39. *
  40. * Like Un*x AS, we have one set of local label counters for entire assembly,
  41. * not one set per (sub)segment like in most assemblers. This implies that
  42. * one can refer to a label in another segment, and indeed some crufty
  43. * compilers have done just that.
  44. *
  45. * I document the symbol names here to save duplicating words elsewhere.
  46. * The mth occurence of label n: is turned into the symbol "Ln^Am" where
  47. * n is a digit and m is a decimal number. "L" makes it a label discarded
  48. * unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the
  49. * same name as a local label symbol. The first "4:" is "L4^A1" - the m
  50. * numbers begin at 1.
  51. */
  52. typedef short unsigned int
  53. local_label_countT;
  54. static local_label_countT
  55. local_label_counter[10];
  56. static /* Returned to caller, then copied. */
  57. char symbol_name_build[12]; /* used for created names ("4f") */
  58. #ifdef SUN_ASM_SYNTAX
  59. static int local_label_defined[10];
  60. #endif
  61. void
  62. symbol_begin()
  63. {
  64. symbol_lastP = NULL;
  65. symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
  66. sy_hash = hash_new();
  67. bzero ((char *)(& abs_symbol), sizeof(abs_symbol));
  68. abs_symbol . sy_type = N_ABS; /* Can't initialise a union. Sigh. */
  69. bzero ((char *)(local_label_counter), sizeof(local_label_counter) );
  70. local_bss_counter = 0;
  71. }
  72. /*
  73. * local_label_name()
  74. *
  75. * Caller must copy returned name: we re-use the area for the next name.
  76. */
  77. char * /* Return local label name. */
  78. local_label_name(n, augend)
  79. register int n; /* we just saw "n:", "nf" or "nb" : n a digit */
  80. register int augend; /* 0 for nb, 1 for n:, nf */
  81. {
  82. register char * p;
  83. register char * q;
  84. char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
  85. know( n >= 0 );
  86. know( augend == 0 || augend == 1 );
  87. p = symbol_name_build;
  88. * p ++ = 'L';
  89. * p ++ = n + '0'; /* Make into ASCII */
  90. * p ++ = 1; /* ^A */
  91. n = local_label_counter [ n ] + augend;
  92. /* version number of this local label */
  93. /*
  94. * Next code just does sprintf( {}, "%d", n);
  95. * It is more elegant to do the next part recursively, but a procedure
  96. * call for each digit emitted is considered too costly.
  97. */
  98. q = symbol_name_temporary;
  99. for (*q++=0; n; q++) /* emits NOTHING if n starts as 0 */
  100. {
  101. know(n>0); /* We expect n > 0 always */
  102. *q = n % 10 + '0';
  103. n /= 10;
  104. }
  105. while ( * p ++ = * -- q )
  106. {
  107. }
  108. /* The label, as a '\0' ended string, starts at symbol_name_build. */
  109. return (symbol_name_build);
  110. }
  111. void
  112. local_colon (n)
  113. int n; /* just saw "n:" */
  114. {
  115. local_label_counter [n] ++;
  116. #ifdef SUN_ASM_SYNTAX
  117. local_label_defined[n]=1;
  118. #endif
  119. colon (local_label_name (n, 0));
  120. }
  121. /*
  122. * symbol_new()
  123. *
  124. * Return a pointer to a new symbol.
  125. * Die if we can't make a new symbol.
  126. * Fill in the symbol's values.
  127. * Add symbol to end of symbol chain.
  128. *
  129. *
  130. * Please always call this to create a new symbol.
  131. *
  132. * Changes since 1985: Symbol names may not contain '\0'. Sigh.
  133. */
  134. symbolS *
  135. symbol_new (name, type, other, desc, value, frag)
  136. char * name; /* We copy this: OK to alter your copy. */
  137. unsigned char type; /* As in <a.out.h>. */
  138. char other; /* As in <a.out.h>. */
  139. short int desc; /* As in <a.out.h>. */
  140. valueT value; /* As in <a.out.h>, often an address. */
  141. /* Often used as offset from frag address. */
  142. struct frag * frag; /* For sy_frag. */
  143. {
  144. register symbolS * symbolP;
  145. register char * preserved_copy_of_name;
  146. register unsigned int name_length;
  147. char * p;
  148. name_length = strlen(name) + 1;
  149. obstack_grow(&notes,name,name_length);
  150. p=obstack_finish(&notes);
  151. /* obstack_1done( &notes, name, name_length, &p ); */
  152. preserved_copy_of_name = p;
  153. p=obstack_alloc(&notes,sizeof(struct symbol));
  154. /* obstack_1blank( &notes, sizeof(struct symbol), &p ); */
  155. symbolP = (symbolS *) p;
  156. symbolP -> sy_name = preserved_copy_of_name;
  157. symbolP -> sy_type = type;
  158. symbolP -> sy_other = other;
  159. symbolP -> sy_desc = desc;
  160. symbolP -> sy_value = value;
  161. symbolP -> sy_frag = frag;
  162. symbolP -> sy_next = NULL; /* End of chain. */
  163. symbolP -> sy_forward = NULL; /* JF */
  164. #ifdef SUSPECT
  165. symbolP -> sy_name_offset = ~ 0; /* Impossible offset catches errors. */
  166. symbolP -> sy_number = ~ 0; /* Ditto. */
  167. #endif
  168. /*
  169. * Link to end of symbol chain.
  170. */
  171. if (symbol_lastP)
  172. {
  173. symbol_lastP -> sy_next = symbolP;
  174. }
  175. else
  176. {
  177. symbol_rootP = symbolP;
  178. }
  179. symbol_lastP = symbolP;
  180. return (symbolP);
  181. }
  182. /*
  183. * colon()
  184. *
  185. * We have just seen "<name>:".
  186. * Creates a struct symbol unless it already exists.
  187. *
  188. * Gripes if we are redefining a symbol incompatibly (and ignores it).
  189. *
  190. */
  191. void
  192. colon (sym_name) /* just seen "x:" - rattle symbols & frags */
  193. register char * sym_name; /* symbol name, as a cannonical string */
  194. /* We copy this string: OK to alter later. */
  195. {
  196. register struct symbol * symbolP; /* symbol we are working with */
  197. #ifdef SUN_ASM_SYNTAX
  198. /* Sun local labes go out of scope whenever a non-local symbol is
  199. defined. */
  200. if(*sym_name !='L')
  201. bzero((void *)local_label_defined,sizeof(local_label_defined));
  202. #endif
  203. #ifndef WORKING_DOT_WORD
  204. if(new_broken_words) {
  205. struct broken_word *a;
  206. int possible_bytes;
  207. fragS *frag_tmp;
  208. char *frag_opcode;
  209. extern md_short_jump_size;
  210. extern md_long_jump_size;
  211. possible_bytes=md_short_jump_size+new_broken_words*md_long_jump_size;
  212. frag_tmp=frag_now;
  213. frag_opcode=frag_var(rs_broken_word,possible_bytes,possible_bytes,(relax_substateT)0,(symbolS *)broken_words,(long int)0,(char *)0);
  214. /* We want to store the pointer to where to insert the jump table in the
  215. fr_opcode of the rs_broken_word frag. This requires a little hackery */
  216. while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
  217. frag_tmp=frag_tmp->fr_next;
  218. know(frag_tmp);
  219. frag_tmp->fr_opcode=frag_opcode;
  220. new_broken_words = 0;
  221. for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
  222. a->dispfrag=frag_tmp;
  223. }
  224. #endif
  225. if (symbolP = symbol_table_lookup( sym_name ))
  226. {
  227. #ifdef VMS
  228. /*
  229. * If the new symbol is .comm AND it has a size of zero,
  230. * we ignore it (i.e. the old symbol overrides it)
  231. */
  232. if ((seg_N_TYPE [(int) now_seg] == (N_UNDF | N_EXT)) &&
  233. ((obstack_next_free(& frags) - frag_now -> fr_literal) == 0))
  234. return;
  235. /*
  236. * If the old symbol is .comm and it has a size of zero,
  237. * we override it with the new symbol value.
  238. */
  239. if ((symbolP -> sy_type == (N_UNDF | N_EXT)) &&
  240. (symbolP->sy_value == 0)) {
  241. symbolP -> sy_frag = frag_now;
  242. symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  243. symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  244. return;
  245. }
  246. #endif /* VMS */
  247. /*
  248. * Now check for undefined symbols
  249. */
  250. if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  251. {
  252. if( symbolP -> sy_other == 0
  253. && symbolP -> sy_desc == 0
  254. && symbolP -> sy_value == 0)
  255. {
  256. symbolP -> sy_frag = frag_now;
  257. symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  258. know( N_UNDF == 0 );
  259. symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  260. }
  261. else
  262. {
  263. #ifdef VMS
  264. /*
  265. * There are still several cases to check:
  266. * A .comm/.lcomm symbol being redefined as
  267. * initialized data is OK
  268. * A .comm/.lcomm symbol being redefined with
  269. * a larger size is also OK
  270. */
  271. char New_Type = seg_N_TYPE [(int) now_seg];
  272. if (((symbolP->sy_type == (N_UNDF | N_EXT)) ||
  273. (symbolP->sy_type == N_BSS)) &&
  274. (((New_Type & ~N_EXT) == N_DATA) ||
  275. (New_Type == symbolP->sy_type))) {
  276. /*
  277. * Select which of the 2 cases this is
  278. */
  279. if (New_Type == symbolP->sy_type) {
  280. /*
  281. * If the new size is larger we just
  282. * change its value. If the new size
  283. * is smaller, we ignore this symbol
  284. */
  285. if (symbolP->sy_value <
  286. (obstack_next_free(& frags) -
  287. frag_now -> fr_literal)) {
  288. symbolP -> sy_value =
  289. obstack_next_free(& frags) -
  290. frag_now -> fr_literal;
  291. }
  292. } else {
  293. /*
  294. * It is a .comm/.lcomm being converted
  295. * to initialized data.
  296. */
  297. symbolP -> sy_frag = frag_now;
  298. symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  299. symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  300. }
  301. } else {
  302. #endif /* VMS */
  303. as_fatal( "Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
  304. sym_name,
  305. seg_name [(int) N_TYPE_seg [symbolP -> sy_type & N_TYPE]],
  306. symbolP -> sy_other, symbolP -> sy_desc,
  307. symbolP -> sy_value);
  308. #ifdef VMS
  309. }
  310. #endif /* VMS */
  311. }
  312. }
  313. else
  314. {
  315. as_fatal("Symbol %s already defined.",sym_name);
  316. }
  317. }
  318. else
  319. {
  320. symbolP = symbol_new (sym_name,
  321. (unsigned char)(seg_N_TYPE [(int) now_seg]),
  322. 0,
  323. 0,
  324. (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
  325. frag_now);
  326. symbol_table_insert (symbolP);
  327. }
  328. }
  329. /*
  330. * symbol_table_insert()
  331. *
  332. * Die if we can't insert the symbol.
  333. *
  334. */
  335. void
  336. symbol_table_insert (symbolP)
  337. struct symbol * symbolP;
  338. {
  339. register char * error_string;
  340. know( symbolP );
  341. know( symbolP -> sy_name );
  342. if ( * (error_string = hash_jam (sy_hash, symbolP -> sy_name, (char *)symbolP)))
  343. {
  344. as_fatal( "Inserting \"%s\" into symbol table failed: %s",
  345. symbolP -> sy_name, error_string);
  346. }
  347. }
  348. /*
  349. * symbol_find_or_make()
  350. *
  351. * If a symbol name does not exist, create it as undefined, and insert
  352. * it into the symbol table. Return a pointer to it.
  353. */
  354. symbolS *
  355. symbol_find_or_make (name)
  356. char * name;
  357. {
  358. register symbolS * symbolP;
  359. symbolP = symbol_table_lookup (name);
  360. if (symbolP == NULL)
  361. {
  362. symbolP = symbol_new (name, N_UNDF, 0, 0, 0, & zero_address_frag);
  363. symbol_table_insert (symbolP);
  364. }
  365. return (symbolP);
  366. }
  367. /*
  368. * symbol_find()
  369. *
  370. * Implement symbol table lookup.
  371. * In: A symbol's name as a string: '\0' can't be part of a symbol name.
  372. * Out: NULL if the name was not in the symbol table, else the address
  373. * of a struct symbol associated with that name.
  374. */
  375. symbolS *
  376. symbol_find (name)
  377. char * name;
  378. {
  379. return ( (symbolS *) hash_find( sy_hash, name ));
  380. }
  381. /* end: symbols.c */