blob: 224d695958c409a377922f2944c3b821509d9936 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +04002 *
H. Peter Anvinfc0ff222016-03-07 21:46:04 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * labels.c label handling for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <string.h>
42#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000043
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044#include "nasm.h"
45#include "nasmlib.h"
H. Peter Anvin6244f4b2007-09-14 18:03:29 -070046#include "hashtbl.h"
H. Peter Anvinc6645832014-11-25 12:07:10 -080047#include "labels.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49/*
50 * A local label is one that begins with exactly one period. Things
51 * that begin with _two_ periods are NASM-specific things.
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000052 *
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.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056 */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040057#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) == '.'))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040066#define LABEL_BLOCK 128 /* no. of labels/block */
67#define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040069#define END_LIST -3 /* don't clash with NO_SEG! */
70#define END_BLOCK -2
71#define BOGUS_VALUE -4
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040073#define PERMTS_SIZE 16384 /* size of text blocks */
H. Peter Anvin565be912009-07-05 22:15:57 -070074#if (PERMTS_SIZE < IDLEN_MAX)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040075 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
Ed Berosetc06f6df2003-09-08 00:30:40 +000076#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077
78/* values for label.defn.is_global */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040079#define DEFINED_BIT 1
80#define GLOBAL_BIT 2
81#define EXTERN_BIT 4
82#define COMMON_BIT 8
H. Peter Anvin76690a12002-04-30 20:52:49 +000083
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040084#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)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000089
H. Peter Anvine2c80182005-01-15 22:15:51 +000090union label { /* actual label structures */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000091 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -080092 int32_t segment;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040093 int64_t offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000094 char *label, *special;
H. Peter Anvinaf535c12002-04-30 20:59:21 +000095 int is_global, is_norm;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096 } defn;
97 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -080098 int32_t movingon;
99 int64_t dummy;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000100 union label *next;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000101 } admin;
102};
103
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104struct permts { /* permanent text storage */
105 struct permts *next; /* for the linked list */
106 int size, usage; /* size and used space in ... */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000107 char data[PERMTS_SIZE]; /* ... the data block itself */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108};
109
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400110extern int64_t global_offset_changed; /* defined in nasm.c */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000111
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400112static struct hash_table ltab; /* labels hash table */
113static union label *ldata; /* all label data blocks */
114static union label *lfree; /* labels free block */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115static struct permts *perm_head; /* start of perm. text storage */
116static struct permts *perm_tail; /* end of perm. text storage */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118static void init_block(union label *blk);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700119static char *perm_copy(const char *string);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121static char *prevlabel;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122
H. Peter Anvin70055962007-10-11 00:05:31 -0700123static bool initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000124
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000125char lprefix[PREFIX_MAX] = { 0 };
126char lpostfix[PREFIX_MAX] = { 0 };
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000127
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128/*
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800129 * Emit a symdef to the output and the debug format backends.
130 */
131static void out_symdef(char *name, int32_t segment, int64_t offset,
132 int is_global, char *special)
133{
134 ofmt->symdef(name, segment, offset, is_global, special);
135
136 /*
137 * NASM special symbols are not passed to the debug format; none
138 * of the current backends want to see them.
139 */
140 if (!(name[0] == '.' && name[1] == '.' && name[2] != '@'))
141 dfmt->debug_deflabel(name, segment, offset, is_global, special);
142}
143
144/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145 * Internal routine: finds the `union label' corresponding to the
146 * given label name. Creates a new one, if it isn't found, and if
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700147 * `create' is true.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000148 */
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300149static union label *find_label(char *label, int create, int *created)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000150{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700151 char *prev;
152 int prevlen, len;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700153 union label *lptr, **lpp;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700154 char label_str[IDLEN_MAX];
155 struct hash_insert ip;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000156
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700157 if (islocal(label)) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000158 prev = prevlabel;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400159 prevlen = strlen(prev);
160 len = strlen(label);
161 if (prevlen + len >= IDLEN_MAX) {
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300162 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
163 IDLEN_MAX);
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400164 return NULL;
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300165 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400166 memcpy(label_str, prev, prevlen);
167 memcpy(label_str+prevlen, label, len+1);
168 label = label_str;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700169 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000170 prev = "";
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400171 prevlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000172 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000173
H. Peter Anvin166c2472008-05-28 12:28:58 -0700174 lpp = (union label **) hash_find(&ltab, label, &ip);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700175 lptr = lpp ? *lpp : NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700176
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300177 if (lptr || !create) {
178 if (created)
179 *created = 0;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400180 return lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300181 }
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700182
183 /* Create a new label... */
184 if (lfree->admin.movingon == END_BLOCK) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400185 /*
186 * must allocate a new block
187 */
188 lfree->admin.next = (union label *)nasm_malloc(LBLK_SIZE);
189 lfree = lfree->admin.next;
190 init_block(lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700191 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700192
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300193 if (created)
194 *created = 1;
195
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700196 lfree->admin.movingon = BOGUS_VALUE;
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700197 lfree->defn.label = perm_copy(label);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700198 lfree->defn.special = NULL;
199 lfree->defn.is_global = NOT_DEFINED_YET;
H. Peter Anvin70653092007-10-19 14:42:29 -0700200
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700201 hash_add(&ip, lfree->defn.label, lfree);
202 return lfree++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000203}
204
Charles Crayne4e8563d2007-11-05 17:19:32 -0800205bool lookup_label(char *label, int32_t *segment, int64_t *offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000206{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000207 union label *lptr;
208
Keith Kaniosb7a89542007-04-12 02:40:54 +0000209 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700210 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000211
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300212 lptr = find_label(label, 0, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000213 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000214 *segment = lptr->defn.segment;
215 *offset = lptr->defn.offset;
H. Peter Anvin70055962007-10-11 00:05:31 -0700216 return true;
Cyrill Gorcunovd7a64b72010-04-20 15:26:08 +0400217 }
218
219 return false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220}
221
H. Peter Anvin70055962007-10-11 00:05:31 -0700222bool is_extern(char *label)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000223{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000224 union label *lptr;
225
Keith Kaniosb7a89542007-04-12 02:40:54 +0000226 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700227 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000228
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300229 lptr = find_label(label, 0, NULL);
H. Peter Anvin70055962007-10-11 00:05:31 -0700230 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000231}
232
Charles Crayne4e8563d2007-11-05 17:19:32 -0800233void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700234 bool is_norm, bool isextrn)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000235{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000236 union label *lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300237 int exi, created;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238
H. Peter Anvineba20a72002-04-30 20:53:55 +0000239 /* This routine possibly ought to check for phase errors. Most assemblers
240 * check for phase errors at this point. I don't know whether phase errors
241 * are even possible, nor whether they are checked somewhere else
242 */
243
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244 (void)special; /* Don't warn that this parameter is unused */
245 (void)is_norm; /* Don't warn that this parameter is unused */
246 (void)isextrn; /* Don't warn that this parameter is unused */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000247
248#ifdef DEBUG
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400249#if DEBUG < 3
H. Peter Anvineba20a72002-04-30 20:53:55 +0000250 if (!strncmp(label, "debugdump", 9))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000251#endif
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200252 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 label, segment, offset, special, is_norm, isextrn);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254#endif
255
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300256 lptr = find_label(label, 1, &created);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000257 if (!lptr)
H. Peter Anvin41087062016-03-03 14:27:34 -0800258 nasm_panic(0, "can't find label `%s' on pass two", label);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300260 if (created)
261 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
262
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000263 if (!islocal(label)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000264 if (!islocalchar(*label) && lptr->defn.is_norm)
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000265 prevlabel = lptr->defn.label;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000266 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000267
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700268 if (lptr->defn.offset != offset)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400269 global_offset_changed++;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700270
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000271 lptr->defn.offset = offset;
Charles Craynecd341802008-06-04 15:53:21 -0700272 lptr->defn.segment = segment;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000273
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274 if (pass0 == 1) {
275 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
276 if (exi) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000277 char *xsymbol;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000278 int slen;
279 slen = strlen(lprefix);
280 slen += strlen(lptr->defn.label);
281 slen += strlen(lpostfix);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000282 slen++; /* room for that null char */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283 xsymbol = nasm_malloc(slen);
284 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
285 lpostfix);
286
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800287 out_symdef(xsymbol, segment, offset, exi,
288 special ? special : lptr->defn.special);
289 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290 } else {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400291 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800292 out_symdef(lptr->defn.label, segment, offset, exi,
293 special ? special : lptr->defn.special);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000294 }
295 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400296 } /* if (pass0 == 1) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000297}
298
Charles Crayne4e8563d2007-11-05 17:19:32 -0800299void define_label(char *label, int32_t segment, int64_t offset, char *special,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700300 bool is_norm, bool isextrn)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000301{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000302 union label *lptr;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000303 int exi;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000304
H. Peter Anvineba20a72002-04-30 20:53:55 +0000305#ifdef DEBUG
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000306#if DEBUG<3
H. Peter Anvineba20a72002-04-30 20:53:55 +0000307 if (!strncmp(label, "debugdump", 9))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000308#endif
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200309 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310 label, segment, offset, special, is_norm, isextrn);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000311#endif
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300312 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300313 if (!lptr)
314 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000315 if (lptr->defn.is_global & DEFINED_BIT) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700316 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000317 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000318 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000319 lptr->defn.is_global |= DEFINED_BIT;
320 if (isextrn)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321 lptr->defn.is_global |= EXTERN_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000322
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700323 if (!islocalchar(label[0]) && is_norm) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400324 /* not local, but not special either */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325 prevlabel = lptr->defn.label;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700326 } else if (islocal(label) && !*prevlabel) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700327 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 " non-local labels");
329 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000330
331 lptr->defn.segment = segment;
332 lptr->defn.offset = offset;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000333 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000334
Charles Crayne82e94992008-03-03 14:43:55 -0800335 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
337 if (exi) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000338 char *xsymbol;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000339 int slen;
340 slen = strlen(lprefix);
341 slen += strlen(lptr->defn.label);
342 slen += strlen(lpostfix);
343 slen++; /* room for that null char */
344 xsymbol = nasm_malloc(slen);
345 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
346 lpostfix);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000347
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800348 out_symdef(xsymbol, segment, offset, exi,
349 special ? special : lptr->defn.special);
350 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 } else {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400352 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800353 out_symdef(lptr->defn.label, segment, offset, exi,
354 special ? special : lptr->defn.special);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 }
356 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400357 } /* if (pass0 == 1) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000358}
359
H. Peter Anvin605f5152009-07-18 18:31:41 -0700360void define_common(char *label, int32_t segment, int32_t size, char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000361{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362 union label *lptr;
363
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300364 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300365 if (!lptr)
366 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700367 if ((lptr->defn.is_global & DEFINED_BIT) &&
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400368 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
369 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
370 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000371 }
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700372 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000373
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700374 if (!islocalchar(label[0])) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000375 prevlabel = lptr->defn.label;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700376 } else {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700377 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000378 "common variable");
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400379 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700380 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000381
382 lptr->defn.segment = segment;
383 lptr->defn.offset = 0;
384
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700385 if (pass0 == 0)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400386 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700387
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800388 out_symdef(lptr->defn.label, segment, size, 2,
389 special ? special : lptr->defn.special);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000390}
391
H. Peter Anvin605f5152009-07-18 18:31:41 -0700392void declare_as_global(char *label, char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000394 union label *lptr;
395
396 if (islocal(label)) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700397 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000398 " global", label);
399 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000400 }
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300401 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300402 if (!lptr)
403 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000404 switch (lptr->defn.is_global & TYPE_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 case NOT_DEFINED_YET:
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000406 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700407 lptr->defn.special = special ? perm_copy(special) : NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000408 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
410 case GLOBAL_SYMBOL:
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000411 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 case LOCAL_SYMBOL:
Charles Crayne18152f02009-01-28 19:07:18 -0800413 if (!(lptr->defn.is_global & EXTERN_BIT)) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700414 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
Charles Crayne18152f02009-01-28 19:07:18 -0800415 "after symbol definition is an experimental feature", label);
416 lptr->defn.is_global = GLOBAL_SYMBOL;
417 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000418 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419 }
420}
421
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000423{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700424 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700426 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
427 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000428
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400429 perm_head = perm_tail =
430 (struct permts *)nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000431
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000432 perm_head->next = NULL;
433 perm_head->size = PERMTS_SIZE;
434 perm_head->usage = 0;
435
436 prevlabel = "";
437
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700438 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000440 return 0;
441}
442
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000444{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700445 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000446
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700447 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000448
H. Peter Anvin166c2472008-05-28 12:28:58 -0700449 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000450
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700451 lptr = lhold = ldata;
452 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400453 lptr = &lptr[LABEL_BLOCK-1];
454 lptr = lptr->admin.next;
455 nasm_free(lhold);
456 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000457 }
458
459 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000460 perm_tail = perm_head;
461 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463 }
464}
465
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000467{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000468 int j;
469
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 for (j = 0; j < LABEL_BLOCK - 1; j++)
471 blk[j].admin.movingon = END_LIST;
472 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
473 blk[LABEL_BLOCK - 1].admin.next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000474}
475
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700476static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700478 char *p;
479 int len = strlen(string)+1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000480
H. Peter Anvin565be912009-07-05 22:15:57 -0700481 nasm_assert(len <= PERMTS_SIZE);
482
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483 if (perm_tail->size - perm_tail->usage < len) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 perm_tail->next =
485 (struct permts *)nasm_malloc(sizeof(struct permts));
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000486 perm_tail = perm_tail->next;
487 perm_tail->next = NULL;
488 perm_tail->size = PERMTS_SIZE;
489 perm_tail->usage = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490 }
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700491 p = perm_tail->data + perm_tail->usage;
492 memcpy(p, string, len);
493 perm_tail->usage += len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000494
495 return p;
496}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497
Charles Crayned60059e2008-03-12 22:39:03 -0700498char *local_scope(char *label)
499{
500 return islocal(label) ? prevlabel : "";
501}
502
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503/*
504 * Notes regarding bug involving redefinition of external segments.
505 *
506 * Up to and including v0.97, the following code didn't work. From 0.97
507 * developers release 2 onwards, it will generate an error.
508 *
509 * EXTERN extlabel
510 * newlabel EQU extlabel + 1
511 *
512 * The results of allowing this code through are that two import records
513 * are generated, one for 'extlabel' and one for 'newlabel'.
514 *
515 * The reason for this is an inadequacy in the defined interface between
516 * the label manager and the output formats. The problem lies in how the
517 * output format driver tells that a label is an external label for which
518 * a label import record must be produced. Most (all except bin?) produce
519 * the record if the segment number of the label is not one of the internal
520 * segments that the output driver is producing.
521 *
522 * A simple fix to this would be to make the output formats keep track of
523 * which symbols they've produced import records for, and make them not
524 * produce import records for segments that are already defined.
525 *
526 * The best way, which is slightly harder but reduces duplication of code
527 * and should therefore make the entire system smaller and more stable is
528 * to change the interface between assembler, define_label(), and
529 * the output module. The changes that are needed are:
530 *
531 * The semantics of the 'isextern' flag passed to define_label() need
532 * examining. This information may or may not tell us what we need to
533 * know (ie should we be generating an import record at this point for this
534 * label). If these aren't the semantics, the semantics should be changed
535 * to this.
536 *
537 * The output module interface needs changing, so that the `isextern' flag
538 * is passed to the module, so that it can be easily tested for.
539 */