blob: ccdedace19efd39ec828daf673acff94be19cf04 [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)
149 ofmt->symdef(lptr->defn.label, 0, 0, 3, lptr->defn.special);
150 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 Anvinaf5f9182018-06-14 19:53:45 -0700427 if (!segment) {
428 segment = lptr->defn.segment;
429 if (!segment)
430 segment = lptr->defn.segment = seg_alloc();
431 }
432
H. Peter Anvin98578072018-06-01 18:02:54 -0700433 if (lptr->defn.defined || lptr->defn.type == LBL_BACKEND) {
434 /* We have seen this on at least one previous pass */
435 mangle_label_name(lptr);
436 handle_herelabel(lptr, &segment, &offset);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437 }
438
H. Peter Anvin98578072018-06-01 18:02:54 -0700439 if (ismagic(label) && lptr->defn.type == LBL_LOCAL)
440 lptr->defn.type = LBL_SPECIAL;
H. Peter Anvin73482482018-06-11 14:54:14 -0700441
H. Peter Anvin98578072018-06-01 18:02:54 -0700442 if (!islocal(label) && normal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 prevlabel = lptr->defn.label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000445
H. Peter Anvin73482482018-06-11 14:54:14 -0700446 if (lptr->defn.type == LBL_COMMON) {
447 size = offset;
448 offset = 0;
449 } else {
450 size = 0; /* This is a hack... */
451 }
452
H. Peter Anvin98578072018-06-01 18:02:54 -0700453 changed = !lptr->defn.defined || lptr->defn.segment != segment ||
H. Peter Anvin73482482018-06-11 14:54:14 -0700454 lptr->defn.offset != offset || lptr->defn.size != size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700455 global_offset_changed += changed;
456
457 /*
458 * This probably should be ERR_NONFATAL, but not quite yet. As a
459 * special case, LBL_SPECIAL symbols are allowed to be changed
460 * even during the last pass.
461 */
462 if (changed && pass0 == 2 && lptr->defn.type != LBL_SPECIAL)
463 nasm_error(ERR_WARNING, "label `%s' changed during code generation",
464 lptr->defn.label);
465
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000466 lptr->defn.segment = segment;
H. Peter Anvin98578072018-06-01 18:02:54 -0700467 lptr->defn.offset = offset;
H. Peter Anvin73482482018-06-11 14:54:14 -0700468 lptr->defn.size = size;
H. Peter Anvin98578072018-06-01 18:02:54 -0700469 lptr->defn.defined = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000470
H. Peter Anvin98578072018-06-01 18:02:54 -0700471 out_symdef(lptr);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000472}
473
H. Peter Anvin98578072018-06-01 18:02:54 -0700474/*
475 * Define a special backend label
476 */
477void backend_label(const char *label, int32_t segment, int64_t offset)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478{
H. Peter Anvin98578072018-06-01 18:02:54 -0700479 if (!declare_label(label, LBL_BACKEND, NULL))
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400480 return;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -0700481
H. Peter Anvin98578072018-06-01 18:02:54 -0700482 define_label(label, segment, offset, false);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483}
484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485int init_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000486{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700487 hash_init(&ltab, HASH_LARGE);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488
H. Peter Anvin3e555482017-04-23 17:02:46 -0700489 ldata = lfree = nasm_malloc(LBLK_SIZE);
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700490 init_block(lfree);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400492 perm_head = perm_tail =
H. Peter Anvin3e555482017-04-23 17:02:46 -0700493 nasm_malloc(sizeof(struct permts));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000494
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495 perm_head->next = NULL;
496 perm_head->size = PERMTS_SIZE;
497 perm_head->usage = 0;
498
499 prevlabel = "";
500
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700501 initialized = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000502
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000503 return 0;
504}
505
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506void cleanup_labels(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000507{
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700508 union label *lptr, *lhold;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000509
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700510 initialized = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000511
H. Peter Anvin166c2472008-05-28 12:28:58 -0700512 hash_free(&ltab);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000513
H. Peter Anvin6244f4b2007-09-14 18:03:29 -0700514 lptr = lhold = ldata;
515 while (lptr) {
Cyrill Gorcunovd143f4f2010-07-28 10:18:48 +0400516 lptr = &lptr[LABEL_BLOCK-1];
517 lptr = lptr->admin.next;
518 nasm_free(lhold);
519 lhold = lptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520 }
521
522 while (perm_head) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000523 perm_tail = perm_head;
524 perm_head = perm_head->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 nasm_free(perm_tail);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000526 }
527}
528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529static void init_block(union label *blk)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000530{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000531 int j;
532
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 for (j = 0; j < LABEL_BLOCK - 1; j++)
534 blk[j].admin.movingon = END_LIST;
535 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
536 blk[LABEL_BLOCK - 1].admin.next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000537}
538
H. Peter Anvin98578072018-06-01 18:02:54 -0700539static char * safe_alloc perm_alloc(size_t len)
540{
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700541 char *p;
H. Peter Anvin73482482018-06-11 14:54:14 -0700542
H. Peter Anvin98578072018-06-01 18:02:54 -0700543 if (perm_tail->size - perm_tail->usage < len) {
544 size_t alloc_len = (len > PERMTS_SIZE) ? len : PERMTS_SIZE;
545 perm_tail->next = nasm_malloc(PERMTS_HEADER + alloc_len);
546 perm_tail = perm_tail->next;
547 perm_tail->next = NULL;
548 perm_tail->size = alloc_len;
549 perm_tail->usage = 0;
550 }
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700551 p = perm_tail->data + perm_tail->usage;
H. Peter Anvin98578072018-06-01 18:02:54 -0700552 perm_tail->usage += len;
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700553 return p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700554}
555
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700556static char *perm_copy(const char *string)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000557{
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700558 char *p;
H. Peter Anvin98578072018-06-01 18:02:54 -0700559 size_t len;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000560
H. Peter Anvin98578072018-06-01 18:02:54 -0700561 if (!string)
562 return NULL;
H. Peter Anvin565be912009-07-05 22:15:57 -0700563
H. Peter Anvin98578072018-06-01 18:02:54 -0700564 len = strlen(string)+1; /* Include final NUL */
565
566 p = perm_alloc(len);
H. Peter Anvind2fb7a62007-09-16 17:53:17 -0700567 memcpy(p, string, len);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568
569 return p;
570}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700572static char *
H. Peter Anvin98578072018-06-01 18:02:54 -0700573perm_copy3(const char *s1, const char *s2, const char *s3)
574{
575 char *p;
576 size_t l1 = strlen(s1);
577 size_t l2 = strlen(s2);
578 size_t l3 = strlen(s3)+1; /* Include final NUL */
H. Peter Anvin73482482018-06-11 14:54:14 -0700579
H. Peter Anvin98578072018-06-01 18:02:54 -0700580 p = perm_alloc(l1+l2+l3);
581 memcpy(p, s1, l1);
582 memcpy(p+l1, s2, l2);
583 memcpy(p+l1+l2, s3, l3);
584
585 return p;
586}
587
588const char *local_scope(const char *label)
Charles Crayned60059e2008-03-12 22:39:03 -0700589{
590 return islocal(label) ? prevlabel : "";
591}
592
H. Peter Anvineba20a72002-04-30 20:53:55 +0000593/*
594 * Notes regarding bug involving redefinition of external segments.
595 *
596 * Up to and including v0.97, the following code didn't work. From 0.97
597 * developers release 2 onwards, it will generate an error.
598 *
599 * EXTERN extlabel
600 * newlabel EQU extlabel + 1
601 *
602 * The results of allowing this code through are that two import records
603 * are generated, one for 'extlabel' and one for 'newlabel'.
604 *
605 * The reason for this is an inadequacy in the defined interface between
606 * the label manager and the output formats. The problem lies in how the
607 * output format driver tells that a label is an external label for which
608 * a label import record must be produced. Most (all except bin?) produce
609 * the record if the segment number of the label is not one of the internal
610 * segments that the output driver is producing.
611 *
612 * A simple fix to this would be to make the output formats keep track of
613 * which symbols they've produced import records for, and make them not
614 * produce import records for segments that are already defined.
615 *
616 * The best way, which is slightly harder but reduces duplication of code
617 * and should therefore make the entire system smaller and more stable is
618 * to change the interface between assembler, define_label(), and
619 * the output module. The changes that are needed are:
620 *
621 * The semantics of the 'isextern' flag passed to define_label() need
622 * examining. This information may or may not tell us what we need to
623 * know (ie should we be generating an import record at this point for this
624 * label). If these aren't the semantics, the semantics should be changed
625 * to this.
626 *
627 * The output module interface needs changing, so that the `isextern' flag
628 * is passed to the module, so that it can be easily tested for.
629 */