blob: eec5a84490032ef3a7432d005dc8d0332064be88 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +04002 *
H. Peter Anvin892c4812018-05-30 14:43:46 -07003 * Copyright 1996-2018 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/*
H. Peter Anvin98578072018-06-01 18:02:54 -070051 * A dot-local label is one that begins with exactly one period. Things
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052 * 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
H. Peter Anvin98578072018-06-01 18:02:54 -070055 * @@.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056 */
H. Peter Anvin98578072018-06-01 18:02:54 -070057static bool islocal(const char *l)
58{
59 if (tasm_compatible_mode) {
60 if (l[0] == '@' && l[1] == '@')
61 return true;
62 }
63
64 return (l[0] == '.' && l[1] != '.');
65}
66
67/*
68 * Return true if this falls into NASM's '..' namespace
69 */
70static bool ismagic(const char *l)
71{
72 return l[0] == '.' && l[1] == '.' && l[2] != '@';
73}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000074
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040075#define LABEL_BLOCK 128 /* no. of labels/block */
76#define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040078#define END_LIST -3 /* don't clash with NO_SEG! */
79#define END_BLOCK -2
80#define BOGUS_VALUE -4
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000081
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040082#define PERMTS_SIZE 16384 /* size of text blocks */
H. Peter Anvin565be912009-07-05 22:15:57 -070083#if (PERMTS_SIZE < IDLEN_MAX)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040084 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
Ed Berosetc06f6df2003-09-08 00:30:40 +000085#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin98578072018-06-01 18:02:54 -070087/* string values for enum label_type */
88static const char * const types[] =
89{"local", "global", "static", "extern", "common", "special",
90 "output format special"};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000091
H. Peter Anvine2c80182005-01-15 22:15:51 +000092union label { /* actual label structures */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000093 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -080094 int32_t segment;
H. Peter Anvin29695c82018-06-14 17:04:32 -070095 int32_t subsection; /* Available for ofmt->herelabel() */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040096 int64_t offset;
H. Peter Anvin73482482018-06-11 14:54:14 -070097 int64_t size;
H. Peter Anvin98578072018-06-01 18:02:54 -070098 char *label, *mangled, *special;
99 enum label_type type, mangled_type;
100 bool defined;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000101 } defn;
102 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -0800103 int32_t movingon;
104 int64_t dummy;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000105 union label *next;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000106 } admin;
107};
108
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109struct permts { /* permanent text storage */
110 struct permts *next; /* for the linked list */
H. Peter Anvin98578072018-06-01 18:02:54 -0700111 unsigned int size, usage; /* size and used space in ... */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000112 char data[PERMTS_SIZE]; /* ... the data block itself */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113};
H. Peter Anvin98578072018-06-01 18:02:54 -0700114#define PERMTS_HEADER offsetof(struct permts, data)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115
H. Peter Anvin9c595b62017-03-07 19:44:21 -0800116uint64_t global_offset_changed; /* counter for global offset changes */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000117
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400118static struct hash_table ltab; /* labels hash table */
119static union label *ldata; /* all label data blocks */
120static union label *lfree; /* labels free block */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000121static struct permts *perm_head; /* start of perm. text storage */
122static struct permts *perm_tail; /* end of perm. text storage */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124static void init_block(union label *blk);
H. Peter Anvin98578072018-06-01 18:02:54 -0700125static char *perm_alloc(size_t len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700126static char *perm_copy(const char *string);
H. Peter Anvin98578072018-06-01 18:02:54 -0700127static char *perm_copy3(const char *s1, const char *s2, const char *s3);
128static const char *mangle_label_name(union label *lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129
H. Peter Anvin98578072018-06-01 18:02:54 -0700130static const char *prevlabel;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131
H. Peter Anvin70055962007-10-11 00:05:31 -0700132static bool initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000133
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134/*
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800135 * Emit a symdef to the output and the debug format backends.
136 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700137static void out_symdef(union label *lptr)
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800138{
H. Peter Anvin98578072018-06-01 18:02:54 -0700139 int backend_type;
H. Peter Anvin73482482018-06-11 14:54:14 -0700140 int64_t backend_offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700141
142 /* Backend-defined special segments are passed to symdef immediately */
143 if (pass0 == 2) {
144 /* Emit special fixups for globals and commons */
145 switch (lptr->defn.type) {
146 case LBL_GLOBAL:
H. Peter Anvin98578072018-06-01 18:02:54 -0700147 case LBL_EXTERN:
H. Peter Anvin73482482018-06-11 14:54:14 -0700148 case LBL_COMMON:
H. Peter Anvin98578072018-06-01 18:02:54 -0700149 if (lptr->defn.special)
150 ofmt->symdef(lptr->defn.label, 0, 0, 3, lptr->defn.special);
151 break;
152 default:
153 break;
154 }
155 return;
156 }
157
158 if (pass0 != 1 && lptr->defn.type != LBL_BACKEND)
159 return;
160
161 /* Clean up this hack... */
162 switch(lptr->defn.type) {
163 case LBL_GLOBAL:
164 backend_type = 1;
H. Peter Anvin73482482018-06-11 14:54:14 -0700165 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700166 break;
167 case LBL_COMMON:
168 backend_type = 2;
H. Peter Anvin73482482018-06-11 14:54:14 -0700169 backend_offset = lptr->defn.size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700170 break;
171 default:
172 backend_type = 0;
H. Peter Anvin73482482018-06-11 14:54:14 -0700173 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700174 break;
175 }
176
177 /* Might be necessary for a backend symbol */
178 mangle_label_name(lptr);
179
180 ofmt->symdef(lptr->defn.mangled, lptr->defn.segment,
H. Peter Anvin73482482018-06-11 14:54:14 -0700181 backend_offset, backend_type,
H. Peter Anvin98578072018-06-01 18:02:54 -0700182 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800183
184 /*
185 * NASM special symbols are not passed to the debug format; none
186 * of the current backends want to see them.
187 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700188 if (lptr->defn.type == LBL_SPECIAL || lptr->defn.type == LBL_BACKEND)
189 return;
190
191 dfmt->debug_deflabel(lptr->defn.mangled, lptr->defn.segment,
192 lptr->defn.offset, backend_type,
193 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800194}
195
196/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000197 * Internal routine: finds the `union label' corresponding to the
198 * given label name. Creates a new one, if it isn't found, and if
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700199 * `create' is true.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000200 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700201static union label *find_label(const char *label, bool create, bool *created)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000202{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700203 union label *lptr, **lpp;
H. Peter Anvin98578072018-06-01 18:02:54 -0700204 char *label_str = NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700205 struct hash_insert ip;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206
H. Peter Anvin98578072018-06-01 18:02:54 -0700207 if (islocal(label))
208 label = label_str = nasm_strcat(prevlabel, label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000209
H. Peter Anvin166c2472008-05-28 12:28:58 -0700210 lpp = (union label **) hash_find(&ltab, label, &ip);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700211 lptr = lpp ? *lpp : NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700212
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300213 if (lptr || !create) {
214 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700215 *created = false;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400216 return lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300217 }
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700218
219 /* Create a new label... */
220 if (lfree->admin.movingon == END_BLOCK) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400221 /*
222 * must allocate a new block
223 */
H. Peter Anvin3e555482017-04-23 17:02:46 -0700224 lfree->admin.next = nasm_malloc(LBLK_SIZE);
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400225 lfree = lfree->admin.next;
226 init_block(lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700227 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700228
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300229 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700230 *created = true;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300231
H. Peter Anvin98578072018-06-01 18:02:54 -0700232 nasm_zero(*lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700233 lfree->admin.movingon = BOGUS_VALUE;
H. Peter Anvin98578072018-06-01 18:02:54 -0700234 lfree->defn.label = perm_copy(label);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700235 lfree->defn.subsection = NO_SEG;
H. Peter Anvin98578072018-06-01 18:02:54 -0700236 if (label_str)
237 nasm_free(label_str);
H. Peter Anvin70653092007-10-19 14:42:29 -0700238
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700239 hash_add(&ip, lfree->defn.label, lfree);
240 return lfree++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000241}
242
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700243bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000244{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000245 union label *lptr;
246
Keith Kaniosb7a89542007-04-12 02:40:54 +0000247 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700248 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000249
H. Peter Anvin98578072018-06-01 18:02:54 -0700250 lptr = find_label(label, false, NULL);
251 if (lptr && lptr->defn.defined) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000252 *segment = lptr->defn.segment;
253 *offset = lptr->defn.offset;
H. Peter Anvin70055962007-10-11 00:05:31 -0700254 return true;
Cyrill Gorcunovd7a64b72010-04-20 15:26:08 +0400255 }
256
257 return false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000258}
259
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700260bool is_extern(const char *label)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000261{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000262 union label *lptr;
263
Keith Kaniosb7a89542007-04-12 02:40:54 +0000264 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700265 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000266
H. Peter Anvin98578072018-06-01 18:02:54 -0700267 lptr = find_label(label, false, NULL);
268 return lptr && lptr->defn.type == LBL_EXTERN;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000269}
270
H. Peter Anvin98578072018-06-01 18:02:54 -0700271static const char *mangle_strings[] = {"", "", "", ""};
272static bool mangle_string_set[ARRAY_SIZE(mangle_strings)];
273
274/*
275 * Set a prefix or suffix
276 */
277void set_label_mangle(enum mangle_index which, const char *what)
278{
279 if (mangle_string_set[which])
280 return; /* Once set, do not change */
281
282 mangle_strings[which] = perm_copy(what);
283 mangle_string_set[which] = true;
284}
285
286/*
287 * Format a label name with appropriate prefixes and suffixes
288 */
289static const char *mangle_label_name(union label *lptr)
290{
291 const char *prefix;
292 const char *suffix;
293
294 if (likely(lptr->defn.mangled &&
295 lptr->defn.mangled_type == lptr->defn.type))
296 return lptr->defn.mangled; /* Already mangled */
297
298 switch (lptr->defn.type) {
299 case LBL_GLOBAL:
300 case LBL_STATIC:
301 case LBL_EXTERN:
302 prefix = mangle_strings[LM_GPREFIX];
303 suffix = mangle_strings[LM_GSUFFIX];
304 break;
305 case LBL_BACKEND:
306 case LBL_SPECIAL:
307 prefix = suffix = "";
308 break;
309 default:
310 prefix = mangle_strings[LM_LPREFIX];
311 suffix = mangle_strings[LM_LSUFFIX];
312 break;
313 }
314
315 lptr->defn.mangled_type = lptr->defn.type;
316
317 if (!(*prefix) && !(*suffix))
318 lptr->defn.mangled = lptr->defn.label;
319 else
320 lptr->defn.mangled = perm_copy3(prefix, lptr->defn.label, suffix);
321
322 return lptr->defn.mangled;
323}
324
325static void
H. Peter Anvin29695c82018-06-14 17:04:32 -0700326handle_herelabel(union label *lptr, int32_t *segment, int64_t *offset)
H. Peter Anvin892c4812018-05-30 14:43:46 -0700327{
328 int32_t oldseg;
329
330 if (likely(!ofmt->herelabel))
331 return;
332
333 if (unlikely(location.segment == NO_SEG))
334 return;
335
336 oldseg = *segment;
337
338 if (oldseg == location.segment && *offset == location.offset) {
339 /* This label is defined at this location */
340 int32_t newseg;
341
H. Peter Anvin98578072018-06-01 18:02:54 -0700342 nasm_assert(lptr->defn.mangled);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700343 newseg = ofmt->herelabel(lptr->defn.mangled, lptr->defn.type,
344 oldseg, &lptr->defn.subsection);
H. Peter Anvin892c4812018-05-30 14:43:46 -0700345 if (likely(newseg == oldseg))
346 return;
347
348 *segment = newseg;
349 *offset = switch_segment(newseg);
350 }
351}
352
H. Peter Anvin98578072018-06-01 18:02:54 -0700353static bool declare_label_lptr(union label *lptr,
354 enum label_type type, const char *special)
355{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700356 if (special && !special[0])
357 special = NULL;
358
H. Peter Anvin98578072018-06-01 18:02:54 -0700359 if (lptr->defn.type == type ||
360 (pass0 == 0 && lptr->defn.type == LBL_LOCAL)) {
361 lptr->defn.type = type;
362 if (special) {
363 if (!lptr->defn.special)
364 lptr->defn.special = perm_copy(special);
365 else if (nasm_stricmp(lptr->defn.special, special))
366 nasm_error(ERR_NONFATAL,
367 "symbol `%s' has inconsistent attributes `%s' and `%s'",
368 lptr->defn.label, lptr->defn.special, special);
369 }
370 return true;
371 }
372
373 /* EXTERN can be replaced with GLOBAL or COMMON */
374 if (lptr->defn.type == LBL_EXTERN &&
375 (type == LBL_GLOBAL || type == LBL_COMMON)) {
376 lptr->defn.type = type;
377 /* Override special unconditionally */
378 if (special)
379 lptr->defn.special = perm_copy(special);
380 return true;
381 }
382
383 /* GLOBAL or COMMON ignore subsequent EXTERN */
384 if ((lptr->defn.type == LBL_GLOBAL || lptr->defn.type == LBL_COMMON) &&
385 type == LBL_EXTERN) {
386 if (!lptr->defn.special)
387 lptr->defn.special = perm_copy(special);
388 return true;
389 }
390
391 nasm_error(ERR_NONFATAL, "symbol `%s' declared both as %s and %s",
392 lptr->defn.label, types[lptr->defn.type], types[type]);
393
394 return false;
395}
396
397bool declare_label(const char *label, enum label_type type, const char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000398{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000399 union label *lptr;
H. Peter Anvin98578072018-06-01 18:02:54 -0700400 bool created;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401
H. Peter Anvin98578072018-06-01 18:02:54 -0700402 lptr = find_label(label, true, &created);
403 return declare_label_lptr(lptr, type, special);
404}
405
406/*
407 * The "normal" argument decides if we should update the local segment
408 * base name or not.
409 */
410void define_label(const char *label, int32_t segment,
411 int64_t offset, bool normal)
412{
413 union label *lptr;
414 bool created, changed;
H. Peter Anvin73482482018-06-11 14:54:14 -0700415 int64_t size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700416
417 /*
418 * Phase errors here can be one of two types: a new label appears,
419 * or the offset changes. Increment global_offset_changed when that
420 * happens, to tell the assembler core to make another pass.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000421 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700422 lptr = find_label(label, true, &created);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000423
H. Peter Anvin98578072018-06-01 18:02:54 -0700424 if (pass0 > 1) {
425 if (created)
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300426 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000427 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000428
H. Peter Anvin98578072018-06-01 18:02:54 -0700429 if (lptr->defn.defined || lptr->defn.type == LBL_BACKEND) {
430 /* We have seen this on at least one previous pass */
431 mangle_label_name(lptr);
432 handle_herelabel(lptr, &segment, &offset);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433 }
434
H. Peter Anvin98578072018-06-01 18:02:54 -0700435 if (ismagic(label) && lptr->defn.type == LBL_LOCAL)
436 lptr->defn.type = LBL_SPECIAL;
H. Peter Anvin73482482018-06-11 14:54:14 -0700437
H. Peter Anvin98578072018-06-01 18:02:54 -0700438 if (!islocal(label) && normal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 prevlabel = lptr->defn.label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000441
H. Peter Anvin73482482018-06-11 14:54:14 -0700442 if (lptr->defn.type == LBL_COMMON) {
443 size = offset;
444 offset = 0;
445 } else {
446 size = 0; /* This is a hack... */
447 }
448
H. Peter Anvin98578072018-06-01 18:02:54 -0700449 changed = !lptr->defn.defined || lptr->defn.segment != segment ||
H. Peter Anvin73482482018-06-11 14:54:14 -0700450 lptr->defn.offset != offset || lptr->defn.size != size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700451 global_offset_changed += changed;
452
453 /*
454 * This probably should be ERR_NONFATAL, but not quite yet. As a
455 * special case, LBL_SPECIAL symbols are allowed to be changed
456 * even during the last pass.
457 */
458 if (changed && pass0 == 2 && lptr->defn.type != LBL_SPECIAL)
459 nasm_error(ERR_WARNING, "label `%s' changed during code generation",
460 lptr->defn.label);
461
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000462 lptr->defn.segment = segment;
H. Peter Anvin98578072018-06-01 18:02:54 -0700463 lptr->defn.offset = offset;
H. Peter Anvin73482482018-06-11 14:54:14 -0700464 lptr->defn.size = size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700465 lptr->defn.defined = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000466
H. Peter Anvin98578072018-06-01 18:02:54 -0700467 out_symdef(lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000468}
469
H. Peter Anvin98578072018-06-01 18:02:54 -0700470/*
471 * Define a special backend label
472 */
473void backend_label(const char *label, int32_t segment, int64_t offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000474{
H. Peter Anvin98578072018-06-01 18:02:54 -0700475 if (!declare_label(label, LBL_BACKEND, NULL))
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400476 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700477
H. Peter Anvin98578072018-06-01 18:02:54 -0700478 define_label(label, segment, offset, false);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000479}
480
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000482{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700483 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000484
H. Peter Anvin3e555482017-04-23 17:02:46 -0700485 ldata = lfree = nasm_malloc(LBLK_SIZE);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700486 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400488 perm_head = perm_tail =
H. Peter Anvin3e555482017-04-23 17:02:46 -0700489 nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000490
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491 perm_head->next = NULL;
492 perm_head->size = PERMTS_SIZE;
493 perm_head->usage = 0;
494
495 prevlabel = "";
496
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700497 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000498
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499 return 0;
500}
501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700504 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700506 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000507
H. Peter Anvin166c2472008-05-28 12:28:58 -0700508 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000509
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700510 lptr = lhold = ldata;
511 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400512 lptr = &lptr[LABEL_BLOCK-1];
513 lptr = lptr->admin.next;
514 nasm_free(lhold);
515 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000516 }
517
518 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000519 perm_tail = perm_head;
520 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000522 }
523}
524
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000527 int j;
528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 for (j = 0; j < LABEL_BLOCK - 1; j++)
530 blk[j].admin.movingon = END_LIST;
531 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
532 blk[LABEL_BLOCK - 1].admin.next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533}
534
H. Peter Anvin98578072018-06-01 18:02:54 -0700535static char * safe_alloc perm_alloc(size_t len)
536{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700537 char *p;
H. Peter Anvin73482482018-06-11 14:54:14 -0700538
H. Peter Anvin98578072018-06-01 18:02:54 -0700539 if (perm_tail->size - perm_tail->usage < len) {
540 size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE;
541 perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len);
542 perm_tail = perm_tail->next;
543 perm_tail->next = NULL;
544 perm_tail->size = alloc_len;
545 perm_tail->usage = 0;
546 }
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700547 p = perm_tail->data + perm_tail->usage;
H. Peter Anvin98578072018-06-01 18:02:54 -0700548 perm_tail->usage += len;
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700549 return p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700550}
551
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700552static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000553{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700554 char *p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700555 size_t len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000556
H. Peter Anvin98578072018-06-01 18:02:54 -0700557 if (!string)
558 return NULL;
H. Peter Anvin565be912009-07-05 22:15:57 -0700559
H. Peter Anvin98578072018-06-01 18:02:54 -0700560 len = strlen(string)+1; /* Include final NUL */
561
562 p = perm_alloc(len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700563 memcpy(p, string, len);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000564
565 return p;
566}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700568static char *
H. Peter Anvin98578072018-06-01 18:02:54 -0700569perm_copy3(const char *s1, const char *s2, const char *s3)
570{
571 char *p;
572 size_t l1 = strlen(s1);
573 size_t l2 = strlen(s2);
574 size_t l3 = strlen(s3)+1; /* Include final NUL */
H. Peter Anvin73482482018-06-11 14:54:14 -0700575
H. Peter Anvin98578072018-06-01 18:02:54 -0700576 p = perm_alloc(l1+l2+l3);
577 memcpy(p, s1, l1);
578 memcpy(p+l1, s2, l2);
579 memcpy(p+l1+l2, s3, l3);
580
581 return p;
582}
583
584const char *local_scope(const char *label)
Charles Crayned60059e2008-03-12 22:39:03 -0700585{
586 return islocal(label) ? prevlabel : "";
587}
588
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589/*
590 * Notes regarding bug involving redefinition of external segments.
591 *
592 * Up to and including v0.97, the following code didn't work. From 0.97
593 * developers release 2 onwards, it will generate an error.
594 *
595 * EXTERN extlabel
596 * newlabel EQU extlabel + 1
597 *
598 * The results of allowing this code through are that two import records
599 * are generated, one for 'extlabel' and one for 'newlabel'.
600 *
601 * The reason for this is an inadequacy in the defined interface between
602 * the label manager and the output formats. The problem lies in how the
603 * output format driver tells that a label is an external label for which
604 * a label import record must be produced. Most (all except bin?) produce
605 * the record if the segment number of the label is not one of the internal
606 * segments that the output driver is producing.
607 *
608 * A simple fix to this would be to make the output formats keep track of
609 * which symbols they've produced import records for, and make them not
610 * produce import records for segments that are already defined.
611 *
612 * The best way, which is slightly harder but reduces duplication of code
613 * and should therefore make the entire system smaller and more stable is
614 * to change the interface between assembler, define_label(), and
615 * the output module. The changes that are needed are:
616 *
617 * The semantics of the 'isextern' flag passed to define_label() need
618 * examining. This information may or may not tell us what we need to
619 * know (ie should we be generating an import record at this point for this
620 * label). If these aren't the semantics, the semantics should be changed
621 * to this.
622 *
623 * The output module interface needs changing, so that the `isextern' flag
624 * is passed to the module, so that it can be easily tested for.
625 */