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
58 static iflag_t
get_cpu(char *value
)
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"))
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
);
100 iflag_set(&r
, IF_PLEVEL
);
101 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
102 "unknown 'cpu' type");
107 static int get_bits(char *value
)
111 if ((i
= atoi(value
)) == 16)
112 return i
; /* set for a 16-bit segment */
114 if (iflag_ffs(&cpu
) < IF_386
) {
115 nasm_error(ERR_NONFATAL
,
116 "cannot specify 32-bit segment on processor below a 386");
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");
126 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
127 "`%s' is not a valid segment size; must be 16, 32 or 64",
134 static enum directive
parse_directive_line(char **directive
, char **value
)
138 buf
= nasm_skip_spaces(*directive
);
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.
147 q
= strchr(buf
, ']');
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.
159 p
= strchr(buf
, ';');
161 if (p
< q
) /* ouch! somewhere inside */
166 /* no brace, no trailing spaces */
168 nasm_zap_spaces_rev(--q
);
171 p
= nasm_skip_spaces(++buf
);
172 q
= nasm_skip_word(p
);
174 return D_corrupt
; /* sigh... no value there */
178 /* and value finally */
179 p
= nasm_skip_spaces(++q
);
182 return directive_find(*directive
);
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.
190 bool process_directives(char *directive
)
193 char *value
, *p
, *q
, *special
;
194 struct tokenval tokval
;
195 bool bad_param
= false;
196 int pass2
= passn
> 1 ? 2 : 1;
198 d
= parse_directive_line(&directive
, &value
);
202 return D_none
; /* Not a directive */
205 nasm_error(ERR_NONFATAL
, "invalid directive line");
208 default: /* It's a backend-specific directive */
209 switch (ofmt
->directive(d
, value
, pass2
)) {
225 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
226 "unrecognised directive [%s]", directive
);
229 case D_SEGMENT
: /* [SEGMENT n] */
233 int32_t seg
= ofmt
->section(value
, pass2
, &sb
);
236 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
237 "segment name `%s' not recognized", value
);
240 location
.segment
= seg
;
245 case D_SECTALIGN
: /* [SECTALIGN n] */
252 tokval
.t_type
= TOKEN_INVALID
;
253 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
255 uint64_t align
= e
->value
;
257 if (!is_power2(e
->value
)) {
258 nasm_error(ERR_NONFATAL
,
259 "segment alignment `%s' is not power of two",
261 } else if (align
> UINT64_C(0x7fffffff)) {
263 * FIXME: Please make some sane message here
264 * ofmt should have some 'check' method which
265 * would report segment alignment bounds.
267 nasm_error(ERR_NONFATAL
,
268 "absurdly large segment alignment `%s' (2^%d)",
269 value
, ilog2_64(align
));
272 /* callee should be able to handle all details */
273 if (location
.segment
!= NO_SEG
)
274 ofmt
->sectalign(location
.segment
, align
);
280 case D_EXTERN
: /* [EXTERN label:special] */
282 value
++; /* skip initial $ if present */
285 while (*q
&& *q
!= ':')
289 ofmt
->symdef(value
, 0L, 0L, 3, q
);
291 } else if (passn
== 1) {
296 while (*q
&& *q
!= ':') {
302 nasm_error(ERR_NONFATAL
, "identifier expected after EXTERN");
310 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
312 pass0
= 1; /* fake pass 1 in labels.c */
313 declare_as_global(value
, special
);
314 define_label(value
, seg_alloc(), 0L, NULL
,
318 } /* else pass0 == 1 */
321 case D_BITS
: /* [BITS bits] */
322 globalbits
= get_bits(value
);
325 case D_GLOBAL
: /* [GLOBAL symbol:special] */
327 value
++; /* skip initial $ if present */
328 if (pass0
== 2) { /* pass 2 */
330 while (*q
&& *q
!= ':')
334 ofmt
->symdef(value
, 0L, 0L, 3, q
);
336 } else if (pass2
== 1) { /* pass == 1 */
342 while (*q
&& *q
!= ':') {
348 nasm_error(ERR_NONFATAL
,
349 "identifier expected after GLOBAL");
357 declare_as_global(value
, special
);
361 case D_COMMON
: /* [COMMON symbol size:special] */
368 value
++; /* skip initial $ if present */
373 while (*p
&& !nasm_isspace(*p
)) {
379 nasm_error(ERR_NONFATAL
, "identifier expected after COMMON");
383 p
= nasm_zap_spaces_fwd(p
);
385 while (*q
&& *q
!= ':')
393 size
= readnum(p
, &rn_error
);
395 nasm_error(ERR_NONFATAL
,
396 "invalid size specified"
397 " in COMMON declaration");
401 nasm_error(ERR_NONFATAL
,
402 "no size specified in"
403 " COMMON declaration");
408 define_common(value
, seg_alloc(), size
, special
);
409 } else if (pass0
== 2) {
411 ofmt
->symdef(value
, 0L, 0L, 3, special
);
416 case D_ABSOLUTE
: /* [ABSOLUTE address] */
422 tokval
.t_type
= TOKEN_INVALID
;
423 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
427 1 ? ERR_NONFATAL
: ERR_PANIC
,
428 "cannot use non-relocatable expression as "
431 absolute
.segment
= reloc_seg(e
);
432 absolute
.offset
= reloc_value(e
);
434 } else if (passn
== 1)
435 absolute
.offset
= 0x100; /* don't go near zero in case of / */
437 nasm_panic(0, "invalid ABSOLUTE address "
440 location
.segment
= NO_SEG
;
444 case D_DEBUG
: /* [DEBUG] */
446 bool badid
, overlong
;
451 badid
= overlong
= false;
452 if (!isidstart(*p
)) {
455 while (*p
&& !nasm_isspace(*p
)) {
456 if (q
>= debugid
+ sizeof debugid
- 1) {
467 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
468 "identifier expected after DEBUG");
472 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
473 "DEBUG identifier too long");
476 p
= nasm_skip_spaces(p
);
478 dfmt
->debug_directive(debugid
, p
);
482 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
483 if (!set_warning_status(value
)) {
484 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
485 "unknown warning option: %s", value
);
489 case D_CPU
: /* [CPU] */
490 cpu
= get_cpu(value
);
493 case D_LIST
: /* [LIST {+|-}] */
494 value
= nasm_skip_spaces(value
);
506 case D_DEFAULT
: /* [DEFAULT] */
509 tokval
.t_type
= TOKEN_INVALID
;
510 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
511 switch (tokval
.t_integer
) {
534 if (float_option(value
)) {
535 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
536 "unknown 'float' directive: %s", value
);
541 process_pragma(value
);
546 /* A common error message */
548 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",