blob: 8c2223a0087460b1ed2298a41b8ab5e0985cd95c [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +04002 *
H. Peter Anvin9c595b62017-03-07 19:44:21 -08003 * Copyright 1996-2017 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 Anvinb20bc732017-03-07 19:23:03 -080046#include "error.h"
H. Peter Anvin6244f4b2007-09-14 18:03:29 -070047#include "hashtbl.h"
H. Peter Anvinc6645832014-11-25 12:07:10 -080048#include "labels.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049
50/*
51 * A local label is one that begins with exactly one period. Things
52 * that begin with _two_ periods are NASM-specific things.
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000053 *
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.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057 */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040058#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) == '.'))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040067#define LABEL_BLOCK 128 /* no. of labels/block */
68#define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040070#define END_LIST -3 /* don't clash with NO_SEG! */
71#define END_BLOCK -2
72#define BOGUS_VALUE -4
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040074#define PERMTS_SIZE 16384 /* size of text blocks */
H. Peter Anvin565be912009-07-05 22:15:57 -070075#if (PERMTS_SIZE < IDLEN_MAX)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040076 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
Ed Berosetc06f6df2003-09-08 00:30:40 +000077#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078
79/* values for label.defn.is_global */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040080#define DEFINED_BIT 1
81#define GLOBAL_BIT 2
82#define EXTERN_BIT 4
83#define COMMON_BIT 8
H. Peter Anvin76690a12002-04-30 20:52:49 +000084
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040085#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)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000090
H. Peter Anvine2c80182005-01-15 22:15:51 +000091union label { /* actual label structures */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000092 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -080093 int32_t segment;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040094 int64_t offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000095 char *label, *special;
H. Peter Anvinaf535c12002-04-30 20:59:21 +000096 int is_global, is_norm;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000097 } defn;
98 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -080099 int32_t movingon;
100 int64_t dummy;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000101 union label *next;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000102 } admin;
103};
104
H. Peter Anvine2c80182005-01-15 22:15:51 +0000105struct permts { /* permanent text storage */
106 struct permts *next; /* for the linked list */
107 int size, usage; /* size and used space in ... */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000108 char data[PERMTS_SIZE]; /* ... the data block itself */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000109};
110
H. Peter Anvin9c595b62017-03-07 19:44:21 -0800111uint64_t global_offset_changed; /* counter for global offset changes */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000112
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400113static struct hash_table ltab; /* labels hash table */
114static union label *ldata; /* all label data blocks */
115static union label *lfree; /* labels free block */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116static struct permts *perm_head; /* start of perm. text storage */
117static struct permts *perm_tail; /* end of perm. text storage */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000118
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119static void init_block(union label *blk);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700120static char *perm_copy(const char *string);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000122static char *prevlabel;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123
H. Peter Anvin70055962007-10-11 00:05:31 -0700124static bool initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000125
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000126char lprefix[PREFIX_MAX] = { 0 };
127char lpostfix[PREFIX_MAX] = { 0 };
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000128
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129/*
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800130 * Emit a symdef to the output and the debug format backends.
131 */
132static void out_symdef(char *name, int32_t segment, int64_t offset,
133 int is_global, char *special)
134{
135 ofmt->symdef(name, segment, offset, is_global, special);
136
137 /*
138 * NASM special symbols are not passed to the debug format; none
139 * of the current backends want to see them.
140 */
141 if (!(name[0] == '.' && name[1] == '.' && name[2] != '@'))
142 dfmt->debug_deflabel(name, segment, offset, is_global, special);
143}
144
145/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000146 * Internal routine: finds the `union label' corresponding to the
147 * given label name. Creates a new one, if it isn't found, and if
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700148 * `create' is true.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000149 */
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700150static union label *find_label(const char *label, int create, int *created)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000151{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700152 char *prev;
153 int prevlen, len;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700154 union label *lptr, **lpp;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700155 char label_str[IDLEN_MAX];
156 struct hash_insert ip;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000157
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700158 if (islocal(label)) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000159 prev = prevlabel;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400160 prevlen = strlen(prev);
161 len = strlen(label);
162 if (prevlen + len >= IDLEN_MAX) {
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300163 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
164 IDLEN_MAX);
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400165 return NULL;
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300166 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400167 memcpy(label_str, prev, prevlen);
168 memcpy(label_str+prevlen, label, len+1);
169 label = label_str;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700170 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000171 prev = "";
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400172 prevlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000173 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174
H. Peter Anvin166c2472008-05-28 12:28:58 -0700175 lpp = (union label **) hash_find(&ltab, label, &ip);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700176 lptr = lpp ? *lpp : NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700177
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300178 if (lptr || !create) {
179 if (created)
180 *created = 0;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400181 return lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300182 }
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700183
184 /* Create a new label... */
185 if (lfree->admin.movingon == END_BLOCK) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400186 /*
187 * must allocate a new block
188 */
189 lfree->admin.next = (union label *)nasm_malloc(LBLK_SIZE);
190 lfree = lfree->admin.next;
191 init_block(lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700192 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700193
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300194 if (created)
195 *created = 1;
196
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700197 lfree->admin.movingon = BOGUS_VALUE;
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700198 lfree->defn.label = perm_copy(label);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700199 lfree->defn.special = NULL;
200 lfree->defn.is_global = NOT_DEFINED_YET;
H. Peter Anvin70653092007-10-19 14:42:29 -0700201
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700202 hash_add(&ip, lfree->defn.label, lfree);
203 return lfree++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000204}
205
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700206bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000207{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208 union label *lptr;
209
Keith Kaniosb7a89542007-04-12 02:40:54 +0000210 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700211 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000212
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300213 lptr = find_label(label, 0, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000214 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000215 *segment = lptr->defn.segment;
216 *offset = lptr->defn.offset;
H. Peter Anvin70055962007-10-11 00:05:31 -0700217 return true;
Cyrill Gorcunovd7a64b72010-04-20 15:26:08 +0400218 }
219
220 return false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221}
222
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700223bool is_extern(const char *label)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000224{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000225 union label *lptr;
226
Keith Kaniosb7a89542007-04-12 02:40:54 +0000227 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700228 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000229
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300230 lptr = find_label(label, 0, NULL);
H. Peter Anvin70055962007-10-11 00:05:31 -0700231 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000232}
233
Charles Crayne4e8563d2007-11-05 17:19:32 -0800234void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700235 bool is_norm, bool isextrn)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000236{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237 union label *lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300238 int exi, created;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000239
H. Peter Anvineba20a72002-04-30 20:53:55 +0000240 /* 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
243 */
244
H. Peter Anvine2c80182005-01-15 22:15:51 +0000245 (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 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000248
249#ifdef DEBUG
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400250#if DEBUG < 3
H. Peter Anvineba20a72002-04-30 20:53:55 +0000251 if (!strncmp(label, "debugdump", 9))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000252#endif
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200253 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254 label, segment, offset, special, is_norm, isextrn);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000255#endif
256
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300257 lptr = find_label(label, 1, &created);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000258 if (!lptr)
H. Peter Anvin41087062016-03-03 14:27:34 -0800259 nasm_panic(0, "can't find label `%s' on pass two", label);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000260
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300261 if (created)
262 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
263
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000264 if (!islocal(label)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 if (!islocalchar(*label) && lptr->defn.is_norm)
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000266 prevlabel = lptr->defn.label;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000268
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700269 if (lptr->defn.offset != offset)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400270 global_offset_changed++;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700271
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000272 lptr->defn.offset = offset;
Charles Craynecd341802008-06-04 15:53:21 -0700273 lptr->defn.segment = segment;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000274
H. Peter Anvine2c80182005-01-15 22:15:51 +0000275 if (pass0 == 1) {
276 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
277 if (exi) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000278 char *xsymbol;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000279 int slen;
280 slen = strlen(lprefix);
281 slen += strlen(lptr->defn.label);
282 slen += strlen(lpostfix);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000283 slen++; /* room for that null char */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284 xsymbol = nasm_malloc(slen);
285 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
286 lpostfix);
287
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800288 out_symdef(xsymbol, segment, offset, exi,
289 special ? special : lptr->defn.special);
290 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291 } else {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400292 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800293 out_symdef(lptr->defn.label, segment, offset, exi,
294 special ? special : lptr->defn.special);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295 }
296 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400297 } /* if (pass0 == 1) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000298}
299
Charles Crayne4e8563d2007-11-05 17:19:32 -0800300void define_label(char *label, int32_t segment, int64_t offset, char *special,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700301 bool is_norm, bool isextrn)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000302{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000303 union label *lptr;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304 int exi;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000305
H. Peter Anvineba20a72002-04-30 20:53:55 +0000306#ifdef DEBUG
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000307#if DEBUG<3
H. Peter Anvineba20a72002-04-30 20:53:55 +0000308 if (!strncmp(label, "debugdump", 9))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000309#endif
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200310 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311 label, segment, offset, special, is_norm, isextrn);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000312#endif
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300313 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300314 if (!lptr)
315 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000316 if (lptr->defn.is_global & DEFINED_BIT) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700317 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000318 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000319 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000320 lptr->defn.is_global |= DEFINED_BIT;
321 if (isextrn)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 lptr->defn.is_global |= EXTERN_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000323
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700324 if (!islocalchar(label[0]) && is_norm) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400325 /* not local, but not special either */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 prevlabel = lptr->defn.label;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700327 } else if (islocal(label) && !*prevlabel) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700328 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 " non-local labels");
330 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000331
332 lptr->defn.segment = segment;
333 lptr->defn.offset = offset;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000334 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335
Charles Crayne82e94992008-03-03 14:43:55 -0800336 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000337 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
338 if (exi) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000339 char *xsymbol;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000340 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);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000348
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800349 out_symdef(xsymbol, segment, offset, exi,
350 special ? special : lptr->defn.special);
351 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 } else {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400353 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800354 out_symdef(lptr->defn.label, segment, offset, exi,
355 special ? special : lptr->defn.special);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356 }
357 }
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400358 } /* if (pass0 == 1) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359}
360
H. Peter Anvin605f5152009-07-18 18:31:41 -0700361void define_common(char *label, int32_t segment, int32_t size, char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000362{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000363 union label *lptr;
364
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300365 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300366 if (!lptr)
367 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700368 if ((lptr->defn.is_global & DEFINED_BIT) &&
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400369 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
370 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
371 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000372 }
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700373 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000374
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700375 if (!islocalchar(label[0])) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000376 prevlabel = lptr->defn.label;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700377 } else {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700378 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000379 "common variable");
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400380 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700381 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000382
383 lptr->defn.segment = segment;
384 lptr->defn.offset = 0;
385
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700386 if (pass0 == 0)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400387 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700388
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800389 out_symdef(lptr->defn.label, segment, size, 2,
390 special ? special : lptr->defn.special);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391}
392
H. Peter Anvin605f5152009-07-18 18:31:41 -0700393void declare_as_global(char *label, char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000394{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000395 union label *lptr;
396
397 if (islocal(label)) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700398 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000399 " global", label);
400 return;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401 }
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300402 lptr = find_label(label, 1, NULL);
Cyrill Gorcunov7bb0e522010-02-18 19:06:14 +0300403 if (!lptr)
404 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000405 switch (lptr->defn.is_global & TYPE_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 case NOT_DEFINED_YET:
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000407 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700408 lptr->defn.special = special ? perm_copy(special) : NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000409 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
411 case GLOBAL_SYMBOL:
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000412 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 case LOCAL_SYMBOL:
Charles Crayne18152f02009-01-28 19:07:18 -0800414 if (!(lptr->defn.is_global & EXTERN_BIT)) {
H. Peter Anvin605f5152009-07-18 18:31:41 -0700415 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
Charles Crayne18152f02009-01-28 19:07:18 -0800416 "after symbol definition is an experimental feature", label);
417 lptr->defn.is_global = GLOBAL_SYMBOL;
418 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000419 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000420 }
421}
422
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000424{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700425 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700427 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
428 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000429
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400430 perm_head = perm_tail =
431 (struct permts *)nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000432
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433 perm_head->next = NULL;
434 perm_head->size = PERMTS_SIZE;
435 perm_head->usage = 0;
436
437 prevlabel = "";
438
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700439 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000441 return 0;
442}
443
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000445{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700446 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000447
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700448 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449
H. Peter Anvin166c2472008-05-28 12:28:58 -0700450 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000451
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700452 lptr = lhold = ldata;
453 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400454 lptr = &lptr[LABEL_BLOCK-1];
455 lptr = lptr->admin.next;
456 nasm_free(lhold);
457 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000458 }
459
460 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000461 perm_tail = perm_head;
462 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000464 }
465}
466
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000469 int j;
470
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 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;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000475}
476
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700477static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700479 char *p;
480 int len = strlen(string)+1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000481
H. Peter Anvin565be912009-07-05 22:15:57 -0700482 nasm_assert(len <= PERMTS_SIZE);
483
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000484 if (perm_tail->size - perm_tail->usage < len) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 perm_tail->next =
486 (struct permts *)nasm_malloc(sizeof(struct permts));
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000487 perm_tail = perm_tail->next;
488 perm_tail->next = NULL;
489 perm_tail->size = PERMTS_SIZE;
490 perm_tail->usage = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491 }
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700492 p = perm_tail->data + perm_tail->usage;
493 memcpy(p, string, len);
494 perm_tail->usage += len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
496 return p;
497}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000498
Charles Crayned60059e2008-03-12 22:39:03 -0700499char *local_scope(char *label)
500{
501 return islocal(label) ? prevlabel : "";
502}
503
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504/*
505 * Notes regarding bug involving redefinition of external segments.
506 *
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.
509 *
510 * EXTERN extlabel
511 * newlabel EQU extlabel + 1
512 *
513 * The results of allowing this code through are that two import records
514 * are generated, one for 'extlabel' and one for 'newlabel'.
515 *
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.
522 *
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.
526 *
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:
531 *
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.
537 *
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.
540 */