blob: 46079b48716fe111eb6e19040fbc76a1f61e5923 [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++) {
Cyrill Gorcunova7f318c2018-06-07 00:06:58 +0300105 if (!nasm_stricmp(value, cpu->name))
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800106 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800107 }
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800108
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800109 if (!cpu->name)
110 nasm_nonfatal("unknown 'cpu' type '%s'", value);
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800111
112 iflag_set_cpu(&r, cpu->level);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800113 return r;
114}
115
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800116static int get_bits(const char *value)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800117{
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800118 int i = atoi(value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800119
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800120 switch (i) {
121 case 16:
122 break; /* Always safe */
123 case 32:
124 if (!iflag_cpu_level_ok(&cpu, IF_386)) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300125 nasm_nonfatal("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)) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300131 nasm_nonfatal("cannot specify 64-bit segment on processor below an x86-64");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800132 i = 16;
133 }
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800134 break;
135 default:
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800136 nasm_nonfatal("`%s' is not a valid segment size; must be 16, 32 or 64",
137 value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800138 i = 16;
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800139 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800140 }
141 return i;
142}
143
H. Peter Anvin5253f582017-04-03 00:09:58 -0700144static enum directive parse_directive_line(char **directive, char **value)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800145{
146 char *p, *q, *buf;
147
148 buf = nasm_skip_spaces(*directive);
149
150 /*
151 * It should be enclosed in [ ].
152 * XXX: we don't check there is nothing else on the remainder of the
153 * line, except a possible comment.
154 */
155 if (*buf != '[')
156 return D_none;
157 q = strchr(buf, ']');
158 if (!q)
159 return D_corrupt;
160
161 /*
162 * Strip off the comments. XXX: this doesn't account for quoted
163 * strings inside a directive. We should really strip the
164 * comments in generic code, not here. While we're at it, it
165 * would be better to pass the backend a series of tokens instead
166 * of a raw string, and actually process quoted strings for it,
167 * like of like argv is handled in C.
168 */
169 p = strchr(buf, ';');
170 if (p) {
171 if (p < q) /* ouch! somewhere inside */
172 return D_corrupt;
173 *p = '\0';
174 }
175
176 /* no brace, no trailing spaces */
177 *q = '\0';
178 nasm_zap_spaces_rev(--q);
179
180 /* directive */
181 p = nasm_skip_spaces(++buf);
182 q = nasm_skip_word(p);
183 if (!q)
184 return D_corrupt; /* sigh... no value there */
185 *q = '\0';
186 *directive = p;
187
188 /* and value finally */
189 p = nasm_skip_spaces(++q);
190 *value = p;
191
H. Peter Anvin5253f582017-04-03 00:09:58 -0700192 return directive_find(*directive);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800193}
194
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800195/*
196 * Process a line from the assembler and try to handle it if it
197 * is a directive. Return true if the line was handled (including
198 * if it was an error), false otherwise.
199 */
200bool process_directives(char *directive)
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800201{
H. Peter Anvin5253f582017-04-03 00:09:58 -0700202 enum directive d;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800203 char *value, *p, *q, *special;
204 struct tokenval tokval;
205 bool bad_param = false;
H. Peter Anvin98578072018-06-01 18:02:54 -0700206 enum label_type type;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800207
208 d = parse_directive_line(&directive, &value);
209
210 switch (d) {
211 case D_none:
212 return D_none; /* Not a directive */
213
214 case D_corrupt:
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300215 nasm_nonfatal("invalid directive line");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800216 break;
217
218 default: /* It's a backend-specific directive */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800219 switch (ofmt->directive(d, value)) {
H. Peter Anvine562b702017-03-07 22:40:00 -0800220 case DIRR_UNKNOWN:
221 goto unknown;
222 case DIRR_OK:
223 case DIRR_ERROR:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800224 break;
H. Peter Anvine562b702017-03-07 22:40:00 -0800225 case DIRR_BADPARAM:
226 bad_param = true;
227 break;
228 default:
229 panic();
230 }
231 break;
232
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800233 case D_unknown:
H. Peter Anvine562b702017-03-07 22:40:00 -0800234 unknown:
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800235 nasm_nonfatal("unrecognized directive [%s]", directive);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800236 break;
237
238 case D_SEGMENT: /* [SEGMENT n] */
239 case D_SECTION:
240 {
H. Peter Anvine8001272017-09-27 14:22:16 -0700241 int sb = globalbits;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800242 int32_t seg = ofmt->section(value, &sb);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800243
244 if (seg == NO_SEG) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800245 nasm_nonfatal("segment name `%s' not recognized", value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800246 } else {
H. Peter Anvine8001272017-09-27 14:22:16 -0700247 globalbits = sb;
H. Peter Anvin892c4812018-05-30 14:43:46 -0700248 switch_segment(seg);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800249 }
250 break;
251 }
252
253 case D_SECTALIGN: /* [SECTALIGN n] */
254 {
255 expr *e;
256
257 if (*value) {
258 stdscan_reset();
259 stdscan_set(value);
260 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800261 e = evaluate(stdscan, NULL, &tokval, NULL, true, NULL);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800262 if (e) {
263 uint64_t align = e->value;
264
265 if (!is_power2(e->value)) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300266 nasm_nonfatal("segment alignment `%s' is not power of two",
267 value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800268 } else if (align > UINT64_C(0x7fffffff)) {
269 /*
270 * FIXME: Please make some sane message here
271 * ofmt should have some 'check' method which
272 * would report segment alignment bounds.
273 */
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300274 nasm_nonfatal("absurdly large segment alignment `%s' (2^%d)",
275 value, ilog2_64(align));
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800276 }
277
278 /* callee should be able to handle all details */
279 if (location.segment != NO_SEG)
280 ofmt->sectalign(location.segment, align);
281 }
282 }
283 break;
284 }
285
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800286 case D_BITS: /* [BITS bits] */
287 globalbits = get_bits(value);
288 break;
289
H. Peter Anvin98578072018-06-01 18:02:54 -0700290 case D_GLOBAL: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
291 type = LBL_GLOBAL;
292 goto symdef;
293 case D_STATIC:
294 type = LBL_STATIC;
295 goto symdef;
296 case D_EXTERN:
297 type = LBL_EXTERN;
298 goto symdef;
299 case D_COMMON:
300 type = LBL_COMMON;
301 goto symdef;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800302
H. Peter Anvin98578072018-06-01 18:02:54 -0700303 symdef:
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800304 {
H. Peter Anvin98578072018-06-01 18:02:54 -0700305 bool validid = true;
306 int64_t size = 0;
307 char *sizestr;
308 bool rn_error;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800309
H. Peter Anvin73482482018-06-11 14:54:14 -0700310 if (*value == '$')
311 value++; /* skip initial $ if present */
312
H. Peter Anvin98578072018-06-01 18:02:54 -0700313 q = value;
H. Peter Anvin13506202018-11-28 14:55:58 -0800314 if (!nasm_isidstart(*q)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800315 validid = false;
H. Peter Anvin73482482018-06-11 14:54:14 -0700316 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -0700317 q++;
H. Peter Anvin73482482018-06-11 14:54:14 -0700318 while (*q && *q != ':' && !nasm_isspace(*q)) {
H. Peter Anvin13506202018-11-28 14:55:58 -0800319 if (!nasm_isidchar(*q))
H. Peter Anvin73482482018-06-11 14:54:14 -0700320 validid = false;
321 q++;
322 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800323 }
324 if (!validid) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300325 nasm_nonfatal("identifier expected after %s, got `%s'",
326 directive, value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800327 break;
328 }
329
H. Peter Anvin98578072018-06-01 18:02:54 -0700330 if (nasm_isspace(*q)) {
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700331 *q++ = '\0';
332 sizestr = q = nasm_skip_spaces(q);
H. Peter Anvin98578072018-06-01 18:02:54 -0700333 q = strchr(q, ':');
334 } else {
335 sizestr = NULL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800336 }
H. Peter Anvin98578072018-06-01 18:02:54 -0700337
H. Peter Anvin3cb90682018-06-01 21:05:45 -0700338 if (q && *q == ':') {
H. Peter Anvin98578072018-06-01 18:02:54 -0700339 *q++ = '\0';
340 special = q;
341 } else {
342 special = NULL;
343 }
344
345 if (type == LBL_COMMON) {
346 if (sizestr)
347 size = readnum(sizestr, &rn_error);
348 if (!sizestr || rn_error)
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300349 nasm_nonfatal("%s size specified in common declaration",
350 sizestr ? "invalid" : "no");
H. Peter Anvin98578072018-06-01 18:02:54 -0700351 } else if (sizestr) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300352 nasm_nonfatal("invalid syntax in %s declaration", directive);
H. Peter Anvin98578072018-06-01 18:02:54 -0700353 }
354
H. Peter Anvin98578072018-06-01 18:02:54 -0700355 if (!declare_label(value, type, special))
356 break;
357
358 if (type == LBL_COMMON || type == LBL_EXTERN)
H. Peter Anvinaf5f9182018-06-14 19:53:45 -0700359 define_label(value, 0, size, false);
H. Peter Anvin98578072018-06-01 18:02:54 -0700360
361 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800362 }
363
364 case D_ABSOLUTE: /* [ABSOLUTE address] */
365 {
366 expr *e;
367
368 stdscan_reset();
369 stdscan_set(value);
370 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800371 e = evaluate(stdscan, NULL, &tokval, NULL, true, NULL);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800372 if (e) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800373 if (!is_reloc(e)) {
374 nasm_nonfatal("cannot use non-relocatable expression as "
375 "ABSOLUTE address");
376 } else {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800377 absolute.segment = reloc_seg(e);
378 absolute.offset = reloc_value(e);
379 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800380 } else if (pass_first()) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800381 absolute.offset = 0x100; /* don't go near zero in case of / */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800382 } else {
383 nasm_nonfatal("invalid ABSOLUTE address");
384 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800385 in_absolute = true;
386 location.segment = NO_SEG;
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700387 location.offset = absolute.offset;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800388 break;
389 }
390
391 case D_DEBUG: /* [DEBUG] */
392 {
393 bool badid, overlong;
394 char debugid[128];
395
396 p = value;
397 q = debugid;
398 badid = overlong = false;
H. Peter Anvin13506202018-11-28 14:55:58 -0800399 if (!nasm_isidstart(*p)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800400 badid = true;
401 } else {
402 while (*p && !nasm_isspace(*p)) {
403 if (q >= debugid + sizeof debugid - 1) {
404 overlong = true;
405 break;
406 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800407 if (!nasm_isidchar(*p))
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800408 badid = true;
409 *q++ = *p++;
410 }
411 *q = 0;
412 }
413 if (badid) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800414 nasm_nonfatal("identifier expected after DEBUG");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800415 break;
416 }
417 if (overlong) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800418 nasm_nonfatal("DEBUG identifier too long");
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800419 break;
420 }
421 p = nasm_skip_spaces(p);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800422 if (pass_final())
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800423 dfmt->debug_directive(debugid, p);
424 break;
425 }
426
427 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800428 set_warning_status(value);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800429 break;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800430
431 case D_CPU: /* [CPU] */
432 cpu = get_cpu(value);
433 break;
434
435 case D_LIST: /* [LIST {+|-}] */
436 value = nasm_skip_spaces(value);
437 if (*value == '+') {
438 user_nolist = false;
439 } else {
440 if (*value == '-') {
441 user_nolist = true;
442 } else {
443 bad_param = true;
444 }
445 }
446 break;
447
448 case D_DEFAULT: /* [DEFAULT] */
449 stdscan_reset();
450 stdscan_set(value);
451 tokval.t_type = TOKEN_INVALID;
452 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
453 switch (tokval.t_integer) {
454 case S_REL:
455 globalrel = 1;
456 break;
457 case S_ABS:
458 globalrel = 0;
459 break;
460 case P_BND:
461 globalbnd = 1;
462 break;
463 case P_NOBND:
464 globalbnd = 0;
465 break;
466 default:
467 bad_param = true;
468 break;
469 }
470 } else {
471 bad_param = true;
472 }
473 break;
474
475 case D_FLOAT:
476 if (float_option(value)) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800477 nasm_nonfatal("unknown 'float' directive: %s", value);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800478 }
479 break;
480
481 case D_PRAGMA:
482 process_pragma(value);
483 break;
484 }
485
486
487 /* A common error message */
488 if (bad_param) {
Cyrill Gorcunov7c5de5b2018-12-01 14:17:40 +0300489 nasm_nonfatal("invalid parameter to [%s] directive", directive);
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800490 }
491
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800492 return d != D_none;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800493}