blob: 4cf6f2ab004c55c2702564cc2c8efe77be9a6679 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin322bee02019-08-10 01:38:06 -070072const char *_progname;
73
H. Peter Anvin55568c12016-10-03 19:46:49 -070074static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030075static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080076static bool skip_this_pass(errflags severity);
77static void nasm_verror_asm(errflags severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070079static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000080
H. Peter Anvin77016c82018-12-10 22:29:49 -080081struct error_format {
82 const char *beforeline; /* Before line number, if present */
83 const char *afterline; /* After line number, if present */
84 const char *beforemsg; /* Before actual message */
85};
86
87static const struct error_format errfmt_gnu = { ":", "", ": " };
88static const struct error_format errfmt_msvc = { "(", ")", " : " };
89static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080090static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080091
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070092unsigned int debug_nasm; /* Debugging messages? */
93
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080094static bool using_debug_info, opt_verbose_info;
95static const char *debug_format;
96
H. Peter Anvin3366e312018-02-07 14:14:36 -080097#ifndef ABORT_ON_PANIC
98# define ABORT_ON_PANIC 0
99#endif
100static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700101static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800102
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700103bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800104enum pass_type _pass_type;
105const char * const _pass_types[] =
106{
107 "init", "first", "optimize", "stabilize", "final"
108};
109int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000110int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800111int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000112
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700113struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800114
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800115const char *inname;
116const char *outname;
117static const char *listname;
118static const char *errname;
119
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700120static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700121
H. Peter Anvin338656c2016-02-17 20:59:22 -0800122const struct ofmt *ofmt = &OF_DEFAULT;
123const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400124const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800126FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000127
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400128FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300129struct optimization optimizing =
130 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100131static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400132
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800133iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400134static iflag_t cmd_cpu;
135
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800136struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800137bool in_absolute; /* Flag we are in ABSOLUTE seg */
138struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000141
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700143static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144
H. Peter Anvine7469712016-02-18 02:20:59 -0800145static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300146static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800147bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400148
H. Peter Anvin34754622018-11-28 12:36:53 -0800149#define OP_NORMAL (1U << 0)
150#define OP_PREPROCESS (1U << 1)
151#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400152
153static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400154
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700155/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700156static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700157static bool depend_missing_ok = false;
158static const char *depend_target = NULL;
159static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300160struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000161
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700162static bool want_usage;
163static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800164bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700166static char *quote_for_pmake(const char *str);
167static char *quote_for_wmake(const char *str);
168static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700169
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700170/*
171 * Execution limits that can be set via a command-line option or %pragma
172 */
173
H. Peter Anvin322bee02019-08-10 01:38:06 -0700174/*
175 * This is really unlimited; it would take far longer than the
176 * current age of the universe for this limit to be reached even on
177 * much faster CPUs than currently exist.
178*/
179#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180
H. Peter Anvin322bee02019-08-10 01:38:06 -0700181int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700182
183struct limit_info {
184 const char *name;
185 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700186 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700187};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700188/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700190 { "passes", "total number of passes", LIMIT_MAX_VAL },
191 { "stalled-passes", "number of passes without forward progress", 1000 },
192 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700193 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
194 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700195 { "rep", "%rep count", 1000000 },
196 { "eval", "expression evaluation descent", 1000000},
197 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700198};
199
H. Peter Anvin322bee02019-08-10 01:38:06 -0700200static void set_default_limits(void)
201{
202 int i;
203 for (i = 0; i <= LIMIT_MAX; i++)
204 nasm_limit[i] = limit_info[i].default_val;
205}
206
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207enum directive_result
208nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700209{
210 int i;
211 int64_t val;
212 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700213 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700215 if (!limit)
216 limit = "";
217 if (!valstr)
218 valstr = "";
219
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700220 for (i = 0; i <= LIMIT_MAX; i++) {
221 if (!nasm_stricmp(limit, limit_info[i].name))
222 break;
223 }
224 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800225 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800226 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700227 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700228 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700229 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700230 return DIRR_ERROR;
231 }
232
233 if (!nasm_stricmp(valstr, "unlimited")) {
234 val = LIMIT_MAX_VAL;
235 } else {
236 val = readnum(valstr, &rn_error);
237 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800238 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800239 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700240 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700241 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700242 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700243 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700244 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700245 if (val > LIMIT_MAX_VAL)
246 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700247 }
248
249 nasm_limit[i] = val;
250 return DIRR_OK;
251}
252
H. Peter Anvin892c4812018-05-30 14:43:46 -0700253int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400254{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700255 location.segment = segment;
256 if (segment == NO_SEG) {
257 location.offset = absolute.offset;
258 in_absolute = true;
259 } else {
260 location.offset = raa_read(offsets, segment);
261 in_absolute = false;
262 }
263 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400264}
265
266static void set_curr_offs(int64_t l_off)
267{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800268 if (in_absolute)
269 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400270 else
271 offsets = raa_write(offsets, location.segment, l_off);
272}
273
H. Peter Anvin892c4812018-05-30 14:43:46 -0700274static void increment_offset(int64_t delta)
275{
276 if (unlikely(delta == 0))
277 return;
278
279 location.offset += delta;
280 set_curr_offs(location.offset);
281}
282
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000283static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000284{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000285 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800287 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000288 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000289 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000290}
291
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800292/*
293 * Define system-defined macros that are not part of
294 * macros/standard.mac.
295 */
296static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800297{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700298 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800299 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800300
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700301 if (oct->have_local) {
302 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400303 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700304 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400305 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700306 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400307 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700308 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400309 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800310 }
311
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700312 if (oct->have_gm) {
313 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400314 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700315 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400316 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700317 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400318 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700319 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400320 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800321 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700322
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700323 if (oct->have_posix) {
324 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400325 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800326 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800327
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400328 /*
329 * In case if output format is defined by alias
330 * we have to put shortname of the alias itself here
331 * otherwise ABI backward compatibility gets broken.
332 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400333 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400334 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400335 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800336
337 /*
338 * Output-format specific macros.
339 */
340 if (ofmt->stdmac)
341 preproc->extra_stdmac(ofmt->stdmac);
342
343 /*
344 * Debug format, if any
345 */
346 if (dfmt != &null_debug_form) {
347 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
348 preproc->pre_define(temp);
349 }
350}
351
352/*
353 * Initialize the preprocessor, set up the include path, and define
354 * the system-included macros. This is called between passes 1 and 2
355 * of parsing the command options; ofmt and dfmt are defined at this
356 * point.
357 *
358 * Command-line specified preprocessor directives (-p, -d, -u,
359 * --pragma, --before) are processed after this function.
360 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300361static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800362{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800363 preproc->init();
364 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300365 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800366}
367
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300368static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700369{
370 FILE *deps;
371 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700372 bool wmake = (quote_for_make == quote_for_wmake);
373 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800374 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700375
376 if (!list)
377 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700378
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700379 wrapstr = wmake ? " &\n " : " \\\n ";
380 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381
382 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700383 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400384 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800385 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400386 return;
387 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700388 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400389 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700391
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700392 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800393 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700394 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 len = strlen(file);
396 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700397 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400398 linepos = 1;
399 }
400 fprintf(deps, " %s", file);
401 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700402 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700403 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700404 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700405
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800406 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700407 if (depend_emit_phony) {
408 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700409 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700410 nasm_free(file);
411 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700412 }
413
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800414 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700415
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700416 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400417 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700418}
419
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700420/* Convert a struct tm to a POSIX-style time constant */
421static int64_t make_posix_time(const struct tm *tm)
422{
423 int64_t t;
424 int64_t y = tm->tm_year;
425
426 /* See IEEE 1003.1:2004, section 4.14 */
427
428 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
429 t += tm->tm_yday;
430 t *= 24;
431 t += tm->tm_hour;
432 t *= 60;
433 t += tm->tm_min;
434 t *= 60;
435 t += tm->tm_sec;
436
437 return t;
438}
439
440static void timestamp(void)
441{
442 struct compile_time * const oct = &official_compile_time;
443 const struct tm *tp, *best_gm;
444
445 time(&oct->t);
446
447 best_gm = NULL;
448
449 tp = localtime(&oct->t);
450 if (tp) {
451 oct->local = *tp;
452 best_gm = &oct->local;
453 oct->have_local = true;
454 }
455
456 tp = gmtime(&oct->t);
457 if (tp) {
458 oct->gm = *tp;
459 best_gm = &oct->gm;
460 oct->have_gm = true;
461 if (!oct->have_local)
462 oct->local = oct->gm;
463 } else {
464 oct->gm = oct->local;
465 }
466
467 if (best_gm) {
468 oct->posix = make_posix_time(best_gm);
469 oct->have_posix = true;
470 }
471}
472
H. Peter Anvin038d8612007-04-12 16:54:50 +0000473int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000474{
H. Peter Anvin322bee02019-08-10 01:38:06 -0700475 _progname = argv[0];
476 if (!_progname || !_progname[0])
477 _progname = "nasm";
478
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700479 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800480
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800481 error_file = stderr;
482
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800483 iflag_set_default_cpu(&cpu);
484 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400485
H. Peter Anvin322bee02019-08-10 01:38:06 -0700486 set_default_limits();
487
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800488 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300489
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800490 _pass_type = PASS_INIT;
491 _passn = 0;
492
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700493 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800494 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
H. Peter Anvin13506202018-11-28 14:55:58 -0800496 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700497 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700498
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700499 /*
500 * We must call init_labels() before the command line parsing,
501 * because we may be setting prefixes/suffixes from the command
502 * line.
503 */
504 init_labels();
505
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000507 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000508
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000509 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400510 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000511
H. Peter Anvin55568c12016-10-03 19:46:49 -0700512 parse_cmdline(argc, argv, 1);
513 if (terminate_after_phase) {
514 if (want_usage)
515 usage();
516 return 1;
517 }
518
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800519 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800520 if (!using_debug_info) {
521 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800522 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800523 } else if (!debug_format) {
524 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300525 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800526 } else {
527 dfmt = dfmt_find(ofmt, debug_format);
528 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800529 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800530 debug_format, ofmt->shortname);
531 }
532 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000533
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300534 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800535
536 parse_cmdline(argc, argv, 2);
537 if (terminate_after_phase) {
538 if (want_usage)
539 usage();
540 return 1;
541 }
542
543 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800544 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000545
H. Peter Anvin34754622018-11-28 12:36:53 -0800546 /* Dependency filename if we are also doing other things */
547 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
548 if (outname)
549 depend_file = nasm_strcat(outname, ".d");
550 else
551 depend_file = filename_set_extension(inname, ".d");
552 }
553
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300554 /*
555 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800556 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300557 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300558 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800559 if (!outname && !(operating_mode & OP_PREPROCESS)) {
560 outname = filename_set_extension(inname, ofmt->extension);
561 if (!strcmp(outname, inname)) {
562 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800563 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 -0800564 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300565 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800566
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800567 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700568
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700569 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400570 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700571
H. Peter Anvin34754622018-11-28 12:36:53 -0800572 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000573 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700574
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400575 if (depend_missing_ok)
576 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700577
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800578 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 while ((line = preproc->getline()))
581 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800582 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800583 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400584 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000585 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700586 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000587 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000589
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800590 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700591 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800593 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 } else
595 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000596
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700597 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000598
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800599 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
600 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800601
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 while ((line = preproc->getline())) {
603 /*
604 * We generate %line directives if needed for later programs
605 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000606 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 int altline = src_get(&linnum, &file_name);
608 if (altline) {
609 if (altline == 1 && lineinc == 1)
610 nasm_fputs("", ofile);
611 else {
612 lineinc = (altline != -1 || lineinc != 1);
613 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000614 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 file_name);
616 }
617 prior_linnum = linnum;
618 }
619 nasm_fputs(line, ofile);
620 nasm_free(line);
621 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800622 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800623 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 if (ofile)
625 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700626 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400628 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400629 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400631 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700632 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400633 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800634 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400636 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400637 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700639 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200640
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400641 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800642 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400643 cleanup_labels();
644 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800645 if (ferror(ofile))
646 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400647 }
648
649 if (ofile) {
650 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700651 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400652 remove(outname);
653 ofile = NULL;
654 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000655 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000656
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800657 preproc->cleanup_session();
658
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700659 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400660 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700661
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000662 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000664
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 raa_free(offsets);
666 saa_free(forwrefs);
667 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000668 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700669 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800670 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000671
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700672 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000673}
674
H. Peter Anvineba20a72002-04-30 20:53:55 +0000675/*
676 * Get a parameter for a command line option.
677 * First arg must be in the form of e.g. -f...
678 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800679static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000680{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800681 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400682 if (p[2]) /* the parameter's in the option */
683 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800685 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800688 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000689 return NULL;
690}
691
H. Peter Anvindc242712007-11-18 11:55:10 -0800692/*
693 * Copy a filename
694 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800695static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800696{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800697 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700698 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800699
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800700 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800701}
702
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700703/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700704 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700705 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700706static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700707{
708 const char *p;
709 char *os, *q;
710
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400711 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700712 size_t nbs = 0;
713
714 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400715 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700716
717 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400718 switch (*p) {
719 case ' ':
720 case '\t':
721 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
722 n += nbs + 2;
723 nbs = 0;
724 break;
725 case '$':
726 case '#':
727 nbs = 0;
728 n += 2;
729 break;
730 case '\\':
731 nbs++;
732 n++;
733 break;
734 default:
735 nbs = 0;
736 n++;
737 break;
738 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700739 }
740
741 /* Convert N backslashes at the end of filename to 2N backslashes */
742 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400743 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700744
745 os = q = nasm_malloc(n);
746
747 nbs = 0;
748 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400749 switch (*p) {
750 case ' ':
751 case '\t':
752 while (nbs--)
753 *q++ = '\\';
754 *q++ = '\\';
755 *q++ = *p;
756 break;
757 case '$':
758 *q++ = *p;
759 *q++ = *p;
760 nbs = 0;
761 break;
762 case '#':
763 *q++ = '\\';
764 *q++ = *p;
765 nbs = 0;
766 break;
767 case '\\':
768 *q++ = *p;
769 nbs++;
770 break;
771 default:
772 *q++ = *p;
773 nbs = 0;
774 break;
775 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700776 }
777 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400778 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700779
780 *q = '\0';
781
782 return os;
783}
784
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700785/*
786 * Convert a string to a Watcom make-safe form
787 */
788static char *quote_for_wmake(const char *str)
789{
790 const char *p;
791 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700792 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700793
794 size_t n = 1; /* Terminating zero */
795
796 if (!str)
797 return NULL;
798
799 for (p = str; *p; p++) {
800 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700801 case ' ':
802 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700803 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700804 quote = true;
805 n++;
806 break;
807 case '\"':
808 quote = true;
809 n += 2;
810 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700811 case '$':
812 case '#':
813 n += 2;
814 break;
815 default:
816 n++;
817 break;
818 }
819 }
820
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700821 if (quote)
822 n += 2;
823
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700824 os = q = nasm_malloc(n);
825
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700826 if (quote)
827 *q++ = '\"';
828
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700829 for (p = str; *p; p++) {
830 switch (*p) {
831 case '$':
832 case '#':
833 *q++ = '$';
834 *q++ = *p;
835 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700836 case '\"':
837 *q++ = *p;
838 *q++ = *p;
839 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700840 default:
841 *q++ = *p;
842 break;
843 }
844 }
845
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700846 if (quote)
847 *q++ = '\"';
848
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700849 *q = '\0';
850
851 return os;
852}
853
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100854enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800855 OPT_BOGUS,
856 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700857 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800858 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700859 OPT_MANGLE,
860 OPT_INCLUDE,
861 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700862 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700863 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800864 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700865 OPT_NO_LINE,
866 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100867};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700868enum need_arg {
869 ARG_NO,
870 ARG_YES,
871 ARG_MAYBE
872};
873
H. Peter Anvin3366e312018-02-07 14:14:36 -0800874struct textargs {
875 const char *label;
876 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700877 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700878 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800879};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800880static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700881 {"v", OPT_VERSION, ARG_NO, 0},
882 {"version", OPT_VERSION, ARG_NO, 0},
883 {"help", OPT_HELP, ARG_NO, 0},
884 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
885 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
886 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
887 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
888 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
889 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
890 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
891 {"include", OPT_INCLUDE, ARG_YES, 0},
892 {"pragma", OPT_PRAGMA, ARG_YES, 0},
893 {"before", OPT_BEFORE, ARG_YES, 0},
894 {"limit-", OPT_LIMIT, ARG_YES, 0},
895 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
896 {"no-line", OPT_NO_LINE, ARG_NO, 0},
897 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
898 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000899};
900
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400901static void show_version(void)
902{
903 printf("NASM version %s compiled on %s%s\n",
904 nasm_version, nasm_date, nasm_compile_options);
905 exit(0);
906}
907
H. Peter Anvin423e3812007-11-15 17:12:29 -0800908static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700909static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000910{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000911 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800912 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000913
914 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800915 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000916
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700918 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400919 /* These parameters take values */
920 if (!(param = get_param(p, q, &advance)))
921 return advance;
922 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800923
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 switch (p[1]) {
925 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 if (pass == 1)
927 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700929
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800932 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700934
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 if (pass == 1) {
937 ofmt = ofmt_find(param, &ofmt_alias);
938 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800939 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 }
942 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700943
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400944 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800945 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700947
H. Peter Anvin55568c12016-10-03 19:46:49 -0700948 if (!*param) {
949 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300950 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700951 } else {
952 while (*param) {
953 switch (*param) {
954 case '0': case '1': case '2': case '3': case '4':
955 case '5': case '6': case '7': case '8': case '9':
956 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700957
Chang S. Baea5786342018-08-15 23:22:21 +0300958 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
959 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700960 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300961 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 else
Chang S. Baea5786342018-08-15 23:22:21 +0300963 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700964 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700965
H. Peter Anvin55568c12016-10-03 19:46:49 -0700966 case 'v':
967 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400968 param++;
969 opt_verbose_info = true;
970 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700971
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 case 'x':
973 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300974 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700975 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700976
H. Peter Anvin55568c12016-10-03 19:46:49 -0700977 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700978 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 *param);
980 break;
981 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400982 }
Chang S. Baea5786342018-08-15 23:22:21 +0300983 if (optimizing.level > MAX_OPTIMIZE)
984 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400985 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400986 }
987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400989 case 'p': /* pre-include */
990 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 if (pass == 2)
992 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 case 'd': /* pre-define */
996 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700997 if (pass == 2)
998 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001000
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001001 case 'u': /* un-define */
1002 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001003 if (pass == 2)
1004 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001006
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001007 case 'i': /* include search path */
1008 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001009 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001010 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001013 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001014 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001015 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001016 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001017
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001018 case 'L': /* listing options */
1019 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001020 while (*param)
1021 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001022 }
1023 break;
1024
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001025 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001027 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001028 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001029
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001030 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001031 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001032 using_debug_info = true;
1033 debug_format = param;
1034 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001035 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001036
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001037 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001038 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001039 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1040 errfmt = &errfmt_msvc;
1041 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1042 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001044 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001049 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 using_debug_info = true;
1051 if (p[2])
1052 debug_format = nasm_skip_spaces(p + 2);
1053 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001057 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001058 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001062 /* legacy option */
1063 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 exit(0);
1065 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001066
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001068 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001069 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001070 nasm_ctype_tasm_mode();
1071 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001075 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001077
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001078 case 'e': /* preprocess only */
1079 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001080 if (pass == 1)
1081 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001083
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001084 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001085 if (pass == 1)
1086 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001088
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001089 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001090 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001091 if (pass == 2)
1092 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001093 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001096 if (pass == 1) {
1097 switch (p[2]) {
1098 case 'W':
1099 quote_for_make = quote_for_wmake;
1100 break;
1101 case 'D':
1102 case 'F':
1103 case 'T':
1104 case 'Q':
1105 advance = true;
1106 break;
1107 default:
1108 break;
1109 }
1110 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001111 switch (p[2]) {
1112 case 0:
1113 operating_mode = OP_DEPEND;
1114 break;
1115 case 'G':
1116 operating_mode = OP_DEPEND;
1117 depend_missing_ok = true;
1118 break;
1119 case 'P':
1120 depend_emit_phony = true;
1121 break;
1122 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001123 operating_mode |= OP_DEPEND;
1124 if (q && (q[0] != '-' || q[1] == '\0')) {
1125 depend_file = q;
1126 advance = true;
1127 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001128 break;
1129 case 'F':
1130 depend_file = q;
1131 advance = true;
1132 break;
1133 case 'T':
1134 depend_target = q;
1135 advance = true;
1136 break;
1137 case 'Q':
1138 depend_target = quote_for_make(q);
1139 advance = true;
1140 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001141 case 'W':
1142 /* handled in pass 1 */
1143 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001144 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001145 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001146 break;
1147 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001148 }
1149 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001150 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001151 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001152 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001154
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 case '-':
1156 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001157 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001159 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001160 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001161
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001162 p += 2;
1163
1164 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001165 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 break;
1167 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001168
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001169 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001171 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001172 olen = strlen(tx->label);
1173
1174 if (olen > plen)
1175 continue;
1176
1177 if (nasm_memicmp(p, tx->label, olen))
1178 continue;
1179
1180 if (tx->label[olen-1] == '-')
1181 break; /* Incomplete option */
1182
1183 if (!p[olen] || p[olen] == '=')
1184 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001186
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001187 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001188 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001189 }
1190
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001191 opt = tx->opt;
1192
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001193 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001194 if (param)
1195 *param++ = '\0';
1196
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001197 switch (tx->need_arg) {
1198 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001199 if (!param) {
1200 param = q;
1201 advance = true;
1202 }
1203
1204 /* Note: a null string is a valid parameter */
1205 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001206 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001207 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001209 break;
1210
1211 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001212 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001213 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001214 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001215 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001216 break;
1217
1218 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1219 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001220 }
1221
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001222 switch (opt) {
1223 case OPT_BOGUS:
1224 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001225 case OPT_VERSION:
1226 show_version();
1227 break;
1228 case OPT_ABORT_ON_PANIC:
1229 abort_on_panic = true;
1230 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001231 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001232 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001233 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001234 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001235 case OPT_INCLUDE:
1236 if (pass == 2)
1237 preproc->pre_include(q);
1238 break;
1239 case OPT_PRAGMA:
1240 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001241 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001242 break;
1243 case OPT_BEFORE:
1244 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001245 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001246 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001247 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001248 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001249 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001250 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001251 case OPT_KEEP_ALL:
1252 keep_all = true;
1253 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001254 case OPT_NO_LINE:
1255 pp_noline = true;
1256 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001257 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001258 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001259 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001260 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001261 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001262 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001263 default:
1264 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001266
1267 if (eqsave)
1268 *eqsave = '='; /* Restore = argument separator */
1269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 break;
1271 }
1272
1273 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001274 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 break;
1276 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001277 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001278 /* In theory we could allow multiple input files... */
1279 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001280 }
1281
1282 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001283}
1284
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285#define ARG_BUF_DELTA 128
1286
H. Peter Anvin55568c12016-10-03 19:46:49 -07001287static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001289 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001290 int bufsize, prevargsize;
1291
1292 bufsize = prevargsize = ARG_BUF_DELTA;
1293 buffer = nasm_malloc(ARG_BUF_DELTA);
1294 prevarg = nasm_malloc(ARG_BUF_DELTA);
1295 prevarg[0] = '\0';
1296
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 p = buffer;
1299 while (1) { /* Loop to handle long lines */
1300 q = fgets(p, bufsize - (p - buffer), rfile);
1301 if (!q)
1302 break;
1303 p += strlen(p);
1304 if (p > buffer && p[-1] == '\n')
1305 break;
1306 if (p - buffer > bufsize - 10) {
1307 int offset;
1308 offset = p - buffer;
1309 bufsize += ARG_BUF_DELTA;
1310 buffer = nasm_realloc(buffer, bufsize);
1311 p = buffer + offset;
1312 }
1313 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001314
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (!q && p == buffer) {
1316 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001317 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 nasm_free(buffer);
1319 nasm_free(prevarg);
1320 return;
1321 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001322
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 /*
1324 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1325 * them are present at the end of the line.
1326 */
1327 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001328
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001329 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001332 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001333
H. Peter Anvin55568c12016-10-03 19:46:49 -07001334 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336
Charles Crayne192d5b52007-10-18 19:02:42 -07001337 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 prevargsize += ARG_BUF_DELTA;
1339 prevarg = nasm_realloc(prevarg, prevargsize);
1340 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001341 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001342 }
1343}
1344
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001345/* Function to process args from a string of args, rather than the
1346 * argv array. Used by the environment variable and response file
1347 * processing.
1348 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001349static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001351 char *p, *q, *arg, *prevarg;
1352 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001353
1354 p = args;
1355 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001357 arg = NULL;
1358 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 q = p;
1360 while (*p && *p != separator)
1361 p++;
1362 while (*p == separator)
1363 *p++ = '\0';
1364 prevarg = arg;
1365 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001366 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001368 }
1369 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001370 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001371}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001372
H. Peter Anvin55568c12016-10-03 19:46:49 -07001373static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001374{
1375 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001376 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001377 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001378 perror(file);
1379 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001380 }
1381 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001382 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001383 }
1384 fclose(f);
1385}
1386
H. Peter Anvin55568c12016-10-03 19:46:49 -07001387static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001388{
1389 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001390 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001391
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001392 /*
1393 * Initialize all the warnings to their default state, including
1394 * warning index 0 used for "always on".
1395 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001396 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001397
1398 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001399 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001400 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001401 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001402 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001404 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001406 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001407
1408 /*
1409 * Now process the actual command line.
1410 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001412 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 argv++;
1414 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001415 /*
1416 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 * arguments like the environment variable. This allows us
1418 * to have multiple arguments on a single line, which is
1419 * different to the -@resp file processing below for regular
1420 * NASM.
1421 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001422 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 argc--;
1424 argv++;
1425 }
1426 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001427 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001428 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001429 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001430 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001431 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001433 } else {
1434 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1435 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 }
1437 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001438 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001439 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001440 }
1441
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001442 /*
1443 * Look for basic command line typos. This definitely doesn't
1444 * catch all errors, but it might help cases of fumbled fingers.
1445 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001446 if (pass != 2)
1447 return;
1448
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001449 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001450 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001451 else if ((errname && !strcmp(inname, errname)) ||
1452 (outname && !strcmp(inname, outname)) ||
1453 (listname && !strcmp(inname, listname)) ||
1454 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001455 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001456
1457 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001458 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001459 if (!error_file) {
1460 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001461 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001462 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001463 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001464}
1465
H. Peter Anvin29651542018-12-18 19:14:40 -08001466static void forward_refs(insn *instruction)
1467{
1468 int i;
1469 struct forwrefinfo *fwinf;
1470
1471 instruction->forw_ref = false;
1472
1473 if (!optimizing.level)
1474 return; /* For -O0 don't bother */
1475
1476 if (!forwref)
1477 return;
1478
1479 if (forwref->lineno != globallineno)
1480 return;
1481
1482 instruction->forw_ref = true;
1483 do {
1484 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1485 forwref = saa_rstruct(forwrefs);
1486 } while (forwref && forwref->lineno == globallineno);
1487
1488 if (!pass_first())
1489 return;
1490
1491 for (i = 0; i < instruction->operands; i++) {
1492 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1493 fwinf = saa_wstruct(forwrefs);
1494 fwinf->lineno = globallineno;
1495 fwinf->operand = i;
1496 }
1497 }
1498}
1499
1500static void process_insn(insn *instruction)
1501{
1502 int32_t n;
1503 int64_t l;
1504
1505 if (!instruction->times)
1506 return; /* Nothing to do... */
1507
1508 nasm_assert(instruction->times > 0);
1509
1510 /*
1511 * NOTE: insn_size() can change instruction->times
1512 * (usually to 1) when called.
1513 */
1514 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001515 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001516 for (n = 1; n <= instruction->times; n++) {
1517 l = insn_size(location.segment, location.offset,
1518 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001519 /* l == -1 -> invalid instruction */
1520 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001521 increment_offset(l);
1522 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001523 if (list_option('p')) {
1524 struct out_data dummy;
1525 memset(&dummy, 0, sizeof dummy);
1526 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1527 dummy.offset = start;
1528 dummy.size = location.offset - start;
1529 lfmt->output(&dummy);
1530 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001531 } else {
1532 l = assemble(location.segment, location.offset,
1533 globalbits, instruction);
1534 /* We can't get an invalid instruction here */
1535 increment_offset(l);
1536
1537 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001538 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001539 for (n = 2; n <= instruction->times; n++) {
1540 l = assemble(location.segment, location.offset,
1541 globalbits, instruction);
1542 increment_offset(l);
1543 }
1544 lfmt->downlevel(LIST_TIMES);
1545 }
1546 }
1547}
1548
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001549static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001551 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001553 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001554 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001555
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001556 switch (cmd_sb) {
1557 case 16:
1558 break;
1559 case 32:
1560 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001561 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001562 break;
1563 case 64:
1564 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001565 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001566 break;
1567 default:
1568 panic();
1569 break;
1570 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001572 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001573
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001574 if (listname && !keep_all) {
1575 /* Remove the list file in case we die before the output pass */
1576 remove(listname);
1577 }
1578
1579 while (!terminate_after_phase && !pass_final()) {
1580 _passn++;
1581 if (pass_type() != PASS_OPT || !global_offset_changed)
1582 _pass_type++;
1583 global_offset_changed = 0;
1584
1585 /*
1586 * Create a warning buffer list unless we are in
1587 * pass 2 (everything will be emitted immediately in pass 2.)
1588 */
1589 if (warn_list) {
1590 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001591 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001592 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001593
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001594 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001595 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001596
H. Peter Anvincac0b192017-03-28 16:12:30 -07001597 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001599 if (listname) {
1600 if (pass_final() || list_on_every_pass()) {
1601 active_list_options = list_options;
1602 lfmt->init(listname);
1603 } else if (active_list_options) {
1604 /*
1605 * Looks like we used the list engine on a previous pass,
1606 * but now it is turned off, presumably via %pragma -p
1607 */
1608 lfmt->cleanup();
1609 if (!keep_all)
1610 remove(listname);
1611 active_list_options = 0;
1612 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001613 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001614
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001615 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001616 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 saa_rewind(forwrefs);
1618 forwref = saa_rstruct(forwrefs);
1619 raa_free(offsets);
1620 offsets = raa_init();
1621 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001622 location.segment = NO_SEG;
1623 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001624 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001625 location.known = true;
1626 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001627 switch_segment(ofmt->section(NULL, &globalbits));
1628 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001629
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001631
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001633 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001634 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001635 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001636
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001637 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001638 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001639 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001640 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001641 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001642 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001643
H. Peter Anvinc7131682017-03-07 17:45:01 -08001644 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001645 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001646 forward_refs(&output_ins);
1647 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001648 cleanup_insn(&output_ins);
1649
1650 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001653
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001654 preproc->cleanup_pass();
1655
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001656 if (global_offset_changed) {
1657 switch (pass_type()) {
1658 case PASS_OPT:
1659 /*
1660 * This is the only pass type that can be executed more
1661 * than once, and therefore has the ability to stall.
1662 */
1663 if (global_offset_changed < prev_offset_changed) {
1664 prev_offset_changed = global_offset_changed;
1665 stall_count = 0;
1666 } else {
1667 stall_count++;
1668 }
1669
1670 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1671 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1672 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001673 nasm_nonfatalf(ERR_UNDEAD,
1674 "unable to find valid values for all labels "
1675 "after %"PRId64" passes; "
1676 "stalled for %"PRId64", giving up.",
1677 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001678 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001679 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001680 }
1681 break;
1682
1683 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001684 /*!
1685 *!phase [off] phase error during stabilization
1686 *! warns about symbols having changed values during
1687 *! the second-to-last assembly pass. This is not
1688 *! inherently fatal, but may be a source of bugs.
1689 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001690 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1691 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001692 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001693 break;
1694
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001695 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001696 nasm_nonfatalf(ERR_UNDEAD,
1697 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001698 break;
1699
1700 default:
1701 /* This is normal, we'll keep going... */
1702 break;
1703 }
1704 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001705
1706 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001707 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001708
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001709 if (opt_verbose_info && pass_final()) {
1710 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001711 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001712 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001713
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001714 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001715 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001716}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717
Ed Berosetfa771012002-06-09 20:56:40 +00001718/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001719 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001720 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001721static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001722{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001723 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001724
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001725 if ((severity & ERR_MASK) >= ERR_FATAL)
1726 return 0; /* Fatal errors are never suppressible */
1727
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001728 /* Warnings MUST HAVE a warning category specifier! */
1729 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001730
1731 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001732 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001733
1734 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001735}
1736
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001737static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001738{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001739 errflags type = severity & ERR_MASK;
1740
H. Peter Anvin8f622462017-04-02 19:02:29 -07001741 /*
1742 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001743 * We can never skip fatal errors as by definition they cannot be
1744 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001745 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001746 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001747 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001748
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001749 /*
1750 * ERR_LISTMSG messages are always skipped; the list file
1751 * receives them anyway as this function is not consulted
1752 * for sending to the list file.
1753 */
1754 if (type == ERR_LISTMSG)
1755 return true;
1756
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001757 /* This message not applicable unless pass_final */
1758 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001759}
1760
Ed Berosetfa771012002-06-09 20:56:40 +00001761/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001762 * check for suppressed message (usually warnings or notes)
1763 *
1764 * @param severity the severity of the warning or error
1765 * @return true if we should abort error/warning printing
1766 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001767static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001768{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001769 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001770 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001771 return false;
1772
1773 /* This error/warning is pointless if we are dead anyway */
1774 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1775 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001776
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001777 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1778}
1779
1780/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001781 * Return the true error type (the ERR_MASK part) of the given
1782 * severity, accounting for warnings that may need to be promoted to
1783 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001784 *
1785 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001786 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001787 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001788static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001789{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001790 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1791 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001792
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001793 type = severity & ERR_MASK;
1794
1795 /* Promote warning to error? */
1796 if (type == ERR_WARNING) {
1797 uint8_t state = warning_state[warn_index(severity)];
1798 if ((state & warn_is_err) == warn_is_err)
1799 type = ERR_NONFATAL;
1800 }
1801
1802 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001803}
1804
1805/**
Ed Berosetfa771012002-06-09 20:56:40 +00001806 * common error reporting
1807 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001808 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001809 * specific error message to error_file and may or may not return. It
1810 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001811 *
1812 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001813 * @param fmt the printf style format string
1814 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001815static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001816{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001817 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001818 char warnsuf[64];
1819 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001820 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001821 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001822 const char *currentfile = NULL;
1823 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001824 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001825 ";;; ", "debug: ", "info: ", "warning: ",
1826 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001827 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001828
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001829 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001830 return;
1831
1832 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001833 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001834 if (!currentfile) {
1835 currentfile = currentfile ? currentfile :
1836 inname && inname[0] ? inname :
1837 outname && outname[0] ? outname :
1838 NULL;
1839 lineno = 0;
1840 }
1841 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001842
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001843 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001844 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001845 else
1846 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001847
1848 vsnprintf(msg, sizeof msg, fmt, args);
1849 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001850 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1851 /*
1852 * It's a warning without ERR_HERE defined, and we are not already
1853 * unwinding the macros that led us here.
1854 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001855 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001856 (true_type >= ERR_NONFATAL) ? "error=" : "",
1857 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001858 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001859
H. Peter Anvinddb29062018-12-11 00:06:29 -08001860 *linestr = 0;
1861 if (lineno) {
1862 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1863 errfmt->beforeline, lineno, errfmt->afterline);
1864 }
1865
H. Peter Anvin77016c82018-12-10 22:29:49 -08001866 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001867 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001868 const char *here = "";
1869
1870 if (severity & ERR_HERE)
1871 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001872
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001873 if (warn_list && true_type < ERR_NONFATAL &&
1874 !(pass_first() && (severity & ERR_PASS1)))
1875 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001876 /*
1877 * Buffer up warnings until we either get an error
1878 * or we are on the code-generation pass.
1879 */
1880 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1881 file, linestr, errfmt->beforemsg,
1882 pfx, msg, here, warnsuf);
1883 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001884 /*
1885 * If we have buffered warnings, and this is a non-warning,
1886 * output them now.
1887 */
1888 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001889 strlist_write(warn_list, "\n", error_file);
1890 strlist_free(&warn_list);
1891 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001892
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001893 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1894 file, linestr, errfmt->beforemsg,
1895 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001896
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001897 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001898 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001899
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001900 /* Are we recursing from error_list_macros? */
1901 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001902 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001903
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001904 /*
1905 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001906 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001907 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001908 if (severity & ERR_HERE) {
1909 if (lineno)
1910 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1911 pfx, msg, currentfile, lineno, warnsuf);
1912 else if (currentfile)
1913 lfmt->error(severity, "%s%s in file %s%s",
1914 pfx, msg, currentfile, warnsuf);
1915 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001916 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001917 pfx, msg, warnsuf);
1918 } else {
1919 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1920 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001921
H. Peter Anvin8f622462017-04-02 19:02:29 -07001922 if (skip_this_pass(severity))
1923 return;
1924
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001925 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001926 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001927
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001928 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1929 if (!(severity & ERR_HERE))
1930 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001931
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001932 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001933 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001935 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001936 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001937 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001938 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001940 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 break;
1942 case ERR_FATAL:
1943 if (ofile) {
1944 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001945 if (!keep_all)
1946 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001947 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 }
1949 if (want_usage)
1950 usage();
1951 exit(1); /* instantly die */
1952 break; /* placate silly compilers */
1953 case ERR_PANIC:
1954 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001955
1956 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001957 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001958
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001959 if (ofile) {
1960 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001961 if (!keep_all)
1962 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001963 ofile = NULL;
1964 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 exit(3);
1966 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001967 default:
1968 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001969 }
1970}
1971
H. Peter Anvin734b1882002-04-30 21:01:08 +00001972static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001973{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001974 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001975}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001976
H. Peter Anvin322bee02019-08-10 01:38:06 -07001977static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001978{
1979 int i;
1980
H. Peter Anvin322bee02019-08-10 01:38:06 -07001981 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001982 "Usage: %s [-@ response_file] [options...] [--] filename\n"
1983 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07001984 _progname, _progname);
1985 fputs(
1986 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001987 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001988 "\n"
1989 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001990 " -v (or --v) print the NASM version number and exit\n"
1991 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001992 "\n"
1993 " -o outfile write output to outfile\n"
1994 " --keep-all output files will not be removed even if an error happens\n"
1995 "\n"
1996 " -Xformat specifiy error reporting format (gnu or vc)\n"
1997 " -s redirect error messages to stdout\n"
1998 " -Zfile redirect error messages to file\n"
1999 "\n"
2000 " -M generate Makefile dependencies on stdout\n"
2001 " -MG d:o, missing files assumed generated\n"
2002 " -MF file set Makefile dependency file\n"
2003 " -MD file assemble and generate dependencies\n"
2004 " -MT file dependency target name\n"
2005 " -MQ file dependency target name (quoted)\n"
2006 " -MP emit phony targets\n"
2007 "\n"
2008 " -f format select output file format\n"
2009 , out);
2010 ofmt_list(ofmt, out);
2011 fputs(
2012 "\n"
2013 " -g generate debugging information\n"
2014 " -F format select a debugging format (output format dependent)\n"
2015 " -gformat same as -g -F format\n"
2016 , out);
2017 dfmt_list(out);
2018 fputs(
2019 "\n"
2020 " -l listfile write listing to a list file\n"
2021 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002022 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002023 " -Ld show byte and repeat counts in decimal, not hex\n"
2024 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002025 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002026 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002027 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002028 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002029 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002030 "\n"
2031 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2032 " -O0 no optimization\n"
2033 " -O1 minimal optimization\n"
2034 " -Ox multipass optimization (default)\n"
2035 " -Ov display the number of passes executed at the end\n"
2036 " -t assemble in limited SciTech TASM compatible mode\n"
2037 "\n"
2038 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2039 " -a don't preprocess (assemble only)\n"
2040 " -Ipath add a pathname to the include file path\n"
2041 " -Pfile pre-include a file (also --include)\n"
2042 " -Dmacro[=str] pre-define a macro\n"
2043 " -Umacro undefine a macro\n"
2044 " --pragma str pre-executes a specific %%pragma\n"
2045 " --before str add line (usually a preprocessor statement) before the input\n"
2046 " --no-line ignore %line directives in input\n"
2047 "\n"
2048 " --prefix str prepend the given string to the names of all extern,\n"
2049 " common and global symbols (also --gprefix)\n"
2050 " --suffix str append the given string to the names of all extern,\n"
2051 " common and global symbols (also --gprefix)\n"
2052 " --lprefix str prepend the given string to local symbols\n"
2053 " --lpostfix str append the given string to local symbols\n"
2054 "\n"
2055 " -w+x enable warning x (also -Wx)\n"
2056 " -w-x disable warning x (also -Wno-x)\n"
2057 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2058 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2059 , out);
2060
2061 fprintf(out, " %-20s %s\n",
2062 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2063
2064 for (i = 1; i < WARN_IDX_ALL; i++) {
2065 const char *me = warning_name[i];
2066 const char *prev = warning_name[i-1];
2067 const char *next = warning_name[i+1];
2068
2069 if (prev) {
2070 int prev_len = strlen(prev);
2071 const char *dash = me;
2072
2073 while ((dash = strchr(dash+1, '-'))) {
2074 int prefix_len = dash - me; /* Not including final dash */
2075 if (strncmp(next, me, prefix_len+1)) {
2076 /* Only one or last option with this prefix */
2077 break;
2078 }
2079 if (prefix_len >= prev_len ||
2080 strncmp(prev, me, prefix_len) ||
2081 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2082 /* This prefix is different from the previous option */
2083 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2084 prefix_len, me, prefix_len+1, me);
2085 }
2086 }
2087 }
2088
2089 fprintf(out, " %-20s %s%s\n",
2090 warning_name[i], warning_help[i],
2091 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2092 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2093 }
2094
2095 fputs(
2096 "\n"
2097 " --limit-X val set execution limit X\n"
2098 , out);
2099
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002100
2101 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002102 fprintf(out, " %-20s %s [",
2103 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002104 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002105 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002106 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002107 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002108 }
2109 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002110}