1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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
59 static iflag_t
get_cpu(char *value
)
65 if (!strcmp(value
, "8086"))
66 iflag_set(&r
, IF_8086
);
67 else if (!strcmp(value
, "186"))
68 iflag_set(&r
, IF_186
);
69 else if (!strcmp(value
, "286"))
70 iflag_set(&r
, IF_286
);
71 else if (!strcmp(value
, "386"))
72 iflag_set(&r
, IF_386
);
73 else if (!strcmp(value
, "486"))
74 iflag_set(&r
, IF_486
);
75 else if (!strcmp(value
, "586") ||
76 !nasm_stricmp(value
, "pentium"))
77 iflag_set(&r
, IF_PENT
);
78 else if (!strcmp(value
, "686") ||
79 !nasm_stricmp(value
, "ppro") ||
80 !nasm_stricmp(value
, "pentiumpro") ||
81 !nasm_stricmp(value
, "p2"))
83 else if (!nasm_stricmp(value
, "p3") ||
84 !nasm_stricmp(value
, "katmai"))
85 iflag_set(&r
, IF_KATMAI
);
86 else if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
87 !nasm_stricmp(value
, "willamette"))
88 iflag_set(&r
, IF_WILLAMETTE
);
89 else if (!nasm_stricmp(value
, "prescott"))
90 iflag_set(&r
, IF_PRESCOTT
);
91 else if (!nasm_stricmp(value
, "x64") ||
92 !nasm_stricmp(value
, "x86-64"))
93 iflag_set(&r
, IF_X86_64
);
94 else if (!nasm_stricmp(value
, "ia64") ||
95 !nasm_stricmp(value
, "ia-64") ||
96 !nasm_stricmp(value
, "itanium")||
97 !nasm_stricmp(value
, "itanic") ||
98 !nasm_stricmp(value
, "merced"))
99 iflag_set(&r
, IF_IA64
);
101 iflag_set(&r
, IF_PLEVEL
);
102 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
103 "unknown 'cpu' type");
108 static int get_bits(char *value
)
112 if ((i
= atoi(value
)) == 16)
113 return i
; /* set for a 16-bit segment */
115 if (iflag_ffs(&cpu
) < IF_386
) {
116 nasm_error(ERR_NONFATAL
,
117 "cannot specify 32-bit segment on processor below a 386");
120 } else if (i
== 64) {
121 if (iflag_ffs(&cpu
) < IF_X86_64
) {
122 nasm_error(ERR_NONFATAL
,
123 "cannot specify 64-bit segment on processor below an x86-64");
127 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
128 "`%s' is not a valid segment size; must be 16, 32 or 64",
135 static enum directive
parse_directive_line(char **directive
, char **value
)
139 buf
= nasm_skip_spaces(*directive
);
142 * It should be enclosed in [ ].
143 * XXX: we don't check there is nothing else on the remainder of the
144 * line, except a possible comment.
148 q
= strchr(buf
, ']');
153 * Strip off the comments. XXX: this doesn't account for quoted
154 * strings inside a directive. We should really strip the
155 * comments in generic code, not here. While we're at it, it
156 * would be better to pass the backend a series of tokens instead
157 * of a raw string, and actually process quoted strings for it,
158 * like of like argv is handled in C.
160 p
= strchr(buf
, ';');
162 if (p
< q
) /* ouch! somewhere inside */
167 /* no brace, no trailing spaces */
169 nasm_zap_spaces_rev(--q
);
172 p
= nasm_skip_spaces(++buf
);
173 q
= nasm_skip_word(p
);
175 return D_corrupt
; /* sigh... no value there */
179 /* and value finally */
180 p
= nasm_skip_spaces(++q
);
183 return directive_find(*directive
);
187 * Process a line from the assembler and try to handle it if it
188 * is a directive. Return true if the line was handled (including
189 * if it was an error), false otherwise.
191 bool process_directives(char *directive
)
194 char *value
, *p
, *q
, *special
;
195 struct tokenval tokval
;
196 bool bad_param
= false;
197 int pass2
= passn
> 1 ? 2 : 1;
199 d
= parse_directive_line(&directive
, &value
);
203 return D_none
; /* Not a directive */
206 nasm_error(ERR_NONFATAL
, "invalid directive line");
209 default: /* It's a backend-specific directive */
210 switch (ofmt
->directive(d
, value
, pass2
)) {
226 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
227 "unrecognised directive [%s]", directive
);
230 case D_SEGMENT
: /* [SEGMENT n] */
234 int32_t seg
= ofmt
->section(value
, pass2
, &sb
);
237 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
238 "segment name `%s' not recognized", value
);
241 location
.segment
= seg
;
247 case D_SECTALIGN
: /* [SECTALIGN n] */
254 tokval
.t_type
= TOKEN_INVALID
;
255 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
257 uint64_t align
= e
->value
;
259 if (!is_power2(e
->value
)) {
260 nasm_error(ERR_NONFATAL
,
261 "segment alignment `%s' is not power of two",
263 } else if (align
> UINT64_C(0x7fffffff)) {
265 * FIXME: Please make some sane message here
266 * ofmt should have some 'check' method which
267 * would report segment alignment bounds.
269 nasm_error(ERR_NONFATAL
,
270 "absurdly large segment alignment `%s' (2^%d)",
271 value
, ilog2_64(align
));
274 /* callee should be able to handle all details */
275 if (location
.segment
!= NO_SEG
)
276 ofmt
->sectalign(location
.segment
, align
);
282 case D_EXTERN
: /* [EXTERN label:special] */
284 value
++; /* skip initial $ if present */
287 while (*q
&& *q
!= ':')
291 ofmt
->symdef(value
, 0L, 0L, 3, q
);
293 } else if (passn
== 1) {
298 while (*q
&& *q
!= ':') {
304 nasm_error(ERR_NONFATAL
, "identifier expected after EXTERN");
312 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
314 pass0
= 1; /* fake pass 1 in labels.c */
315 declare_as_global(value
, special
);
316 define_label(value
, seg_alloc(), 0L, NULL
,
320 } /* else pass0 == 1 */
323 case D_BITS
: /* [BITS bits] */
324 globalbits
= get_bits(value
);
327 case D_GLOBAL
: /* [GLOBAL symbol:special] */
329 value
++; /* skip initial $ if present */
330 if (pass0
== 2) { /* pass 2 */
332 while (*q
&& *q
!= ':')
336 ofmt
->symdef(value
, 0L, 0L, 3, q
);
338 } else if (pass2
== 1) { /* pass == 1 */
344 while (*q
&& *q
!= ':') {
350 nasm_error(ERR_NONFATAL
,
351 "identifier expected after GLOBAL");
359 declare_as_global(value
, special
);
363 case D_COMMON
: /* [COMMON symbol size:special] */
370 value
++; /* skip initial $ if present */
375 while (*p
&& !nasm_isspace(*p
)) {
381 nasm_error(ERR_NONFATAL
, "identifier expected after COMMON");
385 p
= nasm_zap_spaces_fwd(p
);
387 while (*q
&& *q
!= ':')
395 size
= readnum(p
, &rn_error
);
397 nasm_error(ERR_NONFATAL
,
398 "invalid size specified"
399 " in COMMON declaration");
403 nasm_error(ERR_NONFATAL
,
404 "no size specified in"
405 " COMMON declaration");
410 define_common(value
, seg_alloc(), size
, special
);
411 } else if (pass0
== 2) {
413 ofmt
->symdef(value
, 0L, 0L, 3, special
);
418 case D_ABSOLUTE
: /* [ABSOLUTE address] */
424 tokval
.t_type
= TOKEN_INVALID
;
425 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
429 1 ? ERR_NONFATAL
: ERR_PANIC
,
430 "cannot use non-relocatable expression as "
433 absolute
.segment
= reloc_seg(e
);
434 absolute
.offset
= reloc_value(e
);
436 } else if (passn
== 1)
437 absolute
.offset
= 0x100; /* don't go near zero in case of / */
439 nasm_panic(0, "invalid ABSOLUTE address "
442 location
.segment
= NO_SEG
;
446 case D_DEBUG
: /* [DEBUG] */
448 bool badid
, overlong
;
453 badid
= overlong
= false;
454 if (!isidstart(*p
)) {
457 while (*p
&& !nasm_isspace(*p
)) {
458 if (q
>= debugid
+ sizeof debugid
- 1) {
469 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
470 "identifier expected after DEBUG");
474 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
475 "DEBUG identifier too long");
478 p
= nasm_skip_spaces(p
);
480 dfmt
->debug_directive(debugid
, p
);
484 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
485 if (!set_warning_status(value
)) {
486 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
487 "unknown warning option: %s", value
);
491 case D_CPU
: /* [CPU] */
492 cpu
= get_cpu(value
);
495 case D_LIST
: /* [LIST {+|-}] */
496 value
= nasm_skip_spaces(value
);
508 case D_DEFAULT
: /* [DEFAULT] */
511 tokval
.t_type
= TOKEN_INVALID
;
512 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
513 switch (tokval
.t_integer
) {
536 if (float_option(value
)) {
537 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
538 "unknown 'float' directive: %s", value
);
543 process_pragma(value
);
548 /* A common error message */
550 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",