blob: 94bbc6b29008ff7ba44e0bc19e91403dc3e40276 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 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 Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.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
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080097int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080098static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000099int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800100int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000101
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700102struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800103
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800104const char *inname;
105const char *outname;
106static const char *listname;
107static const char *errname;
108
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109static int globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700110#define GLOBALLINENO_MAX INT32_MAX
111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700120int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100121static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400122
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800123iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400124static iflag_t cmd_cpu;
125
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800126struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800127bool in_absolute; /* Flag we are in ABSOLUTE seg */
128struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000129
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000131
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700133static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134
H. Peter Anvine7469712016-02-18 02:20:59 -0800135static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400136
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400137#define OP_NORMAL (1u << 0)
138#define OP_PREPROCESS (1u << 1)
139#define OP_DEPEND (1u << 2)
140
141static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400142
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700143/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700144static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700145static bool depend_missing_ok = false;
146static const char *depend_target = NULL;
147static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700148StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000149
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700150static bool want_usage;
151static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800152bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000153
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700154static char *quote_for_pmake(const char *str);
155static char *quote_for_wmake(const char *str);
156static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700157
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700158/*
159 * Execution limits that can be set via a command-line option or %pragma
160 */
161
162#define LIMIT_MAX_VAL (INT_MAX >> 1) /* Effectively unlimited */
163
164int nasm_limit[LIMIT_MAX+1] =
165{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000 };
166
167struct limit_info {
168 const char *name;
169 const char *help;
170};
171static const struct limit_info limit_info[LIMIT_MAX+1] = {
172 { "passes", "total number of passes" },
173 { "stalled-passes", "number of passes without forward progress" },
174 { "macro-levels", "levels of macro expansion"},
175 { "rep", "%rep count" },
176 { "eval", "expression evaluation descent"}
177};
178
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700179enum directive_result
180nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700181{
182 int i;
183 int64_t val;
184 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700185 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700186
187 for (i = 0; i <= LIMIT_MAX; i++) {
188 if (!nasm_stricmp(limit, limit_info[i].name))
189 break;
190 }
191 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700192 if (passn == 0)
193 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
194 else
195 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
196 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700197 return DIRR_ERROR;
198 }
199
200 if (!nasm_stricmp(valstr, "unlimited")) {
201 val = LIMIT_MAX_VAL;
202 } else {
203 val = readnum(valstr, &rn_error);
204 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700205 if (passn == 0)
206 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
207 else
208 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
209 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700210 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700211 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700212 if (val > LIMIT_MAX_VAL)
213 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214 }
215
216 nasm_limit[i] = val;
217 return DIRR_OK;
218}
219
H. Peter Anvin892c4812018-05-30 14:43:46 -0700220int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400221{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700222 location.segment = segment;
223 if (segment == NO_SEG) {
224 location.offset = absolute.offset;
225 in_absolute = true;
226 } else {
227 location.offset = raa_read(offsets, segment);
228 in_absolute = false;
229 }
230 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400231}
232
233static void set_curr_offs(int64_t l_off)
234{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800235 if (in_absolute)
236 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400237 else
238 offsets = raa_write(offsets, location.segment, l_off);
239}
240
H. Peter Anvin892c4812018-05-30 14:43:46 -0700241static void increment_offset(int64_t delta)
242{
243 if (unlikely(delta == 0))
244 return;
245
246 location.offset += delta;
247 set_curr_offs(location.offset);
248}
249
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000250static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000251{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000252 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800254 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000255 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257}
258
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259static void define_macros_early(void)
260{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700261 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700264 if (oct->have_local) {
265 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400266 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700267 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700269 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400270 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800273 }
274
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700275 if (oct->have_gm) {
276 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400277 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700278 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700280 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400281 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400283 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800284 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700285
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700286 if (oct->have_posix) {
287 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400288 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800289 }
290}
291
292static void define_macros_late(void)
293{
294 char temp[128];
295
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400296 /*
297 * In case if output format is defined by alias
298 * we have to put shortname of the alias itself here
299 * otherwise ABI backward compatibility gets broken.
300 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400301 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400302 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400303 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800304}
305
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700306static void emit_dependencies(StrList *list)
307{
308 FILE *deps;
309 int linepos, len;
310 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700311 bool wmake = (quote_for_make == quote_for_wmake);
312 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700313
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700314 wrapstr = wmake ? " &\n " : " \\\n ";
315 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700316
317 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700318 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400319 if (!deps) {
320 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
321 "unable to write dependency file `%s'", depend_file);
322 return;
323 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700324 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400325 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700327
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700328 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400329 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700330 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400331 len = strlen(file);
332 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700333 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400334 linepos = 1;
335 }
336 fprintf(deps, " %s", file);
337 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700338 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700339 }
340 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700341
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400342 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700343 if (depend_emit_phony) {
344 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700345 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700346 nasm_free(file);
347 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400348 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700349 }
350
351 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400352 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700353}
354
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700355/* Convert a struct tm to a POSIX-style time constant */
356static int64_t make_posix_time(const struct tm *tm)
357{
358 int64_t t;
359 int64_t y = tm->tm_year;
360
361 /* See IEEE 1003.1:2004, section 4.14 */
362
363 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
364 t += tm->tm_yday;
365 t *= 24;
366 t += tm->tm_hour;
367 t *= 60;
368 t += tm->tm_min;
369 t *= 60;
370 t += tm->tm_sec;
371
372 return t;
373}
374
375static void timestamp(void)
376{
377 struct compile_time * const oct = &official_compile_time;
378 const struct tm *tp, *best_gm;
379
380 time(&oct->t);
381
382 best_gm = NULL;
383
384 tp = localtime(&oct->t);
385 if (tp) {
386 oct->local = *tp;
387 best_gm = &oct->local;
388 oct->have_local = true;
389 }
390
391 tp = gmtime(&oct->t);
392 if (tp) {
393 oct->gm = *tp;
394 best_gm = &oct->gm;
395 oct->have_gm = true;
396 if (!oct->have_local)
397 oct->local = oct->gm;
398 } else {
399 oct->gm = oct->local;
400 }
401
402 if (best_gm) {
403 oct->posix = make_posix_time(best_gm);
404 oct->have_posix = true;
405 }
406}
407
H. Peter Anvin038d8612007-04-12 16:54:50 +0000408int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409{
H. Peter Anvina771be82017-08-16 14:53:18 -0700410 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700411
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700412 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800413
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800414 iflag_set_default_cpu(&cpu);
415 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400416
Charles Crayne2581c862008-09-10 19:21:52 -0700417 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700418 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400419 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000420
H. Peter Anvin97e15752007-09-25 08:47:47 -0700421 error_file = stderr;
422
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700423 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700424 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700425
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000427 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000428
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400430 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000431
H. Peter Anvin55568c12016-10-03 19:46:49 -0700432 parse_cmdline(argc, argv, 1);
433 if (terminate_after_phase) {
434 if (want_usage)
435 usage();
436 return 1;
437 }
438
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700439 /*
440 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700441 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700442 */
443 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800444 define_macros_early();
445
H. Peter Anvin55568c12016-10-03 19:46:49 -0700446 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 if (terminate_after_phase) {
448 if (want_usage)
449 usage();
450 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000451 }
452
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800453 /* Save away the default state of warnings */
454 memcpy(warning_state_init, warning_state, sizeof warning_state);
455
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800456 if (!using_debug_info) {
457 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800458 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800459 } else if (!debug_format) {
460 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800461 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800462 } else {
463 dfmt = dfmt_find(ofmt, debug_format);
464 if (!dfmt) {
465 nasm_fatal(ERR_NOFILE | ERR_USAGE,
466 "unrecognized debug format `%s' for"
467 " output format `%s'",
468 debug_format, ofmt->shortname);
469 }
470 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000471
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400473 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800475 /* no output file name? */
476 if (!outname)
477 outname = filename_set_extension(inname, ofmt->extension);
478
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000479 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800480 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000481
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400482 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700483
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700484 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400485 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700486
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400487 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000488 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700489
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400490 if (depend_missing_ok)
491 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700492
H. Peter Anvin130736c2016-02-17 20:27:41 -0800493 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 while ((line = preproc->getline()))
496 nasm_free(line);
497 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400498 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000499 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700500 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000501 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000503
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800504 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700505 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800507 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 "unable to open output file `%s'",
509 outname);
510 } else
511 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000512
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700513 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000514
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400515 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800516 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800517
518 /* Revert all warnings to the default state */
519 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700520
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 while ((line = preproc->getline())) {
522 /*
523 * We generate %line directives if needed for later programs
524 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000525 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 int altline = src_get(&linnum, &file_name);
527 if (altline) {
528 if (altline == 1 && lineinc == 1)
529 nasm_fputs("", ofile);
530 else {
531 lineinc = (altline != -1 || lineinc != 1);
532 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000533 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 file_name);
535 }
536 prior_linnum = linnum;
537 }
538 nasm_fputs(line, ofile);
539 nasm_free(line);
540 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 preproc->cleanup(0);
542 if (ofile)
543 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700544 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400546 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400547 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400549 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700550 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400551 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800552 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400553 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400555 /*
556 * We must call init_labels() before ofmt->init() since
557 * some object formats will want to define labels in their
558 * init routines. (eg OS/2 defines the FLAT group)
559 */
560 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400562 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400563 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400565 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200566
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400567 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800568 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400569 cleanup_labels();
570 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700571 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400572 nasm_error(ERR_NONFATAL|ERR_NOFILE,
573 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700574 terminate_after_phase = true;
575 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400576 }
577
578 if (ofile) {
579 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700580 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400581 remove(outname);
582 ofile = NULL;
583 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000584 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000585
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700586 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400587 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700588
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000589 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 raa_free(offsets);
593 saa_free(forwrefs);
594 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000595 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700596 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700598 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000599}
600
H. Peter Anvineba20a72002-04-30 20:53:55 +0000601/*
602 * Get a parameter for a command line option.
603 * First arg must be in the form of e.g. -f...
604 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800605static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800607 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400608 if (p[2]) /* the parameter's in the option */
609 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800611 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400614 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000616 return NULL;
617}
618
H. Peter Anvindc242712007-11-18 11:55:10 -0800619/*
620 * Copy a filename
621 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800622static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800623{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800624 if (*dst)
625 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800626
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800627 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800628}
629
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700630/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700631 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700632 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700633static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700634{
635 const char *p;
636 char *os, *q;
637
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400638 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700639 size_t nbs = 0;
640
641 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400642 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700643
644 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400645 switch (*p) {
646 case ' ':
647 case '\t':
648 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
649 n += nbs + 2;
650 nbs = 0;
651 break;
652 case '$':
653 case '#':
654 nbs = 0;
655 n += 2;
656 break;
657 case '\\':
658 nbs++;
659 n++;
660 break;
661 default:
662 nbs = 0;
663 n++;
664 break;
665 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700666 }
667
668 /* Convert N backslashes at the end of filename to 2N backslashes */
669 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400670 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700671
672 os = q = nasm_malloc(n);
673
674 nbs = 0;
675 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400676 switch (*p) {
677 case ' ':
678 case '\t':
679 while (nbs--)
680 *q++ = '\\';
681 *q++ = '\\';
682 *q++ = *p;
683 break;
684 case '$':
685 *q++ = *p;
686 *q++ = *p;
687 nbs = 0;
688 break;
689 case '#':
690 *q++ = '\\';
691 *q++ = *p;
692 nbs = 0;
693 break;
694 case '\\':
695 *q++ = *p;
696 nbs++;
697 break;
698 default:
699 *q++ = *p;
700 nbs = 0;
701 break;
702 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700703 }
704 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400705 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700706
707 *q = '\0';
708
709 return os;
710}
711
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700712/*
713 * Convert a string to a Watcom make-safe form
714 */
715static char *quote_for_wmake(const char *str)
716{
717 const char *p;
718 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700719 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700720
721 size_t n = 1; /* Terminating zero */
722
723 if (!str)
724 return NULL;
725
726 for (p = str; *p; p++) {
727 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700728 case ' ':
729 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700730 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700731 quote = true;
732 n++;
733 break;
734 case '\"':
735 quote = true;
736 n += 2;
737 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700738 case '$':
739 case '#':
740 n += 2;
741 break;
742 default:
743 n++;
744 break;
745 }
746 }
747
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700748 if (quote)
749 n += 2;
750
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700751 os = q = nasm_malloc(n);
752
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700753 if (quote)
754 *q++ = '\"';
755
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700756 for (p = str; *p; p++) {
757 switch (*p) {
758 case '$':
759 case '#':
760 *q++ = '$';
761 *q++ = *p;
762 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700763 case '\"':
764 *q++ = *p;
765 *q++ = *p;
766 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700767 default:
768 *q++ = *p;
769 break;
770 }
771 }
772
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700773 if (quote)
774 *q++ = '\"';
775
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700776 *q = '\0';
777
778 return os;
779}
780
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100781enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800782 OPT_BOGUS,
783 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700784 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800785 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700786 OPT_MANGLE,
787 OPT_INCLUDE,
788 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700789 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700790 OPT_LIMIT,
791 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100792};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800793struct textargs {
794 const char *label;
795 enum text_options opt;
796 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700797 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800798};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800799static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700800 {"v", OPT_VERSION, false, 0},
801 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700802 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700803 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
804 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
805 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
806 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
807 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
808 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
809 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700810 {"include", OPT_INCLUDE, true, 0},
811 {"pragma", OPT_PRAGMA, true, 0},
812 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700813 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700814 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700815 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000816};
817
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400818static void show_version(void)
819{
820 printf("NASM version %s compiled on %s%s\n",
821 nasm_version, nasm_date, nasm_compile_options);
822 exit(0);
823}
824
H. Peter Anvin423e3812007-11-15 17:12:29 -0800825static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700826static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000827{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000828 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800829 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000830
831 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800832 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400835 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
836 /* These parameters take values */
837 if (!(param = get_param(p, q, &advance)))
838 return advance;
839 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 switch (p[1]) {
842 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700843 if (pass == 1)
844 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700846
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700848 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800849 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400850 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700853 if (pass == 1) {
854 ofmt = ofmt_find(param, &ofmt_alias);
855 if (!ofmt) {
856 nasm_fatal(ERR_NOFILE | ERR_USAGE,
857 "unrecognised output format `%s' - "
858 "use -hf for a list", param);
859 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 }
861 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700862
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700864 if (pass == 2) {
865 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700866
H. Peter Anvin55568c12016-10-03 19:46:49 -0700867 if (!*param) {
868 /* Naked -O == -Ox */
869 optimizing = MAX_OPTIMIZE;
870 } else {
871 while (*param) {
872 switch (*param) {
873 case '0': case '1': case '2': case '3': case '4':
874 case '5': case '6': case '7': case '8': case '9':
875 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700876
H. Peter Anvin55568c12016-10-03 19:46:49 -0700877 /* -O0 -> optimizing == -1, 0.98 behaviour */
878 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
879 if (opt < 2)
880 optimizing = opt - 1;
881 else
882 optimizing = opt;
883 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700884
H. Peter Anvin55568c12016-10-03 19:46:49 -0700885 case 'v':
886 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400887 param++;
888 opt_verbose_info = true;
889 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700890
H. Peter Anvin55568c12016-10-03 19:46:49 -0700891 case 'x':
892 param++;
893 optimizing = MAX_OPTIMIZE;
894 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700895
H. Peter Anvin55568c12016-10-03 19:46:49 -0700896 default:
897 nasm_fatal(0,
898 "unknown optimization option -O%c\n",
899 *param);
900 break;
901 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700903 if (optimizing > MAX_OPTIMIZE)
904 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400905 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 }
907 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800908
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400909 case 'p': /* pre-include */
910 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700911 if (pass == 2)
912 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800914
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400915 case 'd': /* pre-define */
916 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 if (pass == 2)
918 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400919 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800920
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400921 case 'u': /* un-define */
922 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700923 if (pass == 2)
924 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800926
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 case 'i': /* include search path */
928 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 if (pass == 2)
930 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400931 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800932
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700934 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800935 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400936 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800937
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700939 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800940 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800942
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 if (pass == 2) {
945 using_debug_info = true;
946 debug_format = param;
947 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800949
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400950 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700951 if (pass == 1) {
952 if (nasm_stricmp("vc", param) == 0)
953 nasm_set_verror(nasm_verror_vc);
954 else if (nasm_stricmp("gnu", param) == 0)
955 nasm_set_verror(nasm_verror_gnu);
956 else
957 nasm_fatal(ERR_NOFILE | ERR_USAGE,
958 "unrecognized error reporting format `%s'",
959 param);
960 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700964 if (pass == 2) {
965 using_debug_info = true;
966 if (p[2])
967 debug_format = nasm_skip_spaces(p + 2);
968 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700972 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400973 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800975
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 case 'y':
977 printf("\nvalid debug formats for '%s' output format are"
978 " ('*' denotes default):\n", ofmt->shortname);
979 dfmt_list(ofmt, stdout);
980 exit(0);
981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700984 if (pass == 2)
985 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400989 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800991
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400992 case 'e': /* preprocess only */
993 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 1)
995 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800997
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400998 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700999 if (pass == 1)
1000 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001002
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001003 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001004 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001005 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001006 if (!set_warning_status(param)) {
1007 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1008 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001014 if (pass == 1) {
1015 switch (p[2]) {
1016 case 'W':
1017 quote_for_make = quote_for_wmake;
1018 break;
1019 case 'D':
1020 case 'F':
1021 case 'T':
1022 case 'Q':
1023 advance = true;
1024 break;
1025 default:
1026 break;
1027 }
1028 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001029 switch (p[2]) {
1030 case 0:
1031 operating_mode = OP_DEPEND;
1032 break;
1033 case 'G':
1034 operating_mode = OP_DEPEND;
1035 depend_missing_ok = true;
1036 break;
1037 case 'P':
1038 depend_emit_phony = true;
1039 break;
1040 case 'D':
1041 operating_mode = OP_NORMAL;
1042 depend_file = q;
1043 advance = true;
1044 break;
1045 case 'F':
1046 depend_file = q;
1047 advance = true;
1048 break;
1049 case 'T':
1050 depend_target = q;
1051 advance = true;
1052 break;
1053 case 'Q':
1054 depend_target = quote_for_make(q);
1055 advance = true;
1056 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001057 case 'W':
1058 /* handled in pass 1 */
1059 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001060 default:
1061 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1062 "unknown dependency option `-M%c'", p[2]);
1063 break;
1064 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001065 }
1066 if (advance && (!q || !q[0])) {
1067 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1068 "option `-M%c' requires a parameter", p[2]);
1069 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001070 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001072
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 case '-':
1074 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001075 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001076 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001077 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001078
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001079 p += 2;
1080
1081 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001082 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
1084 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001085
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001086 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001087 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001088 olen = strlen(tx->label);
1089
1090 if (olen > plen)
1091 continue;
1092
1093 if (nasm_memicmp(p, tx->label, olen))
1094 continue;
1095
1096 if (tx->label[olen-1] == '-')
1097 break; /* Incomplete option */
1098
1099 if (!p[olen] || p[olen] == '=')
1100 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001102
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001103 if (!tx->label) {
1104 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1105 "unrecognized option `--%s'", p);
1106 }
1107
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001108 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001109 if (param)
1110 *param++ = '\0';
1111
H. Peter Anvin3366e312018-02-07 14:14:36 -08001112 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001113 if (!param) {
1114 param = q;
1115 advance = true;
1116 }
1117
1118 /* Note: a null string is a valid parameter */
1119 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001120 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001121 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001122 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 break;
1124 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001125 } else {
1126 if (param) {
1127 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1128 "option `--%s' does not take an argument",
1129 p);
1130
1131 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001132 }
1133
1134 switch (tx->opt) {
1135 case OPT_VERSION:
1136 show_version();
1137 break;
1138 case OPT_ABORT_ON_PANIC:
1139 abort_on_panic = true;
1140 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001141 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001142 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001143 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001144 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001145 case OPT_INCLUDE:
1146 if (pass == 2)
1147 preproc->pre_include(q);
1148 break;
1149 case OPT_PRAGMA:
1150 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001151 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001152 break;
1153 case OPT_BEFORE:
1154 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001155 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001156 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001157 case OPT_LIMIT:
1158 if (pass == 2)
1159 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001160 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001161 case OPT_KEEP_ALL:
1162 keep_all = true;
1163 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001164 case OPT_HELP:
1165 help(0);
1166 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001167 default:
1168 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001170
1171 if (eqsave)
1172 *eqsave = '='; /* Restore = argument separator */
1173
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 break;
1175 }
1176
1177 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001178 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1179 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 break;
1181 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001182 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001183 /* In theory we could allow multiple input files... */
1184 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001185 }
1186
1187 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001188}
1189
H. Peter Anvineba20a72002-04-30 20:53:55 +00001190#define ARG_BUF_DELTA 128
1191
H. Peter Anvin55568c12016-10-03 19:46:49 -07001192static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001193{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001194 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195 int bufsize, prevargsize;
1196
1197 bufsize = prevargsize = ARG_BUF_DELTA;
1198 buffer = nasm_malloc(ARG_BUF_DELTA);
1199 prevarg = nasm_malloc(ARG_BUF_DELTA);
1200 prevarg[0] = '\0';
1201
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 p = buffer;
1204 while (1) { /* Loop to handle long lines */
1205 q = fgets(p, bufsize - (p - buffer), rfile);
1206 if (!q)
1207 break;
1208 p += strlen(p);
1209 if (p > buffer && p[-1] == '\n')
1210 break;
1211 if (p - buffer > bufsize - 10) {
1212 int offset;
1213 offset = p - buffer;
1214 bufsize += ARG_BUF_DELTA;
1215 buffer = nasm_realloc(buffer, bufsize);
1216 p = buffer + offset;
1217 }
1218 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001219
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 if (!q && p == buffer) {
1221 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001222 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 nasm_free(buffer);
1224 nasm_free(prevarg);
1225 return;
1226 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 /*
1229 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1230 * them are present at the end of the line.
1231 */
1232 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001233
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001234 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001236
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001237 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238
H. Peter Anvin55568c12016-10-03 19:46:49 -07001239 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241
Charles Crayne192d5b52007-10-18 19:02:42 -07001242 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 prevargsize += ARG_BUF_DELTA;
1244 prevarg = nasm_realloc(prevarg, prevargsize);
1245 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001246 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001247 }
1248}
1249
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001250/* Function to process args from a string of args, rather than the
1251 * argv array. Used by the environment variable and response file
1252 * processing.
1253 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001254static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001256 char *p, *q, *arg, *prevarg;
1257 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001258
1259 p = args;
1260 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001262 arg = NULL;
1263 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 q = p;
1265 while (*p && *p != separator)
1266 p++;
1267 while (*p == separator)
1268 *p++ = '\0';
1269 prevarg = arg;
1270 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001271 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001273 }
1274 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001275 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001276}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001277
H. Peter Anvin55568c12016-10-03 19:46:49 -07001278static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001279{
1280 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001281 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001282 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001283 perror(file);
1284 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001285 }
1286 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001287 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001288 }
1289 fclose(f);
1290}
1291
H. Peter Anvin55568c12016-10-03 19:46:49 -07001292static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001293{
1294 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001295 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001296 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001297
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001298 /* Initialize all the warnings to their default state */
1299 for (i = 0; i < ERR_WARN_ALL; i++) {
1300 warning_state_init[i] = warning_state[i] =
1301 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1302 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001303
1304 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001305 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001306 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001307 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001308 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001310 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001312 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001313
1314 /*
1315 * Now process the actual command line.
1316 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001318 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 argv++;
1320 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001321 /*
1322 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 * arguments like the environment variable. This allows us
1324 * to have multiple arguments on a single line, which is
1325 * different to the -@resp file processing below for regular
1326 * NASM.
1327 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001328 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 argc--;
1330 argv++;
1331 }
1332 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001333 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001334 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001335 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001336 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001337 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 fclose(rfile);
1339 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001340 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 "unable to open response file `%s'", p);
1342 }
1343 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001344 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001345 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001346 }
1347
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001348 /*
1349 * Look for basic command line typos. This definitely doesn't
1350 * catch all errors, but it might help cases of fumbled fingers.
1351 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001352 if (pass != 2)
1353 return;
1354
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001355 if (!inname)
1356 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001357
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001358 else if ((errname && !strcmp(inname, errname)) ||
1359 (outname && !strcmp(inname, outname)) ||
1360 (listname && !strcmp(inname, listname)) ||
1361 (depend_file && !strcmp(inname, depend_file)))
1362 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1363
1364 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001365 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001366 if (!error_file) {
1367 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001368 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001369 "cannot open file `%s' for error messages",
1370 errname);
1371 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001372 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001373}
1374
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001375static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001376{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001377 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001379 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001380 uint64_t prev_offset_changed;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001381 int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001382
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001383 switch (cmd_sb) {
1384 case 16:
1385 break;
1386 case 32:
1387 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1388 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1389 break;
1390 case 64:
1391 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1392 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1393 break;
1394 default:
1395 panic();
1396 break;
1397 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001398
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001399 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001400 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001401 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001402 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1403 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001404
H. Peter Anvincac0b192017-03-28 16:12:30 -07001405 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 cpu = cmd_cpu;
1407 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001408 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001409 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001410 /* Remove the list file in case we die before the output pass */
1411 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001413 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001414 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001415 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 saa_rewind(forwrefs);
1417 forwref = saa_rstruct(forwrefs);
1418 raa_free(offsets);
1419 offsets = raa_init();
1420 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001421 location.segment = NO_SEG;
1422 location.offset = 0;
1423 if (passn == 1)
1424 location.known = true;
1425 ofmt->reset();
1426 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001427 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001428
1429 /* Revert all warnings to the default state */
1430 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001433
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001435 if (globallineno++ == GLOBALLINENO_MAX)
1436 nasm_error(ERR_FATAL,
1437 "overall line number reaches the maximum %d\n",
1438 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001439
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001440 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001441 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001442 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001443 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001444 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001445 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001446
H. Peter Anvinc7131682017-03-07 17:45:01 -08001447 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001448 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001449
1450 if (optimizing > 0) {
1451 if (forwref != NULL && globallineno == forwref->lineno) {
1452 output_ins.forw_ref = true;
1453 do {
1454 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1455 forwref = saa_rstruct(forwrefs);
1456 } while (forwref != NULL
1457 && forwref->lineno == globallineno);
1458 } else
1459 output_ins.forw_ref = false;
1460
1461 if (output_ins.forw_ref) {
1462 if (passn == 1) {
1463 for (i = 0; i < output_ins.operands; i++) {
1464 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1465 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1466 fwinf->lineno = globallineno;
1467 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001468 }
1469 }
1470 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001471 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001472 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001473
H. Peter Anvinc7131682017-03-07 17:45:01 -08001474 /* forw_ref */
1475 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001476 if (!output_ins.label)
1477 nasm_error(ERR_NONFATAL,
1478 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001479
H. Peter Anvin98578072018-06-01 18:02:54 -07001480 if (output_ins.operands == 1 &&
1481 (output_ins.oprs[0].type & IMMEDIATE) &&
1482 output_ins.oprs[0].wrt == NO_SEG) {
1483 define_label(output_ins.label,
1484 output_ins.oprs[0].segment,
1485 output_ins.oprs[0].offset, false);
1486 } else if (output_ins.operands == 2
1487 && (output_ins.oprs[0].type & IMMEDIATE)
1488 && (output_ins.oprs[0].type & COLON)
1489 && output_ins.oprs[0].segment == NO_SEG
1490 && output_ins.oprs[0].wrt == NO_SEG
1491 && (output_ins.oprs[1].type & IMMEDIATE)
1492 && output_ins.oprs[1].segment == NO_SEG
1493 && output_ins.oprs[1].wrt == NO_SEG) {
1494 define_label(output_ins.label,
1495 output_ins.oprs[0].offset | SEG_ABS,
1496 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001497 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001498 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001499 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001500 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001501 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001503 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001504
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001505 for (n = 1; n <= output_ins.times; n++) {
1506 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001507 int64_t l = insn_size(location.segment,
1508 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001509 globalbits, &output_ins);
1510
1511 /* if (using_debug_info) && output_ins.opcode != -1) */
1512 if (using_debug_info)
1513 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001514 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001515 int32_t typeinfo =
1516 TYS_ELEMENTS(output_ins.operands);
1517 switch (output_ins.opcode) {
1518 case I_RESB:
1519 typeinfo =
1520 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1521 break;
1522 case I_RESW:
1523 typeinfo =
1524 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1525 break;
1526 case I_RESD:
1527 typeinfo =
1528 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1529 break;
1530 case I_RESQ:
1531 typeinfo =
1532 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1533 break;
1534 case I_REST:
1535 typeinfo =
1536 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1537 break;
1538 case I_RESO:
1539 typeinfo =
1540 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1541 break;
1542 case I_RESY:
1543 typeinfo =
1544 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1545 break;
1546 case I_RESZ:
1547 typeinfo =
1548 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1549 break;
1550 case I_DB:
1551 typeinfo |= TY_BYTE;
1552 break;
1553 case I_DW:
1554 typeinfo |= TY_WORD;
1555 break;
1556 case I_DD:
1557 if (output_ins.eops_float)
1558 typeinfo |= TY_FLOAT;
1559 else
1560 typeinfo |= TY_DWORD;
1561 break;
1562 case I_DQ:
1563 typeinfo |= TY_QWORD;
1564 break;
1565 case I_DT:
1566 typeinfo |= TY_TBYTE;
1567 break;
1568 case I_DO:
1569 typeinfo |= TY_OWORD;
1570 break;
1571 case I_DY:
1572 typeinfo |= TY_YWORD;
1573 break;
1574 case I_DZ:
1575 typeinfo |= TY_ZWORD;
1576 break;
1577 default:
1578 typeinfo = TY_LABEL;
1579 break;
1580 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001581
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001582 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001583 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001584
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001585 /*
1586 * For INCBIN, let the code in assemble
1587 * handle TIMES, so we don't have to read the
1588 * input file over and over.
1589 */
1590 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001591 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001592 }
1593 /*
1594 * else l == -1 => invalid instruction, which will be
1595 * flagged as an error on pass 2
1596 */
1597 } else {
1598 if (n == 2)
1599 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001600 increment_offset(assemble(location.segment,
1601 location.offset,
1602 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001604 } /* not an EQU */
1605 }
1606 if (output_ins.times > 1)
1607 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001608
H. Peter Anvinc7131682017-03-07 17:45:01 -08001609 cleanup_insn(&output_ins);
1610
1611 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001614
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001615 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001616 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001617 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 if (pass1 == 1)
1620 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001622 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001624 } else if (global_offset_changed &&
1625 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001626 prev_offset_changed = global_offset_changed;
1627 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001628 } else {
1629 stall_count++;
1630 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001631
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001632 if (terminate_after_phase)
1633 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001634
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001635 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1636 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001637 /* We get here if the labels don't converge
1638 * Example: FOO equ FOO + 1
1639 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001640 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001641 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001642 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001643 nasm_error(ERR_NONFATAL,
1644 "Possible causes: recursive EQUs, macro abuse.");
1645 break;
1646 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001647 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001650 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001651 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001652 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001653 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1654 }
1655}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656
Ed Berosetfa771012002-06-09 20:56:40 +00001657/**
1658 * gnu style error reporting
1659 * This function prints an error message to error_file in the
1660 * style used by GNU. An example would be:
1661 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001662 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001663 * which the error occurs (or is detected) and "error:" is one of
1664 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001665 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001666 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001667 *
1668 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001669 * @param fmt the printf style format string
1670 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001671static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001672{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001673 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001674 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001675
Ed Berosetfa771012002-06-09 20:56:40 +00001676 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001678
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001679 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001680 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001681 if (!currentfile || (severity & ERR_TOPFILE)) {
1682 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1683 lineno = 0;
1684 }
1685 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001686
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001687 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001688 if (!lineno)
1689 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001690 else
1691 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001692 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001693
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001694 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001695}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001696
Ed Berosetfa771012002-06-09 20:56:40 +00001697/**
1698 * MS style error reporting
1699 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001700 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001701 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001702 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001703 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001704 * which the error occurs (or is detected) and "error:" is one of
1705 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001706 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001707 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001708 *
1709 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001710 * @param fmt the printf style format string
1711 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001712static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001713{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001714 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001715 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 if (is_suppressed_warning(severity))
1718 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001719
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001720 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001722
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001723 if (!skip_this_pass(severity)) {
1724 if (currentfile) {
1725 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001726 } else {
1727 fputs("nasm: ", error_file);
1728 }
Ed Berosetfa771012002-06-09 20:56:40 +00001729 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001730
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001731 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001732}
1733
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001734/*
1735 * check to see if this is a suppressable warning
1736 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001737static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001738{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001739 /* Not a warning at all */
1740 if ((severity & ERR_MASK) != ERR_WARNING)
1741 return false;
1742
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001743 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001744}
1745
Ed Berosetfa771012002-06-09 20:56:40 +00001746/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001747 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001748 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001749 * not in pass 1
1750 *
1751 * @param severity the severity of the warning or error
1752 * @return true if we should abort error/warning printing
1753 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001754static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001755{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001756 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001757 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001758 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1759 else
1760 return false;
1761}
1762
1763static bool warning_is_error(int severity)
1764{
1765 if (is_valid_warning(severity))
1766 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001767 else
1768 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001769}
1770
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001771static bool skip_this_pass(int severity)
1772{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001773 /*
1774 * See if it's a pass-specific error or warning which should be skipped.
1775 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1776 * they cannot be resumed from.
1777 */
1778 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001779 return false;
1780
H. Peter Anvin934f0472016-05-09 12:00:19 -07001781 /*
1782 * passn is 1 on the very first pass only.
1783 * pass0 is 2 on the code-generation (final) pass only.
1784 * These are the passes we care about in this case.
1785 */
1786 return (((severity & ERR_PASS1) && passn != 1) ||
1787 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001788}
1789
Ed Berosetfa771012002-06-09 20:56:40 +00001790/**
1791 * common error reporting
1792 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001793 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001794 * specific error message to error_file and may or may not return. It
1795 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001796 *
1797 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001798 * @param fmt the printf style format string
1799 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001800static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001801{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001802 char msg[1024];
1803 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001804
H. Peter Anvin7df04172008-06-10 18:27:38 -07001805 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001807 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 break;
1809 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001810 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
1812 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001813 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
1815 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001816 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 break;
1818 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001819 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001821 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001822 pfx = "";
1823 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001824 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001825
H. Peter Anvin934f0472016-05-09 12:00:19 -07001826 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001827 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001828 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001829 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1830 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001831
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001832 if (!skip_this_pass(severity))
1833 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001834
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001835 /* Are we recursing from error_list_macros? */
1836 if (severity & ERR_PP_LISTMACRO)
1837 return;
1838
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001839 /*
1840 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001841 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001842 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001843 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001844
H. Peter Anvin8f622462017-04-02 19:02:29 -07001845 if (skip_this_pass(severity))
1846 return;
1847
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001848 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001849 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001850
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001851 preproc->error_list_macros(severity);
1852
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001853 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 case ERR_DEBUG:
1855 /* no further action, by definition */
1856 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001857 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001858 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001859 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001860 terminate_after_phase = true;
1861 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001863 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 break;
1865 case ERR_FATAL:
1866 if (ofile) {
1867 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001868 if (!keep_all)
1869 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001870 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 }
1872 if (want_usage)
1873 usage();
1874 exit(1); /* instantly die */
1875 break; /* placate silly compilers */
1876 case ERR_PANIC:
1877 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001878
1879 if (abort_on_panic)
1880 abort(); /* halt, catch fire, dump core/stop debugger */
1881
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001882 if (ofile) {
1883 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001884 if (!keep_all)
1885 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001886 ofile = NULL;
1887 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 exit(3);
1889 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001890 }
1891}
1892
H. Peter Anvin734b1882002-04-30 21:01:08 +00001893static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001894{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001895 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001896}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001897
1898static void help(const char xopt)
1899{
1900 int i;
1901
1902 printf
1903 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1904 "[-l listfile]\n"
1905 " [options...] [--] filename\n"
1906 " or nasm -v (or --v) for version info\n\n"
1907 "\n"
1908 "Response files should contain command line parameters,\n"
1909 "one per line.\n"
1910 "\n"
1911 " -t assemble in SciTech TASM compatible mode\n");
1912 printf
1913 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1914 " -a don't preprocess (assemble only)\n"
1915 " -M generate Makefile dependencies on stdout\n"
1916 " -MG d:o, missing files assumed generated\n"
1917 " -MF file set Makefile dependency file\n"
1918 " -MD file assemble and generate dependencies\n"
1919 " -MT file dependency target name\n"
1920 " -MQ file dependency target name (quoted)\n"
1921 " -MP emit phony target\n\n"
1922 " -Zfile redirect error messages to file\n"
1923 " -s redirect error messages to stdout\n\n"
1924 " -g generate debugging information\n\n"
1925 " -F format select a debugging format\n\n"
1926 " -gformat same as -g -F format\n\n"
1927 " -o outfile write output to an outfile\n\n"
1928 " -f format select an output format\n\n"
1929 " -l listfile write listing to a listfile\n\n"
1930 " -Ipath add a pathname to the include file path\n");
1931 printf
1932 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1933 " -O0 no optimization\n"
1934 " -O1 minimal optimization\n"
1935 " -Ox multipass optimization (default)\n"
1936 " -Pfile pre-include a file (also --include)\n"
1937 " -Dmacro[=str] pre-define a macro\n"
1938 " -Umacro undefine a macro\n"
1939 " -Xformat specifiy error reporting format (gnu or vc)\n"
1940 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1941 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1942 " -w[+-]error[=foo]\n"
1943 " promote [specific] warnings to errors\n"
1944 " -h show invocation summary and exit\n\n"
1945 " --pragma str pre-executes a specific %%pragma\n"
1946 " --before str add line (usually a preprocessor statement) before the input\n"
1947 " --prefix str prepend the given string to all the given string\n"
1948 " to all extern, common and global symbols\n"
1949 " --suffix str append the given string to all the given string\n"
1950 " to all extern, common and global symbols\n"
1951 " --lprefix str prepend the given string to all other symbols\n"
1952 " --limit-X val set execution limit X\n");
1953
1954 for (i = 0; i <= LIMIT_MAX; i++) {
1955 printf(" %-15s %s (default ",
1956 limit_info[i].name, limit_info[i].help);
1957 if (nasm_limit[i] < LIMIT_MAX_VAL) {
1958 printf("%d)\n", nasm_limit[i]);
1959 } else {
1960 printf("unlimited)\n");
1961 }
1962 }
1963
1964 printf("\nWarnings for the -W/-w options:\n");
1965
1966 for (i = 0; i <= ERR_WARN_ALL; i++)
1967 printf(" %-23s %s%s\n",
1968 warnings[i].name, warnings[i].help,
1969 i == ERR_WARN_ALL ? "\n" :
1970 warnings[i].enabled ? " (default on)" :
1971 " (default off)");
1972
1973 if (xopt == 'f') {
1974 printf("valid output formats for -f are"
1975 " (`*' denotes default):\n");
1976 ofmt_list(ofmt, stdout);
1977 } else {
1978 printf("For a list of valid output formats, use -hf.\n");
1979 printf("For a list of debug formats, use -f <format> -y.\n");
1980 }
1981}