blob: bd6ccf83ceea71e4437ff17fc27c4e0b6b833eb6 [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
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin322bee02019-08-10 01:38:06 -070072const char *_progname;
73
H. Peter Anvin55568c12016-10-03 19:46:49 -070074static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030075static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080076static bool skip_this_pass(errflags severity);
77static void nasm_verror_asm(errflags severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070079static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000080
H. Peter Anvin77016c82018-12-10 22:29:49 -080081struct error_format {
82 const char *beforeline; /* Before line number, if present */
83 const char *afterline; /* After line number, if present */
84 const char *beforemsg; /* Before actual message */
85};
86
87static const struct error_format errfmt_gnu = { ":", "", ": " };
88static const struct error_format errfmt_msvc = { "(", ")", " : " };
89static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080090static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080091
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070092unsigned int debug_nasm; /* Debugging messages? */
93
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080094static bool using_debug_info, opt_verbose_info;
95static const char *debug_format;
96
H. Peter Anvin3366e312018-02-07 14:14:36 -080097#ifndef ABORT_ON_PANIC
98# define ABORT_ON_PANIC 0
99#endif
100static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700101static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800102
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700103bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800104enum pass_type _pass_type;
105const char * const _pass_types[] =
106{
107 "init", "first", "optimize", "stabilize", "final"
108};
109int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000110int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800111int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000112
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700113struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800114
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800115const char *inname;
116const char *outname;
117static const char *listname;
118static const char *errname;
119
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700120static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700121
H. Peter Anvin338656c2016-02-17 20:59:22 -0800122const struct ofmt *ofmt = &OF_DEFAULT;
123const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400124const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800126FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000127
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400128FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300129struct optimization optimizing =
130 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100131static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400132
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800133iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400134static iflag_t cmd_cpu;
135
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800136struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800137bool in_absolute; /* Flag we are in ABSOLUTE seg */
138struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000141
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700143static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144
H. Peter Anvine7469712016-02-18 02:20:59 -0800145static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300146static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800147bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400148
H. Peter Anvin34754622018-11-28 12:36:53 -0800149#define OP_NORMAL (1U << 0)
150#define OP_PREPROCESS (1U << 1)
151#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400152
153static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400154
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700155/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700156static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700157static bool depend_missing_ok = false;
158static const char *depend_target = NULL;
159static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300160struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000161
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700162static bool want_usage;
163static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800164bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700166static char *quote_for_pmake(const char *str);
167static char *quote_for_wmake(const char *str);
168static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700169
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700170/*
171 * Execution limits that can be set via a command-line option or %pragma
172 */
173
H. Peter Anvin322bee02019-08-10 01:38:06 -0700174/*
175 * This is really unlimited; it would take far longer than the
176 * current age of the universe for this limit to be reached even on
177 * much faster CPUs than currently exist.
178*/
179#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180
H. Peter Anvin322bee02019-08-10 01:38:06 -0700181int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700182
183struct limit_info {
184 const char *name;
185 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700186 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700187};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700188/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700190 { "passes", "total number of passes", LIMIT_MAX_VAL },
191 { "stalled-passes", "number of passes without forward progress", 1000 },
192 { "macro-levels", "levels of macro expansion", 10000 },
193 { "macro-tokens", "tokens processed during macro expansion", 10000000 },
194 { "rep", "%rep count", 1000000 },
195 { "eval", "expression evaluation descent", 1000000},
196 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700197};
198
H. Peter Anvin322bee02019-08-10 01:38:06 -0700199static void set_default_limits(void)
200{
201 int i;
202 for (i = 0; i <= LIMIT_MAX; i++)
203 nasm_limit[i] = limit_info[i].default_val;
204}
205
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700206enum directive_result
207nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700208{
209 int i;
210 int64_t val;
211 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700212 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213
214 for (i = 0; i <= LIMIT_MAX; i++) {
215 if (!nasm_stricmp(limit, limit_info[i].name))
216 break;
217 }
218 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800219 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800220 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700221 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700222 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700223 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700224 return DIRR_ERROR;
225 }
226
227 if (!nasm_stricmp(valstr, "unlimited")) {
228 val = LIMIT_MAX_VAL;
229 } else {
230 val = readnum(valstr, &rn_error);
231 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800232 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800233 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700234 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700235 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700236 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700237 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700238 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700239 if (val > LIMIT_MAX_VAL)
240 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700241 }
242
243 nasm_limit[i] = val;
244 return DIRR_OK;
245}
246
H. Peter Anvin892c4812018-05-30 14:43:46 -0700247int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400248{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700249 location.segment = segment;
250 if (segment == NO_SEG) {
251 location.offset = absolute.offset;
252 in_absolute = true;
253 } else {
254 location.offset = raa_read(offsets, segment);
255 in_absolute = false;
256 }
257 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400258}
259
260static void set_curr_offs(int64_t l_off)
261{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800262 if (in_absolute)
263 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400264 else
265 offsets = raa_write(offsets, location.segment, l_off);
266}
267
H. Peter Anvin892c4812018-05-30 14:43:46 -0700268static void increment_offset(int64_t delta)
269{
270 if (unlikely(delta == 0))
271 return;
272
273 location.offset += delta;
274 set_curr_offs(location.offset);
275}
276
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000277static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000278{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000279 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800281 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000282 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000284}
285
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800286/*
287 * Define system-defined macros that are not part of
288 * macros/standard.mac.
289 */
290static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700292 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800293 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800294
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700295 if (oct->have_local) {
296 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400297 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700298 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400299 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700300 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400301 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700302 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400303 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800304 }
305
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700306 if (oct->have_gm) {
307 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400308 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700309 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400310 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700311 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400312 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700313 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400314 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800315 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700316
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700317 if (oct->have_posix) {
318 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400319 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800320 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800321
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400322 /*
323 * In case if output format is defined by alias
324 * we have to put shortname of the alias itself here
325 * otherwise ABI backward compatibility gets broken.
326 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400327 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400328 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400329 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800330
331 /*
332 * Output-format specific macros.
333 */
334 if (ofmt->stdmac)
335 preproc->extra_stdmac(ofmt->stdmac);
336
337 /*
338 * Debug format, if any
339 */
340 if (dfmt != &null_debug_form) {
341 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
342 preproc->pre_define(temp);
343 }
344}
345
346/*
347 * Initialize the preprocessor, set up the include path, and define
348 * the system-included macros. This is called between passes 1 and 2
349 * of parsing the command options; ofmt and dfmt are defined at this
350 * point.
351 *
352 * Command-line specified preprocessor directives (-p, -d, -u,
353 * --pragma, --before) are processed after this function.
354 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300355static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800356{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800357 preproc->init();
358 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300359 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800360}
361
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300362static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700363{
364 FILE *deps;
365 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700366 bool wmake = (quote_for_make == quote_for_wmake);
367 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800368 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700369
370 if (!list)
371 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700372
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700373 wrapstr = wmake ? " &\n " : " \\\n ";
374 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700375
376 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700377 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400378 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800379 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400380 return;
381 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700382 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400383 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700384 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700385
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700386 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800387 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700388 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400389 len = strlen(file);
390 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700391 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400392 linepos = 1;
393 }
394 fprintf(deps, " %s", file);
395 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700396 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700397 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700398 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700399
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800400 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700401 if (depend_emit_phony) {
402 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700403 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700404 nasm_free(file);
405 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700406 }
407
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800408 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700409
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700410 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400411 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700412}
413
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700414/* Convert a struct tm to a POSIX-style time constant */
415static int64_t make_posix_time(const struct tm *tm)
416{
417 int64_t t;
418 int64_t y = tm->tm_year;
419
420 /* See IEEE 1003.1:2004, section 4.14 */
421
422 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
423 t += tm->tm_yday;
424 t *= 24;
425 t += tm->tm_hour;
426 t *= 60;
427 t += tm->tm_min;
428 t *= 60;
429 t += tm->tm_sec;
430
431 return t;
432}
433
434static void timestamp(void)
435{
436 struct compile_time * const oct = &official_compile_time;
437 const struct tm *tp, *best_gm;
438
439 time(&oct->t);
440
441 best_gm = NULL;
442
443 tp = localtime(&oct->t);
444 if (tp) {
445 oct->local = *tp;
446 best_gm = &oct->local;
447 oct->have_local = true;
448 }
449
450 tp = gmtime(&oct->t);
451 if (tp) {
452 oct->gm = *tp;
453 best_gm = &oct->gm;
454 oct->have_gm = true;
455 if (!oct->have_local)
456 oct->local = oct->gm;
457 } else {
458 oct->gm = oct->local;
459 }
460
461 if (best_gm) {
462 oct->posix = make_posix_time(best_gm);
463 oct->have_posix = true;
464 }
465}
466
H. Peter Anvin038d8612007-04-12 16:54:50 +0000467int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468{
H. Peter Anvin322bee02019-08-10 01:38:06 -0700469 _progname = argv[0];
470 if (!_progname || !_progname[0])
471 _progname = "nasm";
472
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700473 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800474
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800475 error_file = stderr;
476
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800477 iflag_set_default_cpu(&cpu);
478 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400479
H. Peter Anvin322bee02019-08-10 01:38:06 -0700480 set_default_limits();
481
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800482 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300483
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800484 _pass_type = PASS_INIT;
485 _passn = 0;
486
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700487 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800488 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000489
H. Peter Anvin13506202018-11-28 14:55:58 -0800490 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700491 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700492
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700493 /*
494 * We must call init_labels() before the command line parsing,
495 * because we may be setting prefixes/suffixes from the command
496 * line.
497 */
498 init_labels();
499
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000501 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000502
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000503 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400504 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000505
H. Peter Anvin55568c12016-10-03 19:46:49 -0700506 parse_cmdline(argc, argv, 1);
507 if (terminate_after_phase) {
508 if (want_usage)
509 usage();
510 return 1;
511 }
512
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800513 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800514 if (!using_debug_info) {
515 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800516 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800517 } else if (!debug_format) {
518 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300519 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800520 } else {
521 dfmt = dfmt_find(ofmt, debug_format);
522 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800523 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800524 debug_format, ofmt->shortname);
525 }
526 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000527
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300528 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800529
530 parse_cmdline(argc, argv, 2);
531 if (terminate_after_phase) {
532 if (want_usage)
533 usage();
534 return 1;
535 }
536
537 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800538 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000539
H. Peter Anvin34754622018-11-28 12:36:53 -0800540 /* Dependency filename if we are also doing other things */
541 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
542 if (outname)
543 depend_file = nasm_strcat(outname, ".d");
544 else
545 depend_file = filename_set_extension(inname, ".d");
546 }
547
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300548 /*
549 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800550 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300551 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300552 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800553 if (!outname && !(operating_mode & OP_PREPROCESS)) {
554 outname = filename_set_extension(inname, ofmt->extension);
555 if (!strcmp(outname, inname)) {
556 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800557 nasm_warn(WARN_OTHER, "default output file same as input, using `%s' for output\n", outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800558 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300559 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800560
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800561 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700562
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700563 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400564 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700565
H. Peter Anvin34754622018-11-28 12:36:53 -0800566 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000567 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700568
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400569 if (depend_missing_ok)
570 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700571
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800572 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 while ((line = preproc->getline()))
575 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800576 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800577 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400578 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000579 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700580 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000581 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800584 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700585 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800587 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 } else
589 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000590
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700591 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000592
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800593 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
594 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800595
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 while ((line = preproc->getline())) {
597 /*
598 * We generate %line directives if needed for later programs
599 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000600 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 int altline = src_get(&linnum, &file_name);
602 if (altline) {
603 if (altline == 1 && lineinc == 1)
604 nasm_fputs("", ofile);
605 else {
606 lineinc = (altline != -1 || lineinc != 1);
607 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000608 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 file_name);
610 }
611 prior_linnum = linnum;
612 }
613 nasm_fputs(line, ofile);
614 nasm_free(line);
615 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800616 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800617 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 if (ofile)
619 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700620 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400622 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400623 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400625 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700626 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400627 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800628 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400630 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400631 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700633 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200634
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400635 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800636 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400637 cleanup_labels();
638 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800639 if (ferror(ofile))
640 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400641 }
642
643 if (ofile) {
644 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700645 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400646 remove(outname);
647 ofile = NULL;
648 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000649 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000650
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800651 preproc->cleanup_session();
652
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700653 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400654 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700655
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000656 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000658
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 raa_free(offsets);
660 saa_free(forwrefs);
661 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000662 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700663 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800664 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000665
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700666 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000667}
668
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669/*
670 * Get a parameter for a command line option.
671 * First arg must be in the form of e.g. -f...
672 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800673static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000674{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800675 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400676 if (p[2]) /* the parameter's in the option */
677 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800679 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800682 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000683 return NULL;
684}
685
H. Peter Anvindc242712007-11-18 11:55:10 -0800686/*
687 * Copy a filename
688 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800689static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800690{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800691 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700692 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800693
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800694 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800695}
696
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700697/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700698 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700699 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700700static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700701{
702 const char *p;
703 char *os, *q;
704
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400705 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700706 size_t nbs = 0;
707
708 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400709 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700710
711 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400712 switch (*p) {
713 case ' ':
714 case '\t':
715 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
716 n += nbs + 2;
717 nbs = 0;
718 break;
719 case '$':
720 case '#':
721 nbs = 0;
722 n += 2;
723 break;
724 case '\\':
725 nbs++;
726 n++;
727 break;
728 default:
729 nbs = 0;
730 n++;
731 break;
732 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700733 }
734
735 /* Convert N backslashes at the end of filename to 2N backslashes */
736 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400737 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700738
739 os = q = nasm_malloc(n);
740
741 nbs = 0;
742 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400743 switch (*p) {
744 case ' ':
745 case '\t':
746 while (nbs--)
747 *q++ = '\\';
748 *q++ = '\\';
749 *q++ = *p;
750 break;
751 case '$':
752 *q++ = *p;
753 *q++ = *p;
754 nbs = 0;
755 break;
756 case '#':
757 *q++ = '\\';
758 *q++ = *p;
759 nbs = 0;
760 break;
761 case '\\':
762 *q++ = *p;
763 nbs++;
764 break;
765 default:
766 *q++ = *p;
767 nbs = 0;
768 break;
769 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700770 }
771 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400772 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700773
774 *q = '\0';
775
776 return os;
777}
778
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700779/*
780 * Convert a string to a Watcom make-safe form
781 */
782static char *quote_for_wmake(const char *str)
783{
784 const char *p;
785 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700786 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700787
788 size_t n = 1; /* Terminating zero */
789
790 if (!str)
791 return NULL;
792
793 for (p = str; *p; p++) {
794 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700795 case ' ':
796 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700797 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700798 quote = true;
799 n++;
800 break;
801 case '\"':
802 quote = true;
803 n += 2;
804 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700805 case '$':
806 case '#':
807 n += 2;
808 break;
809 default:
810 n++;
811 break;
812 }
813 }
814
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700815 if (quote)
816 n += 2;
817
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700818 os = q = nasm_malloc(n);
819
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700820 if (quote)
821 *q++ = '\"';
822
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700823 for (p = str; *p; p++) {
824 switch (*p) {
825 case '$':
826 case '#':
827 *q++ = '$';
828 *q++ = *p;
829 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700830 case '\"':
831 *q++ = *p;
832 *q++ = *p;
833 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700834 default:
835 *q++ = *p;
836 break;
837 }
838 }
839
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700840 if (quote)
841 *q++ = '\"';
842
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700843 *q = '\0';
844
845 return os;
846}
847
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100848enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800849 OPT_BOGUS,
850 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700851 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800852 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700853 OPT_MANGLE,
854 OPT_INCLUDE,
855 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700856 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700857 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800858 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700859 OPT_NO_LINE,
860 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100861};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700862enum need_arg {
863 ARG_NO,
864 ARG_YES,
865 ARG_MAYBE
866};
867
H. Peter Anvin3366e312018-02-07 14:14:36 -0800868struct textargs {
869 const char *label;
870 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700871 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700872 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800873};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800874static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700875 {"v", OPT_VERSION, ARG_NO, 0},
876 {"version", OPT_VERSION, ARG_NO, 0},
877 {"help", OPT_HELP, ARG_NO, 0},
878 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
879 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
880 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
881 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
882 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
883 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
884 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
885 {"include", OPT_INCLUDE, ARG_YES, 0},
886 {"pragma", OPT_PRAGMA, ARG_YES, 0},
887 {"before", OPT_BEFORE, ARG_YES, 0},
888 {"limit-", OPT_LIMIT, ARG_YES, 0},
889 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
890 {"no-line", OPT_NO_LINE, ARG_NO, 0},
891 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
892 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000893};
894
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400895static void show_version(void)
896{
897 printf("NASM version %s compiled on %s%s\n",
898 nasm_version, nasm_date, nasm_compile_options);
899 exit(0);
900}
901
H. Peter Anvin423e3812007-11-15 17:12:29 -0800902static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700903static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000904{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000905 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800906 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000907
908 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800909 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000910
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700912 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 /* These parameters take values */
914 if (!(param = get_param(p, q, &advance)))
915 return advance;
916 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800917
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 switch (p[1]) {
919 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700920 if (pass == 1)
921 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700923
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400924 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700925 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800926 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700928
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400929 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700930 if (pass == 1) {
931 ofmt = ofmt_find(param, &ofmt_alias);
932 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800933 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700934 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 }
936 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700937
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800939 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700941
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 if (!*param) {
943 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300944 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700945 } else {
946 while (*param) {
947 switch (*param) {
948 case '0': case '1': case '2': case '3': case '4':
949 case '5': case '6': case '7': case '8': case '9':
950 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700951
Chang S. Baea5786342018-08-15 23:22:21 +0300952 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
953 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700954 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300955 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700956 else
Chang S. Baea5786342018-08-15 23:22:21 +0300957 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700958 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700959
H. Peter Anvin55568c12016-10-03 19:46:49 -0700960 case 'v':
961 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400962 param++;
963 opt_verbose_info = true;
964 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700965
H. Peter Anvin55568c12016-10-03 19:46:49 -0700966 case 'x':
967 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300968 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700969 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700970
H. Peter Anvin55568c12016-10-03 19:46:49 -0700971 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700972 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700973 *param);
974 break;
975 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400976 }
Chang S. Baea5786342018-08-15 23:22:21 +0300977 if (optimizing.level > MAX_OPTIMIZE)
978 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400979 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 }
981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 case 'p': /* pre-include */
984 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 if (pass == 2)
986 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400989 case 'd': /* pre-define */
990 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 if (pass == 2)
992 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 case 'u': /* un-define */
996 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700997 if (pass == 2)
998 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001000
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001001 case 'i': /* include search path */
1002 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001003 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001004 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001006
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001007 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001008 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001009 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001011
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001012 case 'L': /* listing options */
1013 if (pass == 2) {
1014 while (*param) {
1015 unsigned int p = *param - '@';
1016 if (p <= 63)
1017 list_options |= (UINT64_C(1) << p);
1018 param++;
1019 }
1020 }
1021 break;
1022
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001023 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001025 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001028 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001029 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001030 using_debug_info = true;
1031 debug_format = param;
1032 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001033 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001034
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001035 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001036 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001037 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1038 errfmt = &errfmt_msvc;
1039 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1040 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001041 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001042 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001047 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 using_debug_info = true;
1049 if (p[2])
1050 debug_format = nasm_skip_spaces(p + 2);
1051 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001055 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001056 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001060 /* legacy option */
1061 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 exit(0);
1063 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001066 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001067 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001068 nasm_ctype_tasm_mode();
1069 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001073 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001075
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001076 case 'e': /* preprocess only */
1077 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001078 if (pass == 1)
1079 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001081
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001082 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001083 if (pass == 1)
1084 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001086
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001087 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001088 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001089 if (pass == 2)
1090 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001091 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001094 if (pass == 1) {
1095 switch (p[2]) {
1096 case 'W':
1097 quote_for_make = quote_for_wmake;
1098 break;
1099 case 'D':
1100 case 'F':
1101 case 'T':
1102 case 'Q':
1103 advance = true;
1104 break;
1105 default:
1106 break;
1107 }
1108 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001109 switch (p[2]) {
1110 case 0:
1111 operating_mode = OP_DEPEND;
1112 break;
1113 case 'G':
1114 operating_mode = OP_DEPEND;
1115 depend_missing_ok = true;
1116 break;
1117 case 'P':
1118 depend_emit_phony = true;
1119 break;
1120 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001121 operating_mode |= OP_DEPEND;
1122 if (q && (q[0] != '-' || q[1] == '\0')) {
1123 depend_file = q;
1124 advance = true;
1125 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001126 break;
1127 case 'F':
1128 depend_file = q;
1129 advance = true;
1130 break;
1131 case 'T':
1132 depend_target = q;
1133 advance = true;
1134 break;
1135 case 'Q':
1136 depend_target = quote_for_make(q);
1137 advance = true;
1138 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001139 case 'W':
1140 /* handled in pass 1 */
1141 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001142 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001143 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001144 break;
1145 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001146 }
1147 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001148 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001149 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001150 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case '-':
1154 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001155 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001156 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001157 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001158 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001160 p += 2;
1161
1162 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001163 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 break;
1165 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001166
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001167 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001168 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001169 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 olen = strlen(tx->label);
1171
1172 if (olen > plen)
1173 continue;
1174
1175 if (nasm_memicmp(p, tx->label, olen))
1176 continue;
1177
1178 if (tx->label[olen-1] == '-')
1179 break; /* Incomplete option */
1180
1181 if (!p[olen] || p[olen] == '=')
1182 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001184
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001185 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001186 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001187 }
1188
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001189 opt = tx->opt;
1190
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001191 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001192 if (param)
1193 *param++ = '\0';
1194
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001195 switch (tx->need_arg) {
1196 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001197 if (!param) {
1198 param = q;
1199 advance = true;
1200 }
1201
1202 /* Note: a null string is a valid parameter */
1203 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001204 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001205 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001207 break;
1208
1209 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001210 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001211 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001212 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001213 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001214 break;
1215
1216 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1217 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001218 }
1219
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001220 switch (opt) {
1221 case OPT_BOGUS:
1222 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001223 case OPT_VERSION:
1224 show_version();
1225 break;
1226 case OPT_ABORT_ON_PANIC:
1227 abort_on_panic = true;
1228 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001229 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001230 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001231 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001232 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001233 case OPT_INCLUDE:
1234 if (pass == 2)
1235 preproc->pre_include(q);
1236 break;
1237 case OPT_PRAGMA:
1238 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001239 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001240 break;
1241 case OPT_BEFORE:
1242 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001243 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001244 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001245 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001246 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001247 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001248 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001249 case OPT_KEEP_ALL:
1250 keep_all = true;
1251 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001252 case OPT_NO_LINE:
1253 pp_noline = true;
1254 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001255 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001256 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001257 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001258 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001259 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001260 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001261 default:
1262 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001264
1265 if (eqsave)
1266 *eqsave = '='; /* Restore = argument separator */
1267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 break;
1269 }
1270
1271 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001272 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 break;
1274 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001275 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001276 /* In theory we could allow multiple input files... */
1277 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001278 }
1279
1280 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001281}
1282
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283#define ARG_BUF_DELTA 128
1284
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288 int bufsize, prevargsize;
1289
1290 bufsize = prevargsize = ARG_BUF_DELTA;
1291 buffer = nasm_malloc(ARG_BUF_DELTA);
1292 prevarg = nasm_malloc(ARG_BUF_DELTA);
1293 prevarg[0] = '\0';
1294
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 p = buffer;
1297 while (1) { /* Loop to handle long lines */
1298 q = fgets(p, bufsize - (p - buffer), rfile);
1299 if (!q)
1300 break;
1301 p += strlen(p);
1302 if (p > buffer && p[-1] == '\n')
1303 break;
1304 if (p - buffer > bufsize - 10) {
1305 int offset;
1306 offset = p - buffer;
1307 bufsize += ARG_BUF_DELTA;
1308 buffer = nasm_realloc(buffer, bufsize);
1309 p = buffer + offset;
1310 }
1311 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (!q && p == buffer) {
1314 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001315 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 nasm_free(buffer);
1317 nasm_free(prevarg);
1318 return;
1319 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001320
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 /*
1322 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1323 * them are present at the end of the line.
1324 */
1325 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001326
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001327 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001329
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001330 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331
H. Peter Anvin55568c12016-10-03 19:46:49 -07001332 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001334
Charles Crayne192d5b52007-10-18 19:02:42 -07001335 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 prevargsize += ARG_BUF_DELTA;
1337 prevarg = nasm_realloc(prevarg, prevargsize);
1338 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001339 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001340 }
1341}
1342
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001343/* Function to process args from a string of args, rather than the
1344 * argv array. Used by the environment variable and response file
1345 * processing.
1346 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001347static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001349 char *p, *q, *arg, *prevarg;
1350 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001351
1352 p = args;
1353 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001355 arg = NULL;
1356 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 q = p;
1358 while (*p && *p != separator)
1359 p++;
1360 while (*p == separator)
1361 *p++ = '\0';
1362 prevarg = arg;
1363 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001364 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001366 }
1367 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001368 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001369}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001370
H. Peter Anvin55568c12016-10-03 19:46:49 -07001371static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001372{
1373 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001374 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001375 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001376 perror(file);
1377 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001378 }
1379 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001380 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001381 }
1382 fclose(f);
1383}
1384
H. Peter Anvin55568c12016-10-03 19:46:49 -07001385static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386{
1387 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001388 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001389
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001390 /*
1391 * Initialize all the warnings to their default state, including
1392 * warning index 0 used for "always on".
1393 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001394 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001395
1396 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001397 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001398 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001399 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001400 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001402 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001404 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001405
1406 /*
1407 * Now process the actual command line.
1408 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001410 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 argv++;
1412 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001413 /*
1414 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 * arguments like the environment variable. This allows us
1416 * to have multiple arguments on a single line, which is
1417 * different to the -@resp file processing below for regular
1418 * NASM.
1419 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001420 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 argc--;
1422 argv++;
1423 }
1424 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001425 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001426 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001427 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001428 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001429 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001431 } else {
1432 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1433 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 }
1435 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001436 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001437 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001438 }
1439
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001440 /*
1441 * Look for basic command line typos. This definitely doesn't
1442 * catch all errors, but it might help cases of fumbled fingers.
1443 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001444 if (pass != 2)
1445 return;
1446
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001447 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001448 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001449 else if ((errname && !strcmp(inname, errname)) ||
1450 (outname && !strcmp(inname, outname)) ||
1451 (listname && !strcmp(inname, listname)) ||
1452 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001453 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001454
1455 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001456 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001457 if (!error_file) {
1458 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001459 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001460 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001461 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001462}
1463
H. Peter Anvin29651542018-12-18 19:14:40 -08001464static void forward_refs(insn *instruction)
1465{
1466 int i;
1467 struct forwrefinfo *fwinf;
1468
1469 instruction->forw_ref = false;
1470
1471 if (!optimizing.level)
1472 return; /* For -O0 don't bother */
1473
1474 if (!forwref)
1475 return;
1476
1477 if (forwref->lineno != globallineno)
1478 return;
1479
1480 instruction->forw_ref = true;
1481 do {
1482 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1483 forwref = saa_rstruct(forwrefs);
1484 } while (forwref && forwref->lineno == globallineno);
1485
1486 if (!pass_first())
1487 return;
1488
1489 for (i = 0; i < instruction->operands; i++) {
1490 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1491 fwinf = saa_wstruct(forwrefs);
1492 fwinf->lineno = globallineno;
1493 fwinf->operand = i;
1494 }
1495 }
1496}
1497
1498static void process_insn(insn *instruction)
1499{
1500 int32_t n;
1501 int64_t l;
1502
1503 if (!instruction->times)
1504 return; /* Nothing to do... */
1505
1506 nasm_assert(instruction->times > 0);
1507
1508 /*
1509 * NOTE: insn_size() can change instruction->times
1510 * (usually to 1) when called.
1511 */
1512 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001513 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001514 for (n = 1; n <= instruction->times; n++) {
1515 l = insn_size(location.segment, location.offset,
1516 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001517 /* l == -1 -> invalid instruction */
1518 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001519 increment_offset(l);
1520 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001521 if (list_option('p')) {
1522 struct out_data dummy;
1523 memset(&dummy, 0, sizeof dummy);
1524 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1525 dummy.offset = start;
1526 dummy.size = location.offset - start;
1527 lfmt->output(&dummy);
1528 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001529 } else {
1530 l = assemble(location.segment, location.offset,
1531 globalbits, instruction);
1532 /* We can't get an invalid instruction here */
1533 increment_offset(l);
1534
1535 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001536 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001537 for (n = 2; n <= instruction->times; n++) {
1538 l = assemble(location.segment, location.offset,
1539 globalbits, instruction);
1540 increment_offset(l);
1541 }
1542 lfmt->downlevel(LIST_TIMES);
1543 }
1544 }
1545}
1546
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001547static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001548{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001549 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001551 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001552 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001553
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001554 switch (cmd_sb) {
1555 case 16:
1556 break;
1557 case 32:
1558 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001559 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001560 break;
1561 case 64:
1562 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001563 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001564 break;
1565 default:
1566 panic();
1567 break;
1568 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001570 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001571
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001572 if (listname && !keep_all) {
1573 /* Remove the list file in case we die before the output pass */
1574 remove(listname);
1575 }
1576
1577 while (!terminate_after_phase && !pass_final()) {
1578 _passn++;
1579 if (pass_type() != PASS_OPT || !global_offset_changed)
1580 _pass_type++;
1581 global_offset_changed = 0;
1582
1583 /*
1584 * Create a warning buffer list unless we are in
1585 * pass 2 (everything will be emitted immediately in pass 2.)
1586 */
1587 if (warn_list) {
1588 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001589 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001590 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001591
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001592 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001593 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001594
H. Peter Anvincac0b192017-03-28 16:12:30 -07001595 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001597 if (listname) {
1598 if (pass_final() || list_on_every_pass()) {
1599 active_list_options = list_options;
1600 lfmt->init(listname);
1601 } else if (active_list_options) {
1602 /*
1603 * Looks like we used the list engine on a previous pass,
1604 * but now it is turned off, presumably via %pragma -p
1605 */
1606 lfmt->cleanup();
1607 if (!keep_all)
1608 remove(listname);
1609 active_list_options = 0;
1610 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001611 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001612
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001613 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001614 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 saa_rewind(forwrefs);
1616 forwref = saa_rstruct(forwrefs);
1617 raa_free(offsets);
1618 offsets = raa_init();
1619 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001620 location.segment = NO_SEG;
1621 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001622 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001623 location.known = true;
1624 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001625 switch_segment(ofmt->section(NULL, &globalbits));
1626 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001629
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001631 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001632 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001633 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001634
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001635 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001636 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001637 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001638 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001639 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001640 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001641
H. Peter Anvinc7131682017-03-07 17:45:01 -08001642 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001643 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001644 forward_refs(&output_ins);
1645 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001646 cleanup_insn(&output_ins);
1647
1648 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001651
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001652 preproc->cleanup_pass();
1653
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001654 if (global_offset_changed) {
1655 switch (pass_type()) {
1656 case PASS_OPT:
1657 /*
1658 * This is the only pass type that can be executed more
1659 * than once, and therefore has the ability to stall.
1660 */
1661 if (global_offset_changed < prev_offset_changed) {
1662 prev_offset_changed = global_offset_changed;
1663 stall_count = 0;
1664 } else {
1665 stall_count++;
1666 }
1667
1668 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1669 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1670 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001671 nasm_nonfatalf(ERR_UNDEAD,
1672 "unable to find valid values for all labels "
1673 "after %"PRId64" passes; "
1674 "stalled for %"PRId64", giving up.",
1675 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001676 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001677 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001678 }
1679 break;
1680
1681 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001682 /*!
1683 *!phase [off] phase error during stabilization
1684 *! warns about symbols having changed values during
1685 *! the second-to-last assembly pass. This is not
1686 *! inherently fatal, but may be a source of bugs.
1687 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001688 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1689 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001690 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001691 break;
1692
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001693 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001694 nasm_nonfatalf(ERR_UNDEAD,
1695 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001696 break;
1697
1698 default:
1699 /* This is normal, we'll keep going... */
1700 break;
1701 }
1702 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001703
1704 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001707 if (opt_verbose_info && pass_final()) {
1708 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001709 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001710 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001712 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001713 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001714}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001715
Ed Berosetfa771012002-06-09 20:56:40 +00001716/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001717 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001718 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001719static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001720{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001721 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001722
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001723 if ((severity & ERR_MASK) >= ERR_FATAL)
1724 return 0; /* Fatal errors are never suppressible */
1725
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001726 /* Warnings MUST HAVE a warning category specifier! */
1727 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001728
1729 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001730 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001731
1732 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001733}
1734
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001735static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001736{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001737 errflags type = severity & ERR_MASK;
1738
H. Peter Anvin8f622462017-04-02 19:02:29 -07001739 /*
1740 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001741 * We can never skip fatal errors as by definition they cannot be
1742 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001743 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001744 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001745 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001746
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001747 /*
1748 * ERR_LISTMSG messages are always skipped; the list file
1749 * receives them anyway as this function is not consulted
1750 * for sending to the list file.
1751 */
1752 if (type == ERR_LISTMSG)
1753 return true;
1754
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001755 /* This message not applicable unless pass_final */
1756 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001757}
1758
Ed Berosetfa771012002-06-09 20:56:40 +00001759/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001760 * check for suppressed message (usually warnings or notes)
1761 *
1762 * @param severity the severity of the warning or error
1763 * @return true if we should abort error/warning printing
1764 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001765static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001766{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001767 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001768 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001769 return false;
1770
1771 /* This error/warning is pointless if we are dead anyway */
1772 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1773 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001774
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001775 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1776}
1777
1778/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001779 * Return the true error type (the ERR_MASK part) of the given
1780 * severity, accounting for warnings that may need to be promoted to
1781 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001782 *
1783 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001784 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001785 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001786static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001787{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001788 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1789 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001790
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001791 type = severity & ERR_MASK;
1792
1793 /* Promote warning to error? */
1794 if (type == ERR_WARNING) {
1795 uint8_t state = warning_state[warn_index(severity)];
1796 if ((state & warn_is_err) == warn_is_err)
1797 type = ERR_NONFATAL;
1798 }
1799
1800 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001801}
1802
1803/**
Ed Berosetfa771012002-06-09 20:56:40 +00001804 * common error reporting
1805 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001806 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001807 * specific error message to error_file and may or may not return. It
1808 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001809 *
1810 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001811 * @param fmt the printf style format string
1812 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001813static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001814{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001815 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001816 char warnsuf[64];
1817 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001818 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001819 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001820 const char *currentfile = NULL;
1821 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001822 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001823 ";;; ", "debug: ", "info: ", "warning: ",
1824 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001825 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001826
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001827 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001828 return;
1829
1830 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001831 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001832 if (!currentfile) {
1833 currentfile = currentfile ? currentfile :
1834 inname && inname[0] ? inname :
1835 outname && outname[0] ? outname :
1836 NULL;
1837 lineno = 0;
1838 }
1839 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001840
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001841 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001842 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001843 else
1844 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001845
1846 vsnprintf(msg, sizeof msg, fmt, args);
1847 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001848 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1849 /*
1850 * It's a warning without ERR_HERE defined, and we are not already
1851 * unwinding the macros that led us here.
1852 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001853 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001854 (true_type >= ERR_NONFATAL) ? "error=" : "",
1855 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001856 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001857
H. Peter Anvinddb29062018-12-11 00:06:29 -08001858 *linestr = 0;
1859 if (lineno) {
1860 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1861 errfmt->beforeline, lineno, errfmt->afterline);
1862 }
1863
H. Peter Anvin77016c82018-12-10 22:29:49 -08001864 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001865 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001866 const char *here = "";
1867
1868 if (severity & ERR_HERE)
1869 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001870
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001871 if (warn_list && true_type < ERR_NONFATAL &&
1872 !(pass_first() && (severity & ERR_PASS1)))
1873 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001874 /*
1875 * Buffer up warnings until we either get an error
1876 * or we are on the code-generation pass.
1877 */
1878 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1879 file, linestr, errfmt->beforemsg,
1880 pfx, msg, here, warnsuf);
1881 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001882 /*
1883 * If we have buffered warnings, and this is a non-warning,
1884 * output them now.
1885 */
1886 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001887 strlist_write(warn_list, "\n", error_file);
1888 strlist_free(&warn_list);
1889 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001890
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001891 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1892 file, linestr, errfmt->beforemsg,
1893 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001894
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001895 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001896 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001897
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001898 /* Are we recursing from error_list_macros? */
1899 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001900 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001901
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001902 /*
1903 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001904 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001905 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001906 if (severity & ERR_HERE) {
1907 if (lineno)
1908 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1909 pfx, msg, currentfile, lineno, warnsuf);
1910 else if (currentfile)
1911 lfmt->error(severity, "%s%s in file %s%s",
1912 pfx, msg, currentfile, warnsuf);
1913 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001914 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001915 pfx, msg, warnsuf);
1916 } else {
1917 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1918 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001919
H. Peter Anvin8f622462017-04-02 19:02:29 -07001920 if (skip_this_pass(severity))
1921 return;
1922
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001923 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001924 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001925
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001926 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1927 if (!(severity & ERR_HERE))
1928 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001929
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001930 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001931 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001933 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001934 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001935 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001936 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001938 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 break;
1940 case ERR_FATAL:
1941 if (ofile) {
1942 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001943 if (!keep_all)
1944 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001945 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 }
1947 if (want_usage)
1948 usage();
1949 exit(1); /* instantly die */
1950 break; /* placate silly compilers */
1951 case ERR_PANIC:
1952 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001953
1954 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001955 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001956
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001957 if (ofile) {
1958 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001959 if (!keep_all)
1960 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001961 ofile = NULL;
1962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 exit(3);
1964 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001965 default:
1966 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001967 }
1968}
1969
H. Peter Anvin734b1882002-04-30 21:01:08 +00001970static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001971{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001972 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001973}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001974
H. Peter Anvin322bee02019-08-10 01:38:06 -07001975static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001976{
1977 int i;
1978
H. Peter Anvin322bee02019-08-10 01:38:06 -07001979 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001980 "Usage: %s [-@ response_file] [options...] [--] filename\n"
1981 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07001982 _progname, _progname);
1983 fputs(
1984 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001985 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001986 "\n"
1987 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001988 " -v (or --v) print the NASM version number and exit\n"
1989 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001990 "\n"
1991 " -o outfile write output to outfile\n"
1992 " --keep-all output files will not be removed even if an error happens\n"
1993 "\n"
1994 " -Xformat specifiy error reporting format (gnu or vc)\n"
1995 " -s redirect error messages to stdout\n"
1996 " -Zfile redirect error messages to file\n"
1997 "\n"
1998 " -M generate Makefile dependencies on stdout\n"
1999 " -MG d:o, missing files assumed generated\n"
2000 " -MF file set Makefile dependency file\n"
2001 " -MD file assemble and generate dependencies\n"
2002 " -MT file dependency target name\n"
2003 " -MQ file dependency target name (quoted)\n"
2004 " -MP emit phony targets\n"
2005 "\n"
2006 " -f format select output file format\n"
2007 , out);
2008 ofmt_list(ofmt, out);
2009 fputs(
2010 "\n"
2011 " -g generate debugging information\n"
2012 " -F format select a debugging format (output format dependent)\n"
2013 " -gformat same as -g -F format\n"
2014 , out);
2015 dfmt_list(out);
2016 fputs(
2017 "\n"
2018 " -l listfile write listing to a list file\n"
2019 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002020 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002021 " -Ld show byte and repeat counts in decimal, not hex\n"
2022 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002023 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002024 " -Lm show all single-line macro definitions\n"
2025 " -Lp output a list file every pass, in case of errors\n"
2026 "\n"
2027 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2028 " -O0 no optimization\n"
2029 " -O1 minimal optimization\n"
2030 " -Ox multipass optimization (default)\n"
2031 " -Ov display the number of passes executed at the end\n"
2032 " -t assemble in limited SciTech TASM compatible mode\n"
2033 "\n"
2034 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2035 " -a don't preprocess (assemble only)\n"
2036 " -Ipath add a pathname to the include file path\n"
2037 " -Pfile pre-include a file (also --include)\n"
2038 " -Dmacro[=str] pre-define a macro\n"
2039 " -Umacro undefine a macro\n"
2040 " --pragma str pre-executes a specific %%pragma\n"
2041 " --before str add line (usually a preprocessor statement) before the input\n"
2042 " --no-line ignore %line directives in input\n"
2043 "\n"
2044 " --prefix str prepend the given string to the names of all extern,\n"
2045 " common and global symbols (also --gprefix)\n"
2046 " --suffix str append the given string to the names of all extern,\n"
2047 " common and global symbols (also --gprefix)\n"
2048 " --lprefix str prepend the given string to local symbols\n"
2049 " --lpostfix str append the given string to local symbols\n"
2050 "\n"
2051 " -w+x enable warning x (also -Wx)\n"
2052 " -w-x disable warning x (also -Wno-x)\n"
2053 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2054 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2055 , out);
2056
2057 fprintf(out, " %-20s %s\n",
2058 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2059
2060 for (i = 1; i < WARN_IDX_ALL; i++) {
2061 const char *me = warning_name[i];
2062 const char *prev = warning_name[i-1];
2063 const char *next = warning_name[i+1];
2064
2065 if (prev) {
2066 int prev_len = strlen(prev);
2067 const char *dash = me;
2068
2069 while ((dash = strchr(dash+1, '-'))) {
2070 int prefix_len = dash - me; /* Not including final dash */
2071 if (strncmp(next, me, prefix_len+1)) {
2072 /* Only one or last option with this prefix */
2073 break;
2074 }
2075 if (prefix_len >= prev_len ||
2076 strncmp(prev, me, prefix_len) ||
2077 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2078 /* This prefix is different from the previous option */
2079 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2080 prefix_len, me, prefix_len+1, me);
2081 }
2082 }
2083 }
2084
2085 fprintf(out, " %-20s %s%s\n",
2086 warning_name[i], warning_help[i],
2087 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2088 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2089 }
2090
2091 fputs(
2092 "\n"
2093 " --limit-X val set execution limit X\n"
2094 , out);
2095
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002096
2097 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002098 fprintf(out, " %-20s %s [",
2099 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002100 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002101 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002102 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002103 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002104 }
2105 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002106}