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
;
246 case D_SECTALIGN
: /* [SECTALIGN n] */
253 tokval
.t_type
= TOKEN_INVALID
;
254 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
256 uint64_t align
= e
->value
;
258 if (!is_power2(e
->value
)) {
259 nasm_error(ERR_NONFATAL
,
260 "segment alignment `%s' is not power of two",
262 } else if (align
> UINT64_C(0x7fffffff)) {
264 * FIXME: Please make some sane message here
265 * ofmt should have some 'check' method which
266 * would report segment alignment bounds.
268 nasm_error(ERR_NONFATAL
,
269 "absurdly large segment alignment `%s' (2^%d)",
270 value
, ilog2_64(align
));
273 /* callee should be able to handle all details */
274 if (location
.segment
!= NO_SEG
)
275 ofmt
->sectalign(location
.segment
, align
);
281 case D_EXTERN
: /* [EXTERN label:special] */
283 value
++; /* skip initial $ if present */
286 while (*q
&& *q
!= ':')
290 ofmt
->symdef(value
, 0L, 0L, 3, q
);
292 } else if (passn
== 1) {
297 while (*q
&& *q
!= ':') {
303 nasm_error(ERR_NONFATAL
, "identifier expected after EXTERN");
311 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
313 pass0
= 1; /* fake pass 1 in labels.c */
314 declare_as_global(value
, special
);
315 define_label(value
, seg_alloc(), 0L, NULL
,
319 } /* else pass0 == 1 */
322 case D_BITS
: /* [BITS bits] */
323 globalbits
= get_bits(value
);
326 case D_GLOBAL
: /* [GLOBAL symbol:special] */
328 value
++; /* skip initial $ if present */
329 if (pass0
== 2) { /* pass 2 */
331 while (*q
&& *q
!= ':')
335 ofmt
->symdef(value
, 0L, 0L, 3, q
);
337 } else if (pass2
== 1) { /* pass == 1 */
343 while (*q
&& *q
!= ':') {
349 nasm_error(ERR_NONFATAL
,
350 "identifier expected after GLOBAL");
358 declare_as_global(value
, special
);
362 case D_COMMON
: /* [COMMON symbol size:special] */
369 value
++; /* skip initial $ if present */
374 while (*p
&& !nasm_isspace(*p
)) {
380 nasm_error(ERR_NONFATAL
, "identifier expected after COMMON");
384 p
= nasm_zap_spaces_fwd(p
);
386 while (*q
&& *q
!= ':')
394 size
= readnum(p
, &rn_error
);
396 nasm_error(ERR_NONFATAL
,
397 "invalid size specified"
398 " in COMMON declaration");
402 nasm_error(ERR_NONFATAL
,
403 "no size specified in"
404 " COMMON declaration");
409 define_common(value
, seg_alloc(), size
, special
);
410 } else if (pass0
== 2) {
412 ofmt
->symdef(value
, 0L, 0L, 3, special
);
417 case D_ABSOLUTE
: /* [ABSOLUTE address] */
423 tokval
.t_type
= TOKEN_INVALID
;
424 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
428 1 ? ERR_NONFATAL
: ERR_PANIC
,
429 "cannot use non-relocatable expression as "
432 absolute
.segment
= reloc_seg(e
);
433 absolute
.offset
= reloc_value(e
);
435 } else if (passn
== 1)
436 absolute
.offset
= 0x100; /* don't go near zero in case of / */
438 nasm_panic(0, "invalid ABSOLUTE address "
441 location
.segment
= NO_SEG
;
445 case D_DEBUG
: /* [DEBUG] */
447 bool badid
, overlong
;
452 badid
= overlong
= false;
453 if (!isidstart(*p
)) {
456 while (*p
&& !nasm_isspace(*p
)) {
457 if (q
>= debugid
+ sizeof debugid
- 1) {
468 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
469 "identifier expected after DEBUG");
473 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
474 "DEBUG identifier too long");
477 p
= nasm_skip_spaces(p
);
479 dfmt
->debug_directive(debugid
, p
);
483 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
484 if (!set_warning_status(value
)) {
485 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
486 "unknown warning option: %s", value
);
490 case D_CPU
: /* [CPU] */
491 cpu
= get_cpu(value
);
494 case D_LIST
: /* [LIST {+|-}] */
495 value
= nasm_skip_spaces(value
);
507 case D_DEFAULT
: /* [DEFAULT] */
510 tokval
.t_type
= TOKEN_INVALID
;
511 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
512 switch (tokval
.t_integer
) {
535 if (float_option(value
)) {
536 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
537 "unknown 'float' directive: %s", value
);
542 process_pragma(value
);
547 /* A common error message */
549 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",