blob: 4c30323a292ec99469d18e8ed6f0fa508e382060 [file] [log] [blame]
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvina7ecf262018-02-06 14:43:07 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvinb20bc732017-03-07 19:23:03 -08004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * 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.
17 *
18 * 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 * Parse and handle assembler directives
36 */
37
38#include "compiler.h"
39
40#include <stdlib.h>
41#include <string.h>
42#include <ctype.h>
43#include <limits.h>
44
45#include "nasm.h"
46#include "nasmlib.h"
47#include "error.h"
48#include "float.h"
49#include "stdscan.h"
50#include "preproc.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080051#include "eval.h"
52#include "assemble.h"
53#include "outform.h"
54#include "listing.h"
55#include "labels.h"
56#include "iflag.h"
57
H. Peter Anvina7ecf262018-02-06 14:43:07 -080058struct cpunames {
59 const char *name;
60 unsigned int level;
61 /* Eventually a table of features */
62};
63
64static iflag_t get_cpu(const char *value)
H. Peter Anvinb20bc732017-03-07 19:23:03 -080065{
66 iflag_t r;
H. Peter Anvina7ecf262018-02-06 14:43:07 -080067 const struct cpunames *cpu;
68 static const struct cpunames cpunames[] = {
69 { "8086", IF_8086 },
70 { "186", IF_186 },
71 { "286", IF_286 },
72 { "386", IF_386 },
73 { "486", IF_486 },
74 { "586", IF_PENT },
75 { "pentium", IF_PENT },
76 { "pentiummmx", IF_PENT },
77 { "686", IF_P6 },
78 { "p6", IF_P6 },
79 { "ppro", IF_P6 },
80 { "pentiumpro", IF_P6 },
81 { "p2", IF_P6 }, /* +MMX */
82 { "pentiumii", IF_P6 },
83 { "p3", IF_KATMAI },
84 { "katmai", IF_KATMAI },
85 { "p4", IF_WILLAMETTE },
86 { "willamette", IF_WILLAMETTE },
87 { "prescott", IF_PRESCOTT },
88 { "x64", IF_X86_64 },
89 { "x86-64", IF_X86_64 },
90 { "ia64", IF_IA64 },
91 { "ia-64", IF_IA64 },
92 { "itanium", IF_IA64 },
93 { "itanic", IF_IA64 },
94 { "merced", IF_IA64 },
95 { "any", IF_PLEVEL },
96 { "default", IF_PLEVEL },
97 { "all", IF_PLEVEL },
98 { NULL, IF_PLEVEL } /* Error and final default entry */
99 };
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800100
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800101 for (cpu = cpunames; cpu->name; cpu++) {
102 if (!strcmp(value, cpu->name))
103 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800104 }
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800105
106 if (!cpu->name) {
107 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
108 "unknown 'cpu' type '%s'", value);
109 }
110
111 iflag_set_cpu(&r, cpu->level);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800112 return r;
113}
114
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800115static int get_bits(const char *value)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800116{
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800117 int i = atoi(value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800118
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800119 switch (i) {
120 case 16:
121 break; /* Always safe */
122 case 32:
123 if (!iflag_cpu_level_ok(&cpu, IF_386)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124 nasm_error(ERR_NONFATAL,
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800125 "cannot specify 32-bit segment on processor below a 386");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800126 i = 16;
127 }
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800128 break;
129 case 64:
130 if (!iflag_cpu_level_ok(&cpu, IF_X86_64)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800131 nasm_error(ERR_NONFATAL,
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800132 "cannot specify 64-bit segment on processor below an x86-64");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800133 i = 16;
134 }
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800135 break;
136 default:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800137 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800138 "`%s' is not a valid segment size; must be 16, 32 or 64",
139 value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800140 i = 16;
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800141 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800142 }
143 return i;
144}
145
H. Peter Anvin5253f582017-04-03 00:09:58 -0700146static enum directive parse_directive_line(char **directive, char **value)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800147{
148 char *p, *q, *buf;
149
150 buf = nasm_skip_spaces(*directive);
151
152 /*
153 * It should be enclosed in [ ].
154 * XXX: we don't check there is nothing else on the remainder of the
155 * line, except a possible comment.
156 */
157 if (*buf != '[')
158 return D_none;
159 q = strchr(buf, ']');
160 if (!q)
161 return D_corrupt;
162
163 /*
164 * Strip off the comments. XXX: this doesn't account for quoted
165 * strings inside a directive. We should really strip the
166 * comments in generic code, not here. While we're at it, it
167 * would be better to pass the backend a series of tokens instead
168 * of a raw string, and actually process quoted strings for it,
169 * like of like argv is handled in C.
170 */
171 p = strchr(buf, ';');
172 if (p) {
173 if (p < q) /* ouch! somewhere inside */
174 return D_corrupt;
175 *p = '\0';
176 }
177
178 /* no brace, no trailing spaces */
179 *q = '\0';
180 nasm_zap_spaces_rev(--q);
181
182 /* directive */
183 p = nasm_skip_spaces(++buf);
184 q = nasm_skip_word(p);
185 if (!q)
186 return D_corrupt; /* sigh... no value there */
187 *q = '\0';
188 *directive = p;
189
190 /* and value finally */
191 p = nasm_skip_spaces(++q);
192 *value = p;
193
H. Peter Anvin5253f582017-04-03 00:09:58 -0700194 return directive_find(*directive);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800195}
196
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800197/*
198 * Process a line from the assembler and try to handle it if it
199 * is a directive. Return true if the line was handled (including
200 * if it was an error), false otherwise.
201 */
202bool process_directives(char *directive)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800203{
H. Peter Anvin5253f582017-04-03 00:09:58 -0700204 enum directive d;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800205 char *value, *p, *q, *special;
206 struct tokenval tokval;
207 bool bad_param = false;
208 int pass2 = passn > 1 ? 2 : 1;
209
210 d = parse_directive_line(&directive, &value);
211
212 switch (d) {
213 case D_none:
214 return D_none; /* Not a directive */
215
216 case D_corrupt:
217 nasm_error(ERR_NONFATAL, "invalid directive line");
218 break;
219
220 default: /* It's a backend-specific directive */
H. Peter Anvine562b702017-03-07 22:40:00 -0800221 switch (ofmt->directive(d, value, pass2)) {
222 case DIRR_UNKNOWN:
223 goto unknown;
224 case DIRR_OK:
225 case DIRR_ERROR:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800226 break;
H. Peter Anvine562b702017-03-07 22:40:00 -0800227 case DIRR_BADPARAM:
228 bad_param = true;
229 break;
230 default:
231 panic();
232 }
233 break;
234
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800235 case D_unknown:
H. Peter Anvine562b702017-03-07 22:40:00 -0800236 unknown:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800237 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
238 "unrecognised directive [%s]", directive);
239 break;
240
241 case D_SEGMENT: /* [SEGMENT n] */
242 case D_SECTION:
243 {
H. Peter Anvine8001272017-09-27 14:22:16 -0700244 int sb = globalbits;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800245 int32_t seg = ofmt->section(value, pass2, &sb);
246
247 if (seg == NO_SEG) {
248 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
249 "segment name `%s' not recognized", value);
250 } else {
251 in_absolute = false;
252 location.segment = seg;
H. Peter Anvine8001272017-09-27 14:22:16 -0700253 globalbits = sb;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800254 }
255 break;
256 }
257
258 case D_SECTALIGN: /* [SECTALIGN n] */
259 {
260 expr *e;
261
262 if (*value) {
263 stdscan_reset();
264 stdscan_set(value);
265 tokval.t_type = TOKEN_INVALID;
266 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
267 if (e) {
268 uint64_t align = e->value;
269
270 if (!is_power2(e->value)) {
271 nasm_error(ERR_NONFATAL,
272 "segment alignment `%s' is not power of two",
273 value);
274 } else if (align > UINT64_C(0x7fffffff)) {
275 /*
276 * FIXME: Please make some sane message here
277 * ofmt should have some 'check' method which
278 * would report segment alignment bounds.
279 */
280 nasm_error(ERR_NONFATAL,
281 "absurdly large segment alignment `%s' (2^%d)",
282 value, ilog2_64(align));
283 }
284
285 /* callee should be able to handle all details */
286 if (location.segment != NO_SEG)
287 ofmt->sectalign(location.segment, align);
288 }
289 }
290 break;
291 }
292
293 case D_EXTERN: /* [EXTERN label:special] */
294 if (*value == '$')
295 value++; /* skip initial $ if present */
296 if (pass0 == 2) {
297 q = value;
298 while (*q && *q != ':')
299 q++;
300 if (*q == ':') {
301 *q++ = '\0';
302 ofmt->symdef(value, 0L, 0L, 3, q);
303 }
304 } else if (passn == 1) {
305 bool validid = true;
306 q = value;
307 if (!isidstart(*q))
308 validid = false;
309 while (*q && *q != ':') {
310 if (!isidchar(*q))
311 validid = false;
312 q++;
313 }
314 if (!validid) {
315 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
316 break;
317 }
318 if (*q == ':') {
319 *q++ = '\0';
320 special = q;
321 } else
322 special = NULL;
323 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
324 int temp = pass0;
325 pass0 = 1; /* fake pass 1 in labels.c */
326 declare_as_global(value, special);
327 define_label(value, seg_alloc(), 0L, NULL,
328 false, true);
329 pass0 = temp;
330 }
331 } /* else pass0 == 1 */
332 break;
333
334 case D_BITS: /* [BITS bits] */
335 globalbits = get_bits(value);
336 break;
337
338 case D_GLOBAL: /* [GLOBAL symbol:special] */
339 if (*value == '$')
340 value++; /* skip initial $ if present */
341 if (pass0 == 2) { /* pass 2 */
342 q = value;
343 while (*q && *q != ':')
344 q++;
345 if (*q == ':') {
346 *q++ = '\0';
347 ofmt->symdef(value, 0L, 0L, 3, q);
348 }
349 } else if (pass2 == 1) { /* pass == 1 */
350 bool validid = true;
351
352 q = value;
353 if (!isidstart(*q))
354 validid = false;
355 while (*q && *q != ':') {
356 if (!isidchar(*q))
357 validid = false;
358 q++;
359 }
360 if (!validid) {
361 nasm_error(ERR_NONFATAL,
362 "identifier expected after GLOBAL");
363 break;
364 }
365 if (*q == ':') {
366 *q++ = '\0';
367 special = q;
368 } else
369 special = NULL;
370 declare_as_global(value, special);
371 } /* pass == 1 */
372 break;
373
374 case D_COMMON: /* [COMMON symbol size:special] */
375 {
376 int64_t size;
377 bool rn_error;
378 bool validid;
379
380 if (*value == '$')
381 value++; /* skip initial $ if present */
382 p = value;
383 validid = true;
384 if (!isidstart(*p))
385 validid = false;
386 while (*p && !nasm_isspace(*p)) {
387 if (!isidchar(*p))
388 validid = false;
389 p++;
390 }
391 if (!validid) {
392 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
393 break;
394 }
395 if (*p) {
396 p = nasm_zap_spaces_fwd(p);
397 q = p;
398 while (*q && *q != ':')
399 q++;
400 if (*q == ':') {
401 *q++ = '\0';
402 special = q;
403 } else {
404 special = NULL;
405 }
406 size = readnum(p, &rn_error);
407 if (rn_error) {
408 nasm_error(ERR_NONFATAL,
409 "invalid size specified"
410 " in COMMON declaration");
411 break;
412 }
413 } else {
414 nasm_error(ERR_NONFATAL,
415 "no size specified in"
416 " COMMON declaration");
417 break;
418 }
419
420 if (pass0 < 2) {
421 define_common(value, seg_alloc(), size, special);
422 } else if (pass0 == 2) {
423 if (special)
424 ofmt->symdef(value, 0L, 0L, 3, special);
425 }
426 break;
427 }
428
429 case D_ABSOLUTE: /* [ABSOLUTE address] */
430 {
431 expr *e;
432
433 stdscan_reset();
434 stdscan_set(value);
435 tokval.t_type = TOKEN_INVALID;
436 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
437 if (e) {
438 if (!is_reloc(e))
439 nasm_error(pass0 ==
440 1 ? ERR_NONFATAL : ERR_PANIC,
441 "cannot use non-relocatable expression as "
442 "ABSOLUTE address");
443 else {
444 absolute.segment = reloc_seg(e);
445 absolute.offset = reloc_value(e);
446 }
447 } else if (passn == 1)
448 absolute.offset = 0x100; /* don't go near zero in case of / */
449 else
450 nasm_panic(0, "invalid ABSOLUTE address "
451 "in pass two");
452 in_absolute = true;
453 location.segment = NO_SEG;
454 break;
455 }
456
457 case D_DEBUG: /* [DEBUG] */
458 {
459 bool badid, overlong;
460 char debugid[128];
461
462 p = value;
463 q = debugid;
464 badid = overlong = false;
465 if (!isidstart(*p)) {
466 badid = true;
467 } else {
468 while (*p && !nasm_isspace(*p)) {
469 if (q >= debugid + sizeof debugid - 1) {
470 overlong = true;
471 break;
472 }
473 if (!isidchar(*p))
474 badid = true;
475 *q++ = *p++;
476 }
477 *q = 0;
478 }
479 if (badid) {
480 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
481 "identifier expected after DEBUG");
482 break;
483 }
484 if (overlong) {
485 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
486 "DEBUG identifier too long");
487 break;
488 }
489 p = nasm_skip_spaces(p);
490 if (pass0 == 2)
491 dfmt->debug_directive(debugid, p);
492 break;
493 }
494
495 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800496 if (!set_warning_status(value)) {
497 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
498 "unknown warning option: %s", value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800499 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800500 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800501
502 case D_CPU: /* [CPU] */
503 cpu = get_cpu(value);
504 break;
505
506 case D_LIST: /* [LIST {+|-}] */
507 value = nasm_skip_spaces(value);
508 if (*value == '+') {
509 user_nolist = false;
510 } else {
511 if (*value == '-') {
512 user_nolist = true;
513 } else {
514 bad_param = true;
515 }
516 }
517 break;
518
519 case D_DEFAULT: /* [DEFAULT] */
520 stdscan_reset();
521 stdscan_set(value);
522 tokval.t_type = TOKEN_INVALID;
523 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
524 switch (tokval.t_integer) {
525 case S_REL:
526 globalrel = 1;
527 break;
528 case S_ABS:
529 globalrel = 0;
530 break;
531 case P_BND:
532 globalbnd = 1;
533 break;
534 case P_NOBND:
535 globalbnd = 0;
536 break;
537 default:
538 bad_param = true;
539 break;
540 }
541 } else {
542 bad_param = true;
543 }
544 break;
545
546 case D_FLOAT:
547 if (float_option(value)) {
548 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
549 "unknown 'float' directive: %s", value);
550 }
551 break;
552
553 case D_PRAGMA:
554 process_pragma(value);
555 break;
556 }
557
558
559 /* A common error message */
560 if (bad_param) {
561 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
562 directive);
563 }
564
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800565 return d != D_none;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800566}