blob: 201076031cca9c65a88a18b7ada4aecc00d53b84 [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
H. Peter Anvin, Intelbc77f9c2018-06-25 12:39:42 -070075/*
76 * Return true if we should update the local label base
77 * as a result of this symbol. We must exclude local labels
78 * as well as any kind of special labels, including ..@ ones.
79 */
80static bool set_prevlabel(const char *l)
81{
82 if (tasm_compatible_mode) {
83 if (l[0] == '@' && l[1] == '@')
84 return false;
85 }
86
87 return l[0] != '.';
88}
89
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040090#define LABEL_BLOCK 128 /* no. of labels/block */
91#define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000092
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040093#define END_LIST -3 /* don't clash with NO_SEG! */
94#define END_BLOCK -2
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000095
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040096#define PERMTS_SIZE 16384 /* size of text blocks */
H. Peter Anvin565be912009-07-05 22:15:57 -070097#if (PERMTS_SIZE < IDLEN_MAX)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040098 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
Ed Berosetc06f6df2003-09-08 00:30:40 +000099#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100
H. Peter Anvin98578072018-06-01 18:02:54 -0700101/* string values for enum label_type */
102static const char * const types[] =
103{"local", "global", "static", "extern", "common", "special",
104 "output format special"};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106union label { /* actual label structures */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -0800108 int32_t segment;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700109 int32_t subsection; /* Available for ofmt->herelabel() */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400110 int64_t offset;
H. Peter Anvin73482482018-06-11 14:54:14 -0700111 int64_t size;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800112 int64_t defined; /* 0 if undefined, passn+1 for when defn seen */
H. Peter Anvin98578072018-06-01 18:02:54 -0700113 char *label, *mangled, *special;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800114 const char *def_file; /* Where defined */
115 int32_t def_line;
H. Peter Anvin98578072018-06-01 18:02:54 -0700116 enum label_type type, mangled_type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117 } defn;
118 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -0800119 int32_t movingon;
120 int64_t dummy;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000121 union label *next;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122 } admin;
123};
124
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125struct permts { /* permanent text storage */
126 struct permts *next; /* for the linked list */
H. Peter Anvin98578072018-06-01 18:02:54 -0700127 unsigned int size, usage; /* size and used space in ... */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000128 char data[PERMTS_SIZE]; /* ... the data block itself */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129};
H. Peter Anvin98578072018-06-01 18:02:54 -0700130#define PERMTS_HEADER offsetof(struct permts, data)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131
H. Peter Anvin9c595b62017-03-07 19:44:21 -0800132uint64_t global_offset_changed; /* counter for global offset changes */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000133
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400134static struct hash_table ltab; /* labels hash table */
135static union label *ldata; /* all label data blocks */
136static union label *lfree; /* labels free block */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000137static struct permts *perm_head; /* start of perm. text storage */
138static struct permts *perm_tail; /* end of perm. text storage */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000139
H. Peter Anvine2c80182005-01-15 22:15:51 +0000140static void init_block(union label *blk);
H. Peter Anvin98578072018-06-01 18:02:54 -0700141static char *perm_alloc(size_t len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700142static char *perm_copy(const char *string);
H. Peter Anvin98578072018-06-01 18:02:54 -0700143static char *perm_copy3(const char *s1, const char *s2, const char *s3);
144static const char *mangle_label_name(union label *lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145
H. Peter Anvin98578072018-06-01 18:02:54 -0700146static const char *prevlabel;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000147
H. Peter Anvin70055962007-10-11 00:05:31 -0700148static bool initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000149
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000150/*
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800151 * Emit a symdef to the output and the debug format backends.
152 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700153static void out_symdef(union label *lptr)
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800154{
H. Peter Anvin98578072018-06-01 18:02:54 -0700155 int backend_type;
H. Peter Anvin73482482018-06-11 14:54:14 -0700156 int64_t backend_offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700157
158 /* Backend-defined special segments are passed to symdef immediately */
159 if (pass0 == 2) {
160 /* Emit special fixups for globals and commons */
161 switch (lptr->defn.type) {
162 case LBL_GLOBAL:
H. Peter Anvin98578072018-06-01 18:02:54 -0700163 case LBL_EXTERN:
H. Peter Anvin73482482018-06-11 14:54:14 -0700164 case LBL_COMMON:
H. Peter Anvin98578072018-06-01 18:02:54 -0700165 if (lptr->defn.special)
H. Peter Anvina7c8e392018-06-18 14:17:26 -0700166 ofmt->symdef(lptr->defn.mangled, 0, 0, 3, lptr->defn.special);
H. Peter Anvin98578072018-06-01 18:02:54 -0700167 break;
168 default:
169 break;
170 }
171 return;
172 }
173
174 if (pass0 != 1 && lptr->defn.type != LBL_BACKEND)
175 return;
176
177 /* Clean up this hack... */
178 switch(lptr->defn.type) {
179 case LBL_GLOBAL:
Cyrill Gorcunov70d42962018-07-01 01:59:07 +0300180 case LBL_EXTERN:
H. Peter Anvin98578072018-06-01 18:02:54 -0700181 backend_type = 1;
H. Peter Anvin73482482018-06-11 14:54:14 -0700182 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700183 break;
184 case LBL_COMMON:
185 backend_type = 2;
H. Peter Anvin73482482018-06-11 14:54:14 -0700186 backend_offset = lptr->defn.size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700187 break;
188 default:
189 backend_type = 0;
H. Peter Anvin73482482018-06-11 14:54:14 -0700190 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700191 break;
192 }
193
194 /* Might be necessary for a backend symbol */
195 mangle_label_name(lptr);
196
197 ofmt->symdef(lptr->defn.mangled, lptr->defn.segment,
H. Peter Anvin73482482018-06-11 14:54:14 -0700198 backend_offset, backend_type,
H. Peter Anvin98578072018-06-01 18:02:54 -0700199 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800200
201 /*
202 * NASM special symbols are not passed to the debug format; none
203 * of the current backends want to see them.
204 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700205 if (lptr->defn.type == LBL_SPECIAL || lptr->defn.type == LBL_BACKEND)
206 return;
207
208 dfmt->debug_deflabel(lptr->defn.mangled, lptr->defn.segment,
209 lptr->defn.offset, backend_type,
210 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800211}
212
213/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214 * Internal routine: finds the `union label' corresponding to the
215 * given label name. Creates a new one, if it isn't found, and if
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700216 * `create' is true.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700218static union label *find_label(const char *label, bool create, bool *created)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000219{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700220 union label *lptr, **lpp;
H. Peter Anvin98578072018-06-01 18:02:54 -0700221 char *label_str = NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700222 struct hash_insert ip;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000223
Cyrill Gorcunov8e740c62018-10-13 17:18:05 +0300224 nasm_assert(label != NULL);
225
H. Peter Anvin98578072018-06-01 18:02:54 -0700226 if (islocal(label))
227 label = label_str = nasm_strcat(prevlabel, label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000228
H. Peter Anvin166c2472008-05-28 12:28:58 -0700229 lpp = (union label **) hash_find(&ltab, label, &ip);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700230 lptr = lpp ? *lpp : NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700231
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300232 if (lptr || !create) {
233 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700234 *created = false;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400235 return lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300236 }
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700237
238 /* Create a new label... */
239 if (lfree->admin.movingon == END_BLOCK) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400240 /*
241 * must allocate a new block
242 */
H. Peter Anvin3e555482017-04-23 17:02:46 -0700243 lfree->admin.next = nasm_malloc(LBLK_SIZE);
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400244 lfree = lfree->admin.next;
245 init_block(lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700246 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700247
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300248 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700249 *created = true;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300250
H. Peter Anvin98578072018-06-01 18:02:54 -0700251 nasm_zero(*lfree);
H. Peter Anvin98578072018-06-01 18:02:54 -0700252 lfree->defn.label = perm_copy(label);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700253 lfree->defn.subsection = NO_SEG;
H. Peter Anvin98578072018-06-01 18:02:54 -0700254 if (label_str)
255 nasm_free(label_str);
H. Peter Anvin70653092007-10-19 14:42:29 -0700256
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700257 hash_add(&ip, lfree->defn.label, lfree);
258 return lfree++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000259}
260
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700261bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000262{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000263 union label *lptr;
264
Keith Kaniosb7a89542007-04-12 02:40:54 +0000265 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700266 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000267
H. Peter Anvin98578072018-06-01 18:02:54 -0700268 lptr = find_label(label, false, NULL);
269 if (lptr && lptr->defn.defined) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000270 *segment = lptr->defn.segment;
271 *offset = lptr->defn.offset;
H. Peter Anvin70055962007-10-11 00:05:31 -0700272 return true;
Cyrill Gorcunovd7a64b72010-04-20 15:26:08 +0400273 }
274
275 return false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000276}
277
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700278bool is_extern(const char *label)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280 union label *lptr;
281
Keith Kaniosb7a89542007-04-12 02:40:54 +0000282 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700283 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000284
H. Peter Anvin98578072018-06-01 18:02:54 -0700285 lptr = find_label(label, false, NULL);
286 return lptr && lptr->defn.type == LBL_EXTERN;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000287}
288
H. Peter Anvin98578072018-06-01 18:02:54 -0700289static const char *mangle_strings[] = {"", "", "", ""};
290static bool mangle_string_set[ARRAY_SIZE(mangle_strings)];
291
292/*
293 * Set a prefix or suffix
294 */
295void set_label_mangle(enum mangle_index which, const char *what)
296{
297 if (mangle_string_set[which])
298 return; /* Once set, do not change */
299
300 mangle_strings[which] = perm_copy(what);
301 mangle_string_set[which] = true;
302}
303
304/*
305 * Format a label name with appropriate prefixes and suffixes
306 */
307static const char *mangle_label_name(union label *lptr)
308{
309 const char *prefix;
310 const char *suffix;
311
312 if (likely(lptr->defn.mangled &&
313 lptr->defn.mangled_type == lptr->defn.type))
314 return lptr->defn.mangled; /* Already mangled */
315
316 switch (lptr->defn.type) {
317 case LBL_GLOBAL:
318 case LBL_STATIC:
319 case LBL_EXTERN:
320 prefix = mangle_strings[LM_GPREFIX];
321 suffix = mangle_strings[LM_GSUFFIX];
322 break;
323 case LBL_BACKEND:
324 case LBL_SPECIAL:
325 prefix = suffix = "";
326 break;
327 default:
328 prefix = mangle_strings[LM_LPREFIX];
329 suffix = mangle_strings[LM_LSUFFIX];
330 break;
331 }
332
333 lptr->defn.mangled_type = lptr->defn.type;
334
335 if (!(*prefix) && !(*suffix))
336 lptr->defn.mangled = lptr->defn.label;
337 else
338 lptr->defn.mangled = perm_copy3(prefix, lptr->defn.label, suffix);
339
340 return lptr->defn.mangled;
341}
342
343static void
H. Peter Anvin29695c82018-06-14 17:04:32 -0700344handle_herelabel(union label *lptr, int32_t *segment, int64_t *offset)
H. Peter Anvin892c4812018-05-30 14:43:46 -0700345{
346 int32_t oldseg;
347
348 if (likely(!ofmt->herelabel))
349 return;
350
351 if (unlikely(location.segment == NO_SEG))
352 return;
353
354 oldseg = *segment;
355
356 if (oldseg == location.segment && *offset == location.offset) {
357 /* This label is defined at this location */
358 int32_t newseg;
H. Peter Anvin (Intel)d6441192018-06-27 20:20:21 -0700359 bool copyoffset = false;
H. Peter Anvin892c4812018-05-30 14:43:46 -0700360
H. Peter Anvin98578072018-06-01 18:02:54 -0700361 nasm_assert(lptr->defn.mangled);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700362 newseg = ofmt->herelabel(lptr->defn.mangled, lptr->defn.type,
H. Peter Anvin (Intel)d6441192018-06-27 20:20:21 -0700363 oldseg, &lptr->defn.subsection, &copyoffset);
H. Peter Anvin892c4812018-05-30 14:43:46 -0700364 if (likely(newseg == oldseg))
365 return;
366
367 *segment = newseg;
H. Peter Anvin (Intel)d6441192018-06-27 20:20:21 -0700368 if (copyoffset) {
369 /* Maintain the offset from the old to the new segment */
370 switch_segment(newseg);
371 location.offset = *offset;
372 } else {
373 /* Keep a separate offset for the new segment */
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800374 *offset = switch_segment(newseg);
H. Peter Anvin (Intel)d6441192018-06-27 20:20:21 -0700375 }
H. Peter Anvin892c4812018-05-30 14:43:46 -0700376 }
377}
378
H. Peter Anvin98578072018-06-01 18:02:54 -0700379static bool declare_label_lptr(union label *lptr,
380 enum label_type type, const char *special)
381{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700382 if (special && !special[0])
383 special = NULL;
384
H. Peter Anvin98578072018-06-01 18:02:54 -0700385 if (lptr->defn.type == type ||
386 (pass0 == 0 && lptr->defn.type == LBL_LOCAL)) {
387 lptr->defn.type = type;
388 if (special) {
389 if (!lptr->defn.special)
390 lptr->defn.special = perm_copy(special);
391 else if (nasm_stricmp(lptr->defn.special, special))
392 nasm_error(ERR_NONFATAL,
393 "symbol `%s' has inconsistent attributes `%s' and `%s'",
394 lptr->defn.label, lptr->defn.special, special);
395 }
396 return true;
397 }
398
399 /* EXTERN can be replaced with GLOBAL or COMMON */
400 if (lptr->defn.type == LBL_EXTERN &&
401 (type == LBL_GLOBAL || type == LBL_COMMON)) {
402 lptr->defn.type = type;
403 /* Override special unconditionally */
404 if (special)
405 lptr->defn.special = perm_copy(special);
406 return true;
407 }
408
409 /* GLOBAL or COMMON ignore subsequent EXTERN */
410 if ((lptr->defn.type == LBL_GLOBAL || lptr->defn.type == LBL_COMMON) &&
411 type == LBL_EXTERN) {
412 if (!lptr->defn.special)
413 lptr->defn.special = perm_copy(special);
H. Peter Anvin, Intel4fb2acc2018-06-25 13:11:01 -0700414 return false; /* Don't call define_label() after this! */
H. Peter Anvin98578072018-06-01 18:02:54 -0700415 }
416
417 nasm_error(ERR_NONFATAL, "symbol `%s' declared both as %s and %s",
418 lptr->defn.label, types[lptr->defn.type], types[type]);
419
420 return false;
421}
422
423bool declare_label(const char *label, enum label_type type, const char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000424{
Cyrill Gorcunova8e3d6a2018-06-30 20:02:24 +0300425 union label *lptr = find_label(label, true, NULL);
H. Peter Anvin98578072018-06-01 18:02:54 -0700426 return declare_label_lptr(lptr, type, special);
427}
428
429/*
430 * The "normal" argument decides if we should update the local segment
431 * base name or not.
432 */
433void define_label(const char *label, int32_t segment,
434 int64_t offset, bool normal)
435{
436 union label *lptr;
437 bool created, changed;
H. Peter Anvin73482482018-06-11 14:54:14 -0700438 int64_t size;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800439 int64_t lastdef;
440
441 /*
442 * The backend may invoke this before pass 1, so treat that as
443 * a special "pass".
444 */
H. Peter Anvin (Intel)0402a2d2018-12-14 13:01:39 -0800445 const int64_t lpass = passn + 1;
H. Peter Anvin98578072018-06-01 18:02:54 -0700446
447 /*
448 * Phase errors here can be one of two types: a new label appears,
449 * or the offset changes. Increment global_offset_changed when that
450 * happens, to tell the assembler core to make another pass.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700452 lptr = find_label(label, true, &created);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000453
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800454 lastdef = lptr->defn.defined;
455
H. Peter Anvin, Intelc5e45f62018-06-25 13:16:53 -0700456 if (segment) {
457 /* We are actually defining this label */
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800458 if (lptr->defn.type == LBL_EXTERN) {
459 /* auto-promote EXTERN to GLOBAL */
H. Peter Anvin, Intelc5e45f62018-06-25 13:16:53 -0700460 lptr->defn.type = LBL_GLOBAL;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800461 lastdef = 0; /* We are "re-creating" this label */
462 }
H. Peter Anvin, Intelc5e45f62018-06-25 13:16:53 -0700463 } else {
464 /* It's a pseudo-segment (extern, common) */
H. Peter Anvin46c839a2018-06-14 20:00:07 -0700465 segment = lptr->defn.segment ? lptr->defn.segment : seg_alloc();
H. Peter Anvin, Intelc5e45f62018-06-25 13:16:53 -0700466 }
H. Peter Anvinaf5f9182018-06-14 19:53:45 -0700467
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800468 if (lastdef || lptr->defn.type == LBL_BACKEND) {
469 /*
470 * We have seen this on at least one previous pass, or
471 * potentially earlier in this same pass (in which case we
472 * will probably error out further down.)
473 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700474 mangle_label_name(lptr);
475 handle_herelabel(lptr, &segment, &offset);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000476 }
477
H. Peter Anvin98578072018-06-01 18:02:54 -0700478 if (ismagic(label) && lptr->defn.type == LBL_LOCAL)
479 lptr->defn.type = LBL_SPECIAL;
H. Peter Anvin73482482018-06-11 14:54:14 -0700480
H. Peter Anvin, Intelbc77f9c2018-06-25 12:39:42 -0700481 if (set_prevlabel(label) && normal)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 prevlabel = lptr->defn.label;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483
H. Peter Anvin73482482018-06-11 14:54:14 -0700484 if (lptr->defn.type == LBL_COMMON) {
485 size = offset;
486 offset = 0;
487 } else {
488 size = 0; /* This is a hack... */
489 }
490
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800491 changed = created || !lastdef ||
H. Peter Anvin, Intelbc77f9c2018-06-25 12:39:42 -0700492 lptr->defn.segment != segment ||
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800493 lptr->defn.offset != offset ||
494 lptr->defn.size != size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700495 global_offset_changed += changed;
496
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800497 if (lastdef == lpass) {
498 int32_t saved_line = 0;
499 const char *saved_fname = NULL;
500 int noteflags;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800501
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800502 /*
503 * Defined elsewhere in the program, seen in this pass.
504 */
505 if (changed) {
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800506 nasm_error(ERR_NONFATAL,
507 "label `%s' inconsistently redefined",
508 lptr->defn.label);
H. Peter Anvine2f5edb2018-12-11 00:06:29 -0800509 noteflags = ERR_NOTE|ERR_HERE;
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800510 } else {
511 nasm_error(ERR_WARNING|WARN_LABEL_REDEF|ERR_PASS2,
512 "label `%s' redefined to an identical value",
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800513 lptr->defn.label);
H. Peter Anvine2f5edb2018-12-11 00:06:29 -0800514 noteflags = ERR_NOTE|ERR_HERE|WARN_LABEL_REDEF|ERR_PASS2;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800515 }
H. Peter Anvin98578072018-06-01 18:02:54 -0700516
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800517 src_get(&saved_line, &saved_fname);
518 src_set(lptr->defn.def_line, lptr->defn.def_file);
H. Peter Anvine2f5edb2018-12-11 00:06:29 -0800519 nasm_error(noteflags, "label `%s' originally defined",
520 lptr->defn.label);
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800521 src_set(saved_line, saved_fname);
522 } else if (changed && pass0 > 1 && lptr->defn.type != LBL_SPECIAL) {
523 /*
H. Peter Anvin (Intel)0402a2d2018-12-14 13:01:39 -0800524 * WARN_LABEL_LATE defaults to an error, as this should never
525 * actually happen. Just in case this is a backwards
526 * compatibility problem, still make it a warning so that the
527 * user can suppress or demote it.
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -0800528 *
529 * As a special case, LBL_SPECIAL symbols are allowed to be changed
530 * even during the last pass.
531 */
532 nasm_error(ERR_WARNING|WARN_LABEL_LATE,
533 "label `%s' %s during code generation",
534 lptr->defn.label, created ? "defined" : "changed");
535 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000536 lptr->defn.segment = segment;
H. Peter Anvin98578072018-06-01 18:02:54 -0700537 lptr->defn.offset = offset;
H. Peter Anvin73482482018-06-11 14:54:14 -0700538 lptr->defn.size = size;
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800539 lptr->defn.defined = lpass;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000540
H. Peter Anvinb424ae32018-12-10 13:30:51 -0800541 if (changed || lastdef != lpass)
542 src_get(&lptr->defn.def_line, &lptr->defn.def_file);
543
544 if (lastdef != lpass)
545 out_symdef(lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000546}
547
H. Peter Anvin98578072018-06-01 18:02:54 -0700548/*
549 * Define a special backend label
550 */
551void backend_label(const char *label, int32_t segment, int64_t offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552{
H. Peter Anvin98578072018-06-01 18:02:54 -0700553 if (!declare_label(label, LBL_BACKEND, NULL))
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400554 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700555
H. Peter Anvin98578072018-06-01 18:02:54 -0700556 define_label(label, segment, offset, false);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557}
558
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700561 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562
H. Peter Anvin3e555482017-04-23 17:02:46 -0700563 ldata = lfree = nasm_malloc(LBLK_SIZE);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700564 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400566 perm_head = perm_tail =
H. Peter Anvin3e555482017-04-23 17:02:46 -0700567 nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569 perm_head->next = NULL;
570 perm_head->size = PERMTS_SIZE;
571 perm_head->usage = 0;
572
573 prevlabel = "";
574
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700575 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000577 return 0;
578}
579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700582 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000583
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700584 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000585
H. Peter Anvin166c2472008-05-28 12:28:58 -0700586 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000587
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700588 lptr = lhold = ldata;
589 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400590 lptr = &lptr[LABEL_BLOCK-1];
591 lptr = lptr->admin.next;
592 nasm_free(lhold);
593 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000594 }
595
596 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000597 perm_tail = perm_head;
598 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000600 }
601}
602
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605 int j;
606
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 for (j = 0; j < LABEL_BLOCK - 1; j++)
608 blk[j].admin.movingon = END_LIST;
609 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
610 blk[LABEL_BLOCK - 1].admin.next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000611}
612
H. Peter Anvin98578072018-06-01 18:02:54 -0700613static char * safe_alloc perm_alloc(size_t len)
614{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700615 char *p;
H. Peter Anvin73482482018-06-11 14:54:14 -0700616
H. Peter Anvin98578072018-06-01 18:02:54 -0700617 if (perm_tail->size - perm_tail->usage < len) {
618 size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE;
619 perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len);
620 perm_tail = perm_tail->next;
621 perm_tail->next = NULL;
622 perm_tail->size = alloc_len;
623 perm_tail->usage = 0;
624 }
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700625 p = perm_tail->data + perm_tail->usage;
H. Peter Anvin98578072018-06-01 18:02:54 -0700626 perm_tail->usage += len;
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700627 return p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700628}
629
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700630static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700632 char *p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700633 size_t len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634
H. Peter Anvin98578072018-06-01 18:02:54 -0700635 if (!string)
636 return NULL;
H. Peter Anvin565be912009-07-05 22:15:57 -0700637
H. Peter Anvin98578072018-06-01 18:02:54 -0700638 len = strlen(string)+1; /* Include final NUL */
639
640 p = perm_alloc(len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700641 memcpy(p, string, len);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000642
643 return p;
644}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700646static char *
H. Peter Anvin98578072018-06-01 18:02:54 -0700647perm_copy3(const char *s1, const char *s2, const char *s3)
648{
649 char *p;
650 size_t l1 = strlen(s1);
651 size_t l2 = strlen(s2);
652 size_t l3 = strlen(s3)+1; /* Include final NUL */
H. Peter Anvin73482482018-06-11 14:54:14 -0700653
H. Peter Anvin98578072018-06-01 18:02:54 -0700654 p = perm_alloc(l1+l2+l3);
655 memcpy(p, s1, l1);
656 memcpy(p+l1, s2, l2);
657 memcpy(p+l1+l2, s3, l3);
658
659 return p;
660}
661
662const char *local_scope(const char *label)
Charles Crayned60059e2008-03-12 22:39:03 -0700663{
664 return islocal(label) ? prevlabel : "";
665}
666
H. Peter Anvineba20a72002-04-30 20:53:55 +0000667/*
668 * Notes regarding bug involving redefinition of external segments.
669 *
670 * Up to and including v0.97, the following code didn't work. From 0.97
671 * developers release 2 onwards, it will generate an error.
672 *
673 * EXTERN extlabel
674 * newlabel EQU extlabel + 1
675 *
676 * The results of allowing this code through are that two import records
677 * are generated, one for 'extlabel' and one for 'newlabel'.
678 *
679 * The reason for this is an inadequacy in the defined interface between
680 * the label manager and the output formats. The problem lies in how the
681 * output format driver tells that a label is an external label for which
682 * a label import record must be produced. Most (all except bin?) produce
683 * the record if the segment number of the label is not one of the internal
684 * segments that the output driver is producing.
685 *
686 * A simple fix to this would be to make the output formats keep track of
687 * which symbols they've produced import records for, and make them not
688 * produce import records for segments that are already defined.
689 *
690 * The best way, which is slightly harder but reduces duplication of code
691 * and should therefore make the entire system smaller and more stable is
692 * to change the interface between assembler, define_label(), and
693 * the output module. The changes that are needed are:
694 *
695 * The semantics of the 'isextern' flag passed to define_label() need
696 * examining. This information may or may not tell us what we need to
697 * know (ie should we be generating an import record at this point for this
698 * label). If these aren't the semantics, the semantics should be changed
699 * to this.
700 *
701 * The output module interface needs changing, so that the `isextern' flag
702 * is passed to the module, so that it can be easily tested for.
703 */