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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * labels.c label handling for the Netwide Assembler
62 * A local label is one that begins with exactly one period. Things
63 * that begin with _two_ periods are NASM-specific things.
65 * If TASM compatibility is enabled, a local label can also begin with
66 * @@, so @@local is a TASM compatible local label. Note that we only
67 * check for the first @ symbol, although TASM requires both.
70 (tasm_compatible_mode ? \
71 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
72 ((l)[0] == '.' && (l)[1] != '.'))
73 #define islocalchar(c) \
74 (tasm_compatible_mode ? \
75 ((c) == '.' || (c) == '@') : \
78 #define LABEL_BLOCK 128 /* no. of labels/block */
79 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
81 #define END_LIST -3 /* don't clash with NO_SEG! */
83 #define BOGUS_VALUE -4
85 #define PERMTS_SIZE 4096 /* size of text blocks */
86 #if (PERMTS_SIZE > IDLEN_MAX)
87 #error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
90 /* values for label.defn.is_global */
96 #define NOT_DEFINED_YET 0
98 #define LOCAL_SYMBOL (DEFINED_BIT)
99 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
100 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
102 union label
{ /* actual label structures */
106 char *label
, *special
;
107 int is_global
, is_norm
;
116 struct permts
{ /* permanent text storage */
117 struct permts
*next
; /* for the linked list */
118 int size
, usage
; /* size and used space in ... */
119 char data
[PERMTS_SIZE
]; /* ... the data block itself */
122 extern int64_t global_offset_changed
; /* defined in nasm.c */
124 static struct hash_table ltab
; /* labels hash table */
125 static union label
*ldata
; /* all label data blocks */
126 static union label
*lfree
; /* labels free block */
127 static struct permts
*perm_head
; /* start of perm. text storage */
128 static struct permts
*perm_tail
; /* end of perm. text storage */
130 static void init_block(union label
*blk
);
131 static char *perm_copy(const char *string
);
133 static char *prevlabel
;
135 static bool initialized
= false;
137 char lprefix
[PREFIX_MAX
] = { 0 };
138 char lpostfix
[PREFIX_MAX
] = { 0 };
141 * Internal routine: finds the `union label' corresponding to the
142 * given label name. Creates a new one, if it isn't found, and if
145 static union label
*find_label(char *label
, int create
)
149 union label
*lptr
, **lpp
;
150 char label_str
[IDLEN_MAX
];
151 struct hash_insert ip
;
153 if (islocal(label
)) {
155 prevlen
= strlen(prev
);
157 if (prevlen
+len
>= IDLEN_MAX
)
158 return NULL
; /* Error... */
159 memcpy(label_str
, prev
, prevlen
);
160 memcpy(label_str
+prevlen
, label
, len
+1);
167 lpp
= (union label
**) hash_find(<ab
, label
, &ip
);
168 lptr
= lpp
? *lpp
: NULL
;
173 /* Create a new label... */
174 if (lfree
->admin
.movingon
== END_BLOCK
) {
176 * must allocate a new block
179 (union label
*)nasm_malloc(LBLK_SIZE
);
180 lfree
= lfree
->admin
.next
;
184 lfree
->admin
.movingon
= BOGUS_VALUE
;
185 lfree
->defn
.label
= perm_copy(label
);
186 lfree
->defn
.special
= NULL
;
187 lfree
->defn
.is_global
= NOT_DEFINED_YET
;
189 hash_add(&ip
, lfree
->defn
.label
, lfree
);
193 bool lookup_label(char *label
, int32_t *segment
, int64_t *offset
)
200 lptr
= find_label(label
, 0);
201 if (lptr
&& (lptr
->defn
.is_global
& DEFINED_BIT
)) {
202 *segment
= lptr
->defn
.segment
;
203 *offset
= lptr
->defn
.offset
;
209 bool is_extern(char *label
)
216 lptr
= find_label(label
, 0);
217 return (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
));
220 void redefine_label(char *label
, int32_t segment
, int64_t offset
, char *special
,
221 bool is_norm
, bool isextrn
, struct ofmt
*ofmt
,
227 /* This routine possibly ought to check for phase errors. Most assemblers
228 * check for phase errors at this point. I don't know whether phase errors
229 * are even possible, nor whether they are checked somewhere else
232 (void)special
; /* Don't warn that this parameter is unused */
233 (void)is_norm
; /* Don't warn that this parameter is unused */
234 (void)isextrn
; /* Don't warn that this parameter is unused */
235 (void)ofmt
; /* Don't warn that this parameter is unused */
239 if (!strncmp(label
, "debugdump", 9))
241 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
242 label
, segment
, offset
, special
, is_norm
, isextrn
);
245 lptr
= find_label(label
, 1);
247 error(ERR_PANIC
, "can't find label `%s' on pass two", label
);
249 if (!islocal(label
)) {
250 if (!islocalchar(*label
) && lptr
->defn
.is_norm
)
251 prevlabel
= lptr
->defn
.label
;
254 if (lptr
->defn
.offset
!= offset
)
255 global_offset_changed
++;
257 lptr
->defn
.offset
= offset
;
258 lptr
->defn
.segment
= segment
;
261 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
265 slen
= strlen(lprefix
);
266 slen
+= strlen(lptr
->defn
.label
);
267 slen
+= strlen(lpostfix
);
268 slen
++; /* room for that null char */
269 xsymbol
= nasm_malloc(slen
);
270 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
273 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
274 special
? special
: lptr
->defn
.special
);
275 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
277 special
? special
: lptr
->
279 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
281 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
283 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
284 special
? special
: lptr
->defn
.special
);
285 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
292 /* if (pass0 == 1) */
295 void define_label(char *label
, int32_t segment
, int64_t offset
, char *special
,
296 bool is_norm
, bool isextrn
, struct ofmt
*ofmt
, efunc error
)
303 if (!strncmp(label
, "debugdump", 9))
305 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
306 label
, segment
, offset
, special
, is_norm
, isextrn
);
308 lptr
= find_label(label
, 1);
309 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
310 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
313 lptr
->defn
.is_global
|= DEFINED_BIT
;
315 lptr
->defn
.is_global
|= EXTERN_BIT
;
317 if (!islocalchar(label
[0]) && is_norm
) {
318 /* not local, but not special either */
319 prevlabel
= lptr
->defn
.label
;
320 } else if (islocal(label
) && !*prevlabel
) {
321 error(ERR_NONFATAL
, "attempt to define a local label before any"
322 " non-local labels");
325 lptr
->defn
.segment
= segment
;
326 lptr
->defn
.offset
= offset
;
327 lptr
->defn
.is_norm
= (!islocalchar(label
[0]) && is_norm
);
329 if (pass0
== 1 || (!is_norm
&& !isextrn
&& (segment
> 0) && (segment
& 1))) {
330 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
334 slen
= strlen(lprefix
);
335 slen
+= strlen(lptr
->defn
.label
);
336 slen
+= strlen(lpostfix
);
337 slen
++; /* room for that null char */
338 xsymbol
= nasm_malloc(slen
);
339 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
342 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
343 special
? special
: lptr
->defn
.special
);
344 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
346 special
? special
: lptr
->
348 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
350 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
352 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
353 special
? special
: lptr
->defn
.special
);
354 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
360 } /* if (pass0 == 1) */
363 void define_common(char *label
, int32_t segment
, int32_t size
, char *special
,
364 struct ofmt
*ofmt
, efunc error
)
368 lptr
= find_label(label
, 1);
369 if ((lptr
->defn
.is_global
& DEFINED_BIT
) &&
370 (passn
== 1 || !(lptr
->defn
.is_global
& COMMON_BIT
))) {
371 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
374 lptr
->defn
.is_global
|= DEFINED_BIT
|COMMON_BIT
;
376 if (!islocalchar(label
[0])) {
377 prevlabel
= lptr
->defn
.label
;
379 error(ERR_NONFATAL
, "attempt to define a local label as a "
384 lptr
->defn
.segment
= segment
;
385 lptr
->defn
.offset
= 0;
390 ofmt
->symdef(lptr
->defn
.label
, segment
, size
, 2,
391 special
? special
: lptr
->defn
.special
);
392 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
393 special
? special
: lptr
->defn
.
397 void declare_as_global(char *label
, char *special
, efunc error
)
401 if (islocal(label
)) {
402 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
406 lptr
= find_label(label
, 1);
407 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
408 case NOT_DEFINED_YET
:
409 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
410 lptr
->defn
.special
= special
? perm_copy(special
) : NULL
;
412 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
416 if (!(lptr
->defn
.is_global
& EXTERN_BIT
)) {
417 error(ERR_WARNING
, "symbol `%s': GLOBAL directive "
418 "after symbol definition is an experimental feature", label
);
419 lptr
->defn
.is_global
= GLOBAL_SYMBOL
;
425 int init_labels(void)
427 hash_init(<ab
, HASH_LARGE
);
429 ldata
= lfree
= (union label
*)nasm_malloc(LBLK_SIZE
);
433 perm_tail
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
435 perm_head
->next
= NULL
;
436 perm_head
->size
= PERMTS_SIZE
;
437 perm_head
->usage
= 0;
446 void cleanup_labels(void)
448 union label
*lptr
, *lhold
;
454 lptr
= lhold
= ldata
;
456 lptr
= &lptr
[LABEL_BLOCK
-1];
457 lptr
= lptr
->admin
.next
;
463 perm_tail
= perm_head
;
464 perm_head
= perm_head
->next
;
465 nasm_free(perm_tail
);
469 static void init_block(union label
*blk
)
473 for (j
= 0; j
< LABEL_BLOCK
- 1; j
++)
474 blk
[j
].admin
.movingon
= END_LIST
;
475 blk
[LABEL_BLOCK
- 1].admin
.movingon
= END_BLOCK
;
476 blk
[LABEL_BLOCK
- 1].admin
.next
= NULL
;
479 static char *perm_copy(const char *string
)
482 int len
= strlen(string
)+1;
484 if (perm_tail
->size
- perm_tail
->usage
< len
) {
486 (struct permts
*)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
;
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.
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
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.