blob: 6d73a5f5c9127eef3eea3d1fba50690e4ccb5ac0 [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 Anvin (Intel)d6e81772019-08-09 08:06:39 -0700120static uint64_t list_options;
121uint64_t active_list_options; /* Set during the final pass only */
122
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700123static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700124
H. Peter Anvin338656c2016-02-17 20:59:22 -0800125const struct ofmt *ofmt = &OF_DEFAULT;
126const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400127const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800129FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000130
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400131FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300132struct optimization optimizing =
133 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100134static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400135
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800136iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400137static iflag_t cmd_cpu;
138
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800139struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800140bool in_absolute; /* Flag we are in ABSOLUTE seg */
141struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000142
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000144
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700146static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000147
H. Peter Anvine7469712016-02-18 02:20:59 -0800148static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300149static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800150bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400151
H. Peter Anvin34754622018-11-28 12:36:53 -0800152#define OP_NORMAL (1U << 0)
153#define OP_PREPROCESS (1U << 1)
154#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400155
156static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400157
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700158/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700159static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700160static bool depend_missing_ok = false;
161static const char *depend_target = NULL;
162static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300163struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000164
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700165static bool want_usage;
166static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800167bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000168
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700169static char *quote_for_pmake(const char *str);
170static char *quote_for_wmake(const char *str);
171static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700172
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700173/*
174 * Execution limits that can be set via a command-line option or %pragma
175 */
176
H. Peter Anvin322bee02019-08-10 01:38:06 -0700177/*
178 * This is really unlimited; it would take far longer than the
179 * current age of the universe for this limit to be reached even on
180 * much faster CPUs than currently exist.
181*/
182#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183
H. Peter Anvin322bee02019-08-10 01:38:06 -0700184int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700185
186struct limit_info {
187 const char *name;
188 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700189 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700190};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700191/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700192static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700193 { "passes", "total number of passes", LIMIT_MAX_VAL },
194 { "stalled-passes", "number of passes without forward progress", 1000 },
195 { "macro-levels", "levels of macro expansion", 10000 },
196 { "macro-tokens", "tokens processed during macro expansion", 10000000 },
197 { "rep", "%rep count", 1000000 },
198 { "eval", "expression evaluation descent", 1000000},
199 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700200};
201
H. Peter Anvin322bee02019-08-10 01:38:06 -0700202static void set_default_limits(void)
203{
204 int i;
205 for (i = 0; i <= LIMIT_MAX; i++)
206 nasm_limit[i] = limit_info[i].default_val;
207}
208
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700209enum directive_result
210nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700211{
212 int i;
213 int64_t val;
214 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700215 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700216
217 for (i = 0; i <= LIMIT_MAX; i++) {
218 if (!nasm_stricmp(limit, limit_info[i].name))
219 break;
220 }
221 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800222 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800223 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700224 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700225 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700226 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700227 return DIRR_ERROR;
228 }
229
230 if (!nasm_stricmp(valstr, "unlimited")) {
231 val = LIMIT_MAX_VAL;
232 } else {
233 val = readnum(valstr, &rn_error);
234 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800235 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800236 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700237 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700238 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700239 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700240 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700241 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700242 if (val > LIMIT_MAX_VAL)
243 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700244 }
245
246 nasm_limit[i] = val;
247 return DIRR_OK;
248}
249
H. Peter Anvin892c4812018-05-30 14:43:46 -0700250int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400251{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700252 location.segment = segment;
253 if (segment == NO_SEG) {
254 location.offset = absolute.offset;
255 in_absolute = true;
256 } else {
257 location.offset = raa_read(offsets, segment);
258 in_absolute = false;
259 }
260 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400261}
262
263static void set_curr_offs(int64_t l_off)
264{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800265 if (in_absolute)
266 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400267 else
268 offsets = raa_write(offsets, location.segment, l_off);
269}
270
H. Peter Anvin892c4812018-05-30 14:43:46 -0700271static void increment_offset(int64_t delta)
272{
273 if (unlikely(delta == 0))
274 return;
275
276 location.offset += delta;
277 set_curr_offs(location.offset);
278}
279
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000280static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000281{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000282 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800284 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000285 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000287}
288
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800289/*
290 * Define system-defined macros that are not part of
291 * macros/standard.mac.
292 */
293static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800294{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700295 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800297
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700298 if (oct->have_local) {
299 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400300 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700301 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400302 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700303 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400304 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700305 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400306 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800307 }
308
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700309 if (oct->have_gm) {
310 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400311 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700312 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700314 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400315 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700316 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400317 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800318 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700319
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700320 if (oct->have_posix) {
321 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400322 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800323 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800324
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400325 /*
326 * In case if output format is defined by alias
327 * we have to put shortname of the alias itself here
328 * otherwise ABI backward compatibility gets broken.
329 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400330 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400331 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400332 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800333
334 /*
335 * Output-format specific macros.
336 */
337 if (ofmt->stdmac)
338 preproc->extra_stdmac(ofmt->stdmac);
339
340 /*
341 * Debug format, if any
342 */
343 if (dfmt != &null_debug_form) {
344 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
345 preproc->pre_define(temp);
346 }
347}
348
349/*
350 * Initialize the preprocessor, set up the include path, and define
351 * the system-included macros. This is called between passes 1 and 2
352 * of parsing the command options; ofmt and dfmt are defined at this
353 * point.
354 *
355 * Command-line specified preprocessor directives (-p, -d, -u,
356 * --pragma, --before) are processed after this function.
357 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300358static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800359{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800360 preproc->init();
361 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300362 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800363}
364
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300365static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700366{
367 FILE *deps;
368 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700369 bool wmake = (quote_for_make == quote_for_wmake);
370 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800371 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700372
373 if (!list)
374 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700375
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700376 wrapstr = wmake ? " &\n " : " \\\n ";
377 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700378
379 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700380 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400381 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800382 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400383 return;
384 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400386 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700388
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700389 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800390 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700391 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400392 len = strlen(file);
393 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700394 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 linepos = 1;
396 }
397 fprintf(deps, " %s", file);
398 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700399 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700400 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700401 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700402
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800403 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700404 if (depend_emit_phony) {
405 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700406 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700407 nasm_free(file);
408 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700409 }
410
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800411 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700412
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700413 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400414 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700415}
416
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700417/* Convert a struct tm to a POSIX-style time constant */
418static int64_t make_posix_time(const struct tm *tm)
419{
420 int64_t t;
421 int64_t y = tm->tm_year;
422
423 /* See IEEE 1003.1:2004, section 4.14 */
424
425 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
426 t += tm->tm_yday;
427 t *= 24;
428 t += tm->tm_hour;
429 t *= 60;
430 t += tm->tm_min;
431 t *= 60;
432 t += tm->tm_sec;
433
434 return t;
435}
436
437static void timestamp(void)
438{
439 struct compile_time * const oct = &official_compile_time;
440 const struct tm *tp, *best_gm;
441
442 time(&oct->t);
443
444 best_gm = NULL;
445
446 tp = localtime(&oct->t);
447 if (tp) {
448 oct->local = *tp;
449 best_gm = &oct->local;
450 oct->have_local = true;
451 }
452
453 tp = gmtime(&oct->t);
454 if (tp) {
455 oct->gm = *tp;
456 best_gm = &oct->gm;
457 oct->have_gm = true;
458 if (!oct->have_local)
459 oct->local = oct->gm;
460 } else {
461 oct->gm = oct->local;
462 }
463
464 if (best_gm) {
465 oct->posix = make_posix_time(best_gm);
466 oct->have_posix = true;
467 }
468}
469
H. Peter Anvin038d8612007-04-12 16:54:50 +0000470int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471{
H. Peter Anvin322bee02019-08-10 01:38:06 -0700472 _progname = argv[0];
473 if (!_progname || !_progname[0])
474 _progname = "nasm";
475
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700476 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800477
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800478 error_file = stderr;
479
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800480 iflag_set_default_cpu(&cpu);
481 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400482
H. Peter Anvin322bee02019-08-10 01:38:06 -0700483 set_default_limits();
484
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800485 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300486
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800487 _pass_type = PASS_INIT;
488 _passn = 0;
489
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700490 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800491 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492
H. Peter Anvin13506202018-11-28 14:55:58 -0800493 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700494 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700495
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700496 /*
497 * We must call init_labels() before the command line parsing,
498 * because we may be setting prefixes/suffixes from the command
499 * line.
500 */
501 init_labels();
502
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000503 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000504 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400507 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000508
H. Peter Anvin55568c12016-10-03 19:46:49 -0700509 parse_cmdline(argc, argv, 1);
510 if (terminate_after_phase) {
511 if (want_usage)
512 usage();
513 return 1;
514 }
515
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800516 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800517 if (!using_debug_info) {
518 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800519 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800520 } else if (!debug_format) {
521 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300522 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800523 } else {
524 dfmt = dfmt_find(ofmt, debug_format);
525 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800526 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800527 debug_format, ofmt->shortname);
528 }
529 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000530
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300531 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800532
533 parse_cmdline(argc, argv, 2);
534 if (terminate_after_phase) {
535 if (want_usage)
536 usage();
537 return 1;
538 }
539
540 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800541 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000542
H. Peter Anvin34754622018-11-28 12:36:53 -0800543 /* Dependency filename if we are also doing other things */
544 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
545 if (outname)
546 depend_file = nasm_strcat(outname, ".d");
547 else
548 depend_file = filename_set_extension(inname, ".d");
549 }
550
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300551 /*
552 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800553 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300554 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300555 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800556 if (!outname && !(operating_mode & OP_PREPROCESS)) {
557 outname = filename_set_extension(inname, ofmt->extension);
558 if (!strcmp(outname, inname)) {
559 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800560 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 -0800561 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300562 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800563
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800564 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700565
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700566 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400567 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700568
H. Peter Anvin34754622018-11-28 12:36:53 -0800569 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000570 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700571
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400572 if (depend_missing_ok)
573 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700574
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800575 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 while ((line = preproc->getline()))
578 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800579 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800580 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400581 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700583 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000584 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000586
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800587 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700588 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800590 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 } else
592 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700594 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000595
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800596 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
597 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800598
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 while ((line = preproc->getline())) {
600 /*
601 * We generate %line directives if needed for later programs
602 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000603 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 int altline = src_get(&linnum, &file_name);
605 if (altline) {
606 if (altline == 1 && lineinc == 1)
607 nasm_fputs("", ofile);
608 else {
609 lineinc = (altline != -1 || lineinc != 1);
610 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000611 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 file_name);
613 }
614 prior_linnum = linnum;
615 }
616 nasm_fputs(line, ofile);
617 nasm_free(line);
618 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800619 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800620 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 if (ofile)
622 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700623 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400625 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400626 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400628 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700629 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400630 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800631 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400633 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400634 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700636 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200637
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400638 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800639 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400640 cleanup_labels();
641 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800642 if (ferror(ofile))
643 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400644 }
645
646 if (ofile) {
647 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700648 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400649 remove(outname);
650 ofile = NULL;
651 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000652 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000653
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800654 preproc->cleanup_session();
655
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700656 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400657 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700658
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000659 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000661
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 raa_free(offsets);
663 saa_free(forwrefs);
664 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000665 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700666 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800667 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000668
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700669 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000670}
671
H. Peter Anvineba20a72002-04-30 20:53:55 +0000672/*
673 * Get a parameter for a command line option.
674 * First arg must be in the form of e.g. -f...
675 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800676static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000677{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800678 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400679 if (p[2]) /* the parameter's in the option */
680 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800682 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800685 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686 return NULL;
687}
688
H. Peter Anvindc242712007-11-18 11:55:10 -0800689/*
690 * Copy a filename
691 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800692static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800693{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800694 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700695 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800696
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800697 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800698}
699
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700700/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700701 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700702 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700703static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700704{
705 const char *p;
706 char *os, *q;
707
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400708 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700709 size_t nbs = 0;
710
711 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400712 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700713
714 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400715 switch (*p) {
716 case ' ':
717 case '\t':
718 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
719 n += nbs + 2;
720 nbs = 0;
721 break;
722 case '$':
723 case '#':
724 nbs = 0;
725 n += 2;
726 break;
727 case '\\':
728 nbs++;
729 n++;
730 break;
731 default:
732 nbs = 0;
733 n++;
734 break;
735 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700736 }
737
738 /* Convert N backslashes at the end of filename to 2N backslashes */
739 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400740 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700741
742 os = q = nasm_malloc(n);
743
744 nbs = 0;
745 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400746 switch (*p) {
747 case ' ':
748 case '\t':
749 while (nbs--)
750 *q++ = '\\';
751 *q++ = '\\';
752 *q++ = *p;
753 break;
754 case '$':
755 *q++ = *p;
756 *q++ = *p;
757 nbs = 0;
758 break;
759 case '#':
760 *q++ = '\\';
761 *q++ = *p;
762 nbs = 0;
763 break;
764 case '\\':
765 *q++ = *p;
766 nbs++;
767 break;
768 default:
769 *q++ = *p;
770 nbs = 0;
771 break;
772 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700773 }
774 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400775 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700776
777 *q = '\0';
778
779 return os;
780}
781
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700782/*
783 * Convert a string to a Watcom make-safe form
784 */
785static char *quote_for_wmake(const char *str)
786{
787 const char *p;
788 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700789 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700790
791 size_t n = 1; /* Terminating zero */
792
793 if (!str)
794 return NULL;
795
796 for (p = str; *p; p++) {
797 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700798 case ' ':
799 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700800 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700801 quote = true;
802 n++;
803 break;
804 case '\"':
805 quote = true;
806 n += 2;
807 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700808 case '$':
809 case '#':
810 n += 2;
811 break;
812 default:
813 n++;
814 break;
815 }
816 }
817
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700818 if (quote)
819 n += 2;
820
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700821 os = q = nasm_malloc(n);
822
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700823 if (quote)
824 *q++ = '\"';
825
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700826 for (p = str; *p; p++) {
827 switch (*p) {
828 case '$':
829 case '#':
830 *q++ = '$';
831 *q++ = *p;
832 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700833 case '\"':
834 *q++ = *p;
835 *q++ = *p;
836 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700837 default:
838 *q++ = *p;
839 break;
840 }
841 }
842
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700843 if (quote)
844 *q++ = '\"';
845
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700846 *q = '\0';
847
848 return os;
849}
850
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100851enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800852 OPT_BOGUS,
853 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700854 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800855 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700856 OPT_MANGLE,
857 OPT_INCLUDE,
858 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700859 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700860 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800861 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700862 OPT_NO_LINE,
863 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100864};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700865enum need_arg {
866 ARG_NO,
867 ARG_YES,
868 ARG_MAYBE
869};
870
H. Peter Anvin3366e312018-02-07 14:14:36 -0800871struct textargs {
872 const char *label;
873 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700874 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700875 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800876};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800877static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700878 {"v", OPT_VERSION, ARG_NO, 0},
879 {"version", OPT_VERSION, ARG_NO, 0},
880 {"help", OPT_HELP, ARG_NO, 0},
881 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
882 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
883 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
884 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
885 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
886 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
887 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
888 {"include", OPT_INCLUDE, ARG_YES, 0},
889 {"pragma", OPT_PRAGMA, ARG_YES, 0},
890 {"before", OPT_BEFORE, ARG_YES, 0},
891 {"limit-", OPT_LIMIT, ARG_YES, 0},
892 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
893 {"no-line", OPT_NO_LINE, ARG_NO, 0},
894 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
895 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000896};
897
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400898static void show_version(void)
899{
900 printf("NASM version %s compiled on %s%s\n",
901 nasm_version, nasm_date, nasm_compile_options);
902 exit(0);
903}
904
H. Peter Anvin423e3812007-11-15 17:12:29 -0800905static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000907{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000908 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800909 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000910
911 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800912 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000913
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700915 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 /* These parameters take values */
917 if (!(param = get_param(p, q, &advance)))
918 return advance;
919 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 switch (p[1]) {
922 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700923 if (pass == 1)
924 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700926
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700928 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800929 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700931
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700933 if (pass == 1) {
934 ofmt = ofmt_find(param, &ofmt_alias);
935 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800936 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 }
939 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700940
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800942 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700943 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700944
H. Peter Anvin55568c12016-10-03 19:46:49 -0700945 if (!*param) {
946 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300947 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700948 } else {
949 while (*param) {
950 switch (*param) {
951 case '0': case '1': case '2': case '3': case '4':
952 case '5': case '6': case '7': case '8': case '9':
953 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700954
Chang S. Baea5786342018-08-15 23:22:21 +0300955 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
956 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300958 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700959 else
Chang S. Baea5786342018-08-15 23:22:21 +0300960 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700961 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700962
H. Peter Anvin55568c12016-10-03 19:46:49 -0700963 case 'v':
964 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400965 param++;
966 opt_verbose_info = true;
967 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700968
H. Peter Anvin55568c12016-10-03 19:46:49 -0700969 case 'x':
970 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300971 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700973
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700975 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700976 *param);
977 break;
978 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400979 }
Chang S. Baea5786342018-08-15 23:22:21 +0300980 if (optimizing.level > MAX_OPTIMIZE)
981 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400982 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 }
984 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800985
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400986 case 'p': /* pre-include */
987 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700988 if (pass == 2)
989 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400990 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800991
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400992 case 'd': /* pre-define */
993 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 2)
995 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800997
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400998 case 'u': /* un-define */
999 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001000 if (pass == 2)
1001 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001002 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001003
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001004 case 'i': /* include search path */
1005 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001006 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001007 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001009
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001012 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001013 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001014
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001015 case 'L': /* listing options */
1016 if (pass == 2) {
1017 while (*param) {
1018 unsigned int p = *param - '@';
1019 if (p <= 63)
1020 list_options |= (UINT64_C(1) << p);
1021 param++;
1022 }
H. Peter Anvin322bee02019-08-10 01:38:06 -07001023 if (list_options & (UINT64_C(1) << ('p' - '@'))) {
1024 /* Do listings for every pass */
1025 active_list_options = list_options;
1026 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001027 }
1028 break;
1029
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001030 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001031 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001032 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001033 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001034
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001035 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001036 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 using_debug_info = true;
1038 debug_format = param;
1039 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001040 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001041
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001042 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001044 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1045 errfmt = &errfmt_msvc;
1046 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1047 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001049 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001054 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001055 using_debug_info = true;
1056 if (p[2])
1057 debug_format = nasm_skip_spaces(p + 2);
1058 }
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 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001062 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001063 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001067 /* legacy option */
1068 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 exit(0);
1070 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001073 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001074 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001075 nasm_ctype_tasm_mode();
1076 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001078
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001080 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001082
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001083 case 'e': /* preprocess only */
1084 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001085 if (pass == 1)
1086 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001088
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001089 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001090 if (pass == 1)
1091 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001093
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001094 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001095 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001096 if (pass == 2)
1097 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001098 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001101 if (pass == 1) {
1102 switch (p[2]) {
1103 case 'W':
1104 quote_for_make = quote_for_wmake;
1105 break;
1106 case 'D':
1107 case 'F':
1108 case 'T':
1109 case 'Q':
1110 advance = true;
1111 break;
1112 default:
1113 break;
1114 }
1115 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001116 switch (p[2]) {
1117 case 0:
1118 operating_mode = OP_DEPEND;
1119 break;
1120 case 'G':
1121 operating_mode = OP_DEPEND;
1122 depend_missing_ok = true;
1123 break;
1124 case 'P':
1125 depend_emit_phony = true;
1126 break;
1127 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001128 operating_mode |= OP_DEPEND;
1129 if (q && (q[0] != '-' || q[1] == '\0')) {
1130 depend_file = q;
1131 advance = true;
1132 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001133 break;
1134 case 'F':
1135 depend_file = q;
1136 advance = true;
1137 break;
1138 case 'T':
1139 depend_target = q;
1140 advance = true;
1141 break;
1142 case 'Q':
1143 depend_target = quote_for_make(q);
1144 advance = true;
1145 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001146 case 'W':
1147 /* handled in pass 1 */
1148 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001149 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001150 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001151 break;
1152 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001153 }
1154 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001155 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001156 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001157 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 case '-':
1161 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001162 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001163 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001164 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001165 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001166
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001167 p += 2;
1168
1169 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001170 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 break;
1172 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001173
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001174 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001175 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001176 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001177 olen = strlen(tx->label);
1178
1179 if (olen > plen)
1180 continue;
1181
1182 if (nasm_memicmp(p, tx->label, olen))
1183 continue;
1184
1185 if (tx->label[olen-1] == '-')
1186 break; /* Incomplete option */
1187
1188 if (!p[olen] || p[olen] == '=')
1189 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001191
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001192 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001193 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001194 }
1195
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001196 opt = tx->opt;
1197
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001198 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001199 if (param)
1200 *param++ = '\0';
1201
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001202 switch (tx->need_arg) {
1203 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001204 if (!param) {
1205 param = q;
1206 advance = true;
1207 }
1208
1209 /* Note: a null string is a valid parameter */
1210 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001211 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001212 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001214 break;
1215
1216 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001217 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001218 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001219 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001220 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001221 break;
1222
1223 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1224 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001225 }
1226
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001227 switch (opt) {
1228 case OPT_BOGUS:
1229 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001230 case OPT_VERSION:
1231 show_version();
1232 break;
1233 case OPT_ABORT_ON_PANIC:
1234 abort_on_panic = true;
1235 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001236 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001237 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001238 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001239 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001240 case OPT_INCLUDE:
1241 if (pass == 2)
1242 preproc->pre_include(q);
1243 break;
1244 case OPT_PRAGMA:
1245 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001246 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001247 break;
1248 case OPT_BEFORE:
1249 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001250 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001251 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001252 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001253 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001254 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001255 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001256 case OPT_KEEP_ALL:
1257 keep_all = true;
1258 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001259 case OPT_NO_LINE:
1260 pp_noline = true;
1261 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001262 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001263 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001264 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001265 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001266 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001267 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001268 default:
1269 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001271
1272 if (eqsave)
1273 *eqsave = '='; /* Restore = argument separator */
1274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 break;
1276 }
1277
1278 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001279 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 break;
1281 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001282 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001283 /* In theory we could allow multiple input files... */
1284 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001285 }
1286
1287 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001288}
1289
H. Peter Anvineba20a72002-04-30 20:53:55 +00001290#define ARG_BUF_DELTA 128
1291
H. Peter Anvin55568c12016-10-03 19:46:49 -07001292static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001293{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001294 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295 int bufsize, prevargsize;
1296
1297 bufsize = prevargsize = ARG_BUF_DELTA;
1298 buffer = nasm_malloc(ARG_BUF_DELTA);
1299 prevarg = nasm_malloc(ARG_BUF_DELTA);
1300 prevarg[0] = '\0';
1301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 p = buffer;
1304 while (1) { /* Loop to handle long lines */
1305 q = fgets(p, bufsize - (p - buffer), rfile);
1306 if (!q)
1307 break;
1308 p += strlen(p);
1309 if (p > buffer && p[-1] == '\n')
1310 break;
1311 if (p - buffer > bufsize - 10) {
1312 int offset;
1313 offset = p - buffer;
1314 bufsize += ARG_BUF_DELTA;
1315 buffer = nasm_realloc(buffer, bufsize);
1316 p = buffer + offset;
1317 }
1318 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001319
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 if (!q && p == buffer) {
1321 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001322 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 nasm_free(buffer);
1324 nasm_free(prevarg);
1325 return;
1326 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 /*
1329 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1330 * them are present at the end of the line.
1331 */
1332 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001333
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001334 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001337 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338
H. Peter Anvin55568c12016-10-03 19:46:49 -07001339 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341
Charles Crayne192d5b52007-10-18 19:02:42 -07001342 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 prevargsize += ARG_BUF_DELTA;
1344 prevarg = nasm_realloc(prevarg, prevargsize);
1345 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001346 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347 }
1348}
1349
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001350/* Function to process args from a string of args, rather than the
1351 * argv array. Used by the environment variable and response file
1352 * processing.
1353 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001354static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001356 char *p, *q, *arg, *prevarg;
1357 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001358
1359 p = args;
1360 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001362 arg = NULL;
1363 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 q = p;
1365 while (*p && *p != separator)
1366 p++;
1367 while (*p == separator)
1368 *p++ = '\0';
1369 prevarg = arg;
1370 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001371 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001373 }
1374 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001375 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001376}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001377
H. Peter Anvin55568c12016-10-03 19:46:49 -07001378static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001379{
1380 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001381 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001382 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001383 perror(file);
1384 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001385 }
1386 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001387 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001388 }
1389 fclose(f);
1390}
1391
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001393{
1394 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001395 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001396
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001397 /*
1398 * Initialize all the warnings to their default state, including
1399 * warning index 0 used for "always on".
1400 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001401 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001402
1403 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001404 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001405 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001406 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001407 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001409 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001411 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001412
1413 /*
1414 * Now process the actual command line.
1415 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001417 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 argv++;
1419 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001420 /*
1421 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 * arguments like the environment variable. This allows us
1423 * to have multiple arguments on a single line, which is
1424 * different to the -@resp file processing below for regular
1425 * NASM.
1426 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001427 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 argc--;
1429 argv++;
1430 }
1431 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001432 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001433 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001434 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001435 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001436 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001438 } else {
1439 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1440 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 }
1442 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001443 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001444 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001445 }
1446
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001447 /*
1448 * Look for basic command line typos. This definitely doesn't
1449 * catch all errors, but it might help cases of fumbled fingers.
1450 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001451 if (pass != 2)
1452 return;
1453
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001454 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001455 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001456 else if ((errname && !strcmp(inname, errname)) ||
1457 (outname && !strcmp(inname, outname)) ||
1458 (listname && !strcmp(inname, listname)) ||
1459 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001460 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001461
1462 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001463 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001464 if (!error_file) {
1465 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001466 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001467 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001468 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001469}
1470
H. Peter Anvin29651542018-12-18 19:14:40 -08001471static void forward_refs(insn *instruction)
1472{
1473 int i;
1474 struct forwrefinfo *fwinf;
1475
1476 instruction->forw_ref = false;
1477
1478 if (!optimizing.level)
1479 return; /* For -O0 don't bother */
1480
1481 if (!forwref)
1482 return;
1483
1484 if (forwref->lineno != globallineno)
1485 return;
1486
1487 instruction->forw_ref = true;
1488 do {
1489 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1490 forwref = saa_rstruct(forwrefs);
1491 } while (forwref && forwref->lineno == globallineno);
1492
1493 if (!pass_first())
1494 return;
1495
1496 for (i = 0; i < instruction->operands; i++) {
1497 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1498 fwinf = saa_wstruct(forwrefs);
1499 fwinf->lineno = globallineno;
1500 fwinf->operand = i;
1501 }
1502 }
1503}
1504
1505static void process_insn(insn *instruction)
1506{
1507 int32_t n;
1508 int64_t l;
1509
1510 if (!instruction->times)
1511 return; /* Nothing to do... */
1512
1513 nasm_assert(instruction->times > 0);
1514
1515 /*
1516 * NOTE: insn_size() can change instruction->times
1517 * (usually to 1) when called.
1518 */
1519 if (!pass_final()) {
1520 for (n = 1; n <= instruction->times; n++) {
1521 l = insn_size(location.segment, location.offset,
1522 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001523
1524 if (list_option('p')) {
1525 if (l > 0) {
1526 struct out_data dummy;
1527 memset(&dummy, 0, sizeof dummy);
1528 dummy.type = OUT_RESERVE;
1529 dummy.offset = location.offset;
1530 dummy.size = l;
1531 lfmt->output(&dummy);
1532 }
1533 }
1534
1535 /* l == -1 -> invalid instruction */
1536 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001537 increment_offset(l);
1538 }
1539 } else {
1540 l = assemble(location.segment, location.offset,
1541 globalbits, instruction);
1542 /* We can't get an invalid instruction here */
1543 increment_offset(l);
1544
1545 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001546 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001547 for (n = 2; n <= instruction->times; n++) {
1548 l = assemble(location.segment, location.offset,
1549 globalbits, instruction);
1550 increment_offset(l);
1551 }
1552 lfmt->downlevel(LIST_TIMES);
1553 }
1554 }
1555}
1556
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001557static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001558{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001559 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001561 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001562 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001563
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001564 switch (cmd_sb) {
1565 case 16:
1566 break;
1567 case 32:
1568 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001569 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001570 break;
1571 case 64:
1572 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001573 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001574 break;
1575 default:
1576 panic();
1577 break;
1578 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001579
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001580 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001581
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001582 if (listname && !keep_all) {
1583 /* Remove the list file in case we die before the output pass */
1584 remove(listname);
1585 }
1586
1587 while (!terminate_after_phase && !pass_final()) {
1588 _passn++;
1589 if (pass_type() != PASS_OPT || !global_offset_changed)
1590 _pass_type++;
1591 global_offset_changed = 0;
1592
1593 /*
1594 * Create a warning buffer list unless we are in
1595 * pass 2 (everything will be emitted immediately in pass 2.)
1596 */
1597 if (warn_list) {
1598 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001599 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001600 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001601
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001602 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001603 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001604
H. Peter Anvincac0b192017-03-28 16:12:30 -07001605 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 cpu = cmd_cpu;
H. Peter Anvin322bee02019-08-10 01:38:06 -07001607 if (pass_final() || list_option('p')) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001608 active_list_options = list_options;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001609 lfmt->init(listname);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001610 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001611
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001612 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001613 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 saa_rewind(forwrefs);
1615 forwref = saa_rstruct(forwrefs);
1616 raa_free(offsets);
1617 offsets = raa_init();
1618 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001619 location.segment = NO_SEG;
1620 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001621 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001622 location.known = true;
1623 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001624 switch_segment(ofmt->section(NULL, &globalbits));
1625 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001626
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001628
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001630 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001631 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001632 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001634 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001635 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001636 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001637 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001638 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001639 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001640
H. Peter Anvinc7131682017-03-07 17:45:01 -08001641 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001642 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001643 forward_refs(&output_ins);
1644 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001645 cleanup_insn(&output_ins);
1646
1647 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001650
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001651 preproc->cleanup_pass();
1652
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001653 if (global_offset_changed) {
1654 switch (pass_type()) {
1655 case PASS_OPT:
1656 /*
1657 * This is the only pass type that can be executed more
1658 * than once, and therefore has the ability to stall.
1659 */
1660 if (global_offset_changed < prev_offset_changed) {
1661 prev_offset_changed = global_offset_changed;
1662 stall_count = 0;
1663 } else {
1664 stall_count++;
1665 }
1666
1667 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1668 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1669 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001670 nasm_nonfatalf(ERR_UNDEAD,
1671 "unable to find valid values for all labels "
1672 "after %"PRId64" passes; "
1673 "stalled for %"PRId64", giving up.",
1674 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001675 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001676 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001677 }
1678 break;
1679
1680 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001681 /*!
1682 *!phase [off] phase error during stabilization
1683 *! warns about symbols having changed values during
1684 *! the second-to-last assembly pass. This is not
1685 *! inherently fatal, but may be a source of bugs.
1686 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001687 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1688 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001689 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001690 break;
1691
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001692 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001693 nasm_nonfatalf(ERR_UNDEAD,
1694 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001695 break;
1696
1697 default:
1698 /* This is normal, we'll keep going... */
1699 break;
1700 }
1701 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001702
1703 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001704 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001705
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001706 if (opt_verbose_info && pass_final()) {
1707 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001708 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001709 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001710
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001711 strlist_free(&warn_list);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001712 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001713}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001714
Ed Berosetfa771012002-06-09 20:56:40 +00001715/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001716 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001717 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001718static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001719{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001720 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001721
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001722 if ((severity & ERR_MASK) >= ERR_FATAL)
1723 return 0; /* Fatal errors are never suppressible */
1724
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001725 /* Warnings MUST HAVE a warning category specifier! */
1726 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001727
1728 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001729 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001730
1731 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001732}
1733
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001734static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001735{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001736 errflags type = severity & ERR_MASK;
1737
H. Peter Anvin8f622462017-04-02 19:02:29 -07001738 /*
1739 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001740 * We can never skip fatal errors as by definition they cannot be
1741 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001742 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001743 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001744 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001745
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001746 /*
1747 * ERR_LISTMSG messages are always skipped; the list file
1748 * receives them anyway as this function is not consulted
1749 * for sending to the list file.
1750 */
1751 if (type == ERR_LISTMSG)
1752 return true;
1753
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001754 /* This message not applicable unless pass_final */
1755 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001756}
1757
Ed Berosetfa771012002-06-09 20:56:40 +00001758/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001759 * check for suppressed message (usually warnings or notes)
1760 *
1761 * @param severity the severity of the warning or error
1762 * @return true if we should abort error/warning printing
1763 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001764static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001765{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001766 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001767 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001768 return false;
1769
1770 /* This error/warning is pointless if we are dead anyway */
1771 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1772 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001773
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001774 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1775}
1776
1777/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001778 * Return the true error type (the ERR_MASK part) of the given
1779 * severity, accounting for warnings that may need to be promoted to
1780 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001781 *
1782 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001783 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001784 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001785static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001786{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001787 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1788 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001789
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001790 type = severity & ERR_MASK;
1791
1792 /* Promote warning to error? */
1793 if (type == ERR_WARNING) {
1794 uint8_t state = warning_state[warn_index(severity)];
1795 if ((state & warn_is_err) == warn_is_err)
1796 type = ERR_NONFATAL;
1797 }
1798
1799 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001800}
1801
1802/**
Ed Berosetfa771012002-06-09 20:56:40 +00001803 * common error reporting
1804 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001805 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001806 * specific error message to error_file and may or may not return. It
1807 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001808 *
1809 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001810 * @param fmt the printf style format string
1811 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001812static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001813{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001814 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001815 char warnsuf[64];
1816 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001817 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001818 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001819 const char *currentfile = NULL;
1820 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001821 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001822 ";;; ", "debug: ", "info: ", "warning: ",
1823 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001824 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001825
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001826 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001827 return;
1828
1829 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001830 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001831 if (!currentfile) {
1832 currentfile = currentfile ? currentfile :
1833 inname && inname[0] ? inname :
1834 outname && outname[0] ? outname :
1835 NULL;
1836 lineno = 0;
1837 }
1838 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001839
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001840 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001841 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001842 else
1843 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001844
1845 vsnprintf(msg, sizeof msg, fmt, args);
1846 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001847 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1848 /*
1849 * It's a warning without ERR_HERE defined, and we are not already
1850 * unwinding the macros that led us here.
1851 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001852 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001853 (true_type >= ERR_NONFATAL) ? "error=" : "",
1854 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001855 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001856
H. Peter Anvinddb29062018-12-11 00:06:29 -08001857 *linestr = 0;
1858 if (lineno) {
1859 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1860 errfmt->beforeline, lineno, errfmt->afterline);
1861 }
1862
H. Peter Anvin77016c82018-12-10 22:29:49 -08001863 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001864 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001865 const char *here = "";
1866
1867 if (severity & ERR_HERE)
1868 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001869
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001870 if (warn_list && true_type < ERR_NONFATAL &&
1871 !(pass_first() && (severity & ERR_PASS1)))
1872 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001873 /*
1874 * Buffer up warnings until we either get an error
1875 * or we are on the code-generation pass.
1876 */
1877 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1878 file, linestr, errfmt->beforemsg,
1879 pfx, msg, here, warnsuf);
1880 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001881 /*
1882 * If we have buffered warnings, and this is a non-warning,
1883 * output them now.
1884 */
1885 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001886 strlist_write(warn_list, "\n", error_file);
1887 strlist_free(&warn_list);
1888 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001889
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001890 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1891 file, linestr, errfmt->beforemsg,
1892 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001893
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001894 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001895 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001896
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001897 /* Are we recursing from error_list_macros? */
1898 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001899 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001900
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001901 /*
1902 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001903 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001904 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001905 if (severity & ERR_HERE) {
1906 if (lineno)
1907 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1908 pfx, msg, currentfile, lineno, warnsuf);
1909 else if (currentfile)
1910 lfmt->error(severity, "%s%s in file %s%s",
1911 pfx, msg, currentfile, warnsuf);
1912 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001913 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001914 pfx, msg, warnsuf);
1915 } else {
1916 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1917 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001918
H. Peter Anvin8f622462017-04-02 19:02:29 -07001919 if (skip_this_pass(severity))
1920 return;
1921
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001922 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001923 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001924
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001925 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1926 if (!(severity & ERR_HERE))
1927 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001928
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001929 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001930 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001932 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001933 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001934 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001935 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001937 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 break;
1939 case ERR_FATAL:
1940 if (ofile) {
1941 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001942 if (!keep_all)
1943 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001944 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 }
1946 if (want_usage)
1947 usage();
1948 exit(1); /* instantly die */
1949 break; /* placate silly compilers */
1950 case ERR_PANIC:
1951 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001952
1953 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001954 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001955
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001956 if (ofile) {
1957 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001958 if (!keep_all)
1959 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001960 ofile = NULL;
1961 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 exit(3);
1963 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001964 default:
1965 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001966 }
1967}
1968
H. Peter Anvin734b1882002-04-30 21:01:08 +00001969static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001970{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001971 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001972}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001973
H. Peter Anvin322bee02019-08-10 01:38:06 -07001974static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001975{
1976 int i;
1977
H. Peter Anvin322bee02019-08-10 01:38:06 -07001978 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001979 "Usage: %s [-@ response_file] [options...] [--] filename\n"
1980 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07001981 _progname, _progname);
1982 fputs(
1983 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001984 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001985 "\n"
1986 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07001987 " -v (or --v) print the NASM version number and exit\n"
1988 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07001989 "\n"
1990 " -o outfile write output to outfile\n"
1991 " --keep-all output files will not be removed even if an error happens\n"
1992 "\n"
1993 " -Xformat specifiy error reporting format (gnu or vc)\n"
1994 " -s redirect error messages to stdout\n"
1995 " -Zfile redirect error messages to file\n"
1996 "\n"
1997 " -M generate Makefile dependencies on stdout\n"
1998 " -MG d:o, missing files assumed generated\n"
1999 " -MF file set Makefile dependency file\n"
2000 " -MD file assemble and generate dependencies\n"
2001 " -MT file dependency target name\n"
2002 " -MQ file dependency target name (quoted)\n"
2003 " -MP emit phony targets\n"
2004 "\n"
2005 " -f format select output file format\n"
2006 , out);
2007 ofmt_list(ofmt, out);
2008 fputs(
2009 "\n"
2010 " -g generate debugging information\n"
2011 " -F format select a debugging format (output format dependent)\n"
2012 " -gformat same as -g -F format\n"
2013 , out);
2014 dfmt_list(out);
2015 fputs(
2016 "\n"
2017 " -l listfile write listing to a list file\n"
2018 " -Lflags... add optional information to the list file\n"
2019 " -Ld show byte and repeat counts in decimal, not hex\n"
2020 " -Le show the preprocessed output\n"
2021 " -Lm show all single-line macro definitions\n"
2022 " -Lp output a list file every pass, in case of errors\n"
2023 "\n"
2024 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2025 " -O0 no optimization\n"
2026 " -O1 minimal optimization\n"
2027 " -Ox multipass optimization (default)\n"
2028 " -Ov display the number of passes executed at the end\n"
2029 " -t assemble in limited SciTech TASM compatible mode\n"
2030 "\n"
2031 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2032 " -a don't preprocess (assemble only)\n"
2033 " -Ipath add a pathname to the include file path\n"
2034 " -Pfile pre-include a file (also --include)\n"
2035 " -Dmacro[=str] pre-define a macro\n"
2036 " -Umacro undefine a macro\n"
2037 " --pragma str pre-executes a specific %%pragma\n"
2038 " --before str add line (usually a preprocessor statement) before the input\n"
2039 " --no-line ignore %line directives in input\n"
2040 "\n"
2041 " --prefix str prepend the given string to the names of all extern,\n"
2042 " common and global symbols (also --gprefix)\n"
2043 " --suffix str append the given string to the names of all extern,\n"
2044 " common and global symbols (also --gprefix)\n"
2045 " --lprefix str prepend the given string to local symbols\n"
2046 " --lpostfix str append the given string to local symbols\n"
2047 "\n"
2048 " -w+x enable warning x (also -Wx)\n"
2049 " -w-x disable warning x (also -Wno-x)\n"
2050 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2051 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2052 , out);
2053
2054 fprintf(out, " %-20s %s\n",
2055 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2056
2057 for (i = 1; i < WARN_IDX_ALL; i++) {
2058 const char *me = warning_name[i];
2059 const char *prev = warning_name[i-1];
2060 const char *next = warning_name[i+1];
2061
2062 if (prev) {
2063 int prev_len = strlen(prev);
2064 const char *dash = me;
2065
2066 while ((dash = strchr(dash+1, '-'))) {
2067 int prefix_len = dash - me; /* Not including final dash */
2068 if (strncmp(next, me, prefix_len+1)) {
2069 /* Only one or last option with this prefix */
2070 break;
2071 }
2072 if (prefix_len >= prev_len ||
2073 strncmp(prev, me, prefix_len) ||
2074 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2075 /* This prefix is different from the previous option */
2076 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2077 prefix_len, me, prefix_len+1, me);
2078 }
2079 }
2080 }
2081
2082 fprintf(out, " %-20s %s%s\n",
2083 warning_name[i], warning_help[i],
2084 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2085 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2086 }
2087
2088 fputs(
2089 "\n"
2090 " --limit-X val set execution limit X\n"
2091 , out);
2092
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002093
2094 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002095 fprintf(out, " %-20s %s [",
2096 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002097 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002098 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002099 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002100 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002101 }
2102 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002103}