test/reldef.asm: add some global symbols
[nasm.git] / asm / labels.c
blob1183151ceb38fe2811c51f9a4cb7b44aee093546
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * labels.c label handling for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "error.h"
47 #include "hashtbl.h"
48 #include "labels.h"
51 * A local label is one that begins with exactly one period. Things
52 * that begin with _two_ periods are NASM-specific things.
54 * If TASM compatibility is enabled, a local label can also begin with
55 * @@, so @@local is a TASM compatible local label. Note that we only
56 * check for the first @ symbol, although TASM requires both.
58 #define islocal(l) \
59 (tasm_compatible_mode ? \
60 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
61 ((l)[0] == '.' && (l)[1] != '.'))
62 #define islocalchar(c) \
63 (tasm_compatible_mode ? \
64 ((c) == '.' || (c) == '@') : \
65 ((c) == '.'))
67 #define LABEL_BLOCK 128 /* no. of labels/block */
68 #define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
70 #define END_LIST -3 /* don't clash with NO_SEG! */
71 #define END_BLOCK -2
72 #define BOGUS_VALUE -4
74 #define PERMTS_SIZE 16384 /* size of text blocks */
75 #if (PERMTS_SIZE < IDLEN_MAX)
76 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
77 #endif
79 /* values for label.defn.is_global */
80 #define DEFINED_BIT 1
81 #define GLOBAL_BIT 2
82 #define EXTERN_BIT 4
83 #define COMMON_BIT 8
85 #define NOT_DEFINED_YET 0
86 #define TYPE_MASK 3
87 #define LOCAL_SYMBOL (DEFINED_BIT)
88 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
89 #define GLOBAL_SYMBOL (DEFINED_BIT | GLOBAL_BIT)
91 union label { /* actual label structures */
92 struct {
93 int32_t segment;
94 int64_t offset;
95 char *label, *special;
96 int is_global, is_norm;
97 } defn;
98 struct {
99 int32_t movingon;
100 int64_t dummy;
101 union label *next;
102 } admin;
105 struct permts { /* permanent text storage */
106 struct permts *next; /* for the linked list */
107 int size, usage; /* size and used space in ... */
108 char data[PERMTS_SIZE]; /* ... the data block itself */
111 uint64_t global_offset_changed; /* counter for global offset changes */
113 static struct hash_table ltab; /* labels hash table */
114 static union label *ldata; /* all label data blocks */
115 static union label *lfree; /* labels free block */
116 static struct permts *perm_head; /* start of perm. text storage */
117 static struct permts *perm_tail; /* end of perm. text storage */
119 static void init_block(union label *blk);
120 static char *perm_copy(const char *string);
122 static char *prevlabel;
124 static bool initialized = false;
126 char lprefix[PREFIX_MAX] = { 0 };
127 char lpostfix[PREFIX_MAX] = { 0 };
130 * Emit a symdef to the output and the debug format backends.
132 static void out_symdef(char *name, int32_t segment, int64_t offset,
133 int is_global, char *special)
135 ofmt->symdef(name, segment, offset, is_global, special);
138 * NASM special symbols are not passed to the debug format; none
139 * of the current backends want to see them.
141 if (!(name[0] == '.' && name[1] == '.' && name[2] != '@'))
142 dfmt->debug_deflabel(name, segment, offset, is_global, special);
146 * Internal routine: finds the `union label' corresponding to the
147 * given label name. Creates a new one, if it isn't found, and if
148 * `create' is true.
150 static union label *find_label(const char *label, int create, int *created)
152 char *prev;
153 int prevlen, len;
154 union label *lptr, **lpp;
155 char label_str[IDLEN_MAX];
156 struct hash_insert ip;
158 if (islocal(label)) {
159 prev = prevlabel;
160 prevlen = strlen(prev);
161 len = strlen(label);
162 if (prevlen + len >= IDLEN_MAX) {
163 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
164 IDLEN_MAX);
165 return NULL;
167 memcpy(label_str, prev, prevlen);
168 memcpy(label_str+prevlen, label, len+1);
169 label = label_str;
170 } else {
171 prev = "";
172 prevlen = 0;
175 lpp = (union label **) hash_find(&ltab, label, &ip);
176 lptr = lpp ? *lpp : NULL;
178 if (lptr || !create) {
179 if (created)
180 *created = 0;
181 return lptr;
184 /* Create a new label... */
185 if (lfree->admin.movingon == END_BLOCK) {
187 * must allocate a new block
189 lfree->admin.next = nasm_malloc(LBLK_SIZE);
190 lfree = lfree->admin.next;
191 init_block(lfree);
194 if (created)
195 *created = 1;
197 lfree->admin.movingon = BOGUS_VALUE;
198 lfree->defn.label = perm_copy(label);
199 lfree->defn.special = NULL;
200 lfree->defn.is_global = NOT_DEFINED_YET;
202 hash_add(&ip, lfree->defn.label, lfree);
203 return lfree++;
206 bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
208 union label *lptr;
210 if (!initialized)
211 return false;
213 lptr = find_label(label, 0, NULL);
214 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
215 *segment = lptr->defn.segment;
216 *offset = lptr->defn.offset;
217 return true;
220 return false;
223 bool is_extern(const char *label)
225 union label *lptr;
227 if (!initialized)
228 return false;
230 lptr = find_label(label, 0, NULL);
231 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
234 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
235 bool is_norm, bool isextrn)
237 union label *lptr;
238 int exi, created;
240 /* This routine possibly ought to check for phase errors. Most assemblers
241 * check for phase errors at this point. I don't know whether phase errors
242 * are even possible, nor whether they are checked somewhere else
245 (void)special; /* Don't warn that this parameter is unused */
246 (void)is_norm; /* Don't warn that this parameter is unused */
247 (void)isextrn; /* Don't warn that this parameter is unused */
249 #ifdef DEBUG
250 #if DEBUG < 3
251 if (!strncmp(label, "debugdump", 9))
252 #endif
253 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
254 label, segment, offset, special, is_norm, isextrn);
255 #endif
257 lptr = find_label(label, 1, &created);
258 if (!lptr)
259 nasm_panic(0, "can't find label `%s' on pass two", label);
261 if (created)
262 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
264 if (!islocal(label)) {
265 if (!islocalchar(*label) && lptr->defn.is_norm)
266 prevlabel = lptr->defn.label;
269 if (lptr->defn.offset != offset)
270 global_offset_changed++;
272 lptr->defn.offset = offset;
273 lptr->defn.segment = segment;
275 if (pass0 == 1) {
276 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
277 if (exi) {
278 char *xsymbol;
279 int slen;
280 slen = strlen(lprefix);
281 slen += strlen(lptr->defn.label);
282 slen += strlen(lpostfix);
283 slen++; /* room for that null char */
284 xsymbol = nasm_malloc(slen);
285 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
286 lpostfix);
288 out_symdef(xsymbol, segment, offset, exi,
289 special ? special : lptr->defn.special);
290 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
291 } else {
292 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
293 out_symdef(lptr->defn.label, segment, offset, exi,
294 special ? special : lptr->defn.special);
297 } /* if (pass0 == 1) */
300 void define_label(char *label, int32_t segment, int64_t offset, char *special,
301 bool is_norm, bool isextrn)
303 union label *lptr;
304 int exi;
306 #ifdef DEBUG
307 #if DEBUG<3
308 if (!strncmp(label, "debugdump", 9))
309 #endif
310 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
311 label, segment, offset, special, is_norm, isextrn);
312 #endif
313 lptr = find_label(label, 1, NULL);
314 if (!lptr)
315 return;
316 if (lptr->defn.is_global & DEFINED_BIT) {
317 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
318 return;
320 lptr->defn.is_global |= DEFINED_BIT;
321 if (isextrn)
322 lptr->defn.is_global |= EXTERN_BIT;
324 if (!islocalchar(label[0]) && is_norm) {
325 /* not local, but not special either */
326 prevlabel = lptr->defn.label;
327 } else if (islocal(label) && !*prevlabel) {
328 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
329 " non-local labels");
332 lptr->defn.segment = segment;
333 lptr->defn.offset = offset;
334 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
336 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
337 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
338 if (exi) {
339 char *xsymbol;
340 int slen;
341 slen = strlen(lprefix);
342 slen += strlen(lptr->defn.label);
343 slen += strlen(lpostfix);
344 slen++; /* room for that null char */
345 xsymbol = nasm_malloc(slen);
346 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
347 lpostfix);
349 out_symdef(xsymbol, segment, offset, exi,
350 special ? special : lptr->defn.special);
351 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
352 } else {
353 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
354 out_symdef(lptr->defn.label, segment, offset, exi,
355 special ? special : lptr->defn.special);
358 } /* if (pass0 == 1) */
361 void define_common(char *label, int32_t segment, int32_t size, char *special)
363 union label *lptr;
365 lptr = find_label(label, 1, NULL);
366 if (!lptr)
367 return;
368 if ((lptr->defn.is_global & DEFINED_BIT) &&
369 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
370 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
371 return;
373 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
375 if (!islocalchar(label[0])) {
376 prevlabel = lptr->defn.label;
377 } else {
378 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
379 "common variable");
380 return;
383 lptr->defn.segment = segment;
384 lptr->defn.offset = 0;
386 if (pass0 == 0)
387 return;
389 out_symdef(lptr->defn.label, segment, size, 2,
390 special ? special : lptr->defn.special);
393 void declare_as_global(char *label, char *special)
395 union label *lptr;
397 if (islocal(label)) {
398 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
399 " global", label);
400 return;
402 lptr = find_label(label, 1, NULL);
403 if (!lptr)
404 return;
405 switch (lptr->defn.is_global & TYPE_MASK) {
406 case NOT_DEFINED_YET:
407 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
408 lptr->defn.special = special ? perm_copy(special) : NULL;
409 break;
410 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
411 case GLOBAL_SYMBOL:
412 break;
413 case LOCAL_SYMBOL:
414 if (!(lptr->defn.is_global & EXTERN_BIT)) {
415 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
416 "after symbol definition is an experimental feature", label);
417 lptr->defn.is_global = GLOBAL_SYMBOL;
419 break;
423 int init_labels(void)
425 hash_init(&ltab, HASH_LARGE);
427 ldata = lfree = nasm_malloc(LBLK_SIZE);
428 init_block(lfree);
430 perm_head = perm_tail =
431 nasm_malloc(sizeof(struct permts));
433 perm_head->next = NULL;
434 perm_head->size = PERMTS_SIZE;
435 perm_head->usage = 0;
437 prevlabel = "";
439 initialized = true;
441 return 0;
444 void cleanup_labels(void)
446 union label *lptr, *lhold;
448 initialized = false;
450 hash_free(&ltab);
452 lptr = lhold = ldata;
453 while (lptr) {
454 lptr = &lptr[LABEL_BLOCK-1];
455 lptr = lptr->admin.next;
456 nasm_free(lhold);
457 lhold = lptr;
460 while (perm_head) {
461 perm_tail = perm_head;
462 perm_head = perm_head->next;
463 nasm_free(perm_tail);
467 static void init_block(union label *blk)
469 int j;
471 for (j = 0; j < LABEL_BLOCK - 1; j++)
472 blk[j].admin.movingon = END_LIST;
473 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
474 blk[LABEL_BLOCK - 1].admin.next = NULL;
477 static char *perm_copy(const char *string)
479 char *p;
480 int len = strlen(string)+1;
482 nasm_assert(len <= PERMTS_SIZE);
484 if (perm_tail->size - perm_tail->usage < len) {
485 perm_tail->next =
486 nasm_malloc(sizeof(struct permts));
487 perm_tail = perm_tail->next;
488 perm_tail->next = NULL;
489 perm_tail->size = PERMTS_SIZE;
490 perm_tail->usage = 0;
492 p = perm_tail->data + perm_tail->usage;
493 memcpy(p, string, len);
494 perm_tail->usage += len;
496 return p;
499 char *local_scope(char *label)
501 return islocal(label) ? prevlabel : "";
505 * Notes regarding bug involving redefinition of external segments.
507 * Up to and including v0.97, the following code didn't work. From 0.97
508 * developers release 2 onwards, it will generate an error.
510 * EXTERN extlabel
511 * newlabel EQU extlabel + 1
513 * The results of allowing this code through are that two import records
514 * are generated, one for 'extlabel' and one for 'newlabel'.
516 * The reason for this is an inadequacy in the defined interface between
517 * the label manager and the output formats. The problem lies in how the
518 * output format driver tells that a label is an external label for which
519 * a label import record must be produced. Most (all except bin?) produce
520 * the record if the segment number of the label is not one of the internal
521 * segments that the output driver is producing.
523 * A simple fix to this would be to make the output formats keep track of
524 * which symbols they've produced import records for, and make them not
525 * produce import records for segments that are already defined.
527 * The best way, which is slightly harder but reduces duplication of code
528 * and should therefore make the entire system smaller and more stable is
529 * to change the interface between assembler, define_label(), and
530 * the output module. The changes that are needed are:
532 * The semantics of the 'isextern' flag passed to define_label() need
533 * examining. This information may or may not tell us what we need to
534 * know (ie should we be generating an import record at this point for this
535 * label). If these aren't the semantics, the semantics should be changed
536 * to this.
538 * The output module interface needs changing, so that the `isextern' flag
539 * is passed to the module, so that it can be easily tested for.