blob: 333ba70cc46813c3624977f5392fe90d064f3891 [file] [log] [blame]
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter 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{
108 "init", "first", "optimize", "stabilize", "final"
109};
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;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700458
459 if (!p || !*p)
460 return nasm_strdup("\"\"");
461
462 if (*p <= ' ' || nasm_isquote(*p)) {
463 goto quote;
464 } else {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700465 unsigned char cutoff = ' ';
466
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700467 while (*p) {
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700468 if (*p < cutoff)
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700469 goto quote;
H. Peter Anvin (Intel)3957f6f2020-06-14 20:17:57 -0700470 cutoff = ' ' + (*p == ' ');
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700471 p++;
472 }
473 if (p[-1] <= ' ' || nasm_isquote(p[-1]))
474 goto quote;
475 }
476
477 /* Quoting not necessary */
478 return nasm_strdup(fn);
479
480quote:
481 return nasm_quote(fn, NULL);
482}
483
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700484static void timestamp(void)
485{
486 struct compile_time * const oct = &official_compile_time;
487 const struct tm *tp, *best_gm;
488
489 time(&oct->t);
490
491 best_gm = NULL;
492
493 tp = localtime(&oct->t);
494 if (tp) {
495 oct->local = *tp;
496 best_gm = &oct->local;
497 oct->have_local = true;
498 }
499
500 tp = gmtime(&oct->t);
501 if (tp) {
502 oct->gm = *tp;
503 best_gm = &oct->gm;
504 oct->have_gm = true;
505 if (!oct->have_local)
506 oct->local = oct->gm;
507 } else {
508 oct->gm = oct->local;
509 }
510
511 if (best_gm) {
512 oct->posix = make_posix_time(best_gm);
513 oct->have_posix = true;
514 }
515}
516
H. Peter Anvin038d8612007-04-12 16:54:50 +0000517int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700519 /* Do these as early as possible */
520 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700521 _progname = argv[0];
522 if (!_progname || !_progname[0])
523 _progname = "nasm";
524
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700525 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800526
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800527 iflag_set_default_cpu(&cpu);
528 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400529
H. Peter Anvin322bee02019-08-10 01:38:06 -0700530 set_default_limits();
531
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800532 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300533
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800534 _pass_type = PASS_INIT;
535 _passn = 0;
536
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700537 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000538
H. Peter Anvin13506202018-11-28 14:55:58 -0800539 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700540 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700541
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700542 /*
543 * We must call init_labels() before the command line parsing,
544 * because we may be setting prefixes/suffixes from the command
545 * line.
546 */
547 init_labels();
548
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000549 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000550 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400553 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000554
H. Peter Anvin55568c12016-10-03 19:46:49 -0700555 parse_cmdline(argc, argv, 1);
556 if (terminate_after_phase) {
557 if (want_usage)
558 usage();
559 return 1;
560 }
561
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800562 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800563 if (!using_debug_info) {
564 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800565 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800566 } else if (!debug_format) {
567 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300568 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800569 } else {
570 dfmt = dfmt_find(ofmt, debug_format);
571 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800572 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800573 debug_format, ofmt->shortname);
574 }
575 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000576
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300577 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800578
579 parse_cmdline(argc, argv, 2);
580 if (terminate_after_phase) {
581 if (want_usage)
582 usage();
583 return 1;
584 }
585
586 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800587 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000588
H. Peter Anvin34754622018-11-28 12:36:53 -0800589 /* Dependency filename if we are also doing other things */
590 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
591 if (outname)
592 depend_file = nasm_strcat(outname, ".d");
593 else
594 depend_file = filename_set_extension(inname, ".d");
595 }
596
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300597 /*
598 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800599 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300600 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300601 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800602 if (!outname && !(operating_mode & OP_PREPROCESS)) {
603 outname = filename_set_extension(inname, ofmt->extension);
604 if (!strcmp(outname, inname)) {
605 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800606 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 -0800607 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300608 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800609
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800610 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700611
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700612 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400613 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700614
H. Peter Anvin34754622018-11-28 12:36:53 -0800615 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000616 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700617
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400618 if (depend_missing_ok)
619 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700620
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800621 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 while ((line = preproc->getline()))
624 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800625 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800626 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400627 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000628 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700629 const char *file_name = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700630 char *quoted_file_name = nasm_quote_filename(file_name);
631 int32_t linnum = 0;
632 int32_t lineinc = 0;
633 FILE *out;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000634
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800635 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700636 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800638 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700639 out = ofile;
640 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 ofile = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700642 out = stdout;
643 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000644
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700645 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000646
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800647 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
648 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800649
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 while ((line = preproc->getline())) {
651 /*
652 * We generate %line directives if needed for later programs
653 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700654 struct src_location where = src_where();
655 if (file_name != where.filename) {
656 file_name = where.filename;
657 linnum = -1; /* Force a new %line statement */
658 lineinc = file_name ? 1 : 0;
659 nasm_free(quoted_file_name);
660 quoted_file_name = nasm_quote_filename(file_name);
661 } else if (lineinc) {
662 if (linnum + lineinc == where.lineno) {
663 /* Add one blank line to account for increment */
664 fputc('\n', out);
665 linnum += lineinc;
666 } else if (linnum - lineinc == where.lineno) {
667 /*
668 * Standing still, probably a macro. Set increment
669 * to zero.
670 */
671 lineinc = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700673 } else {
674 /* lineinc == 0 */
675 if (linnum + 1 == where.lineno)
676 lineinc = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700678
679 /* Skip blank lines if we will need a %line anyway */
680 if (linnum == -1 && !line[0])
681 continue;
682
683 if (linnum != where.lineno) {
684 fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
685 where.lineno, lineinc, quoted_file_name);
686 }
687 linnum = where.lineno + lineinc;
688
689 fputs(line, out);
690 fputc('\n', out);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700692
693 nasm_free(quoted_file_name);
694
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800695 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800696 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 if (ofile)
698 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700699 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400701 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400702 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400704 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700705 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400706 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800707 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400709 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400710 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700712 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200713
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400714 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800715 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400716 cleanup_labels();
717 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800718 if (ferror(ofile))
719 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400720 }
721
722 if (ofile) {
723 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700724 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400725 remove(outname);
726 ofile = NULL;
727 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000729
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800730 preproc->cleanup_session();
731
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700732 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400733 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700734
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000735 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 raa_free(offsets);
739 saa_free(forwrefs);
740 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000741 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700742 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800743 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000744
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700745 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000746}
747
H. Peter Anvineba20a72002-04-30 20:53:55 +0000748/*
749 * Get a parameter for a command line option.
750 * First arg must be in the form of e.g. -f...
751 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800752static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000753{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400755 if (p[2]) /* the parameter's in the option */
756 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800758 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000760 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800761 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000762 return NULL;
763}
764
H. Peter Anvindc242712007-11-18 11:55:10 -0800765/*
766 * Copy a filename
767 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800768static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800769{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800770 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700771 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800772
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800773 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800774}
775
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700776/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700777 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700778 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700779static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700780{
781 const char *p;
782 char *os, *q;
783
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400784 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700785 size_t nbs = 0;
786
787 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400788 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700789
790 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400791 switch (*p) {
792 case ' ':
793 case '\t':
794 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
795 n += nbs + 2;
796 nbs = 0;
797 break;
798 case '$':
799 case '#':
800 nbs = 0;
801 n += 2;
802 break;
803 case '\\':
804 nbs++;
805 n++;
806 break;
807 default:
808 nbs = 0;
809 n++;
810 break;
811 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700812 }
813
814 /* Convert N backslashes at the end of filename to 2N backslashes */
815 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400816 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700817
818 os = q = nasm_malloc(n);
819
820 nbs = 0;
821 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400822 switch (*p) {
823 case ' ':
824 case '\t':
825 while (nbs--)
826 *q++ = '\\';
827 *q++ = '\\';
828 *q++ = *p;
829 break;
830 case '$':
831 *q++ = *p;
832 *q++ = *p;
833 nbs = 0;
834 break;
835 case '#':
836 *q++ = '\\';
837 *q++ = *p;
838 nbs = 0;
839 break;
840 case '\\':
841 *q++ = *p;
842 nbs++;
843 break;
844 default:
845 *q++ = *p;
846 nbs = 0;
847 break;
848 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700849 }
850 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400851 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700852
853 *q = '\0';
854
855 return os;
856}
857
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700858/*
859 * Convert a string to a Watcom make-safe form
860 */
861static char *quote_for_wmake(const char *str)
862{
863 const char *p;
864 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700865 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700866
867 size_t n = 1; /* Terminating zero */
868
869 if (!str)
870 return NULL;
871
872 for (p = str; *p; p++) {
873 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700874 case ' ':
875 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700876 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700877 quote = true;
878 n++;
879 break;
880 case '\"':
881 quote = true;
882 n += 2;
883 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700884 case '$':
885 case '#':
886 n += 2;
887 break;
888 default:
889 n++;
890 break;
891 }
892 }
893
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700894 if (quote)
895 n += 2;
896
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700897 os = q = nasm_malloc(n);
898
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700899 if (quote)
900 *q++ = '\"';
901
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700902 for (p = str; *p; p++) {
903 switch (*p) {
904 case '$':
905 case '#':
906 *q++ = '$';
907 *q++ = *p;
908 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700909 case '\"':
910 *q++ = *p;
911 *q++ = *p;
912 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700913 default:
914 *q++ = *p;
915 break;
916 }
917 }
918
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700919 if (quote)
920 *q++ = '\"';
921
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700922 *q = '\0';
923
924 return os;
925}
926
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100927enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800928 OPT_BOGUS,
929 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700930 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800931 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700932 OPT_MANGLE,
933 OPT_INCLUDE,
934 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700935 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700936 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800937 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700938 OPT_NO_LINE,
939 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100940};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700941enum need_arg {
942 ARG_NO,
943 ARG_YES,
944 ARG_MAYBE
945};
946
H. Peter Anvin3366e312018-02-07 14:14:36 -0800947struct textargs {
948 const char *label;
949 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700950 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700951 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800952};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800953static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700954 {"v", OPT_VERSION, ARG_NO, 0},
955 {"version", OPT_VERSION, ARG_NO, 0},
956 {"help", OPT_HELP, ARG_NO, 0},
957 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
958 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
959 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
960 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
961 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
962 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
963 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
964 {"include", OPT_INCLUDE, ARG_YES, 0},
965 {"pragma", OPT_PRAGMA, ARG_YES, 0},
966 {"before", OPT_BEFORE, ARG_YES, 0},
967 {"limit-", OPT_LIMIT, ARG_YES, 0},
968 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
969 {"no-line", OPT_NO_LINE, ARG_NO, 0},
970 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
971 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000972};
973
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400974static void show_version(void)
975{
976 printf("NASM version %s compiled on %s%s\n",
977 nasm_version, nasm_date, nasm_compile_options);
978 exit(0);
979}
980
H. Peter Anvin423e3812007-11-15 17:12:29 -0800981static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700982static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000984 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800985 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000986
987 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700991 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400992 /* These parameters take values */
993 if (!(param = get_param(p, q, &advance)))
994 return advance;
995 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800996
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 switch (p[1]) {
998 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700999 if (pass == 1)
1000 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001002
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001004 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001005 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -07001007
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 if (pass == 1) {
1010 ofmt = ofmt_find(param, &ofmt_alias);
1011 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001012 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001013 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001014 }
1015 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001016
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001017 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001018 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001019 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001020
H. Peter Anvin55568c12016-10-03 19:46:49 -07001021 if (!*param) {
1022 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +03001023 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 } else {
1025 while (*param) {
1026 switch (*param) {
1027 case '0': case '1': case '2': case '3': case '4':
1028 case '5': case '6': case '7': case '8': case '9':
1029 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001030
Chang S. Baea5786342018-08-15 23:22:21 +03001031 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
1032 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001033 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +03001034 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001035 else
Chang S. Baea5786342018-08-15 23:22:21 +03001036 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001038
H. Peter Anvin55568c12016-10-03 19:46:49 -07001039 case 'v':
1040 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001041 param++;
1042 opt_verbose_info = true;
1043 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001044
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 case 'x':
1046 param++;
Chang S. Baea5786342018-08-15 23:22:21 +03001047 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001049
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001051 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -07001052 *param);
1053 break;
1054 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001055 }
Chang S. Baea5786342018-08-15 23:22:21 +03001056 if (optimizing.level > MAX_OPTIMIZE)
1057 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001058 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001059 }
1060 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001061
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001062 case 'p': /* pre-include */
1063 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001064 if (pass == 2)
1065 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001066 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001067
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001068 case 'd': /* pre-define */
1069 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001070 if (pass == 2)
1071 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001072 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001073
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001074 case 'u': /* un-define */
1075 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001076 if (pass == 2)
1077 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001078 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001079
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001080 case 'i': /* include search path */
1081 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001082 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001083 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001084 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001085
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001086 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001087 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001088 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001089 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001090
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001091 case 'L': /* listing options */
1092 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001093 while (*param)
1094 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001095 }
1096 break;
1097
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001098 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001099 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001100 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001101 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001102
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001103 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001104 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001105 using_debug_info = true;
1106 debug_format = param;
1107 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001108 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001109
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001110 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001111 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001112 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1113 errfmt = &errfmt_msvc;
1114 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1115 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001116 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001117 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001120
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001122 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001123 using_debug_info = true;
1124 if (p[2])
1125 debug_format = nasm_skip_spaces(p + 2);
1126 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001130 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001131 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001135 /* legacy option */
1136 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 exit(0);
1138 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001141 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001142 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 nasm_ctype_tasm_mode();
1144 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001148 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001150
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001151 case 'e': /* preprocess only */
1152 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001153 if (pass == 1)
1154 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001156
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001157 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001158 if (pass == 1)
1159 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001161
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001162 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001163 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001164 if (pass == 2)
1165 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001166 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001169 if (pass == 1) {
1170 switch (p[2]) {
1171 case 'W':
1172 quote_for_make = quote_for_wmake;
1173 break;
1174 case 'D':
1175 case 'F':
1176 case 'T':
1177 case 'Q':
1178 advance = true;
1179 break;
1180 default:
1181 break;
1182 }
1183 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001184 switch (p[2]) {
1185 case 0:
1186 operating_mode = OP_DEPEND;
1187 break;
1188 case 'G':
1189 operating_mode = OP_DEPEND;
1190 depend_missing_ok = true;
1191 break;
1192 case 'P':
1193 depend_emit_phony = true;
1194 break;
1195 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001196 operating_mode |= OP_DEPEND;
1197 if (q && (q[0] != '-' || q[1] == '\0')) {
1198 depend_file = q;
1199 advance = true;
1200 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001201 break;
1202 case 'F':
1203 depend_file = q;
1204 advance = true;
1205 break;
1206 case 'T':
1207 depend_target = q;
1208 advance = true;
1209 break;
1210 case 'Q':
1211 depend_target = quote_for_make(q);
1212 advance = true;
1213 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001214 case 'W':
1215 /* handled in pass 1 */
1216 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001217 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001218 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001219 break;
1220 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001221 }
1222 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001223 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001224 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001225 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 case '-':
1229 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001230 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001231 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001232 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001233 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001234
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001235 p += 2;
1236
1237 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001238 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 break;
1240 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001241
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001242 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001243 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001244 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001245 olen = strlen(tx->label);
1246
1247 if (olen > plen)
1248 continue;
1249
1250 if (nasm_memicmp(p, tx->label, olen))
1251 continue;
1252
1253 if (tx->label[olen-1] == '-')
1254 break; /* Incomplete option */
1255
1256 if (!p[olen] || p[olen] == '=')
1257 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001259
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001260 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001261 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001262 }
1263
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001264 opt = tx->opt;
1265
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001266 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001267 if (param)
1268 *param++ = '\0';
1269
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001270 switch (tx->need_arg) {
1271 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001272 if (!param) {
1273 param = q;
1274 advance = true;
1275 }
1276
1277 /* Note: a null string is a valid parameter */
1278 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001279 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001280 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001282 break;
1283
1284 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001285 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001286 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001287 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001288 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001289 break;
1290
1291 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1292 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001293 }
1294
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001295 switch (opt) {
1296 case OPT_BOGUS:
1297 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001298 case OPT_VERSION:
1299 show_version();
1300 break;
1301 case OPT_ABORT_ON_PANIC:
1302 abort_on_panic = true;
1303 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001304 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001305 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001306 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001307 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001308 case OPT_INCLUDE:
1309 if (pass == 2)
1310 preproc->pre_include(q);
1311 break;
1312 case OPT_PRAGMA:
1313 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001314 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001315 break;
1316 case OPT_BEFORE:
1317 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001318 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001319 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001320 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001321 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001322 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001323 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001324 case OPT_KEEP_ALL:
1325 keep_all = true;
1326 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001327 case OPT_NO_LINE:
1328 pp_noline = true;
1329 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001330 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001331 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001332 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001333 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001334 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001335 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001336 default:
1337 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001339
1340 if (eqsave)
1341 *eqsave = '='; /* Restore = argument separator */
1342
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 break;
1344 }
1345
1346 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001347 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 break;
1349 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001350 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001351 /* In theory we could allow multiple input files... */
1352 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001353 }
1354
1355 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001356}
1357
H. Peter Anvineba20a72002-04-30 20:53:55 +00001358#define ARG_BUF_DELTA 128
1359
H. Peter Anvin55568c12016-10-03 19:46:49 -07001360static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001361{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001362 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001363 int bufsize, prevargsize;
1364
1365 bufsize = prevargsize = ARG_BUF_DELTA;
1366 buffer = nasm_malloc(ARG_BUF_DELTA);
1367 prevarg = nasm_malloc(ARG_BUF_DELTA);
1368 prevarg[0] = '\0';
1369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 p = buffer;
1372 while (1) { /* Loop to handle long lines */
1373 q = fgets(p, bufsize - (p - buffer), rfile);
1374 if (!q)
1375 break;
1376 p += strlen(p);
1377 if (p > buffer && p[-1] == '\n')
1378 break;
1379 if (p - buffer > bufsize - 10) {
1380 int offset;
1381 offset = p - buffer;
1382 bufsize += ARG_BUF_DELTA;
1383 buffer = nasm_realloc(buffer, bufsize);
1384 p = buffer + offset;
1385 }
1386 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 if (!q && p == buffer) {
1389 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001390 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 nasm_free(buffer);
1392 nasm_free(prevarg);
1393 return;
1394 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 /*
1397 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1398 * them are present at the end of the line.
1399 */
1400 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001401
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001402 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001405 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406
H. Peter Anvin55568c12016-10-03 19:46:49 -07001407 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001409
Charles Crayne192d5b52007-10-18 19:02:42 -07001410 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 prevargsize += ARG_BUF_DELTA;
1412 prevarg = nasm_realloc(prevarg, prevargsize);
1413 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001414 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001415 }
1416}
1417
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001418/* Function to process args from a string of args, rather than the
1419 * argv array. Used by the environment variable and response file
1420 * processing.
1421 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001422static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001424 char *p, *q, *arg, *prevarg;
1425 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001426
1427 p = args;
1428 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001430 arg = NULL;
1431 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 q = p;
1433 while (*p && *p != separator)
1434 p++;
1435 while (*p == separator)
1436 *p++ = '\0';
1437 prevarg = arg;
1438 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001439 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001441 }
1442 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001443 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001444}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001445
H. Peter Anvin55568c12016-10-03 19:46:49 -07001446static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001447{
1448 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001449 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001450 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001451 perror(file);
1452 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001453 }
1454 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001455 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001456 }
1457 fclose(f);
1458}
1459
H. Peter Anvin55568c12016-10-03 19:46:49 -07001460static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461{
1462 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001463 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001464
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001465 /*
1466 * Initialize all the warnings to their default state, including
1467 * warning index 0 used for "always on".
1468 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001469 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001470
1471 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001472 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001473 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001474 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001475 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001477 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001479 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001480
1481 /*
1482 * Now process the actual command line.
1483 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001485 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 argv++;
1487 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001488 /*
1489 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 * arguments like the environment variable. This allows us
1491 * to have multiple arguments on a single line, which is
1492 * different to the -@resp file processing below for regular
1493 * NASM.
1494 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001495 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 argc--;
1497 argv++;
1498 }
1499 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001500 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001501 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001502 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001503 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001504 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001506 } else {
1507 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1508 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 }
1510 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001511 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001512 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001513 }
1514
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001515 /*
1516 * Look for basic command line typos. This definitely doesn't
1517 * catch all errors, but it might help cases of fumbled fingers.
1518 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001519 if (pass != 2)
1520 return;
1521
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001522 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001523 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001524 else if ((errname && !strcmp(inname, errname)) ||
1525 (outname && !strcmp(inname, outname)) ||
1526 (listname && !strcmp(inname, listname)) ||
1527 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001528 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001529
1530 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001531 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001532 if (!error_file) {
1533 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001534 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001535 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001536 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001537}
1538
H. Peter Anvin29651542018-12-18 19:14:40 -08001539static void forward_refs(insn *instruction)
1540{
1541 int i;
1542 struct forwrefinfo *fwinf;
1543
1544 instruction->forw_ref = false;
1545
1546 if (!optimizing.level)
1547 return; /* For -O0 don't bother */
1548
1549 if (!forwref)
1550 return;
1551
1552 if (forwref->lineno != globallineno)
1553 return;
1554
1555 instruction->forw_ref = true;
1556 do {
1557 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1558 forwref = saa_rstruct(forwrefs);
1559 } while (forwref && forwref->lineno == globallineno);
1560
1561 if (!pass_first())
1562 return;
1563
1564 for (i = 0; i < instruction->operands; i++) {
1565 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1566 fwinf = saa_wstruct(forwrefs);
1567 fwinf->lineno = globallineno;
1568 fwinf->operand = i;
1569 }
1570 }
1571}
1572
1573static void process_insn(insn *instruction)
1574{
1575 int32_t n;
1576 int64_t l;
1577
1578 if (!instruction->times)
1579 return; /* Nothing to do... */
1580
1581 nasm_assert(instruction->times > 0);
1582
1583 /*
1584 * NOTE: insn_size() can change instruction->times
1585 * (usually to 1) when called.
1586 */
1587 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001588 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001589 for (n = 1; n <= instruction->times; n++) {
1590 l = insn_size(location.segment, location.offset,
1591 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001592 /* l == -1 -> invalid instruction */
1593 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001594 increment_offset(l);
1595 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001596 if (list_option('p')) {
1597 struct out_data dummy;
1598 memset(&dummy, 0, sizeof dummy);
1599 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1600 dummy.offset = start;
1601 dummy.size = location.offset - start;
1602 lfmt->output(&dummy);
1603 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001604 } else {
1605 l = assemble(location.segment, location.offset,
1606 globalbits, instruction);
1607 /* We can't get an invalid instruction here */
1608 increment_offset(l);
1609
1610 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001611 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001612 for (n = 2; n <= instruction->times; n++) {
1613 l = assemble(location.segment, location.offset,
1614 globalbits, instruction);
1615 increment_offset(l);
1616 }
1617 lfmt->downlevel(LIST_TIMES);
1618 }
1619 }
1620}
1621
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001622static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001623{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001624 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001626 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001627 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001628
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001629 switch (cmd_sb) {
1630 case 16:
1631 break;
1632 case 32:
1633 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001634 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001635 break;
1636 case 64:
1637 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001638 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001639 break;
1640 default:
1641 panic();
1642 break;
1643 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001645 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001646
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001647 if (listname && !keep_all) {
1648 /* Remove the list file in case we die before the output pass */
1649 remove(listname);
1650 }
1651
1652 while (!terminate_after_phase && !pass_final()) {
1653 _passn++;
1654 if (pass_type() != PASS_OPT || !global_offset_changed)
1655 _pass_type++;
1656 global_offset_changed = 0;
1657
1658 /*
1659 * Create a warning buffer list unless we are in
1660 * pass 2 (everything will be emitted immediately in pass 2.)
1661 */
1662 if (warn_list) {
1663 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001664 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001665 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001666
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001667 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001668 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001669
H. Peter Anvincac0b192017-03-28 16:12:30 -07001670 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001672 if (listname) {
1673 if (pass_final() || list_on_every_pass()) {
1674 active_list_options = list_options;
1675 lfmt->init(listname);
1676 } else if (active_list_options) {
1677 /*
1678 * Looks like we used the list engine on a previous pass,
1679 * but now it is turned off, presumably via %pragma -p
1680 */
1681 lfmt->cleanup();
1682 if (!keep_all)
1683 remove(listname);
1684 active_list_options = 0;
1685 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001686 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001687
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001688 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001689 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 saa_rewind(forwrefs);
1691 forwref = saa_rstruct(forwrefs);
1692 raa_free(offsets);
1693 offsets = raa_init();
1694 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001695 location.segment = NO_SEG;
1696 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001697 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001698 location.known = true;
1699 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001700 switch_segment(ofmt->section(NULL, &globalbits));
1701 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001702
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001704
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001706 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001707 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001708 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001710 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001711 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001712 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001713 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001714 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001715 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001716
H. Peter Anvinc7131682017-03-07 17:45:01 -08001717 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001718 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001719 forward_refs(&output_ins);
1720 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001721 cleanup_insn(&output_ins);
1722
1723 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001726
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001727 preproc->cleanup_pass();
1728
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001729 /* We better not be having an error hold still... */
1730 nasm_assert(!errhold_stack);
1731
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001732 if (global_offset_changed) {
1733 switch (pass_type()) {
1734 case PASS_OPT:
1735 /*
1736 * This is the only pass type that can be executed more
1737 * than once, and therefore has the ability to stall.
1738 */
1739 if (global_offset_changed < prev_offset_changed) {
1740 prev_offset_changed = global_offset_changed;
1741 stall_count = 0;
1742 } else {
1743 stall_count++;
1744 }
1745
1746 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1747 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1748 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001749 nasm_nonfatalf(ERR_UNDEAD,
1750 "unable to find valid values for all labels "
1751 "after %"PRId64" passes; "
1752 "stalled for %"PRId64", giving up.",
1753 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001754 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001755 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001756 }
1757 break;
1758
1759 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001760 /*!
1761 *!phase [off] phase error during stabilization
1762 *! warns about symbols having changed values during
1763 *! the second-to-last assembly pass. This is not
1764 *! inherently fatal, but may be a source of bugs.
1765 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001766 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1767 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001768 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001769 break;
1770
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001771 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001772 nasm_nonfatalf(ERR_UNDEAD,
1773 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001774 break;
1775
1776 default:
1777 /* This is normal, we'll keep going... */
1778 break;
1779 }
1780 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001781
1782 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001783 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001785 if (opt_verbose_info && pass_final()) {
1786 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001787 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001788 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001789
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001790 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001791 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001792}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001793
Ed Berosetfa771012002-06-09 20:56:40 +00001794/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001795 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001796 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001797static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001798{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001799 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001800
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001801 if ((severity & ERR_MASK) >= ERR_FATAL)
1802 return 0; /* Fatal errors are never suppressible */
1803
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001804 /* Warnings MUST HAVE a warning category specifier! */
1805 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001806
1807 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001808 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001809
1810 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001811}
1812
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001813static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001814{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001815 errflags type = severity & ERR_MASK;
1816
H. Peter Anvin8f622462017-04-02 19:02:29 -07001817 /*
1818 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001819 * We can never skip fatal errors as by definition they cannot be
1820 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001821 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001822 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001823 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001824
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001825 /*
1826 * ERR_LISTMSG messages are always skipped; the list file
1827 * receives them anyway as this function is not consulted
1828 * for sending to the list file.
1829 */
1830 if (type == ERR_LISTMSG)
1831 return true;
1832
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001833 /* This message not applicable unless pass_final */
1834 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001835}
1836
Ed Berosetfa771012002-06-09 20:56:40 +00001837/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001838 * check for suppressed message (usually warnings or notes)
1839 *
1840 * @param severity the severity of the warning or error
1841 * @return true if we should abort error/warning printing
1842 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001843static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001844{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001845 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001846 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001847 return false;
1848
1849 /* This error/warning is pointless if we are dead anyway */
1850 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1851 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001852
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001853 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1854 return true;
1855
1856 if (preproc && !(severity & ERR_PP_LISTMACRO))
1857 return preproc->suppress_error(severity);
1858
1859 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001860}
1861
1862/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001863 * Return the true error type (the ERR_MASK part) of the given
1864 * severity, accounting for warnings that may need to be promoted to
1865 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001866 *
1867 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001868 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001869 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001870static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001871{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001872 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1873 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001874
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001875 type = severity & ERR_MASK;
1876
1877 /* Promote warning to error? */
1878 if (type == ERR_WARNING) {
1879 uint8_t state = warning_state[warn_index(severity)];
1880 if ((state & warn_is_err) == warn_is_err)
1881 type = ERR_NONFATAL;
1882 }
1883
1884 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001885}
1886
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001887/*
1888 * The various error type prefixes
1889 */
1890static const char * const error_pfx_table[ERR_MASK+1] = {
1891 ";;; ", "debug: ", "info: ", "warning: ",
1892 "error: ", "fatal: ", "critical: ", "panic: "
1893};
1894static const char no_file_name[] = "nasm"; /* What to print if no file name */
1895
1896/*
1897 * For fatal/critical/panic errors, kill this process.
1898 */
1899static fatal_func die_hard(errflags true_type, errflags severity)
1900{
1901 fflush(NULL);
1902
1903 if (true_type == ERR_PANIC && abort_on_panic)
1904 abort();
1905
1906 if (ofile) {
1907 fclose(ofile);
1908 if (!keep_all)
1909 remove(outname);
1910 ofile = NULL;
1911 }
1912
1913 if (severity & ERR_USAGE)
1914 usage();
1915
1916 /* Terminate immediately */
1917 exit(true_type - ERR_FATAL + 1);
1918}
1919
1920/*
1921 * error reporting for critical and panic errors: minimize
1922 * the amount of system dependencies for getting a message out,
1923 * and in particular try to avoid memory allocations.
1924 */
1925fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1926{
1927 const char *currentfile = no_file_name;
1928 int32_t lineno = 0;
1929 errflags true_type = severity & ERR_MASK;
1930 static bool been_here = false;
1931
1932 if (unlikely(been_here))
1933 abort(); /* Recursive error... just die */
1934
1935 been_here = true;
1936
1937 if (!(severity & ERR_NOFILE)) {
1938 src_get(&lineno, &currentfile);
1939 if (!currentfile) {
1940 currentfile =
1941 inname && inname[0] ? inname :
1942 outname && outname[0] ? outname :
1943 no_file_name;
1944 lineno = 0;
1945 }
1946 }
1947
1948 fputs(error_pfx_table[severity], error_file);
1949 fputs(currentfile, error_file);
1950 if (lineno) {
1951 fprintf(error_file, "%s%"PRId32"%s",
1952 errfmt->beforeline, lineno, errfmt->afterline);
1953 }
1954 fputs(errfmt->beforemsg, error_file);
1955 vfprintf(error_file, fmt, args);
1956 fputc('\n', error_file);
1957
1958 die_hard(true_type, severity);
1959}
1960
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001961/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001962 * Stack of tentative error hold lists.
1963 */
1964struct nasm_errtext {
1965 struct nasm_errtext *next;
1966 const char *currentfile; /* Owned by the filename system */
1967 char *msg; /* Owned by this structure */
1968 errflags severity;
1969 errflags true_type;
1970 int32_t lineno;
1971};
1972struct nasm_errhold {
1973 struct nasm_errhold *up;
1974 struct nasm_errtext *head, **tail;
1975};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001976
1977static void nasm_free_error(struct nasm_errtext *et)
1978{
1979 nasm_free(et->msg);
1980 nasm_free(et);
1981}
1982
1983static void nasm_issue_error(struct nasm_errtext *et);
1984
1985struct nasm_errhold *nasm_error_hold_push(void)
1986{
1987 struct nasm_errhold *eh;
1988
1989 nasm_new(eh);
1990 eh->up = errhold_stack;
1991 eh->tail = &eh->head;
1992 errhold_stack = eh;
1993
1994 return eh;
1995}
1996
1997void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
1998{
1999 struct nasm_errtext *et, *etmp;
2000
2001 /* Allow calling with a null argument saying no hold in the first place */
2002 if (!eh)
2003 return;
2004
2005 /* This *must* be the current top of the errhold stack */
2006 nasm_assert(eh == errhold_stack);
2007
2008 if (eh->head) {
2009 if (issue) {
2010 if (eh->up) {
2011 /* Commit the current hold list to the previous level */
2012 *eh->up->tail = eh->head;
2013 eh->up->tail = eh->tail;
2014 } else {
2015 /* Issue errors */
2016 list_for_each_safe(et, etmp, eh->head)
2017 nasm_issue_error(et);
2018 }
2019 } else {
2020 /* Free the list, drop errors */
2021 list_for_each_safe(et, etmp, eh->head)
2022 nasm_free_error(et);
2023 }
2024 }
2025
2026 errhold_stack = eh->up;
2027 nasm_free(eh);
2028}
2029
2030/**
Ed Berosetfa771012002-06-09 20:56:40 +00002031 * common error reporting
2032 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002033 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002034 * specific error message to error_file and may or may not return. It
2035 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002036 *
2037 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002038 * @param fmt the printf style format string
2039 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07002040void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002041{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002042 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002043 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002044
2045 if (true_type >= ERR_CRITICAL)
2046 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002047
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08002048 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08002049 return;
2050
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002051 nasm_new(et);
2052 et->severity = severity;
2053 et->true_type = true_type;
2054 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002055 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002056 src_get(&et->lineno, &et->currentfile);
2057
2058 if (!et->currentfile) {
2059 et->currentfile =
H. Peter Anvin77016c82018-12-10 22:29:49 -08002060 inname && inname[0] ? inname :
2061 outname && outname[0] ? outname :
2062 NULL;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002063 et->lineno = 0;
H. Peter Anvin77016c82018-12-10 22:29:49 -08002064 }
2065 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002066
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002067 if (errhold_stack && true_type <= ERR_NONFATAL) {
2068 /* It is a tentative error */
2069 *errhold_stack->tail = et;
2070 errhold_stack->tail = &et->next;
2071 } else {
2072 nasm_issue_error(et);
2073 }
2074
2075 /*
2076 * Don't do this before then, if we do, we lose messages in the list
2077 * file, as the list file is only generated in the last pass.
2078 */
2079 if (skip_this_pass(severity))
2080 return;
2081
2082 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2083 if (preproc)
2084 preproc->error_list_macros(severity);
2085}
2086
2087/*
2088 * Actually print, list and take action on an error
2089 */
2090static void nasm_issue_error(struct nasm_errtext *et)
2091{
2092 const char *pfx;
2093 char warnsuf[64]; /* Warning suffix */
2094 char linestr[64]; /* Formatted line number if applicable */
2095 const errflags severity = et->severity;
2096 const errflags true_type = et->true_type;
2097 const char * const currentfile = et->currentfile;
2098 const uint32_t lineno = et->lineno;
2099
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002100 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002101 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002102 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002103 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002104
H. Peter Anvinddb29062018-12-11 00:06:29 -08002105 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002106 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2107 /*
2108 * It's a warning without ERR_HERE defined, and we are not already
2109 * unwinding the macros that led us here.
2110 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002111 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002112 (true_type >= ERR_NONFATAL) ? "error=" : "",
2113 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002114 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002115
H. Peter Anvinddb29062018-12-11 00:06:29 -08002116 *linestr = 0;
2117 if (lineno) {
2118 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
2119 errfmt->beforeline, lineno, errfmt->afterline);
2120 }
2121
H. Peter Anvin77016c82018-12-10 22:29:49 -08002122 if (!skip_this_pass(severity)) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002123 const char *file = currentfile ? currentfile : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002124 const char *here = "";
2125
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002126 if (severity & ERR_HERE) {
2127 here = currentfile ? " here" : " in an unknown location";
2128 }
2129
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002130 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002131 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002132 /*
2133 * Buffer up warnings until we either get an error
2134 * or we are on the code-generation pass.
2135 */
2136 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2137 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002138 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002139 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002140 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002141 * Actually output an error. If we have buffered
2142 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002143 */
2144 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002145 strlist_write(warn_list, "\n", error_file);
2146 strlist_free(&warn_list);
2147 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002148
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002149 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2150 file, linestr, errfmt->beforemsg,
2151 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002152 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002153 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002154
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002155 /* Are we recursing from error_list_macros? */
2156 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002157 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002158
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002159 /*
2160 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002161 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002162 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002163 if (severity & ERR_HERE) {
2164 if (lineno)
2165 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002166 pfx, et->msg, currentfile, lineno, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002167 else if (currentfile)
2168 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002169 pfx, et->msg, currentfile, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002170 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002171 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002172 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002173 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002174 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002175 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002176
H. Peter Anvin8f622462017-04-02 19:02:29 -07002177 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002178 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002179
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002180 if (true_type >= ERR_FATAL)
2181 die_hard(true_type, severity);
2182 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002183 terminate_after_phase = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002184
2185done:
2186 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187}
2188
H. Peter Anvin734b1882002-04-30 21:01:08 +00002189static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002190{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002191 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002192}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002193
H. Peter Anvin322bee02019-08-10 01:38:06 -07002194static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002195{
2196 int i;
2197
H. Peter Anvin322bee02019-08-10 01:38:06 -07002198 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002199 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2200 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002201 _progname, _progname);
2202 fputs(
2203 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002204 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002205 "\n"
2206 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002207 " -v (or --v) print the NASM version number and exit\n"
2208 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002209 "\n"
2210 " -o outfile write output to outfile\n"
2211 " --keep-all output files will not be removed even if an error happens\n"
2212 "\n"
2213 " -Xformat specifiy error reporting format (gnu or vc)\n"
2214 " -s redirect error messages to stdout\n"
2215 " -Zfile redirect error messages to file\n"
2216 "\n"
2217 " -M generate Makefile dependencies on stdout\n"
2218 " -MG d:o, missing files assumed generated\n"
2219 " -MF file set Makefile dependency file\n"
2220 " -MD file assemble and generate dependencies\n"
2221 " -MT file dependency target name\n"
2222 " -MQ file dependency target name (quoted)\n"
2223 " -MP emit phony targets\n"
2224 "\n"
2225 " -f format select output file format\n"
2226 , out);
2227 ofmt_list(ofmt, out);
2228 fputs(
2229 "\n"
2230 " -g generate debugging information\n"
2231 " -F format select a debugging format (output format dependent)\n"
2232 " -gformat same as -g -F format\n"
2233 , out);
2234 dfmt_list(out);
2235 fputs(
2236 "\n"
2237 " -l listfile write listing to a list file\n"
2238 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002239 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002240 " -Ld show byte and repeat counts in decimal, not hex\n"
2241 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002242 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002243 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002244 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002245 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002246 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002247 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002248 "\n"
2249 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2250 " -O0 no optimization\n"
2251 " -O1 minimal optimization\n"
2252 " -Ox multipass optimization (default)\n"
2253 " -Ov display the number of passes executed at the end\n"
2254 " -t assemble in limited SciTech TASM compatible mode\n"
2255 "\n"
2256 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2257 " -a don't preprocess (assemble only)\n"
2258 " -Ipath add a pathname to the include file path\n"
2259 " -Pfile pre-include a file (also --include)\n"
2260 " -Dmacro[=str] pre-define a macro\n"
2261 " -Umacro undefine a macro\n"
2262 " --pragma str pre-executes a specific %%pragma\n"
2263 " --before str add line (usually a preprocessor statement) before the input\n"
2264 " --no-line ignore %line directives in input\n"
2265 "\n"
2266 " --prefix str prepend the given string to the names of all extern,\n"
2267 " common and global symbols (also --gprefix)\n"
2268 " --suffix str append the given string to the names of all extern,\n"
2269 " common and global symbols (also --gprefix)\n"
2270 " --lprefix str prepend the given string to local symbols\n"
2271 " --lpostfix str append the given string to local symbols\n"
2272 "\n"
2273 " -w+x enable warning x (also -Wx)\n"
2274 " -w-x disable warning x (also -Wno-x)\n"
2275 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2276 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2277 , out);
2278
2279 fprintf(out, " %-20s %s\n",
2280 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2281
2282 for (i = 1; i < WARN_IDX_ALL; i++) {
2283 const char *me = warning_name[i];
2284 const char *prev = warning_name[i-1];
2285 const char *next = warning_name[i+1];
2286
2287 if (prev) {
2288 int prev_len = strlen(prev);
2289 const char *dash = me;
2290
2291 while ((dash = strchr(dash+1, '-'))) {
2292 int prefix_len = dash - me; /* Not including final dash */
2293 if (strncmp(next, me, prefix_len+1)) {
2294 /* Only one or last option with this prefix */
2295 break;
2296 }
2297 if (prefix_len >= prev_len ||
2298 strncmp(prev, me, prefix_len) ||
2299 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2300 /* This prefix is different from the previous option */
2301 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2302 prefix_len, me, prefix_len+1, me);
2303 }
2304 }
2305 }
2306
2307 fprintf(out, " %-20s %s%s\n",
2308 warning_name[i], warning_help[i],
2309 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2310 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2311 }
2312
2313 fputs(
2314 "\n"
2315 " --limit-X val set execution limit X\n"
2316 , out);
2317
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002318
2319 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002320 fprintf(out, " %-20s %s [",
2321 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002322 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002323 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002324 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002325 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002326 }
2327 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002328}