blob: 32f131805218986ac280535d3c020ebccd068793 [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
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000080
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040081#define PERMTS_SIZE 16384 /* size of text blocks */
H. Peter Anvin565be912009-07-05 22:15:57 -070082#if (PERMTS_SIZE < IDLEN_MAX)
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040083 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
Ed Berosetc06f6df2003-09-08 00:30:40 +000084#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085
H. Peter Anvin98578072018-06-01 18:02:54 -070086/* string values for enum label_type */
87static const char * const types[] =
88{"local", "global", "static", "extern", "common", "special",
89 "output format special"};
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;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094 int32_t subsection; /* Available for ofmt->herelabel() */
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +040095 int64_t offset;
H. Peter Anvin73482482018-06-11 14:54:14 -070096 int64_t size;
H. Peter Anvin98578072018-06-01 18:02:54 -070097 char *label, *mangled, *special;
98 enum label_type type, mangled_type;
99 bool defined;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 } defn;
101 struct {
Charles Crayne4e8563d2007-11-05 17:19:32 -0800102 int32_t movingon;
103 int64_t dummy;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000104 union label *next;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105 } admin;
106};
107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108struct permts { /* permanent text storage */
109 struct permts *next; /* for the linked list */
H. Peter Anvin98578072018-06-01 18:02:54 -0700110 unsigned int size, usage; /* size and used space in ... */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000111 char data[PERMTS_SIZE]; /* ... the data block itself */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000112};
H. Peter Anvin98578072018-06-01 18:02:54 -0700113#define PERMTS_HEADER offsetof(struct permts, data)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvin9c595b62017-03-07 19:44:21 -0800115uint64_t global_offset_changed; /* counter for global offset changes */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000116
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400117static struct hash_table ltab; /* labels hash table */
118static union label *ldata; /* all label data blocks */
119static union label *lfree; /* labels free block */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120static struct permts *perm_head; /* start of perm. text storage */
121static struct permts *perm_tail; /* end of perm. text storage */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123static void init_block(union label *blk);
H. Peter Anvin98578072018-06-01 18:02:54 -0700124static char *perm_alloc(size_t len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700125static char *perm_copy(const char *string);
H. Peter Anvin98578072018-06-01 18:02:54 -0700126static char *perm_copy3(const char *s1, const char *s2, const char *s3);
127static const char *mangle_label_name(union label *lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
H. Peter Anvin98578072018-06-01 18:02:54 -0700129static const char *prevlabel;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130
H. Peter Anvin70055962007-10-11 00:05:31 -0700131static bool initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000132
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133/*
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800134 * Emit a symdef to the output and the debug format backends.
135 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700136static void out_symdef(union label *lptr)
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800137{
H. Peter Anvin98578072018-06-01 18:02:54 -0700138 int backend_type;
H. Peter Anvin73482482018-06-11 14:54:14 -0700139 int64_t backend_offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700140
141 /* Backend-defined special segments are passed to symdef immediately */
142 if (pass0 == 2) {
143 /* Emit special fixups for globals and commons */
144 switch (lptr->defn.type) {
145 case LBL_GLOBAL:
H. Peter Anvin98578072018-06-01 18:02:54 -0700146 case LBL_EXTERN:
H. Peter Anvin73482482018-06-11 14:54:14 -0700147 case LBL_COMMON:
H. Peter Anvin98578072018-06-01 18:02:54 -0700148 if (lptr->defn.special)
H. Peter Anvina7c8e392018-06-18 14:17:26 -0700149 ofmt->symdef(lptr->defn.mangled, 0, 0, 3, lptr->defn.special);
H. Peter Anvin98578072018-06-01 18:02:54 -0700150 break;
151 default:
152 break;
153 }
154 return;
155 }
156
157 if (pass0 != 1 && lptr->defn.type != LBL_BACKEND)
158 return;
159
160 /* Clean up this hack... */
161 switch(lptr->defn.type) {
162 case LBL_GLOBAL:
163 backend_type = 1;
H. Peter Anvin73482482018-06-11 14:54:14 -0700164 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700165 break;
166 case LBL_COMMON:
167 backend_type = 2;
H. Peter Anvin73482482018-06-11 14:54:14 -0700168 backend_offset = lptr->defn.size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700169 break;
170 default:
171 backend_type = 0;
H. Peter Anvin73482482018-06-11 14:54:14 -0700172 backend_offset = lptr->defn.offset;
H. Peter Anvin98578072018-06-01 18:02:54 -0700173 break;
174 }
175
176 /* Might be necessary for a backend symbol */
177 mangle_label_name(lptr);
178
179 ofmt->symdef(lptr->defn.mangled, lptr->defn.segment,
H. Peter Anvin73482482018-06-11 14:54:14 -0700180 backend_offset, backend_type,
H. Peter Anvin98578072018-06-01 18:02:54 -0700181 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800182
183 /*
184 * NASM special symbols are not passed to the debug format; none
185 * of the current backends want to see them.
186 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700187 if (lptr->defn.type == LBL_SPECIAL || lptr->defn.type == LBL_BACKEND)
188 return;
189
190 dfmt->debug_deflabel(lptr->defn.mangled, lptr->defn.segment,
191 lptr->defn.offset, backend_type,
192 lptr->defn.special);
H. Peter Anvinfc0ff222016-03-07 21:46:04 -0800193}
194
195/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000196 * Internal routine: finds the `union label' corresponding to the
197 * given label name. Creates a new one, if it isn't found, and if
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700198 * `create' is true.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000199 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700200static union label *find_label(const char *label, bool create, bool *created)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700202 union label *lptr, **lpp;
H. Peter Anvin98578072018-06-01 18:02:54 -0700203 char *label_str = NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700204 struct hash_insert ip;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000205
H. Peter Anvin98578072018-06-01 18:02:54 -0700206 if (islocal(label))
207 label = label_str = nasm_strcat(prevlabel, label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208
H. Peter Anvin166c2472008-05-28 12:28:58 -0700209 lpp = (union label **) hash_find(&ltab, label, &ip);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700210 lptr = lpp ? *lpp : NULL;
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700211
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300212 if (lptr || !create) {
213 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700214 *created = false;
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400215 return lptr;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300216 }
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700217
218 /* Create a new label... */
219 if (lfree->admin.movingon == END_BLOCK) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400220 /*
221 * must allocate a new block
222 */
H. Peter Anvin3e555482017-04-23 17:02:46 -0700223 lfree->admin.next = nasm_malloc(LBLK_SIZE);
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400224 lfree = lfree->admin.next;
225 init_block(lfree);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700226 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700227
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300228 if (created)
H. Peter Anvin98578072018-06-01 18:02:54 -0700229 *created = true;
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300230
H. Peter Anvin98578072018-06-01 18:02:54 -0700231 nasm_zero(*lfree);
H. Peter Anvin98578072018-06-01 18:02:54 -0700232 lfree->defn.label = perm_copy(label);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700233 lfree->defn.subsection = NO_SEG;
H. Peter Anvin98578072018-06-01 18:02:54 -0700234 if (label_str)
235 nasm_free(label_str);
H. Peter Anvin70653092007-10-19 14:42:29 -0700236
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700237 hash_add(&ip, lfree->defn.label, lfree);
238 return lfree++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000239}
240
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700241bool lookup_label(const char *label, int32_t *segment, int64_t *offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000242{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000243 union label *lptr;
244
Keith Kaniosb7a89542007-04-12 02:40:54 +0000245 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700246 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000247
H. Peter Anvin98578072018-06-01 18:02:54 -0700248 lptr = find_label(label, false, NULL);
249 if (lptr && lptr->defn.defined) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000250 *segment = lptr->defn.segment;
251 *offset = lptr->defn.offset;
H. Peter Anvin70055962007-10-11 00:05:31 -0700252 return true;
Cyrill Gorcunovd7a64b72010-04-20 15:26:08 +0400253 }
254
255 return false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000256}
257
H. Peter Anvin785ffb92017-03-14 18:41:25 -0700258bool is_extern(const char *label)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000260 union label *lptr;
261
Keith Kaniosb7a89542007-04-12 02:40:54 +0000262 if (!initialized)
H. Peter Anvin70055962007-10-11 00:05:31 -0700263 return false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000264
H. Peter Anvin98578072018-06-01 18:02:54 -0700265 lptr = find_label(label, false, NULL);
266 return lptr && lptr->defn.type == LBL_EXTERN;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000267}
268
H. Peter Anvin98578072018-06-01 18:02:54 -0700269static const char *mangle_strings[] = {"", "", "", ""};
270static bool mangle_string_set[ARRAY_SIZE(mangle_strings)];
271
272/*
273 * Set a prefix or suffix
274 */
275void set_label_mangle(enum mangle_index which, const char *what)
276{
277 if (mangle_string_set[which])
278 return; /* Once set, do not change */
279
280 mangle_strings[which] = perm_copy(what);
281 mangle_string_set[which] = true;
282}
283
284/*
285 * Format a label name with appropriate prefixes and suffixes
286 */
287static const char *mangle_label_name(union label *lptr)
288{
289 const char *prefix;
290 const char *suffix;
291
292 if (likely(lptr->defn.mangled &&
293 lptr->defn.mangled_type == lptr->defn.type))
294 return lptr->defn.mangled; /* Already mangled */
295
296 switch (lptr->defn.type) {
297 case LBL_GLOBAL:
298 case LBL_STATIC:
299 case LBL_EXTERN:
300 prefix = mangle_strings[LM_GPREFIX];
301 suffix = mangle_strings[LM_GSUFFIX];
302 break;
303 case LBL_BACKEND:
304 case LBL_SPECIAL:
305 prefix = suffix = "";
306 break;
307 default:
308 prefix = mangle_strings[LM_LPREFIX];
309 suffix = mangle_strings[LM_LSUFFIX];
310 break;
311 }
312
313 lptr->defn.mangled_type = lptr->defn.type;
314
315 if (!(*prefix) && !(*suffix))
316 lptr->defn.mangled = lptr->defn.label;
317 else
318 lptr->defn.mangled = perm_copy3(prefix, lptr->defn.label, suffix);
319
320 return lptr->defn.mangled;
321}
322
323static void
H. Peter Anvin29695c82018-06-14 17:04:32 -0700324handle_herelabel(union label *lptr, int32_t *segment, int64_t *offset)
H. Peter Anvin892c4812018-05-30 14:43:46 -0700325{
326 int32_t oldseg;
327
328 if (likely(!ofmt->herelabel))
329 return;
330
331 if (unlikely(location.segment == NO_SEG))
332 return;
333
334 oldseg = *segment;
335
336 if (oldseg == location.segment && *offset == location.offset) {
337 /* This label is defined at this location */
338 int32_t newseg;
339
H. Peter Anvin98578072018-06-01 18:02:54 -0700340 nasm_assert(lptr->defn.mangled);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700341 newseg = ofmt->herelabel(lptr->defn.mangled, lptr->defn.type,
342 oldseg, &lptr->defn.subsection);
H. Peter Anvin892c4812018-05-30 14:43:46 -0700343 if (likely(newseg == oldseg))
344 return;
345
346 *segment = newseg;
347 *offset = switch_segment(newseg);
348 }
349}
350
H. Peter Anvin98578072018-06-01 18:02:54 -0700351static bool declare_label_lptr(union label *lptr,
352 enum label_type type, const char *special)
353{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700354 if (special && !special[0])
355 special = NULL;
356
H. Peter Anvin98578072018-06-01 18:02:54 -0700357 if (lptr->defn.type == type ||
358 (pass0 == 0 && lptr->defn.type == LBL_LOCAL)) {
359 lptr->defn.type = type;
360 if (special) {
361 if (!lptr->defn.special)
362 lptr->defn.special = perm_copy(special);
363 else if (nasm_stricmp(lptr->defn.special, special))
364 nasm_error(ERR_NONFATAL,
365 "symbol `%s' has inconsistent attributes `%s' and `%s'",
366 lptr->defn.label, lptr->defn.special, special);
367 }
368 return true;
369 }
370
371 /* EXTERN can be replaced with GLOBAL or COMMON */
372 if (lptr->defn.type == LBL_EXTERN &&
373 (type == LBL_GLOBAL || type == LBL_COMMON)) {
374 lptr->defn.type = type;
375 /* Override special unconditionally */
376 if (special)
377 lptr->defn.special = perm_copy(special);
378 return true;
379 }
380
381 /* GLOBAL or COMMON ignore subsequent EXTERN */
382 if ((lptr->defn.type == LBL_GLOBAL || lptr->defn.type == LBL_COMMON) &&
383 type == LBL_EXTERN) {
384 if (!lptr->defn.special)
385 lptr->defn.special = perm_copy(special);
386 return true;
387 }
388
389 nasm_error(ERR_NONFATAL, "symbol `%s' declared both as %s and %s",
390 lptr->defn.label, types[lptr->defn.type], types[type]);
391
392 return false;
393}
394
395bool declare_label(const char *label, enum label_type type, const char *special)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000396{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397 union label *lptr;
H. Peter Anvin98578072018-06-01 18:02:54 -0700398 bool created;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399
H. Peter Anvin98578072018-06-01 18:02:54 -0700400 lptr = find_label(label, true, &created);
401 return declare_label_lptr(lptr, type, special);
402}
403
404/*
405 * The "normal" argument decides if we should update the local segment
406 * base name or not.
407 */
408void define_label(const char *label, int32_t segment,
409 int64_t offset, bool normal)
410{
411 union label *lptr;
412 bool created, changed;
H. Peter Anvin73482482018-06-11 14:54:14 -0700413 int64_t size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700414
415 /*
416 * Phase errors here can be one of two types: a new label appears,
417 * or the offset changes. Increment global_offset_changed when that
418 * happens, to tell the assembler core to make another pass.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000419 */
H. Peter Anvin98578072018-06-01 18:02:54 -0700420 lptr = find_label(label, true, &created);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000421
H. Peter Anvin98578072018-06-01 18:02:54 -0700422 if (pass0 > 1) {
423 if (created)
Cyrill Gorcunov5dfcab62016-07-18 11:49:47 +0300424 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000426
H. Peter Anvin46c839a2018-06-14 20:00:07 -0700427 if (!segment)
428 segment = lptr->defn.segment ? lptr->defn.segment : seg_alloc();
H. Peter Anvinaf5f9182018-06-14 19:53:45 -0700429
H. Peter Anvin98578072018-06-01 18:02:54 -0700430 if (lptr->defn.defined || lptr->defn.type == LBL_BACKEND) {
431 /* We have seen this on at least one previous pass */
432 mangle_label_name(lptr);
433 handle_herelabel(lptr, &segment, &offset);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000434 }
435
H. Peter Anvin98578072018-06-01 18:02:54 -0700436 if (ismagic(label) && lptr->defn.type == LBL_LOCAL)
437 lptr->defn.type = LBL_SPECIAL;
H. Peter Anvin73482482018-06-11 14:54:14 -0700438
H. Peter Anvin98578072018-06-01 18:02:54 -0700439 if (!islocal(label) && normal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 prevlabel = lptr->defn.label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442
H. Peter Anvin73482482018-06-11 14:54:14 -0700443 if (lptr->defn.type == LBL_COMMON) {
444 size = offset;
445 offset = 0;
446 } else {
447 size = 0; /* This is a hack... */
448 }
449
H. Peter Anvin98578072018-06-01 18:02:54 -0700450 changed = !lptr->defn.defined || lptr->defn.segment != segment ||
H. Peter Anvin73482482018-06-11 14:54:14 -0700451 lptr->defn.offset != offset || lptr->defn.size != size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700452 global_offset_changed += changed;
453
454 /*
455 * This probably should be ERR_NONFATAL, but not quite yet. As a
456 * special case, LBL_SPECIAL symbols are allowed to be changed
457 * even during the last pass.
458 */
459 if (changed && pass0 == 2 && lptr->defn.type != LBL_SPECIAL)
460 nasm_error(ERR_WARNING, "label `%s' changed during code generation",
461 lptr->defn.label);
462
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463 lptr->defn.segment = segment;
H. Peter Anvin98578072018-06-01 18:02:54 -0700464 lptr->defn.offset = offset;
H. Peter Anvin73482482018-06-11 14:54:14 -0700465 lptr->defn.size = size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700466 lptr->defn.defined = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467
H. Peter Anvin98578072018-06-01 18:02:54 -0700468 out_symdef(lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000469}
470
H. Peter Anvin98578072018-06-01 18:02:54 -0700471/*
472 * Define a special backend label
473 */
474void backend_label(const char *label, int32_t segment, int64_t offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000475{
H. Peter Anvin98578072018-06-01 18:02:54 -0700476 if (!declare_label(label, LBL_BACKEND, NULL))
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400477 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700478
H. Peter Anvin98578072018-06-01 18:02:54 -0700479 define_label(label, segment, offset, false);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000480}
481
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700484 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485
H. Peter Anvin3e555482017-04-23 17:02:46 -0700486 ldata = lfree = nasm_malloc(LBLK_SIZE);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700487 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400489 perm_head = perm_tail =
H. Peter Anvin3e555482017-04-23 17:02:46 -0700490 nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 perm_head->next = NULL;
493 perm_head->size = PERMTS_SIZE;
494 perm_head->usage = 0;
495
496 prevlabel = "";
497
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700498 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000499
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 return 0;
501}
502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700505 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700507 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000508
H. Peter Anvin166c2472008-05-28 12:28:58 -0700509 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000510
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700511 lptr = lhold = ldata;
512 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400513 lptr = &lptr[LABEL_BLOCK-1];
514 lptr = lptr->admin.next;
515 nasm_free(lhold);
516 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000517 }
518
519 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000520 perm_tail = perm_head;
521 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523 }
524}
525
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000528 int j;
529
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 for (j = 0; j < LABEL_BLOCK - 1; j++)
531 blk[j].admin.movingon = END_LIST;
532 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
533 blk[LABEL_BLOCK - 1].admin.next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000534}
535
H. Peter Anvin98578072018-06-01 18:02:54 -0700536static char * safe_alloc perm_alloc(size_t len)
537{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700538 char *p;
H. Peter Anvin73482482018-06-11 14:54:14 -0700539
H. Peter Anvin98578072018-06-01 18:02:54 -0700540 if (perm_tail->size - perm_tail->usage < len) {
541 size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE;
542 perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len);
543 perm_tail = perm_tail->next;
544 perm_tail->next = NULL;
545 perm_tail->size = alloc_len;
546 perm_tail->usage = 0;
547 }
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700548 p = perm_tail->data + perm_tail->usage;
H. Peter Anvin98578072018-06-01 18:02:54 -0700549 perm_tail->usage += len;
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700550 return p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700551}
552
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700553static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000554{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700555 char *p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700556 size_t len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557
H. Peter Anvin98578072018-06-01 18:02:54 -0700558 if (!string)
559 return NULL;
H. Peter Anvin565be912009-07-05 22:15:57 -0700560
H. Peter Anvin98578072018-06-01 18:02:54 -0700561 len = strlen(string)+1; /* Include final NUL */
562
563 p = perm_alloc(len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700564 memcpy(p, string, len);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565
566 return p;
567}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700569static char *
H. Peter Anvin98578072018-06-01 18:02:54 -0700570perm_copy3(const char *s1, const char *s2, const char *s3)
571{
572 char *p;
573 size_t l1 = strlen(s1);
574 size_t l2 = strlen(s2);
575 size_t l3 = strlen(s3)+1; /* Include final NUL */
H. Peter Anvin73482482018-06-11 14:54:14 -0700576
H. Peter Anvin98578072018-06-01 18:02:54 -0700577 p = perm_alloc(l1+l2+l3);
578 memcpy(p, s1, l1);
579 memcpy(p+l1, s2, l2);
580 memcpy(p+l1+l2, s3, l3);
581
582 return p;
583}
584
585const char *local_scope(const char *label)
Charles Crayned60059e2008-03-12 22:39:03 -0700586{
587 return islocal(label) ? prevlabel : "";
588}
589
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590/*
591 * Notes regarding bug involving redefinition of external segments.
592 *
593 * Up to and including v0.97, the following code didn't work. From 0.97
594 * developers release 2 onwards, it will generate an error.
595 *
596 * EXTERN extlabel
597 * newlabel EQU extlabel + 1
598 *
599 * The results of allowing this code through are that two import records
600 * are generated, one for 'extlabel' and one for 'newlabel'.
601 *
602 * The reason for this is an inadequacy in the defined interface between
603 * the label manager and the output formats. The problem lies in how the
604 * output format driver tells that a label is an external label for which
605 * a label import record must be produced. Most (all except bin?) produce
606 * the record if the segment number of the label is not one of the internal
607 * segments that the output driver is producing.
608 *
609 * A simple fix to this would be to make the output formats keep track of
610 * which symbols they've produced import records for, and make them not
611 * produce import records for segments that are already defined.
612 *
613 * The best way, which is slightly harder but reduces duplication of code
614 * and should therefore make the entire system smaller and more stable is
615 * to change the interface between assembler, define_label(), and
616 * the output module. The changes that are needed are:
617 *
618 * The semantics of the 'isextern' flag passed to define_label() need
619 * examining. This information may or may not tell us what we need to
620 * know (ie should we be generating an import record at this point for this
621 * label). If these aren't the semantics, the semantics should be changed
622 * to this.
623 *
624 * The output module interface needs changing, so that the `isextern' flag
625 * is passed to the module, so that it can be easily tested for.
626 */