output: macho -- Add support for N_PEXT in macho output
[nasm.git] / asm / directiv.c
blob7e646832d175ca43a2b459ad431381c6d9431c30
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
35 * Parse and handle assembler directives
38 #include "compiler.h"
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <limits.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "ilog2.h"
48 #include "error.h"
49 #include "float.h"
50 #include "stdscan.h"
51 #include "preproc.h"
52 #include "eval.h"
53 #include "assemble.h"
54 #include "outform.h"
55 #include "listing.h"
56 #include "labels.h"
57 #include "iflag.h"
59 struct cpunames {
60 const char *name;
61 unsigned int level;
62 /* Eventually a table of features */
65 static iflag_t get_cpu(const char *value)
67 iflag_t r;
68 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 */
102 iflag_clear_all(&r);
104 for (cpu = cpunames; cpu->name; cpu++) {
105 if (!strcmp(value, cpu->name))
106 break;
109 if (!cpu->name) {
110 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
111 "unknown 'cpu' type '%s'", value);
114 iflag_set_cpu(&r, cpu->level);
115 return r;
118 static int get_bits(const char *value)
120 int i = atoi(value);
122 switch (i) {
123 case 16:
124 break; /* Always safe */
125 case 32:
126 if (!iflag_cpu_level_ok(&cpu, IF_386)) {
127 nasm_error(ERR_NONFATAL,
128 "cannot specify 32-bit segment on processor below a 386");
129 i = 16;
131 break;
132 case 64:
133 if (!iflag_cpu_level_ok(&cpu, IF_X86_64)) {
134 nasm_error(ERR_NONFATAL,
135 "cannot specify 64-bit segment on processor below an x86-64");
136 i = 16;
138 break;
139 default:
140 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
141 "`%s' is not a valid segment size; must be 16, 32 or 64",
142 value);
143 i = 16;
144 break;
146 return i;
149 static enum directive parse_directive_line(char **directive, char **value)
151 char *p, *q, *buf;
153 buf = nasm_skip_spaces(*directive);
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.
160 if (*buf != '[')
161 return D_none;
162 q = strchr(buf, ']');
163 if (!q)
164 return D_corrupt;
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.
174 p = strchr(buf, ';');
175 if (p) {
176 if (p < q) /* ouch! somewhere inside */
177 return D_corrupt;
178 *p = '\0';
181 /* no brace, no trailing spaces */
182 *q = '\0';
183 nasm_zap_spaces_rev(--q);
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;
193 /* and value finally */
194 p = nasm_skip_spaces(++q);
195 *value = p;
197 return directive_find(*directive);
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.
205 bool process_directives(char *directive)
207 enum directive d;
208 char *value, *p, *q, *special;
209 struct tokenval tokval;
210 bool bad_param = false;
211 int pass2 = passn > 1 ? 2 : 1;
213 d = parse_directive_line(&directive, &value);
215 switch (d) {
216 case D_none:
217 return D_none; /* Not a directive */
219 case D_corrupt:
220 nasm_error(ERR_NONFATAL, "invalid directive line");
221 break;
223 default: /* It's a backend-specific directive */
224 switch (ofmt->directive(d, value, pass2)) {
225 case DIRR_UNKNOWN:
226 goto unknown;
227 case DIRR_OK:
228 case DIRR_ERROR:
229 break;
230 case DIRR_BADPARAM:
231 bad_param = true;
232 break;
233 default:
234 panic();
236 break;
238 case D_unknown:
239 unknown:
240 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
241 "unrecognised directive [%s]", directive);
242 break;
244 case D_SEGMENT: /* [SEGMENT n] */
245 case D_SECTION:
247 int sb = globalbits;
248 int32_t seg = ofmt->section(value, pass2, &sb);
250 if (seg == NO_SEG) {
251 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
252 "segment name `%s' not recognized", value);
253 } else {
254 in_absolute = false;
255 location.segment = seg;
256 globalbits = sb;
258 break;
261 case D_SECTALIGN: /* [SECTALIGN n] */
263 expr *e;
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;
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)) {
279 * FIXME: Please make some sane message here
280 * ofmt should have some 'check' method which
281 * would report segment alignment bounds.
283 nasm_error(ERR_NONFATAL,
284 "absurdly large segment alignment `%s' (2^%d)",
285 value, ilog2_64(align));
288 /* callee should be able to handle all details */
289 if (location.segment != NO_SEG)
290 ofmt->sectalign(location.segment, align);
293 break;
296 case D_EXTERN: /* [EXTERN label:special] */
297 if (*value == '$')
298 value++; /* skip initial $ if present */
299 if (pass0 == 2) {
300 q = value;
301 while (*q && *q != ':')
302 q++;
303 if (*q == ':') {
304 *q++ = '\0';
305 ofmt->symdef(value, 0L, 0L, 3, q);
307 } else if (passn == 1) {
308 bool validid = true;
309 q = value;
310 if (!isidstart(*q))
311 validid = false;
312 while (*q && *q != ':') {
313 if (!isidchar(*q))
314 validid = false;
315 q++;
317 if (!validid) {
318 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
319 break;
321 if (*q == ':') {
322 *q++ = '\0';
323 special = q;
324 } else
325 special = NULL;
326 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
327 int temp = pass0;
328 pass0 = 1; /* fake pass 1 in labels.c */
329 declare_as_global(value, special);
330 define_label(value, seg_alloc(), 0L, NULL,
331 false, true);
332 pass0 = temp;
334 } /* else pass0 == 1 */
335 break;
337 case D_BITS: /* [BITS bits] */
338 globalbits = get_bits(value);
339 break;
341 case D_GLOBAL: /* [GLOBAL symbol:special] */
342 if (*value == '$')
343 value++; /* skip initial $ if present */
344 if (pass0 == 2) { /* pass 2 */
345 q = value;
346 while (*q && *q != ':')
347 q++;
348 if (*q == ':') {
349 *q++ = '\0';
350 ofmt->symdef(value, 0L, 0L, 3, q);
352 } else if (pass2 == 1) { /* pass == 1 */
353 bool validid = true;
355 q = value;
356 if (!isidstart(*q))
357 validid = false;
358 while (*q && *q != ':') {
359 if (!isidchar(*q))
360 validid = false;
361 q++;
363 if (!validid) {
364 nasm_error(ERR_NONFATAL,
365 "identifier expected after GLOBAL");
366 break;
368 if (*q == ':') {
369 *q++ = '\0';
370 special = q;
371 } else
372 special = NULL;
373 declare_as_global(value, special);
374 } /* pass == 1 */
375 break;
377 case D_COMMON: /* [COMMON symbol size:special] */
379 int64_t size;
380 bool rn_error;
381 bool validid;
383 if (*value == '$')
384 value++; /* skip initial $ if present */
385 p = value;
386 validid = true;
387 if (!isidstart(*p))
388 validid = false;
389 while (*p && !nasm_isspace(*p)) {
390 if (!isidchar(*p))
391 validid = false;
392 p++;
394 if (!validid) {
395 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
396 break;
398 if (*p) {
399 p = nasm_zap_spaces_fwd(p);
400 q = p;
401 while (*q && *q != ':')
402 q++;
403 if (*q == ':') {
404 *q++ = '\0';
405 special = q;
406 } else {
407 special = NULL;
409 size = readnum(p, &rn_error);
410 if (rn_error) {
411 nasm_error(ERR_NONFATAL,
412 "invalid size specified"
413 " in COMMON declaration");
414 break;
416 } else {
417 nasm_error(ERR_NONFATAL,
418 "no size specified in"
419 " COMMON declaration");
420 break;
423 if (pass0 < 2) {
424 define_common(value, seg_alloc(), size, special);
425 } else if (pass0 == 2) {
426 if (special)
427 ofmt->symdef(value, 0L, 0L, 3, special);
429 break;
432 case D_ABSOLUTE: /* [ABSOLUTE address] */
434 expr *e;
436 stdscan_reset();
437 stdscan_set(value);
438 tokval.t_type = TOKEN_INVALID;
439 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
440 if (e) {
441 if (!is_reloc(e))
442 nasm_error(pass0 ==
443 1 ? ERR_NONFATAL : ERR_PANIC,
444 "cannot use non-relocatable expression as "
445 "ABSOLUTE address");
446 else {
447 absolute.segment = reloc_seg(e);
448 absolute.offset = reloc_value(e);
450 } else if (passn == 1)
451 absolute.offset = 0x100; /* don't go near zero in case of / */
452 else
453 nasm_panic(0, "invalid ABSOLUTE address "
454 "in pass two");
455 in_absolute = true;
456 location.segment = NO_SEG;
457 break;
460 case D_DEBUG: /* [DEBUG] */
462 bool badid, overlong;
463 char debugid[128];
465 p = value;
466 q = debugid;
467 badid = overlong = false;
468 if (!isidstart(*p)) {
469 badid = true;
470 } else {
471 while (*p && !nasm_isspace(*p)) {
472 if (q >= debugid + sizeof debugid - 1) {
473 overlong = true;
474 break;
476 if (!isidchar(*p))
477 badid = true;
478 *q++ = *p++;
480 *q = 0;
482 if (badid) {
483 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
484 "identifier expected after DEBUG");
485 break;
487 if (overlong) {
488 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
489 "DEBUG identifier too long");
490 break;
492 p = nasm_skip_spaces(p);
493 if (pass0 == 2)
494 dfmt->debug_directive(debugid, p);
495 break;
498 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
499 if (!set_warning_status(value)) {
500 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
501 "unknown warning option: %s", value);
503 break;
505 case D_CPU: /* [CPU] */
506 cpu = get_cpu(value);
507 break;
509 case D_LIST: /* [LIST {+|-}] */
510 value = nasm_skip_spaces(value);
511 if (*value == '+') {
512 user_nolist = false;
513 } else {
514 if (*value == '-') {
515 user_nolist = true;
516 } else {
517 bad_param = true;
520 break;
522 case D_DEFAULT: /* [DEFAULT] */
523 stdscan_reset();
524 stdscan_set(value);
525 tokval.t_type = TOKEN_INVALID;
526 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
527 switch (tokval.t_integer) {
528 case S_REL:
529 globalrel = 1;
530 break;
531 case S_ABS:
532 globalrel = 0;
533 break;
534 case P_BND:
535 globalbnd = 1;
536 break;
537 case P_NOBND:
538 globalbnd = 0;
539 break;
540 default:
541 bad_param = true;
542 break;
544 } else {
545 bad_param = true;
547 break;
549 case D_FLOAT:
550 if (float_option(value)) {
551 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
552 "unknown 'float' directive: %s", value);
554 break;
556 case D_PRAGMA:
557 process_pragma(value);
558 break;
562 /* A common error message */
563 if (bad_param) {
564 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
565 directive);
568 return d != D_none;