blob: 51a74dfc38e775b581b55262eb565eea44bef569 [file] [log] [blame]
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter 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);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070078static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
H. Peter Anvin77016c82018-12-10 22:29:49 -080080struct error_format {
81 const char *beforeline; /* Before line number, if present */
82 const char *afterline; /* After line number, if present */
83 const char *beforemsg; /* Before actual message */
84};
85
86static const struct error_format errfmt_gnu = { ":", "", ": " };
87static const struct error_format errfmt_msvc = { "(", ")", " : " };
88static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080089static struct strlist *warn_list;
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -070090static struct nasm_errhold *errhold_stack;
H. Peter Anvin77016c82018-12-10 22:29:49 -080091
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070092unsigned int debug_nasm; /* Debugging messages? */
93
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080094static bool using_debug_info, opt_verbose_info;
95static const char *debug_format;
96
H. Peter Anvin3366e312018-02-07 14:14:36 -080097#ifndef ABORT_ON_PANIC
98# define ABORT_ON_PANIC 0
99#endif
100static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700101static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800102
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700103bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800104enum pass_type _pass_type;
105const char * const _pass_types[] =
106{
107 "init", "first", "optimize", "stabilize", "final"
108};
109int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000110int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800111int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000112
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700113struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800114
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800115const char *inname;
116const char *outname;
117static const char *listname;
118static const char *errname;
119
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700120static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700121
H. Peter Anvin338656c2016-02-17 20:59:22 -0800122const struct ofmt *ofmt = &OF_DEFAULT;
123const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400124const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800126FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000127
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400128FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300129struct optimization optimizing =
130 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100131static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400132
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800133iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400134static iflag_t cmd_cpu;
135
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800136struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800137bool in_absolute; /* Flag we are in ABSOLUTE seg */
138struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000141
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700143static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144
H. Peter Anvine7469712016-02-18 02:20:59 -0800145static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300146static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800147bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400148
H. Peter Anvin34754622018-11-28 12:36:53 -0800149#define OP_NORMAL (1U << 0)
150#define OP_PREPROCESS (1U << 1)
151#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400152
153static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400154
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700155/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700156static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700157static bool depend_missing_ok = false;
158static const char *depend_target = NULL;
159static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300160struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000161
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700162static bool want_usage;
163static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800164bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700166static char *quote_for_pmake(const char *str);
167static char *quote_for_wmake(const char *str);
168static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700169
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700170/*
171 * Execution limits that can be set via a command-line option or %pragma
172 */
173
H. Peter Anvin322bee02019-08-10 01:38:06 -0700174/*
175 * This is really unlimited; it would take far longer than the
176 * current age of the universe for this limit to be reached even on
177 * much faster CPUs than currently exist.
178*/
179#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180
H. Peter Anvin322bee02019-08-10 01:38:06 -0700181int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700182
183struct limit_info {
184 const char *name;
185 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700186 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700187};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700188/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700190 { "passes", "total number of passes", LIMIT_MAX_VAL },
191 { "stalled-passes", "number of passes without forward progress", 1000 },
192 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700193 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
194 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700195 { "rep", "%rep count", 1000000 },
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700196 { "eval", "expression evaluation descent", 8192 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700197 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700198};
199
H. Peter Anvin322bee02019-08-10 01:38:06 -0700200static void set_default_limits(void)
201{
202 int i;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700203 size_t rl;
204 int64_t new_limit;
205
H. Peter Anvin322bee02019-08-10 01:38:06 -0700206 for (i = 0; i <= LIMIT_MAX; i++)
207 nasm_limit[i] = limit_info[i].default_val;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700208
209 /*
210 * Try to set a sensible default value for the eval depth based
211 * on the limit of the stack size, if knowable...
212 */
213 rl = nasm_get_stack_size_limit();
214 new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
215 if (new_limit < nasm_limit[LIMIT_EVAL])
216 nasm_limit[LIMIT_EVAL] = new_limit;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700217}
218
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700219enum directive_result
220nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700221{
222 int i;
223 int64_t val;
224 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700225 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700226
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700227 if (!limit)
228 limit = "";
229 if (!valstr)
230 valstr = "";
231
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700232 for (i = 0; i <= LIMIT_MAX; i++) {
233 if (!nasm_stricmp(limit, limit_info[i].name))
234 break;
235 }
236 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800237 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800238 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700239 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700240 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700241 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700242 return DIRR_ERROR;
243 }
244
245 if (!nasm_stricmp(valstr, "unlimited")) {
246 val = LIMIT_MAX_VAL;
247 } else {
248 val = readnum(valstr, &rn_error);
249 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800250 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800251 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700252 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700253 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700254 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700255 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700256 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700257 if (val > LIMIT_MAX_VAL)
258 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700259 }
260
261 nasm_limit[i] = val;
262 return DIRR_OK;
263}
264
H. Peter Anvin892c4812018-05-30 14:43:46 -0700265int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400266{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700267 location.segment = segment;
268 if (segment == NO_SEG) {
269 location.offset = absolute.offset;
270 in_absolute = true;
271 } else {
272 location.offset = raa_read(offsets, segment);
273 in_absolute = false;
274 }
275 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400276}
277
278static void set_curr_offs(int64_t l_off)
279{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800280 if (in_absolute)
281 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400282 else
283 offsets = raa_write(offsets, location.segment, l_off);
284}
285
H. Peter Anvin892c4812018-05-30 14:43:46 -0700286static void increment_offset(int64_t delta)
287{
288 if (unlikely(delta == 0))
289 return;
290
291 location.offset += delta;
292 set_curr_offs(location.offset);
293}
294
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000295static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000296{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000297 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000298 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800299 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000300 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000302}
303
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800304/*
305 * Define system-defined macros that are not part of
306 * macros/standard.mac.
307 */
308static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800309{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700310 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800311 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800312
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700313 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700314 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400315 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700316 strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400317 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700318 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400319 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700320 strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400321 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800322 }
323
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700324 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700325 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400326 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700327 strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400328 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700329 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400330 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700331 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400332 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800333 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700334
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700335 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700336 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400337 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800338 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800339
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400340 /*
341 * In case if output format is defined by alias
342 * we have to put shortname of the alias itself here
343 * otherwise ABI backward compatibility gets broken.
344 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700345 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400346 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400347 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800348
349 /*
350 * Output-format specific macros.
351 */
352 if (ofmt->stdmac)
353 preproc->extra_stdmac(ofmt->stdmac);
354
355 /*
356 * Debug format, if any
357 */
358 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700359 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800360 preproc->pre_define(temp);
361 }
362}
363
364/*
365 * Initialize the preprocessor, set up the include path, and define
366 * the system-included macros. This is called between passes 1 and 2
367 * of parsing the command options; ofmt and dfmt are defined at this
368 * point.
369 *
370 * Command-line specified preprocessor directives (-p, -d, -u,
371 * --pragma, --before) are processed after this function.
372 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300373static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800374{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800375 preproc->init();
376 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300377 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800378}
379
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300380static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381{
382 FILE *deps;
383 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700384 bool wmake = (quote_for_make == quote_for_wmake);
385 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800386 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700387
388 if (!list)
389 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700390
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700391 wrapstr = wmake ? " &\n " : " \\\n ";
392 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700393
394 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700395 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400396 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800397 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400398 return;
399 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700400 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400401 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700402 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700403
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700404 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800405 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700406 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400407 len = strlen(file);
408 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700409 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400410 linepos = 1;
411 }
412 fprintf(deps, " %s", file);
413 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700414 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700415 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700416 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700417
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800418 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700419 if (depend_emit_phony) {
420 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700421 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700422 nasm_free(file);
423 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700424 }
425
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800426 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700427
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700428 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400429 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700430}
431
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700432/* Convert a struct tm to a POSIX-style time constant */
433static int64_t make_posix_time(const struct tm *tm)
434{
435 int64_t t;
436 int64_t y = tm->tm_year;
437
438 /* See IEEE 1003.1:2004, section 4.14 */
439
440 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
441 t += tm->tm_yday;
442 t *= 24;
443 t += tm->tm_hour;
444 t *= 60;
445 t += tm->tm_min;
446 t *= 60;
447 t += tm->tm_sec;
448
449 return t;
450}
451
452static void timestamp(void)
453{
454 struct compile_time * const oct = &official_compile_time;
455 const struct tm *tp, *best_gm;
456
457 time(&oct->t);
458
459 best_gm = NULL;
460
461 tp = localtime(&oct->t);
462 if (tp) {
463 oct->local = *tp;
464 best_gm = &oct->local;
465 oct->have_local = true;
466 }
467
468 tp = gmtime(&oct->t);
469 if (tp) {
470 oct->gm = *tp;
471 best_gm = &oct->gm;
472 oct->have_gm = true;
473 if (!oct->have_local)
474 oct->local = oct->gm;
475 } else {
476 oct->gm = oct->local;
477 }
478
479 if (best_gm) {
480 oct->posix = make_posix_time(best_gm);
481 oct->have_posix = true;
482 }
483}
484
H. Peter Anvin038d8612007-04-12 16:54:50 +0000485int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000486{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700487 /* Do these as early as possible */
488 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700489 _progname = argv[0];
490 if (!_progname || !_progname[0])
491 _progname = "nasm";
492
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700493 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800494
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800495 iflag_set_default_cpu(&cpu);
496 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400497
H. Peter Anvin322bee02019-08-10 01:38:06 -0700498 set_default_limits();
499
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800500 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300501
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800502 _pass_type = PASS_INIT;
503 _passn = 0;
504
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700505 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvin13506202018-11-28 14:55:58 -0800507 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700508 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700509
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700510 /*
511 * We must call init_labels() before the command line parsing,
512 * because we may be setting prefixes/suffixes from the command
513 * line.
514 */
515 init_labels();
516
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000517 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000518 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000519
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000520 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400521 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000522
H. Peter Anvin55568c12016-10-03 19:46:49 -0700523 parse_cmdline(argc, argv, 1);
524 if (terminate_after_phase) {
525 if (want_usage)
526 usage();
527 return 1;
528 }
529
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800530 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800531 if (!using_debug_info) {
532 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800533 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800534 } else if (!debug_format) {
535 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300536 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800537 } else {
538 dfmt = dfmt_find(ofmt, debug_format);
539 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800540 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800541 debug_format, ofmt->shortname);
542 }
543 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000544
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300545 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800546
547 parse_cmdline(argc, argv, 2);
548 if (terminate_after_phase) {
549 if (want_usage)
550 usage();
551 return 1;
552 }
553
554 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800555 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000556
H. Peter Anvin34754622018-11-28 12:36:53 -0800557 /* Dependency filename if we are also doing other things */
558 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
559 if (outname)
560 depend_file = nasm_strcat(outname, ".d");
561 else
562 depend_file = filename_set_extension(inname, ".d");
563 }
564
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300565 /*
566 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800567 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300568 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300569 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800570 if (!outname && !(operating_mode & OP_PREPROCESS)) {
571 outname = filename_set_extension(inname, ofmt->extension);
572 if (!strcmp(outname, inname)) {
573 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800574 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 -0800575 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300576 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800577
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800578 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700579
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700580 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400581 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700582
H. Peter Anvin34754622018-11-28 12:36:53 -0800583 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000584 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700585
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400586 if (depend_missing_ok)
587 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700588
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800589 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 while ((line = preproc->getline()))
592 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800593 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800594 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400595 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000596 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700597 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000598 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000600
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800601 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700602 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800604 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 } else
606 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000607
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700608 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000609
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800610 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
611 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800612
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 while ((line = preproc->getline())) {
614 /*
615 * We generate %line directives if needed for later programs
616 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000617 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 int altline = src_get(&linnum, &file_name);
619 if (altline) {
620 if (altline == 1 && lineinc == 1)
621 nasm_fputs("", ofile);
622 else {
623 lineinc = (altline != -1 || lineinc != 1);
624 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000625 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 file_name);
627 }
628 prior_linnum = linnum;
629 }
630 nasm_fputs(line, ofile);
631 nasm_free(line);
632 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800633 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800634 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 if (ofile)
636 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700637 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400639 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400640 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400642 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700643 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400644 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800645 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400647 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400648 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700650 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200651
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400652 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800653 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400654 cleanup_labels();
655 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800656 if (ferror(ofile))
657 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400658 }
659
660 if (ofile) {
661 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700662 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400663 remove(outname);
664 ofile = NULL;
665 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000666 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000667
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800668 preproc->cleanup_session();
669
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700670 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400671 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700672
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000673 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000675
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 raa_free(offsets);
677 saa_free(forwrefs);
678 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000679 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700680 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800681 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000682
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700683 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000684}
685
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686/*
687 * Get a parameter for a command line option.
688 * First arg must be in the form of e.g. -f...
689 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800690static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800692 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400693 if (p[2]) /* the parameter's in the option */
694 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800696 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000698 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800699 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000700 return NULL;
701}
702
H. Peter Anvindc242712007-11-18 11:55:10 -0800703/*
704 * Copy a filename
705 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800706static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800707{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800708 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700709 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800710
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800711 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800712}
713
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700714/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700715 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700716 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700717static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700718{
719 const char *p;
720 char *os, *q;
721
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400722 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700723 size_t nbs = 0;
724
725 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400726 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700727
728 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400729 switch (*p) {
730 case ' ':
731 case '\t':
732 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
733 n += nbs + 2;
734 nbs = 0;
735 break;
736 case '$':
737 case '#':
738 nbs = 0;
739 n += 2;
740 break;
741 case '\\':
742 nbs++;
743 n++;
744 break;
745 default:
746 nbs = 0;
747 n++;
748 break;
749 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700750 }
751
752 /* Convert N backslashes at the end of filename to 2N backslashes */
753 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400754 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700755
756 os = q = nasm_malloc(n);
757
758 nbs = 0;
759 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400760 switch (*p) {
761 case ' ':
762 case '\t':
763 while (nbs--)
764 *q++ = '\\';
765 *q++ = '\\';
766 *q++ = *p;
767 break;
768 case '$':
769 *q++ = *p;
770 *q++ = *p;
771 nbs = 0;
772 break;
773 case '#':
774 *q++ = '\\';
775 *q++ = *p;
776 nbs = 0;
777 break;
778 case '\\':
779 *q++ = *p;
780 nbs++;
781 break;
782 default:
783 *q++ = *p;
784 nbs = 0;
785 break;
786 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700787 }
788 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400789 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700790
791 *q = '\0';
792
793 return os;
794}
795
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700796/*
797 * Convert a string to a Watcom make-safe form
798 */
799static char *quote_for_wmake(const char *str)
800{
801 const char *p;
802 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700803 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700804
805 size_t n = 1; /* Terminating zero */
806
807 if (!str)
808 return NULL;
809
810 for (p = str; *p; p++) {
811 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700812 case ' ':
813 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700814 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700815 quote = true;
816 n++;
817 break;
818 case '\"':
819 quote = true;
820 n += 2;
821 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700822 case '$':
823 case '#':
824 n += 2;
825 break;
826 default:
827 n++;
828 break;
829 }
830 }
831
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700832 if (quote)
833 n += 2;
834
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700835 os = q = nasm_malloc(n);
836
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700837 if (quote)
838 *q++ = '\"';
839
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700840 for (p = str; *p; p++) {
841 switch (*p) {
842 case '$':
843 case '#':
844 *q++ = '$';
845 *q++ = *p;
846 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700847 case '\"':
848 *q++ = *p;
849 *q++ = *p;
850 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700851 default:
852 *q++ = *p;
853 break;
854 }
855 }
856
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700857 if (quote)
858 *q++ = '\"';
859
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700860 *q = '\0';
861
862 return os;
863}
864
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100865enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800866 OPT_BOGUS,
867 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700868 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800869 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700870 OPT_MANGLE,
871 OPT_INCLUDE,
872 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700873 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700874 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800875 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700876 OPT_NO_LINE,
877 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100878};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700879enum need_arg {
880 ARG_NO,
881 ARG_YES,
882 ARG_MAYBE
883};
884
H. Peter Anvin3366e312018-02-07 14:14:36 -0800885struct textargs {
886 const char *label;
887 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700888 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700889 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800890};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800891static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700892 {"v", OPT_VERSION, ARG_NO, 0},
893 {"version", OPT_VERSION, ARG_NO, 0},
894 {"help", OPT_HELP, ARG_NO, 0},
895 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
896 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
897 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
898 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
899 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
900 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
901 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
902 {"include", OPT_INCLUDE, ARG_YES, 0},
903 {"pragma", OPT_PRAGMA, ARG_YES, 0},
904 {"before", OPT_BEFORE, ARG_YES, 0},
905 {"limit-", OPT_LIMIT, ARG_YES, 0},
906 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
907 {"no-line", OPT_NO_LINE, ARG_NO, 0},
908 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
909 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000910};
911
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400912static void show_version(void)
913{
914 printf("NASM version %s compiled on %s%s\n",
915 nasm_version, nasm_date, nasm_compile_options);
916 exit(0);
917}
918
H. Peter Anvin423e3812007-11-15 17:12:29 -0800919static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700920static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000921{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000922 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800923 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000924
925 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800926 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000927
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700929 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 /* These parameters take values */
931 if (!(param = get_param(p, q, &advance)))
932 return advance;
933 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 switch (p[1]) {
936 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 if (pass == 1)
938 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700940
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800943 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400944 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700945
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700947 if (pass == 1) {
948 ofmt = ofmt_find(param, &ofmt_alias);
949 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800950 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700951 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400952 }
953 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700954
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800956 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700958
H. Peter Anvin55568c12016-10-03 19:46:49 -0700959 if (!*param) {
960 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300961 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 } else {
963 while (*param) {
964 switch (*param) {
965 case '0': case '1': case '2': case '3': case '4':
966 case '5': case '6': case '7': case '8': case '9':
967 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700968
Chang S. Baea5786342018-08-15 23:22:21 +0300969 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
970 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700971 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300972 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700973 else
Chang S. Baea5786342018-08-15 23:22:21 +0300974 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700975 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700976
H. Peter Anvin55568c12016-10-03 19:46:49 -0700977 case 'v':
978 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400979 param++;
980 opt_verbose_info = true;
981 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700982
H. Peter Anvin55568c12016-10-03 19:46:49 -0700983 case 'x':
984 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300985 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700986 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700987
H. Peter Anvin55568c12016-10-03 19:46:49 -0700988 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700989 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700990 *param);
991 break;
992 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 }
Chang S. Baea5786342018-08-15 23:22:21 +0300994 if (optimizing.level > MAX_OPTIMIZE)
995 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400997 }
998 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800999
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001000 case 'p': /* pre-include */
1001 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001002 if (pass == 2)
1003 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001004 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001005
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 case 'd': /* pre-define */
1007 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001008 if (pass == 2)
1009 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001011
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001012 case 'u': /* un-define */
1013 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001014 if (pass == 2)
1015 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001016 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001017
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001018 case 'i': /* include search path */
1019 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001020 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001021 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001022 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001023
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001024 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001025 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001026 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001027 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001028
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001029 case 'L': /* listing options */
1030 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001031 while (*param)
1032 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001033 }
1034 break;
1035
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001036 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001038 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001039 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001040
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001041 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001042 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 using_debug_info = true;
1044 debug_format = param;
1045 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001046 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001047
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001048 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001049 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001050 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1051 errfmt = &errfmt_msvc;
1052 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1053 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001054 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001055 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001056 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001060 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001061 using_debug_info = true;
1062 if (p[2])
1063 debug_format = nasm_skip_spaces(p + 2);
1064 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001066
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001068 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001069 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001073 /* legacy option */
1074 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 exit(0);
1076 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001079 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001080 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001081 nasm_ctype_tasm_mode();
1082 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001086 show_version();
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 'e': /* preprocess only */
1090 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001091 if (pass == 1)
1092 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001094
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001095 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001096 if (pass == 1)
1097 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001099
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001100 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001101 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001102 if (pass == 2)
1103 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001104 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001105
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001107 if (pass == 1) {
1108 switch (p[2]) {
1109 case 'W':
1110 quote_for_make = quote_for_wmake;
1111 break;
1112 case 'D':
1113 case 'F':
1114 case 'T':
1115 case 'Q':
1116 advance = true;
1117 break;
1118 default:
1119 break;
1120 }
1121 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001122 switch (p[2]) {
1123 case 0:
1124 operating_mode = OP_DEPEND;
1125 break;
1126 case 'G':
1127 operating_mode = OP_DEPEND;
1128 depend_missing_ok = true;
1129 break;
1130 case 'P':
1131 depend_emit_phony = true;
1132 break;
1133 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001134 operating_mode |= OP_DEPEND;
1135 if (q && (q[0] != '-' || q[1] == '\0')) {
1136 depend_file = q;
1137 advance = true;
1138 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001139 break;
1140 case 'F':
1141 depend_file = q;
1142 advance = true;
1143 break;
1144 case 'T':
1145 depend_target = q;
1146 advance = true;
1147 break;
1148 case 'Q':
1149 depend_target = quote_for_make(q);
1150 advance = true;
1151 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001152 case 'W':
1153 /* handled in pass 1 */
1154 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001155 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001156 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001157 break;
1158 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001159 }
1160 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001161 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001162 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001163 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 case '-':
1167 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001168 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001169 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001170 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001171 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001172
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001173 p += 2;
1174
1175 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001176 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 break;
1178 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001179
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001180 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001181 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001182 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001183 olen = strlen(tx->label);
1184
1185 if (olen > plen)
1186 continue;
1187
1188 if (nasm_memicmp(p, tx->label, olen))
1189 continue;
1190
1191 if (tx->label[olen-1] == '-')
1192 break; /* Incomplete option */
1193
1194 if (!p[olen] || p[olen] == '=')
1195 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001197
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001198 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001199 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001200 }
1201
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001202 opt = tx->opt;
1203
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001204 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001205 if (param)
1206 *param++ = '\0';
1207
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001208 switch (tx->need_arg) {
1209 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001210 if (!param) {
1211 param = q;
1212 advance = true;
1213 }
1214
1215 /* Note: a null string is a valid parameter */
1216 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001217 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001218 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001220 break;
1221
1222 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001223 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001224 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001225 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001226 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001227 break;
1228
1229 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1230 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001231 }
1232
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001233 switch (opt) {
1234 case OPT_BOGUS:
1235 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001236 case OPT_VERSION:
1237 show_version();
1238 break;
1239 case OPT_ABORT_ON_PANIC:
1240 abort_on_panic = true;
1241 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001242 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001243 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001244 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001245 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001246 case OPT_INCLUDE:
1247 if (pass == 2)
1248 preproc->pre_include(q);
1249 break;
1250 case OPT_PRAGMA:
1251 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001252 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001253 break;
1254 case OPT_BEFORE:
1255 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001256 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001257 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001258 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001259 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001260 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001261 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001262 case OPT_KEEP_ALL:
1263 keep_all = true;
1264 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001265 case OPT_NO_LINE:
1266 pp_noline = true;
1267 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001268 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001269 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001270 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001271 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001272 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001273 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001274 default:
1275 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001277
1278 if (eqsave)
1279 *eqsave = '='; /* Restore = argument separator */
1280
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 break;
1282 }
1283
1284 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001285 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 break;
1287 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001288 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001289 /* In theory we could allow multiple input files... */
1290 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001291 }
1292
1293 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001294}
1295
H. Peter Anvineba20a72002-04-30 20:53:55 +00001296#define ARG_BUF_DELTA 128
1297
H. Peter Anvin55568c12016-10-03 19:46:49 -07001298static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001299{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001300 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001301 int bufsize, prevargsize;
1302
1303 bufsize = prevargsize = ARG_BUF_DELTA;
1304 buffer = nasm_malloc(ARG_BUF_DELTA);
1305 prevarg = nasm_malloc(ARG_BUF_DELTA);
1306 prevarg[0] = '\0';
1307
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 p = buffer;
1310 while (1) { /* Loop to handle long lines */
1311 q = fgets(p, bufsize - (p - buffer), rfile);
1312 if (!q)
1313 break;
1314 p += strlen(p);
1315 if (p > buffer && p[-1] == '\n')
1316 break;
1317 if (p - buffer > bufsize - 10) {
1318 int offset;
1319 offset = p - buffer;
1320 bufsize += ARG_BUF_DELTA;
1321 buffer = nasm_realloc(buffer, bufsize);
1322 p = buffer + offset;
1323 }
1324 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001325
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 if (!q && p == buffer) {
1327 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001328 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 nasm_free(buffer);
1330 nasm_free(prevarg);
1331 return;
1332 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001333
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 /*
1335 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1336 * them are present at the end of the line.
1337 */
1338 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001339
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001340 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001342
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001343 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001344
H. Peter Anvin55568c12016-10-03 19:46:49 -07001345 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347
Charles Crayne192d5b52007-10-18 19:02:42 -07001348 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 prevargsize += ARG_BUF_DELTA;
1350 prevarg = nasm_realloc(prevarg, prevargsize);
1351 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001352 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001353 }
1354}
1355
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001356/* Function to process args from a string of args, rather than the
1357 * argv array. Used by the environment variable and response file
1358 * processing.
1359 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001360static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001362 char *p, *q, *arg, *prevarg;
1363 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001364
1365 p = args;
1366 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001368 arg = NULL;
1369 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 q = p;
1371 while (*p && *p != separator)
1372 p++;
1373 while (*p == separator)
1374 *p++ = '\0';
1375 prevarg = arg;
1376 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001377 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001379 }
1380 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001381 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001382}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001383
H. Peter Anvin55568c12016-10-03 19:46:49 -07001384static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001385{
1386 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001387 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001388 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001389 perror(file);
1390 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001391 }
1392 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001393 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001394 }
1395 fclose(f);
1396}
1397
H. Peter Anvin55568c12016-10-03 19:46:49 -07001398static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399{
1400 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001401 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001402
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001403 /*
1404 * Initialize all the warnings to their default state, including
1405 * warning index 0 used for "always on".
1406 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001407 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001408
1409 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001410 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001411 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001412 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001413 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001415 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001417 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001418
1419 /*
1420 * Now process the actual command line.
1421 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001423 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 argv++;
1425 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001426 /*
1427 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 * arguments like the environment variable. This allows us
1429 * to have multiple arguments on a single line, which is
1430 * different to the -@resp file processing below for regular
1431 * NASM.
1432 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001433 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 argc--;
1435 argv++;
1436 }
1437 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001438 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001439 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001440 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001441 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001442 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001444 } else {
1445 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1446 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 }
1448 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001449 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001450 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001451 }
1452
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001453 /*
1454 * Look for basic command line typos. This definitely doesn't
1455 * catch all errors, but it might help cases of fumbled fingers.
1456 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001457 if (pass != 2)
1458 return;
1459
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001460 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001461 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001462 else if ((errname && !strcmp(inname, errname)) ||
1463 (outname && !strcmp(inname, outname)) ||
1464 (listname && !strcmp(inname, listname)) ||
1465 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001466 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001467
1468 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001469 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001470 if (!error_file) {
1471 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001472 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001473 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001474 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001475}
1476
H. Peter Anvin29651542018-12-18 19:14:40 -08001477static void forward_refs(insn *instruction)
1478{
1479 int i;
1480 struct forwrefinfo *fwinf;
1481
1482 instruction->forw_ref = false;
1483
1484 if (!optimizing.level)
1485 return; /* For -O0 don't bother */
1486
1487 if (!forwref)
1488 return;
1489
1490 if (forwref->lineno != globallineno)
1491 return;
1492
1493 instruction->forw_ref = true;
1494 do {
1495 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1496 forwref = saa_rstruct(forwrefs);
1497 } while (forwref && forwref->lineno == globallineno);
1498
1499 if (!pass_first())
1500 return;
1501
1502 for (i = 0; i < instruction->operands; i++) {
1503 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1504 fwinf = saa_wstruct(forwrefs);
1505 fwinf->lineno = globallineno;
1506 fwinf->operand = i;
1507 }
1508 }
1509}
1510
1511static void process_insn(insn *instruction)
1512{
1513 int32_t n;
1514 int64_t l;
1515
1516 if (!instruction->times)
1517 return; /* Nothing to do... */
1518
1519 nasm_assert(instruction->times > 0);
1520
1521 /*
1522 * NOTE: insn_size() can change instruction->times
1523 * (usually to 1) when called.
1524 */
1525 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001526 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001527 for (n = 1; n <= instruction->times; n++) {
1528 l = insn_size(location.segment, location.offset,
1529 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001530 /* l == -1 -> invalid instruction */
1531 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001532 increment_offset(l);
1533 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001534 if (list_option('p')) {
1535 struct out_data dummy;
1536 memset(&dummy, 0, sizeof dummy);
1537 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1538 dummy.offset = start;
1539 dummy.size = location.offset - start;
1540 lfmt->output(&dummy);
1541 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001542 } else {
1543 l = assemble(location.segment, location.offset,
1544 globalbits, instruction);
1545 /* We can't get an invalid instruction here */
1546 increment_offset(l);
1547
1548 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001549 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001550 for (n = 2; n <= instruction->times; n++) {
1551 l = assemble(location.segment, location.offset,
1552 globalbits, instruction);
1553 increment_offset(l);
1554 }
1555 lfmt->downlevel(LIST_TIMES);
1556 }
1557 }
1558}
1559
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001560static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001561{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001562 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001564 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001565 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001566
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001567 switch (cmd_sb) {
1568 case 16:
1569 break;
1570 case 32:
1571 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001572 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001573 break;
1574 case 64:
1575 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001576 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001577 break;
1578 default:
1579 panic();
1580 break;
1581 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001582
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001583 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001584
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001585 if (listname && !keep_all) {
1586 /* Remove the list file in case we die before the output pass */
1587 remove(listname);
1588 }
1589
1590 while (!terminate_after_phase && !pass_final()) {
1591 _passn++;
1592 if (pass_type() != PASS_OPT || !global_offset_changed)
1593 _pass_type++;
1594 global_offset_changed = 0;
1595
1596 /*
1597 * Create a warning buffer list unless we are in
1598 * pass 2 (everything will be emitted immediately in pass 2.)
1599 */
1600 if (warn_list) {
1601 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001602 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001603 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001604
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001605 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001606 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001607
H. Peter Anvincac0b192017-03-28 16:12:30 -07001608 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001610 if (listname) {
1611 if (pass_final() || list_on_every_pass()) {
1612 active_list_options = list_options;
1613 lfmt->init(listname);
1614 } else if (active_list_options) {
1615 /*
1616 * Looks like we used the list engine on a previous pass,
1617 * but now it is turned off, presumably via %pragma -p
1618 */
1619 lfmt->cleanup();
1620 if (!keep_all)
1621 remove(listname);
1622 active_list_options = 0;
1623 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001624 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001625
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001626 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001627 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 saa_rewind(forwrefs);
1629 forwref = saa_rstruct(forwrefs);
1630 raa_free(offsets);
1631 offsets = raa_init();
1632 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001633 location.segment = NO_SEG;
1634 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001635 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001636 location.known = true;
1637 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001638 switch_segment(ofmt->section(NULL, &globalbits));
1639 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001640
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001642
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001644 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001645 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001646 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001647
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001648 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001649 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001650 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001651 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001652 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001653 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001654
H. Peter Anvinc7131682017-03-07 17:45:01 -08001655 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001656 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001657 forward_refs(&output_ins);
1658 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001659 cleanup_insn(&output_ins);
1660
1661 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001664
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001665 preproc->cleanup_pass();
1666
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001667 /* We better not be having an error hold still... */
1668 nasm_assert(!errhold_stack);
1669
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001670 if (global_offset_changed) {
1671 switch (pass_type()) {
1672 case PASS_OPT:
1673 /*
1674 * This is the only pass type that can be executed more
1675 * than once, and therefore has the ability to stall.
1676 */
1677 if (global_offset_changed < prev_offset_changed) {
1678 prev_offset_changed = global_offset_changed;
1679 stall_count = 0;
1680 } else {
1681 stall_count++;
1682 }
1683
1684 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1685 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1686 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001687 nasm_nonfatalf(ERR_UNDEAD,
1688 "unable to find valid values for all labels "
1689 "after %"PRId64" passes; "
1690 "stalled for %"PRId64", giving up.",
1691 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001692 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001693 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001694 }
1695 break;
1696
1697 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001698 /*!
1699 *!phase [off] phase error during stabilization
1700 *! warns about symbols having changed values during
1701 *! the second-to-last assembly pass. This is not
1702 *! inherently fatal, but may be a source of bugs.
1703 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001704 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1705 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001706 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001707 break;
1708
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001709 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001710 nasm_nonfatalf(ERR_UNDEAD,
1711 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001712 break;
1713
1714 default:
1715 /* This is normal, we'll keep going... */
1716 break;
1717 }
1718 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001719
1720 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001721 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001722
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001723 if (opt_verbose_info && pass_final()) {
1724 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001725 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001726 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001727
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001728 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001729 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001730}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001731
Ed Berosetfa771012002-06-09 20:56:40 +00001732/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001733 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001734 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001735static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001736{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001737 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001738
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001739 if ((severity & ERR_MASK) >= ERR_FATAL)
1740 return 0; /* Fatal errors are never suppressible */
1741
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001742 /* Warnings MUST HAVE a warning category specifier! */
1743 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001744
1745 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001746 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001747
1748 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001749}
1750
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001751static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001752{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001753 errflags type = severity & ERR_MASK;
1754
H. Peter Anvin8f622462017-04-02 19:02:29 -07001755 /*
1756 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001757 * We can never skip fatal errors as by definition they cannot be
1758 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001759 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001760 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001761 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001762
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001763 /*
1764 * ERR_LISTMSG messages are always skipped; the list file
1765 * receives them anyway as this function is not consulted
1766 * for sending to the list file.
1767 */
1768 if (type == ERR_LISTMSG)
1769 return true;
1770
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001771 /* This message not applicable unless pass_final */
1772 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001773}
1774
Ed Berosetfa771012002-06-09 20:56:40 +00001775/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001776 * check for suppressed message (usually warnings or notes)
1777 *
1778 * @param severity the severity of the warning or error
1779 * @return true if we should abort error/warning printing
1780 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001781static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001782{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001783 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001784 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001785 return false;
1786
1787 /* This error/warning is pointless if we are dead anyway */
1788 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1789 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001790
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001791 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1792 return true;
1793
1794 if (preproc && !(severity & ERR_PP_LISTMACRO))
1795 return preproc->suppress_error(severity);
1796
1797 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001798}
1799
1800/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001801 * Return the true error type (the ERR_MASK part) of the given
1802 * severity, accounting for warnings that may need to be promoted to
1803 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001804 *
1805 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001806 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001807 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001808static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001809{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001810 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1811 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001812
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001813 type = severity & ERR_MASK;
1814
1815 /* Promote warning to error? */
1816 if (type == ERR_WARNING) {
1817 uint8_t state = warning_state[warn_index(severity)];
1818 if ((state & warn_is_err) == warn_is_err)
1819 type = ERR_NONFATAL;
1820 }
1821
1822 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001823}
1824
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001825/*
1826 * The various error type prefixes
1827 */
1828static const char * const error_pfx_table[ERR_MASK+1] = {
1829 ";;; ", "debug: ", "info: ", "warning: ",
1830 "error: ", "fatal: ", "critical: ", "panic: "
1831};
1832static const char no_file_name[] = "nasm"; /* What to print if no file name */
1833
1834/*
1835 * For fatal/critical/panic errors, kill this process.
1836 */
1837static fatal_func die_hard(errflags true_type, errflags severity)
1838{
1839 fflush(NULL);
1840
1841 if (true_type == ERR_PANIC && abort_on_panic)
1842 abort();
1843
1844 if (ofile) {
1845 fclose(ofile);
1846 if (!keep_all)
1847 remove(outname);
1848 ofile = NULL;
1849 }
1850
1851 if (severity & ERR_USAGE)
1852 usage();
1853
1854 /* Terminate immediately */
1855 exit(true_type - ERR_FATAL + 1);
1856}
1857
1858/*
1859 * error reporting for critical and panic errors: minimize
1860 * the amount of system dependencies for getting a message out,
1861 * and in particular try to avoid memory allocations.
1862 */
1863fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1864{
1865 const char *currentfile = no_file_name;
1866 int32_t lineno = 0;
1867 errflags true_type = severity & ERR_MASK;
1868 static bool been_here = false;
1869
1870 if (unlikely(been_here))
1871 abort(); /* Recursive error... just die */
1872
1873 been_here = true;
1874
1875 if (!(severity & ERR_NOFILE)) {
1876 src_get(&lineno, &currentfile);
1877 if (!currentfile) {
1878 currentfile =
1879 inname && inname[0] ? inname :
1880 outname && outname[0] ? outname :
1881 no_file_name;
1882 lineno = 0;
1883 }
1884 }
1885
1886 fputs(error_pfx_table[severity], error_file);
1887 fputs(currentfile, error_file);
1888 if (lineno) {
1889 fprintf(error_file, "%s%"PRId32"%s",
1890 errfmt->beforeline, lineno, errfmt->afterline);
1891 }
1892 fputs(errfmt->beforemsg, error_file);
1893 vfprintf(error_file, fmt, args);
1894 fputc('\n', error_file);
1895
1896 die_hard(true_type, severity);
1897}
1898
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001899/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001900 * Stack of tentative error hold lists.
1901 */
1902struct nasm_errtext {
1903 struct nasm_errtext *next;
1904 const char *currentfile; /* Owned by the filename system */
1905 char *msg; /* Owned by this structure */
1906 errflags severity;
1907 errflags true_type;
1908 int32_t lineno;
1909};
1910struct nasm_errhold {
1911 struct nasm_errhold *up;
1912 struct nasm_errtext *head, **tail;
1913};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001914
1915static void nasm_free_error(struct nasm_errtext *et)
1916{
1917 nasm_free(et->msg);
1918 nasm_free(et);
1919}
1920
1921static void nasm_issue_error(struct nasm_errtext *et);
1922
1923struct nasm_errhold *nasm_error_hold_push(void)
1924{
1925 struct nasm_errhold *eh;
1926
1927 nasm_new(eh);
1928 eh->up = errhold_stack;
1929 eh->tail = &eh->head;
1930 errhold_stack = eh;
1931
1932 return eh;
1933}
1934
1935void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
1936{
1937 struct nasm_errtext *et, *etmp;
1938
1939 /* Allow calling with a null argument saying no hold in the first place */
1940 if (!eh)
1941 return;
1942
1943 /* This *must* be the current top of the errhold stack */
1944 nasm_assert(eh == errhold_stack);
1945
1946 if (eh->head) {
1947 if (issue) {
1948 if (eh->up) {
1949 /* Commit the current hold list to the previous level */
1950 *eh->up->tail = eh->head;
1951 eh->up->tail = eh->tail;
1952 } else {
1953 /* Issue errors */
1954 list_for_each_safe(et, etmp, eh->head)
1955 nasm_issue_error(et);
1956 }
1957 } else {
1958 /* Free the list, drop errors */
1959 list_for_each_safe(et, etmp, eh->head)
1960 nasm_free_error(et);
1961 }
1962 }
1963
1964 errhold_stack = eh->up;
1965 nasm_free(eh);
1966}
1967
1968/**
Ed Berosetfa771012002-06-09 20:56:40 +00001969 * common error reporting
1970 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001971 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001972 * specific error message to error_file and may or may not return. It
1973 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001974 *
1975 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001976 * @param fmt the printf style format string
1977 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001978void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001979{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001980 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001981 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001982
1983 if (true_type >= ERR_CRITICAL)
1984 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001985
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001986 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001987 return;
1988
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001989 nasm_new(et);
1990 et->severity = severity;
1991 et->true_type = true_type;
1992 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001993 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001994 src_get(&et->lineno, &et->currentfile);
1995
1996 if (!et->currentfile) {
1997 et->currentfile =
H. Peter Anvin77016c82018-12-10 22:29:49 -08001998 inname && inname[0] ? inname :
1999 outname && outname[0] ? outname :
2000 NULL;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002001 et->lineno = 0;
H. Peter Anvin77016c82018-12-10 22:29:49 -08002002 }
2003 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002004
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002005 if (errhold_stack && true_type <= ERR_NONFATAL) {
2006 /* It is a tentative error */
2007 *errhold_stack->tail = et;
2008 errhold_stack->tail = &et->next;
2009 } else {
2010 nasm_issue_error(et);
2011 }
2012
2013 /*
2014 * Don't do this before then, if we do, we lose messages in the list
2015 * file, as the list file is only generated in the last pass.
2016 */
2017 if (skip_this_pass(severity))
2018 return;
2019
2020 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2021 if (preproc)
2022 preproc->error_list_macros(severity);
2023}
2024
2025/*
2026 * Actually print, list and take action on an error
2027 */
2028static void nasm_issue_error(struct nasm_errtext *et)
2029{
2030 const char *pfx;
2031 char warnsuf[64]; /* Warning suffix */
2032 char linestr[64]; /* Formatted line number if applicable */
2033 const errflags severity = et->severity;
2034 const errflags true_type = et->true_type;
2035 const char * const currentfile = et->currentfile;
2036 const uint32_t lineno = et->lineno;
2037
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002038 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002039 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002040 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002041 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002042
H. Peter Anvinddb29062018-12-11 00:06:29 -08002043 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002044 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2045 /*
2046 * It's a warning without ERR_HERE defined, and we are not already
2047 * unwinding the macros that led us here.
2048 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002049 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002050 (true_type >= ERR_NONFATAL) ? "error=" : "",
2051 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002052 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002053
H. Peter Anvinddb29062018-12-11 00:06:29 -08002054 *linestr = 0;
2055 if (lineno) {
2056 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
2057 errfmt->beforeline, lineno, errfmt->afterline);
2058 }
2059
H. Peter Anvin77016c82018-12-10 22:29:49 -08002060 if (!skip_this_pass(severity)) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002061 const char *file = currentfile ? currentfile : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002062 const char *here = "";
2063
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002064 if (severity & ERR_HERE) {
2065 here = currentfile ? " here" : " in an unknown location";
2066 }
2067
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002068 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002069 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002070 /*
2071 * Buffer up warnings until we either get an error
2072 * or we are on the code-generation pass.
2073 */
2074 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2075 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002076 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002077 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002078 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002079 * Actually output an error. If we have buffered
2080 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002081 */
2082 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002083 strlist_write(warn_list, "\n", error_file);
2084 strlist_free(&warn_list);
2085 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002086
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002087 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2088 file, linestr, errfmt->beforemsg,
2089 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002090 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002091 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002092
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002093 /* Are we recursing from error_list_macros? */
2094 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002095 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002096
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002097 /*
2098 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002099 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002100 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002101 if (severity & ERR_HERE) {
2102 if (lineno)
2103 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002104 pfx, et->msg, currentfile, lineno, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002105 else if (currentfile)
2106 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002107 pfx, et->msg, currentfile, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002108 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002109 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002110 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002111 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002112 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002113 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002114
H. Peter Anvin8f622462017-04-02 19:02:29 -07002115 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002116 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002117
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002118 if (true_type >= ERR_FATAL)
2119 die_hard(true_type, severity);
2120 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002121 terminate_after_phase = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002122
2123done:
2124 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002125}
2126
H. Peter Anvin734b1882002-04-30 21:01:08 +00002127static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002128{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002129 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002130}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002131
H. Peter Anvin322bee02019-08-10 01:38:06 -07002132static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002133{
2134 int i;
2135
H. Peter Anvin322bee02019-08-10 01:38:06 -07002136 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002137 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2138 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002139 _progname, _progname);
2140 fputs(
2141 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002142 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002143 "\n"
2144 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002145 " -v (or --v) print the NASM version number and exit\n"
2146 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002147 "\n"
2148 " -o outfile write output to outfile\n"
2149 " --keep-all output files will not be removed even if an error happens\n"
2150 "\n"
2151 " -Xformat specifiy error reporting format (gnu or vc)\n"
2152 " -s redirect error messages to stdout\n"
2153 " -Zfile redirect error messages to file\n"
2154 "\n"
2155 " -M generate Makefile dependencies on stdout\n"
2156 " -MG d:o, missing files assumed generated\n"
2157 " -MF file set Makefile dependency file\n"
2158 " -MD file assemble and generate dependencies\n"
2159 " -MT file dependency target name\n"
2160 " -MQ file dependency target name (quoted)\n"
2161 " -MP emit phony targets\n"
2162 "\n"
2163 " -f format select output file format\n"
2164 , out);
2165 ofmt_list(ofmt, out);
2166 fputs(
2167 "\n"
2168 " -g generate debugging information\n"
2169 " -F format select a debugging format (output format dependent)\n"
2170 " -gformat same as -g -F format\n"
2171 , out);
2172 dfmt_list(out);
2173 fputs(
2174 "\n"
2175 " -l listfile write listing to a list file\n"
2176 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002177 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002178 " -Ld show byte and repeat counts in decimal, not hex\n"
2179 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002180 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002181 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002182 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002183 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002184 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002185 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002186 "\n"
2187 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2188 " -O0 no optimization\n"
2189 " -O1 minimal optimization\n"
2190 " -Ox multipass optimization (default)\n"
2191 " -Ov display the number of passes executed at the end\n"
2192 " -t assemble in limited SciTech TASM compatible mode\n"
2193 "\n"
2194 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2195 " -a don't preprocess (assemble only)\n"
2196 " -Ipath add a pathname to the include file path\n"
2197 " -Pfile pre-include a file (also --include)\n"
2198 " -Dmacro[=str] pre-define a macro\n"
2199 " -Umacro undefine a macro\n"
2200 " --pragma str pre-executes a specific %%pragma\n"
2201 " --before str add line (usually a preprocessor statement) before the input\n"
2202 " --no-line ignore %line directives in input\n"
2203 "\n"
2204 " --prefix str prepend the given string to the names of all extern,\n"
2205 " common and global symbols (also --gprefix)\n"
2206 " --suffix str append the given string to the names of all extern,\n"
2207 " common and global symbols (also --gprefix)\n"
2208 " --lprefix str prepend the given string to local symbols\n"
2209 " --lpostfix str append the given string to local symbols\n"
2210 "\n"
2211 " -w+x enable warning x (also -Wx)\n"
2212 " -w-x disable warning x (also -Wno-x)\n"
2213 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2214 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2215 , out);
2216
2217 fprintf(out, " %-20s %s\n",
2218 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2219
2220 for (i = 1; i < WARN_IDX_ALL; i++) {
2221 const char *me = warning_name[i];
2222 const char *prev = warning_name[i-1];
2223 const char *next = warning_name[i+1];
2224
2225 if (prev) {
2226 int prev_len = strlen(prev);
2227 const char *dash = me;
2228
2229 while ((dash = strchr(dash+1, '-'))) {
2230 int prefix_len = dash - me; /* Not including final dash */
2231 if (strncmp(next, me, prefix_len+1)) {
2232 /* Only one or last option with this prefix */
2233 break;
2234 }
2235 if (prefix_len >= prev_len ||
2236 strncmp(prev, me, prefix_len) ||
2237 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2238 /* This prefix is different from the previous option */
2239 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2240 prefix_len, me, prefix_len+1, me);
2241 }
2242 }
2243 }
2244
2245 fprintf(out, " %-20s %s%s\n",
2246 warning_name[i], warning_help[i],
2247 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2248 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2249 }
2250
2251 fputs(
2252 "\n"
2253 " --limit-X val set execution limit X\n"
2254 , out);
2255
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002256
2257 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002258 fprintf(out, " %-20s %s [",
2259 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002260 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002261 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002262 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002263 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002264 }
2265 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002266}