1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/* labels.c label handling for the Netwide Assembler
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "nasm.h"
#include "nasmlib.h"
#include "hashtbl.h"
/*
* A local label is one that begins with exactly one period. Things
* that begin with _two_ periods are NASM-specific things.
*
* If TASM compatibility is enabled, a local label can also begin with
* @@, so @@local is a TASM compatible local label. Note that we only
* check for the first @ symbol, although TASM requires both.
*/
#define islocal(l) \
(tasm_compatible_mode ? \
(((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
((l)[0] == '.' && (l)[1] != '.'))
#define islocalchar(c) \
(tasm_compatible_mode ? \
((c) == '.' || (c) == '@') : \
((c) == '.'))
#define LABEL_BLOCK 128 /* no. of labels/block */
#define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
#define END_LIST -3 /* don't clash with NO_SEG! */
#define END_BLOCK -2
#define BOGUS_VALUE -4
#define PERMTS_SIZE 4096 /* size of text blocks */
#if (PERMTS_SIZE > IDLEN_MAX)
#error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
#endif
/* values for label.defn.is_global */
#define DEFINED_BIT 1
#define GLOBAL_BIT 2
#define EXTERN_BIT 4
#define NOT_DEFINED_YET 0
#define TYPE_MASK 3
#define LOCAL_SYMBOL (DEFINED_BIT)
#define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
#define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
union label { /* actual label structures */
struct {
int32_t segment, offset;
char *label, *special;
int is_global, is_norm;
} defn;
struct {
int32_t movingon, dummy;
union label *next;
} admin;
};
struct permts { /* permanent text storage */
struct permts *next; /* for the linked list */
int size, usage; /* size and used space in ... */
char data[PERMTS_SIZE]; /* ... the data block itself */
};
extern int global_offset_changed; /* defined in nasm.c */
static struct hash_table *ltab; /* labels hash table */
static union label *ldata; /* all label data blocks */
static union label *lfree; /* labels free block */
static struct permts *perm_head; /* start of perm. text storage */
static struct permts *perm_tail; /* end of perm. text storage */
static void init_block(union label *blk);
static char *perm_copy(char *string1, char *string2);
static char *prevlabel;
static int initialized = FALSE;
char lprefix[PREFIX_MAX] = { 0 };
char lpostfix[PREFIX_MAX] = { 0 };
/*
* Internal routine: finds the `union label' corresponding to the
* given label name. Creates a new one, if it isn't found, and if
* `create' is TRUE.
*/
static union label *find_label(char *label, int create)
{
char *prev;
int prevlen, len;
union label *lptr;
char label_str[IDLEN_MAX];
struct hash_insert ip;
if (islocal(label)) {
prev = prevlabel;
prevlen = strlen(prev);
len = strlen(label);
if (prevlen+len >= IDLEN_MAX)
return NULL; /* Error... */
memcpy(label_str, prev, prevlen);
memcpy(label_str+prevlen, label, len);
label_str[len += prevlen] = '\0';
label = label_str;
} else {
prev = "";
prevlen = 0;
}
lptr = hash_find(ltab, label, &ip);
if (lptr || !create)
return lptr;
/* Create a new label... */
if (lfree->admin.movingon == END_BLOCK) {
/*
* must allocate a new block
*/
lfree->admin.next =
(union label *)nasm_malloc(LBLK_SIZE);
lfree = lfree->admin.next;
init_block(lfree);
}
lfree->admin.movingon = BOGUS_VALUE;
lfree->defn.label = perm_copy(prev, label);
lfree->defn.special = NULL;
lfree->defn.is_global = NOT_DEFINED_YET;
hash_add(&ip, lfree->defn.label, lfree);
return lfree++;
}
int lookup_label(char *label, int32_t *segment, int32_t *offset)
{
union label *lptr;
if (!initialized)
return 0;
lptr = find_label(label, 0);
if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
*segment = lptr->defn.segment;
*offset = lptr->defn.offset;
return 1;
} else
return 0;
}
int is_extern(char *label)
{
union label *lptr;
if (!initialized)
return 0;
lptr = find_label(label, 0);
if (lptr && (lptr->defn.is_global & EXTERN_BIT))
return 1;
else
return 0;
}
void redefine_label(char *label, int32_t segment, int32_t offset, char *special,
int is_norm, int isextrn, struct ofmt *ofmt,
efunc error)
{
union label *lptr;
int exi;
/* This routine possibly ought to check for phase errors. Most assemblers
* check for phase errors at this point. I don't know whether phase errors
* are even possible, nor whether they are checked somewhere else
*/
(void)segment; /* Don't warn that this parameter is unused */
(void)special; /* Don't warn that this parameter is unused */
(void)is_norm; /* Don't warn that this parameter is unused */
(void)isextrn; /* Don't warn that this parameter is unused */
(void)ofmt; /* Don't warn that this parameter is unused */
#ifdef DEBUG
#if DEBUG<3
if (!strncmp(label, "debugdump", 9))
#endif
error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
label, segment, offset, special, is_norm, isextrn);
#endif
lptr = find_label(label, 1);
if (!lptr)
error(ERR_PANIC, "can't find label `%s' on pass two", label);
if (!islocal(label)) {
if (!islocalchar(*label) && lptr->defn.is_norm)
prevlabel = lptr->defn.label;
}
global_offset_changed |= (lptr->defn.offset != offset);
lptr->defn.offset = offset;
if (pass0 == 1) {
exi = !!(lptr->defn.is_global & GLOBAL_BIT);
if (exi) {
char *xsymbol;
int slen;
slen = strlen(lprefix);
slen += strlen(lptr->defn.label);
slen += strlen(lpostfix);
slen++; /* room for that null char */
xsymbol = nasm_malloc(slen);
snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
lpostfix);
ofmt->symdef(xsymbol, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
exi,
special ? special : lptr->
defn.special);
/** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
} else {
if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
EXTERN_BIT) {
ofmt->symdef(lptr->defn.label, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(label, segment, offset,
exi,
special ? special :
lptr->defn.special);
}
}
}
/* if (pass0 == 1) */
}
void define_label(char *label, int32_t segment, int32_t offset, char *special,
int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
{
union label *lptr;
int exi;
#ifdef DEBUG
#if DEBUG<3
if (!strncmp(label, "debugdump", 9))
#endif
error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
label, segment, offset, special, is_norm, isextrn);
#endif
lptr = find_label(label, 1);
if (lptr->defn.is_global & DEFINED_BIT) {
error(ERR_NONFATAL, "symbol `%s' redefined", label);
return;
}
lptr->defn.is_global |= DEFINED_BIT;
if (isextrn)
lptr->defn.is_global |= EXTERN_BIT;
if (!islocalchar(label[0]) && is_norm) /* not local, but not special either */
prevlabel = lptr->defn.label;
else if (islocal(label) && !*prevlabel) {
error(ERR_NONFATAL, "attempt to define a local label before any"
" non-local labels");
}
lptr->defn.segment = segment;
lptr->defn.offset = offset;
lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
if (pass0 == 1 || (!is_norm && !isextrn && (segment & 1))) {
exi = !!(lptr->defn.is_global & GLOBAL_BIT);
if (exi) {
char *xsymbol;
int slen;
slen = strlen(lprefix);
slen += strlen(lptr->defn.label);
slen += strlen(lpostfix);
slen++; /* room for that null char */
xsymbol = nasm_malloc(slen);
snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
lpostfix);
ofmt->symdef(xsymbol, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
exi,
special ? special : lptr->
defn.special);
/** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
} else {
if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
EXTERN_BIT) {
ofmt->symdef(lptr->defn.label, segment, offset, exi,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(label, segment, offset,
exi,
special ? special :
lptr->defn.special);
}
}
} /* if (pass0 == 1) */
}
void define_common(char *label, int32_t segment, int32_t size, char *special,
struct ofmt *ofmt, efunc error)
{
union label *lptr;
lptr = find_label(label, 1);
if (lptr->defn.is_global & DEFINED_BIT) {
error(ERR_NONFATAL, "symbol `%s' redefined", label);
return;
}
lptr->defn.is_global |= DEFINED_BIT;
if (!islocalchar(label[0])) /* not local, but not special either */
prevlabel = lptr->defn.label;
else
error(ERR_NONFATAL, "attempt to define a local label as a "
"common variable");
lptr->defn.segment = segment;
lptr->defn.offset = 0;
ofmt->symdef(lptr->defn.label, segment, size, 2,
special ? special : lptr->defn.special);
ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
special ? special : lptr->defn.
special);
}
void declare_as_global(char *label, char *special, efunc error)
{
union label *lptr;
if (islocal(label)) {
error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
" global", label);
return;
}
lptr = find_label(label, 1);
switch (lptr->defn.is_global & TYPE_MASK) {
case NOT_DEFINED_YET:
lptr->defn.is_global = GLOBAL_PLACEHOLDER;
lptr->defn.special = special ? perm_copy(special, "") : NULL;
break;
case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
case GLOBAL_SYMBOL:
break;
case LOCAL_SYMBOL:
if (!lptr->defn.is_global & EXTERN_BIT)
error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
" appear before symbol definition", label);
break;
}
}
int init_labels(void)
{
ltab = hash_init();
ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
init_block(lfree);
perm_head =
perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
perm_head->next = NULL;
perm_head->size = PERMTS_SIZE;
perm_head->usage = 0;
prevlabel = "";
initialized = TRUE;
return 0;
}
static void cleanup_hashed_label(char *key, void *data)
{
/* The key is part of the permanent string storage */
/* The data is part of the ldata chain */
(void)key; (void)data;
}
void cleanup_labels(void)
{
union label *lptr, *lhold;
initialized = FALSE;
hash_free(ltab, cleanup_hashed_label);
lptr = lhold = ldata;
while (lptr) {
lptr = &lptr[LABEL_BLOCK-1];
lptr = lptr->admin.next;
nasm_free(lhold);
lhold = lptr;
}
while (perm_head) {
perm_tail = perm_head;
perm_head = perm_head->next;
nasm_free(perm_tail);
}
}
static void init_block(union label *blk)
{
int j;
for (j = 0; j < LABEL_BLOCK - 1; j++)
blk[j].admin.movingon = END_LIST;
blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
blk[LABEL_BLOCK - 1].admin.next = NULL;
}
static char *perm_copy(char *string1, char *string2)
{
char *p, *q;
int len = strlen(string1) + strlen(string2) + 1;
if (perm_tail->size - perm_tail->usage < len) {
perm_tail->next =
(struct permts *)nasm_malloc(sizeof(struct permts));
perm_tail = perm_tail->next;
perm_tail->next = NULL;
perm_tail->size = PERMTS_SIZE;
perm_tail->usage = 0;
}
p = q = perm_tail->data + perm_tail->usage;
while ((*q = *string1++))
q++;
while ((*q++ = *string2++)) ;
perm_tail->usage = q - perm_tail->data;
return p;
}
/*
* Notes regarding bug involving redefinition of external segments.
*
* Up to and including v0.97, the following code didn't work. From 0.97
* developers release 2 onwards, it will generate an error.
*
* EXTERN extlabel
* newlabel EQU extlabel + 1
*
* The results of allowing this code through are that two import records
* are generated, one for 'extlabel' and one for 'newlabel'.
*
* The reason for this is an inadequacy in the defined interface between
* the label manager and the output formats. The problem lies in how the
* output format driver tells that a label is an external label for which
* a label import record must be produced. Most (all except bin?) produce
* the record if the segment number of the label is not one of the internal
* segments that the output driver is producing.
*
* A simple fix to this would be to make the output formats keep track of
* which symbols they've produced import records for, and make them not
* produce import records for segments that are already defined.
*
* The best way, which is slightly harder but reduces duplication of code
* and should therefore make the entire system smaller and more stable is
* to change the interface between assembler, define_label(), and
* the output module. The changes that are needed are:
*
* The semantics of the 'isextern' flag passed to define_label() need
* examining. This information may or may not tell us what we need to
* know (ie should we be generating an import record at this point for this
* label). If these aren't the semantics, the semantics should be changed
* to this.
*
* The output module interface needs changing, so that the `isextern' flag
* is passed to the module, so that it can be easily tested for.
*/
|