blob: fa800b949e4af0abf31afc4b63d1424a6f612a51 [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;
H. Peter Anvin98578072018-06-01 18:02:54 -0700212 enum label_type type;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800213
214 d = parse_directive_line(&directive, &value);
215
216 switch (d) {
217 case D_none:
218 return D_none; /* Not a directive */
219
220 case D_corrupt:
221 nasm_error(ERR_NONFATAL, "invalid directive line");
222 break;
223
224 default: /* It's a backend-specific directive */
H. Peter Anvine562b702017-03-07 22:40:00 -0800225 switch (ofmt->directive(d, value, pass2)) {
226 case DIRR_UNKNOWN:
227 goto unknown;
228 case DIRR_OK:
229 case DIRR_ERROR:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800230 break;
H. Peter Anvine562b702017-03-07 22:40:00 -0800231 case DIRR_BADPARAM:
232 bad_param = true;
233 break;
234 default:
235 panic();
236 }
237 break;
238
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800239 case D_unknown:
H. Peter Anvine562b702017-03-07 22:40:00 -0800240 unknown:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800241 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
242 "unrecognised directive [%s]", directive);
243 break;
244
245 case D_SEGMENT: /* [SEGMENT n] */
246 case D_SECTION:
247 {
H. Peter Anvine8001272017-09-27 14:22:16 -0700248 int sb = globalbits;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800249 int32_t seg = ofmt->section(value, pass2, &sb);
250
251 if (seg == NO_SEG) {
252 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
253 "segment name `%s' not recognized", value);
254 } else {
H. Peter Anvine8001272017-09-27 14:22:16 -0700255 globalbits = sb;
H. Peter Anvin892c4812018-05-30 14:43:46 -0700256 switch_segment(seg);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800257 }
258 break;
259 }
260
261 case D_SECTALIGN: /* [SECTALIGN n] */
262 {
263 expr *e;
264
265 if (*value) {
266 stdscan_reset();
267 stdscan_set(value);
268 tokval.t_type = TOKEN_INVALID;
269 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
270 if (e) {
271 uint64_t align = e->value;
272
273 if (!is_power2(e->value)) {
274 nasm_error(ERR_NONFATAL,
275 "segment alignment `%s' is not power of two",
276 value);
277 } else if (align > UINT64_C(0x7fffffff)) {
278 /*
279 * FIXME: Please make some sane message here
280 * ofmt should have some 'check' method which
281 * would report segment alignment bounds.
282 */
283 nasm_error(ERR_NONFATAL,
284 "absurdly large segment alignment `%s' (2^%d)",
285 value, ilog2_64(align));
286 }
287
288 /* callee should be able to handle all details */
289 if (location.segment != NO_SEG)
290 ofmt->sectalign(location.segment, align);
291 }
292 }
293 break;
294 }
295
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800296 case D_BITS: /* [BITS bits] */
297 globalbits = get_bits(value);
298 break;
299
H. Peter Anvin98578072018-06-01 18:02:54 -0700300 case D_GLOBAL: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
301 type = LBL_GLOBAL;
302 goto symdef;
303 case D_STATIC:
304 type = LBL_STATIC;
305 goto symdef;
306 case D_EXTERN:
307 type = LBL_EXTERN;
308 goto symdef;
309 case D_COMMON:
310 type = LBL_COMMON;
311 goto symdef;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800312
H. Peter Anvin98578072018-06-01 18:02:54 -0700313 symdef:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800314 {
H. Peter Anvin98578072018-06-01 18:02:54 -0700315 bool validid = true;
316 int64_t size = 0;
317 char *sizestr;
318 bool rn_error;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800319
H. Peter Anvin98578072018-06-01 18:02:54 -0700320 q = value;
321 if (!isidstart(*q))
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800322 validid = false;
H. Peter Anvin98578072018-06-01 18:02:54 -0700323 while (*q && *q != ':' && !nasm_isspace(*q)) {
324 if (!isidchar(*q))
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800325 validid = false;
H. Peter Anvin98578072018-06-01 18:02:54 -0700326 q++;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800327 }
328 if (!validid) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800329 nasm_error(ERR_NONFATAL,
H. Peter Anvin98578072018-06-01 18:02:54 -0700330 "identifier expected after %s", directive);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800331 break;
332 }
333
H. Peter Anvin98578072018-06-01 18:02:54 -0700334 if (nasm_isspace(*q)) {
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700335 *q++ = '\0';
336 sizestr = q = nasm_skip_spaces(q);
H. Peter Anvin98578072018-06-01 18:02:54 -0700337 q = strchr(q, ':');
338 } else {
339 sizestr = NULL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800340 }
H. Peter Anvin98578072018-06-01 18:02:54 -0700341
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700342 if (q && *q == ':') {
H. Peter Anvin98578072018-06-01 18:02:54 -0700343 *q++ = '\0';
344 special = q;
345 } else {
346 special = NULL;
347 }
348
349 if (type == LBL_COMMON) {
350 if (sizestr)
351 size = readnum(sizestr, &rn_error);
352 if (!sizestr || rn_error)
353 nasm_error(ERR_NONFATAL,
354 "%s size specified in common declaration",
355 sizestr ? "invalid" : "no");
356 } else if (sizestr) {
357 nasm_error(ERR_NONFATAL, "invalid syntax in %s declaration",
358 directive);
359 }
360
361 if (*value == '$')
362 value++; /* skip initial $ if present */
363
364 if (!declare_label(value, type, special))
365 break;
366
367 if (type == LBL_COMMON || type == LBL_EXTERN)
368 define_label(value, 0, size, false);
369
370 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800371 }
372
373 case D_ABSOLUTE: /* [ABSOLUTE address] */
374 {
375 expr *e;
376
377 stdscan_reset();
378 stdscan_set(value);
379 tokval.t_type = TOKEN_INVALID;
380 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
381 if (e) {
382 if (!is_reloc(e))
383 nasm_error(pass0 ==
384 1 ? ERR_NONFATAL : ERR_PANIC,
385 "cannot use non-relocatable expression as "
386 "ABSOLUTE address");
387 else {
388 absolute.segment = reloc_seg(e);
389 absolute.offset = reloc_value(e);
390 }
391 } else if (passn == 1)
392 absolute.offset = 0x100; /* don't go near zero in case of / */
393 else
394 nasm_panic(0, "invalid ABSOLUTE address "
395 "in pass two");
396 in_absolute = true;
397 location.segment = NO_SEG;
398 break;
399 }
400
401 case D_DEBUG: /* [DEBUG] */
402 {
403 bool badid, overlong;
404 char debugid[128];
405
406 p = value;
407 q = debugid;
408 badid = overlong = false;
409 if (!isidstart(*p)) {
410 badid = true;
411 } else {
412 while (*p && !nasm_isspace(*p)) {
413 if (q >= debugid + sizeof debugid - 1) {
414 overlong = true;
415 break;
416 }
417 if (!isidchar(*p))
418 badid = true;
419 *q++ = *p++;
420 }
421 *q = 0;
422 }
423 if (badid) {
424 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
425 "identifier expected after DEBUG");
426 break;
427 }
428 if (overlong) {
429 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
430 "DEBUG identifier too long");
431 break;
432 }
433 p = nasm_skip_spaces(p);
434 if (pass0 == 2)
435 dfmt->debug_directive(debugid, p);
436 break;
437 }
438
439 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800440 if (!set_warning_status(value)) {
441 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
442 "unknown warning option: %s", value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800443 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800444 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800445
446 case D_CPU: /* [CPU] */
447 cpu = get_cpu(value);
448 break;
449
450 case D_LIST: /* [LIST {+|-}] */
451 value = nasm_skip_spaces(value);
452 if (*value == '+') {
453 user_nolist = false;
454 } else {
455 if (*value == '-') {
456 user_nolist = true;
457 } else {
458 bad_param = true;
459 }
460 }
461 break;
462
463 case D_DEFAULT: /* [DEFAULT] */
464 stdscan_reset();
465 stdscan_set(value);
466 tokval.t_type = TOKEN_INVALID;
467 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
468 switch (tokval.t_integer) {
469 case S_REL:
470 globalrel = 1;
471 break;
472 case S_ABS:
473 globalrel = 0;
474 break;
475 case P_BND:
476 globalbnd = 1;
477 break;
478 case P_NOBND:
479 globalbnd = 0;
480 break;
481 default:
482 bad_param = true;
483 break;
484 }
485 } else {
486 bad_param = true;
487 }
488 break;
489
490 case D_FLOAT:
491 if (float_option(value)) {
492 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
493 "unknown 'float' directive: %s", value);
494 }
495 break;
496
497 case D_PRAGMA:
498 process_pragma(value);
499 break;
500 }
501
502
503 /* A common error message */
504 if (bad_param) {
505 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
506 directive);
507 }
508
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800509 return d != D_none;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800510}