blob: a9793a20880d2e4bbc6b3970b9d75e425325cfdd [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;
449 * 3. The filename is empty.
450 *
451 * The filename is returned in a newly allocated buffer.
452 */
453static char *nasm_quote_filename(const char *fn)
454{
455 const char *p = fn;
456
457 if (!p || !*p)
458 return nasm_strdup("\"\"");
459
460 if (*p <= ' ' || nasm_isquote(*p)) {
461 goto quote;
462 } else {
463 while (*p) {
464 if (*p < ' ')
465 goto quote;
466 p++;
467 }
468 if (p[-1] <= ' ' || nasm_isquote(p[-1]))
469 goto quote;
470 }
471
472 /* Quoting not necessary */
473 return nasm_strdup(fn);
474
475quote:
476 return nasm_quote(fn, NULL);
477}
478
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700479static void timestamp(void)
480{
481 struct compile_time * const oct = &official_compile_time;
482 const struct tm *tp, *best_gm;
483
484 time(&oct->t);
485
486 best_gm = NULL;
487
488 tp = localtime(&oct->t);
489 if (tp) {
490 oct->local = *tp;
491 best_gm = &oct->local;
492 oct->have_local = true;
493 }
494
495 tp = gmtime(&oct->t);
496 if (tp) {
497 oct->gm = *tp;
498 best_gm = &oct->gm;
499 oct->have_gm = true;
500 if (!oct->have_local)
501 oct->local = oct->gm;
502 } else {
503 oct->gm = oct->local;
504 }
505
506 if (best_gm) {
507 oct->posix = make_posix_time(best_gm);
508 oct->have_posix = true;
509 }
510}
511
H. Peter Anvin038d8612007-04-12 16:54:50 +0000512int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700514 /* Do these as early as possible */
515 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700516 _progname = argv[0];
517 if (!_progname || !_progname[0])
518 _progname = "nasm";
519
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700520 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800521
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800522 iflag_set_default_cpu(&cpu);
523 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400524
H. Peter Anvin322bee02019-08-10 01:38:06 -0700525 set_default_limits();
526
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800527 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300528
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800529 _pass_type = PASS_INIT;
530 _passn = 0;
531
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700532 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533
H. Peter Anvin13506202018-11-28 14:55:58 -0800534 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700535 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700536
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700537 /*
538 * We must call init_labels() before the command line parsing,
539 * because we may be setting prefixes/suffixes from the command
540 * line.
541 */
542 init_labels();
543
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000544 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000546
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000547 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400548 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000549
H. Peter Anvin55568c12016-10-03 19:46:49 -0700550 parse_cmdline(argc, argv, 1);
551 if (terminate_after_phase) {
552 if (want_usage)
553 usage();
554 return 1;
555 }
556
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800557 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800558 if (!using_debug_info) {
559 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800560 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800561 } else if (!debug_format) {
562 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300563 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800564 } else {
565 dfmt = dfmt_find(ofmt, debug_format);
566 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800567 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800568 debug_format, ofmt->shortname);
569 }
570 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000571
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300572 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800573
574 parse_cmdline(argc, argv, 2);
575 if (terminate_after_phase) {
576 if (want_usage)
577 usage();
578 return 1;
579 }
580
581 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800582 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000583
H. Peter Anvin34754622018-11-28 12:36:53 -0800584 /* Dependency filename if we are also doing other things */
585 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
586 if (outname)
587 depend_file = nasm_strcat(outname, ".d");
588 else
589 depend_file = filename_set_extension(inname, ".d");
590 }
591
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300592 /*
593 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800594 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300595 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300596 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800597 if (!outname && !(operating_mode & OP_PREPROCESS)) {
598 outname = filename_set_extension(inname, ofmt->extension);
599 if (!strcmp(outname, inname)) {
600 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800601 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 -0800602 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300603 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800604
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800605 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700606
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400608 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700609
H. Peter Anvin34754622018-11-28 12:36:53 -0800610 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000611 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700612
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400613 if (depend_missing_ok)
614 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700615
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800616 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 while ((line = preproc->getline()))
619 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800620 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800621 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400622 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000623 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700624 const char *file_name = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700625 char *quoted_file_name = nasm_quote_filename(file_name);
626 int32_t linnum = 0;
627 int32_t lineinc = 0;
628 FILE *out;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000629
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800630 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700631 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800633 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700634 out = ofile;
635 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 ofile = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700637 out = stdout;
638 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000639
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700640 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800642 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
643 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800644
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 while ((line = preproc->getline())) {
646 /*
647 * We generate %line directives if needed for later programs
648 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700649 struct src_location where = src_where();
650 if (file_name != where.filename) {
651 file_name = where.filename;
652 linnum = -1; /* Force a new %line statement */
653 lineinc = file_name ? 1 : 0;
654 nasm_free(quoted_file_name);
655 quoted_file_name = nasm_quote_filename(file_name);
656 } else if (lineinc) {
657 if (linnum + lineinc == where.lineno) {
658 /* Add one blank line to account for increment */
659 fputc('\n', out);
660 linnum += lineinc;
661 } else if (linnum - lineinc == where.lineno) {
662 /*
663 * Standing still, probably a macro. Set increment
664 * to zero.
665 */
666 lineinc = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700668 } else {
669 /* lineinc == 0 */
670 if (linnum + 1 == where.lineno)
671 lineinc = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700673
674 /* Skip blank lines if we will need a %line anyway */
675 if (linnum == -1 && !line[0])
676 continue;
677
678 if (linnum != where.lineno) {
679 fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
680 where.lineno, lineinc, quoted_file_name);
681 }
682 linnum = where.lineno + lineinc;
683
684 fputs(line, out);
685 fputc('\n', out);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700687
688 nasm_free(quoted_file_name);
689
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800690 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800691 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 if (ofile)
693 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700694 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400696 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400697 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400699 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700700 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400701 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800702 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400704 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400705 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700707 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200708
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400709 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800710 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400711 cleanup_labels();
712 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800713 if (ferror(ofile))
714 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400715 }
716
717 if (ofile) {
718 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700719 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400720 remove(outname);
721 ofile = NULL;
722 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000724
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800725 preproc->cleanup_session();
726
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700727 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400728 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700729
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000732
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 raa_free(offsets);
734 saa_free(forwrefs);
735 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000736 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700737 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800738 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700740 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000741}
742
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743/*
744 * Get a parameter for a command line option.
745 * First arg must be in the form of e.g. -f...
746 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800747static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000748{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800749 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400750 if (p[2]) /* the parameter's in the option */
751 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800753 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000755 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800756 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757 return NULL;
758}
759
H. Peter Anvindc242712007-11-18 11:55:10 -0800760/*
761 * Copy a filename
762 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800763static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800764{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800765 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700766 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800767
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800768 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800769}
770
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700771/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700772 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700773 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700774static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700775{
776 const char *p;
777 char *os, *q;
778
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400779 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700780 size_t nbs = 0;
781
782 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400783 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700784
785 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400786 switch (*p) {
787 case ' ':
788 case '\t':
789 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
790 n += nbs + 2;
791 nbs = 0;
792 break;
793 case '$':
794 case '#':
795 nbs = 0;
796 n += 2;
797 break;
798 case '\\':
799 nbs++;
800 n++;
801 break;
802 default:
803 nbs = 0;
804 n++;
805 break;
806 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700807 }
808
809 /* Convert N backslashes at the end of filename to 2N backslashes */
810 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400811 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700812
813 os = q = nasm_malloc(n);
814
815 nbs = 0;
816 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400817 switch (*p) {
818 case ' ':
819 case '\t':
820 while (nbs--)
821 *q++ = '\\';
822 *q++ = '\\';
823 *q++ = *p;
824 break;
825 case '$':
826 *q++ = *p;
827 *q++ = *p;
828 nbs = 0;
829 break;
830 case '#':
831 *q++ = '\\';
832 *q++ = *p;
833 nbs = 0;
834 break;
835 case '\\':
836 *q++ = *p;
837 nbs++;
838 break;
839 default:
840 *q++ = *p;
841 nbs = 0;
842 break;
843 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700844 }
845 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400846 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700847
848 *q = '\0';
849
850 return os;
851}
852
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700853/*
854 * Convert a string to a Watcom make-safe form
855 */
856static char *quote_for_wmake(const char *str)
857{
858 const char *p;
859 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700860 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700861
862 size_t n = 1; /* Terminating zero */
863
864 if (!str)
865 return NULL;
866
867 for (p = str; *p; p++) {
868 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700869 case ' ':
870 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700871 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700872 quote = true;
873 n++;
874 break;
875 case '\"':
876 quote = true;
877 n += 2;
878 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700879 case '$':
880 case '#':
881 n += 2;
882 break;
883 default:
884 n++;
885 break;
886 }
887 }
888
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700889 if (quote)
890 n += 2;
891
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700892 os = q = nasm_malloc(n);
893
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700894 if (quote)
895 *q++ = '\"';
896
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700897 for (p = str; *p; p++) {
898 switch (*p) {
899 case '$':
900 case '#':
901 *q++ = '$';
902 *q++ = *p;
903 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700904 case '\"':
905 *q++ = *p;
906 *q++ = *p;
907 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700908 default:
909 *q++ = *p;
910 break;
911 }
912 }
913
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700914 if (quote)
915 *q++ = '\"';
916
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700917 *q = '\0';
918
919 return os;
920}
921
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100922enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800923 OPT_BOGUS,
924 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700925 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800926 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700927 OPT_MANGLE,
928 OPT_INCLUDE,
929 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700930 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700931 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800932 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700933 OPT_NO_LINE,
934 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100935};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700936enum need_arg {
937 ARG_NO,
938 ARG_YES,
939 ARG_MAYBE
940};
941
H. Peter Anvin3366e312018-02-07 14:14:36 -0800942struct textargs {
943 const char *label;
944 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700945 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700946 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800947};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800948static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700949 {"v", OPT_VERSION, ARG_NO, 0},
950 {"version", OPT_VERSION, ARG_NO, 0},
951 {"help", OPT_HELP, ARG_NO, 0},
952 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
953 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
954 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
955 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
956 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
957 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
958 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
959 {"include", OPT_INCLUDE, ARG_YES, 0},
960 {"pragma", OPT_PRAGMA, ARG_YES, 0},
961 {"before", OPT_BEFORE, ARG_YES, 0},
962 {"limit-", OPT_LIMIT, ARG_YES, 0},
963 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
964 {"no-line", OPT_NO_LINE, ARG_NO, 0},
965 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
966 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000967};
968
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400969static void show_version(void)
970{
971 printf("NASM version %s compiled on %s%s\n",
972 nasm_version, nasm_date, nasm_compile_options);
973 exit(0);
974}
975
H. Peter Anvin423e3812007-11-15 17:12:29 -0800976static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700977static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000978{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000979 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800980 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000981
982 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000984
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700986 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 /* These parameters take values */
988 if (!(param = get_param(p, q, &advance)))
989 return advance;
990 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 switch (p[1]) {
993 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 1)
995 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700997
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400998 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700999 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001000 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001001 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -07001002
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001004 if (pass == 1) {
1005 ofmt = ofmt_find(param, &ofmt_alias);
1006 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001007 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001008 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001009 }
1010 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001011
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001012 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001013 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001014 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001015
H. Peter Anvin55568c12016-10-03 19:46:49 -07001016 if (!*param) {
1017 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +03001018 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001019 } else {
1020 while (*param) {
1021 switch (*param) {
1022 case '0': case '1': case '2': case '3': case '4':
1023 case '5': case '6': case '7': case '8': case '9':
1024 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001025
Chang S. Baea5786342018-08-15 23:22:21 +03001026 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
1027 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001028 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +03001029 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001030 else
Chang S. Baea5786342018-08-15 23:22:21 +03001031 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001032 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001033
H. Peter Anvin55568c12016-10-03 19:46:49 -07001034 case 'v':
1035 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001036 param++;
1037 opt_verbose_info = true;
1038 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001039
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 case 'x':
1041 param++;
Chang S. Baea5786342018-08-15 23:22:21 +03001042 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001044
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001046 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -07001047 *param);
1048 break;
1049 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001050 }
Chang S. Baea5786342018-08-15 23:22:21 +03001051 if (optimizing.level > MAX_OPTIMIZE)
1052 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001053 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001054 }
1055 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001056
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001057 case 'p': /* pre-include */
1058 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001059 if (pass == 2)
1060 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001061 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001062
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001063 case 'd': /* pre-define */
1064 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001065 if (pass == 2)
1066 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001067 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001068
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001069 case 'u': /* un-define */
1070 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001071 if (pass == 2)
1072 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001073 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001074
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001075 case 'i': /* include search path */
1076 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001077 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001078 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001079 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001080
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001081 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001082 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001083 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001084 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001085
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001086 case 'L': /* listing options */
1087 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001088 while (*param)
1089 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001090 }
1091 break;
1092
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001093 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001094 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001095 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001096 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001097
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001098 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001099 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100 using_debug_info = true;
1101 debug_format = param;
1102 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001103 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001104
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001105 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001106 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001107 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1108 errfmt = &errfmt_msvc;
1109 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1110 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001111 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001112 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001113 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001115
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001117 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 using_debug_info = true;
1119 if (p[2])
1120 debug_format = nasm_skip_spaces(p + 2);
1121 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001125 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001126 exit(0); /* never need usage message here */
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 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001130 /* legacy option */
1131 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 exit(0);
1133 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001134
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001136 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001137 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001138 nasm_ctype_tasm_mode();
1139 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001143 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001145
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001146 case 'e': /* preprocess only */
1147 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001148 if (pass == 1)
1149 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001151
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001152 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001153 if (pass == 1)
1154 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001156
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001157 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001158 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001159 if (pass == 2)
1160 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001161 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001164 if (pass == 1) {
1165 switch (p[2]) {
1166 case 'W':
1167 quote_for_make = quote_for_wmake;
1168 break;
1169 case 'D':
1170 case 'F':
1171 case 'T':
1172 case 'Q':
1173 advance = true;
1174 break;
1175 default:
1176 break;
1177 }
1178 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001179 switch (p[2]) {
1180 case 0:
1181 operating_mode = OP_DEPEND;
1182 break;
1183 case 'G':
1184 operating_mode = OP_DEPEND;
1185 depend_missing_ok = true;
1186 break;
1187 case 'P':
1188 depend_emit_phony = true;
1189 break;
1190 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001191 operating_mode |= OP_DEPEND;
1192 if (q && (q[0] != '-' || q[1] == '\0')) {
1193 depend_file = q;
1194 advance = true;
1195 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001196 break;
1197 case 'F':
1198 depend_file = q;
1199 advance = true;
1200 break;
1201 case 'T':
1202 depend_target = q;
1203 advance = true;
1204 break;
1205 case 'Q':
1206 depend_target = quote_for_make(q);
1207 advance = true;
1208 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001209 case 'W':
1210 /* handled in pass 1 */
1211 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001212 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001213 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001214 break;
1215 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001216 }
1217 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001218 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001219 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001220 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001222
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 case '-':
1224 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001225 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001226 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001227 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001228 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001229
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001230 p += 2;
1231
1232 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001233 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 break;
1235 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001236
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001237 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001238 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001239 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001240 olen = strlen(tx->label);
1241
1242 if (olen > plen)
1243 continue;
1244
1245 if (nasm_memicmp(p, tx->label, olen))
1246 continue;
1247
1248 if (tx->label[olen-1] == '-')
1249 break; /* Incomplete option */
1250
1251 if (!p[olen] || p[olen] == '=')
1252 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001254
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001255 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001256 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001257 }
1258
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001259 opt = tx->opt;
1260
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001261 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001262 if (param)
1263 *param++ = '\0';
1264
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001265 switch (tx->need_arg) {
1266 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001267 if (!param) {
1268 param = q;
1269 advance = true;
1270 }
1271
1272 /* Note: a null string is a valid parameter */
1273 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001274 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001275 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001277 break;
1278
1279 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001280 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001281 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001282 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001283 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001284 break;
1285
1286 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1287 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001288 }
1289
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001290 switch (opt) {
1291 case OPT_BOGUS:
1292 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001293 case OPT_VERSION:
1294 show_version();
1295 break;
1296 case OPT_ABORT_ON_PANIC:
1297 abort_on_panic = true;
1298 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001299 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001300 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001301 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001302 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001303 case OPT_INCLUDE:
1304 if (pass == 2)
1305 preproc->pre_include(q);
1306 break;
1307 case OPT_PRAGMA:
1308 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001309 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001310 break;
1311 case OPT_BEFORE:
1312 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001313 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001314 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001315 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001316 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001317 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001318 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001319 case OPT_KEEP_ALL:
1320 keep_all = true;
1321 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001322 case OPT_NO_LINE:
1323 pp_noline = true;
1324 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001325 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001326 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001327 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001328 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001329 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001330 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001331 default:
1332 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001334
1335 if (eqsave)
1336 *eqsave = '='; /* Restore = argument separator */
1337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 break;
1339 }
1340
1341 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001342 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 break;
1344 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001345 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001346 /* In theory we could allow multiple input files... */
1347 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001348 }
1349
1350 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001351}
1352
H. Peter Anvineba20a72002-04-30 20:53:55 +00001353#define ARG_BUF_DELTA 128
1354
H. Peter Anvin55568c12016-10-03 19:46:49 -07001355static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001356{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001357 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001358 int bufsize, prevargsize;
1359
1360 bufsize = prevargsize = ARG_BUF_DELTA;
1361 buffer = nasm_malloc(ARG_BUF_DELTA);
1362 prevarg = nasm_malloc(ARG_BUF_DELTA);
1363 prevarg[0] = '\0';
1364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 p = buffer;
1367 while (1) { /* Loop to handle long lines */
1368 q = fgets(p, bufsize - (p - buffer), rfile);
1369 if (!q)
1370 break;
1371 p += strlen(p);
1372 if (p > buffer && p[-1] == '\n')
1373 break;
1374 if (p - buffer > bufsize - 10) {
1375 int offset;
1376 offset = p - buffer;
1377 bufsize += ARG_BUF_DELTA;
1378 buffer = nasm_realloc(buffer, bufsize);
1379 p = buffer + offset;
1380 }
1381 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001382
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 if (!q && p == buffer) {
1384 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001385 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 nasm_free(buffer);
1387 nasm_free(prevarg);
1388 return;
1389 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 /*
1392 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1393 * them are present at the end of the line.
1394 */
1395 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001396
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001397 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001400 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001401
H. Peter Anvin55568c12016-10-03 19:46:49 -07001402 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404
Charles Crayne192d5b52007-10-18 19:02:42 -07001405 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 prevargsize += ARG_BUF_DELTA;
1407 prevarg = nasm_realloc(prevarg, prevargsize);
1408 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001409 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410 }
1411}
1412
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001413/* Function to process args from a string of args, rather than the
1414 * argv array. Used by the environment variable and response file
1415 * processing.
1416 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001417static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001419 char *p, *q, *arg, *prevarg;
1420 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001421
1422 p = args;
1423 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001425 arg = NULL;
1426 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 q = p;
1428 while (*p && *p != separator)
1429 p++;
1430 while (*p == separator)
1431 *p++ = '\0';
1432 prevarg = arg;
1433 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001434 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001436 }
1437 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001438 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001439}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001440
H. Peter Anvin55568c12016-10-03 19:46:49 -07001441static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001442{
1443 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001444 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001445 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001446 perror(file);
1447 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001448 }
1449 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001450 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001451 }
1452 fclose(f);
1453}
1454
H. Peter Anvin55568c12016-10-03 19:46:49 -07001455static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001456{
1457 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001458 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001459
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001460 /*
1461 * Initialize all the warnings to their default state, including
1462 * warning index 0 used for "always on".
1463 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001464 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001465
1466 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001467 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001468 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001469 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001470 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001472 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001474 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001475
1476 /*
1477 * Now process the actual command line.
1478 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001480 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 argv++;
1482 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001483 /*
1484 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 * arguments like the environment variable. This allows us
1486 * to have multiple arguments on a single line, which is
1487 * different to the -@resp file processing below for regular
1488 * NASM.
1489 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001490 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 argc--;
1492 argv++;
1493 }
1494 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001495 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001496 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001497 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001498 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001499 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001501 } else {
1502 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1503 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 }
1505 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001506 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001507 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001508 }
1509
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001510 /*
1511 * Look for basic command line typos. This definitely doesn't
1512 * catch all errors, but it might help cases of fumbled fingers.
1513 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001514 if (pass != 2)
1515 return;
1516
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001517 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001518 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001519 else if ((errname && !strcmp(inname, errname)) ||
1520 (outname && !strcmp(inname, outname)) ||
1521 (listname && !strcmp(inname, listname)) ||
1522 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001523 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001524
1525 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001526 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001527 if (!error_file) {
1528 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001529 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001530 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001531 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001532}
1533
H. Peter Anvin29651542018-12-18 19:14:40 -08001534static void forward_refs(insn *instruction)
1535{
1536 int i;
1537 struct forwrefinfo *fwinf;
1538
1539 instruction->forw_ref = false;
1540
1541 if (!optimizing.level)
1542 return; /* For -O0 don't bother */
1543
1544 if (!forwref)
1545 return;
1546
1547 if (forwref->lineno != globallineno)
1548 return;
1549
1550 instruction->forw_ref = true;
1551 do {
1552 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1553 forwref = saa_rstruct(forwrefs);
1554 } while (forwref && forwref->lineno == globallineno);
1555
1556 if (!pass_first())
1557 return;
1558
1559 for (i = 0; i < instruction->operands; i++) {
1560 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1561 fwinf = saa_wstruct(forwrefs);
1562 fwinf->lineno = globallineno;
1563 fwinf->operand = i;
1564 }
1565 }
1566}
1567
1568static void process_insn(insn *instruction)
1569{
1570 int32_t n;
1571 int64_t l;
1572
1573 if (!instruction->times)
1574 return; /* Nothing to do... */
1575
1576 nasm_assert(instruction->times > 0);
1577
1578 /*
1579 * NOTE: insn_size() can change instruction->times
1580 * (usually to 1) when called.
1581 */
1582 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001583 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001584 for (n = 1; n <= instruction->times; n++) {
1585 l = insn_size(location.segment, location.offset,
1586 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001587 /* l == -1 -> invalid instruction */
1588 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001589 increment_offset(l);
1590 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001591 if (list_option('p')) {
1592 struct out_data dummy;
1593 memset(&dummy, 0, sizeof dummy);
1594 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1595 dummy.offset = start;
1596 dummy.size = location.offset - start;
1597 lfmt->output(&dummy);
1598 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001599 } else {
1600 l = assemble(location.segment, location.offset,
1601 globalbits, instruction);
1602 /* We can't get an invalid instruction here */
1603 increment_offset(l);
1604
1605 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001606 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001607 for (n = 2; n <= instruction->times; n++) {
1608 l = assemble(location.segment, location.offset,
1609 globalbits, instruction);
1610 increment_offset(l);
1611 }
1612 lfmt->downlevel(LIST_TIMES);
1613 }
1614 }
1615}
1616
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001617static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001618{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001619 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001621 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001622 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001623
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001624 switch (cmd_sb) {
1625 case 16:
1626 break;
1627 case 32:
1628 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001629 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001630 break;
1631 case 64:
1632 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001633 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001634 break;
1635 default:
1636 panic();
1637 break;
1638 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001639
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001640 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001641
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001642 if (listname && !keep_all) {
1643 /* Remove the list file in case we die before the output pass */
1644 remove(listname);
1645 }
1646
1647 while (!terminate_after_phase && !pass_final()) {
1648 _passn++;
1649 if (pass_type() != PASS_OPT || !global_offset_changed)
1650 _pass_type++;
1651 global_offset_changed = 0;
1652
1653 /*
1654 * Create a warning buffer list unless we are in
1655 * pass 2 (everything will be emitted immediately in pass 2.)
1656 */
1657 if (warn_list) {
1658 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001659 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001660 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001661
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001662 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001663 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001664
H. Peter Anvincac0b192017-03-28 16:12:30 -07001665 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001667 if (listname) {
1668 if (pass_final() || list_on_every_pass()) {
1669 active_list_options = list_options;
1670 lfmt->init(listname);
1671 } else if (active_list_options) {
1672 /*
1673 * Looks like we used the list engine on a previous pass,
1674 * but now it is turned off, presumably via %pragma -p
1675 */
1676 lfmt->cleanup();
1677 if (!keep_all)
1678 remove(listname);
1679 active_list_options = 0;
1680 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001681 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001682
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001683 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001684 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 saa_rewind(forwrefs);
1686 forwref = saa_rstruct(forwrefs);
1687 raa_free(offsets);
1688 offsets = raa_init();
1689 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001690 location.segment = NO_SEG;
1691 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001692 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001693 location.known = true;
1694 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001695 switch_segment(ofmt->section(NULL, &globalbits));
1696 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001697
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001699
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001701 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001702 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001703 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001704
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001705 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001706 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001707 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001708 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001709 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001710 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001711
H. Peter Anvinc7131682017-03-07 17:45:01 -08001712 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001713 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001714 forward_refs(&output_ins);
1715 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001716 cleanup_insn(&output_ins);
1717
1718 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001721
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001722 preproc->cleanup_pass();
1723
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07001724 /* We better not be having an error hold still... */
1725 nasm_assert(!errhold_stack);
1726
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001727 if (global_offset_changed) {
1728 switch (pass_type()) {
1729 case PASS_OPT:
1730 /*
1731 * This is the only pass type that can be executed more
1732 * than once, and therefore has the ability to stall.
1733 */
1734 if (global_offset_changed < prev_offset_changed) {
1735 prev_offset_changed = global_offset_changed;
1736 stall_count = 0;
1737 } else {
1738 stall_count++;
1739 }
1740
1741 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1742 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1743 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001744 nasm_nonfatalf(ERR_UNDEAD,
1745 "unable to find valid values for all labels "
1746 "after %"PRId64" passes; "
1747 "stalled for %"PRId64", giving up.",
1748 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001749 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001750 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001751 }
1752 break;
1753
1754 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001755 /*!
1756 *!phase [off] phase error during stabilization
1757 *! warns about symbols having changed values during
1758 *! the second-to-last assembly pass. This is not
1759 *! inherently fatal, but may be a source of bugs.
1760 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001761 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1762 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001763 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001764 break;
1765
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001766 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001767 nasm_nonfatalf(ERR_UNDEAD,
1768 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001769 break;
1770
1771 default:
1772 /* This is normal, we'll keep going... */
1773 break;
1774 }
1775 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001776
1777 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001778 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001779
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001780 if (opt_verbose_info && pass_final()) {
1781 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001782 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001783 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001785 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001786 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001787}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001788
Ed Berosetfa771012002-06-09 20:56:40 +00001789/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001790 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001791 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001792static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001793{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001794 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001795
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001796 if ((severity & ERR_MASK) >= ERR_FATAL)
1797 return 0; /* Fatal errors are never suppressible */
1798
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001799 /* Warnings MUST HAVE a warning category specifier! */
1800 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001801
1802 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001803 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001804
1805 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001806}
1807
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001808static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001809{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001810 errflags type = severity & ERR_MASK;
1811
H. Peter Anvin8f622462017-04-02 19:02:29 -07001812 /*
1813 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001814 * We can never skip fatal errors as by definition they cannot be
1815 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001816 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001817 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001818 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001819
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001820 /*
1821 * ERR_LISTMSG messages are always skipped; the list file
1822 * receives them anyway as this function is not consulted
1823 * for sending to the list file.
1824 */
1825 if (type == ERR_LISTMSG)
1826 return true;
1827
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001828 /* This message not applicable unless pass_final */
1829 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001830}
1831
Ed Berosetfa771012002-06-09 20:56:40 +00001832/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001833 * check for suppressed message (usually warnings or notes)
1834 *
1835 * @param severity the severity of the warning or error
1836 * @return true if we should abort error/warning printing
1837 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001838static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001839{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001840 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001841 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001842 return false;
1843
1844 /* This error/warning is pointless if we are dead anyway */
1845 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1846 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001847
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001848 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1849 return true;
1850
1851 if (preproc && !(severity & ERR_PP_LISTMACRO))
1852 return preproc->suppress_error(severity);
1853
1854 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001855}
1856
1857/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001858 * Return the true error type (the ERR_MASK part) of the given
1859 * severity, accounting for warnings that may need to be promoted to
1860 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001861 *
1862 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001863 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001864 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001865static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001866{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001867 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1868 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001869
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001870 type = severity & ERR_MASK;
1871
1872 /* Promote warning to error? */
1873 if (type == ERR_WARNING) {
1874 uint8_t state = warning_state[warn_index(severity)];
1875 if ((state & warn_is_err) == warn_is_err)
1876 type = ERR_NONFATAL;
1877 }
1878
1879 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001880}
1881
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001882/*
1883 * The various error type prefixes
1884 */
1885static const char * const error_pfx_table[ERR_MASK+1] = {
1886 ";;; ", "debug: ", "info: ", "warning: ",
1887 "error: ", "fatal: ", "critical: ", "panic: "
1888};
1889static const char no_file_name[] = "nasm"; /* What to print if no file name */
1890
1891/*
1892 * For fatal/critical/panic errors, kill this process.
1893 */
1894static fatal_func die_hard(errflags true_type, errflags severity)
1895{
1896 fflush(NULL);
1897
1898 if (true_type == ERR_PANIC && abort_on_panic)
1899 abort();
1900
1901 if (ofile) {
1902 fclose(ofile);
1903 if (!keep_all)
1904 remove(outname);
1905 ofile = NULL;
1906 }
1907
1908 if (severity & ERR_USAGE)
1909 usage();
1910
1911 /* Terminate immediately */
1912 exit(true_type - ERR_FATAL + 1);
1913}
1914
1915/*
1916 * error reporting for critical and panic errors: minimize
1917 * the amount of system dependencies for getting a message out,
1918 * and in particular try to avoid memory allocations.
1919 */
1920fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1921{
1922 const char *currentfile = no_file_name;
1923 int32_t lineno = 0;
1924 errflags true_type = severity & ERR_MASK;
1925 static bool been_here = false;
1926
1927 if (unlikely(been_here))
1928 abort(); /* Recursive error... just die */
1929
1930 been_here = true;
1931
1932 if (!(severity & ERR_NOFILE)) {
1933 src_get(&lineno, &currentfile);
1934 if (!currentfile) {
1935 currentfile =
1936 inname && inname[0] ? inname :
1937 outname && outname[0] ? outname :
1938 no_file_name;
1939 lineno = 0;
1940 }
1941 }
1942
1943 fputs(error_pfx_table[severity], error_file);
1944 fputs(currentfile, error_file);
1945 if (lineno) {
1946 fprintf(error_file, "%s%"PRId32"%s",
1947 errfmt->beforeline, lineno, errfmt->afterline);
1948 }
1949 fputs(errfmt->beforemsg, error_file);
1950 vfprintf(error_file, fmt, args);
1951 fputc('\n', error_file);
1952
1953 die_hard(true_type, severity);
1954}
1955
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001956/**
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001957 * Stack of tentative error hold lists.
1958 */
1959struct nasm_errtext {
1960 struct nasm_errtext *next;
1961 const char *currentfile; /* Owned by the filename system */
1962 char *msg; /* Owned by this structure */
1963 errflags severity;
1964 errflags true_type;
1965 int32_t lineno;
1966};
1967struct nasm_errhold {
1968 struct nasm_errhold *up;
1969 struct nasm_errtext *head, **tail;
1970};
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07001971
1972static void nasm_free_error(struct nasm_errtext *et)
1973{
1974 nasm_free(et->msg);
1975 nasm_free(et);
1976}
1977
1978static void nasm_issue_error(struct nasm_errtext *et);
1979
1980struct nasm_errhold *nasm_error_hold_push(void)
1981{
1982 struct nasm_errhold *eh;
1983
1984 nasm_new(eh);
1985 eh->up = errhold_stack;
1986 eh->tail = &eh->head;
1987 errhold_stack = eh;
1988
1989 return eh;
1990}
1991
1992void nasm_error_hold_pop(struct nasm_errhold *eh, bool issue)
1993{
1994 struct nasm_errtext *et, *etmp;
1995
1996 /* Allow calling with a null argument saying no hold in the first place */
1997 if (!eh)
1998 return;
1999
2000 /* This *must* be the current top of the errhold stack */
2001 nasm_assert(eh == errhold_stack);
2002
2003 if (eh->head) {
2004 if (issue) {
2005 if (eh->up) {
2006 /* Commit the current hold list to the previous level */
2007 *eh->up->tail = eh->head;
2008 eh->up->tail = eh->tail;
2009 } else {
2010 /* Issue errors */
2011 list_for_each_safe(et, etmp, eh->head)
2012 nasm_issue_error(et);
2013 }
2014 } else {
2015 /* Free the list, drop errors */
2016 list_for_each_safe(et, etmp, eh->head)
2017 nasm_free_error(et);
2018 }
2019 }
2020
2021 errhold_stack = eh->up;
2022 nasm_free(eh);
2023}
2024
2025/**
Ed Berosetfa771012002-06-09 20:56:40 +00002026 * common error reporting
2027 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002028 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002029 * specific error message to error_file and may or may not return. It
2030 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002031 *
2032 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002033 * @param fmt the printf style format string
2034 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07002035void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002036{
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002037 struct nasm_errtext *et;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002038 errflags true_type = true_error_type(severity);
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002039
2040 if (true_type >= ERR_CRITICAL)
2041 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002042
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08002043 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08002044 return;
2045
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002046 nasm_new(et);
2047 et->severity = severity;
2048 et->true_type = true_type;
2049 et->msg = nasm_vasprintf(fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08002050 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002051 src_get(&et->lineno, &et->currentfile);
2052
2053 if (!et->currentfile) {
2054 et->currentfile =
H. Peter Anvin77016c82018-12-10 22:29:49 -08002055 inname && inname[0] ? inname :
2056 outname && outname[0] ? outname :
2057 NULL;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002058 et->lineno = 0;
H. Peter Anvin77016c82018-12-10 22:29:49 -08002059 }
2060 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002061
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002062 if (errhold_stack && true_type <= ERR_NONFATAL) {
2063 /* It is a tentative error */
2064 *errhold_stack->tail = et;
2065 errhold_stack->tail = &et->next;
2066 } else {
2067 nasm_issue_error(et);
2068 }
2069
2070 /*
2071 * Don't do this before then, if we do, we lose messages in the list
2072 * file, as the list file is only generated in the last pass.
2073 */
2074 if (skip_this_pass(severity))
2075 return;
2076
2077 if (!(severity & (ERR_HERE|ERR_PP_LISTMACRO)))
2078 if (preproc)
2079 preproc->error_list_macros(severity);
2080}
2081
2082/*
2083 * Actually print, list and take action on an error
2084 */
2085static void nasm_issue_error(struct nasm_errtext *et)
2086{
2087 const char *pfx;
2088 char warnsuf[64]; /* Warning suffix */
2089 char linestr[64]; /* Formatted line number if applicable */
2090 const errflags severity = et->severity;
2091 const errflags true_type = et->true_type;
2092 const char * const currentfile = et->currentfile;
2093 const uint32_t lineno = et->lineno;
2094
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002095 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002096 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08002097 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002098 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08002099
H. Peter Anvinddb29062018-12-11 00:06:29 -08002100 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002101 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
2102 /*
2103 * It's a warning without ERR_HERE defined, and we are not already
2104 * unwinding the macros that led us here.
2105 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002106 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002107 (true_type >= ERR_NONFATAL) ? "error=" : "",
2108 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07002109 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002110
H. Peter Anvinddb29062018-12-11 00:06:29 -08002111 *linestr = 0;
2112 if (lineno) {
2113 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
2114 errfmt->beforeline, lineno, errfmt->afterline);
2115 }
2116
H. Peter Anvin77016c82018-12-10 22:29:49 -08002117 if (!skip_this_pass(severity)) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002118 const char *file = currentfile ? currentfile : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002119 const char *here = "";
2120
H. Peter Anvin (Intel)f7fadcd2020-06-05 13:19:45 -07002121 if (severity & ERR_HERE) {
2122 here = currentfile ? " here" : " in an unknown location";
2123 }
2124
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002125 if (warn_list && true_type < ERR_NONFATAL &&
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002126 !(pass_first() && (severity & ERR_PASS1))) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002127 /*
2128 * Buffer up warnings until we either get an error
2129 * or we are on the code-generation pass.
2130 */
2131 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
2132 file, linestr, errfmt->beforemsg,
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002133 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002134 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002135 /*
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002136 * Actually output an error. If we have buffered
2137 * warnings, and this is a non-warning, output them now.
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002138 */
2139 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002140 strlist_write(warn_list, "\n", error_file);
2141 strlist_free(&warn_list);
2142 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08002143
H. Peter Anvin (Intel)283bc922020-06-04 16:19:51 -07002144 fprintf(error_file, "%s%s%s%s%s%s%s\n",
2145 file, linestr, errfmt->beforemsg,
2146 pfx, et->msg, here, warnsuf);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03002147 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08002148 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002149
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002150 /* Are we recursing from error_list_macros? */
2151 if (severity & ERR_PP_LISTMACRO)
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002152 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002153
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002154 /*
2155 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002156 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002157 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08002158 if (severity & ERR_HERE) {
2159 if (lineno)
2160 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002161 pfx, et->msg, currentfile, lineno, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002162 else if (currentfile)
2163 lfmt->error(severity, "%s%s in file %s%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002164 pfx, et->msg, currentfile, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002165 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002166 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002167 pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002168 } else {
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002169 lfmt->error(severity, "%s%s%s", pfx, et->msg, warnsuf);
H. Peter Anvinddb29062018-12-11 00:06:29 -08002170 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002171
H. Peter Anvin8f622462017-04-02 19:02:29 -07002172 if (skip_this_pass(severity))
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002173 goto done;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002174
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002175 if (true_type >= ERR_FATAL)
2176 die_hard(true_type, severity);
2177 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002178 terminate_after_phase = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07002179
2180done:
2181 nasm_free_error(et);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002182}
2183
H. Peter Anvin734b1882002-04-30 21:01:08 +00002184static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002185{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002186 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002188
H. Peter Anvin322bee02019-08-10 01:38:06 -07002189static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002190{
2191 int i;
2192
H. Peter Anvin322bee02019-08-10 01:38:06 -07002193 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002194 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2195 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002196 _progname, _progname);
2197 fputs(
2198 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002199 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002200 "\n"
2201 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002202 " -v (or --v) print the NASM version number and exit\n"
2203 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002204 "\n"
2205 " -o outfile write output to outfile\n"
2206 " --keep-all output files will not be removed even if an error happens\n"
2207 "\n"
2208 " -Xformat specifiy error reporting format (gnu or vc)\n"
2209 " -s redirect error messages to stdout\n"
2210 " -Zfile redirect error messages to file\n"
2211 "\n"
2212 " -M generate Makefile dependencies on stdout\n"
2213 " -MG d:o, missing files assumed generated\n"
2214 " -MF file set Makefile dependency file\n"
2215 " -MD file assemble and generate dependencies\n"
2216 " -MT file dependency target name\n"
2217 " -MQ file dependency target name (quoted)\n"
2218 " -MP emit phony targets\n"
2219 "\n"
2220 " -f format select output file format\n"
2221 , out);
2222 ofmt_list(ofmt, out);
2223 fputs(
2224 "\n"
2225 " -g generate debugging information\n"
2226 " -F format select a debugging format (output format dependent)\n"
2227 " -gformat same as -g -F format\n"
2228 , out);
2229 dfmt_list(out);
2230 fputs(
2231 "\n"
2232 " -l listfile write listing to a list file\n"
2233 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002234 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002235 " -Ld show byte and repeat counts in decimal, not hex\n"
2236 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002237 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002238 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002239 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002240 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002241 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002242 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002243 "\n"
2244 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2245 " -O0 no optimization\n"
2246 " -O1 minimal optimization\n"
2247 " -Ox multipass optimization (default)\n"
2248 " -Ov display the number of passes executed at the end\n"
2249 " -t assemble in limited SciTech TASM compatible mode\n"
2250 "\n"
2251 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2252 " -a don't preprocess (assemble only)\n"
2253 " -Ipath add a pathname to the include file path\n"
2254 " -Pfile pre-include a file (also --include)\n"
2255 " -Dmacro[=str] pre-define a macro\n"
2256 " -Umacro undefine a macro\n"
2257 " --pragma str pre-executes a specific %%pragma\n"
2258 " --before str add line (usually a preprocessor statement) before the input\n"
2259 " --no-line ignore %line directives in input\n"
2260 "\n"
2261 " --prefix str prepend the given string to the names of all extern,\n"
2262 " common and global symbols (also --gprefix)\n"
2263 " --suffix str append the given string to the names of all extern,\n"
2264 " common and global symbols (also --gprefix)\n"
2265 " --lprefix str prepend the given string to local symbols\n"
2266 " --lpostfix str append the given string to local symbols\n"
2267 "\n"
2268 " -w+x enable warning x (also -Wx)\n"
2269 " -w-x disable warning x (also -Wno-x)\n"
2270 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2271 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2272 , out);
2273
2274 fprintf(out, " %-20s %s\n",
2275 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2276
2277 for (i = 1; i < WARN_IDX_ALL; i++) {
2278 const char *me = warning_name[i];
2279 const char *prev = warning_name[i-1];
2280 const char *next = warning_name[i+1];
2281
2282 if (prev) {
2283 int prev_len = strlen(prev);
2284 const char *dash = me;
2285
2286 while ((dash = strchr(dash+1, '-'))) {
2287 int prefix_len = dash - me; /* Not including final dash */
2288 if (strncmp(next, me, prefix_len+1)) {
2289 /* Only one or last option with this prefix */
2290 break;
2291 }
2292 if (prefix_len >= prev_len ||
2293 strncmp(prev, me, prefix_len) ||
2294 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2295 /* This prefix is different from the previous option */
2296 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2297 prefix_len, me, prefix_len+1, me);
2298 }
2299 }
2300 }
2301
2302 fprintf(out, " %-20s %s%s\n",
2303 warning_name[i], warning_help[i],
2304 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2305 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2306 }
2307
2308 fputs(
2309 "\n"
2310 " --limit-X val set execution limit X\n"
2311 , out);
2312
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002313
2314 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002315 fprintf(out, " %-20s %s [",
2316 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002317 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002318 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002319 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002320 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002321 }
2322 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002323}