blob: 82789f8c9d89415793853e5281153b52eef25a45 [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
Byoungchan Lee3dc3d472021-07-27 05:09:09 +0900171#if defined(OF_MACHO) || defined(OF_MACHO64)
172extern bool macho_set_min_os(const char *str);
173#endif
174
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700175/*
176 * Execution limits that can be set via a command-line option or %pragma
177 */
178
H. Peter Anvin322bee02019-08-10 01:38:06 -0700179/*
180 * This is really unlimited; it would take far longer than the
181 * current age of the universe for this limit to be reached even on
182 * much faster CPUs than currently exist.
183*/
184#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700185
H. Peter Anvin322bee02019-08-10 01:38:06 -0700186int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700187
188struct limit_info {
189 const char *name;
190 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700191 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700192};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700193/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700194static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700195 { "passes", "total number of passes", LIMIT_MAX_VAL },
196 { "stalled-passes", "number of passes without forward progress", 1000 },
197 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700198 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
199 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700200 { "rep", "%rep count", 1000000 },
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700201 { "eval", "expression evaluation descent", 8192 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700202 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700203};
204
H. Peter Anvin322bee02019-08-10 01:38:06 -0700205static void set_default_limits(void)
206{
207 int i;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700208 size_t rl;
209 int64_t new_limit;
210
H. Peter Anvin322bee02019-08-10 01:38:06 -0700211 for (i = 0; i <= LIMIT_MAX; i++)
212 nasm_limit[i] = limit_info[i].default_val;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700213
214 /*
215 * Try to set a sensible default value for the eval depth based
216 * on the limit of the stack size, if knowable...
217 */
218 rl = nasm_get_stack_size_limit();
219 new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
220 if (new_limit < nasm_limit[LIMIT_EVAL])
221 nasm_limit[LIMIT_EVAL] = new_limit;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700222}
223
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700224enum directive_result
225nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700226{
227 int i;
228 int64_t val;
229 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700230 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700231
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700232 if (!limit)
233 limit = "";
234 if (!valstr)
235 valstr = "";
236
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700237 for (i = 0; i <= LIMIT_MAX; i++) {
238 if (!nasm_stricmp(limit, limit_info[i].name))
239 break;
240 }
241 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800242 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800243 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700244 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700245 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700246 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700247 return DIRR_ERROR;
248 }
249
250 if (!nasm_stricmp(valstr, "unlimited")) {
251 val = LIMIT_MAX_VAL;
252 } else {
253 val = readnum(valstr, &rn_error);
254 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800255 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800256 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700257 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700258 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700259 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700260 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700261 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700262 if (val > LIMIT_MAX_VAL)
263 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700264 }
265
266 nasm_limit[i] = val;
267 return DIRR_OK;
268}
269
H. Peter Anvin892c4812018-05-30 14:43:46 -0700270int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400271{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700272 location.segment = segment;
273 if (segment == NO_SEG) {
274 location.offset = absolute.offset;
275 in_absolute = true;
276 } else {
277 location.offset = raa_read(offsets, segment);
278 in_absolute = false;
279 }
280 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400281}
282
283static void set_curr_offs(int64_t l_off)
284{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800285 if (in_absolute)
286 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400287 else
288 offsets = raa_write(offsets, location.segment, l_off);
289}
290
H. Peter Anvin892c4812018-05-30 14:43:46 -0700291static void increment_offset(int64_t delta)
292{
293 if (unlikely(delta == 0))
294 return;
295
296 location.offset += delta;
297 set_curr_offs(location.offset);
298}
299
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800300/*
301 * Define system-defined macros that are not part of
302 * macros/standard.mac.
303 */
304static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800305{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700306 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800307 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800308
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700309 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700310 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &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, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700314 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400315 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700316 strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400317 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800318 }
319
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700320 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700321 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &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_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400324 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700325 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400326 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700327 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400328 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800329 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700330
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700331 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700332 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400333 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800334 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800335
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400336 /*
337 * In case if output format is defined by alias
338 * we have to put shortname of the alias itself here
339 * otherwise ABI backward compatibility gets broken.
340 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700341 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400342 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400343 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800344
345 /*
346 * Output-format specific macros.
347 */
348 if (ofmt->stdmac)
349 preproc->extra_stdmac(ofmt->stdmac);
350
351 /*
352 * Debug format, if any
353 */
354 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700355 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800356 preproc->pre_define(temp);
357 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800358}
359
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800360/*
361 * Initialize the preprocessor, set up the include path, and define
362 * the system-included macros. This is called between passes 1 and 2
363 * of parsing the command options; ofmt and dfmt are defined at this
364 * point.
365 *
366 * Command-line specified preprocessor directives (-p, -d, -u,
367 * --pragma, --before) are processed after this function.
368 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300369static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800370{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800371 preproc->init();
372 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300373 preproc->include_path(ipath);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000374}
375
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300376static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700377{
378 FILE *deps;
379 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700380 bool wmake = (quote_for_make == quote_for_wmake);
381 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800382 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700383
384 if (!list)
385 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700386
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700387 wrapstr = wmake ? " &\n " : " \\\n ";
388 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389
390 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700391 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400392 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800393 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400394 return;
395 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700396 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400397 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700398 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700399
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700400 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800401 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700402 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400403 len = strlen(file);
404 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700405 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400406 linepos = 1;
407 }
408 fprintf(deps, " %s", file);
409 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700410 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700411 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700412 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700413
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800414 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700415 if (depend_emit_phony) {
416 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700417 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700418 nasm_free(file);
419 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700420 }
421
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800422 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700423
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700424 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400425 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700426}
427
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700428/* Convert a struct tm to a POSIX-style time constant */
429static int64_t make_posix_time(const struct tm *tm)
430{
431 int64_t t;
432 int64_t y = tm->tm_year;
433
434 /* See IEEE 1003.1:2004, section 4.14 */
435
436 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
437 t += tm->tm_yday;
438 t *= 24;
439 t += tm->tm_hour;
440 t *= 60;
441 t += tm->tm_min;
442 t *= 60;
443 t += tm->tm_sec;
444
445 return t;
446}
447
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700448/*
449 * Quote a filename string if and only if it is necessary.
450 * It is considered necessary if any one of these is true:
451 * 1. The filename contains control characters;
452 * 2. The filename starts or ends with a space or quote mark;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700453 * 3. The filename contains more than one space in a row;
454 * 4. The filename is empty.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700455 *
456 * The filename is returned in a newly allocated buffer.
457 */
458static char *nasm_quote_filename(const char *fn)
459{
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700460 const unsigned char *p =
461 (const unsigned char *)fn;
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300462 size_t len;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700463
464 if (!p || !*p)
465 return nasm_strdup("\"\"");
466
467 if (*p <= ' ' || nasm_isquote(*p)) {
468 goto quote;
469 } else {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700470 unsigned char cutoff = ' ';
471
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700472 while (*p) {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700473 if (*p < cutoff)
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700474 goto quote;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700475 cutoff = ' ' + (*p == ' ');
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700476 p++;
477 }
478 if (p[-1] <= ' ' || nasm_isquote(p[-1]))
479 goto quote;
480 }
481
482 /* Quoting not necessary */
483 return nasm_strdup(fn);
484
485quote:
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300486 len = strlen(fn);
487 return nasm_quote(fn, &len);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700488}
489
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700490static void timestamp(void)
491{
492 struct compile_time * const oct = &official_compile_time;
Dale Curtis4ee6a692018-11-19 13:52:05 -0800493#if 1
494 // Chromium patch: Builds should be deterministic and not embed timestamps.
495 memset(oct, 0, sizeof(official_compile_time));
496#else
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700497 const struct tm *tp, *best_gm;
498
499 time(&oct->t);
500
501 best_gm = NULL;
502
503 tp = localtime(&oct->t);
504 if (tp) {
505 oct->local = *tp;
506 best_gm = &oct->local;
507 oct->have_local = true;
508 }
509
510 tp = gmtime(&oct->t);
511 if (tp) {
512 oct->gm = *tp;
513 best_gm = &oct->gm;
514 oct->have_gm = true;
515 if (!oct->have_local)
516 oct->local = oct->gm;
517 } else {
518 oct->gm = oct->local;
519 }
520
521 if (best_gm) {
522 oct->posix = make_posix_time(best_gm);
523 oct->have_posix = true;
524 }
Dale Curtis4ee6a692018-11-19 13:52:05 -0800525#endif
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700526}
527
H. Peter Anvin038d8612007-04-12 16:54:50 +0000528int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700530 /* Do these as early as possible */
531 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700532 _progname = argv[0];
533 if (!_progname || !_progname[0])
534 _progname = "nasm";
535
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700536 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800537
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800538 iflag_set_default_cpu(&cpu);
539 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400540
H. Peter Anvin322bee02019-08-10 01:38:06 -0700541 set_default_limits();
542
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800543 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300544
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800545 _pass_type = PASS_INIT;
546 _passn = 0;
547
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700548 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000549
H. Peter Anvin13506202018-11-28 14:55:58 -0800550 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700551 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700552
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700553 /*
554 * We must call init_labels() before the command line parsing,
555 * because we may be setting prefixes/suffixes from the command
556 * line.
557 */
558 init_labels();
559
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000560 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000561 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000563 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400564 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000565
H. Peter Anvin55568c12016-10-03 19:46:49 -0700566 parse_cmdline(argc, argv, 1);
567 if (terminate_after_phase) {
568 if (want_usage)
569 usage();
570 return 1;
571 }
572
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800573 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800574 if (!using_debug_info) {
575 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800576 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800577 } else if (!debug_format) {
578 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300579 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800580 } else {
581 dfmt = dfmt_find(ofmt, debug_format);
582 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800583 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800584 debug_format, ofmt->shortname);
585 }
586 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000587
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300588 preproc_init(include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000589
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590 parse_cmdline(argc, argv, 2);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000591 if (terminate_after_phase) {
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000592 if (want_usage)
593 usage();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595 }
596
597 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800598 init_warnings();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000599
H. Peter Anvin34754622018-11-28 12:36:53 -0800600 /* Dependency filename if we are also doing other things */
601 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
602 if (outname)
603 depend_file = nasm_strcat(outname, ".d");
604 else
605 depend_file = filename_set_extension(inname, ".d");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606 }
607
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300608 /*
609 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800610 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300611 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300612 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800613 if (!outname && !(operating_mode & OP_PREPROCESS)) {
614 outname = filename_set_extension(inname, ofmt->extension);
615 if (!strcmp(outname, inname)) {
616 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800617 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 -0800618 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300619 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800620
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800621 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700622
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700623 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400624 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700625
H. Peter Anvin34754622018-11-28 12:36:53 -0800626 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000627 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700628
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400629 if (depend_missing_ok)
630 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700631
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800632 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 while ((line = preproc->getline()))
635 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800636 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800637 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400638 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000639 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700640 const char *file_name = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700641 char *quoted_file_name = nasm_quote_filename(file_name);
642 int32_t linnum = 0;
643 int32_t lineinc = 0;
644 FILE *out;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000645
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800646 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700647 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800649 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700650 out = ofile;
651 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 ofile = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700653 out = stdout;
654 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000655
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700656 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000657
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -0700658 _pass_type = PASS_PREPROC;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800659 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700660
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 while ((line = preproc->getline())) {
662 /*
663 * We generate %line directives if needed for later programs
664 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700665 struct src_location where = src_where();
666 if (file_name != where.filename) {
667 file_name = where.filename;
668 linnum = -1; /* Force a new %line statement */
669 lineinc = file_name ? 1 : 0;
670 nasm_free(quoted_file_name);
671 quoted_file_name = nasm_quote_filename(file_name);
672 } else if (lineinc) {
673 if (linnum + lineinc == where.lineno) {
674 /* Add one blank line to account for increment */
675 fputc('\n', out);
676 linnum += lineinc;
677 } else if (linnum - lineinc == where.lineno) {
678 /*
679 * Standing still, probably a macro. Set increment
680 * to zero.
681 */
682 lineinc = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700684 } else {
685 /* lineinc == 0 */
686 if (linnum + 1 == where.lineno)
687 lineinc = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700689
690 /* Skip blank lines if we will need a %line anyway */
691 if (linnum == -1 && !line[0])
692 continue;
693
694 if (linnum != where.lineno) {
695 fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
696 where.lineno, lineinc, quoted_file_name);
697 }
698 linnum = where.lineno + lineinc;
699
700 fputs(line, out);
701 fputc('\n', out);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700703
704 nasm_free(quoted_file_name);
705
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800706 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800707 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 if (ofile)
709 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700710 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400712 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400713 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400715 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700716 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400717 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800718 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400720 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400721 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700723 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200724
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400725 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800726 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400727 cleanup_labels();
728 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800729 if (ferror(ofile))
730 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400731 }
732
733 if (ofile) {
734 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700735 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400736 remove(outname);
737 ofile = NULL;
738 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000740
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800741 preproc->cleanup_session();
742
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700743 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400744 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700745
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000748
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 raa_free(offsets);
750 saa_free(forwrefs);
751 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000752 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700753 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800754 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000755
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700756 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000757}
758
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759/*
760 * Get a parameter for a command line option.
761 * First arg must be in the form of e.g. -f...
762 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800763static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000764{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800765 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400766 if (p[2]) /* the parameter's in the option */
767 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800769 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000771 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800772 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000773 return NULL;
774}
775
H. Peter Anvindc242712007-11-18 11:55:10 -0800776/*
777 * Copy a filename
778 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800779static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800780{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800781 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700782 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800783
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800784 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800785}
786
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700787/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700788 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700789 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700790static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700791{
792 const char *p;
793 char *os, *q;
794
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400795 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700796 size_t nbs = 0;
797
798 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400799 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700800
801 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400802 switch (*p) {
803 case ' ':
804 case '\t':
805 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
806 n += nbs + 2;
807 nbs = 0;
808 break;
809 case '$':
810 case '#':
811 nbs = 0;
812 n += 2;
813 break;
814 case '\\':
815 nbs++;
816 n++;
817 break;
818 default:
819 nbs = 0;
820 n++;
821 break;
822 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700823 }
824
825 /* Convert N backslashes at the end of filename to 2N backslashes */
826 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400827 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700828
829 os = q = nasm_malloc(n);
830
831 nbs = 0;
832 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400833 switch (*p) {
834 case ' ':
835 case '\t':
836 while (nbs--)
837 *q++ = '\\';
838 *q++ = '\\';
839 *q++ = *p;
840 break;
841 case '$':
842 *q++ = *p;
843 *q++ = *p;
844 nbs = 0;
845 break;
846 case '#':
847 *q++ = '\\';
848 *q++ = *p;
849 nbs = 0;
850 break;
851 case '\\':
852 *q++ = *p;
853 nbs++;
854 break;
855 default:
856 *q++ = *p;
857 nbs = 0;
858 break;
859 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700860 }
861 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400862 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700863
864 *q = '\0';
865
866 return os;
867}
868
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700869/*
870 * Convert a string to a Watcom make-safe form
871 */
872static char *quote_for_wmake(const char *str)
873{
874 const char *p;
875 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700876 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700877
878 size_t n = 1; /* Terminating zero */
879
880 if (!str)
881 return NULL;
882
883 for (p = str; *p; p++) {
884 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700885 case ' ':
886 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700887 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700888 quote = true;
889 n++;
890 break;
891 case '\"':
892 quote = true;
893 n += 2;
894 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700895 case '$':
896 case '#':
897 n += 2;
898 break;
899 default:
900 n++;
901 break;
902 }
903 }
904
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700905 if (quote)
906 n += 2;
907
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700908 os = q = nasm_malloc(n);
909
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700910 if (quote)
911 *q++ = '\"';
912
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700913 for (p = str; *p; p++) {
914 switch (*p) {
915 case '$':
916 case '#':
917 *q++ = '$';
918 *q++ = *p;
919 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700920 case '\"':
921 *q++ = *p;
922 *q++ = *p;
923 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700924 default:
925 *q++ = *p;
926 break;
927 }
928 }
929
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700930 if (quote)
931 *q++ = '\"';
932
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700933 *q = '\0';
934
935 return os;
936}
937
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100938enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800939 OPT_BOGUS,
940 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700941 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800942 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700943 OPT_MANGLE,
944 OPT_INCLUDE,
945 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700946 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700947 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800948 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700949 OPT_NO_LINE,
Byoungchan Lee3dc3d472021-07-27 05:09:09 +0900950 OPT_DEBUG,
951 OPT_MACHO_MIN_OS
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100952};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700953enum need_arg {
954 ARG_NO,
955 ARG_YES,
956 ARG_MAYBE
957};
958
H. Peter Anvin3366e312018-02-07 14:14:36 -0800959struct textargs {
960 const char *label;
961 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700962 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700963 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800964};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800965static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700966 {"v", OPT_VERSION, ARG_NO, 0},
967 {"version", OPT_VERSION, ARG_NO, 0},
968 {"help", OPT_HELP, ARG_NO, 0},
969 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
970 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
971 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
972 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
973 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
974 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
975 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
976 {"include", OPT_INCLUDE, ARG_YES, 0},
977 {"pragma", OPT_PRAGMA, ARG_YES, 0},
978 {"before", OPT_BEFORE, ARG_YES, 0},
979 {"limit-", OPT_LIMIT, ARG_YES, 0},
980 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
981 {"no-line", OPT_NO_LINE, ARG_NO, 0},
982 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
Byoungchan Lee3dc3d472021-07-27 05:09:09 +0900983 {"macho-min-os", OPT_MACHO_MIN_OS, ARG_YES, 0},
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700984 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000985};
986
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400987static void show_version(void)
988{
Dale Curtis10760492018-10-31 13:03:37 -0700989 printf("NASM version %s%s\n",
990 nasm_version, nasm_compile_options);
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400991 exit(0);
992}
993
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700995static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000996{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000997 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000999
1000 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -08001001 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001004 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 /* These parameters take values */
1006 if (!(param = get_param(p, q, &advance)))
1007 return advance;
1008 }
H. Peter Anvin423e3812007-11-15 17:12:29 -08001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 switch (p[1]) {
1011 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001012 if (pass == 1)
1013 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001015
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001016 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001017 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001018 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001019 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -07001020
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001021 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001022 if (pass == 1) {
1023 ofmt = ofmt_find(param, &ofmt_alias);
1024 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001025 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001027 }
1028 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001029
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001030 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001031 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001032 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001033
H. Peter Anvin55568c12016-10-03 19:46:49 -07001034 if (!*param) {
1035 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +03001036 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 } else {
1038 while (*param) {
1039 switch (*param) {
1040 case '0': case '1': case '2': case '3': case '4':
1041 case '5': case '6': case '7': case '8': case '9':
1042 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001043
Chang S. Baea5786342018-08-15 23:22:21 +03001044 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
1045 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001046 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +03001047 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 else
Chang S. Baea5786342018-08-15 23:22:21 +03001049 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001051
H. Peter Anvin55568c12016-10-03 19:46:49 -07001052 case 'v':
1053 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001054 param++;
1055 opt_verbose_info = true;
1056 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001057
H. Peter Anvin55568c12016-10-03 19:46:49 -07001058 case 'x':
1059 param++;
Chang S. Baea5786342018-08-15 23:22:21 +03001060 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001061 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001062
H. Peter Anvin55568c12016-10-03 19:46:49 -07001063 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001064 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -07001065 *param);
1066 break;
1067 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001068 }
Chang S. Baea5786342018-08-15 23:22:21 +03001069 if (optimizing.level > MAX_OPTIMIZE)
1070 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001071 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001072 }
1073 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001074
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001075 case 'p': /* pre-include */
1076 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001077 if (pass == 2)
1078 preproc->pre_include(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 'd': /* pre-define */
1082 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001083 if (pass == 2)
1084 preproc->pre_define(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 'u': /* un-define */
1088 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001089 if (pass == 2)
1090 preproc->pre_undefine(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 'i': /* include search path */
1094 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001095 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001096 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001097 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001098
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001099 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001101 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001102 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001103
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001104 case 'L': /* listing options */
1105 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001106 while (*param)
1107 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001108 }
1109 break;
1110
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001111 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001112 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001113 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001114 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001115
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001116 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001117 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 using_debug_info = true;
1119 debug_format = param;
1120 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001121 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001122
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001123 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001124 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001125 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1126 errfmt = &errfmt_msvc;
1127 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1128 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001129 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001130 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001131 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001135 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001136 using_debug_info = true;
1137 if (p[2])
1138 debug_format = nasm_skip_spaces(p + 2);
1139 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001143 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001144 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001148 /* legacy option */
1149 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 exit(0);
1151 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001154 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001155 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001156 nasm_ctype_tasm_mode();
1157 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001161 show_version();
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 'e': /* preprocess only */
1165 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001166 if (pass == 1)
1167 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001169
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001170 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001171 if (pass == 1)
1172 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001174
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001175 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001176 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001177 if (pass == 2)
1178 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001179 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001180
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001182 if (pass == 1) {
1183 switch (p[2]) {
1184 case 'W':
1185 quote_for_make = quote_for_wmake;
1186 break;
1187 case 'D':
1188 case 'F':
1189 case 'T':
1190 case 'Q':
1191 advance = true;
1192 break;
1193 default:
1194 break;
1195 }
1196 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001197 switch (p[2]) {
1198 case 0:
1199 operating_mode = OP_DEPEND;
1200 break;
1201 case 'G':
1202 operating_mode = OP_DEPEND;
1203 depend_missing_ok = true;
1204 break;
1205 case 'P':
1206 depend_emit_phony = true;
1207 break;
1208 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001209 operating_mode |= OP_DEPEND;
1210 if (q && (q[0] != '-' || q[1] == '\0')) {
1211 depend_file = q;
1212 advance = true;
1213 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001214 break;
1215 case 'F':
1216 depend_file = q;
1217 advance = true;
1218 break;
1219 case 'T':
1220 depend_target = q;
1221 advance = true;
1222 break;
1223 case 'Q':
1224 depend_target = quote_for_make(q);
1225 advance = true;
1226 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001227 case 'W':
1228 /* handled in pass 1 */
1229 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001230 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001231 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001232 break;
1233 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001234 }
1235 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001236 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001237 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001238 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001240
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 case '-':
1242 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001243 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001244 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001245 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001246 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001247
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001248 p += 2;
1249
1250 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001251 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 break;
1253 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001254
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001255 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001256 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001257 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001258 olen = strlen(tx->label);
1259
1260 if (olen > plen)
1261 continue;
1262
1263 if (nasm_memicmp(p, tx->label, olen))
1264 continue;
1265
1266 if (tx->label[olen-1] == '-')
1267 break; /* Incomplete option */
1268
1269 if (!p[olen] || p[olen] == '=')
1270 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001272
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001273 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001274 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001275 }
1276
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001277 opt = tx->opt;
1278
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001279 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001280 if (param)
1281 *param++ = '\0';
1282
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001283 switch (tx->need_arg) {
1284 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001285 if (!param) {
1286 param = q;
1287 advance = true;
1288 }
1289
1290 /* Note: a null string is a valid parameter */
1291 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001292 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001293 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001295 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001296
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001297 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001298 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001299 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001300 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001301 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001302 break;
1303
1304 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1305 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001306 }
1307
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001308 switch (opt) {
1309 case OPT_BOGUS:
1310 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001311 case OPT_VERSION:
1312 show_version();
1313 break;
1314 case OPT_ABORT_ON_PANIC:
1315 abort_on_panic = true;
1316 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001317 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001318 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001319 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001320 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001321 case OPT_INCLUDE:
1322 if (pass == 2)
1323 preproc->pre_include(q);
1324 break;
1325 case OPT_PRAGMA:
1326 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001327 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001328 break;
1329 case OPT_BEFORE:
1330 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001331 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001332 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001333 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001334 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001335 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001336 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001337 case OPT_KEEP_ALL:
1338 keep_all = true;
1339 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001340 case OPT_NO_LINE:
1341 pp_noline = true;
1342 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001343 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001344 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001345 break;
Byoungchan Lee3dc3d472021-07-27 05:09:09 +09001346 case OPT_MACHO_MIN_OS:
1347 if (pass == 2) {
1348 if (strstr(ofmt->shortname, "macho") != ofmt->shortname) {
1349 nasm_error(
1350 ERR_WARNING | WARN_OTHER | ERR_USAGE,
1351 "macho-min-os is only valid for macho format, current: %s",
1352 ofmt->shortname);
1353 break;
1354 }
1355#if defined(OF_MACHO) || defined(OF_MACHO64)
1356 if (!macho_set_min_os(param)) {
1357 nasm_fatalf(ERR_USAGE, "failed to set minimum os for mach-o '%s'",
1358 param);
1359 }
1360#endif
1361 }
1362 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001363 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001364 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001365 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001366 default:
1367 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001369
1370 if (eqsave)
1371 *eqsave = '='; /* Restore = argument separator */
1372
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 break;
1374 }
1375
1376 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001377 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 break;
1379 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001380 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001381 /* In theory we could allow multiple input files... */
1382 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001383 }
1384
1385 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001386}
1387
H. Peter Anvineba20a72002-04-30 20:53:55 +00001388#define ARG_BUF_DELTA 128
1389
H. Peter Anvin55568c12016-10-03 19:46:49 -07001390static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001392 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001393 int bufsize, prevargsize;
1394
1395 bufsize = prevargsize = ARG_BUF_DELTA;
1396 buffer = nasm_malloc(ARG_BUF_DELTA);
1397 prevarg = nasm_malloc(ARG_BUF_DELTA);
1398 prevarg[0] = '\0';
1399
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 p = buffer;
1402 while (1) { /* Loop to handle long lines */
1403 q = fgets(p, bufsize - (p - buffer), rfile);
1404 if (!q)
1405 break;
1406 p += strlen(p);
1407 if (p > buffer && p[-1] == '\n')
1408 break;
1409 if (p - buffer > bufsize - 10) {
1410 int offset;
1411 offset = p - buffer;
1412 bufsize += ARG_BUF_DELTA;
1413 buffer = nasm_realloc(buffer, bufsize);
1414 p = buffer + offset;
1415 }
1416 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001417
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 if (!q && p == buffer) {
1419 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001420 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 nasm_free(buffer);
1422 nasm_free(prevarg);
1423 return;
1424 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 /*
1427 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1428 * them are present at the end of the line.
1429 */
1430 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001431
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001432 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001435 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001436
H. Peter Anvin55568c12016-10-03 19:46:49 -07001437 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001439
Charles Crayne192d5b52007-10-18 19:02:42 -07001440 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 prevargsize += ARG_BUF_DELTA;
1442 prevarg = nasm_realloc(prevarg, prevargsize);
1443 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001444 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001445 }
1446}
1447
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001448/* Function to process args from a string of args, rather than the
1449 * argv array. Used by the environment variable and response file
1450 * processing.
1451 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001452static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001454 char *p, *q, *arg, *prevarg;
1455 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001456
1457 p = args;
1458 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001460 arg = NULL;
1461 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 q = p;
1463 while (*p && *p != separator)
1464 p++;
1465 while (*p == separator)
1466 *p++ = '\0';
1467 prevarg = arg;
1468 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001469 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001471 }
1472 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001473 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001474}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001475
H. Peter Anvin55568c12016-10-03 19:46:49 -07001476static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001477{
1478 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001479 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001480 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001481 perror(file);
1482 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001483 }
1484 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001485 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001486 }
1487 fclose(f);
1488}
1489
H. Peter Anvin55568c12016-10-03 19:46:49 -07001490static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001491{
1492 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001493 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001494
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001495 /*
1496 * Initialize all the warnings to their default state, including
1497 * warning index 0 used for "always on".
1498 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001499 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001500
1501 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001502 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001503 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001504 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001507 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001509 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510
1511 /*
1512 * Now process the actual command line.
1513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001515 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 argv++;
1517 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001518 /*
1519 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 * arguments like the environment variable. This allows us
1521 * to have multiple arguments on a single line, which is
1522 * different to the -@resp file processing below for regular
1523 * NASM.
1524 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001525 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 argc--;
1527 argv++;
1528 }
1529 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001530 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001531 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001532 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001533 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001534 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001536 } else {
1537 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1538 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 }
1540 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001541 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001542 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001543 }
1544
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001545 /*
1546 * Look for basic command line typos. This definitely doesn't
1547 * catch all errors, but it might help cases of fumbled fingers.
1548 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001549 if (pass != 2)
1550 return;
1551
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001552 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001553 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001554 else if ((errname && !strcmp(inname, errname)) ||
1555 (outname && !strcmp(inname, outname)) ||
1556 (listname && !strcmp(inname, listname)) ||
1557 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001558 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001559
1560 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001561 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001562 if (!error_file) {
1563 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001564 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001565 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001566 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001567}
1568
H. Peter Anvin29651542018-12-18 19:14:40 -08001569static void forward_refs(insn *instruction)
1570{
1571 int i;
1572 struct forwrefinfo *fwinf;
1573
1574 instruction->forw_ref = false;
1575
1576 if (!optimizing.level)
1577 return; /* For -O0 don't bother */
1578
1579 if (!forwref)
1580 return;
1581
1582 if (forwref->lineno != globallineno)
1583 return;
1584
1585 instruction->forw_ref = true;
1586 do {
1587 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1588 forwref = saa_rstruct(forwrefs);
1589 } while (forwref && forwref->lineno == globallineno);
1590
1591 if (!pass_first())
1592 return;
1593
1594 for (i = 0; i < instruction->operands; i++) {
1595 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1596 fwinf = saa_wstruct(forwrefs);
1597 fwinf->lineno = globallineno;
1598 fwinf->operand = i;
1599 }
1600 }
1601}
1602
1603static void process_insn(insn *instruction)
1604{
1605 int32_t n;
1606 int64_t l;
1607
1608 if (!instruction->times)
1609 return; /* Nothing to do... */
1610
1611 nasm_assert(instruction->times > 0);
1612
1613 /*
1614 * NOTE: insn_size() can change instruction->times
1615 * (usually to 1) when called.
1616 */
1617 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001618 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001619 for (n = 1; n <= instruction->times; n++) {
1620 l = insn_size(location.segment, location.offset,
1621 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001622 /* l == -1 -> invalid instruction */
1623 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001624 increment_offset(l);
1625 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001626 if (list_option('p')) {
1627 struct out_data dummy;
1628 memset(&dummy, 0, sizeof dummy);
1629 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1630 dummy.offset = start;
1631 dummy.size = location.offset - start;
1632 lfmt->output(&dummy);
1633 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001634 } else {
1635 l = assemble(location.segment, location.offset,
1636 globalbits, instruction);
1637 /* We can't get an invalid instruction here */
1638 increment_offset(l);
1639
1640 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001641 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001642 for (n = 2; n <= instruction->times; n++) {
1643 l = assemble(location.segment, location.offset,
1644 globalbits, instruction);
1645 increment_offset(l);
1646 }
1647 lfmt->downlevel(LIST_TIMES);
1648 }
1649 }
1650}
1651
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001652static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001653{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001654 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001656 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001657 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001658
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001659 switch (cmd_sb) {
1660 case 16:
1661 break;
1662 case 32:
1663 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001664 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001665 break;
1666 case 64:
1667 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001668 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001669 break;
1670 default:
1671 panic();
1672 break;
1673 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001674
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001675 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001676
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001677 if (listname && !keep_all) {
1678 /* Remove the list file in case we die before the output pass */
1679 remove(listname);
1680 }
1681
1682 while (!terminate_after_phase && !pass_final()) {
1683 _passn++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001684 switch (pass_type()) {
1685 case PASS_INIT:
1686 _pass_type = PASS_FIRST;
1687 break;
1688 case PASS_OPT:
1689 if (global_offset_changed)
1690 break; /* One more optimization pass */
1691 /* fall through */
1692 default:
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001693 _pass_type++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001694 break;
1695 }
1696
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001697 global_offset_changed = 0;
1698
1699 /*
1700 * Create a warning buffer list unless we are in
1701 * pass 2 (everything will be emitted immediately in pass 2.)
1702 */
1703 if (warn_list) {
1704 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001705 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001706 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001707
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001708 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001709 warn_list = strlist_alloc(false);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001710
H. Peter Anvincac0b192017-03-28 16:12:30 -07001711 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001713 if (listname) {
1714 if (pass_final() || list_on_every_pass()) {
1715 active_list_options = list_options;
1716 lfmt->init(listname);
1717 } else if (active_list_options) {
1718 /*
1719 * Looks like we used the list engine on a previous pass,
1720 * but now it is turned off, presumably via %pragma -p
1721 */
1722 lfmt->cleanup();
1723 if (!keep_all)
1724 remove(listname);
1725 active_list_options = 0;
1726 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001728
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001729 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001730 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 saa_rewind(forwrefs);
1732 forwref = saa_rstruct(forwrefs);
1733 raa_free(offsets);
1734 offsets = raa_init();
1735 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001736 location.segment = NO_SEG;
1737 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001738 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001739 location.known = true;
1740 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001741 switch_segment(ofmt->section(NULL, &globalbits));
1742 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001747 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001748 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001749 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001751 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001752 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001753 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001754 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001755 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001756 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001757
H. Peter Anvinc7131682017-03-07 17:45:01 -08001758 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001759 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001760 forward_refs(&output_ins);
1761 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001762 cleanup_insn(&output_ins);
1763
1764 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001767
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001768 preproc->cleanup_pass();
1769
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001770 /* We better not be having an error hold still... */
1771 nasm_assert(!errhold_stack);
1772
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001773 if (global_offset_changed) {
1774 switch (pass_type()) {
1775 case PASS_OPT:
1776 /*
1777 * This is the only pass type that can be executed more
1778 * than once, and therefore has the ability to stall.
1779 */
1780 if (global_offset_changed < prev_offset_changed) {
1781 prev_offset_changed = global_offset_changed;
1782 stall_count = 0;
1783 } else {
1784 stall_count++;
1785 }
1786
1787 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1788 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1789 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001790 nasm_nonfatalf(ERR_UNDEAD,
1791 "unable to find valid values for all labels "
1792 "after %"PRId64" passes; "
1793 "stalled for %"PRId64", giving up.",
1794 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001795 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001796 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001797 }
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001798 break;
1799
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001800 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001801 /*!
1802 *!phase [off] phase error during stabilization
1803 *! warns about symbols having changed values during
1804 *! the second-to-last assembly pass. This is not
1805 *! inherently fatal, but may be a source of bugs.
1806 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001807 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1808 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001809 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001810 break;
1811
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001812 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001813 nasm_nonfatalf(ERR_UNDEAD,
1814 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001815 break;
1816
1817 default:
1818 /* This is normal, we'll keep going... */
1819 break;
1820 }
1821 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001822
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001823 reset_warnings();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001824 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001825
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001826 if (opt_verbose_info && pass_final()) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001827 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001828 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001829 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830
H. Peter Anvin734b1882002-04-30 21:01:08 +00001831 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001832 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001833}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001834
Ed Berosetfa771012002-06-09 20:56:40 +00001835/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001836 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001837 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001838static size_t warn_index(errflags severity)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001839{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001840 size_t index;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001841
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001842 if ((severity & ERR_MASK) >= ERR_FATAL)
1843 return 0; /* Fatal errors are never suppressible */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001844
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001845 /* Warnings MUST HAVE a warning category specifier! */
1846 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001847
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001848 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001849 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001850
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001851 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001852}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001853
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001854static bool skip_this_pass(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001855{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001856 errflags type = severity & ERR_MASK;
Ed Berosetfa771012002-06-09 20:56:40 +00001857
H. Peter Anvin8f622462017-04-02 19:02:29 -07001858 /*
1859 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001860 * We can never skip fatal errors as by definition they cannot be
1861 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001862 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001863 if (type >= ERR_FATAL)
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001864 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001865
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001866 /*
1867 * ERR_LISTMSG messages are always skipped; the list file
1868 * receives them anyway as this function is not consulted
1869 * for sending to the list file.
1870 */
1871 if (type == ERR_LISTMSG)
1872 return true;
1873
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001874 /*
1875 * This message not applicable unless it is the last pass we are going
1876 * to execute; this can be either the final code-generation pass or
1877 * the single pass executed in preproc-only mode.
1878 */
1879 return (severity & ERR_PASS2) && !pass_final_or_preproc();
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001880}
1881
1882/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001883 * check for suppressed message (usually warnings or notes)
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001884 *
1885 * @param severity the severity of the warning or error
1886 * @return true if we should abort error/warning printing
1887 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001888static bool is_suppressed(errflags severity)
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001889{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001890 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001891 if ((severity & ERR_MASK) >= ERR_FATAL)
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001892 return false;
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001893
1894 /* This error/warning is pointless if we are dead anyway */
1895 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1896 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001897
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001898 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1899 return true;
1900
1901 if (preproc && !(severity & ERR_PP_LISTMACRO))
1902 return preproc->suppress_error(severity);
1903
1904 return false;
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001905}
Cyrill Gorcunov0e754dc2011-12-05 01:01:06 +04001906
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001907/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001908 * Return the true error type (the ERR_MASK part) of the given
1909 * severity, accounting for warnings that may need to be promoted to
1910 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001911 *
1912 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001913 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001914 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001915static errflags true_error_type(errflags severity)
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001916{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001917 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1918 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001919
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001920 type = severity & ERR_MASK;
1921
1922 /* Promote warning to error? */
1923 if (type == ERR_WARNING) {
1924 uint8_t state = warning_state[warn_index(severity)];
1925 if ((state & warn_is_err) == warn_is_err)
1926 type = ERR_NONFATAL;
1927 }
1928
1929 return type;
Ed Berosetfa771012002-06-09 20:56:40 +00001930}
1931
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001932/*
1933 * The various error type prefixes
1934 */
1935static const char * const error_pfx_table[ERR_MASK+1] = {
1936 ";;; ", "debug: ", "info: ", "warning: ",
1937 "error: ", "fatal: ", "critical: ", "panic: "
1938};
1939static const char no_file_name[] = "nasm"; /* What to print if no file name */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001940
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001941/*
1942 * For fatal/critical/panic errors, kill this process.
1943 */
1944static fatal_func die_hard(errflags true_type, errflags severity)
1945{
1946 fflush(NULL);
1947
1948 if (true_type == ERR_PANIC && abort_on_panic)
1949 abort();
1950
1951 if (ofile) {
1952 fclose(ofile);
1953 if (!keep_all)
1954 remove(outname);
1955 ofile = NULL;
1956 }
1957
1958 if (severity & ERR_USAGE)
1959 usage();
1960
1961 /* Terminate immediately */
1962 exit(true_type - ERR_FATAL + 1);
1963}
1964
1965/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001966 * Returns the struct src_location appropriate for use, after some
1967 * potential filename mangling.
1968 */
1969static struct src_location error_where(errflags severity)
1970{
1971 struct src_location where;
1972
1973 if (severity & ERR_NOFILE) {
1974 where.filename = NULL;
1975 where.lineno = 0;
1976 } else {
1977 where = src_where_error();
1978
1979 if (!where.filename) {
1980 where.filename =
1981 inname && inname[0] ? inname :
1982 outname && outname[0] ? outname :
1983 NULL;
1984 where.lineno = 0;
1985 }
1986 }
1987
1988 return where;
1989}
1990
1991/*
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001992 * error reporting for critical and panic errors: minimize
1993 * the amount of system dependencies for getting a message out,
1994 * and in particular try to avoid memory allocations.
1995 */
1996fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1997{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001998 struct src_location where;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001999 errflags true_type = severity & ERR_MASK;
2000 static bool been_here = false;
2001
2002 if (unlikely(been_here))
2003 abort(); /* Recursive error... just die */
2004
2005 been_here = true;
2006
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002007 where = error_where(severity);
2008 if (!where.filename)
2009 where.filename = no_file_name;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002010
2011 fputs(error_pfx_table[severity], error_file);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002012 fputs(where.filename, error_file);
2013 if (where.lineno) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002014 fprintf(error_file, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002015 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002016 }
2017 fputs(errfmt->beforemsg, error_file);
2018 vfprintf(error_file, fmt, args);
2019 fputc('\n', error_file);
2020
2021 die_hard(true_type, severity);
2022}
2023
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08002024/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002025 * Stack of tentative error hold lists.
2026 */
2027struct nasm_errtext {
2028 struct nasm_errtext *next;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002029 char *msg; /* Owned by this structure */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002030 struct src_location where; /* Owned by the srcfile system */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002031 errflags severity;
2032 errflags true_type;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002033};
2034struct nasm_errhold {
2035 struct nasm_errhold *up;
2036 struct nasm_errtext *head, **tail;
2037};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002038
2039static void nasm_free_error(struct nasm_errtext *et)
2040{
2041 nasm_free(et->msg);
2042 nasm_free(et);
2043}
2044
2045static void nasm_issue_error(struct nasm_errtext *et);
2046
2047struct nasm_errhold *nasm_error_hold_push(void)
2048{
2049 struct nasm_errhold *eh;
2050
2051 nasm_new(eh);
2052 eh->up = errhold_stack;
2053 eh->tail = &eh->head;
2054 errhold_stack = eh;
2055
2056 return eh;
2057}
2058
2059void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
2060{
2061 struct nasm_errtext *et, *etmp;
2062
2063 /* Allow calling with a null argument saying no hold in the first place */
2064 if (!eh)
2065 return;
2066
2067 /* This *must* be the current top of the errhold stack */
2068 nasm_assert(eh == errhold_stack);
2069
2070 if (eh->head) {
2071 if (issue) {
2072 if (eh->up) {
2073 /* Commit the current hold list to the previous level */
2074 *eh->up->tail = eh->head;
2075 eh->up->tail = eh->tail;
2076 } else {
2077 /* Issue errors */
2078 list_for_each_safe(et, etmp, eh->head)
2079 nasm_issue_error(et);
2080 }
2081 } else {
2082 /* Free the list, drop errors */
2083 list_for_each_safe(et, etmp, eh->head)
2084 nasm_free_error(et);
2085 }
2086 }
2087
2088 errhold_stack = eh->up;
2089 nasm_free(eh);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002090}
2091
Ed Berosetfa771012002-06-09 20:56:40 +00002092/**
2093 * common error reporting
2094 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002095 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002096 * specific error message to error_file and may or may not return. It
2097 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002098 *
2099 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002100 * @param fmt the printf style format string
2101 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07002102void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002103{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002104 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002105 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002106
2107 if (true_type >= ERR_CRITICAL)
2108 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002109
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08002110 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08002111 return;
2112
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002113 nasm_new(et);
2114 et->severity = severity;
2115 et->true_type = true_type;
2116 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002117 et->where = error_where(severity);
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002118
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002119 if (errhold_stack && true_type <= ERR_NONFATAL) {
2120 /* It is a tentative error */
2121 *errhold_stack->tail = et;
2122 errhold_stack->tail = &et->next;
2123 } else {
2124 nasm_issue_error(et);
2125 }
2126
2127 /*
2128 * Don't do this before then, if we do, we lose messages in the list
2129 * file, as the list file is only generated in the last pass.
2130 */
2131 if (skip_this_pass(severity))
2132 return;
2133
2134 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2135 if (preproc)
2136 preproc->error_list_macros(severity);
2137}
2138
2139/*
2140 * Actually print, list and take action on an error
2141 */
2142static void nasm_issue_error(struct nasm_errtext *et)
2143{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002144 const char *pfx;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002145 char warnsuf[64]; /* Warning suffix */
2146 char linestr[64]; /* Formatted line number if applicable */
2147 const errflags severity = et->severity;
2148 const errflags true_type = et->true_type;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002149 const struct src_location where = et->where;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002150
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002151 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002152 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002153 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002154 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002155
H. Peter Anvinddb29062018-12-11 00:06:29 -08002156 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002157 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2158 /*
2159 * It's a warning without ERR_HERE defined, and we are not already
2160 * unwinding the macros that led us here.
2161 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002162 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002163 (true_type >= ERR_NONFATAL) ? "error=" : "",
2164 warning_name[warn_index(severity)]);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002165 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002166
H. Peter Anvinddb29062018-12-11 00:06:29 -08002167 *linestr = 0;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002168 if (where.lineno) {
H. Peter Anvinddb29062018-12-11 00:06:29 -08002169 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002170 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002171 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002172
H. Peter Anvin77016c82018-12-10 22:29:49 -08002173 if (!skip_this_pass(severity)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002174 const char *file = where.filename ? where.filename : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002175 const char *here = "";
2176
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002177 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002178 here = where.filename ? " here" : " in an unknown location";
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002179 }
2180
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002181 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002182 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002183 /*
2184 * Buffer up warnings until we either get an error
2185 * or we are on the code-generation pass.
2186 */
2187 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2188 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002189 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002190 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002191 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002192 * Actually output an error. If we have buffered
2193 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002194 */
2195 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002196 strlist_write(warn_list, "\n", error_file);
2197 strlist_free(&warn_list);
2198 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002199
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002200 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2201 file, linestr, errfmt->beforemsg,
2202 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002203 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002204 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002205
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002206 /* Are we recursing from error_list_macros? */
2207 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002208 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002209
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002210 /*
2211 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002212 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002213 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002214 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002215 if (where.lineno)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002216 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002217 pfx, et->msg, where.filename, where.lineno, warnsuf);
2218 else if (where.filename)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002219 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002220 pfx, et->msg, where.filename, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002221 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002222 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002223 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002224 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002225 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002226 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002227
H. Peter Anvin8f622462017-04-02 19:02:29 -07002228 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002229 goto done;
H. Peter Anvin8f622462017-04-02 19:02:29 -07002230
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002231 if (true_type >= ERR_FATAL)
2232 die_hard(true_type, severity);
2233 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002234 terminate_after_phase = true;
H. Peter Anvin3366e312018-02-07 14:14:36 -08002235
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002236done:
2237 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002238}
2239
H. Peter Anvin734b1882002-04-30 21:01:08 +00002240static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002241{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002242 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002243}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002244
H. Peter Anvin322bee02019-08-10 01:38:06 -07002245static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002246{
2247 int i;
2248
H. Peter Anvin322bee02019-08-10 01:38:06 -07002249 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002250 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2251 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002252 _progname, _progname);
2253 fputs(
2254 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002255 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002256 "\n"
2257 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002258 " -v (or --v) print the NASM version number and exit\n"
2259 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002260 "\n"
2261 " -o outfile write output to outfile\n"
2262 " --keep-all output files will not be removed even if an error happens\n"
2263 "\n"
2264 " -Xformat specifiy error reporting format (gnu or vc)\n"
2265 " -s redirect error messages to stdout\n"
2266 " -Zfile redirect error messages to file\n"
2267 "\n"
2268 " -M generate Makefile dependencies on stdout\n"
2269 " -MG d:o, missing files assumed generated\n"
2270 " -MF file set Makefile dependency file\n"
2271 " -MD file assemble and generate dependencies\n"
2272 " -MT file dependency target name\n"
2273 " -MQ file dependency target name (quoted)\n"
2274 " -MP emit phony targets\n"
2275 "\n"
2276 " -f format select output file format\n"
2277 , out);
2278 ofmt_list(ofmt, out);
2279 fputs(
2280 "\n"
2281 " -g generate debugging information\n"
2282 " -F format select a debugging format (output format dependent)\n"
2283 " -gformat same as -g -F format\n"
2284 , out);
2285 dfmt_list(out);
2286 fputs(
2287 "\n"
2288 " -l listfile write listing to a list file\n"
2289 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002290 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002291 " -Ld show byte and repeat counts in decimal, not hex\n"
2292 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002293 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002294 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002295 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002296 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002297 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002298 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002299 "\n"
2300 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2301 " -O0 no optimization\n"
2302 " -O1 minimal optimization\n"
2303 " -Ox multipass optimization (default)\n"
2304 " -Ov display the number of passes executed at the end\n"
2305 " -t assemble in limited SciTech TASM compatible mode\n"
2306 "\n"
2307 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2308 " -a don't preprocess (assemble only)\n"
2309 " -Ipath add a pathname to the include file path\n"
2310 " -Pfile pre-include a file (also --include)\n"
2311 " -Dmacro[=str] pre-define a macro\n"
2312 " -Umacro undefine a macro\n"
2313 " --pragma str pre-executes a specific %%pragma\n"
2314 " --before str add line (usually a preprocessor statement) before the input\n"
2315 " --no-line ignore %line directives in input\n"
2316 "\n"
2317 " --prefix str prepend the given string to the names of all extern,\n"
2318 " common and global symbols (also --gprefix)\n"
2319 " --suffix str append the given string to the names of all extern,\n"
2320 " common and global symbols (also --gprefix)\n"
2321 " --lprefix str prepend the given string to local symbols\n"
2322 " --lpostfix str append the given string to local symbols\n"
2323 "\n"
Byoungchan Lee3dc3d472021-07-27 05:09:09 +09002324 " --macho-min-os minos minimum os version for mach-o format(example: macos-11.0)\n"
2325 "\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002326 " -w+x enable warning x (also -Wx)\n"
2327 " -w-x disable warning x (also -Wno-x)\n"
2328 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2329 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2330 , out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002331
H. Peter Anvin322bee02019-08-10 01:38:06 -07002332 fprintf(out, " %-20s %s\n",
2333 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2334
2335 for (i = 1; i < WARN_IDX_ALL; i++) {
2336 const char *me = warning_name[i];
2337 const char *prev = warning_name[i-1];
2338 const char *next = warning_name[i+1];
2339
2340 if (prev) {
2341 int prev_len = strlen(prev);
2342 const char *dash = me;
2343
2344 while ((dash = strchr(dash+1, '-'))) {
2345 int prefix_len = dash - me; /* Not including final dash */
2346 if (strncmp(next, me, prefix_len+1)) {
2347 /* Only one or last option with this prefix */
2348 break;
2349 }
2350 if (prefix_len >= prev_len ||
2351 strncmp(prev, me, prefix_len) ||
2352 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2353 /* This prefix is different from the previous option */
2354 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2355 prefix_len, me, prefix_len+1, me);
2356 }
2357 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002358 }
H. Peter Anvin322bee02019-08-10 01:38:06 -07002359
2360 fprintf(out, " %-20s %s%s\n",
2361 warning_name[i], warning_help[i],
2362 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2363 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002364 }
2365
H. Peter Anvin322bee02019-08-10 01:38:06 -07002366 fputs(
2367 "\n"
2368 " --limit-X val set execution limit X\n"
2369 , out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002370
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002371
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002372 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002373 fprintf(out, " %-20s %s [",
2374 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002375 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002376 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002377 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002378 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002379 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002380 }
2381}