blob: e8bb42d385f85f96cdc9df0008b1198f724d45dc [file] [log] [blame]
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * 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
58static iflag_t get_cpu(char *value)
59{
60 iflag_t r;
61
62 iflag_clear_all(&r);
63
64 if (!strcmp(value, "8086"))
65 iflag_set(&r, IF_8086);
66 else if (!strcmp(value, "186"))
67 iflag_set(&r, IF_186);
68 else if (!strcmp(value, "286"))
69 iflag_set(&r, IF_286);
70 else if (!strcmp(value, "386"))
71 iflag_set(&r, IF_386);
72 else if (!strcmp(value, "486"))
73 iflag_set(&r, IF_486);
74 else if (!strcmp(value, "586") ||
75 !nasm_stricmp(value, "pentium"))
76 iflag_set(&r, IF_PENT);
77 else if (!strcmp(value, "686") ||
78 !nasm_stricmp(value, "ppro") ||
79 !nasm_stricmp(value, "pentiumpro") ||
80 !nasm_stricmp(value, "p2"))
81 iflag_set(&r, IF_P6);
82 else if (!nasm_stricmp(value, "p3") ||
83 !nasm_stricmp(value, "katmai"))
84 iflag_set(&r, IF_KATMAI);
85 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
86 !nasm_stricmp(value, "willamette"))
87 iflag_set(&r, IF_WILLAMETTE);
88 else if (!nasm_stricmp(value, "prescott"))
89 iflag_set(&r, IF_PRESCOTT);
90 else if (!nasm_stricmp(value, "x64") ||
91 !nasm_stricmp(value, "x86-64"))
92 iflag_set(&r, IF_X86_64);
93 else if (!nasm_stricmp(value, "ia64") ||
94 !nasm_stricmp(value, "ia-64") ||
95 !nasm_stricmp(value, "itanium")||
96 !nasm_stricmp(value, "itanic") ||
97 !nasm_stricmp(value, "merced"))
98 iflag_set(&r, IF_IA64);
99 else {
100 iflag_set(&r, IF_PLEVEL);
101 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
102 "unknown 'cpu' type");
103 }
104 return r;
105}
106
107static int get_bits(char *value)
108{
109 int i;
110
111 if ((i = atoi(value)) == 16)
112 return i; /* set for a 16-bit segment */
113 else if (i == 32) {
114 if (iflag_ffs(&cpu) < IF_386) {
115 nasm_error(ERR_NONFATAL,
116 "cannot specify 32-bit segment on processor below a 386");
117 i = 16;
118 }
119 } else if (i == 64) {
120 if (iflag_ffs(&cpu) < IF_X86_64) {
121 nasm_error(ERR_NONFATAL,
122 "cannot specify 64-bit segment on processor below an x86-64");
123 i = 16;
124 }
125 } else {
126 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
127 "`%s' is not a valid segment size; must be 16, 32 or 64",
128 value);
129 i = 16;
130 }
131 return i;
132}
133
134static enum directives parse_directive_line(char **directive, char **value)
135{
136 char *p, *q, *buf;
137
138 buf = nasm_skip_spaces(*directive);
139
140 /*
141 * It should be enclosed in [ ].
142 * XXX: we don't check there is nothing else on the remainder of the
143 * line, except a possible comment.
144 */
145 if (*buf != '[')
146 return D_none;
147 q = strchr(buf, ']');
148 if (!q)
149 return D_corrupt;
150
151 /*
152 * Strip off the comments. XXX: this doesn't account for quoted
153 * strings inside a directive. We should really strip the
154 * comments in generic code, not here. While we're at it, it
155 * would be better to pass the backend a series of tokens instead
156 * of a raw string, and actually process quoted strings for it,
157 * like of like argv is handled in C.
158 */
159 p = strchr(buf, ';');
160 if (p) {
161 if (p < q) /* ouch! somewhere inside */
162 return D_corrupt;
163 *p = '\0';
164 }
165
166 /* no brace, no trailing spaces */
167 *q = '\0';
168 nasm_zap_spaces_rev(--q);
169
170 /* directive */
171 p = nasm_skip_spaces(++buf);
172 q = nasm_skip_word(p);
173 if (!q)
174 return D_corrupt; /* sigh... no value there */
175 *q = '\0';
176 *directive = p;
177
178 /* and value finally */
179 p = nasm_skip_spaces(++q);
180 *value = p;
181
182 return find_directive(*directive);
183}
184
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800185/*
186 * Process a line from the assembler and try to handle it if it
187 * is a directive. Return true if the line was handled (including
188 * if it was an error), false otherwise.
189 */
190bool process_directives(char *directive)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800191{
192 enum directives d;
193 char *value, *p, *q, *special;
194 struct tokenval tokval;
195 bool bad_param = false;
196 int pass2 = passn > 1 ? 2 : 1;
197
198 d = parse_directive_line(&directive, &value);
199
200 switch (d) {
201 case D_none:
202 return D_none; /* Not a directive */
203
204 case D_corrupt:
205 nasm_error(ERR_NONFATAL, "invalid directive line");
206 break;
207
208 default: /* It's a backend-specific directive */
209 if (ofmt->directive(d, value, pass2))
210 break;
211 /* else fall through */
212 case D_unknown:
213 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
214 "unrecognised directive [%s]", directive);
215 break;
216
217 case D_SEGMENT: /* [SEGMENT n] */
218 case D_SECTION:
219 {
220 int sb;
221 int32_t seg = ofmt->section(value, pass2, &sb);
222
223 if (seg == NO_SEG) {
224 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
225 "segment name `%s' not recognized", value);
226 } else {
227 in_absolute = false;
228 location.segment = seg;
229 }
230 break;
231 }
232
233 case D_SECTALIGN: /* [SECTALIGN n] */
234 {
235 expr *e;
236
237 if (*value) {
238 stdscan_reset();
239 stdscan_set(value);
240 tokval.t_type = TOKEN_INVALID;
241 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
242 if (e) {
243 uint64_t align = e->value;
244
245 if (!is_power2(e->value)) {
246 nasm_error(ERR_NONFATAL,
247 "segment alignment `%s' is not power of two",
248 value);
249 } else if (align > UINT64_C(0x7fffffff)) {
250 /*
251 * FIXME: Please make some sane message here
252 * ofmt should have some 'check' method which
253 * would report segment alignment bounds.
254 */
255 nasm_error(ERR_NONFATAL,
256 "absurdly large segment alignment `%s' (2^%d)",
257 value, ilog2_64(align));
258 }
259
260 /* callee should be able to handle all details */
261 if (location.segment != NO_SEG)
262 ofmt->sectalign(location.segment, align);
263 }
264 }
265 break;
266 }
267
268 case D_EXTERN: /* [EXTERN label:special] */
269 if (*value == '$')
270 value++; /* skip initial $ if present */
271 if (pass0 == 2) {
272 q = value;
273 while (*q && *q != ':')
274 q++;
275 if (*q == ':') {
276 *q++ = '\0';
277 ofmt->symdef(value, 0L, 0L, 3, q);
278 }
279 } else if (passn == 1) {
280 bool validid = true;
281 q = value;
282 if (!isidstart(*q))
283 validid = false;
284 while (*q && *q != ':') {
285 if (!isidchar(*q))
286 validid = false;
287 q++;
288 }
289 if (!validid) {
290 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
291 break;
292 }
293 if (*q == ':') {
294 *q++ = '\0';
295 special = q;
296 } else
297 special = NULL;
298 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
299 int temp = pass0;
300 pass0 = 1; /* fake pass 1 in labels.c */
301 declare_as_global(value, special);
302 define_label(value, seg_alloc(), 0L, NULL,
303 false, true);
304 pass0 = temp;
305 }
306 } /* else pass0 == 1 */
307 break;
308
309 case D_BITS: /* [BITS bits] */
310 globalbits = get_bits(value);
311 break;
312
313 case D_GLOBAL: /* [GLOBAL symbol:special] */
314 if (*value == '$')
315 value++; /* skip initial $ if present */
316 if (pass0 == 2) { /* pass 2 */
317 q = value;
318 while (*q && *q != ':')
319 q++;
320 if (*q == ':') {
321 *q++ = '\0';
322 ofmt->symdef(value, 0L, 0L, 3, q);
323 }
324 } else if (pass2 == 1) { /* pass == 1 */
325 bool validid = true;
326
327 q = value;
328 if (!isidstart(*q))
329 validid = false;
330 while (*q && *q != ':') {
331 if (!isidchar(*q))
332 validid = false;
333 q++;
334 }
335 if (!validid) {
336 nasm_error(ERR_NONFATAL,
337 "identifier expected after GLOBAL");
338 break;
339 }
340 if (*q == ':') {
341 *q++ = '\0';
342 special = q;
343 } else
344 special = NULL;
345 declare_as_global(value, special);
346 } /* pass == 1 */
347 break;
348
349 case D_COMMON: /* [COMMON symbol size:special] */
350 {
351 int64_t size;
352 bool rn_error;
353 bool validid;
354
355 if (*value == '$')
356 value++; /* skip initial $ if present */
357 p = value;
358 validid = true;
359 if (!isidstart(*p))
360 validid = false;
361 while (*p && !nasm_isspace(*p)) {
362 if (!isidchar(*p))
363 validid = false;
364 p++;
365 }
366 if (!validid) {
367 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
368 break;
369 }
370 if (*p) {
371 p = nasm_zap_spaces_fwd(p);
372 q = p;
373 while (*q && *q != ':')
374 q++;
375 if (*q == ':') {
376 *q++ = '\0';
377 special = q;
378 } else {
379 special = NULL;
380 }
381 size = readnum(p, &rn_error);
382 if (rn_error) {
383 nasm_error(ERR_NONFATAL,
384 "invalid size specified"
385 " in COMMON declaration");
386 break;
387 }
388 } else {
389 nasm_error(ERR_NONFATAL,
390 "no size specified in"
391 " COMMON declaration");
392 break;
393 }
394
395 if (pass0 < 2) {
396 define_common(value, seg_alloc(), size, special);
397 } else if (pass0 == 2) {
398 if (special)
399 ofmt->symdef(value, 0L, 0L, 3, special);
400 }
401 break;
402 }
403
404 case D_ABSOLUTE: /* [ABSOLUTE address] */
405 {
406 expr *e;
407
408 stdscan_reset();
409 stdscan_set(value);
410 tokval.t_type = TOKEN_INVALID;
411 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
412 if (e) {
413 if (!is_reloc(e))
414 nasm_error(pass0 ==
415 1 ? ERR_NONFATAL : ERR_PANIC,
416 "cannot use non-relocatable expression as "
417 "ABSOLUTE address");
418 else {
419 absolute.segment = reloc_seg(e);
420 absolute.offset = reloc_value(e);
421 }
422 } else if (passn == 1)
423 absolute.offset = 0x100; /* don't go near zero in case of / */
424 else
425 nasm_panic(0, "invalid ABSOLUTE address "
426 "in pass two");
427 in_absolute = true;
428 location.segment = NO_SEG;
429 break;
430 }
431
432 case D_DEBUG: /* [DEBUG] */
433 {
434 bool badid, overlong;
435 char debugid[128];
436
437 p = value;
438 q = debugid;
439 badid = overlong = false;
440 if (!isidstart(*p)) {
441 badid = true;
442 } else {
443 while (*p && !nasm_isspace(*p)) {
444 if (q >= debugid + sizeof debugid - 1) {
445 overlong = true;
446 break;
447 }
448 if (!isidchar(*p))
449 badid = true;
450 *q++ = *p++;
451 }
452 *q = 0;
453 }
454 if (badid) {
455 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
456 "identifier expected after DEBUG");
457 break;
458 }
459 if (overlong) {
460 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
461 "DEBUG identifier too long");
462 break;
463 }
464 p = nasm_skip_spaces(p);
465 if (pass0 == 2)
466 dfmt->debug_directive(debugid, p);
467 break;
468 }
469
470 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
471 {
472 enum warn_action { WID_OFF, WID_ON, WID_RESET };
473 enum warn_action action;
474 int i;
475
476 value = nasm_skip_spaces(value);
477 switch(*value) {
478 case '-': action = WID_OFF; value++; break;
479 case '+': action = WID_ON; value++; break;
480 case '*': action = WID_RESET; value++; break;
481 default: action = WID_ON; break;
482 }
483
484 for (i = 1; i <= ERR_WARN_MAX; i++)
485 if (!nasm_stricmp(value, warnings[i].name))
486 break;
487 if (i <= ERR_WARN_MAX) {
488 switch (action) {
489 case WID_OFF:
490 warning_on[i] = false;
491 break;
492 case WID_ON:
493 warning_on[i] = true;
494 break;
495 case WID_RESET:
496 warning_on[i] = warning_on_global[i];
497 break;
498 }
499 }
500 break;
501 }
502
503 case D_CPU: /* [CPU] */
504 cpu = get_cpu(value);
505 break;
506
507 case D_LIST: /* [LIST {+|-}] */
508 value = nasm_skip_spaces(value);
509 if (*value == '+') {
510 user_nolist = false;
511 } else {
512 if (*value == '-') {
513 user_nolist = true;
514 } else {
515 bad_param = true;
516 }
517 }
518 break;
519
520 case D_DEFAULT: /* [DEFAULT] */
521 stdscan_reset();
522 stdscan_set(value);
523 tokval.t_type = TOKEN_INVALID;
524 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
525 switch (tokval.t_integer) {
526 case S_REL:
527 globalrel = 1;
528 break;
529 case S_ABS:
530 globalrel = 0;
531 break;
532 case P_BND:
533 globalbnd = 1;
534 break;
535 case P_NOBND:
536 globalbnd = 0;
537 break;
538 default:
539 bad_param = true;
540 break;
541 }
542 } else {
543 bad_param = true;
544 }
545 break;
546
547 case D_FLOAT:
548 if (float_option(value)) {
549 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
550 "unknown 'float' directive: %s", value);
551 }
552 break;
553
554 case D_PRAGMA:
555 process_pragma(value);
556 break;
557 }
558
559
560 /* A common error message */
561 if (bad_param) {
562 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
563 directive);
564 }
565
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800566 return d != D_none;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800567}