blob: 7f24b1b7f9fe093a8341733307502920cfce4702 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07003 * Copyright 1996-2020 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 Anvin (Intel)6e9554f2020-06-14 23:21:44 -070047#include "floats.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 Anvin (Intel)b2927482020-06-14 20:09:11 -070058#include "quote.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080059#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvin31387b22010-07-15 18:28:52 -070061/*
62 * This is the maximum number of optimization passes to do. If we ever
63 * find a case where the optimizer doesn't naturally converge, we might
64 * have to drop this value so the assembler doesn't appear to just hang.
65 */
66#define MAX_OPTIMIZE (INT_MAX >> 1)
67
H. Peter Anvine2c80182005-01-15 22:15:51 +000068struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000069 int lineno;
70 int operand;
71};
72
H. Peter Anvin322bee02019-08-10 01:38:06 -070073const char *_progname;
74
H. Peter Anvin55568c12016-10-03 19:46:49 -070075static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030076static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080077static bool skip_this_pass(errflags severity);
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 Anvin (Intel)283bc922020-06-04 16:19:51 -070091static struct nasm_errhold *errhold_stack;
H. Peter Anvin77016c82018-12-10 22:29:49 -080092
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070093unsigned int debug_nasm; /* Debugging messages? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000094
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080095static bool using_debug_info, opt_verbose_info;
96static const char *debug_format;
97
H. Peter Anvin3366e312018-02-07 14:14:36 -080098#ifndef ABORT_ON_PANIC
99# define ABORT_ON_PANIC 0
100#endif
101static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700102static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800103
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700104bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800105enum pass_type _pass_type;
106const char * const _pass_types[] =
107{
H. Peter Anvin (Intel)91bc5182020-07-08 09:14:58 -0700108 "init", "preproc-only", "first", "optimize", "stabilize", "final"
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800109};
110int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000111int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800112int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000113
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700114struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800115
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800116const char *inname;
117const char *outname;
118static const char *listname;
119static const char *errname;
120
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700121static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700122
H. Peter Anvin338656c2016-02-17 20:59:22 -0800123const struct ofmt *ofmt = &OF_DEFAULT;
124const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400125const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800127FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000128
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400129FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300130struct optimization optimizing =
131 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100132static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400133
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800134iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400135static iflag_t cmd_cpu;
136
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800137struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800138bool in_absolute; /* Flag we are in ABSOLUTE seg */
139struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000140
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000142
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700144static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145
H. Peter Anvine7469712016-02-18 02:20:59 -0800146static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300147static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800148bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400149
H. Peter Anvin34754622018-11-28 12:36:53 -0800150#define OP_NORMAL (1U << 0)
151#define OP_PREPROCESS (1U << 1)
152#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400153
154static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400155
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700156/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700157static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700158static bool depend_missing_ok = false;
159static const char *depend_target = NULL;
160static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300161struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000162
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700163static bool want_usage;
164static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800165bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000166
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700167static char *quote_for_pmake(const char *str);
168static char *quote_for_wmake(const char *str);
169static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700170
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700171/*
172 * Execution limits that can be set via a command-line option or %pragma
173 */
174
H. Peter Anvin322bee02019-08-10 01:38:06 -0700175/*
176 * This is really unlimited; it would take far longer than the
177 * current age of the universe for this limit to be reached even on
178 * much faster CPUs than currently exist.
179*/
180#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700181
H. Peter Anvin322bee02019-08-10 01:38:06 -0700182int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183
184struct limit_info {
185 const char *name;
186 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700187 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700189/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700190static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700191 { "passes", "total number of passes", LIMIT_MAX_VAL },
192 { "stalled-passes", "number of passes without forward progress", 1000 },
193 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700194 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
195 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700196 { "rep", "%rep count", 1000000 },
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700197 { "eval", "expression evaluation descent", 8192 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700198 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199};
200
H. Peter Anvin322bee02019-08-10 01:38:06 -0700201static void set_default_limits(void)
202{
203 int i;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700204 size_t rl;
205 int64_t new_limit;
206
H. Peter Anvin322bee02019-08-10 01:38:06 -0700207 for (i = 0; i <= LIMIT_MAX; i++)
208 nasm_limit[i] = limit_info[i].default_val;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700209
210 /*
211 * Try to set a sensible default value for the eval depth based
212 * on the limit of the stack size, if knowable...
213 */
214 rl = nasm_get_stack_size_limit();
215 new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
216 if (new_limit < nasm_limit[LIMIT_EVAL])
217 nasm_limit[LIMIT_EVAL] = new_limit;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700218}
219
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700220enum directive_result
221nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700222{
223 int i;
224 int64_t val;
225 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700226 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700227
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700228 if (!limit)
229 limit = "";
230 if (!valstr)
231 valstr = "";
232
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700233 for (i = 0; i <= LIMIT_MAX; i++) {
234 if (!nasm_stricmp(limit, limit_info[i].name))
235 break;
236 }
237 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800238 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800239 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700240 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700241 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700242 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700243 return DIRR_ERROR;
244 }
245
246 if (!nasm_stricmp(valstr, "unlimited")) {
247 val = LIMIT_MAX_VAL;
248 } else {
249 val = readnum(valstr, &rn_error);
250 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800251 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800252 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700253 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700254 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700255 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700256 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700257 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700258 if (val > LIMIT_MAX_VAL)
259 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700260 }
261
262 nasm_limit[i] = val;
263 return DIRR_OK;
264}
265
H. Peter Anvin892c4812018-05-30 14:43:46 -0700266int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400267{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700268 location.segment = segment;
269 if (segment == NO_SEG) {
270 location.offset = absolute.offset;
271 in_absolute = true;
272 } else {
273 location.offset = raa_read(offsets, segment);
274 in_absolute = false;
275 }
276 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400277}
278
279static void set_curr_offs(int64_t l_off)
280{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800281 if (in_absolute)
282 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400283 else
284 offsets = raa_write(offsets, location.segment, l_off);
285}
286
H. Peter Anvin892c4812018-05-30 14:43:46 -0700287static void increment_offset(int64_t delta)
288{
289 if (unlikely(delta == 0))
290 return;
291
292 location.offset += delta;
293 set_curr_offs(location.offset);
294}
295
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800296/*
297 * Define system-defined macros that are not part of
298 * macros/standard.mac.
299 */
300static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800301{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700302 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800303 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800304
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700305 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700306 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400307 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700308 strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400309 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700310 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400311 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700312 strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800314 }
315
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700316 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700317 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400318 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700319 strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400320 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700321 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400322 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700323 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400324 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800325 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700326
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700327 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700328 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400329 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800330 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400332 /*
333 * In case if output format is defined by alias
334 * we have to put shortname of the alias itself here
335 * otherwise ABI backward compatibility gets broken.
336 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700337 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400338 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400339 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800340
341 /*
342 * Output-format specific macros.
343 */
344 if (ofmt->stdmac)
345 preproc->extra_stdmac(ofmt->stdmac);
346
347 /*
348 * Debug format, if any
349 */
350 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700351 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800352 preproc->pre_define(temp);
353 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800354}
355
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800356/*
357 * Initialize the preprocessor, set up the include path, and define
358 * the system-included macros. This is called between passes 1 and 2
359 * of parsing the command options; ofmt and dfmt are defined at this
360 * point.
361 *
362 * Command-line specified preprocessor directives (-p, -d, -u,
363 * --pragma, --before) are processed after this function.
364 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300365static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800366{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800367 preproc->init();
368 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300369 preproc->include_path(ipath);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000370}
371
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300372static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700373{
374 FILE *deps;
375 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700376 bool wmake = (quote_for_make == quote_for_wmake);
377 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800378 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700379
380 if (!list)
381 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700382
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700383 wrapstr = wmake ? " &\n " : " \\\n ";
384 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385
386 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700387 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400388 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800389 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400390 return;
391 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700392 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400393 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700394 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700395
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700396 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800397 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700398 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400399 len = strlen(file);
400 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700401 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400402 linepos = 1;
403 }
404 fprintf(deps, " %s", file);
405 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700406 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700407 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700408 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700409
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800410 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700411 if (depend_emit_phony) {
412 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700413 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700414 nasm_free(file);
415 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700416 }
417
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800418 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700419
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700420 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400421 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700422}
423
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700424/* Convert a struct tm to a POSIX-style time constant */
425static int64_t make_posix_time(const struct tm *tm)
426{
427 int64_t t;
428 int64_t y = tm->tm_year;
429
430 /* See IEEE 1003.1:2004, section 4.14 */
431
432 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
433 t += tm->tm_yday;
434 t *= 24;
435 t += tm->tm_hour;
436 t *= 60;
437 t += tm->tm_min;
438 t *= 60;
439 t += tm->tm_sec;
440
441 return t;
442}
443
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700444/*
445 * Quote a filename string if and only if it is necessary.
446 * It is considered necessary if any one of these is true:
447 * 1. The filename contains control characters;
448 * 2. The filename starts or ends with a space or quote mark;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700449 * 3. The filename contains more than one space in a row;
450 * 4. The filename is empty.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700451 *
452 * The filename is returned in a newly allocated buffer.
453 */
454static char *nasm_quote_filename(const char *fn)
455{
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700456 const unsigned char *p =
457 (const unsigned char *)fn;
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300458 size_t len;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700459
460 if (!p || !*p)
461 return nasm_strdup("\"\"");
462
463 if (*p <= ' ' || nasm_isquote(*p)) {
464 goto quote;
465 } else {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700466 unsigned char cutoff = ' ';
467
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700468 while (*p) {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700469 if (*p < cutoff)
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700470 goto quote;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700471 cutoff = ' ' + (*p == ' ');
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700472 p++;
473 }
474 if (p[-1] <= ' ' || nasm_isquote(p[-1]))
475 goto quote;
476 }
477
478 /* Quoting not necessary */
479 return nasm_strdup(fn);
480
481quote:
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300482 len = strlen(fn);
483 return nasm_quote(fn, &len);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700484}
485
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700486static void timestamp(void)
487{
488 struct compile_time * const oct = &official_compile_time;
Dale Curtis4ee6a692018-11-19 13:52:05 -0800489#if 1
490 // Chromium patch: Builds should be deterministic and not embed timestamps.
491 memset(oct, 0, sizeof(official_compile_time));
492#else
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700493 const struct tm *tp, *best_gm;
494
495 time(&oct->t);
496
497 best_gm = NULL;
498
499 tp = localtime(&oct->t);
500 if (tp) {
501 oct->local = *tp;
502 best_gm = &oct->local;
503 oct->have_local = true;
504 }
505
506 tp = gmtime(&oct->t);
507 if (tp) {
508 oct->gm = *tp;
509 best_gm = &oct->gm;
510 oct->have_gm = true;
511 if (!oct->have_local)
512 oct->local = oct->gm;
513 } else {
514 oct->gm = oct->local;
515 }
516
517 if (best_gm) {
518 oct->posix = make_posix_time(best_gm);
519 oct->have_posix = true;
520 }
Dale Curtis4ee6a692018-11-19 13:52:05 -0800521#endif
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700522}
523
H. Peter Anvin038d8612007-04-12 16:54:50 +0000524int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700526 /* Do these as early as possible */
527 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700528 _progname = argv[0];
529 if (!_progname || !_progname[0])
530 _progname = "nasm";
531
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700532 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800533
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800534 iflag_set_default_cpu(&cpu);
535 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400536
H. Peter Anvin322bee02019-08-10 01:38:06 -0700537 set_default_limits();
538
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800539 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300540
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800541 _pass_type = PASS_INIT;
542 _passn = 0;
543
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700544 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000545
H. Peter Anvin13506202018-11-28 14:55:58 -0800546 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700547 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700548
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700549 /*
550 * We must call init_labels() before the command line parsing,
551 * because we may be setting prefixes/suffixes from the command
552 * line.
553 */
554 init_labels();
555
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000556 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000557 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000558
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000559 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400560 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000561
H. Peter Anvin55568c12016-10-03 19:46:49 -0700562 parse_cmdline(argc, argv, 1);
563 if (terminate_after_phase) {
564 if (want_usage)
565 usage();
566 return 1;
567 }
568
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800569 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800570 if (!using_debug_info) {
571 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800572 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800573 } else if (!debug_format) {
574 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300575 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800576 } else {
577 dfmt = dfmt_find(ofmt, debug_format);
578 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800579 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800580 debug_format, ofmt->shortname);
581 }
582 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000583
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300584 preproc_init(include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000585
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 parse_cmdline(argc, argv, 2);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000587 if (terminate_after_phase) {
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000588 if (want_usage)
589 usage();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000591 }
592
593 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800594 init_warnings();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595
H. Peter Anvin34754622018-11-28 12:36:53 -0800596 /* Dependency filename if we are also doing other things */
597 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
598 if (outname)
599 depend_file = nasm_strcat(outname, ".d");
600 else
601 depend_file = filename_set_extension(inname, ".d");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602 }
603
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300604 /*
605 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800606 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300607 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300608 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800609 if (!outname && !(operating_mode & OP_PREPROCESS)) {
610 outname = filename_set_extension(inname, ofmt->extension);
611 if (!strcmp(outname, inname)) {
612 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800613 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 -0800614 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300615 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800616
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800617 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700618
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700619 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400620 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700621
H. Peter Anvin34754622018-11-28 12:36:53 -0800622 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000623 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700624
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400625 if (depend_missing_ok)
626 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700627
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800628 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 while ((line = preproc->getline()))
631 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800632 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800633 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400634 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000635 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700636 const char *file_name = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700637 char *quoted_file_name = nasm_quote_filename(file_name);
638 int32_t linnum = 0;
639 int32_t lineinc = 0;
640 FILE *out;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800642 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700643 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800645 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700646 out = ofile;
647 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 ofile = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700649 out = stdout;
650 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000651
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700652 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000653
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -0700654 _pass_type = PASS_PREPROC;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800655 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700656
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 while ((line = preproc->getline())) {
658 /*
659 * We generate %line directives if needed for later programs
660 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700661 struct src_location where = src_where();
662 if (file_name != where.filename) {
663 file_name = where.filename;
664 linnum = -1; /* Force a new %line statement */
665 lineinc = file_name ? 1 : 0;
666 nasm_free(quoted_file_name);
667 quoted_file_name = nasm_quote_filename(file_name);
668 } else if (lineinc) {
669 if (linnum + lineinc == where.lineno) {
670 /* Add one blank line to account for increment */
671 fputc('\n', out);
672 linnum += lineinc;
673 } else if (linnum - lineinc == where.lineno) {
674 /*
675 * Standing still, probably a macro. Set increment
676 * to zero.
677 */
678 lineinc = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700680 } else {
681 /* lineinc == 0 */
682 if (linnum + 1 == where.lineno)
683 lineinc = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700685
686 /* Skip blank lines if we will need a %line anyway */
687 if (linnum == -1 && !line[0])
688 continue;
689
690 if (linnum != where.lineno) {
691 fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
692 where.lineno, lineinc, quoted_file_name);
693 }
694 linnum = where.lineno + lineinc;
695
696 fputs(line, out);
697 fputc('\n', out);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700699
700 nasm_free(quoted_file_name);
701
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800702 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800703 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 if (ofile)
705 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700706 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400708 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400709 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400711 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700712 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400713 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800714 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400716 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400717 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700719 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200720
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400721 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800722 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400723 cleanup_labels();
724 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800725 if (ferror(ofile))
726 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400727 }
728
729 if (ofile) {
730 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700731 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400732 remove(outname);
733 ofile = NULL;
734 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000735 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000736
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800737 preproc->cleanup_session();
738
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700739 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400740 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700741
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000742 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000744
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 raa_free(offsets);
746 saa_free(forwrefs);
747 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000748 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700749 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800750 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700752 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000753}
754
H. Peter Anvineba20a72002-04-30 20:53:55 +0000755/*
756 * Get a parameter for a command line option.
757 * First arg must be in the form of e.g. -f...
758 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800759static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000760{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400762 if (p[2]) /* the parameter's in the option */
763 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800765 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000767 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800768 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000769 return NULL;
770}
771
H. Peter Anvindc242712007-11-18 11:55:10 -0800772/*
773 * Copy a filename
774 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800775static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800776{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800777 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700778 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800779
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800780 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800781}
782
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700783/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700784 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700785 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700786static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700787{
788 const char *p;
789 char *os, *q;
790
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400791 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700792 size_t nbs = 0;
793
794 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400795 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700796
797 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400798 switch (*p) {
799 case ' ':
800 case '\t':
801 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
802 n += nbs + 2;
803 nbs = 0;
804 break;
805 case '$':
806 case '#':
807 nbs = 0;
808 n += 2;
809 break;
810 case '\\':
811 nbs++;
812 n++;
813 break;
814 default:
815 nbs = 0;
816 n++;
817 break;
818 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700819 }
820
821 /* Convert N backslashes at the end of filename to 2N backslashes */
822 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400823 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700824
825 os = q = nasm_malloc(n);
826
827 nbs = 0;
828 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400829 switch (*p) {
830 case ' ':
831 case '\t':
832 while (nbs--)
833 *q++ = '\\';
834 *q++ = '\\';
835 *q++ = *p;
836 break;
837 case '$':
838 *q++ = *p;
839 *q++ = *p;
840 nbs = 0;
841 break;
842 case '#':
843 *q++ = '\\';
844 *q++ = *p;
845 nbs = 0;
846 break;
847 case '\\':
848 *q++ = *p;
849 nbs++;
850 break;
851 default:
852 *q++ = *p;
853 nbs = 0;
854 break;
855 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700856 }
857 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400858 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700859
860 *q = '\0';
861
862 return os;
863}
864
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700865/*
866 * Convert a string to a Watcom make-safe form
867 */
868static char *quote_for_wmake(const char *str)
869{
870 const char *p;
871 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700872 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700873
874 size_t n = 1; /* Terminating zero */
875
876 if (!str)
877 return NULL;
878
879 for (p = str; *p; p++) {
880 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700881 case ' ':
882 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700883 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700884 quote = true;
885 n++;
886 break;
887 case '\"':
888 quote = true;
889 n += 2;
890 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700891 case '$':
892 case '#':
893 n += 2;
894 break;
895 default:
896 n++;
897 break;
898 }
899 }
900
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700901 if (quote)
902 n += 2;
903
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700904 os = q = nasm_malloc(n);
905
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700906 if (quote)
907 *q++ = '\"';
908
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700909 for (p = str; *p; p++) {
910 switch (*p) {
911 case '$':
912 case '#':
913 *q++ = '$';
914 *q++ = *p;
915 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700916 case '\"':
917 *q++ = *p;
918 *q++ = *p;
919 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700920 default:
921 *q++ = *p;
922 break;
923 }
924 }
925
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700926 if (quote)
927 *q++ = '\"';
928
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700929 *q = '\0';
930
931 return os;
932}
933
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100934enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800935 OPT_BOGUS,
936 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700937 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800938 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700939 OPT_MANGLE,
940 OPT_INCLUDE,
941 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700942 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700943 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800944 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700945 OPT_NO_LINE,
946 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100947};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700948enum need_arg {
949 ARG_NO,
950 ARG_YES,
951 ARG_MAYBE
952};
953
H. Peter Anvin3366e312018-02-07 14:14:36 -0800954struct textargs {
955 const char *label;
956 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700957 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700958 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800959};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800960static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700961 {"v", OPT_VERSION, ARG_NO, 0},
962 {"version", OPT_VERSION, ARG_NO, 0},
963 {"help", OPT_HELP, ARG_NO, 0},
964 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
965 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
966 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
967 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
968 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
969 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
970 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
971 {"include", OPT_INCLUDE, ARG_YES, 0},
972 {"pragma", OPT_PRAGMA, ARG_YES, 0},
973 {"before", OPT_BEFORE, ARG_YES, 0},
974 {"limit-", OPT_LIMIT, ARG_YES, 0},
975 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
976 {"no-line", OPT_NO_LINE, ARG_NO, 0},
977 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
978 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000979};
980
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400981static void show_version(void)
982{
Dale Curtis10760492018-10-31 13:03:37 -0700983 printf("NASM version %s%s\n",
984 nasm_version, nasm_compile_options);
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400985 exit(0);
986}
987
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700989static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000991 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000993
994 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800995 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000996
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700998 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 /* These parameters take values */
1000 if (!(param = get_param(p, q, &advance)))
1001 return advance;
1002 }
H. Peter Anvin423e3812007-11-15 17:12:29 -08001003
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 switch (p[1]) {
1005 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001006 if (pass == 1)
1007 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001009
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001012 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001013 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -07001014
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001015 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001016 if (pass == 1) {
1017 ofmt = ofmt_find(param, &ofmt_alias);
1018 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001019 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001020 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001021 }
1022 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001023
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001024 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001025 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001027
H. Peter Anvin55568c12016-10-03 19:46:49 -07001028 if (!*param) {
1029 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +03001030 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001031 } else {
1032 while (*param) {
1033 switch (*param) {
1034 case '0': case '1': case '2': case '3': case '4':
1035 case '5': case '6': case '7': case '8': case '9':
1036 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001037
Chang S. Baea5786342018-08-15 23:22:21 +03001038 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
1039 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +03001041 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001042 else
Chang S. Baea5786342018-08-15 23:22:21 +03001043 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001044 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001045
H. Peter Anvin55568c12016-10-03 19:46:49 -07001046 case 'v':
1047 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001048 param++;
1049 opt_verbose_info = true;
1050 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001051
H. Peter Anvin55568c12016-10-03 19:46:49 -07001052 case 'x':
1053 param++;
Chang S. Baea5786342018-08-15 23:22:21 +03001054 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001055 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001056
H. Peter Anvin55568c12016-10-03 19:46:49 -07001057 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001058 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -07001059 *param);
1060 break;
1061 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001062 }
Chang S. Baea5786342018-08-15 23:22:21 +03001063 if (optimizing.level > MAX_OPTIMIZE)
1064 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001065 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001066 }
1067 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001068
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001069 case 'p': /* pre-include */
1070 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001071 if (pass == 2)
1072 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001073 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001074
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001075 case 'd': /* pre-define */
1076 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001077 if (pass == 2)
1078 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001079 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001080
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001081 case 'u': /* un-define */
1082 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001083 if (pass == 2)
1084 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001085 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001086
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001087 case 'i': /* include search path */
1088 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001089 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001090 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001091 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001092
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001093 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001094 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001095 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001096 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001097
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001098 case 'L': /* listing options */
1099 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001100 while (*param)
1101 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001102 }
1103 break;
1104
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001105 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001106 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001107 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001108 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001109
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001110 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001111 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001112 using_debug_info = true;
1113 debug_format = param;
1114 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001115 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001116
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001117 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001119 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1120 errfmt = &errfmt_msvc;
1121 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1122 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001123 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001124 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001129 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001130 using_debug_info = true;
1131 if (p[2])
1132 debug_format = nasm_skip_spaces(p + 2);
1133 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001137 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001138 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001142 /* legacy option */
1143 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 exit(0);
1145 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001148 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001149 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001150 nasm_ctype_tasm_mode();
1151 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001155 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001157
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001158 case 'e': /* preprocess only */
1159 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001160 if (pass == 1)
1161 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001163
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001164 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001165 if (pass == 1)
1166 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001168
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001169 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001170 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001171 if (pass == 2)
1172 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001173 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001174
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001176 if (pass == 1) {
1177 switch (p[2]) {
1178 case 'W':
1179 quote_for_make = quote_for_wmake;
1180 break;
1181 case 'D':
1182 case 'F':
1183 case 'T':
1184 case 'Q':
1185 advance = true;
1186 break;
1187 default:
1188 break;
1189 }
1190 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001191 switch (p[2]) {
1192 case 0:
1193 operating_mode = OP_DEPEND;
1194 break;
1195 case 'G':
1196 operating_mode = OP_DEPEND;
1197 depend_missing_ok = true;
1198 break;
1199 case 'P':
1200 depend_emit_phony = true;
1201 break;
1202 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001203 operating_mode |= OP_DEPEND;
1204 if (q && (q[0] != '-' || q[1] == '\0')) {
1205 depend_file = q;
1206 advance = true;
1207 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001208 break;
1209 case 'F':
1210 depend_file = q;
1211 advance = true;
1212 break;
1213 case 'T':
1214 depend_target = q;
1215 advance = true;
1216 break;
1217 case 'Q':
1218 depend_target = quote_for_make(q);
1219 advance = true;
1220 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001221 case 'W':
1222 /* handled in pass 1 */
1223 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001224 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001225 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001226 break;
1227 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001228 }
1229 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001230 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001231 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001232 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 case '-':
1236 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001237 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001238 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001239 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001240 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001241
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001242 p += 2;
1243
1244 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001245 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 break;
1247 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001248
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001249 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001250 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001251 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001252 olen = strlen(tx->label);
1253
1254 if (olen > plen)
1255 continue;
1256
1257 if (nasm_memicmp(p, tx->label, olen))
1258 continue;
1259
1260 if (tx->label[olen-1] == '-')
1261 break; /* Incomplete option */
1262
1263 if (!p[olen] || p[olen] == '=')
1264 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001266
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001267 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001268 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001269 }
1270
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001271 opt = tx->opt;
1272
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001273 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001274 if (param)
1275 *param++ = '\0';
1276
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001277 switch (tx->need_arg) {
1278 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001279 if (!param) {
1280 param = q;
1281 advance = true;
1282 }
1283
1284 /* Note: a null string is a valid parameter */
1285 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001286 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001287 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001289 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001290
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001291 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001292 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001293 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001294 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001295 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001296 break;
1297
1298 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1299 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001300 }
1301
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001302 switch (opt) {
1303 case OPT_BOGUS:
1304 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001305 case OPT_VERSION:
1306 show_version();
1307 break;
1308 case OPT_ABORT_ON_PANIC:
1309 abort_on_panic = true;
1310 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001311 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001312 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001313 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001314 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001315 case OPT_INCLUDE:
1316 if (pass == 2)
1317 preproc->pre_include(q);
1318 break;
1319 case OPT_PRAGMA:
1320 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001321 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001322 break;
1323 case OPT_BEFORE:
1324 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001325 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001326 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001327 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001328 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001329 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001330 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001331 case OPT_KEEP_ALL:
1332 keep_all = true;
1333 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001334 case OPT_NO_LINE:
1335 pp_noline = true;
1336 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001337 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001338 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001339 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001340 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001341 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001342 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001343 default:
1344 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001346
1347 if (eqsave)
1348 *eqsave = '='; /* Restore = argument separator */
1349
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 break;
1351 }
1352
1353 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001354 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 break;
1356 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001357 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001358 /* In theory we could allow multiple input files... */
1359 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001360 }
1361
1362 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001363}
1364
H. Peter Anvineba20a72002-04-30 20:53:55 +00001365#define ARG_BUF_DELTA 128
1366
H. Peter Anvin55568c12016-10-03 19:46:49 -07001367static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001368{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001369 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001370 int bufsize, prevargsize;
1371
1372 bufsize = prevargsize = ARG_BUF_DELTA;
1373 buffer = nasm_malloc(ARG_BUF_DELTA);
1374 prevarg = nasm_malloc(ARG_BUF_DELTA);
1375 prevarg[0] = '\0';
1376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 p = buffer;
1379 while (1) { /* Loop to handle long lines */
1380 q = fgets(p, bufsize - (p - buffer), rfile);
1381 if (!q)
1382 break;
1383 p += strlen(p);
1384 if (p > buffer && p[-1] == '\n')
1385 break;
1386 if (p - buffer > bufsize - 10) {
1387 int offset;
1388 offset = p - buffer;
1389 bufsize += ARG_BUF_DELTA;
1390 buffer = nasm_realloc(buffer, bufsize);
1391 p = buffer + offset;
1392 }
1393 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001394
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (!q && p == buffer) {
1396 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001397 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 nasm_free(buffer);
1399 nasm_free(prevarg);
1400 return;
1401 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001402
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 /*
1404 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1405 * them are present at the end of the line.
1406 */
1407 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001409 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001412 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413
H. Peter Anvin55568c12016-10-03 19:46:49 -07001414 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001416
Charles Crayne192d5b52007-10-18 19:02:42 -07001417 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 prevargsize += ARG_BUF_DELTA;
1419 prevarg = nasm_realloc(prevarg, prevargsize);
1420 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001421 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422 }
1423}
1424
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001425/* Function to process args from a string of args, rather than the
1426 * argv array. Used by the environment variable and response file
1427 * processing.
1428 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001429static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001431 char *p, *q, *arg, *prevarg;
1432 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001433
1434 p = args;
1435 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001437 arg = NULL;
1438 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 q = p;
1440 while (*p && *p != separator)
1441 p++;
1442 while (*p == separator)
1443 *p++ = '\0';
1444 prevarg = arg;
1445 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001446 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001448 }
1449 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001450 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001451}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001452
H. Peter Anvin55568c12016-10-03 19:46:49 -07001453static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001454{
1455 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001456 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001457 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001458 perror(file);
1459 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001460 }
1461 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001462 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001463 }
1464 fclose(f);
1465}
1466
H. Peter Anvin55568c12016-10-03 19:46:49 -07001467static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001468{
1469 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001470 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001471
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001472 /*
1473 * Initialize all the warnings to their default state, including
1474 * warning index 0 used for "always on".
1475 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001476 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001477
1478 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001479 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001480 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001481 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001482 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001484 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001486 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001487
1488 /*
1489 * Now process the actual command line.
1490 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001492 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 argv++;
1494 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001495 /*
1496 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 * arguments like the environment variable. This allows us
1498 * to have multiple arguments on a single line, which is
1499 * different to the -@resp file processing below for regular
1500 * NASM.
1501 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001502 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 argc--;
1504 argv++;
1505 }
1506 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001507 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001508 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001509 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001510 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001511 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001513 } else {
1514 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1515 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
1517 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001518 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001519 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001520 }
1521
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001522 /*
1523 * Look for basic command line typos. This definitely doesn't
1524 * catch all errors, but it might help cases of fumbled fingers.
1525 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001526 if (pass != 2)
1527 return;
1528
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001529 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001530 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001531 else if ((errname && !strcmp(inname, errname)) ||
1532 (outname && !strcmp(inname, outname)) ||
1533 (listname && !strcmp(inname, listname)) ||
1534 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001535 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001536
1537 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001538 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001539 if (!error_file) {
1540 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001541 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001542 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001543 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001544}
1545
H. Peter Anvin29651542018-12-18 19:14:40 -08001546static void forward_refs(insn *instruction)
1547{
1548 int i;
1549 struct forwrefinfo *fwinf;
1550
1551 instruction->forw_ref = false;
1552
1553 if (!optimizing.level)
1554 return; /* For -O0 don't bother */
1555
1556 if (!forwref)
1557 return;
1558
1559 if (forwref->lineno != globallineno)
1560 return;
1561
1562 instruction->forw_ref = true;
1563 do {
1564 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1565 forwref = saa_rstruct(forwrefs);
1566 } while (forwref && forwref->lineno == globallineno);
1567
1568 if (!pass_first())
1569 return;
1570
1571 for (i = 0; i < instruction->operands; i++) {
1572 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1573 fwinf = saa_wstruct(forwrefs);
1574 fwinf->lineno = globallineno;
1575 fwinf->operand = i;
1576 }
1577 }
1578}
1579
1580static void process_insn(insn *instruction)
1581{
1582 int32_t n;
1583 int64_t l;
1584
1585 if (!instruction->times)
1586 return; /* Nothing to do... */
1587
1588 nasm_assert(instruction->times > 0);
1589
1590 /*
1591 * NOTE: insn_size() can change instruction->times
1592 * (usually to 1) when called.
1593 */
1594 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001595 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001596 for (n = 1; n <= instruction->times; n++) {
1597 l = insn_size(location.segment, location.offset,
1598 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001599 /* l == -1 -> invalid instruction */
1600 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001601 increment_offset(l);
1602 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001603 if (list_option('p')) {
1604 struct out_data dummy;
1605 memset(&dummy, 0, sizeof dummy);
1606 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1607 dummy.offset = start;
1608 dummy.size = location.offset - start;
1609 lfmt->output(&dummy);
1610 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001611 } else {
1612 l = assemble(location.segment, location.offset,
1613 globalbits, instruction);
1614 /* We can't get an invalid instruction here */
1615 increment_offset(l);
1616
1617 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001618 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001619 for (n = 2; n <= instruction->times; n++) {
1620 l = assemble(location.segment, location.offset,
1621 globalbits, instruction);
1622 increment_offset(l);
1623 }
1624 lfmt->downlevel(LIST_TIMES);
1625 }
1626 }
1627}
1628
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001629static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001630{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001631 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001633 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001634 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001635
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001636 switch (cmd_sb) {
1637 case 16:
1638 break;
1639 case 32:
1640 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001641 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001642 break;
1643 case 64:
1644 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001645 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001646 break;
1647 default:
1648 panic();
1649 break;
1650 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001652 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001653
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001654 if (listname && !keep_all) {
1655 /* Remove the list file in case we die before the output pass */
1656 remove(listname);
1657 }
1658
1659 while (!terminate_after_phase && !pass_final()) {
1660 _passn++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001661 switch (pass_type()) {
1662 case PASS_INIT:
1663 _pass_type = PASS_FIRST;
1664 break;
1665 case PASS_OPT:
1666 if (global_offset_changed)
1667 break; /* One more optimization pass */
1668 /* fall through */
1669 default:
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001670 _pass_type++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001671 break;
1672 }
1673
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001674 global_offset_changed = 0;
1675
1676 /*
1677 * Create a warning buffer list unless we are in
1678 * pass 2 (everything will be emitted immediately in pass 2.)
1679 */
1680 if (warn_list) {
1681 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001682 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001683 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001684
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001685 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001686 warn_list = strlist_alloc(false);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001687
H. Peter Anvincac0b192017-03-28 16:12:30 -07001688 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001690 if (listname) {
1691 if (pass_final() || list_on_every_pass()) {
1692 active_list_options = list_options;
1693 lfmt->init(listname);
1694 } else if (active_list_options) {
1695 /*
1696 * Looks like we used the list engine on a previous pass,
1697 * but now it is turned off, presumably via %pragma -p
1698 */
1699 lfmt->cleanup();
1700 if (!keep_all)
1701 remove(listname);
1702 active_list_options = 0;
1703 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001705
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001706 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001707 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 saa_rewind(forwrefs);
1709 forwref = saa_rstruct(forwrefs);
1710 raa_free(offsets);
1711 offsets = raa_init();
1712 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001713 location.segment = NO_SEG;
1714 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001715 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001716 location.known = true;
1717 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001718 switch_segment(ofmt->section(NULL, &globalbits));
1719 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001720
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001722
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001724 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001725 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001726 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001727
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001728 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001729 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001730 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001731 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001732 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001733 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001734
H. Peter Anvinc7131682017-03-07 17:45:01 -08001735 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001736 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001737 forward_refs(&output_ins);
1738 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001739 cleanup_insn(&output_ins);
1740
1741 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001744
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001745 preproc->cleanup_pass();
1746
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001747 /* We better not be having an error hold still... */
1748 nasm_assert(!errhold_stack);
1749
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001750 if (global_offset_changed) {
1751 switch (pass_type()) {
1752 case PASS_OPT:
1753 /*
1754 * This is the only pass type that can be executed more
1755 * than once, and therefore has the ability to stall.
1756 */
1757 if (global_offset_changed < prev_offset_changed) {
1758 prev_offset_changed = global_offset_changed;
1759 stall_count = 0;
1760 } else {
1761 stall_count++;
1762 }
1763
1764 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1765 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1766 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001767 nasm_nonfatalf(ERR_UNDEAD,
1768 "unable to find valid values for all labels "
1769 "after %"PRId64" passes; "
1770 "stalled for %"PRId64", giving up.",
1771 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001772 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001773 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001774 }
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001775 break;
1776
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001777 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001778 /*!
1779 *!phase [off] phase error during stabilization
1780 *! warns about symbols having changed values during
1781 *! the second-to-last assembly pass. This is not
1782 *! inherently fatal, but may be a source of bugs.
1783 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001784 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1785 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001786 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001787 break;
1788
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001789 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001790 nasm_nonfatalf(ERR_UNDEAD,
1791 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001792 break;
1793
1794 default:
1795 /* This is normal, we'll keep going... */
1796 break;
1797 }
1798 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001799
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001800 reset_warnings();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001801 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001802
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001803 if (opt_verbose_info && pass_final()) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001804 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001805 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001806 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807
H. Peter Anvin734b1882002-04-30 21:01:08 +00001808 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001809 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001810}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001811
Ed Berosetfa771012002-06-09 20:56:40 +00001812/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001813 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001814 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001815static size_t warn_index(errflags severity)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001817 size_t index;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001818
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001819 if ((severity & ERR_MASK) >= ERR_FATAL)
1820 return 0; /* Fatal errors are never suppressible */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001821
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001822 /* Warnings MUST HAVE a warning category specifier! */
1823 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001824
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001825 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001826 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001827
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001828 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001829}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001831static bool skip_this_pass(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001832{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001833 errflags type = severity & ERR_MASK;
Ed Berosetfa771012002-06-09 20:56:40 +00001834
H. Peter Anvin8f622462017-04-02 19:02:29 -07001835 /*
1836 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001837 * We can never skip fatal errors as by definition they cannot be
1838 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001839 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001840 if (type >= ERR_FATAL)
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001841 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001842
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001843 /*
1844 * ERR_LISTMSG messages are always skipped; the list file
1845 * receives them anyway as this function is not consulted
1846 * for sending to the list file.
1847 */
1848 if (type == ERR_LISTMSG)
1849 return true;
1850
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001851 /*
1852 * This message not applicable unless it is the last pass we are going
1853 * to execute; this can be either the final code-generation pass or
1854 * the single pass executed in preproc-only mode.
1855 */
1856 return (severity & ERR_PASS2) && !pass_final_or_preproc();
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001857}
1858
1859/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001860 * check for suppressed message (usually warnings or notes)
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001861 *
1862 * @param severity the severity of the warning or error
1863 * @return true if we should abort error/warning printing
1864 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001865static bool is_suppressed(errflags severity)
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001866{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001867 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001868 if ((severity & ERR_MASK) >= ERR_FATAL)
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001869 return false;
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001870
1871 /* This error/warning is pointless if we are dead anyway */
1872 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1873 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001874
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001875 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1876 return true;
1877
1878 if (preproc && !(severity & ERR_PP_LISTMACRO))
1879 return preproc->suppress_error(severity);
1880
1881 return false;
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001882}
Cyrill Gorcunov0e754dc2011-12-05 01:01:06 +04001883
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001884/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001885 * Return the true error type (the ERR_MASK part) of the given
1886 * severity, accounting for warnings that may need to be promoted to
1887 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001888 *
1889 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001890 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001891 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001892static errflags true_error_type(errflags severity)
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001893{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001894 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1895 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001896
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001897 type = severity & ERR_MASK;
1898
1899 /* Promote warning to error? */
1900 if (type == ERR_WARNING) {
1901 uint8_t state = warning_state[warn_index(severity)];
1902 if ((state & warn_is_err) == warn_is_err)
1903 type = ERR_NONFATAL;
1904 }
1905
1906 return type;
Ed Berosetfa771012002-06-09 20:56:40 +00001907}
1908
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001909/*
1910 * The various error type prefixes
1911 */
1912static const char * const error_pfx_table[ERR_MASK+1] = {
1913 ";;; ", "debug: ", "info: ", "warning: ",
1914 "error: ", "fatal: ", "critical: ", "panic: "
1915};
1916static const char no_file_name[] = "nasm"; /* What to print if no file name */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001917
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001918/*
1919 * For fatal/critical/panic errors, kill this process.
1920 */
1921static fatal_func die_hard(errflags true_type, errflags severity)
1922{
1923 fflush(NULL);
1924
1925 if (true_type == ERR_PANIC && abort_on_panic)
1926 abort();
1927
1928 if (ofile) {
1929 fclose(ofile);
1930 if (!keep_all)
1931 remove(outname);
1932 ofile = NULL;
1933 }
1934
1935 if (severity & ERR_USAGE)
1936 usage();
1937
1938 /* Terminate immediately */
1939 exit(true_type - ERR_FATAL + 1);
1940}
1941
1942/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001943 * Returns the struct src_location appropriate for use, after some
1944 * potential filename mangling.
1945 */
1946static struct src_location error_where(errflags severity)
1947{
1948 struct src_location where;
1949
1950 if (severity & ERR_NOFILE) {
1951 where.filename = NULL;
1952 where.lineno = 0;
1953 } else {
1954 where = src_where_error();
1955
1956 if (!where.filename) {
1957 where.filename =
1958 inname && inname[0] ? inname :
1959 outname && outname[0] ? outname :
1960 NULL;
1961 where.lineno = 0;
1962 }
1963 }
1964
1965 return where;
1966}
1967
1968/*
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001969 * error reporting for critical and panic errors: minimize
1970 * the amount of system dependencies for getting a message out,
1971 * and in particular try to avoid memory allocations.
1972 */
1973fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1974{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001975 struct src_location where;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001976 errflags true_type = severity & ERR_MASK;
1977 static bool been_here = false;
1978
1979 if (unlikely(been_here))
1980 abort(); /* Recursive error... just die */
1981
1982 been_here = true;
1983
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001984 where = error_where(severity);
1985 if (!where.filename)
1986 where.filename = no_file_name;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001987
1988 fputs(error_pfx_table[severity], error_file);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001989 fputs(where.filename, error_file);
1990 if (where.lineno) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001991 fprintf(error_file, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001992 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001993 }
1994 fputs(errfmt->beforemsg, error_file);
1995 vfprintf(error_file, fmt, args);
1996 fputc('\n', error_file);
1997
1998 die_hard(true_type, severity);
1999}
2000
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08002001/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002002 * Stack of tentative error hold lists.
2003 */
2004struct nasm_errtext {
2005 struct nasm_errtext *next;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002006 char *msg; /* Owned by this structure */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002007 struct src_location where; /* Owned by the srcfile system */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002008 errflags severity;
2009 errflags true_type;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002010};
2011struct nasm_errhold {
2012 struct nasm_errhold *up;
2013 struct nasm_errtext *head, **tail;
2014};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002015
2016static void nasm_free_error(struct nasm_errtext *et)
2017{
2018 nasm_free(et->msg);
2019 nasm_free(et);
2020}
2021
2022static void nasm_issue_error(struct nasm_errtext *et);
2023
2024struct nasm_errhold *nasm_error_hold_push(void)
2025{
2026 struct nasm_errhold *eh;
2027
2028 nasm_new(eh);
2029 eh->up = errhold_stack;
2030 eh->tail = &eh->head;
2031 errhold_stack = eh;
2032
2033 return eh;
2034}
2035
2036void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
2037{
2038 struct nasm_errtext *et, *etmp;
2039
2040 /* Allow calling with a null argument saying no hold in the first place */
2041 if (!eh)
2042 return;
2043
2044 /* This *must* be the current top of the errhold stack */
2045 nasm_assert(eh == errhold_stack);
2046
2047 if (eh->head) {
2048 if (issue) {
2049 if (eh->up) {
2050 /* Commit the current hold list to the previous level */
2051 *eh->up->tail = eh->head;
2052 eh->up->tail = eh->tail;
2053 } else {
2054 /* Issue errors */
2055 list_for_each_safe(et, etmp, eh->head)
2056 nasm_issue_error(et);
2057 }
2058 } else {
2059 /* Free the list, drop errors */
2060 list_for_each_safe(et, etmp, eh->head)
2061 nasm_free_error(et);
2062 }
2063 }
2064
2065 errhold_stack = eh->up;
2066 nasm_free(eh);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002067}
2068
Ed Berosetfa771012002-06-09 20:56:40 +00002069/**
2070 * common error reporting
2071 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002072 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002073 * specific error message to error_file and may or may not return. It
2074 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002075 *
2076 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002077 * @param fmt the printf style format string
2078 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07002079void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002080{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002081 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002082 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002083
2084 if (true_type >= ERR_CRITICAL)
2085 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002086
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08002087 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08002088 return;
2089
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002090 nasm_new(et);
2091 et->severity = severity;
2092 et->true_type = true_type;
2093 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002094 et->where = error_where(severity);
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002095
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002096 if (errhold_stack && true_type <= ERR_NONFATAL) {
2097 /* It is a tentative error */
2098 *errhold_stack->tail = et;
2099 errhold_stack->tail = &et->next;
2100 } else {
2101 nasm_issue_error(et);
2102 }
2103
2104 /*
2105 * Don't do this before then, if we do, we lose messages in the list
2106 * file, as the list file is only generated in the last pass.
2107 */
2108 if (skip_this_pass(severity))
2109 return;
2110
2111 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2112 if (preproc)
2113 preproc->error_list_macros(severity);
2114}
2115
2116/*
2117 * Actually print, list and take action on an error
2118 */
2119static void nasm_issue_error(struct nasm_errtext *et)
2120{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002121 const char *pfx;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002122 char warnsuf[64]; /* Warning suffix */
2123 char linestr[64]; /* Formatted line number if applicable */
2124 const errflags severity = et->severity;
2125 const errflags true_type = et->true_type;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002126 const struct src_location where = et->where;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002127
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002128 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002129 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002130 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002131 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002132
H. Peter Anvinddb29062018-12-11 00:06:29 -08002133 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002134 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2135 /*
2136 * It's a warning without ERR_HERE defined, and we are not already
2137 * unwinding the macros that led us here.
2138 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002139 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002140 (true_type >= ERR_NONFATAL) ? "error=" : "",
2141 warning_name[warn_index(severity)]);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002142 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002143
H. Peter Anvinddb29062018-12-11 00:06:29 -08002144 *linestr = 0;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002145 if (where.lineno) {
H. Peter Anvinddb29062018-12-11 00:06:29 -08002146 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002147 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002148 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002149
H. Peter Anvin77016c82018-12-10 22:29:49 -08002150 if (!skip_this_pass(severity)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002151 const char *file = where.filename ? where.filename : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002152 const char *here = "";
2153
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002154 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002155 here = where.filename ? " here" : " in an unknown location";
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002156 }
2157
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002158 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002159 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002160 /*
2161 * Buffer up warnings until we either get an error
2162 * or we are on the code-generation pass.
2163 */
2164 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2165 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002166 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002167 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002168 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002169 * Actually output an error. If we have buffered
2170 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002171 */
2172 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002173 strlist_write(warn_list, "\n", error_file);
2174 strlist_free(&warn_list);
2175 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002176
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002177 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2178 file, linestr, errfmt->beforemsg,
2179 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002180 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002181 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002182
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002183 /* Are we recursing from error_list_macros? */
2184 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002185 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002186
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002187 /*
2188 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002189 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002190 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002191 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002192 if (where.lineno)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002193 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002194 pfx, et->msg, where.filename, where.lineno, warnsuf);
2195 else if (where.filename)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002196 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002197 pfx, et->msg, where.filename, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002198 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002199 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002200 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002201 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002202 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002203 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204
H. Peter Anvin8f622462017-04-02 19:02:29 -07002205 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002206 goto done;
H. Peter Anvin8f622462017-04-02 19:02:29 -07002207
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002208 if (true_type >= ERR_FATAL)
2209 die_hard(true_type, severity);
2210 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002211 terminate_after_phase = true;
H. Peter Anvin3366e312018-02-07 14:14:36 -08002212
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002213done:
2214 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002215}
2216
H. Peter Anvin734b1882002-04-30 21:01:08 +00002217static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002218{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002219 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002220}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002221
H. Peter Anvin322bee02019-08-10 01:38:06 -07002222static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002223{
2224 int i;
2225
H. Peter Anvin322bee02019-08-10 01:38:06 -07002226 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002227 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2228 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002229 _progname, _progname);
2230 fputs(
2231 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002232 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002233 "\n"
2234 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002235 " -v (or --v) print the NASM version number and exit\n"
2236 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002237 "\n"
2238 " -o outfile write output to outfile\n"
2239 " --keep-all output files will not be removed even if an error happens\n"
2240 "\n"
2241 " -Xformat specifiy error reporting format (gnu or vc)\n"
2242 " -s redirect error messages to stdout\n"
2243 " -Zfile redirect error messages to file\n"
2244 "\n"
2245 " -M generate Makefile dependencies on stdout\n"
2246 " -MG d:o, missing files assumed generated\n"
2247 " -MF file set Makefile dependency file\n"
2248 " -MD file assemble and generate dependencies\n"
2249 " -MT file dependency target name\n"
2250 " -MQ file dependency target name (quoted)\n"
2251 " -MP emit phony targets\n"
2252 "\n"
2253 " -f format select output file format\n"
2254 , out);
2255 ofmt_list(ofmt, out);
2256 fputs(
2257 "\n"
2258 " -g generate debugging information\n"
2259 " -F format select a debugging format (output format dependent)\n"
2260 " -gformat same as -g -F format\n"
2261 , out);
2262 dfmt_list(out);
2263 fputs(
2264 "\n"
2265 " -l listfile write listing to a list file\n"
2266 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002267 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002268 " -Ld show byte and repeat counts in decimal, not hex\n"
2269 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002270 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002271 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002272 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002273 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002274 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002275 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002276 "\n"
2277 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2278 " -O0 no optimization\n"
2279 " -O1 minimal optimization\n"
2280 " -Ox multipass optimization (default)\n"
2281 " -Ov display the number of passes executed at the end\n"
2282 " -t assemble in limited SciTech TASM compatible mode\n"
2283 "\n"
2284 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2285 " -a don't preprocess (assemble only)\n"
2286 " -Ipath add a pathname to the include file path\n"
2287 " -Pfile pre-include a file (also --include)\n"
2288 " -Dmacro[=str] pre-define a macro\n"
2289 " -Umacro undefine a macro\n"
2290 " --pragma str pre-executes a specific %%pragma\n"
2291 " --before str add line (usually a preprocessor statement) before the input\n"
2292 " --no-line ignore %line directives in input\n"
2293 "\n"
2294 " --prefix str prepend the given string to the names of all extern,\n"
2295 " common and global symbols (also --gprefix)\n"
2296 " --suffix str append the given string to the names of all extern,\n"
2297 " common and global symbols (also --gprefix)\n"
2298 " --lprefix str prepend the given string to local symbols\n"
2299 " --lpostfix str append the given string to local symbols\n"
2300 "\n"
2301 " -w+x enable warning x (also -Wx)\n"
2302 " -w-x disable warning x (also -Wno-x)\n"
2303 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2304 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2305 , out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002306
H. Peter Anvin322bee02019-08-10 01:38:06 -07002307 fprintf(out, " %-20s %s\n",
2308 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2309
2310 for (i = 1; i < WARN_IDX_ALL; i++) {
2311 const char *me = warning_name[i];
2312 const char *prev = warning_name[i-1];
2313 const char *next = warning_name[i+1];
2314
2315 if (prev) {
2316 int prev_len = strlen(prev);
2317 const char *dash = me;
2318
2319 while ((dash = strchr(dash+1, '-'))) {
2320 int prefix_len = dash - me; /* Not including final dash */
2321 if (strncmp(next, me, prefix_len+1)) {
2322 /* Only one or last option with this prefix */
2323 break;
2324 }
2325 if (prefix_len >= prev_len ||
2326 strncmp(prev, me, prefix_len) ||
2327 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2328 /* This prefix is different from the previous option */
2329 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2330 prefix_len, me, prefix_len+1, me);
2331 }
2332 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002333 }
H. Peter Anvin322bee02019-08-10 01:38:06 -07002334
2335 fprintf(out, " %-20s %s%s\n",
2336 warning_name[i], warning_help[i],
2337 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2338 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002339 }
2340
H. Peter Anvin322bee02019-08-10 01:38:06 -07002341 fputs(
2342 "\n"
2343 " --limit-X val set execution limit X\n"
2344 , out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002345
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002346
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002347 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002348 fprintf(out, " %-20s %s [",
2349 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002350 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002351 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002352 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002353 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002354 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002355 }
2356}