blob: a0e171933ab9b9293210885be953ecc5c47fc084 [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 Anvin (Intel)6e9554f2020-06-14 23:21:44 -070047#include "floats.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -070058#include "quote.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080059#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvin31387b22010-07-15 18:28:52 -070061/*
62 * This is the maximum number of optimization passes to do. If we ever
63 * find a case where the optimizer doesn't naturally converge, we might
64 * have to drop this value so the assembler doesn't appear to just hang.
65 */
66#define MAX_OPTIMIZE (INT_MAX >> 1)
67
H. Peter Anvine2c80182005-01-15 22:15:51 +000068struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000069 int lineno;
70 int operand;
71};
72
H. Peter Anvin322bee02019-08-10 01:38:06 -070073const char *_progname;
74
H. Peter Anvin55568c12016-10-03 19:46:49 -070075static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030076static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080077static bool skip_this_pass(errflags severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070079static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000080
H. Peter Anvin77016c82018-12-10 22:29:49 -080081struct error_format {
82 const char *beforeline; /* Before line number, if present */
83 const char *afterline; /* After line number, if present */
84 const char *beforemsg; /* Before actual message */
85};
86
87static const struct error_format errfmt_gnu = { ":", "", ": " };
88static const struct error_format errfmt_msvc = { "(", ")", " : " };
89static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080090static struct strlist *warn_list;
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -070091static struct nasm_errhold *errhold_stack;
H. Peter Anvin77016c82018-12-10 22:29:49 -080092
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070093unsigned int debug_nasm; /* Debugging messages? */
94
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080095static bool using_debug_info, opt_verbose_info;
96static const char *debug_format;
97
H. Peter Anvin3366e312018-02-07 14:14:36 -080098#ifndef ABORT_ON_PANIC
99# define ABORT_ON_PANIC 0
100#endif
101static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700102static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800103
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700104bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800105enum pass_type _pass_type;
106const char * const _pass_types[] =
107{
H. Peter Anvin (Intel)91bc5182020-07-08 09:14:58 -0700108 "init", "preproc-only", "first", "optimize", "stabilize", "final"
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800109};
110int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000111int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800112int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000113
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700114struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800115
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800116const char *inname;
117const char *outname;
118static const char *listname;
119static const char *errname;
120
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700121static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700122
H. Peter Anvin338656c2016-02-17 20:59:22 -0800123const struct ofmt *ofmt = &OF_DEFAULT;
124const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400125const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800127FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000128
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400129FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300130struct optimization optimizing =
131 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100132static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400133
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800134iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400135static iflag_t cmd_cpu;
136
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800137struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800138bool in_absolute; /* Flag we are in ABSOLUTE seg */
139struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000140
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000142
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700144static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145
H. Peter Anvine7469712016-02-18 02:20:59 -0800146static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300147static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800148bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400149
H. Peter Anvin34754622018-11-28 12:36:53 -0800150#define OP_NORMAL (1U << 0)
151#define OP_PREPROCESS (1U << 1)
152#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400153
154static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400155
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700156/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700157static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700158static bool depend_missing_ok = false;
159static const char *depend_target = NULL;
160static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300161struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000162
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700163static bool want_usage;
164static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800165bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000166
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700167static char *quote_for_pmake(const char *str);
168static char *quote_for_wmake(const char *str);
169static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700170
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700171/*
172 * Execution limits that can be set via a command-line option or %pragma
173 */
174
H. Peter Anvin322bee02019-08-10 01:38:06 -0700175/*
176 * This is really unlimited; it would take far longer than the
177 * current age of the universe for this limit to be reached even on
178 * much faster CPUs than currently exist.
179*/
180#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700181
H. Peter Anvin322bee02019-08-10 01:38:06 -0700182int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183
184struct limit_info {
185 const char *name;
186 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700187 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700189/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700190static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700191 { "passes", "total number of passes", LIMIT_MAX_VAL },
192 { "stalled-passes", "number of passes without forward progress", 1000 },
193 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700194 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
195 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700196 { "rep", "%rep count", 1000000 },
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700197 { "eval", "expression evaluation descent", 8192 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700198 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199};
200
H. Peter Anvin322bee02019-08-10 01:38:06 -0700201static void set_default_limits(void)
202{
203 int i;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700204 size_t rl;
205 int64_t new_limit;
206
H. Peter Anvin322bee02019-08-10 01:38:06 -0700207 for (i = 0; i <= LIMIT_MAX; i++)
208 nasm_limit[i] = limit_info[i].default_val;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700209
210 /*
211 * Try to set a sensible default value for the eval depth based
212 * on the limit of the stack size, if knowable...
213 */
214 rl = nasm_get_stack_size_limit();
215 new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
216 if (new_limit < nasm_limit[LIMIT_EVAL])
217 nasm_limit[LIMIT_EVAL] = new_limit;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700218}
219
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700220enum directive_result
221nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700222{
223 int i;
224 int64_t val;
225 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700226 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700227
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700228 if (!limit)
229 limit = "";
230 if (!valstr)
231 valstr = "";
232
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700233 for (i = 0; i <= LIMIT_MAX; i++) {
234 if (!nasm_stricmp(limit, limit_info[i].name))
235 break;
236 }
237 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800238 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800239 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700240 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700241 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700242 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700243 return DIRR_ERROR;
244 }
245
246 if (!nasm_stricmp(valstr, "unlimited")) {
247 val = LIMIT_MAX_VAL;
248 } else {
249 val = readnum(valstr, &rn_error);
250 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800251 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800252 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700253 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700254 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700255 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700256 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700257 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700258 if (val > LIMIT_MAX_VAL)
259 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700260 }
261
262 nasm_limit[i] = val;
263 return DIRR_OK;
264}
265
H. Peter Anvin892c4812018-05-30 14:43:46 -0700266int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400267{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700268 location.segment = segment;
269 if (segment == NO_SEG) {
270 location.offset = absolute.offset;
271 in_absolute = true;
272 } else {
273 location.offset = raa_read(offsets, segment);
274 in_absolute = false;
275 }
276 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400277}
278
279static void set_curr_offs(int64_t l_off)
280{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800281 if (in_absolute)
282 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400283 else
284 offsets = raa_write(offsets, location.segment, l_off);
285}
286
H. Peter Anvin892c4812018-05-30 14:43:46 -0700287static void increment_offset(int64_t delta)
288{
289 if (unlikely(delta == 0))
290 return;
291
292 location.offset += delta;
293 set_curr_offs(location.offset);
294}
295
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800296/*
297 * Define system-defined macros that are not part of
298 * macros/standard.mac.
299 */
300static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800301{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700302 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800303 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800304
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700305 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700306 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400307 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700308 strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400309 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700310 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400311 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700312 strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800314 }
315
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700316 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700317 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400318 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700319 strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400320 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700321 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400322 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700323 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400324 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800325 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700326
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700327 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700328 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400329 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800330 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400332 /*
333 * In case if output format is defined by alias
334 * we have to put shortname of the alias itself here
335 * otherwise ABI backward compatibility gets broken.
336 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700337 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400338 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400339 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800340
341 /*
342 * Output-format specific macros.
343 */
344 if (ofmt->stdmac)
345 preproc->extra_stdmac(ofmt->stdmac);
346
347 /*
348 * Debug format, if any
349 */
350 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700351 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800352 preproc->pre_define(temp);
353 }
354}
355
356/*
357 * Initialize the preprocessor, set up the include path, and define
358 * the system-included macros. This is called between passes 1 and 2
359 * of parsing the command options; ofmt and dfmt are defined at this
360 * point.
361 *
362 * Command-line specified preprocessor directives (-p, -d, -u,
363 * --pragma, --before) are processed after this function.
364 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300365static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800366{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800367 preproc->init();
368 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300369 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800370}
371
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300372static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700373{
374 FILE *deps;
375 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700376 bool wmake = (quote_for_make == quote_for_wmake);
377 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800378 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700379
380 if (!list)
381 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700382
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700383 wrapstr = wmake ? " &\n " : " \\\n ";
384 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385
386 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700387 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400388 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800389 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400390 return;
391 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700392 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400393 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700394 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700395
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700396 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800397 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700398 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400399 len = strlen(file);
400 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700401 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400402 linepos = 1;
403 }
404 fprintf(deps, " %s", file);
405 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700406 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700407 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700408 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700409
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800410 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700411 if (depend_emit_phony) {
412 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700413 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700414 nasm_free(file);
415 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700416 }
417
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800418 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700419
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700420 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400421 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700422}
423
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700424/* Convert a struct tm to a POSIX-style time constant */
425static int64_t make_posix_time(const struct tm *tm)
426{
427 int64_t t;
428 int64_t y = tm->tm_year;
429
430 /* See IEEE 1003.1:2004, section 4.14 */
431
432 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
433 t += tm->tm_yday;
434 t *= 24;
435 t += tm->tm_hour;
436 t *= 60;
437 t += tm->tm_min;
438 t *= 60;
439 t += tm->tm_sec;
440
441 return t;
442}
443
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700444/*
445 * Quote a filename string if and only if it is necessary.
446 * It is considered necessary if any one of these is true:
447 * 1. The filename contains control characters;
448 * 2. The filename starts or ends with a space or quote mark;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700449 * 3. The filename contains more than one space in a row;
450 * 4. The filename is empty.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700451 *
452 * The filename is returned in a newly allocated buffer.
453 */
454static char *nasm_quote_filename(const char *fn)
455{
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700456 const unsigned char *p =
457 (const unsigned char *)fn;
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300458 size_t len;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700459
460 if (!p || !*p)
461 return nasm_strdup("\"\"");
462
463 if (*p <= ' ' || nasm_isquote(*p)) {
464 goto quote;
465 } else {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700466 unsigned char cutoff = ' ';
467
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700468 while (*p) {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700469 if (*p < cutoff)
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700470 goto quote;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700471 cutoff = ' ' + (*p == ' ');
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700472 p++;
473 }
474 if (p[-1] <= ' ' || nasm_isquote(p[-1]))
475 goto quote;
476 }
477
478 /* Quoting not necessary */
479 return nasm_strdup(fn);
480
481quote:
Cyrill Gorcunov65c6ba82020-07-11 19:12:13 +0300482 len = strlen(fn);
483 return nasm_quote(fn, &len);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700484}
485
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700486static void timestamp(void)
487{
488 struct compile_time * const oct = &official_compile_time;
489 const struct tm *tp, *best_gm;
490
491 time(&oct->t);
492
493 best_gm = NULL;
494
495 tp = localtime(&oct->t);
496 if (tp) {
497 oct->local = *tp;
498 best_gm = &oct->local;
499 oct->have_local = true;
500 }
501
502 tp = gmtime(&oct->t);
503 if (tp) {
504 oct->gm = *tp;
505 best_gm = &oct->gm;
506 oct->have_gm = true;
507 if (!oct->have_local)
508 oct->local = oct->gm;
509 } else {
510 oct->gm = oct->local;
511 }
512
513 if (best_gm) {
514 oct->posix = make_posix_time(best_gm);
515 oct->have_posix = true;
516 }
517}
518
H. Peter Anvin038d8612007-04-12 16:54:50 +0000519int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700521 /* Do these as early as possible */
522 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700523 _progname = argv[0];
524 if (!_progname || !_progname[0])
525 _progname = "nasm";
526
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700527 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800528
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800529 iflag_set_default_cpu(&cpu);
530 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400531
H. Peter Anvin322bee02019-08-10 01:38:06 -0700532 set_default_limits();
533
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800534 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300535
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800536 _pass_type = PASS_INIT;
537 _passn = 0;
538
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700539 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000540
H. Peter Anvin13506202018-11-28 14:55:58 -0800541 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700542 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700543
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700544 /*
545 * We must call init_labels() before the command line parsing,
546 * because we may be setting prefixes/suffixes from the command
547 * line.
548 */
549 init_labels();
550
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000552 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000553
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400555 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000556
H. Peter Anvin55568c12016-10-03 19:46:49 -0700557 parse_cmdline(argc, argv, 1);
558 if (terminate_after_phase) {
559 if (want_usage)
560 usage();
561 return 1;
562 }
563
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800564 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800565 if (!using_debug_info) {
566 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800567 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800568 } else if (!debug_format) {
569 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300570 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800571 } else {
572 dfmt = dfmt_find(ofmt, debug_format);
573 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800574 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800575 debug_format, ofmt->shortname);
576 }
577 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000578
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300579 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800580
581 parse_cmdline(argc, argv, 2);
582 if (terminate_after_phase) {
583 if (want_usage)
584 usage();
585 return 1;
586 }
587
588 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800589 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000590
H. Peter Anvin34754622018-11-28 12:36:53 -0800591 /* Dependency filename if we are also doing other things */
592 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
593 if (outname)
594 depend_file = nasm_strcat(outname, ".d");
595 else
596 depend_file = filename_set_extension(inname, ".d");
597 }
598
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300599 /*
600 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800601 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300602 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300603 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800604 if (!outname && !(operating_mode & OP_PREPROCESS)) {
605 outname = filename_set_extension(inname, ofmt->extension);
606 if (!strcmp(outname, inname)) {
607 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800608 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 -0800609 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300610 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800611
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800612 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700613
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700614 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400615 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700616
H. Peter Anvin34754622018-11-28 12:36:53 -0800617 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000618 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700619
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400620 if (depend_missing_ok)
621 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700622
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800623 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 while ((line = preproc->getline()))
626 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800627 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800628 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400629 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000630 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700631 const char *file_name = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700632 char *quoted_file_name = nasm_quote_filename(file_name);
633 int32_t linnum = 0;
634 int32_t lineinc = 0;
635 FILE *out;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000636
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800637 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700638 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800640 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700641 out = ofile;
642 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 ofile = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700644 out = stdout;
645 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000646
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700647 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000648
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -0700649 _pass_type = PASS_PREPROC;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800650 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 while ((line = preproc->getline())) {
653 /*
654 * We generate %line directives if needed for later programs
655 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700656 struct src_location where = src_where();
657 if (file_name != where.filename) {
658 file_name = where.filename;
659 linnum = -1; /* Force a new %line statement */
660 lineinc = file_name ? 1 : 0;
661 nasm_free(quoted_file_name);
662 quoted_file_name = nasm_quote_filename(file_name);
663 } else if (lineinc) {
664 if (linnum + lineinc == where.lineno) {
665 /* Add one blank line to account for increment */
666 fputc('\n', out);
667 linnum += lineinc;
668 } else if (linnum - lineinc == where.lineno) {
669 /*
670 * Standing still, probably a macro. Set increment
671 * to zero.
672 */
673 lineinc = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700675 } else {
676 /* lineinc == 0 */
677 if (linnum + 1 == where.lineno)
678 lineinc = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700680
681 /* Skip blank lines if we will need a %line anyway */
682 if (linnum == -1 && !line[0])
683 continue;
684
685 if (linnum != where.lineno) {
686 fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
687 where.lineno, lineinc, quoted_file_name);
688 }
689 linnum = where.lineno + lineinc;
690
691 fputs(line, out);
692 fputc('\n', out);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700694
695 nasm_free(quoted_file_name);
696
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800697 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800698 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 if (ofile)
700 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700701 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400703 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400704 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400706 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700707 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400708 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800709 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400711 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400712 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700714 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200715
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400716 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800717 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400718 cleanup_labels();
719 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800720 if (ferror(ofile))
721 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400722 }
723
724 if (ofile) {
725 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700726 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400727 remove(outname);
728 ofile = NULL;
729 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000731
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800732 preproc->cleanup_session();
733
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700734 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400735 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700736
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000739
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 raa_free(offsets);
741 saa_free(forwrefs);
742 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000743 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700744 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800745 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700747 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000748}
749
H. Peter Anvineba20a72002-04-30 20:53:55 +0000750/*
751 * Get a parameter for a command line option.
752 * First arg must be in the form of e.g. -f...
753 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000755{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400757 if (p[2]) /* the parameter's in the option */
758 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800760 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000762 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800763 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000764 return NULL;
765}
766
H. Peter Anvindc242712007-11-18 11:55:10 -0800767/*
768 * Copy a filename
769 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800770static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800771{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800772 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700773 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800774
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800775 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800776}
777
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700778/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700779 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700780 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700781static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700782{
783 const char *p;
784 char *os, *q;
785
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400786 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700787 size_t nbs = 0;
788
789 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400790 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700791
792 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400793 switch (*p) {
794 case ' ':
795 case '\t':
796 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
797 n += nbs + 2;
798 nbs = 0;
799 break;
800 case '$':
801 case '#':
802 nbs = 0;
803 n += 2;
804 break;
805 case '\\':
806 nbs++;
807 n++;
808 break;
809 default:
810 nbs = 0;
811 n++;
812 break;
813 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700814 }
815
816 /* Convert N backslashes at the end of filename to 2N backslashes */
817 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400818 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700819
820 os = q = nasm_malloc(n);
821
822 nbs = 0;
823 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400824 switch (*p) {
825 case ' ':
826 case '\t':
827 while (nbs--)
828 *q++ = '\\';
829 *q++ = '\\';
830 *q++ = *p;
831 break;
832 case '$':
833 *q++ = *p;
834 *q++ = *p;
835 nbs = 0;
836 break;
837 case '#':
838 *q++ = '\\';
839 *q++ = *p;
840 nbs = 0;
841 break;
842 case '\\':
843 *q++ = *p;
844 nbs++;
845 break;
846 default:
847 *q++ = *p;
848 nbs = 0;
849 break;
850 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700851 }
852 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400853 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700854
855 *q = '\0';
856
857 return os;
858}
859
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700860/*
861 * Convert a string to a Watcom make-safe form
862 */
863static char *quote_for_wmake(const char *str)
864{
865 const char *p;
866 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700867 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700868
869 size_t n = 1; /* Terminating zero */
870
871 if (!str)
872 return NULL;
873
874 for (p = str; *p; p++) {
875 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700876 case ' ':
877 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700878 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700879 quote = true;
880 n++;
881 break;
882 case '\"':
883 quote = true;
884 n += 2;
885 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700886 case '$':
887 case '#':
888 n += 2;
889 break;
890 default:
891 n++;
892 break;
893 }
894 }
895
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700896 if (quote)
897 n += 2;
898
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700899 os = q = nasm_malloc(n);
900
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700901 if (quote)
902 *q++ = '\"';
903
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700904 for (p = str; *p; p++) {
905 switch (*p) {
906 case '$':
907 case '#':
908 *q++ = '$';
909 *q++ = *p;
910 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700911 case '\"':
912 *q++ = *p;
913 *q++ = *p;
914 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700915 default:
916 *q++ = *p;
917 break;
918 }
919 }
920
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700921 if (quote)
922 *q++ = '\"';
923
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700924 *q = '\0';
925
926 return os;
927}
928
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100929enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800930 OPT_BOGUS,
931 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700932 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800933 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700934 OPT_MANGLE,
935 OPT_INCLUDE,
936 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700937 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700938 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800939 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700940 OPT_NO_LINE,
941 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100942};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700943enum need_arg {
944 ARG_NO,
945 ARG_YES,
946 ARG_MAYBE
947};
948
H. Peter Anvin3366e312018-02-07 14:14:36 -0800949struct textargs {
950 const char *label;
951 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700952 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700953 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800954};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800955static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700956 {"v", OPT_VERSION, ARG_NO, 0},
957 {"version", OPT_VERSION, ARG_NO, 0},
958 {"help", OPT_HELP, ARG_NO, 0},
959 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
960 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
961 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
962 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
963 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
964 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
965 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
966 {"include", OPT_INCLUDE, ARG_YES, 0},
967 {"pragma", OPT_PRAGMA, ARG_YES, 0},
968 {"before", OPT_BEFORE, ARG_YES, 0},
969 {"limit-", OPT_LIMIT, ARG_YES, 0},
970 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
971 {"no-line", OPT_NO_LINE, ARG_NO, 0},
972 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
973 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000974};
975
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400976static void show_version(void)
977{
978 printf("NASM version %s compiled on %s%s\n",
979 nasm_version, nasm_date, nasm_compile_options);
980 exit(0);
981}
982
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700984static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000985{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000986 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800987 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000988
989 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800990 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700993 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 /* These parameters take values */
995 if (!(param = get_param(p, q, &advance)))
996 return advance;
997 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 switch (p[1]) {
1000 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 if (pass == 1)
1002 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001004
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001006 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001007 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -07001009
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 if (pass == 1) {
1012 ofmt = ofmt_find(param, &ofmt_alias);
1013 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001014 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001015 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001016 }
1017 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001018
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001019 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001020 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001021 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001022
H. Peter Anvin55568c12016-10-03 19:46:49 -07001023 if (!*param) {
1024 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +03001025 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 } else {
1027 while (*param) {
1028 switch (*param) {
1029 case '0': case '1': case '2': case '3': case '4':
1030 case '5': case '6': case '7': case '8': case '9':
1031 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001032
Chang S. Baea5786342018-08-15 23:22:21 +03001033 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
1034 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001035 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +03001036 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 else
Chang S. Baea5786342018-08-15 23:22:21 +03001038 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001039 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001040
H. Peter Anvin55568c12016-10-03 19:46:49 -07001041 case 'v':
1042 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001043 param++;
1044 opt_verbose_info = true;
1045 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001046
H. Peter Anvin55568c12016-10-03 19:46:49 -07001047 case 'x':
1048 param++;
Chang S. Baea5786342018-08-15 23:22:21 +03001049 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001051
H. Peter Anvin55568c12016-10-03 19:46:49 -07001052 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001053 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -07001054 *param);
1055 break;
1056 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001057 }
Chang S. Baea5786342018-08-15 23:22:21 +03001058 if (optimizing.level > MAX_OPTIMIZE)
1059 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001060 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001061 }
1062 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001063
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001064 case 'p': /* pre-include */
1065 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001066 if (pass == 2)
1067 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001068 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001069
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001070 case 'd': /* pre-define */
1071 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001072 if (pass == 2)
1073 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001074 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001075
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001076 case 'u': /* un-define */
1077 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001078 if (pass == 2)
1079 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001080 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001081
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001082 case 'i': /* include search path */
1083 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001084 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001085 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001086 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001087
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001088 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001089 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001090 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001091 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001092
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001093 case 'L': /* listing options */
1094 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001095 while (*param)
1096 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001097 }
1098 break;
1099
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001100 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001101 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001102 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001103 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001104
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001105 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001106 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001107 using_debug_info = true;
1108 debug_format = param;
1109 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001110 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001111
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001112 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001113 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001114 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1115 errfmt = &errfmt_msvc;
1116 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1117 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001119 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001120 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001122
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001124 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001125 using_debug_info = true;
1126 if (p[2])
1127 debug_format = nasm_skip_spaces(p + 2);
1128 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001130
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001132 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001133 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001137 /* legacy option */
1138 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 exit(0);
1140 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001144 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001145 nasm_ctype_tasm_mode();
1146 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001150 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001152
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001153 case 'e': /* preprocess only */
1154 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001155 if (pass == 1)
1156 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001158
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001159 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001160 if (pass == 1)
1161 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001163
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001164 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001165 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001166 if (pass == 2)
1167 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001168 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001169
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001171 if (pass == 1) {
1172 switch (p[2]) {
1173 case 'W':
1174 quote_for_make = quote_for_wmake;
1175 break;
1176 case 'D':
1177 case 'F':
1178 case 'T':
1179 case 'Q':
1180 advance = true;
1181 break;
1182 default:
1183 break;
1184 }
1185 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001186 switch (p[2]) {
1187 case 0:
1188 operating_mode = OP_DEPEND;
1189 break;
1190 case 'G':
1191 operating_mode = OP_DEPEND;
1192 depend_missing_ok = true;
1193 break;
1194 case 'P':
1195 depend_emit_phony = true;
1196 break;
1197 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001198 operating_mode |= OP_DEPEND;
1199 if (q && (q[0] != '-' || q[1] == '\0')) {
1200 depend_file = q;
1201 advance = true;
1202 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001203 break;
1204 case 'F':
1205 depend_file = q;
1206 advance = true;
1207 break;
1208 case 'T':
1209 depend_target = q;
1210 advance = true;
1211 break;
1212 case 'Q':
1213 depend_target = quote_for_make(q);
1214 advance = true;
1215 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001216 case 'W':
1217 /* handled in pass 1 */
1218 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001219 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001220 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001221 break;
1222 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001223 }
1224 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001225 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001226 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001227 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 case '-':
1231 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001232 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001233 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001234 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001235 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001236
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001237 p += 2;
1238
1239 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001240 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 break;
1242 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001243
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001244 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001245 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001246 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001247 olen = strlen(tx->label);
1248
1249 if (olen > plen)
1250 continue;
1251
1252 if (nasm_memicmp(p, tx->label, olen))
1253 continue;
1254
1255 if (tx->label[olen-1] == '-')
1256 break; /* Incomplete option */
1257
1258 if (!p[olen] || p[olen] == '=')
1259 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001261
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001262 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001263 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001264 }
1265
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001266 opt = tx->opt;
1267
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001268 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001269 if (param)
1270 *param++ = '\0';
1271
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001272 switch (tx->need_arg) {
1273 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001274 if (!param) {
1275 param = q;
1276 advance = true;
1277 }
1278
1279 /* Note: a null string is a valid parameter */
1280 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001281 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001282 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001284 break;
1285
1286 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001287 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001288 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001289 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001290 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001291 break;
1292
1293 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1294 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001295 }
1296
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001297 switch (opt) {
1298 case OPT_BOGUS:
1299 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001300 case OPT_VERSION:
1301 show_version();
1302 break;
1303 case OPT_ABORT_ON_PANIC:
1304 abort_on_panic = true;
1305 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001306 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001307 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001308 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001309 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001310 case OPT_INCLUDE:
1311 if (pass == 2)
1312 preproc->pre_include(q);
1313 break;
1314 case OPT_PRAGMA:
1315 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001316 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001317 break;
1318 case OPT_BEFORE:
1319 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001320 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001321 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001322 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001323 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001324 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001325 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001326 case OPT_KEEP_ALL:
1327 keep_all = true;
1328 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001329 case OPT_NO_LINE:
1330 pp_noline = true;
1331 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001332 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001333 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001334 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001335 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001336 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001337 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001338 default:
1339 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001341
1342 if (eqsave)
1343 *eqsave = '='; /* Restore = argument separator */
1344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 break;
1346 }
1347
1348 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001349 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 break;
1351 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001352 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001353 /* In theory we could allow multiple input files... */
1354 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001355 }
1356
1357 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001358}
1359
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360#define ARG_BUF_DELTA 128
1361
H. Peter Anvin55568c12016-10-03 19:46:49 -07001362static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001363{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001364 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001365 int bufsize, prevargsize;
1366
1367 bufsize = prevargsize = ARG_BUF_DELTA;
1368 buffer = nasm_malloc(ARG_BUF_DELTA);
1369 prevarg = nasm_malloc(ARG_BUF_DELTA);
1370 prevarg[0] = '\0';
1371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 p = buffer;
1374 while (1) { /* Loop to handle long lines */
1375 q = fgets(p, bufsize - (p - buffer), rfile);
1376 if (!q)
1377 break;
1378 p += strlen(p);
1379 if (p > buffer && p[-1] == '\n')
1380 break;
1381 if (p - buffer > bufsize - 10) {
1382 int offset;
1383 offset = p - buffer;
1384 bufsize += ARG_BUF_DELTA;
1385 buffer = nasm_realloc(buffer, bufsize);
1386 p = buffer + offset;
1387 }
1388 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001389
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 if (!q && p == buffer) {
1391 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 nasm_free(buffer);
1394 nasm_free(prevarg);
1395 return;
1396 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001397
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 /*
1399 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1400 * them are present at the end of the line.
1401 */
1402 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001403
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001404 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001407 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408
H. Peter Anvin55568c12016-10-03 19:46:49 -07001409 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411
Charles Crayne192d5b52007-10-18 19:02:42 -07001412 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 prevargsize += ARG_BUF_DELTA;
1414 prevarg = nasm_realloc(prevarg, prevargsize);
1415 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001416 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001417 }
1418}
1419
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001420/* Function to process args from a string of args, rather than the
1421 * argv array. Used by the environment variable and response file
1422 * processing.
1423 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001424static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001426 char *p, *q, *arg, *prevarg;
1427 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001428
1429 p = args;
1430 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001432 arg = NULL;
1433 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 q = p;
1435 while (*p && *p != separator)
1436 p++;
1437 while (*p == separator)
1438 *p++ = '\0';
1439 prevarg = arg;
1440 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001441 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001443 }
1444 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001445 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001446}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001447
H. Peter Anvin55568c12016-10-03 19:46:49 -07001448static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001449{
1450 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001451 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001452 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001453 perror(file);
1454 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001455 }
1456 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001457 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001458 }
1459 fclose(f);
1460}
1461
H. Peter Anvin55568c12016-10-03 19:46:49 -07001462static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463{
1464 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001465 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001466
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001467 /*
1468 * Initialize all the warnings to their default state, including
1469 * warning index 0 used for "always on".
1470 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001471 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001472
1473 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001474 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001475 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001476 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001477 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001479 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001481 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001482
1483 /*
1484 * Now process the actual command line.
1485 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001487 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 argv++;
1489 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001490 /*
1491 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 * arguments like the environment variable. This allows us
1493 * to have multiple arguments on a single line, which is
1494 * different to the -@resp file processing below for regular
1495 * NASM.
1496 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001497 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 argc--;
1499 argv++;
1500 }
1501 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001502 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001503 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001504 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001505 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001506 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001508 } else {
1509 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1510 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 }
1512 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001513 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001514 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001515 }
1516
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001517 /*
1518 * Look for basic command line typos. This definitely doesn't
1519 * catch all errors, but it might help cases of fumbled fingers.
1520 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001521 if (pass != 2)
1522 return;
1523
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001524 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001525 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001526 else if ((errname && !strcmp(inname, errname)) ||
1527 (outname && !strcmp(inname, outname)) ||
1528 (listname && !strcmp(inname, listname)) ||
1529 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001530 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001531
1532 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001533 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001534 if (!error_file) {
1535 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001536 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001537 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001538 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001539}
1540
H. Peter Anvin29651542018-12-18 19:14:40 -08001541static void forward_refs(insn *instruction)
1542{
1543 int i;
1544 struct forwrefinfo *fwinf;
1545
1546 instruction->forw_ref = false;
1547
1548 if (!optimizing.level)
1549 return; /* For -O0 don't bother */
1550
1551 if (!forwref)
1552 return;
1553
1554 if (forwref->lineno != globallineno)
1555 return;
1556
1557 instruction->forw_ref = true;
1558 do {
1559 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1560 forwref = saa_rstruct(forwrefs);
1561 } while (forwref && forwref->lineno == globallineno);
1562
1563 if (!pass_first())
1564 return;
1565
1566 for (i = 0; i < instruction->operands; i++) {
1567 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1568 fwinf = saa_wstruct(forwrefs);
1569 fwinf->lineno = globallineno;
1570 fwinf->operand = i;
1571 }
1572 }
1573}
1574
1575static void process_insn(insn *instruction)
1576{
1577 int32_t n;
1578 int64_t l;
1579
1580 if (!instruction->times)
1581 return; /* Nothing to do... */
1582
1583 nasm_assert(instruction->times > 0);
1584
1585 /*
1586 * NOTE: insn_size() can change instruction->times
1587 * (usually to 1) when called.
1588 */
1589 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001590 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001591 for (n = 1; n <= instruction->times; n++) {
1592 l = insn_size(location.segment, location.offset,
1593 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001594 /* l == -1 -> invalid instruction */
1595 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001596 increment_offset(l);
1597 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001598 if (list_option('p')) {
1599 struct out_data dummy;
1600 memset(&dummy, 0, sizeof dummy);
1601 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1602 dummy.offset = start;
1603 dummy.size = location.offset - start;
1604 lfmt->output(&dummy);
1605 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001606 } else {
1607 l = assemble(location.segment, location.offset,
1608 globalbits, instruction);
1609 /* We can't get an invalid instruction here */
1610 increment_offset(l);
1611
1612 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001613 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001614 for (n = 2; n <= instruction->times; n++) {
1615 l = assemble(location.segment, location.offset,
1616 globalbits, instruction);
1617 increment_offset(l);
1618 }
1619 lfmt->downlevel(LIST_TIMES);
1620 }
1621 }
1622}
1623
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001624static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001626 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001628 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001629 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001630
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001631 switch (cmd_sb) {
1632 case 16:
1633 break;
1634 case 32:
1635 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001636 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001637 break;
1638 case 64:
1639 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001640 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001641 break;
1642 default:
1643 panic();
1644 break;
1645 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001646
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001647 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001648
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001649 if (listname && !keep_all) {
1650 /* Remove the list file in case we die before the output pass */
1651 remove(listname);
1652 }
1653
1654 while (!terminate_after_phase && !pass_final()) {
1655 _passn++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001656 switch (pass_type()) {
1657 case PASS_INIT:
1658 _pass_type = PASS_FIRST;
1659 break;
1660 case PASS_OPT:
1661 if (global_offset_changed)
1662 break; /* One more optimization pass */
1663 /* fall through */
1664 default:
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001665 _pass_type++;
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001666 break;
1667 }
1668
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001669 global_offset_changed = 0;
1670
1671 /*
1672 * Create a warning buffer list unless we are in
1673 * pass 2 (everything will be emitted immediately in pass 2.)
1674 */
1675 if (warn_list) {
1676 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001677 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001678 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001679
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001680 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001681 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001682
H. Peter Anvincac0b192017-03-28 16:12:30 -07001683 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001685 if (listname) {
1686 if (pass_final() || list_on_every_pass()) {
1687 active_list_options = list_options;
1688 lfmt->init(listname);
1689 } else if (active_list_options) {
1690 /*
1691 * Looks like we used the list engine on a previous pass,
1692 * but now it is turned off, presumably via %pragma -p
1693 */
1694 lfmt->cleanup();
1695 if (!keep_all)
1696 remove(listname);
1697 active_list_options = 0;
1698 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001699 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001700
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001701 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001702 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 saa_rewind(forwrefs);
1704 forwref = saa_rstruct(forwrefs);
1705 raa_free(offsets);
1706 offsets = raa_init();
1707 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001708 location.segment = NO_SEG;
1709 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001710 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001711 location.known = true;
1712 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001713 switch_segment(ofmt->section(NULL, &globalbits));
1714 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001715
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001717
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001719 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001720 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001721 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001723 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001724 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001725 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001726 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001727 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001728 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001729
H. Peter Anvinc7131682017-03-07 17:45:01 -08001730 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001731 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001732 forward_refs(&output_ins);
1733 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001734 cleanup_insn(&output_ins);
1735
1736 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001739
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001740 preproc->cleanup_pass();
1741
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001742 /* We better not be having an error hold still... */
1743 nasm_assert(!errhold_stack);
1744
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001745 if (global_offset_changed) {
1746 switch (pass_type()) {
1747 case PASS_OPT:
1748 /*
1749 * This is the only pass type that can be executed more
1750 * than once, and therefore has the ability to stall.
1751 */
1752 if (global_offset_changed < prev_offset_changed) {
1753 prev_offset_changed = global_offset_changed;
1754 stall_count = 0;
1755 } else {
1756 stall_count++;
1757 }
1758
1759 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1760 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1761 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001762 nasm_nonfatalf(ERR_UNDEAD,
1763 "unable to find valid values for all labels "
1764 "after %"PRId64" passes; "
1765 "stalled for %"PRId64", giving up.",
1766 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001767 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001768 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001769 }
1770 break;
1771
1772 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001773 /*!
1774 *!phase [off] phase error during stabilization
1775 *! warns about symbols having changed values during
1776 *! the second-to-last assembly pass. This is not
1777 *! inherently fatal, but may be a source of bugs.
1778 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001779 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1780 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001781 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001782 break;
1783
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001784 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001785 nasm_nonfatalf(ERR_UNDEAD,
1786 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001787 break;
1788
1789 default:
1790 /* This is normal, we'll keep going... */
1791 break;
1792 }
1793 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001794
1795 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001796 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001797
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001798 if (opt_verbose_info && pass_final()) {
1799 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001800 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001801 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001802
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001803 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001804 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001805}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001806
Ed Berosetfa771012002-06-09 20:56:40 +00001807/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001808 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001809 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001810static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001811{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001812 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001813
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001814 if ((severity & ERR_MASK) >= ERR_FATAL)
1815 return 0; /* Fatal errors are never suppressible */
1816
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001817 /* Warnings MUST HAVE a warning category specifier! */
1818 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001819
1820 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001821 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001822
1823 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001824}
1825
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001826static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001827{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001828 errflags type = severity & ERR_MASK;
1829
H. Peter Anvin8f622462017-04-02 19:02:29 -07001830 /*
1831 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001832 * We can never skip fatal errors as by definition they cannot be
1833 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001834 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001835 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001836 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001837
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001838 /*
1839 * ERR_LISTMSG messages are always skipped; the list file
1840 * receives them anyway as this function is not consulted
1841 * for sending to the list file.
1842 */
1843 if (type == ERR_LISTMSG)
1844 return true;
1845
H. Peter Anvin (Intel)87a832e2020-07-03 18:44:55 -07001846 /*
1847 * This message not applicable unless it is the last pass we are going
1848 * to execute; this can be either the final code-generation pass or
1849 * the single pass executed in preproc-only mode.
1850 */
1851 return (severity & ERR_PASS2) && !pass_final_or_preproc();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001852}
1853
Ed Berosetfa771012002-06-09 20:56:40 +00001854/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001855 * check for suppressed message (usually warnings or notes)
1856 *
1857 * @param severity the severity of the warning or error
1858 * @return true if we should abort error/warning printing
1859 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001860static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001861{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001862 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001863 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001864 return false;
1865
1866 /* This error/warning is pointless if we are dead anyway */
1867 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1868 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001869
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001870 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1871 return true;
1872
1873 if (preproc && !(severity & ERR_PP_LISTMACRO))
1874 return preproc->suppress_error(severity);
1875
1876 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001877}
1878
1879/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001880 * Return the true error type (the ERR_MASK part) of the given
1881 * severity, accounting for warnings that may need to be promoted to
1882 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001883 *
1884 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001885 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001886 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001887static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001888{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001889 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1890 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001891
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001892 type = severity & ERR_MASK;
1893
1894 /* Promote warning to error? */
1895 if (type == ERR_WARNING) {
1896 uint8_t state = warning_state[warn_index(severity)];
1897 if ((state & warn_is_err) == warn_is_err)
1898 type = ERR_NONFATAL;
1899 }
1900
1901 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001902}
1903
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001904/*
1905 * The various error type prefixes
1906 */
1907static const char * const error_pfx_table[ERR_MASK+1] = {
1908 ";;; ", "debug: ", "info: ", "warning: ",
1909 "error: ", "fatal: ", "critical: ", "panic: "
1910};
1911static const char no_file_name[] = "nasm"; /* What to print if no file name */
1912
1913/*
1914 * For fatal/critical/panic errors, kill this process.
1915 */
1916static fatal_func die_hard(errflags true_type, errflags severity)
1917{
1918 fflush(NULL);
1919
1920 if (true_type == ERR_PANIC && abort_on_panic)
1921 abort();
1922
1923 if (ofile) {
1924 fclose(ofile);
1925 if (!keep_all)
1926 remove(outname);
1927 ofile = NULL;
1928 }
1929
1930 if (severity & ERR_USAGE)
1931 usage();
1932
1933 /* Terminate immediately */
1934 exit(true_type - ERR_FATAL + 1);
1935}
1936
1937/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001938 * Returns the struct src_location appropriate for use, after some
1939 * potential filename mangling.
1940 */
1941static struct src_location error_where(errflags severity)
1942{
1943 struct src_location where;
1944
1945 if (severity & ERR_NOFILE) {
1946 where.filename = NULL;
1947 where.lineno = 0;
1948 } else {
1949 where = src_where_error();
1950
1951 if (!where.filename) {
1952 where.filename =
1953 inname && inname[0] ? inname :
1954 outname && outname[0] ? outname :
1955 NULL;
1956 where.lineno = 0;
1957 }
1958 }
1959
1960 return where;
1961}
1962
1963/*
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001964 * error reporting for critical and panic errors: minimize
1965 * the amount of system dependencies for getting a message out,
1966 * and in particular try to avoid memory allocations.
1967 */
1968fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1969{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001970 struct src_location where;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001971 errflags true_type = severity & ERR_MASK;
1972 static bool been_here = false;
1973
1974 if (unlikely(been_here))
1975 abort(); /* Recursive error... just die */
1976
1977 been_here = true;
1978
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001979 where = error_where(severity);
1980 if (!where.filename)
1981 where.filename = no_file_name;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001982
1983 fputs(error_pfx_table[severity], error_file);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001984 fputs(where.filename, error_file);
1985 if (where.lineno) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001986 fprintf(error_file, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001987 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001988 }
1989 fputs(errfmt->beforemsg, error_file);
1990 vfprintf(error_file, fmt, args);
1991 fputc('\n', error_file);
1992
1993 die_hard(true_type, severity);
1994}
1995
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001996/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001997 * Stack of tentative error hold lists.
1998 */
1999struct nasm_errtext {
2000 struct nasm_errtext *next;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002001 char *msg; /* Owned by this structure */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002002 struct src_location where; /* Owned by the srcfile system */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002003 errflags severity;
2004 errflags true_type;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002005};
2006struct nasm_errhold {
2007 struct nasm_errhold *up;
2008 struct nasm_errtext *head, **tail;
2009};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002010
2011static void nasm_free_error(struct nasm_errtext *et)
2012{
2013 nasm_free(et->msg);
2014 nasm_free(et);
2015}
2016
2017static void nasm_issue_error(struct nasm_errtext *et);
2018
2019struct nasm_errhold *nasm_error_hold_push(void)
2020{
2021 struct nasm_errhold *eh;
2022
2023 nasm_new(eh);
2024 eh->up = errhold_stack;
2025 eh->tail = &eh->head;
2026 errhold_stack = eh;
2027
2028 return eh;
2029}
2030
2031void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
2032{
2033 struct nasm_errtext *et, *etmp;
2034
2035 /* Allow calling with a null argument saying no hold in the first place */
2036 if (!eh)
2037 return;
2038
2039 /* This *must* be the current top of the errhold stack */
2040 nasm_assert(eh == errhold_stack);
2041
2042 if (eh->head) {
2043 if (issue) {
2044 if (eh->up) {
2045 /* Commit the current hold list to the previous level */
2046 *eh->up->tail = eh->head;
2047 eh->up->tail = eh->tail;
2048 } else {
2049 /* Issue errors */
2050 list_for_each_safe(et, etmp, eh->head)
2051 nasm_issue_error(et);
2052 }
2053 } else {
2054 /* Free the list, drop errors */
2055 list_for_each_safe(et, etmp, eh->head)
2056 nasm_free_error(et);
2057 }
2058 }
2059
2060 errhold_stack = eh->up;
2061 nasm_free(eh);
2062}
2063
2064/**
Ed Berosetfa771012002-06-09 20:56:40 +00002065 * common error reporting
2066 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002067 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002068 * specific error message to error_file and may or may not return. It
2069 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002070 *
2071 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002072 * @param fmt the printf style format string
2073 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07002074void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002075{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002076 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002077 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002078
2079 if (true_type >= ERR_CRITICAL)
2080 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002081
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08002082 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08002083 return;
2084
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002085 nasm_new(et);
2086 et->severity = severity;
2087 et->true_type = true_type;
2088 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002089 et->where = error_where(severity);
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002090
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002091 if (errhold_stack && true_type <= ERR_NONFATAL) {
2092 /* It is a tentative error */
2093 *errhold_stack->tail = et;
2094 errhold_stack->tail = &et->next;
2095 } else {
2096 nasm_issue_error(et);
2097 }
2098
2099 /*
2100 * Don't do this before then, if we do, we lose messages in the list
2101 * file, as the list file is only generated in the last pass.
2102 */
2103 if (skip_this_pass(severity))
2104 return;
2105
2106 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2107 if (preproc)
2108 preproc->error_list_macros(severity);
2109}
2110
2111/*
2112 * Actually print, list and take action on an error
2113 */
2114static void nasm_issue_error(struct nasm_errtext *et)
2115{
2116 const char *pfx;
2117 char warnsuf[64]; /* Warning suffix */
2118 char linestr[64]; /* Formatted line number if applicable */
2119 const errflags severity = et->severity;
2120 const errflags true_type = et->true_type;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002121 const struct src_location where = et->where;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002122
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002123 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002124 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002125 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002126 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002127
H. Peter Anvinddb29062018-12-11 00:06:29 -08002128 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002129 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2130 /*
2131 * It's a warning without ERR_HERE defined, and we are not already
2132 * unwinding the macros that led us here.
2133 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002134 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002135 (true_type >= ERR_NONFATAL) ? "error=" : "",
2136 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002137 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002138
H. Peter Anvinddb29062018-12-11 00:06:29 -08002139 *linestr = 0;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002140 if (where.lineno) {
H. Peter Anvinddb29062018-12-11 00:06:29 -08002141 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002142 errfmt->beforeline, where.lineno, errfmt->afterline);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002143 }
2144
H. Peter Anvin77016c82018-12-10 22:29:49 -08002145 if (!skip_this_pass(severity)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002146 const char *file = where.filename ? where.filename : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002147 const char *here = "";
2148
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002149 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002150 here = where.filename ? " here" : " in an unknown location";
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002151 }
2152
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002153 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002154 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002155 /*
2156 * Buffer up warnings until we either get an error
2157 * or we are on the code-generation pass.
2158 */
2159 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2160 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002161 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002162 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002163 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002164 * Actually output an error. If we have buffered
2165 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002166 */
2167 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002168 strlist_write(warn_list, "\n", error_file);
2169 strlist_free(&warn_list);
2170 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002171
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002172 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2173 file, linestr, errfmt->beforemsg,
2174 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002175 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002176 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002177
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002178 /* Are we recursing from error_list_macros? */
2179 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002180 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002181
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002182 /*
2183 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002184 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002185 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002186 if (severity & ERR_HERE) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002187 if (where.lineno)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002188 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002189 pfx, et->msg, where.filename, where.lineno, warnsuf);
2190 else if (where.filename)
H. Peter Anvinddb29062018-12-11 00:06:29 -08002191 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07002192 pfx, et->msg, where.filename, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002193 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002194 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002195 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002196 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002197 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002198 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002199
H. Peter Anvin8f622462017-04-02 19:02:29 -07002200 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002201 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002202
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002203 if (true_type >= ERR_FATAL)
2204 die_hard(true_type, severity);
2205 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002206 terminate_after_phase = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002207
2208done:
2209 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002210}
2211
H. Peter Anvin734b1882002-04-30 21:01:08 +00002212static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002213{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002214 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002215}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002216
H. Peter Anvin322bee02019-08-10 01:38:06 -07002217static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002218{
2219 int i;
2220
H. Peter Anvin322bee02019-08-10 01:38:06 -07002221 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002222 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2223 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002224 _progname, _progname);
2225 fputs(
2226 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002227 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002228 "\n"
2229 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002230 " -v (or --v) print the NASM version number and exit\n"
2231 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002232 "\n"
2233 " -o outfile write output to outfile\n"
2234 " --keep-all output files will not be removed even if an error happens\n"
2235 "\n"
2236 " -Xformat specifiy error reporting format (gnu or vc)\n"
2237 " -s redirect error messages to stdout\n"
2238 " -Zfile redirect error messages to file\n"
2239 "\n"
2240 " -M generate Makefile dependencies on stdout\n"
2241 " -MG d:o, missing files assumed generated\n"
2242 " -MF file set Makefile dependency file\n"
2243 " -MD file assemble and generate dependencies\n"
2244 " -MT file dependency target name\n"
2245 " -MQ file dependency target name (quoted)\n"
2246 " -MP emit phony targets\n"
2247 "\n"
2248 " -f format select output file format\n"
2249 , out);
2250 ofmt_list(ofmt, out);
2251 fputs(
2252 "\n"
2253 " -g generate debugging information\n"
2254 " -F format select a debugging format (output format dependent)\n"
2255 " -gformat same as -g -F format\n"
2256 , out);
2257 dfmt_list(out);
2258 fputs(
2259 "\n"
2260 " -l listfile write listing to a list file\n"
2261 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002262 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002263 " -Ld show byte and repeat counts in decimal, not hex\n"
2264 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002265 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002266 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002267 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002268 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002269 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002270 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002271 "\n"
2272 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2273 " -O0 no optimization\n"
2274 " -O1 minimal optimization\n"
2275 " -Ox multipass optimization (default)\n"
2276 " -Ov display the number of passes executed at the end\n"
2277 " -t assemble in limited SciTech TASM compatible mode\n"
2278 "\n"
2279 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2280 " -a don't preprocess (assemble only)\n"
2281 " -Ipath add a pathname to the include file path\n"
2282 " -Pfile pre-include a file (also --include)\n"
2283 " -Dmacro[=str] pre-define a macro\n"
2284 " -Umacro undefine a macro\n"
2285 " --pragma str pre-executes a specific %%pragma\n"
2286 " --before str add line (usually a preprocessor statement) before the input\n"
2287 " --no-line ignore %line directives in input\n"
2288 "\n"
2289 " --prefix str prepend the given string to the names of all extern,\n"
2290 " common and global symbols (also --gprefix)\n"
2291 " --suffix str append the given string to the names of all extern,\n"
2292 " common and global symbols (also --gprefix)\n"
2293 " --lprefix str prepend the given string to local symbols\n"
2294 " --lpostfix str append the given string to local symbols\n"
2295 "\n"
2296 " -w+x enable warning x (also -Wx)\n"
2297 " -w-x disable warning x (also -Wno-x)\n"
2298 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2299 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2300 , out);
2301
2302 fprintf(out, " %-20s %s\n",
2303 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2304
2305 for (i = 1; i < WARN_IDX_ALL; i++) {
2306 const char *me = warning_name[i];
2307 const char *prev = warning_name[i-1];
2308 const char *next = warning_name[i+1];
2309
2310 if (prev) {
2311 int prev_len = strlen(prev);
2312 const char *dash = me;
2313
2314 while ((dash = strchr(dash+1, '-'))) {
2315 int prefix_len = dash - me; /* Not including final dash */
2316 if (strncmp(next, me, prefix_len+1)) {
2317 /* Only one or last option with this prefix */
2318 break;
2319 }
2320 if (prefix_len >= prev_len ||
2321 strncmp(prev, me, prefix_len) ||
2322 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2323 /* This prefix is different from the previous option */
2324 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2325 prefix_len, me, prefix_len+1, me);
2326 }
2327 }
2328 }
2329
2330 fprintf(out, " %-20s %s%s\n",
2331 warning_name[i], warning_help[i],
2332 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2333 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2334 }
2335
2336 fputs(
2337 "\n"
2338 " --limit-X val set execution limit X\n"
2339 , out);
2340
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002341
2342 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002343 fprintf(out, " %-20s %s [",
2344 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002345 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002346 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002347 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002348 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002349 }
2350 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002351}