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