blob: 29cade7a87fb1570b93be6d1aad6e1d41637cda2 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * 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.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080046#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48#include "nasm.h"
49#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
H. Peter Anvin55568c12016-10-03 19:46:49 -070079static void parse_cmdline(int, char **, int);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080082static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040083static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
84static void nasm_verror_vc(int severity, const char *fmt, va_list args);
85static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086static void usage(void);
87
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080088static bool using_debug_info, opt_verbose_info;
89static const char *debug_format;
90
H. Peter Anvin6867acc2007-10-10 14:58:45 -070091bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080092int pass0, passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000093int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080094int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000095
H. Peter Anvin9bd15062009-07-18 21:07:17 -040096static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080097
Keith Kaniosa6dfa782007-04-13 16:47:53 +000098static char inname[FILENAME_MAX];
99static char outname[FILENAME_MAX];
100static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700101static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000103/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800104const struct ofmt *ofmt = &OF_DEFAULT;
105const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000109
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400110FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700111int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100112static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400113
114static iflag_t cpu;
115static iflag_t cmd_cpu;
116
Charles Craynec1905c22008-09-11 18:54:06 -0700117int64_t global_offset_changed; /* referenced in labels.c */
118int64_t prev_offset_changed;
119int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800121struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123int32_t abs_seg; /* ABSOLUTE segment basis */
124int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000125
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000127
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700129static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130
H. Peter Anvine7469712016-02-18 02:20:59 -0800131static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400132
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400133#define OP_NORMAL (1u << 0)
134#define OP_PREPROCESS (1u << 1)
135#define OP_DEPEND (1u << 2)
136
137static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400138
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700140static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700141static bool depend_missing_ok = false;
142static const char *depend_target = NULL;
143static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800147 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200149
H. Peter Anvin972079f2008-09-30 17:01:23 -0700150static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
151static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000152
H. Peter Anvin972079f2008-09-30 17:01:23 -0700153static const struct warning {
154 const char *name;
155 const char *help;
156 bool enabled;
157} warnings[ERR_WARN_MAX+1] = {
158 {"error", "treat warnings as errors", false},
159 {"macro-params", "macro calls with wrong parameter count", true},
160 {"macro-selfref", "cyclic macro references", false},
161 {"macro-defaults", "macros with more default than optional parameters", true},
162 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700163 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700164 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
165 {"float-overflow", "floating point overflow", true},
166 {"float-denorm", "floating point denormal", false},
167 {"float-underflow", "floating point underflow", false},
168 {"float-toolong", "too many digits in floating-point number", true},
169 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800170 {"lock", "lock prefix on unlockable instructions", true},
171 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800172 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800173 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700174 {"ptr", "non-NASM keyword used in other assemblers", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000175};
176
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700177static bool want_usage;
178static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800179bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
H. Peter Anvin55340992012-09-09 17:09:00 -0700181static char *quote_for_make(const char *str);
182
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400183static int64_t get_curr_offs(void)
184{
185 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
186}
187
188static void set_curr_offs(int64_t l_off)
189{
190 if (in_abs_seg)
191 abs_offset = l_off;
192 else
193 offsets = raa_write(offsets, location.segment, l_off);
194}
195
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000196static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000198 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800200 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000202 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203}
204
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800205/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800206static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800207{
208 int64_t t;
209 int64_t y = tm->tm_year;
210
211 /* See IEEE 1003.1:2004, section 4.14 */
212
213 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
214 t += tm->tm_yday;
215 t *= 24;
216 t += tm->tm_hour;
217 t *= 60;
218 t += tm->tm_min;
219 t *= 60;
220 t += tm->tm_sec;
221
222 return t;
223}
224
225static void define_macros_early(void)
226{
227 char temp[128];
228 struct tm lt, *lt_p, gm, *gm_p;
229 int64_t posix_time;
230
231 lt_p = localtime(&official_compile_time);
232 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400233 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400235 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
238 preproc->pre_define(temp);
239 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
240 preproc->pre_define(temp);
241 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
242 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243 }
244
245 gm_p = gmtime(&official_compile_time);
246 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400247 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800248
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400249 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
252 preproc->pre_define(temp);
253 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
254 preproc->pre_define(temp);
255 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
256 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700258
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800260 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800262 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
266 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400267 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
268 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 }
270}
271
272static void define_macros_late(void)
273{
274 char temp[128];
275
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400276 /*
277 * In case if output format is defined by alias
278 * we have to put shortname of the alias itself here
279 * otherwise ABI backward compatibility gets broken.
280 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400281 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400282 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400283 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800284}
285
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700286static void emit_dependencies(StrList *list)
287{
288 FILE *deps;
289 int linepos, len;
290 StrList *l, *nl;
291
292 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700293 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400294 if (!deps) {
295 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
296 "unable to write dependency file `%s'", depend_file);
297 return;
298 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400300 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700301 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700302
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700303 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400304 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700305 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400306 len = strlen(file);
307 if (linepos + len > 62 && linepos > 1) {
308 fprintf(deps, " \\\n ");
309 linepos = 1;
310 }
311 fprintf(deps, " %s", file);
312 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700313 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700314 }
315 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700316
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400317 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 if (depend_emit_phony)
319 fprintf(deps, "%s:\n\n", l->str);
320 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321 }
322
323 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325}
326
H. Peter Anvin038d8612007-04-12 16:54:50 +0000327int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700329 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700330
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331 time(&official_compile_time);
332
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400333 iflag_set(&cpu, IF_PLEVEL);
334 iflag_set(&cmd_cpu, IF_PLEVEL);
335
Charles Crayne2581c862008-09-10 19:21:52 -0700336 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700337 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400338 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
H. Peter Anvin97e15752007-09-25 08:47:47 -0700340 error_file = stderr;
341
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700342 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700343 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700344
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000346 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400349 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000350
H. Peter Anvin55568c12016-10-03 19:46:49 -0700351 parse_cmdline(argc, argv, 1);
352 if (terminate_after_phase) {
353 if (want_usage)
354 usage();
355 return 1;
356 }
357
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700358 /*
359 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700360 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700361 */
362 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800363 define_macros_early();
364
H. Peter Anvin55568c12016-10-03 19:46:49 -0700365 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 if (terminate_after_phase) {
367 if (want_usage)
368 usage();
369 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000370 }
371
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800372 if (!using_debug_info) {
373 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800374 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800375 } else if (!debug_format) {
376 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800377 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800378 } else {
379 dfmt = dfmt_find(ofmt, debug_format);
380 if (!dfmt) {
381 nasm_fatal(ERR_NOFILE | ERR_USAGE,
382 "unrecognized debug format `%s' for"
383 " output format `%s'",
384 debug_format, ofmt->shortname);
385 }
386 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000387
H. Peter Anvin76690a12002-04-30 20:52:49 +0000388 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400389 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000390
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000391 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800392 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000393
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400394 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700395 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400396 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700397
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400398 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000399 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700400
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400401 if (depend_missing_ok)
402 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700403
H. Peter Anvin130736c2016-02-17 20:27:41 -0800404 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400406 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 while ((line = preproc->getline()))
409 nasm_free(line);
410 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400411 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700413 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700418 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800420 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 "unable to open output file `%s'",
422 outname);
423 } else
424 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000425
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700426 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000427
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400428 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800429 preproc->reset(inname, 3, depend_ptr);
430 memcpy(warning_on, warning_on_global,
431 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 while ((line = preproc->getline())) {
434 /*
435 * We generate %line directives if needed for later programs
436 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000437 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 int altline = src_get(&linnum, &file_name);
439 if (altline) {
440 if (altline == 1 && lineinc == 1)
441 nasm_fputs("", ofile);
442 else {
443 lineinc = (altline != -1 || lineinc != 1);
444 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000445 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 file_name);
447 }
448 prior_linnum = linnum;
449 }
450 nasm_fputs(line, ofile);
451 nasm_free(line);
452 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 preproc->cleanup(0);
454 if (ofile)
455 fclose(ofile);
456 if (ofile && terminate_after_phase)
457 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400458 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400459 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400461 if (operating_mode & OP_NORMAL) {
462 /*
463 * We must call ofmt->filename _anyway_, even if the user
464 * has specified their own output file, because some
465 * formats (eg OBJ and COFF) use ofmt->filename to find out
466 * the name of the input file and then put that inside the
467 * file.
468 */
469 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700471 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400472 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800473 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400476 /*
477 * We must call init_labels() before ofmt->init() since
478 * some object formats will want to define labels in their
479 * init routines. (eg OS/2 defines the FLAT group)
480 */
481 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400484 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400486 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200487
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400488 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800489 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400490 cleanup_labels();
491 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700492 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400493 nasm_error(ERR_NONFATAL|ERR_NOFILE,
494 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700495 terminate_after_phase = true;
496 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400497 }
498
499 if (ofile) {
500 fclose(ofile);
501 if (terminate_after_phase)
502 remove(outname);
503 ofile = NULL;
504 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000506
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700507 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400508 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700509
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000510 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000512
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 raa_free(offsets);
514 saa_free(forwrefs);
515 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000516 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700517 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000518
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700519 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000520}
521
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522/*
523 * Get a parameter for a command line option.
524 * First arg must be in the form of e.g. -f...
525 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800526static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800528 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400529 if (p[2]) /* the parameter's in the option */
530 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800532 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400535 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537 return NULL;
538}
539
H. Peter Anvindc242712007-11-18 11:55:10 -0800540/*
541 * Copy a filename
542 */
543static void copy_filename(char *dst, const char *src)
544{
545 size_t len = strlen(src);
546
547 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800548 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400549 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800550 }
551 strncpy(dst, src, FILENAME_MAX);
552}
553
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700554/*
555 * Convert a string to Make-safe form
556 */
557static char *quote_for_make(const char *str)
558{
559 const char *p;
560 char *os, *q;
561
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400562 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700563 size_t nbs = 0;
564
565 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400566 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567
568 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400569 switch (*p) {
570 case ' ':
571 case '\t':
572 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
573 n += nbs + 2;
574 nbs = 0;
575 break;
576 case '$':
577 case '#':
578 nbs = 0;
579 n += 2;
580 break;
581 case '\\':
582 nbs++;
583 n++;
584 break;
585 default:
586 nbs = 0;
587 n++;
588 break;
589 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700590 }
591
592 /* Convert N backslashes at the end of filename to 2N backslashes */
593 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400594 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700595
596 os = q = nasm_malloc(n);
597
598 nbs = 0;
599 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400600 switch (*p) {
601 case ' ':
602 case '\t':
603 while (nbs--)
604 *q++ = '\\';
605 *q++ = '\\';
606 *q++ = *p;
607 break;
608 case '$':
609 *q++ = *p;
610 *q++ = *p;
611 nbs = 0;
612 break;
613 case '#':
614 *q++ = '\\';
615 *q++ = *p;
616 nbs = 0;
617 break;
618 case '\\':
619 *q++ = *p;
620 nbs++;
621 break;
622 default:
623 *q++ = *p;
624 nbs = 0;
625 break;
626 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700627 }
628 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400629 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700630
631 *q = '\0';
632
633 return os;
634}
635
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000637 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000639};
640
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100641enum text_options {
642 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800643 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100644};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800645static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 {"prefix", OPT_PREFIX},
647 {"postfix", OPT_POSTFIX},
648 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000649};
650
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400651static void show_version(void)
652{
653 printf("NASM version %s compiled on %s%s\n",
654 nasm_version, nasm_date, nasm_compile_options);
655 exit(0);
656}
657
H. Peter Anvin423e3812007-11-15 17:12:29 -0800658static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700659static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000660{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000661 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800662 int i;
663 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700664 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000665
666 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800667 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000668
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
671 /* These parameters take values */
672 if (!(param = get_param(p, q, &advance)))
673 return advance;
674 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800675
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 switch (p[1]) {
677 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700678 if (pass == 1)
679 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700681
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400682 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700683 if (pass == 2)
684 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400685 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700686
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400687 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700688 if (pass == 1) {
689 ofmt = ofmt_find(param, &ofmt_alias);
690 if (!ofmt) {
691 nasm_fatal(ERR_NOFILE | ERR_USAGE,
692 "unrecognised output format `%s' - "
693 "use -hf for a list", param);
694 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400695 }
696 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700697
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400698 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700699 if (pass == 2) {
700 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700701
H. Peter Anvin55568c12016-10-03 19:46:49 -0700702 if (!*param) {
703 /* Naked -O == -Ox */
704 optimizing = MAX_OPTIMIZE;
705 } else {
706 while (*param) {
707 switch (*param) {
708 case '0': case '1': case '2': case '3': case '4':
709 case '5': case '6': case '7': case '8': case '9':
710 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700711
H. Peter Anvin55568c12016-10-03 19:46:49 -0700712 /* -O0 -> optimizing == -1, 0.98 behaviour */
713 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
714 if (opt < 2)
715 optimizing = opt - 1;
716 else
717 optimizing = opt;
718 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700719
H. Peter Anvin55568c12016-10-03 19:46:49 -0700720 case 'v':
721 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 param++;
723 opt_verbose_info = true;
724 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700725
H. Peter Anvin55568c12016-10-03 19:46:49 -0700726 case 'x':
727 param++;
728 optimizing = MAX_OPTIMIZE;
729 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700730
H. Peter Anvin55568c12016-10-03 19:46:49 -0700731 default:
732 nasm_fatal(0,
733 "unknown optimization option -O%c\n",
734 *param);
735 break;
736 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700738 if (optimizing > MAX_OPTIMIZE)
739 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 }
742 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800743
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 case 'p': /* pre-include */
745 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700746 if (pass == 2)
747 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400748 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800749
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400750 case 'd': /* pre-define */
751 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700752 if (pass == 2)
753 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400754 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800755
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 case 'u': /* un-define */
757 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700758 if (pass == 2)
759 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400762 case 'i': /* include search path */
763 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700764 if (pass == 2)
765 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400766 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800767
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400768 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700769 if (pass == 2)
770 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400771 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800772
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400773 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700774 if (pass == 1)
775 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400776 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800777
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400778 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700779 if (pass == 2) {
780 using_debug_info = true;
781 debug_format = param;
782 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400783 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800784
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400785 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700786 if (pass == 1) {
787 if (nasm_stricmp("vc", param) == 0)
788 nasm_set_verror(nasm_verror_vc);
789 else if (nasm_stricmp("gnu", param) == 0)
790 nasm_set_verror(nasm_verror_gnu);
791 else
792 nasm_fatal(ERR_NOFILE | ERR_USAGE,
793 "unrecognized error reporting format `%s'",
794 param);
795 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800797
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700799 if (pass == 2) {
800 using_debug_info = true;
801 if (p[2])
802 debug_format = nasm_skip_spaces(p + 2);
803 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 case 'h':
807 printf
808 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
809 "[-l listfile]\n"
810 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400811 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800812 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700814 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700816 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400817 " -MG d:o, missing files assumed generated\n"
818 " -MF <file> set Makefile dependency file\n"
819 " -MD <file> assemble and generate dependencies\n"
820 " -MT <file> dependency target name\n"
821 " -MQ <file> dependency target name (quoted)\n"
822 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700823 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800825 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800827 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400828 " -o outfile write output to an outfile\n\n"
829 " -f format select an output format\n\n"
830 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 " -I<path> adds a pathname to the include file path\n");
832 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700833 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400834 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400835 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400836 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 " -P<file> pre-includes a file\n"
838 " -D<macro>[=<value>] pre-defines a macro\n"
839 " -U<macro> undefines a macro\n"
840 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800841 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400842 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400843 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400844 "--prefix,--postfix\n"
845 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100846 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800847 "Warnings:\n");
848 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700850 warnings[i].name, warnings[i].help,
851 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 printf
853 ("\nresponse files should contain command line parameters"
854 ", one per line.\n");
855 if (p[2] == 'f') {
856 printf("\nvalid output formats for -f are"
857 " (`*' denotes default):\n");
858 ofmt_list(ofmt, stdout);
859 } else {
860 printf("\nFor a list of valid output formats, use -hf.\n");
861 printf("For a list of debug formats, use -f <form> -y.\n");
862 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800865
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 case 'y':
867 printf("\nvalid debug formats for '%s' output format are"
868 " ('*' denotes default):\n", ofmt->shortname);
869 dfmt_list(ofmt, stdout);
870 exit(0);
871 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800872
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700874 if (pass == 2)
875 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800877
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400879 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800881
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400882 case 'e': /* preprocess only */
883 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700884 if (pass == 1)
885 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800887
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700889 if (pass == 1)
890 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800892
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700894 if (pass == 2) {
895 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
896 do_warn = false;
897 param += 3;
898 } else {
899 do_warn = true;
900 }
901 goto set_warning;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700903 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 case 'w':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906 if (pass == 2) {
907 if (param[0] != '+' && param[0] != '-') {
908 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
909 "invalid option to `-w'");
910 break;
911 }
912 do_warn = (param[0] == '+');
913 param++;
914 goto set_warning;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700916 break;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400917
918set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400919 for (i = 0; i <= ERR_WARN_MAX; i++) {
920 if (!nasm_stricmp(param, warnings[i].name))
921 break;
922 }
923 if (i <= ERR_WARN_MAX) {
924 warning_on_global[i] = do_warn;
925 } else if (!nasm_stricmp(param, "all")) {
926 for (i = 1; i <= ERR_WARN_MAX; i++)
927 warning_on_global[i] = do_warn;
928 } else if (!nasm_stricmp(param, "none")) {
929 for (i = 1; i <= ERR_WARN_MAX; i++)
930 warning_on_global[i] = !do_warn;
931 } else {
H. Peter Anvinb4f734f2016-05-09 12:13:08 -0700932 /* Ignore invalid warning names; forward compatibility */
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400933 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 if (pass == 2) {
938 switch (p[2]) {
939 case 0:
940 operating_mode = OP_DEPEND;
941 break;
942 case 'G':
943 operating_mode = OP_DEPEND;
944 depend_missing_ok = true;
945 break;
946 case 'P':
947 depend_emit_phony = true;
948 break;
949 case 'D':
950 operating_mode = OP_NORMAL;
951 depend_file = q;
952 advance = true;
953 break;
954 case 'F':
955 depend_file = q;
956 advance = true;
957 break;
958 case 'T':
959 depend_target = q;
960 advance = true;
961 break;
962 case 'Q':
963 depend_target = quote_for_make(q);
964 advance = true;
965 break;
966 default:
967 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
968 "unknown dependency option `-M%c'", p[2]);
969 break;
970 }
971 if (advance && (!q || !q[0])) {
972 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
973 "option `-M%c' requires a parameter", p[2]);
974 break;
975 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400976 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000978
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 case '-':
980 {
981 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 if (p[2] == 0) { /* -- => stop processing options */
984 stopoptions = 1;
985 break;
986 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400987
988 if (!nasm_stricmp(p, "--v"))
989 show_version();
990
Andy Willis3f546032016-09-13 00:02:21 +0300991 if (!nasm_stricmp(p, "--version"))
992 show_version();
993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 for (s = 0; textopts[s].label; s++) {
995 if (!nasm_stricmp(p + 2, textopts[s].label)) {
996 break;
997 }
998 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 switch (s) {
1001
1002 case OPT_PREFIX:
1003 case OPT_POSTFIX:
1004 {
1005 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001006 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 ERR_USAGE,
1008 "option `--%s' requires an argument",
1009 p + 2);
1010 break;
1011 } else {
1012 advance = 1, param = q;
1013 }
1014
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001015 switch (s) {
1016 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001017 if (pass == 2)
1018 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001020 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001021 if (pass == 2)
1022 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001023 break;
1024 default:
H. Peter Anvin41087062016-03-03 14:27:34 -08001025 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001026 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 break;
1028 }
1029 break;
1030 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08001031
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 default:
1033 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001034 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 "unrecognised option `--%s'", p + 2);
1036 break;
1037 }
1038 }
1039 break;
1040 }
1041
1042 default:
1043 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001044 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 "unrecognised option `-%c'", p[1]);
1046 break;
1047 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001050 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001052 } else {
1053 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001054 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001055 }
1056
1057 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001058}
1059
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060#define ARG_BUF_DELTA 128
1061
H. Peter Anvin55568c12016-10-03 19:46:49 -07001062static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001064 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001065 int bufsize, prevargsize;
1066
1067 bufsize = prevargsize = ARG_BUF_DELTA;
1068 buffer = nasm_malloc(ARG_BUF_DELTA);
1069 prevarg = nasm_malloc(ARG_BUF_DELTA);
1070 prevarg[0] = '\0';
1071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 p = buffer;
1074 while (1) { /* Loop to handle long lines */
1075 q = fgets(p, bufsize - (p - buffer), rfile);
1076 if (!q)
1077 break;
1078 p += strlen(p);
1079 if (p > buffer && p[-1] == '\n')
1080 break;
1081 if (p - buffer > bufsize - 10) {
1082 int offset;
1083 offset = p - buffer;
1084 bufsize += ARG_BUF_DELTA;
1085 buffer = nasm_realloc(buffer, bufsize);
1086 p = buffer + offset;
1087 }
1088 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 if (!q && p == buffer) {
1091 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001092 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 nasm_free(buffer);
1094 nasm_free(prevarg);
1095 return;
1096 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 /*
1099 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1100 * them are present at the end of the line.
1101 */
1102 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001103
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001104 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001106
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001107 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001108
H. Peter Anvin55568c12016-10-03 19:46:49 -07001109 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001111
Charles Crayne192d5b52007-10-18 19:02:42 -07001112 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 prevargsize += ARG_BUF_DELTA;
1114 prevarg = nasm_realloc(prevarg, prevargsize);
1115 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001116 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001117 }
1118}
1119
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001120/* Function to process args from a string of args, rather than the
1121 * argv array. Used by the environment variable and response file
1122 * processing.
1123 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001124static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001126 char *p, *q, *arg, *prevarg;
1127 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001128
1129 p = args;
1130 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001132 arg = NULL;
1133 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 q = p;
1135 while (*p && *p != separator)
1136 p++;
1137 while (*p == separator)
1138 *p++ = '\0';
1139 prevarg = arg;
1140 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001141 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001143 }
1144 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001145 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001146}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001147
H. Peter Anvin55568c12016-10-03 19:46:49 -07001148static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001149{
1150 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001151 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001152 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001153 perror(file);
1154 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001155 }
1156 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001157 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001158 }
1159 fclose(f);
1160}
1161
H. Peter Anvin55568c12016-10-03 19:46:49 -07001162static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001163{
1164 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001165 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001166 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001167
Charles Craynefcce07f2007-09-30 22:15:36 -07001168 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001169
H. Peter Anvin972079f2008-09-30 17:01:23 -07001170 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001171 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001172
1173 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001174 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001175 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001176 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001177 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001179 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001181 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001182
1183 /*
1184 * Now process the actual command line.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001187 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 argv++;
1189 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001190 /*
1191 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 * arguments like the environment variable. This allows us
1193 * to have multiple arguments on a single line, which is
1194 * different to the -@resp file processing below for regular
1195 * NASM.
1196 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001197 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 argc--;
1199 argv++;
1200 }
1201 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001202 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001203 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001204 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001205 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001206 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 fclose(rfile);
1208 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001209 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 "unable to open response file `%s'", p);
1211 }
1212 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001213 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001214 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001215 }
1216
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001217 /*
1218 * Look for basic command line typos. This definitely doesn't
1219 * catch all errors, but it might help cases of fumbled fingers.
1220 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001221 if (pass != 2)
1222 return;
1223
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001224 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001225 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001226 "no input file specified");
1227 else if (!strcmp(inname, errname) ||
1228 !strcmp(inname, outname) ||
1229 !strcmp(inname, listname) ||
1230 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001231 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001232 "file `%s' is both input and output file",
1233 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001234
H. Peter Anvin70653092007-10-19 14:42:29 -07001235 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001236 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001237 if (!error_file) {
1238 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001239 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001240 "cannot open file `%s' for error messages",
1241 errname);
1242 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001243 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001244}
1245
H. Peter Anvin70055962007-10-11 00:05:31 -07001246static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001247
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001248static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001249{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001250 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001252 int i, validid;
1253 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001254 int32_t seg;
1255 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001256 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001258 int pass_max;
Martin Lindhed551b432016-11-16 16:04:04 +01001259 char debugid[128];
Martin Lindhe8cc93f52016-11-16 16:48:13 +01001260 int sb;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001261
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001262 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001263 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001264
Charles Craynec1905c22008-09-11 18:54:06 -07001265 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001266 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 int pass1, pass2;
1268 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001269
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001270 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001271 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1272 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001273
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001274 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275
Keith Kaniosb7a89542007-04-12 02:40:54 +00001276 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 cpu = cmd_cpu;
1278 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001279 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001281 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001282 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001284 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001285 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 saa_rewind(forwrefs);
1287 forwref = saa_rstruct(forwrefs);
1288 raa_free(offsets);
1289 offsets = raa_init();
1290 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001291 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001292 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001293
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001295 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001296 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001297 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001298
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001300 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001302
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001303 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001304 * Here we parse our directives; this is not handled by the
1305 * 'real' parser. This really should be a separate function.
1306 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001308 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001309 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001310 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001311
H. Peter Anvin70055962007-10-11 00:05:31 -07001312 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001313 case D_SEGMENT: /* [SEGMENT n] */
1314 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 seg = ofmt->section(value, pass2, &sb);
1316 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001317 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001318 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 value);
1320 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001321 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 location.segment = seg;
1323 }
1324 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001325 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001326 if (*value) {
1327 stdscan_reset();
1328 stdscan_set(value);
1329 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001330 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001331 if (e) {
1332 unsigned int align = (unsigned int)e->value;
1333 if ((uint64_t)e->value > 0x7fffffff) {
1334 /*
1335 * FIXME: Please make some sane message here
1336 * ofmt should have some 'check' method which
1337 * would report segment alignment bounds.
1338 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001339 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001340 "incorrect segment alignment `%s'", value);
1341 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001342 nasm_error(ERR_NONFATAL,
1343 "segment alignment `%s' is not power of two",
1344 value);
1345 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001346
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001347 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001348 if (location.segment != NO_SEG)
1349 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001350 }
1351 }
1352 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001353 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 if (*value == '$')
1355 value++; /* skip initial $ if present */
1356 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 q = value;
1358 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 *q++ = '\0';
1362 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001364 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001366 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001368 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001371 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 }
1374 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001375 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 "identifier expected after EXTERN");
1377 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001378 }
1379 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 *q++ = '\0';
1381 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001382 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 special = NULL;
1384 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1385 int temp = pass0;
1386 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001387 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001389 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001391 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 } /* else pass0 == 1 */
1393 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001394 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001395 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001397 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 if (*value == '$')
1399 value++; /* skip initial $ if present */
1400 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001401 q = value;
1402 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001404 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 *q++ = '\0';
1406 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001407 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001409 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001410 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001411 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001412 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001413 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001415 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001417 }
1418 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001419 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 "identifier expected after GLOBAL");
1421 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001422 }
1423 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 *q++ = '\0';
1425 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001426 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001428 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 } /* pass == 1 */
1430 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001431 case D_COMMON: /* [COMMON symbol size:special] */
1432 {
1433 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001434
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 if (*value == '$')
1436 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001437 p = value;
1438 validid = true;
1439 if (!isidstart(*p))
1440 validid = false;
1441 while (*p && !nasm_isspace(*p)) {
1442 if (!isidchar(*p))
1443 validid = false;
1444 p++;
1445 }
1446 if (!validid) {
1447 nasm_error(ERR_NONFATAL,
1448 "identifier expected after COMMON");
1449 break;
1450 }
1451 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001452 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001453 q = p;
1454 while (*q && *q != ':')
1455 q++;
1456 if (*q == ':') {
1457 *q++ = '\0';
1458 special = q;
1459 } else {
1460 special = NULL;
1461 }
1462 size = readnum(p, &rn_error);
1463 if (rn_error) {
1464 nasm_error(ERR_NONFATAL,
1465 "invalid size specified"
1466 " in COMMON declaration");
1467 break;
1468 }
1469 } else {
1470 nasm_error(ERR_NONFATAL,
1471 "no size specified in"
1472 " COMMON declaration");
1473 break;
1474 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001475
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001476 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001477 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001478 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001479 if (special)
1480 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 }
1482 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001483 }
1484 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001486 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001488 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 if (e) {
1490 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001491 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 1 ? ERR_NONFATAL : ERR_PANIC,
1493 "cannot use non-relocatable expression as "
1494 "ABSOLUTE address");
1495 else {
1496 abs_seg = reloc_seg(e);
1497 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001498 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001499 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 abs_offset = 0x100; /* don't go near zero in case of / */
1501 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001502 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001504 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 location.segment = NO_SEG;
1506 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001507 case D_DEBUG: /* [DEBUG] */
1508 {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001509 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001510
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 p = value;
1512 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001514 if (!isidstart(*p)) {
1515 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001516 } else {
1517 while (*p && !nasm_isspace(*p)) {
1518 if (q >= debugid + sizeof debugid - 1) {
1519 overlong = true;
1520 break;
1521 }
1522 if (!isidchar(*p))
1523 badid = true;
1524 *q++ = *p++;
1525 }
1526 *q = 0;
1527 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001528 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001529 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1530 "identifier expected after DEBUG");
1531 break;
1532 }
1533 if (overlong) {
1534 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1535 "DEBUG identifier too long");
1536 break;
1537 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001538 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001539 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001540 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001542 }
1543 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001544 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001545 switch(*value) {
1546 case '-': validid = 0; value++; break;
1547 case '+': validid = 1; value++; break;
1548 case '*': validid = 2; value++; break;
1549 default: validid = 1; break;
1550 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001552 for (i = 1; i <= ERR_WARN_MAX; i++)
1553 if (!nasm_stricmp(value, warnings[i].name))
1554 break;
1555 if (i <= ERR_WARN_MAX) {
1556 switch(validid) {
1557 case 0:
1558 warning_on[i] = false;
1559 break;
1560 case 1:
1561 warning_on[i] = true;
1562 break;
1563 case 2:
1564 warning_on[i] = warning_on_global[i];
1565 break;
1566 }
H. Peter Anvinb4f734f2016-05-09 12:13:08 -07001567 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001569 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 cpu = get_cpu(value);
1571 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001572 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001573 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 if (*value == '+') {
1575 user_nolist = 0;
1576 } else {
1577 if (*value == '-') {
1578 user_nolist = 1;
1579 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001580 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 }
1582 }
1583 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001584 case D_DEFAULT: /* [DEFAULT] */
1585 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001586 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001587 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001588 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001589 switch ((int)tokval.t_integer) {
1590 case S_REL:
1591 globalrel = 1;
1592 break;
1593 case S_ABS:
1594 globalrel = 0;
1595 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001596 case P_BND:
1597 globalbnd = 1;
1598 break;
1599 case P_NOBND:
1600 globalbnd = 0;
1601 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001602 default:
1603 err = 1;
1604 break;
1605 }
1606 } else {
1607 err = 1;
1608 }
1609 break;
1610 case D_FLOAT:
1611 if (float_option(value)) {
1612 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1613 "unknown 'float' directive: %s",
1614 value);
1615 }
1616 break;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07001617 case D_PRAGMA:
1618 /* Currently the pragma directive doesn't do anything */
1619 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001621 if (ofmt->directive(d, value, pass2))
1622 break;
1623 /* else fall through */
1624 case D_unknown:
1625 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1626 "unrecognised directive [%s]",
1627 directive);
1628 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001630 if (err) {
1631 nasm_error(ERR_NONFATAL,
1632 "invalid parameter to [%s] directive",
1633 directive);
1634 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001636 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637
Charles Crayne2581c862008-09-10 19:21:52 -07001638 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001640 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001642 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 forwref = saa_rstruct(forwrefs);
1644 } while (forwref != NULL
1645 && forwref->lineno == globallineno);
1646 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001647 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001648
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001649 if (output_ins.forw_ref) {
1650 if (passn == 1) {
1651 for (i = 0; i < output_ins.operands; i++) {
1652 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1653 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1654 fwinf->lineno = globallineno;
1655 fwinf->operand = i;
1656 }
1657 }
1658 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001659 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 /* forw_ref */
1663 if (output_ins.opcode == I_EQU) {
1664 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001665 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 * Special `..' EQUs get processed in pass two,
1667 * except `..@' macro-processor EQUs which are done
1668 * in the normal place.
1669 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001671 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001673
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674 else if (output_ins.label[0] != '.' ||
1675 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 output_ins.label[2] == '@') {
1677 if (output_ins.operands == 1 &&
1678 (output_ins.oprs[0].type & IMMEDIATE) &&
1679 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001680 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 def_label(output_ins.label,
1682 output_ins.oprs[0].segment,
1683 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001684 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001686 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001688 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001690 && (output_ins.oprs[1].type & IMMEDIATE)
1691 && output_ins.oprs[1].segment == NO_SEG
1692 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001694 output_ins.oprs[0].offset | SEG_ABS,
1695 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001696 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001698 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001700 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001701 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001702 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 * Special `..' EQUs get processed here, except
1704 * `..@' macro processor EQUs which are done above.
1705 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 output_ins.label[1] == '.' &&
1708 output_ins.label[2] != '@') {
1709 if (output_ins.operands == 1 &&
1710 (output_ins.oprs[0].type & IMMEDIATE)) {
1711 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001712 output_ins.oprs[0].segment,
1713 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001714 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001716 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001718 && output_ins.oprs[0].segment == NO_SEG
1719 && (output_ins.oprs[1].type & IMMEDIATE)
1720 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001722 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001723 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001724 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001726 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001728 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001729 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001731
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001733
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001734 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001735 &output_ins);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001736 l *= output_ins.times;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001737
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 /* if (using_debug_info) && output_ins.opcode != -1) */
1739 if (using_debug_info)
1740 { /* fbk 03/25/01 */
1741 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001742 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 TYS_ELEMENTS(output_ins.operands);
1744 switch (output_ins.opcode) {
1745 case I_RESB:
1746 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001747 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 break;
1749 case I_RESW:
1750 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001751 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 break;
1753 case I_RESD:
1754 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001755 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 break;
1757 case I_RESQ:
1758 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001759 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 break;
1761 case I_REST:
1762 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001763 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001765 case I_RESO:
1766 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001767 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001768 break;
1769 case I_RESY:
1770 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001771 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001772 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 case I_DB:
1774 typeinfo |= TY_BYTE;
1775 break;
1776 case I_DW:
1777 typeinfo |= TY_WORD;
1778 break;
1779 case I_DD:
1780 if (output_ins.eops_float)
1781 typeinfo |= TY_FLOAT;
1782 else
1783 typeinfo |= TY_DWORD;
1784 break;
1785 case I_DQ:
1786 typeinfo |= TY_QWORD;
1787 break;
1788 case I_DT:
1789 typeinfo |= TY_TBYTE;
1790 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001791 case I_DO:
1792 typeinfo |= TY_OWORD;
1793 break;
1794 case I_DY:
1795 typeinfo |= TY_YWORD;
1796 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 default:
1798 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001799
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001801
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001802 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001803 }
1804 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001806 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001807 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001808 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 * else l == -1 => invalid instruction, which will be
1810 * flagged as an error on pass 2
1811 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001813 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001815 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001816 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001817
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 }
1819 } /* not an EQU */
1820 cleanup_insn(&output_ins);
1821 }
1822 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001823 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001825
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001826 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001827 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001828 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 if (pass1 == 1)
1831 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001832
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001833 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001835 } else if (global_offset_changed &&
1836 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001837 prev_offset_changed = global_offset_changed;
1838 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001839 } else {
1840 stall_count++;
1841 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001842
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001843 if (terminate_after_phase)
1844 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001845
1846 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001847 /* We get here if the labels don't converge
1848 * Example: FOO equ FOO + 1
1849 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001850 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001851 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001852 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001853 nasm_error(ERR_NONFATAL,
1854 "Possible causes: recursive EQUs, macro abuse.");
1855 break;
1856 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001857 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001858
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001860 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001861 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001862 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001863 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1864 }
1865}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001866
H. Peter Anvin70055962007-10-11 00:05:31 -07001867static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001869 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001871 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001872
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001873 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001875 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001876 q = strchr(buf, ']');
1877 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001878 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001879
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001880 /* stip off the comments */
1881 p = strchr(buf, ';');
1882 if (p) {
1883 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001884 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001885 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001886 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001888 /* no brace, no trailing spaces */
1889 *q = '\0';
1890 nasm_zap_spaces_rev(--q);
1891
1892 /* directive */
1893 p = nasm_skip_spaces(++buf);
1894 q = nasm_skip_word(p);
1895 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001896 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001897 *q = '\0';
1898 *directive = p;
1899
1900 /* and value finally */
1901 p = nasm_skip_spaces(++q);
1902 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001903
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001904 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001905}
1906
Ed Berosetfa771012002-06-09 20:56:40 +00001907/**
1908 * gnu style error reporting
1909 * This function prints an error message to error_file in the
1910 * style used by GNU. An example would be:
1911 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001912 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001913 * which the error occurs (or is detected) and "error:" is one of
1914 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001915 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001916 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001917 *
1918 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001919 * @param fmt the printf style format string
1920 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001921static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001922{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001923 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001924 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001925
Ed Berosetfa771012002-06-09 20:56:40 +00001926 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001928
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001929 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001930 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001931
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001932 if (!skip_this_pass(severity)) {
1933 if (currentfile) {
1934 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001935 } else {
1936 fputs("nasm: ", error_file);
1937 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001938 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001939
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001940 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001941}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001942
Ed Berosetfa771012002-06-09 20:56:40 +00001943/**
1944 * MS style error reporting
1945 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001946 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001947 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001948 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001949 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001950 * which the error occurs (or is detected) and "error:" is one of
1951 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001952 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001953 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001954 *
1955 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001956 * @param fmt the printf style format string
1957 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001958static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001959{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001960 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001961 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001962
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 if (is_suppressed_warning(severity))
1964 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001965
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001966 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001968
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001969 if (!skip_this_pass(severity)) {
1970 if (currentfile) {
1971 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001972 } else {
1973 fputs("nasm: ", error_file);
1974 }
Ed Berosetfa771012002-06-09 20:56:40 +00001975 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001976
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001977 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001978}
1979
1980/**
1981 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001982 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001983 * not in pass 1
1984 *
1985 * @param severity the severity of the warning or error
1986 * @return true if we should abort error/warning printing
1987 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001988static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001989{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001990 /* Not a warning at all */
1991 if ((severity & ERR_MASK) != ERR_WARNING)
1992 return false;
1993
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001994 /* Might be a warning but suppresed explicitly */
1995 if (severity & ERR_WARN_MASK)
1996 return !warning_on[WARN_IDX(severity)];
1997 else
1998 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001999}
2000
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002001static bool skip_this_pass(int severity)
2002{
H. Peter Anvin934f0472016-05-09 12:00:19 -07002003 /* See if it's a pass-specific warning which should be skipped. */
2004
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002005 if ((severity & ERR_MASK) > ERR_WARNING)
2006 return false;
2007
H. Peter Anvin934f0472016-05-09 12:00:19 -07002008 /*
2009 * passn is 1 on the very first pass only.
2010 * pass0 is 2 on the code-generation (final) pass only.
2011 * These are the passes we care about in this case.
2012 */
2013 return (((severity & ERR_PASS1) && passn != 1) ||
2014 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002015}
2016
Ed Berosetfa771012002-06-09 20:56:40 +00002017/**
2018 * common error reporting
2019 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002020 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002021 * specific error message to error_file and may or may not return. It
2022 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002023 *
2024 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002025 * @param fmt the printf style format string
2026 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002027static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002028{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002029 char msg[1024];
2030 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002031
H. Peter Anvin7df04172008-06-10 18:27:38 -07002032 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002034 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 break;
2036 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002037 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002038 break;
2039 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002040 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 break;
2042 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002043 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 break;
2045 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002046 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002048 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002049 pfx = "";
2050 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002051 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002052
H. Peter Anvin934f0472016-05-09 12:00:19 -07002053 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002054 if ((severity & (ERR_WARN_MASK|ERR_PP_LISTMACRO)) == ERR_WARN_MASK) {
H. Peter Anvin934f0472016-05-09 12:00:19 -07002055 char *p = strchr(msg, '\0');
2056 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
2057 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002058
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002059 if (!skip_this_pass(severity))
2060 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002061
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002062 /* Are we recursing from error_list_macros? */
2063 if (severity & ERR_PP_LISTMACRO)
2064 return;
2065
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002066 /*
2067 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002068 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002069 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07002070 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002071
2072 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002073 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002074
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002075 preproc->error_list_macros(severity);
2076
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002077 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 case ERR_DEBUG:
2079 /* no further action, by definition */
2080 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002081 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002082 /* Treat warnings as errors */
2083 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2084 terminate_after_phase = true;
2085 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002087 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 break;
2089 case ERR_FATAL:
2090 if (ofile) {
2091 fclose(ofile);
2092 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002093 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 }
2095 if (want_usage)
2096 usage();
2097 exit(1); /* instantly die */
2098 break; /* placate silly compilers */
2099 case ERR_PANIC:
2100 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002101 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002102 if (ofile) {
2103 fclose(ofile);
2104 remove(outname);
2105 ofile = NULL;
2106 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 exit(3);
2108 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002109 }
2110}
2111
H. Peter Anvin734b1882002-04-30 21:01:08 +00002112static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002113{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002114 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002115}
2116
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002117static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002118{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002119 iflag_t r;
2120
2121 iflag_clear_all(&r);
2122
H. Peter Anvine2c80182005-01-15 22:15:51 +00002123 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002124 iflag_set(&r, IF_8086);
2125 else if (!strcmp(value, "186"))
2126 iflag_set(&r, IF_186);
2127 else if (!strcmp(value, "286"))
2128 iflag_set(&r, IF_286);
2129 else if (!strcmp(value, "386"))
2130 iflag_set(&r, IF_386);
2131 else if (!strcmp(value, "486"))
2132 iflag_set(&r, IF_486);
2133 else if (!strcmp(value, "586") ||
2134 !nasm_stricmp(value, "pentium"))
2135 iflag_set(&r, IF_PENT);
2136 else if (!strcmp(value, "686") ||
2137 !nasm_stricmp(value, "ppro") ||
2138 !nasm_stricmp(value, "pentiumpro") ||
2139 !nasm_stricmp(value, "p2"))
2140 iflag_set(&r, IF_P6);
2141 else if (!nasm_stricmp(value, "p3") ||
2142 !nasm_stricmp(value, "katmai"))
2143 iflag_set(&r, IF_KATMAI);
2144 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2145 !nasm_stricmp(value, "willamette"))
2146 iflag_set(&r, IF_WILLAMETTE);
2147 else if (!nasm_stricmp(value, "prescott"))
2148 iflag_set(&r, IF_PRESCOTT);
2149 else if (!nasm_stricmp(value, "x64") ||
2150 !nasm_stricmp(value, "x86-64"))
2151 iflag_set(&r, IF_X86_64);
2152 else if (!nasm_stricmp(value, "ia64") ||
2153 !nasm_stricmp(value, "ia-64") ||
2154 !nasm_stricmp(value, "itanium")||
2155 !nasm_stricmp(value, "itanic") ||
2156 !nasm_stricmp(value, "merced"))
2157 iflag_set(&r, IF_IA64);
2158 else {
2159 iflag_set(&r, IF_PLEVEL);
2160 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2161 "unknown 'cpu' type");
2162 }
2163 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002164}
2165
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002166static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002167{
2168 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002169
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 if ((i = atoi(value)) == 16)
2171 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002172 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002173 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002174 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002175 "cannot specify 32-bit segment on processor below a 386");
2176 i = 16;
2177 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002178 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002179 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002180 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002181 "cannot specify 64-bit segment on processor below an x86-64");
2182 i = 16;
2183 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002184 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002185 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002186 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 value);
2188 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002189 }
2190 return i;
2191}