blob: c84d6750c994db48fe51f5bc9fd48fc6a9e6cb47 [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 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700193 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
194 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700195 { "rep", "%rep count", 1000000 },
196 { "eval", "expression evaluation descent", 1000000},
197 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700198};
199
H. Peter Anvin322bee02019-08-10 01:38:06 -0700200static void set_default_limits(void)
201{
202 int i;
203 for (i = 0; i <= LIMIT_MAX; i++)
204 nasm_limit[i] = limit_info[i].default_val;
205}
206
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207enum directive_result
208nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700209{
210 int i;
211 int64_t val;
212 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700213 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214
215 for (i = 0; i <= LIMIT_MAX; i++) {
216 if (!nasm_stricmp(limit, limit_info[i].name))
217 break;
218 }
219 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800220 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800221 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700222 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700223 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700224 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700225 return DIRR_ERROR;
226 }
227
228 if (!nasm_stricmp(valstr, "unlimited")) {
229 val = LIMIT_MAX_VAL;
230 } else {
231 val = readnum(valstr, &rn_error);
232 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800233 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800234 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700235 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700236 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700237 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700238 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700239 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700240 if (val > LIMIT_MAX_VAL)
241 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700242 }
243
244 nasm_limit[i] = val;
245 return DIRR_OK;
246}
247
H. Peter Anvin892c4812018-05-30 14:43:46 -0700248int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400249{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700250 location.segment = segment;
251 if (segment == NO_SEG) {
252 location.offset = absolute.offset;
253 in_absolute = true;
254 } else {
255 location.offset = raa_read(offsets, segment);
256 in_absolute = false;
257 }
258 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400259}
260
261static void set_curr_offs(int64_t l_off)
262{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800263 if (in_absolute)
264 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400265 else
266 offsets = raa_write(offsets, location.segment, l_off);
267}
268
H. Peter Anvin892c4812018-05-30 14:43:46 -0700269static void increment_offset(int64_t delta)
270{
271 if (unlikely(delta == 0))
272 return;
273
274 location.offset += delta;
275 set_curr_offs(location.offset);
276}
277
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000278static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000280 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800282 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000283 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000285}
286
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800287/*
288 * Define system-defined macros that are not part of
289 * macros/standard.mac.
290 */
291static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800292{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800294 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800295
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700296 if (oct->have_local) {
297 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400298 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700299 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400300 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700301 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400302 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700303 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400304 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800305 }
306
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700307 if (oct->have_gm) {
308 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400309 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700310 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400311 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700312 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700314 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400315 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800316 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700317
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700318 if (oct->have_posix) {
319 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400320 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800321 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800322
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400323 /*
324 * In case if output format is defined by alias
325 * we have to put shortname of the alias itself here
326 * otherwise ABI backward compatibility gets broken.
327 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400328 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400329 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400330 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800331
332 /*
333 * Output-format specific macros.
334 */
335 if (ofmt->stdmac)
336 preproc->extra_stdmac(ofmt->stdmac);
337
338 /*
339 * Debug format, if any
340 */
341 if (dfmt != &null_debug_form) {
342 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
343 preproc->pre_define(temp);
344 }
345}
346
347/*
348 * Initialize the preprocessor, set up the include path, and define
349 * the system-included macros. This is called between passes 1 and 2
350 * of parsing the command options; ofmt and dfmt are defined at this
351 * point.
352 *
353 * Command-line specified preprocessor directives (-p, -d, -u,
354 * --pragma, --before) are processed after this function.
355 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300356static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800357{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800358 preproc->init();
359 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300360 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800361}
362
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300363static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700364{
365 FILE *deps;
366 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700367 bool wmake = (quote_for_make == quote_for_wmake);
368 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800369 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700370
371 if (!list)
372 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700373
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700374 wrapstr = wmake ? " &\n " : " \\\n ";
375 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700376
377 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700378 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400379 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800380 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400381 return;
382 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700383 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400384 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700386
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700387 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800388 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700389 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400390 len = strlen(file);
391 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700392 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400393 linepos = 1;
394 }
395 fprintf(deps, " %s", file);
396 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700397 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700398 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700399 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700400
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800401 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700402 if (depend_emit_phony) {
403 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700404 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700405 nasm_free(file);
406 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700407 }
408
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800409 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700410
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700411 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400412 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700413}
414
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700415/* Convert a struct tm to a POSIX-style time constant */
416static int64_t make_posix_time(const struct tm *tm)
417{
418 int64_t t;
419 int64_t y = tm->tm_year;
420
421 /* See IEEE 1003.1:2004, section 4.14 */
422
423 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
424 t += tm->tm_yday;
425 t *= 24;
426 t += tm->tm_hour;
427 t *= 60;
428 t += tm->tm_min;
429 t *= 60;
430 t += tm->tm_sec;
431
432 return t;
433}
434
435static void timestamp(void)
436{
437 struct compile_time * const oct = &official_compile_time;
438 const struct tm *tp, *best_gm;
439
440 time(&oct->t);
441
442 best_gm = NULL;
443
444 tp = localtime(&oct->t);
445 if (tp) {
446 oct->local = *tp;
447 best_gm = &oct->local;
448 oct->have_local = true;
449 }
450
451 tp = gmtime(&oct->t);
452 if (tp) {
453 oct->gm = *tp;
454 best_gm = &oct->gm;
455 oct->have_gm = true;
456 if (!oct->have_local)
457 oct->local = oct->gm;
458 } else {
459 oct->gm = oct->local;
460 }
461
462 if (best_gm) {
463 oct->posix = make_posix_time(best_gm);
464 oct->have_posix = true;
465 }
466}
467
H. Peter Anvin038d8612007-04-12 16:54:50 +0000468int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000469{
H. Peter Anvin322bee02019-08-10 01:38:06 -0700470 _progname = argv[0];
471 if (!_progname || !_progname[0])
472 _progname = "nasm";
473
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700474 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800475
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800476 error_file = stderr;
477
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800478 iflag_set_default_cpu(&cpu);
479 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400480
H. Peter Anvin322bee02019-08-10 01:38:06 -0700481 set_default_limits();
482
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800483 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300484
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800485 _pass_type = PASS_INIT;
486 _passn = 0;
487
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700488 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800489 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490
H. Peter Anvin13506202018-11-28 14:55:58 -0800491 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700492 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700493
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700494 /*
495 * We must call init_labels() before the command line parsing,
496 * because we may be setting prefixes/suffixes from the command
497 * line.
498 */
499 init_labels();
500
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000501 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000502 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000503
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400505 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000506
H. Peter Anvin55568c12016-10-03 19:46:49 -0700507 parse_cmdline(argc, argv, 1);
508 if (terminate_after_phase) {
509 if (want_usage)
510 usage();
511 return 1;
512 }
513
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800514 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800515 if (!using_debug_info) {
516 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800517 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800518 } else if (!debug_format) {
519 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300520 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800521 } else {
522 dfmt = dfmt_find(ofmt, debug_format);
523 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800524 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800525 debug_format, ofmt->shortname);
526 }
527 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000528
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300529 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800530
531 parse_cmdline(argc, argv, 2);
532 if (terminate_after_phase) {
533 if (want_usage)
534 usage();
535 return 1;
536 }
537
538 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800539 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000540
H. Peter Anvin34754622018-11-28 12:36:53 -0800541 /* Dependency filename if we are also doing other things */
542 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
543 if (outname)
544 depend_file = nasm_strcat(outname, ".d");
545 else
546 depend_file = filename_set_extension(inname, ".d");
547 }
548
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300549 /*
550 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800551 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300552 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300553 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800554 if (!outname && !(operating_mode & OP_PREPROCESS)) {
555 outname = filename_set_extension(inname, ofmt->extension);
556 if (!strcmp(outname, inname)) {
557 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800558 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 -0800559 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300560 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800561
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800562 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700563
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700564 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400565 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700566
H. Peter Anvin34754622018-11-28 12:36:53 -0800567 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000568 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700569
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400570 if (depend_missing_ok)
571 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800573 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 while ((line = preproc->getline()))
576 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800577 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800578 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400579 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000580 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700581 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000582 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000584
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800585 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700586 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800588 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 } else
590 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000591
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700592 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800594 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
595 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800596
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 while ((line = preproc->getline())) {
598 /*
599 * We generate %line directives if needed for later programs
600 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000601 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 int altline = src_get(&linnum, &file_name);
603 if (altline) {
604 if (altline == 1 && lineinc == 1)
605 nasm_fputs("", ofile);
606 else {
607 lineinc = (altline != -1 || lineinc != 1);
608 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000609 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 file_name);
611 }
612 prior_linnum = linnum;
613 }
614 nasm_fputs(line, ofile);
615 nasm_free(line);
616 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800617 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800618 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 if (ofile)
620 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700621 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400623 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400624 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400626 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700627 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400628 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800629 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400631 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400632 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700634 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200635
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400636 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800637 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400638 cleanup_labels();
639 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800640 if (ferror(ofile))
641 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400642 }
643
644 if (ofile) {
645 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700646 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400647 remove(outname);
648 ofile = NULL;
649 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000650 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000651
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800652 preproc->cleanup_session();
653
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700654 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400655 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700656
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000657 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 raa_free(offsets);
661 saa_free(forwrefs);
662 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000663 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700664 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800665 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000666
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700667 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000668}
669
H. Peter Anvineba20a72002-04-30 20:53:55 +0000670/*
671 * Get a parameter for a command line option.
672 * First arg must be in the form of e.g. -f...
673 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800674static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000675{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800676 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400677 if (p[2]) /* the parameter's in the option */
678 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800680 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000682 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800683 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684 return NULL;
685}
686
H. Peter Anvindc242712007-11-18 11:55:10 -0800687/*
688 * Copy a filename
689 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800690static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800691{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800692 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700693 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800694
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800695 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800696}
697
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700698/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700699 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700700 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700701static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700702{
703 const char *p;
704 char *os, *q;
705
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400706 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700707 size_t nbs = 0;
708
709 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400710 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700711
712 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400713 switch (*p) {
714 case ' ':
715 case '\t':
716 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
717 n += nbs + 2;
718 nbs = 0;
719 break;
720 case '$':
721 case '#':
722 nbs = 0;
723 n += 2;
724 break;
725 case '\\':
726 nbs++;
727 n++;
728 break;
729 default:
730 nbs = 0;
731 n++;
732 break;
733 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700734 }
735
736 /* Convert N backslashes at the end of filename to 2N backslashes */
737 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400738 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700739
740 os = q = nasm_malloc(n);
741
742 nbs = 0;
743 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400744 switch (*p) {
745 case ' ':
746 case '\t':
747 while (nbs--)
748 *q++ = '\\';
749 *q++ = '\\';
750 *q++ = *p;
751 break;
752 case '$':
753 *q++ = *p;
754 *q++ = *p;
755 nbs = 0;
756 break;
757 case '#':
758 *q++ = '\\';
759 *q++ = *p;
760 nbs = 0;
761 break;
762 case '\\':
763 *q++ = *p;
764 nbs++;
765 break;
766 default:
767 *q++ = *p;
768 nbs = 0;
769 break;
770 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700771 }
772 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400773 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700774
775 *q = '\0';
776
777 return os;
778}
779
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700780/*
781 * Convert a string to a Watcom make-safe form
782 */
783static char *quote_for_wmake(const char *str)
784{
785 const char *p;
786 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700787 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700788
789 size_t n = 1; /* Terminating zero */
790
791 if (!str)
792 return NULL;
793
794 for (p = str; *p; p++) {
795 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700796 case ' ':
797 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700798 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700799 quote = true;
800 n++;
801 break;
802 case '\"':
803 quote = true;
804 n += 2;
805 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700806 case '$':
807 case '#':
808 n += 2;
809 break;
810 default:
811 n++;
812 break;
813 }
814 }
815
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700816 if (quote)
817 n += 2;
818
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700819 os = q = nasm_malloc(n);
820
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700821 if (quote)
822 *q++ = '\"';
823
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700824 for (p = str; *p; p++) {
825 switch (*p) {
826 case '$':
827 case '#':
828 *q++ = '$';
829 *q++ = *p;
830 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700831 case '\"':
832 *q++ = *p;
833 *q++ = *p;
834 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700835 default:
836 *q++ = *p;
837 break;
838 }
839 }
840
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700841 if (quote)
842 *q++ = '\"';
843
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700844 *q = '\0';
845
846 return os;
847}
848
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100849enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800850 OPT_BOGUS,
851 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700852 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800853 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700854 OPT_MANGLE,
855 OPT_INCLUDE,
856 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700857 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700858 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800859 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700860 OPT_NO_LINE,
861 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100862};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700863enum need_arg {
864 ARG_NO,
865 ARG_YES,
866 ARG_MAYBE
867};
868
H. Peter Anvin3366e312018-02-07 14:14:36 -0800869struct textargs {
870 const char *label;
871 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700872 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700873 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800874};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800875static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700876 {"v", OPT_VERSION, ARG_NO, 0},
877 {"version", OPT_VERSION, ARG_NO, 0},
878 {"help", OPT_HELP, ARG_NO, 0},
879 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
880 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
881 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
882 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
883 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
884 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
885 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
886 {"include", OPT_INCLUDE, ARG_YES, 0},
887 {"pragma", OPT_PRAGMA, ARG_YES, 0},
888 {"before", OPT_BEFORE, ARG_YES, 0},
889 {"limit-", OPT_LIMIT, ARG_YES, 0},
890 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
891 {"no-line", OPT_NO_LINE, ARG_NO, 0},
892 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
893 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000894};
895
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400896static void show_version(void)
897{
898 printf("NASM version %s compiled on %s%s\n",
899 nasm_version, nasm_date, nasm_compile_options);
900 exit(0);
901}
902
H. Peter Anvin423e3812007-11-15 17:12:29 -0800903static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700904static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000905{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000906 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800907 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000908
909 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800910 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000911
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700913 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400914 /* These parameters take values */
915 if (!(param = get_param(p, q, &advance)))
916 return advance;
917 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800918
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 switch (p[1]) {
920 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700921 if (pass == 1)
922 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700924
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800927 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700929
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 if (pass == 1) {
932 ofmt = ofmt_find(param, &ofmt_alias);
933 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800934 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700935 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400936 }
937 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700938
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800940 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700942
H. Peter Anvin55568c12016-10-03 19:46:49 -0700943 if (!*param) {
944 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300945 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 } else {
947 while (*param) {
948 switch (*param) {
949 case '0': case '1': case '2': case '3': case '4':
950 case '5': case '6': case '7': case '8': case '9':
951 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700952
Chang S. Baea5786342018-08-15 23:22:21 +0300953 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
954 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700955 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300956 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 else
Chang S. Baea5786342018-08-15 23:22:21 +0300958 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700959 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700960
H. Peter Anvin55568c12016-10-03 19:46:49 -0700961 case 'v':
962 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400963 param++;
964 opt_verbose_info = true;
965 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700966
H. Peter Anvin55568c12016-10-03 19:46:49 -0700967 case 'x':
968 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300969 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700970 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700971
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700973 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 *param);
975 break;
976 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400977 }
Chang S. Baea5786342018-08-15 23:22:21 +0300978 if (optimizing.level > MAX_OPTIMIZE)
979 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400981 }
982 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400984 case 'p': /* pre-include */
985 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700986 if (pass == 2)
987 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400988 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800989
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400990 case 'd': /* pre-define */
991 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700992 if (pass == 2)
993 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800995
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 case 'u': /* un-define */
997 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700998 if (pass == 2)
999 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001000 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001001
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001002 case 'i': /* include search path */
1003 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001004 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001005 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001007
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001010 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001013 case 'L': /* listing options */
1014 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001015 while (*param)
1016 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001017 }
1018 break;
1019
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001020 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001021 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001022 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001023 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001024
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001025 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001026 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001027 using_debug_info = true;
1028 debug_format = param;
1029 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001030 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001031
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001032 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001033 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001034 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1035 errfmt = &errfmt_msvc;
1036 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1037 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001038 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001039 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001044 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 using_debug_info = true;
1046 if (p[2])
1047 debug_format = nasm_skip_spaces(p + 2);
1048 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001052 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001053 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001057 /* legacy option */
1058 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 exit(0);
1060 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001063 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001064 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001065 nasm_ctype_tasm_mode();
1066 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001070 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001072
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001073 case 'e': /* preprocess only */
1074 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001075 if (pass == 1)
1076 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001078
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001079 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001080 if (pass == 1)
1081 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001083
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001084 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001085 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001086 if (pass == 2)
1087 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001088 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001091 if (pass == 1) {
1092 switch (p[2]) {
1093 case 'W':
1094 quote_for_make = quote_for_wmake;
1095 break;
1096 case 'D':
1097 case 'F':
1098 case 'T':
1099 case 'Q':
1100 advance = true;
1101 break;
1102 default:
1103 break;
1104 }
1105 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001106 switch (p[2]) {
1107 case 0:
1108 operating_mode = OP_DEPEND;
1109 break;
1110 case 'G':
1111 operating_mode = OP_DEPEND;
1112 depend_missing_ok = true;
1113 break;
1114 case 'P':
1115 depend_emit_phony = true;
1116 break;
1117 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001118 operating_mode |= OP_DEPEND;
1119 if (q && (q[0] != '-' || q[1] == '\0')) {
1120 depend_file = q;
1121 advance = true;
1122 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001123 break;
1124 case 'F':
1125 depend_file = q;
1126 advance = true;
1127 break;
1128 case 'T':
1129 depend_target = q;
1130 advance = true;
1131 break;
1132 case 'Q':
1133 depend_target = quote_for_make(q);
1134 advance = true;
1135 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001136 case 'W':
1137 /* handled in pass 1 */
1138 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001139 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001140 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001141 break;
1142 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001143 }
1144 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001145 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001146 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001147 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 case '-':
1151 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001152 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001153 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001154 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001155 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001156
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001157 p += 2;
1158
1159 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001160 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 break;
1162 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001163
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001164 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001166 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001167 olen = strlen(tx->label);
1168
1169 if (olen > plen)
1170 continue;
1171
1172 if (nasm_memicmp(p, tx->label, olen))
1173 continue;
1174
1175 if (tx->label[olen-1] == '-')
1176 break; /* Incomplete option */
1177
1178 if (!p[olen] || p[olen] == '=')
1179 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001181
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001182 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001183 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001184 }
1185
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001186 opt = tx->opt;
1187
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001188 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001189 if (param)
1190 *param++ = '\0';
1191
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001192 switch (tx->need_arg) {
1193 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001194 if (!param) {
1195 param = q;
1196 advance = true;
1197 }
1198
1199 /* Note: a null string is a valid parameter */
1200 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001201 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001202 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001204 break;
1205
1206 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001207 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001208 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001209 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001210 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001211 break;
1212
1213 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1214 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001215 }
1216
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001217 switch (opt) {
1218 case OPT_BOGUS:
1219 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001220 case OPT_VERSION:
1221 show_version();
1222 break;
1223 case OPT_ABORT_ON_PANIC:
1224 abort_on_panic = true;
1225 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001226 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001227 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001228 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001229 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001230 case OPT_INCLUDE:
1231 if (pass == 2)
1232 preproc->pre_include(q);
1233 break;
1234 case OPT_PRAGMA:
1235 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001236 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001237 break;
1238 case OPT_BEFORE:
1239 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001240 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001241 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001242 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001243 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001244 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001245 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001246 case OPT_KEEP_ALL:
1247 keep_all = true;
1248 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001249 case OPT_NO_LINE:
1250 pp_noline = true;
1251 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001252 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001253 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001254 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001255 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001256 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001257 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001258 default:
1259 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001261
1262 if (eqsave)
1263 *eqsave = '='; /* Restore = argument separator */
1264
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 break;
1266 }
1267
1268 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001269 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 break;
1271 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001272 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001273 /* In theory we could allow multiple input files... */
1274 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001275 }
1276
1277 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001278}
1279
H. Peter Anvineba20a72002-04-30 20:53:55 +00001280#define ARG_BUF_DELTA 128
1281
H. Peter Anvin55568c12016-10-03 19:46:49 -07001282static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001284 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285 int bufsize, prevargsize;
1286
1287 bufsize = prevargsize = ARG_BUF_DELTA;
1288 buffer = nasm_malloc(ARG_BUF_DELTA);
1289 prevarg = nasm_malloc(ARG_BUF_DELTA);
1290 prevarg[0] = '\0';
1291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 p = buffer;
1294 while (1) { /* Loop to handle long lines */
1295 q = fgets(p, bufsize - (p - buffer), rfile);
1296 if (!q)
1297 break;
1298 p += strlen(p);
1299 if (p > buffer && p[-1] == '\n')
1300 break;
1301 if (p - buffer > bufsize - 10) {
1302 int offset;
1303 offset = p - buffer;
1304 bufsize += ARG_BUF_DELTA;
1305 buffer = nasm_realloc(buffer, bufsize);
1306 p = buffer + offset;
1307 }
1308 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001309
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (!q && p == buffer) {
1311 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001312 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 nasm_free(buffer);
1314 nasm_free(prevarg);
1315 return;
1316 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001317
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 /*
1319 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1320 * them are present at the end of the line.
1321 */
1322 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001323
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001324 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001326
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001327 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001328
H. Peter Anvin55568c12016-10-03 19:46:49 -07001329 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331
Charles Crayne192d5b52007-10-18 19:02:42 -07001332 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 prevargsize += ARG_BUF_DELTA;
1334 prevarg = nasm_realloc(prevarg, prevargsize);
1335 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001336 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001337 }
1338}
1339
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001340/* Function to process args from a string of args, rather than the
1341 * argv array. Used by the environment variable and response file
1342 * processing.
1343 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001344static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001346 char *p, *q, *arg, *prevarg;
1347 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001348
1349 p = args;
1350 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001352 arg = NULL;
1353 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 q = p;
1355 while (*p && *p != separator)
1356 p++;
1357 while (*p == separator)
1358 *p++ = '\0';
1359 prevarg = arg;
1360 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001361 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001363 }
1364 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001365 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001366}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001367
H. Peter Anvin55568c12016-10-03 19:46:49 -07001368static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001369{
1370 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001371 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001372 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001373 perror(file);
1374 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001375 }
1376 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001377 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001378 }
1379 fclose(f);
1380}
1381
H. Peter Anvin55568c12016-10-03 19:46:49 -07001382static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383{
1384 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001385 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001386
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001387 /*
1388 * Initialize all the warnings to their default state, including
1389 * warning index 0 used for "always on".
1390 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001391 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001392
1393 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001394 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001395 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001396 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001397 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001399 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001401 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001402
1403 /*
1404 * Now process the actual command line.
1405 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001407 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 argv++;
1409 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001410 /*
1411 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 * arguments like the environment variable. This allows us
1413 * to have multiple arguments on a single line, which is
1414 * different to the -@resp file processing below for regular
1415 * NASM.
1416 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001417 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 argc--;
1419 argv++;
1420 }
1421 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001422 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001423 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001424 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001425 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001426 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001428 } else {
1429 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1430 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 }
1432 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001433 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001434 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001435 }
1436
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001437 /*
1438 * Look for basic command line typos. This definitely doesn't
1439 * catch all errors, but it might help cases of fumbled fingers.
1440 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001441 if (pass != 2)
1442 return;
1443
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001444 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001445 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001446 else if ((errname && !strcmp(inname, errname)) ||
1447 (outname && !strcmp(inname, outname)) ||
1448 (listname && !strcmp(inname, listname)) ||
1449 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001450 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001451
1452 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001453 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001454 if (!error_file) {
1455 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001456 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001457 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001458 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001459}
1460
H. Peter Anvin29651542018-12-18 19:14:40 -08001461static void forward_refs(insn *instruction)
1462{
1463 int i;
1464 struct forwrefinfo *fwinf;
1465
1466 instruction->forw_ref = false;
1467
1468 if (!optimizing.level)
1469 return; /* For -O0 don't bother */
1470
1471 if (!forwref)
1472 return;
1473
1474 if (forwref->lineno != globallineno)
1475 return;
1476
1477 instruction->forw_ref = true;
1478 do {
1479 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1480 forwref = saa_rstruct(forwrefs);
1481 } while (forwref && forwref->lineno == globallineno);
1482
1483 if (!pass_first())
1484 return;
1485
1486 for (i = 0; i < instruction->operands; i++) {
1487 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1488 fwinf = saa_wstruct(forwrefs);
1489 fwinf->lineno = globallineno;
1490 fwinf->operand = i;
1491 }
1492 }
1493}
1494
1495static void process_insn(insn *instruction)
1496{
1497 int32_t n;
1498 int64_t l;
1499
1500 if (!instruction->times)
1501 return; /* Nothing to do... */
1502
1503 nasm_assert(instruction->times > 0);
1504
1505 /*
1506 * NOTE: insn_size() can change instruction->times
1507 * (usually to 1) when called.
1508 */
1509 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001510 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001511 for (n = 1; n <= instruction->times; n++) {
1512 l = insn_size(location.segment, location.offset,
1513 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001514 /* l == -1 -> invalid instruction */
1515 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001516 increment_offset(l);
1517 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001518 if (list_option('p')) {
1519 struct out_data dummy;
1520 memset(&dummy, 0, sizeof dummy);
1521 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1522 dummy.offset = start;
1523 dummy.size = location.offset - start;
1524 lfmt->output(&dummy);
1525 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001526 } else {
1527 l = assemble(location.segment, location.offset,
1528 globalbits, instruction);
1529 /* We can't get an invalid instruction here */
1530 increment_offset(l);
1531
1532 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001533 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001534 for (n = 2; n <= instruction->times; n++) {
1535 l = assemble(location.segment, location.offset,
1536 globalbits, instruction);
1537 increment_offset(l);
1538 }
1539 lfmt->downlevel(LIST_TIMES);
1540 }
1541 }
1542}
1543
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001544static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001545{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001546 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001548 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001549 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001550
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001551 switch (cmd_sb) {
1552 case 16:
1553 break;
1554 case 32:
1555 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001556 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001557 break;
1558 case 64:
1559 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001560 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001561 break;
1562 default:
1563 panic();
1564 break;
1565 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001566
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001567 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001568
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001569 if (listname && !keep_all) {
1570 /* Remove the list file in case we die before the output pass */
1571 remove(listname);
1572 }
1573
1574 while (!terminate_after_phase && !pass_final()) {
1575 _passn++;
1576 if (pass_type() != PASS_OPT || !global_offset_changed)
1577 _pass_type++;
1578 global_offset_changed = 0;
1579
1580 /*
1581 * Create a warning buffer list unless we are in
1582 * pass 2 (everything will be emitted immediately in pass 2.)
1583 */
1584 if (warn_list) {
1585 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001586 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001587 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001588
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001589 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001590 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001591
H. Peter Anvincac0b192017-03-28 16:12:30 -07001592 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001594 if (listname) {
1595 if (pass_final() || list_on_every_pass()) {
1596 active_list_options = list_options;
1597 lfmt->init(listname);
1598 } else if (active_list_options) {
1599 /*
1600 * Looks like we used the list engine on a previous pass,
1601 * but now it is turned off, presumably via %pragma -p
1602 */
1603 lfmt->cleanup();
1604 if (!keep_all)
1605 remove(listname);
1606 active_list_options = 0;
1607 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001608 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001609
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001610 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001611 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 saa_rewind(forwrefs);
1613 forwref = saa_rstruct(forwrefs);
1614 raa_free(offsets);
1615 offsets = raa_init();
1616 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001617 location.segment = NO_SEG;
1618 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001619 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001620 location.known = true;
1621 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001622 switch_segment(ofmt->section(NULL, &globalbits));
1623 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001624
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001626
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001628 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001629 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001630 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001632 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001633 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001634 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001635 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001636 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001637 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001638
H. Peter Anvinc7131682017-03-07 17:45:01 -08001639 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001640 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001641 forward_refs(&output_ins);
1642 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001643 cleanup_insn(&output_ins);
1644
1645 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001648
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001649 preproc->cleanup_pass();
1650
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001651 if (global_offset_changed) {
1652 switch (pass_type()) {
1653 case PASS_OPT:
1654 /*
1655 * This is the only pass type that can be executed more
1656 * than once, and therefore has the ability to stall.
1657 */
1658 if (global_offset_changed < prev_offset_changed) {
1659 prev_offset_changed = global_offset_changed;
1660 stall_count = 0;
1661 } else {
1662 stall_count++;
1663 }
1664
1665 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1666 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1667 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001668 nasm_nonfatalf(ERR_UNDEAD,
1669 "unable to find valid values for all labels "
1670 "after %"PRId64" passes; "
1671 "stalled for %"PRId64", giving up.",
1672 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001673 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001674 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001675 }
1676 break;
1677
1678 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001679 /*!
1680 *!phase [off] phase error during stabilization
1681 *! warns about symbols having changed values during
1682 *! the second-to-last assembly pass. This is not
1683 *! inherently fatal, but may be a source of bugs.
1684 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001685 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1686 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001687 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001688 break;
1689
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001690 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001691 nasm_nonfatalf(ERR_UNDEAD,
1692 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001693 break;
1694
1695 default:
1696 /* This is normal, we'll keep going... */
1697 break;
1698 }
1699 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001700
1701 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001702 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001703
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001704 if (opt_verbose_info && pass_final()) {
1705 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001706 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001707 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001708
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001709 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001710 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001711}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001712
Ed Berosetfa771012002-06-09 20:56:40 +00001713/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001714 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001715 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001716static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001717{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001718 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001719
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001720 if ((severity & ERR_MASK) >= ERR_FATAL)
1721 return 0; /* Fatal errors are never suppressible */
1722
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001723 /* Warnings MUST HAVE a warning category specifier! */
1724 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001725
1726 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001727 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001728
1729 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001730}
1731
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001732static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001733{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001734 errflags type = severity & ERR_MASK;
1735
H. Peter Anvin8f622462017-04-02 19:02:29 -07001736 /*
1737 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001738 * We can never skip fatal errors as by definition they cannot be
1739 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001740 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001741 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001742 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001743
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001744 /*
1745 * ERR_LISTMSG messages are always skipped; the list file
1746 * receives them anyway as this function is not consulted
1747 * for sending to the list file.
1748 */
1749 if (type == ERR_LISTMSG)
1750 return true;
1751
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001752 /* This message not applicable unless pass_final */
1753 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001754}
1755
Ed Berosetfa771012002-06-09 20:56:40 +00001756/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001757 * check for suppressed message (usually warnings or notes)
1758 *
1759 * @param severity the severity of the warning or error
1760 * @return true if we should abort error/warning printing
1761 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001762static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001763{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001764 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001765 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001766 return false;
1767
1768 /* This error/warning is pointless if we are dead anyway */
1769 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1770 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001771
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001772 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1773}
1774
1775/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001776 * Return the true error type (the ERR_MASK part) of the given
1777 * severity, accounting for warnings that may need to be promoted to
1778 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001779 *
1780 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001781 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001782 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001783static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001784{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001785 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1786 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001787
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001788 type = severity & ERR_MASK;
1789
1790 /* Promote warning to error? */
1791 if (type == ERR_WARNING) {
1792 uint8_t state = warning_state[warn_index(severity)];
1793 if ((state & warn_is_err) == warn_is_err)
1794 type = ERR_NONFATAL;
1795 }
1796
1797 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001798}
1799
1800/**
Ed Berosetfa771012002-06-09 20:56:40 +00001801 * common error reporting
1802 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001803 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001804 * specific error message to error_file and may or may not return. It
1805 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001806 *
1807 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001808 * @param fmt the printf style format string
1809 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001810static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001811{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001812 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001813 char warnsuf[64];
1814 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001815 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001816 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001817 const char *currentfile = NULL;
1818 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001819 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001820 ";;; ", "debug: ", "info: ", "warning: ",
1821 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001822 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001823
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001824 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001825 return;
1826
1827 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001828 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001829 if (!currentfile) {
1830 currentfile = currentfile ? currentfile :
1831 inname && inname[0] ? inname :
1832 outname && outname[0] ? outname :
1833 NULL;
1834 lineno = 0;
1835 }
1836 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001837
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001838 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001839 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001840 else
1841 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001842
1843 vsnprintf(msg, sizeof msg, fmt, args);
1844 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001845 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1846 /*
1847 * It's a warning without ERR_HERE defined, and we are not already
1848 * unwinding the macros that led us here.
1849 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001850 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001851 (true_type >= ERR_NONFATAL) ? "error=" : "",
1852 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001853 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001854
H. Peter Anvinddb29062018-12-11 00:06:29 -08001855 *linestr = 0;
1856 if (lineno) {
1857 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1858 errfmt->beforeline, lineno, errfmt->afterline);
1859 }
1860
H. Peter Anvin77016c82018-12-10 22:29:49 -08001861 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001862 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001863 const char *here = "";
1864
1865 if (severity & ERR_HERE)
1866 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001867
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001868 if (warn_list && true_type < ERR_NONFATAL &&
1869 !(pass_first() && (severity & ERR_PASS1)))
1870 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001871 /*
1872 * Buffer up warnings until we either get an error
1873 * or we are on the code-generation pass.
1874 */
1875 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1876 file, linestr, errfmt->beforemsg,
1877 pfx, msg, here, warnsuf);
1878 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001879 /*
1880 * If we have buffered warnings, and this is a non-warning,
1881 * output them now.
1882 */
1883 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001884 strlist_write(warn_list, "\n", error_file);
1885 strlist_free(&warn_list);
1886 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001887
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001888 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1889 file, linestr, errfmt->beforemsg,
1890 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001891
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001892 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001893 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001894
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001895 /* Are we recursing from error_list_macros? */
1896 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001897 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001898
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001899 /*
1900 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001901 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001902 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001903 if (severity & ERR_HERE) {
1904 if (lineno)
1905 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1906 pfx, msg, currentfile, lineno, warnsuf);
1907 else if (currentfile)
1908 lfmt->error(severity, "%s%s in file %s%s",
1909 pfx, msg, currentfile, warnsuf);
1910 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001911 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001912 pfx, msg, warnsuf);
1913 } else {
1914 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1915 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001916
H. Peter Anvin8f622462017-04-02 19:02:29 -07001917 if (skip_this_pass(severity))
1918 return;
1919
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001920 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001922
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001923 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1924 if (!(severity & ERR_HERE))
1925 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001926
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001927 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001928 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001930 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001931 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001932 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001933 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001935 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 break;
1937 case ERR_FATAL:
1938 if (ofile) {
1939 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001940 if (!keep_all)
1941 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001942 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 }
1944 if (want_usage)
1945 usage();
1946 exit(1); /* instantly die */
1947 break; /* placate silly compilers */
1948 case ERR_PANIC:
1949 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001950
1951 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001952 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001953
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001954 if (ofile) {
1955 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001956 if (!keep_all)
1957 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001958 ofile = NULL;
1959 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 exit(3);
1961 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001962 default:
1963 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001964 }
1965}
1966
H. Peter Anvin734b1882002-04-30 21:01:08 +00001967static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001968{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001969 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001970}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001971
H. Peter Anvin322bee02019-08-10 01:38:06 -07001972static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001973{
1974 int i;
1975
H. Peter Anvin322bee02019-08-10 01:38:06 -07001976 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001977 "Usage: %s [-@ response_file] [options...] [--] filename\n"
1978 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07001979 _progname, _progname);
1980 fputs(
1981 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001982 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001983 "\n"
1984 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001985 " -v (or --v) print the NASM version number and exit\n"
1986 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001987 "\n"
1988 " -o outfile write output to outfile\n"
1989 " --keep-all output files will not be removed even if an error happens\n"
1990 "\n"
1991 " -Xformat specifiy error reporting format (gnu or vc)\n"
1992 " -s redirect error messages to stdout\n"
1993 " -Zfile redirect error messages to file\n"
1994 "\n"
1995 " -M generate Makefile dependencies on stdout\n"
1996 " -MG d:o, missing files assumed generated\n"
1997 " -MF file set Makefile dependency file\n"
1998 " -MD file assemble and generate dependencies\n"
1999 " -MT file dependency target name\n"
2000 " -MQ file dependency target name (quoted)\n"
2001 " -MP emit phony targets\n"
2002 "\n"
2003 " -f format select output file format\n"
2004 , out);
2005 ofmt_list(ofmt, out);
2006 fputs(
2007 "\n"
2008 " -g generate debugging information\n"
2009 " -F format select a debugging format (output format dependent)\n"
2010 " -gformat same as -g -F format\n"
2011 , out);
2012 dfmt_list(out);
2013 fputs(
2014 "\n"
2015 " -l listfile write listing to a list file\n"
2016 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002017 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002018 " -Ld show byte and repeat counts in decimal, not hex\n"
2019 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002020 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002021 " -Lm show all single-line macro definitions\n"
2022 " -Lp output a list file every pass, in case of errors\n"
2023 "\n"
2024 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2025 " -O0 no optimization\n"
2026 " -O1 minimal optimization\n"
2027 " -Ox multipass optimization (default)\n"
2028 " -Ov display the number of passes executed at the end\n"
2029 " -t assemble in limited SciTech TASM compatible mode\n"
2030 "\n"
2031 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2032 " -a don't preprocess (assemble only)\n"
2033 " -Ipath add a pathname to the include file path\n"
2034 " -Pfile pre-include a file (also --include)\n"
2035 " -Dmacro[=str] pre-define a macro\n"
2036 " -Umacro undefine a macro\n"
2037 " --pragma str pre-executes a specific %%pragma\n"
2038 " --before str add line (usually a preprocessor statement) before the input\n"
2039 " --no-line ignore %line directives in input\n"
2040 "\n"
2041 " --prefix str prepend the given string to the names of all extern,\n"
2042 " common and global symbols (also --gprefix)\n"
2043 " --suffix str append the given string to the names of all extern,\n"
2044 " common and global symbols (also --gprefix)\n"
2045 " --lprefix str prepend the given string to local symbols\n"
2046 " --lpostfix str append the given string to local symbols\n"
2047 "\n"
2048 " -w+x enable warning x (also -Wx)\n"
2049 " -w-x disable warning x (also -Wno-x)\n"
2050 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2051 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2052 , out);
2053
2054 fprintf(out, " %-20s %s\n",
2055 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2056
2057 for (i = 1; i < WARN_IDX_ALL; i++) {
2058 const char *me = warning_name[i];
2059 const char *prev = warning_name[i-1];
2060 const char *next = warning_name[i+1];
2061
2062 if (prev) {
2063 int prev_len = strlen(prev);
2064 const char *dash = me;
2065
2066 while ((dash = strchr(dash+1, '-'))) {
2067 int prefix_len = dash - me; /* Not including final dash */
2068 if (strncmp(next, me, prefix_len+1)) {
2069 /* Only one or last option with this prefix */
2070 break;
2071 }
2072 if (prefix_len >= prev_len ||
2073 strncmp(prev, me, prefix_len) ||
2074 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2075 /* This prefix is different from the previous option */
2076 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2077 prefix_len, me, prefix_len+1, me);
2078 }
2079 }
2080 }
2081
2082 fprintf(out, " %-20s %s%s\n",
2083 warning_name[i], warning_help[i],
2084 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2085 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2086 }
2087
2088 fputs(
2089 "\n"
2090 " --limit-X val set execution limit X\n"
2091 , out);
2092
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002093
2094 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002095 fprintf(out, " %-20s %s [",
2096 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002097 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002098 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002099 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002100 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002101 }
2102 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002103}