1 /* ----------------------------------------------------------------------- *
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
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
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.
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) == '@') : \
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! */
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"
78 /* values for label.defn.is_global */
84 #define NOT_DEFINED_YET 0
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 */
94 char *label
, *special
;
95 int is_global
, is_norm
;
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
133 static union label
*find_label(char *label
, int create
)
137 union label
*lptr
, **lpp
;
138 char label_str
[IDLEN_MAX
];
139 struct hash_insert ip
;
141 if (islocal(label
)) {
143 prevlen
= strlen(prev
);
145 if (prevlen
+len
>= IDLEN_MAX
)
146 return NULL
; /* Error... */
147 memcpy(label_str
, prev
, prevlen
);
148 memcpy(label_str
+prevlen
, label
, len
+1);
155 lpp
= (union label
**) hash_find(<ab
, label
, &ip
);
156 lptr
= lpp
? *lpp
: NULL
;
161 /* Create a new label... */
162 if (lfree
->admin
.movingon
== END_BLOCK
) {
164 * must allocate a new block
167 (union label
*)nasm_malloc(LBLK_SIZE
);
168 lfree
= lfree
->admin
.next
;
172 lfree
->admin
.movingon
= BOGUS_VALUE
;
173 lfree
->defn
.label
= perm_copy(label
);
174 lfree
->defn
.special
= NULL
;
175 lfree
->defn
.is_global
= NOT_DEFINED_YET
;
177 hash_add(&ip
, lfree
->defn
.label
, lfree
);
181 bool lookup_label(char *label
, int32_t *segment
, int64_t *offset
)
188 lptr
= find_label(label
, 0);
189 if (lptr
&& (lptr
->defn
.is_global
& DEFINED_BIT
)) {
190 *segment
= lptr
->defn
.segment
;
191 *offset
= lptr
->defn
.offset
;
197 bool is_extern(char *label
)
204 lptr
= find_label(label
, 0);
205 return (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
));
208 void redefine_label(char *label
, int32_t segment
, int64_t offset
, char *special
,
209 bool is_norm
, bool isextrn
)
214 /* This routine possibly ought to check for phase errors. Most assemblers
215 * check for phase errors at this point. I don't know whether phase errors
216 * are even possible, nor whether they are checked somewhere else
219 (void)special
; /* Don't warn that this parameter is unused */
220 (void)is_norm
; /* Don't warn that this parameter is unused */
221 (void)isextrn
; /* Don't warn that this parameter is unused */
225 if (!strncmp(label
, "debugdump", 9))
227 nasm_error(ERR_DEBUG
, "redefine_label (%s, %"PRIx32
", %"PRIx64
", %s, %d, %d)",
228 label
, segment
, offset
, special
, is_norm
, isextrn
);
231 lptr
= find_label(label
, 1);
233 nasm_error(ERR_PANIC
, "can't find label `%s' on pass two", label
);
235 if (!islocal(label
)) {
236 if (!islocalchar(*label
) && lptr
->defn
.is_norm
)
237 prevlabel
= lptr
->defn
.label
;
240 if (lptr
->defn
.offset
!= offset
)
241 global_offset_changed
++;
243 lptr
->defn
.offset
= offset
;
244 lptr
->defn
.segment
= segment
;
247 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
251 slen
= strlen(lprefix
);
252 slen
+= strlen(lptr
->defn
.label
);
253 slen
+= strlen(lpostfix
);
254 slen
++; /* room for that null char */
255 xsymbol
= nasm_malloc(slen
);
256 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
259 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
260 special
? special
: lptr
->defn
.special
);
261 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
263 special
? special
: lptr
->
265 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
267 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
269 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
270 special
? special
: lptr
->defn
.special
);
271 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
278 /* if (pass0 == 1) */
281 void define_label(char *label
, int32_t segment
, int64_t offset
, char *special
,
282 bool is_norm
, bool isextrn
)
289 if (!strncmp(label
, "debugdump", 9))
291 nasm_error(ERR_DEBUG
, "define_label (%s, %"PRIx32
", %"PRIx64
", %s, %d, %d)",
292 label
, segment
, offset
, special
, is_norm
, isextrn
);
294 lptr
= find_label(label
, 1);
295 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
296 nasm_error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
299 lptr
->defn
.is_global
|= DEFINED_BIT
;
301 lptr
->defn
.is_global
|= EXTERN_BIT
;
303 if (!islocalchar(label
[0]) && is_norm
) {
304 /* not local, but not special either */
305 prevlabel
= lptr
->defn
.label
;
306 } else if (islocal(label
) && !*prevlabel
) {
307 nasm_error(ERR_NONFATAL
, "attempt to define a local label before any"
308 " non-local labels");
311 lptr
->defn
.segment
= segment
;
312 lptr
->defn
.offset
= offset
;
313 lptr
->defn
.is_norm
= (!islocalchar(label
[0]) && is_norm
);
315 if (pass0
== 1 || (!is_norm
&& !isextrn
&& (segment
> 0) && (segment
& 1))) {
316 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
320 slen
= strlen(lprefix
);
321 slen
+= strlen(lptr
->defn
.label
);
322 slen
+= strlen(lpostfix
);
323 slen
++; /* room for that null char */
324 xsymbol
= nasm_malloc(slen
);
325 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
328 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
329 special
? special
: lptr
->defn
.special
);
330 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
332 special
? special
: lptr
->
334 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
336 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
338 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
339 special
? special
: lptr
->defn
.special
);
340 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
346 } /* if (pass0 == 1) */
349 void define_common(char *label
, int32_t segment
, int32_t size
, char *special
)
353 lptr
= find_label(label
, 1);
354 if ((lptr
->defn
.is_global
& DEFINED_BIT
) &&
355 (passn
== 1 || !(lptr
->defn
.is_global
& COMMON_BIT
))) {
356 nasm_error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
359 lptr
->defn
.is_global
|= DEFINED_BIT
|COMMON_BIT
;
361 if (!islocalchar(label
[0])) {
362 prevlabel
= lptr
->defn
.label
;
364 nasm_error(ERR_NONFATAL
, "attempt to define a local label as a "
369 lptr
->defn
.segment
= segment
;
370 lptr
->defn
.offset
= 0;
375 ofmt
->symdef(lptr
->defn
.label
, segment
, size
, 2,
376 special
? special
: lptr
->defn
.special
);
377 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
378 special
? special
: lptr
->defn
.
382 void declare_as_global(char *label
, char *special
)
386 if (islocal(label
)) {
387 nasm_error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
391 lptr
= find_label(label
, 1);
392 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
393 case NOT_DEFINED_YET
:
394 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
395 lptr
->defn
.special
= special
? perm_copy(special
) : NULL
;
397 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
401 if (!(lptr
->defn
.is_global
& EXTERN_BIT
)) {
402 nasm_error(ERR_WARNING
, "symbol `%s': GLOBAL directive "
403 "after symbol definition is an experimental feature", label
);
404 lptr
->defn
.is_global
= GLOBAL_SYMBOL
;
410 int init_labels(void)
412 hash_init(<ab
, HASH_LARGE
);
414 ldata
= lfree
= (union label
*)nasm_malloc(LBLK_SIZE
);
418 perm_tail
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
420 perm_head
->next
= NULL
;
421 perm_head
->size
= PERMTS_SIZE
;
422 perm_head
->usage
= 0;
431 void cleanup_labels(void)
433 union label
*lptr
, *lhold
;
439 lptr
= lhold
= ldata
;
441 lptr
= &lptr
[LABEL_BLOCK
-1];
442 lptr
= lptr
->admin
.next
;
448 perm_tail
= perm_head
;
449 perm_head
= perm_head
->next
;
450 nasm_free(perm_tail
);
454 static void init_block(union label
*blk
)
458 for (j
= 0; j
< LABEL_BLOCK
- 1; j
++)
459 blk
[j
].admin
.movingon
= END_LIST
;
460 blk
[LABEL_BLOCK
- 1].admin
.movingon
= END_BLOCK
;
461 blk
[LABEL_BLOCK
- 1].admin
.next
= NULL
;
464 static char *perm_copy(const char *string
)
467 int len
= strlen(string
)+1;
469 nasm_assert(len
<= PERMTS_SIZE
);
471 if (perm_tail
->size
- perm_tail
->usage
< len
) {
473 (struct permts
*)nasm_malloc(sizeof(struct permts
));
474 perm_tail
= perm_tail
->next
;
475 perm_tail
->next
= NULL
;
476 perm_tail
->size
= PERMTS_SIZE
;
477 perm_tail
->usage
= 0;
479 p
= perm_tail
->data
+ perm_tail
->usage
;
480 memcpy(p
, string
, len
);
481 perm_tail
->usage
+= len
;
486 char *local_scope(char *label
)
488 return islocal(label
) ? prevlabel
: "";
492 * Notes regarding bug involving redefinition of external segments.
494 * Up to and including v0.97, the following code didn't work. From 0.97
495 * developers release 2 onwards, it will generate an error.
498 * newlabel EQU extlabel + 1
500 * The results of allowing this code through are that two import records
501 * are generated, one for 'extlabel' and one for 'newlabel'.
503 * The reason for this is an inadequacy in the defined interface between
504 * the label manager and the output formats. The problem lies in how the
505 * output format driver tells that a label is an external label for which
506 * a label import record must be produced. Most (all except bin?) produce
507 * the record if the segment number of the label is not one of the internal
508 * segments that the output driver is producing.
510 * A simple fix to this would be to make the output formats keep track of
511 * which symbols they've produced import records for, and make them not
512 * produce import records for segments that are already defined.
514 * The best way, which is slightly harder but reduces duplication of code
515 * and should therefore make the entire system smaller and more stable is
516 * to change the interface between assembler, define_label(), and
517 * the output module. The changes that are needed are:
519 * The semantics of the 'isextern' flag passed to define_label() need
520 * examining. This information may or may not tell us what we need to
521 * know (ie should we be generating an import record at this point for this
522 * label). If these aren't the semantics, the semantics should be changed
525 * The output module interface needs changing, so that the `isextern' flag
526 * is passed to the module, so that it can be easily tested for.