preproc.c: Fix NULL deref on token pasting
[nasm.git] / labels.c
blob32937b3fb8f037c9d3d970d9bdc0a6edb841c062
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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>
43 #include <inttypes.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "hashtbl.h"
50 * A local label is one that begins with exactly one period. Things
51 * that begin with _two_ periods are NASM-specific things.
53 * If TASM compatibility is enabled, a local label can also begin with
54 * @@, so @@local is a TASM compatible local label. Note that we only
55 * check for the first @ symbol, although TASM requires both.
57 #define islocal(l) \
58 (tasm_compatible_mode ? \
59 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
60 ((l)[0] == '.' && (l)[1] != '.'))
61 #define islocalchar(c) \
62 (tasm_compatible_mode ? \
63 ((c) == '.' || (c) == '@') : \
64 ((c) == '.'))
66 #define LABEL_BLOCK 128 /* no. of labels/block */
67 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
69 #define END_LIST -3 /* don't clash with NO_SEG! */
70 #define END_BLOCK -2
71 #define BOGUS_VALUE -4
73 #define PERMTS_SIZE 16384 /* size of text blocks */
74 #if (PERMTS_SIZE < IDLEN_MAX)
75 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
76 #endif
78 /* values for label.defn.is_global */
79 #define DEFINED_BIT 1
80 #define GLOBAL_BIT 2
81 #define EXTERN_BIT 4
82 #define COMMON_BIT 8
84 #define NOT_DEFINED_YET 0
85 #define TYPE_MASK 3
86 #define LOCAL_SYMBOL (DEFINED_BIT)
87 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
88 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
90 union label { /* actual label structures */
91 struct {
92 int32_t segment;
93 int64_t offset;
94 char *label, *special;
95 int is_global, is_norm;
96 } defn;
97 struct {
98 int32_t movingon;
99 int64_t dummy;
100 union label *next;
101 } admin;
104 struct permts { /* permanent text storage */
105 struct permts *next; /* for the linked list */
106 int size, usage; /* size and used space in ... */
107 char data[PERMTS_SIZE]; /* ... the data block itself */
110 extern int64_t global_offset_changed; /* defined in nasm.c */
112 static struct hash_table ltab; /* labels hash table */
113 static union label *ldata; /* all label data blocks */
114 static union label *lfree; /* labels free block */
115 static struct permts *perm_head; /* start of perm. text storage */
116 static struct permts *perm_tail; /* end of perm. text storage */
118 static void init_block(union label *blk);
119 static char *perm_copy(const char *string);
121 static char *prevlabel;
123 static bool initialized = false;
125 char lprefix[PREFIX_MAX] = { 0 };
126 char lpostfix[PREFIX_MAX] = { 0 };
129 * Internal routine: finds the `union label' corresponding to the
130 * given label name. Creates a new one, if it isn't found, and if
131 * `create' is true.
133 static union label *find_label(char *label, int create)
135 char *prev;
136 int prevlen, len;
137 union label *lptr, **lpp;
138 char label_str[IDLEN_MAX];
139 struct hash_insert ip;
141 if (islocal(label)) {
142 prev = prevlabel;
143 prevlen = strlen(prev);
144 len = strlen(label);
145 if (prevlen+len >= IDLEN_MAX) {
146 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
147 IDLEN_MAX);
148 return NULL; /* Error... */
150 memcpy(label_str, prev, prevlen);
151 memcpy(label_str+prevlen, label, len+1);
152 label = label_str;
153 } else {
154 prev = "";
155 prevlen = 0;
158 lpp = (union label **) hash_find(&ltab, label, &ip);
159 lptr = lpp ? *lpp : NULL;
161 if (lptr || !create)
162 return lptr;
164 /* Create a new label... */
165 if (lfree->admin.movingon == END_BLOCK) {
167 * must allocate a new block
169 lfree->admin.next =
170 (union label *)nasm_malloc(LBLK_SIZE);
171 lfree = lfree->admin.next;
172 init_block(lfree);
175 lfree->admin.movingon = BOGUS_VALUE;
176 lfree->defn.label = perm_copy(label);
177 lfree->defn.special = NULL;
178 lfree->defn.is_global = NOT_DEFINED_YET;
180 hash_add(&ip, lfree->defn.label, lfree);
181 return lfree++;
184 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
186 union label *lptr;
188 if (!initialized)
189 return false;
191 lptr = find_label(label, 0);
192 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
193 *segment = lptr->defn.segment;
194 *offset = lptr->defn.offset;
195 return true;
196 } else
197 return false;
200 bool is_extern(char *label)
202 union label *lptr;
204 if (!initialized)
205 return false;
207 lptr = find_label(label, 0);
208 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
211 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
212 bool is_norm, bool isextrn)
214 union label *lptr;
215 int exi;
217 /* This routine possibly ought to check for phase errors. Most assemblers
218 * check for phase errors at this point. I don't know whether phase errors
219 * are even possible, nor whether they are checked somewhere else
222 (void)special; /* Don't warn that this parameter is unused */
223 (void)is_norm; /* Don't warn that this parameter is unused */
224 (void)isextrn; /* Don't warn that this parameter is unused */
226 #ifdef DEBUG
227 #if DEBUG<3
228 if (!strncmp(label, "debugdump", 9))
229 #endif
230 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
231 label, segment, offset, special, is_norm, isextrn);
232 #endif
234 lptr = find_label(label, 1);
235 if (!lptr)
236 nasm_error(ERR_PANIC, "can't find label `%s' on pass two", label);
238 if (!islocal(label)) {
239 if (!islocalchar(*label) && lptr->defn.is_norm)
240 prevlabel = lptr->defn.label;
243 if (lptr->defn.offset != offset)
244 global_offset_changed++;
246 lptr->defn.offset = offset;
247 lptr->defn.segment = segment;
249 if (pass0 == 1) {
250 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
251 if (exi) {
252 char *xsymbol;
253 int slen;
254 slen = strlen(lprefix);
255 slen += strlen(lptr->defn.label);
256 slen += strlen(lpostfix);
257 slen++; /* room for that null char */
258 xsymbol = nasm_malloc(slen);
259 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
260 lpostfix);
262 ofmt->symdef(xsymbol, segment, offset, exi,
263 special ? special : lptr->defn.special);
264 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
265 exi,
266 special ? special : lptr->
267 defn.special);
268 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
269 } else {
270 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
271 EXTERN_BIT) {
272 ofmt->symdef(lptr->defn.label, segment, offset, exi,
273 special ? special : lptr->defn.special);
274 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
275 exi,
276 special ? special :
277 lptr->defn.special);
281 /* if (pass0 == 1) */
284 void define_label(char *label, int32_t segment, int64_t offset, char *special,
285 bool is_norm, bool isextrn)
287 union label *lptr;
288 int exi;
290 #ifdef DEBUG
291 #if DEBUG<3
292 if (!strncmp(label, "debugdump", 9))
293 #endif
294 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
295 label, segment, offset, special, is_norm, isextrn);
296 #endif
297 lptr = find_label(label, 1);
298 if (!lptr)
299 return;
300 if (lptr->defn.is_global & DEFINED_BIT) {
301 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
302 return;
304 lptr->defn.is_global |= DEFINED_BIT;
305 if (isextrn)
306 lptr->defn.is_global |= EXTERN_BIT;
308 if (!islocalchar(label[0]) && is_norm) {
309 /* not local, but not special either */
310 prevlabel = lptr->defn.label;
311 } else if (islocal(label) && !*prevlabel) {
312 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
313 " non-local labels");
316 lptr->defn.segment = segment;
317 lptr->defn.offset = offset;
318 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
320 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
321 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
322 if (exi) {
323 char *xsymbol;
324 int slen;
325 slen = strlen(lprefix);
326 slen += strlen(lptr->defn.label);
327 slen += strlen(lpostfix);
328 slen++; /* room for that null char */
329 xsymbol = nasm_malloc(slen);
330 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
331 lpostfix);
333 ofmt->symdef(xsymbol, segment, offset, exi,
334 special ? special : lptr->defn.special);
335 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
336 exi,
337 special ? special : lptr->
338 defn.special);
339 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
340 } else {
341 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
342 EXTERN_BIT) {
343 ofmt->symdef(lptr->defn.label, segment, offset, exi,
344 special ? special : lptr->defn.special);
345 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
346 exi,
347 special ? special :
348 lptr->defn.special);
351 } /* if (pass0 == 1) */
354 void define_common(char *label, int32_t segment, int32_t size, char *special)
356 union label *lptr;
358 lptr = find_label(label, 1);
359 if (!lptr)
360 return;
361 if ((lptr->defn.is_global & DEFINED_BIT) &&
362 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
363 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
364 return;
366 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
368 if (!islocalchar(label[0])) {
369 prevlabel = lptr->defn.label;
370 } else {
371 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
372 "common variable");
373 return;
376 lptr->defn.segment = segment;
377 lptr->defn.offset = 0;
379 if (pass0 == 0)
380 return;
382 ofmt->symdef(lptr->defn.label, segment, size, 2,
383 special ? special : lptr->defn.special);
384 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
385 special ? special : lptr->defn.
386 special);
389 void declare_as_global(char *label, char *special)
391 union label *lptr;
393 if (islocal(label)) {
394 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
395 " global", label);
396 return;
398 lptr = find_label(label, 1);
399 if (!lptr)
400 return;
401 switch (lptr->defn.is_global & TYPE_MASK) {
402 case NOT_DEFINED_YET:
403 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
404 lptr->defn.special = special ? perm_copy(special) : NULL;
405 break;
406 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
407 case GLOBAL_SYMBOL:
408 break;
409 case LOCAL_SYMBOL:
410 if (!(lptr->defn.is_global & EXTERN_BIT)) {
411 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
412 "after symbol definition is an experimental feature", label);
413 lptr->defn.is_global = GLOBAL_SYMBOL;
415 break;
419 int init_labels(void)
421 hash_init(&ltab, HASH_LARGE);
423 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
424 init_block(lfree);
426 perm_head =
427 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
429 perm_head->next = NULL;
430 perm_head->size = PERMTS_SIZE;
431 perm_head->usage = 0;
433 prevlabel = "";
435 initialized = true;
437 return 0;
440 void cleanup_labels(void)
442 union label *lptr, *lhold;
444 initialized = false;
446 hash_free(&ltab);
448 lptr = lhold = ldata;
449 while (lptr) {
450 lptr = &lptr[LABEL_BLOCK-1];
451 lptr = lptr->admin.next;
452 nasm_free(lhold);
453 lhold = lptr;
456 while (perm_head) {
457 perm_tail = perm_head;
458 perm_head = perm_head->next;
459 nasm_free(perm_tail);
463 static void init_block(union label *blk)
465 int j;
467 for (j = 0; j < LABEL_BLOCK - 1; j++)
468 blk[j].admin.movingon = END_LIST;
469 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
470 blk[LABEL_BLOCK - 1].admin.next = NULL;
473 static char *perm_copy(const char *string)
475 char *p;
476 int len = strlen(string)+1;
478 nasm_assert(len <= PERMTS_SIZE);
480 if (perm_tail->size - perm_tail->usage < len) {
481 perm_tail->next =
482 (struct permts *)nasm_malloc(sizeof(struct permts));
483 perm_tail = perm_tail->next;
484 perm_tail->next = NULL;
485 perm_tail->size = PERMTS_SIZE;
486 perm_tail->usage = 0;
488 p = perm_tail->data + perm_tail->usage;
489 memcpy(p, string, len);
490 perm_tail->usage += len;
492 return p;
495 char *local_scope(char *label)
497 return islocal(label) ? prevlabel : "";
501 * Notes regarding bug involving redefinition of external segments.
503 * Up to and including v0.97, the following code didn't work. From 0.97
504 * developers release 2 onwards, it will generate an error.
506 * EXTERN extlabel
507 * newlabel EQU extlabel + 1
509 * The results of allowing this code through are that two import records
510 * are generated, one for 'extlabel' and one for 'newlabel'.
512 * The reason for this is an inadequacy in the defined interface between
513 * the label manager and the output formats. The problem lies in how the
514 * output format driver tells that a label is an external label for which
515 * a label import record must be produced. Most (all except bin?) produce
516 * the record if the segment number of the label is not one of the internal
517 * segments that the output driver is producing.
519 * A simple fix to this would be to make the output formats keep track of
520 * which symbols they've produced import records for, and make them not
521 * produce import records for segments that are already defined.
523 * The best way, which is slightly harder but reduces duplication of code
524 * and should therefore make the entire system smaller and more stable is
525 * to change the interface between assembler, define_label(), and
526 * the output module. The changes that are needed are:
528 * The semantics of the 'isextern' flag passed to define_label() need
529 * examining. This information may or may not tell us what we need to
530 * know (ie should we be generating an import record at this point for this
531 * label). If these aren't the semantics, the semantics should be changed
532 * to this.
534 * The output module interface needs changing, so that the `isextern' flag
535 * is passed to the module, so that it can be easily tested for.