• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Symbol table handling
3  *
4  *  Copyright (C) 2001-2007  Michael Urman, Peter Johnson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include "util.h"
28 
29 #include <limits.h>
30 #include <ctype.h>
31 
32 #include "libyasm-stdint.h"
33 #include "coretype.h"
34 #include "valparam.h"
35 #include "hamt.h"
36 #include "assocdat.h"
37 
38 #include "errwarn.h"
39 #include "intnum.h"
40 #include "floatnum.h"
41 #include "expr.h"
42 #include "symrec.h"
43 
44 #include "bytecode.h"
45 #include "section.h"
46 #include "objfmt.h"
47 
48 
49 typedef enum {
50     SYM_UNKNOWN,                /* for unknown type (COMMON/EXTERN) */
51     SYM_EQU,                    /* for EQU defined symbols (expressions) */
52     SYM_LABEL,                  /* for labels */
53     SYM_CURPOS,                 /* for labels representing the current
54                                    assembly position */
55     SYM_SPECIAL                 /* for special symbols that need to be in
56                                    the symbol table but otherwise have no
57                                    purpose */
58 } sym_type;
59 
60 struct yasm_symrec {
61     char *name;
62     sym_type type;
63     yasm_sym_status status;
64     yasm_sym_vis visibility;
65     unsigned long def_line;     /* line where symbol was first defined */
66     unsigned long decl_line;    /* line where symbol was first declared */
67     unsigned long use_line;     /* line where symbol was first used */
68     union {
69         yasm_expr *expn;        /* equ value */
70 
71         /* bytecode immediately preceding a label */
72         /*@dependent@*/ yasm_bytecode *precbc;
73     } value;
74     unsigned int size;          /* 0 if not user-defined */
75     const char *segment;        /* for segmented systems like DOS */
76 
77     /* associated data; NULL if none */
78     /*@null@*/ /*@only@*/ yasm__assoc_data *assoc_data;
79 };
80 
81 /* Linked list of symbols not in the symbol table. */
82 typedef struct non_table_symrec_s {
83      /*@reldef@*/ SLIST_ENTRY(non_table_symrec_s) link;
84      /*@owned@*/ yasm_symrec *rec;
85 } non_table_symrec;
86 
87 struct yasm_symtab {
88     /* The symbol table: a hash array mapped trie (HAMT). */
89     /*@only@*/ HAMT *sym_table;
90     /* Symbols not in the table */
91     SLIST_HEAD(nontablesymhead_s, non_table_symrec_s) non_table_syms;
92 
93     int case_sensitive;
94 };
95 
96 static void
objext_valparams_destroy(void * data)97 objext_valparams_destroy(void *data)
98 {
99     yasm_vps_destroy((yasm_valparamhead *)data);
100 }
101 
102 static void
objext_valparams_print(void * data,FILE * f,int indent_level)103 objext_valparams_print(void *data, FILE *f, int indent_level)
104 {
105     yasm_vps_print((yasm_valparamhead *)data, f);
106 }
107 
108 static yasm_assoc_data_callback objext_valparams_cb = {
109     objext_valparams_destroy,
110     objext_valparams_print
111 };
112 
113 static void
common_size_destroy(void * data)114 common_size_destroy(void *data)
115 {
116     yasm_expr **e = (yasm_expr **)data;
117     yasm_expr_destroy(*e);
118     yasm_xfree(data);
119 }
120 
121 static void
common_size_print(void * data,FILE * f,int indent_level)122 common_size_print(void *data, FILE *f, int indent_level)
123 {
124     yasm_expr **e = (yasm_expr **)data;
125     yasm_expr_print(*e, f);
126 }
127 
128 static yasm_assoc_data_callback common_size_cb = {
129     common_size_destroy,
130     common_size_print
131 };
132 
133 yasm_symtab *
yasm_symtab_create(void)134 yasm_symtab_create(void)
135 {
136     yasm_symtab *symtab = yasm_xmalloc(sizeof(yasm_symtab));
137     symtab->sym_table = HAMT_create(0, yasm_internal_error_);
138     SLIST_INIT(&symtab->non_table_syms);
139     symtab->case_sensitive = 1;
140     return symtab;
141 }
142 
143 void
yasm_symtab_set_case_sensitive(yasm_symtab * symtab,int sensitive)144 yasm_symtab_set_case_sensitive(yasm_symtab *symtab, int sensitive)
145 {
146     symtab->case_sensitive = sensitive;
147 }
148 
149 static void
symrec_destroy_one(void * d)150 symrec_destroy_one(/*@only@*/ void *d)
151 {
152     yasm_symrec *sym = d;
153     yasm_xfree(sym->name);
154     if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED))
155         yasm_expr_destroy(sym->value.expn);
156     yasm__assoc_data_destroy(sym->assoc_data);
157     yasm_xfree(sym);
158 }
159 
160 static /*@partial@*/ yasm_symrec *
symrec_new_common(char * name,int case_sensitive)161 symrec_new_common(/*@keep@*/ char *name, int case_sensitive)
162 {
163     yasm_symrec *rec = yasm_xmalloc(sizeof(yasm_symrec));
164 
165     if (!case_sensitive) {
166         char *c;
167         for (c=name; *c; c++)
168             *c = tolower(*c);
169     }
170 
171     rec->name = name;
172     rec->type = SYM_UNKNOWN;
173     rec->def_line = 0;
174     rec->decl_line = 0;
175     rec->use_line = 0;
176     rec->visibility = YASM_SYM_LOCAL;
177     rec->size = 0;
178     rec->segment = NULL;
179     rec->assoc_data = NULL;
180     return rec;
181 }
182 
183 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symtab_get_or_new_in_table(yasm_symtab * symtab,char * name)184 symtab_get_or_new_in_table(yasm_symtab *symtab, /*@only@*/ char *name)
185 {
186     yasm_symrec *rec = symrec_new_common(name, symtab->case_sensitive);
187     int replace = 0;
188 
189     rec->status = YASM_SYM_NOSTATUS;
190 
191     if (!symtab->case_sensitive) {
192         char *c;
193         for (c=name; *c; c++)
194             *c = tolower(*c);
195     }
196 
197     return HAMT_insert(symtab->sym_table, name, rec, &replace,
198                        symrec_destroy_one);
199 }
200 
201 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symtab_get_or_new_not_in_table(yasm_symtab * symtab,char * name)202 symtab_get_or_new_not_in_table(yasm_symtab *symtab, /*@only@*/ char *name)
203 {
204     non_table_symrec *sym = yasm_xmalloc(sizeof(non_table_symrec));
205     sym->rec = symrec_new_common(name, symtab->case_sensitive);
206 
207     sym->rec->status = YASM_SYM_NOTINTABLE;
208 
209     SLIST_INSERT_HEAD(&symtab->non_table_syms, sym, link);
210 
211     return sym->rec;
212 }
213 
214 /* create a new symrec */
215 /*@-freshtrans -mustfree@*/
216 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symtab_get_or_new(yasm_symtab * symtab,const char * name,int in_table)217 symtab_get_or_new(yasm_symtab *symtab, const char *name, int in_table)
218 {
219     char *symname = yasm__xstrdup(name);
220 
221     if (in_table)
222         return symtab_get_or_new_in_table(symtab, symname);
223     else
224         return symtab_get_or_new_not_in_table(symtab, symname);
225 }
226 /*@=freshtrans =mustfree@*/
227 
228 int
yasm_symtab_traverse(yasm_symtab * symtab,void * d,int (* func)(yasm_symrec * sym,void * d))229 yasm_symtab_traverse(yasm_symtab *symtab, void *d,
230                      int (*func) (yasm_symrec *sym, void *d))
231 {
232     return HAMT_traverse(symtab->sym_table, d, (int (*) (void *, void *))func);
233 }
234 
235 const yasm_symtab_iter *
yasm_symtab_first(const yasm_symtab * symtab)236 yasm_symtab_first(const yasm_symtab *symtab)
237 {
238     return (const yasm_symtab_iter *)HAMT_first(symtab->sym_table);
239 }
240 
241 /*@null@*/ const yasm_symtab_iter *
yasm_symtab_next(const yasm_symtab_iter * prev)242 yasm_symtab_next(const yasm_symtab_iter *prev)
243 {
244     return (const yasm_symtab_iter *)HAMT_next((const HAMTEntry *)prev);
245 }
246 
247 yasm_symrec *
yasm_symtab_iter_value(const yasm_symtab_iter * cur)248 yasm_symtab_iter_value(const yasm_symtab_iter *cur)
249 {
250     return (yasm_symrec *)HAMTEntry_get_data((const HAMTEntry *)cur);
251 }
252 
253 yasm_symrec *
yasm_symtab_abs_sym(yasm_symtab * symtab)254 yasm_symtab_abs_sym(yasm_symtab *symtab)
255 {
256     yasm_symrec *rec = symtab_get_or_new(symtab, "", 1);
257     rec->def_line = 0;
258     rec->decl_line = 0;
259     rec->use_line = 0;
260     rec->type = SYM_EQU;
261     rec->value.expn =
262         yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0);
263     rec->status |= YASM_SYM_DEFINED|YASM_SYM_VALUED|YASM_SYM_USED;
264     return rec;
265 }
266 
267 yasm_symrec *
yasm_symtab_use(yasm_symtab * symtab,const char * name,unsigned long line)268 yasm_symtab_use(yasm_symtab *symtab, const char *name, unsigned long line)
269 {
270     yasm_symrec *rec = symtab_get_or_new(symtab, name, 1);
271     if (rec->use_line == 0)
272         rec->use_line = line;   /* set line number of first use */
273     rec->status |= YASM_SYM_USED;
274     return rec;
275 }
276 
277 yasm_symrec *
yasm_symtab_get(yasm_symtab * symtab,const char * name)278 yasm_symtab_get(yasm_symtab *symtab, const char *name)
279 {
280     if (!symtab->case_sensitive) {
281         char *_name = yasm__xstrdup(name);
282         char *c;
283         yasm_symrec *ret;
284         for (c=_name; *c; c++)
285             *c = tolower(*c);
286         ret = HAMT_search(symtab->sym_table, _name);
287         yasm_xfree(_name);
288         return ret;
289     } else
290       return HAMT_search(symtab->sym_table, name);
291 }
292 
293 static /*@dependent@*/ yasm_symrec *
symtab_define(yasm_symtab * symtab,const char * name,sym_type type,int in_table,unsigned long line)294 symtab_define(yasm_symtab *symtab, const char *name, sym_type type,
295               int in_table, unsigned long line)
296 {
297     yasm_symrec *rec = symtab_get_or_new(symtab, name, in_table);
298 
299     /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */
300     if (rec->status & YASM_SYM_DEFINED) {
301         yasm_error_set_xref(rec->def_line!=0 ? rec->def_line : rec->decl_line,
302                             N_("`%s' previously defined here"), name);
303         yasm_error_set(YASM_ERROR_GENERAL, N_("redefinition of `%s'"),
304                        name);
305     } else {
306         if (rec->visibility & YASM_SYM_EXTERN)
307             yasm_warn_set(YASM_WARN_GENERAL,
308                           N_("`%s' both defined and declared extern"), name);
309         rec->def_line = line;   /* set line number of definition */
310         rec->type = type;
311         rec->status |= YASM_SYM_DEFINED;
312         rec->size = 0;
313         rec->segment = NULL;
314     }
315     return rec;
316 }
317 
318 yasm_symrec *
yasm_symtab_define_equ(yasm_symtab * symtab,const char * name,yasm_expr * e,unsigned long line)319 yasm_symtab_define_equ(yasm_symtab *symtab, const char *name, yasm_expr *e,
320                        unsigned long line)
321 {
322     yasm_symrec *rec = symtab_define(symtab, name, SYM_EQU, 1, line);
323     if (yasm_error_occurred())
324         return rec;
325     rec->value.expn = e;
326     rec->status |= YASM_SYM_VALUED;
327     return rec;
328 }
329 
330 yasm_symrec *
yasm_symtab_define_label(yasm_symtab * symtab,const char * name,yasm_bytecode * precbc,int in_table,unsigned long line)331 yasm_symtab_define_label(yasm_symtab *symtab, const char *name,
332                          yasm_bytecode *precbc, int in_table,
333                          unsigned long line)
334 {
335     yasm_symrec *rec = symtab_define(symtab, name, SYM_LABEL, in_table, line);
336     if (yasm_error_occurred())
337         return rec;
338     rec->value.precbc = precbc;
339     if (in_table && precbc)
340         yasm_bc__add_symrec(precbc, rec);
341     return rec;
342 }
343 
344 yasm_symrec *
yasm_symtab_define_curpos(yasm_symtab * symtab,const char * name,yasm_bytecode * precbc,unsigned long line)345 yasm_symtab_define_curpos(yasm_symtab *symtab, const char *name,
346                           yasm_bytecode *precbc, unsigned long line)
347 {
348     yasm_symrec *rec = symtab_define(symtab, name, SYM_CURPOS, 0, line);
349     if (yasm_error_occurred())
350         return rec;
351     rec->value.precbc = precbc;
352     return rec;
353 }
354 
355 yasm_symrec *
yasm_symtab_define_special(yasm_symtab * symtab,const char * name,yasm_sym_vis vis)356 yasm_symtab_define_special(yasm_symtab *symtab, const char *name,
357                            yasm_sym_vis vis)
358 {
359     yasm_symrec *rec = symtab_define(symtab, name, SYM_SPECIAL, 1, 0);
360     if (yasm_error_occurred())
361         return rec;
362     rec->status |= YASM_SYM_VALUED;
363     rec->visibility = vis;
364     return rec;
365 }
366 
367 yasm_symrec *
yasm_symtab_declare(yasm_symtab * symtab,const char * name,yasm_sym_vis vis,unsigned long line)368 yasm_symtab_declare(yasm_symtab *symtab, const char *name, yasm_sym_vis vis,
369                     unsigned long line)
370 {
371     yasm_symrec *rec = symtab_get_or_new(symtab, name, 1);
372     yasm_symrec_declare(rec, vis, line);
373     return rec;
374 }
375 
376 void
yasm_symrec_declare(yasm_symrec * rec,yasm_sym_vis vis,unsigned long line)377 yasm_symrec_declare(yasm_symrec *rec, yasm_sym_vis vis, unsigned long line)
378 {
379     /* Allowable combinations:
380      *  Existing State--------------  vis  New State-------------------
381      *  DEFINED GLOBAL COMMON EXTERN  GCE  DEFINED GLOBAL COMMON EXTERN
382      *     0      -      0      0     GCE     0      G      C      E
383      *     0      -      0      1     GE      0      G      0      E
384      *     0      -      1      0     GC      0      G      C      0
385      * X   0      -      1      1
386      *     1      -      0      0      G      1      G      0      0
387      * X   1      -      -      1
388      * X   1      -      1      -
389      */
390     if ((vis == YASM_SYM_GLOBAL) ||
391         (!(rec->status & YASM_SYM_DEFINED) &&
392          (!(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN)) ||
393           ((rec->visibility & YASM_SYM_COMMON) && (vis == YASM_SYM_COMMON)) ||
394           ((rec->visibility & YASM_SYM_EXTERN) && (vis == YASM_SYM_EXTERN))))) {
395         rec->decl_line = line;
396         rec->visibility |= vis;
397     } else
398         yasm_error_set(YASM_ERROR_GENERAL,
399             N_("duplicate definition of `%s'; first defined on line %lu"),
400             rec->name, rec->def_line!=0 ? rec->def_line : rec->decl_line);
401 }
402 
403 typedef struct symtab_finalize_info {
404     unsigned long firstundef_line;
405     int undef_extern;
406     yasm_errwarns *errwarns;
407 } symtab_finalize_info;
408 
409 static int
symtab_parser_finalize_checksym(yasm_symrec * sym,void * d)410 symtab_parser_finalize_checksym(yasm_symrec *sym, /*@null@*/ void *d)
411 {
412     symtab_finalize_info *info = (symtab_finalize_info *)d;
413 
414     /* error if a symbol is used but never defined or extern/common declared */
415     if ((sym->status & YASM_SYM_USED) && !(sym->status & YASM_SYM_DEFINED) &&
416         !(sym->visibility & (YASM_SYM_EXTERN | YASM_SYM_COMMON))) {
417         if (info->undef_extern)
418             sym->visibility |= YASM_SYM_EXTERN;
419         else {
420             yasm_error_set(YASM_ERROR_GENERAL,
421                            N_("undefined symbol `%s' (first use)"), sym->name);
422             yasm_errwarn_propagate(info->errwarns, sym->use_line);
423             if (sym->use_line < info->firstundef_line)
424                 info->firstundef_line = sym->use_line;
425         }
426     }
427 
428     return 0;
429 }
430 
431 void
yasm_symtab_parser_finalize(yasm_symtab * symtab,int undef_extern,yasm_errwarns * errwarns)432 yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern,
433                             yasm_errwarns *errwarns)
434 {
435     symtab_finalize_info info;
436     info.firstundef_line = ULONG_MAX;
437     info.undef_extern = undef_extern;
438     info.errwarns = errwarns;
439     yasm_symtab_traverse(symtab, &info, symtab_parser_finalize_checksym);
440     if (info.firstundef_line < ULONG_MAX) {
441         yasm_error_set(YASM_ERROR_GENERAL,
442                        N_(" (Each undefined symbol is reported only once.)"));
443         yasm_errwarn_propagate(errwarns, info.firstundef_line);
444     }
445 }
446 
447 void
yasm_symtab_destroy(yasm_symtab * symtab)448 yasm_symtab_destroy(yasm_symtab *symtab)
449 {
450     HAMT_destroy(symtab->sym_table, symrec_destroy_one);
451 
452     while (!SLIST_EMPTY(&symtab->non_table_syms)) {
453         non_table_symrec *sym = SLIST_FIRST(&symtab->non_table_syms);
454         SLIST_REMOVE_HEAD(&symtab->non_table_syms, link);
455         symrec_destroy_one(sym->rec);
456         yasm_xfree(sym);
457     }
458 
459     yasm_xfree(symtab);
460 }
461 
462 typedef struct symrec_print_data {
463     FILE *f;
464     int indent_level;
465 } symrec_print_data;
466 
467 /*@+voidabstract@*/
468 static int
symrec_print_wrapper(yasm_symrec * sym,void * d)469 symrec_print_wrapper(yasm_symrec *sym, /*@null@*/ void *d)
470 {
471     symrec_print_data *data = (symrec_print_data *)d;
472     assert(data != NULL);
473     fprintf(data->f, "%*sSymbol `%s'\n", data->indent_level, "", sym->name);
474     yasm_symrec_print(sym, data->f, data->indent_level+1);
475     return 0;
476 }
477 
478 void
yasm_symtab_print(yasm_symtab * symtab,FILE * f,int indent_level)479 yasm_symtab_print(yasm_symtab *symtab, FILE *f, int indent_level)
480 {
481     symrec_print_data data;
482     data.f = f;
483     data.indent_level = indent_level;
484     yasm_symtab_traverse(symtab, &data, symrec_print_wrapper);
485 }
486 /*@=voidabstract@*/
487 
488 const char *
yasm_symrec_get_name(const yasm_symrec * sym)489 yasm_symrec_get_name(const yasm_symrec *sym)
490 {
491     return sym->name;
492 }
493 
494 char *
yasm_symrec_get_global_name(const yasm_symrec * sym,const yasm_object * object)495 yasm_symrec_get_global_name(const yasm_symrec *sym, const yasm_object *object)
496 {
497     if (sym->visibility & (YASM_SYM_GLOBAL|YASM_SYM_COMMON|YASM_SYM_EXTERN)) {
498         char *name = yasm_xmalloc(strlen(object->global_prefix) +
499                                   strlen(sym->name) +
500                                   strlen(object->global_suffix) + 1);
501         strcpy(name, object->global_prefix);
502         strcat(name, sym->name);
503         strcat(name, object->global_suffix);
504         return name;
505     }
506     return yasm__xstrdup(sym->name);
507 }
508 
509 yasm_sym_vis
yasm_symrec_get_visibility(const yasm_symrec * sym)510 yasm_symrec_get_visibility(const yasm_symrec *sym)
511 {
512     return sym->visibility;
513 }
514 
515 yasm_sym_status
yasm_symrec_get_status(const yasm_symrec * sym)516 yasm_symrec_get_status(const yasm_symrec *sym)
517 {
518     return sym->status;
519 }
520 
521 unsigned long
yasm_symrec_get_def_line(const yasm_symrec * sym)522 yasm_symrec_get_def_line(const yasm_symrec *sym)
523 {
524     return sym->def_line;
525 }
526 
527 unsigned long
yasm_symrec_get_decl_line(const yasm_symrec * sym)528 yasm_symrec_get_decl_line(const yasm_symrec *sym)
529 {
530     return sym->decl_line;
531 }
532 
533 unsigned long
yasm_symrec_get_use_line(const yasm_symrec * sym)534 yasm_symrec_get_use_line(const yasm_symrec *sym)
535 {
536     return sym->use_line;
537 }
538 
539 const yasm_expr *
yasm_symrec_get_equ(const yasm_symrec * sym)540 yasm_symrec_get_equ(const yasm_symrec *sym)
541 {
542     if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED))
543         return sym->value.expn;
544     return (const yasm_expr *)NULL;
545 }
546 
547 int
yasm_symrec_get_label(const yasm_symrec * sym,yasm_symrec_get_label_bytecodep * precbc)548 yasm_symrec_get_label(const yasm_symrec *sym,
549                       yasm_symrec_get_label_bytecodep *precbc)
550 {
551     if (!(sym->type == SYM_LABEL || sym->type == SYM_CURPOS)
552         || !sym->value.precbc) {
553         *precbc = (yasm_symrec_get_label_bytecodep)0xDEADBEEF;
554         return 0;
555     }
556     *precbc = sym->value.precbc;
557     return 1;
558 }
559 
560 void
yasm_symrec_set_size(yasm_symrec * sym,int size)561 yasm_symrec_set_size(yasm_symrec *sym, int size)
562 {
563     sym->size = size;
564 }
565 
566 int
yasm_symrec_get_size(const yasm_symrec * sym)567 yasm_symrec_get_size(const yasm_symrec *sym)
568 {
569     return sym->size;
570 }
571 
572 void
yasm_symrec_set_segment(yasm_symrec * sym,const char * segment)573 yasm_symrec_set_segment(yasm_symrec *sym, const char *segment)
574 {
575     sym->segment = segment;
576 }
577 
578 const char *
yasm_symrec_get_segment(const yasm_symrec * sym)579 yasm_symrec_get_segment(const yasm_symrec *sym)
580 {
581     return sym->segment;
582 }
583 
584 int
yasm_symrec_is_abs(const yasm_symrec * sym)585 yasm_symrec_is_abs(const yasm_symrec *sym)
586 {
587     return (sym->def_line == 0 && sym->type == SYM_EQU &&
588             sym->name[0] == '\0');
589 }
590 
591 int
yasm_symrec_is_special(const yasm_symrec * sym)592 yasm_symrec_is_special(const yasm_symrec *sym)
593 {
594     return (sym->type == SYM_SPECIAL);
595 }
596 
597 int
yasm_symrec_is_curpos(const yasm_symrec * sym)598 yasm_symrec_is_curpos(const yasm_symrec *sym)
599 {
600     return (sym->type == SYM_CURPOS);
601 }
602 
603 void
yasm_symrec_set_objext_valparams(yasm_symrec * sym,yasm_valparamhead * objext_valparams)604 yasm_symrec_set_objext_valparams(yasm_symrec *sym,
605                                  /*@only@*/ yasm_valparamhead *objext_valparams)
606 {
607     yasm_symrec_add_data(sym, &objext_valparams_cb, objext_valparams);
608 }
609 
610 yasm_valparamhead *
yasm_symrec_get_objext_valparams(yasm_symrec * sym)611 yasm_symrec_get_objext_valparams(yasm_symrec *sym)
612 {
613     return yasm_symrec_get_data(sym, &objext_valparams_cb);
614 }
615 
616 void
yasm_symrec_set_common_size(yasm_symrec * sym,yasm_expr * common_size)617 yasm_symrec_set_common_size(yasm_symrec *sym,
618                             /*@only@*/ yasm_expr *common_size)
619 {
620     yasm_expr **ep = yasm_xmalloc(sizeof(yasm_expr *));
621     *ep = common_size;
622     yasm_symrec_add_data(sym, &common_size_cb, ep);
623 }
624 
625 yasm_expr **
yasm_symrec_get_common_size(yasm_symrec * sym)626 yasm_symrec_get_common_size(yasm_symrec *sym)
627 {
628     return (yasm_expr **)yasm_symrec_get_data(sym, &common_size_cb);
629 }
630 
631 void *
yasm_symrec_get_data(yasm_symrec * sym,const yasm_assoc_data_callback * callback)632 yasm_symrec_get_data(yasm_symrec *sym,
633                      const yasm_assoc_data_callback *callback)
634 {
635     return yasm__assoc_data_get(sym->assoc_data, callback);
636 }
637 
638 void
yasm_symrec_add_data(yasm_symrec * sym,const yasm_assoc_data_callback * callback,void * data)639 yasm_symrec_add_data(yasm_symrec *sym,
640                      const yasm_assoc_data_callback *callback, void *data)
641 {
642     sym->assoc_data = yasm__assoc_data_add(sym->assoc_data, callback, data);
643 }
644 
645 void
yasm_symrec_print(const yasm_symrec * sym,FILE * f,int indent_level)646 yasm_symrec_print(const yasm_symrec *sym, FILE *f, int indent_level)
647 {
648     switch (sym->type) {
649         case SYM_UNKNOWN:
650             fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, "");
651             break;
652         case SYM_EQU:
653             fprintf(f, "%*s_EQU_\n", indent_level, "");
654             fprintf(f, "%*sExpn=", indent_level, "");
655             if (sym->status & YASM_SYM_VALUED)
656                 yasm_expr_print(sym->value.expn, f);
657             else
658                 fprintf(f, "***UNVALUED***");
659             fprintf(f, "\n");
660             break;
661         case SYM_LABEL:
662         case SYM_CURPOS:
663             fprintf(f, "%*s_%s_\n%*sSection:\n", indent_level, "",
664                     sym->type == SYM_LABEL ? "Label" : "CurPos",
665                     indent_level, "");
666             yasm_section_print(yasm_bc_get_section(sym->value.precbc), f,
667                                indent_level+1, 0);
668             fprintf(f, "%*sPreceding bytecode:\n", indent_level, "");
669             yasm_bc_print(sym->value.precbc, f, indent_level+1);
670             break;
671         case SYM_SPECIAL:
672             fprintf(f, "%*s-Special-\n", indent_level, "");
673             break;
674     }
675 
676     fprintf(f, "%*sStatus=", indent_level, "");
677     if (sym->status == YASM_SYM_NOSTATUS)
678         fprintf(f, "None\n");
679     else {
680         if (sym->status & YASM_SYM_USED)
681             fprintf(f, "Used,");
682         if (sym->status & YASM_SYM_DEFINED)
683             fprintf(f, "Defined,");
684         if (sym->status & YASM_SYM_VALUED)
685             fprintf(f, "Valued,");
686         if (sym->status & YASM_SYM_NOTINTABLE)
687             fprintf(f, "Not in Table,");
688         fprintf(f, "\n");
689     }
690 
691     fprintf(f, "%*sVisibility=", indent_level, "");
692     if (sym->visibility == YASM_SYM_LOCAL)
693         fprintf(f, "Local\n");
694     else {
695         if (sym->visibility & YASM_SYM_GLOBAL)
696             fprintf(f, "Global,");
697         if (sym->visibility & YASM_SYM_COMMON)
698             fprintf(f, "Common,");
699         if (sym->visibility & YASM_SYM_EXTERN)
700             fprintf(f, "Extern,");
701         fprintf(f, "\n");
702     }
703 
704     if (sym->assoc_data) {
705         fprintf(f, "%*sAssociated data:\n", indent_level, "");
706         yasm__assoc_data_print(sym->assoc_data, f, indent_level+1);
707     }
708 
709     fprintf(f, "%*sLine Index (Defined)=%lu\n", indent_level, "",
710             sym->def_line);
711     fprintf(f, "%*sLine Index (Declared)=%lu\n", indent_level, "",
712             sym->decl_line);
713     fprintf(f, "%*sLine Index (Used)=%lu\n", indent_level, "", sym->use_line);
714 }
715