blob: 45490569f8750a84d2bd0d55f9054a0f05ac376f [file] [log] [blame]
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07001 /* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin322bee02019-08-10 01:38:06 -070072const char *_progname;
73
H. Peter Anvin55568c12016-10-03 19:46:49 -070074static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030075static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080076static bool skip_this_pass(errflags severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070078static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
H. Peter Anvin77016c82018-12-10 22:29:49 -080080struct error_format {
81 const char *beforeline; /* Before line number, if present */
82 const char *afterline; /* After line number, if present */
83 const char *beforemsg; /* Before actual message */
84};
85
86static const struct error_format errfmt_gnu = { ":", "", ": " };
87static const struct error_format errfmt_msvc = { "(", ")", " : " };
88static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080089static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080090
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070091unsigned int debug_nasm; /* Debugging messages? */
92
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080093static bool using_debug_info, opt_verbose_info;
94static const char *debug_format;
95
H. Peter Anvin3366e312018-02-07 14:14:36 -080096#ifndef ABORT_ON_PANIC
97# define ABORT_ON_PANIC 0
98#endif
99static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700100static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800101
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700102bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800103enum pass_type _pass_type;
104const char * const _pass_types[] =
105{
106 "init", "first", "optimize", "stabilize", "final"
107};
108int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000109int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800110int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000111
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700112struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800113
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800114const char *inname;
115const char *outname;
116static const char *listname;
117static const char *errname;
118
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700119static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700120
H. Peter Anvin338656c2016-02-17 20:59:22 -0800121const struct ofmt *ofmt = &OF_DEFAULT;
122const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400123const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800125FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000126
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400127FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300128struct optimization optimizing =
129 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100130static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400131
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800132iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400133static iflag_t cmd_cpu;
134
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800135struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800136bool in_absolute; /* Flag we are in ABSOLUTE seg */
137struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000138
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000139static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000140
H. Peter Anvine2c80182005-01-15 22:15:51 +0000141static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700142static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143
H. Peter Anvine7469712016-02-18 02:20:59 -0800144static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300145static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800146bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400147
H. Peter Anvin34754622018-11-28 12:36:53 -0800148#define OP_NORMAL (1U << 0)
149#define OP_PREPROCESS (1U << 1)
150#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400151
152static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400153
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700154/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700155static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700156static bool depend_missing_ok = false;
157static const char *depend_target = NULL;
158static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300159struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000160
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700161static bool want_usage;
162static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800163bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000164
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700165static char *quote_for_pmake(const char *str);
166static char *quote_for_wmake(const char *str);
167static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700168
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700169/*
170 * Execution limits that can be set via a command-line option or %pragma
171 */
172
H. Peter Anvin322bee02019-08-10 01:38:06 -0700173/*
174 * This is really unlimited; it would take far longer than the
175 * current age of the universe for this limit to be reached even on
176 * much faster CPUs than currently exist.
177*/
178#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179
H. Peter Anvin322bee02019-08-10 01:38:06 -0700180int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700181
182struct limit_info {
183 const char *name;
184 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700185 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700186};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700187/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700189 { "passes", "total number of passes", LIMIT_MAX_VAL },
190 { "stalled-passes", "number of passes without forward progress", 1000 },
191 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700192 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
193 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700194 { "rep", "%rep count", 1000000 },
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700195 { "eval", "expression evaluation descent", 8192 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700196 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700197};
198
H. Peter Anvin322bee02019-08-10 01:38:06 -0700199static void set_default_limits(void)
200{
201 int i;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700202 size_t rl;
203 int64_t new_limit;
204
H. Peter Anvin322bee02019-08-10 01:38:06 -0700205 for (i = 0; i <= LIMIT_MAX; i++)
206 nasm_limit[i] = limit_info[i].default_val;
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -0700207
208 /*
209 * Try to set a sensible default value for the eval depth based
210 * on the limit of the stack size, if knowable...
211 */
212 rl = nasm_get_stack_size_limit();
213 new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
214 if (new_limit < nasm_limit[LIMIT_EVAL])
215 nasm_limit[LIMIT_EVAL] = new_limit;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700216}
217
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700218enum directive_result
219nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700220{
221 int i;
222 int64_t val;
223 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700224 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700225
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700226 if (!limit)
227 limit = "";
228 if (!valstr)
229 valstr = "";
230
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700231 for (i = 0; i <= LIMIT_MAX; i++) {
232 if (!nasm_stricmp(limit, limit_info[i].name))
233 break;
234 }
235 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800236 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800237 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700238 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700239 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700240 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700241 return DIRR_ERROR;
242 }
243
244 if (!nasm_stricmp(valstr, "unlimited")) {
245 val = LIMIT_MAX_VAL;
246 } else {
247 val = readnum(valstr, &rn_error);
248 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800249 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800250 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700251 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700252 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700253 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700254 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700255 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700256 if (val > LIMIT_MAX_VAL)
257 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700258 }
259
260 nasm_limit[i] = val;
261 return DIRR_OK;
262}
263
H. Peter Anvin892c4812018-05-30 14:43:46 -0700264int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400265{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700266 location.segment = segment;
267 if (segment == NO_SEG) {
268 location.offset = absolute.offset;
269 in_absolute = true;
270 } else {
271 location.offset = raa_read(offsets, segment);
272 in_absolute = false;
273 }
274 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400275}
276
277static void set_curr_offs(int64_t l_off)
278{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800279 if (in_absolute)
280 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400281 else
282 offsets = raa_write(offsets, location.segment, l_off);
283}
284
H. Peter Anvin892c4812018-05-30 14:43:46 -0700285static void increment_offset(int64_t delta)
286{
287 if (unlikely(delta == 0))
288 return;
289
290 location.offset += delta;
291 set_curr_offs(location.offset);
292}
293
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000294static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000295{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000296 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800298 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000299 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000301}
302
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800303/*
304 * Define system-defined macros that are not part of
305 * macros/standard.mac.
306 */
307static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800308{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700309 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800310 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800311
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700312 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700313 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400314 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700315 strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400316 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700317 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
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, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400320 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800321 }
322
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700323 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700324 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400325 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700326 strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400327 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700328 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400329 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700330 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400331 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800332 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700333
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700334 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700335 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400336 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800337 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800338
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400339 /*
340 * In case if output format is defined by alias
341 * we have to put shortname of the alias itself here
342 * otherwise ABI backward compatibility gets broken.
343 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700344 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400345 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400346 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800347
348 /*
349 * Output-format specific macros.
350 */
351 if (ofmt->stdmac)
352 preproc->extra_stdmac(ofmt->stdmac);
353
354 /*
355 * Debug format, if any
356 */
357 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700358 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800359 preproc->pre_define(temp);
360 }
361}
362
363/*
364 * Initialize the preprocessor, set up the include path, and define
365 * the system-included macros. This is called between passes 1 and 2
366 * of parsing the command options; ofmt and dfmt are defined at this
367 * point.
368 *
369 * Command-line specified preprocessor directives (-p, -d, -u,
370 * --pragma, --before) are processed after this function.
371 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300372static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800373{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800374 preproc->init();
375 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300376 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800377}
378
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300379static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700380{
381 FILE *deps;
382 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700383 bool wmake = (quote_for_make == quote_for_wmake);
384 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800385 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700386
387 if (!list)
388 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700389
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700390 wrapstr = wmake ? " &\n " : " \\\n ";
391 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700392
393 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700394 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800396 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400397 return;
398 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700399 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400400 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700401 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700402
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700403 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800404 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700405 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400406 len = strlen(file);
407 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700408 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400409 linepos = 1;
410 }
411 fprintf(deps, " %s", file);
412 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700413 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700414 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700415 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700416
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800417 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700418 if (depend_emit_phony) {
419 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700420 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700421 nasm_free(file);
422 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700423 }
424
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800425 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700426
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700427 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400428 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700429}
430
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700431/* Convert a struct tm to a POSIX-style time constant */
432static int64_t make_posix_time(const struct tm *tm)
433{
434 int64_t t;
435 int64_t y = tm->tm_year;
436
437 /* See IEEE 1003.1:2004, section 4.14 */
438
439 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
440 t += tm->tm_yday;
441 t *= 24;
442 t += tm->tm_hour;
443 t *= 60;
444 t += tm->tm_min;
445 t *= 60;
446 t += tm->tm_sec;
447
448 return t;
449}
450
451static void timestamp(void)
452{
453 struct compile_time * const oct = &official_compile_time;
454 const struct tm *tp, *best_gm;
455
456 time(&oct->t);
457
458 best_gm = NULL;
459
460 tp = localtime(&oct->t);
461 if (tp) {
462 oct->local = *tp;
463 best_gm = &oct->local;
464 oct->have_local = true;
465 }
466
467 tp = gmtime(&oct->t);
468 if (tp) {
469 oct->gm = *tp;
470 best_gm = &oct->gm;
471 oct->have_gm = true;
472 if (!oct->have_local)
473 oct->local = oct->gm;
474 } else {
475 oct->gm = oct->local;
476 }
477
478 if (best_gm) {
479 oct->posix = make_posix_time(best_gm);
480 oct->have_posix = true;
481 }
482}
483
H. Peter Anvin038d8612007-04-12 16:54:50 +0000484int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000485{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700486 /* Do these as early as possible */
487 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700488 _progname = argv[0];
489 if (!_progname || !_progname[0])
490 _progname = "nasm";
491
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700492 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800493
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800494 iflag_set_default_cpu(&cpu);
495 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400496
H. Peter Anvin322bee02019-08-10 01:38:06 -0700497 set_default_limits();
498
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800499 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300500
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800501 _pass_type = PASS_INIT;
502 _passn = 0;
503
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700504 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505
H. Peter Anvin13506202018-11-28 14:55:58 -0800506 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700507 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700508
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700509 /*
510 * We must call init_labels() before the command line parsing,
511 * because we may be setting prefixes/suffixes from the command
512 * line.
513 */
514 init_labels();
515
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000516 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000517 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000518
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400520 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521
H. Peter Anvin55568c12016-10-03 19:46:49 -0700522 parse_cmdline(argc, argv, 1);
523 if (terminate_after_phase) {
524 if (want_usage)
525 usage();
526 return 1;
527 }
528
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800529 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800530 if (!using_debug_info) {
531 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800532 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800533 } else if (!debug_format) {
534 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300535 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800536 } else {
537 dfmt = dfmt_find(ofmt, debug_format);
538 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800539 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800540 debug_format, ofmt->shortname);
541 }
542 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000543
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300544 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800545
546 parse_cmdline(argc, argv, 2);
547 if (terminate_after_phase) {
548 if (want_usage)
549 usage();
550 return 1;
551 }
552
553 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800554 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000555
H. Peter Anvin34754622018-11-28 12:36:53 -0800556 /* Dependency filename if we are also doing other things */
557 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
558 if (outname)
559 depend_file = nasm_strcat(outname, ".d");
560 else
561 depend_file = filename_set_extension(inname, ".d");
562 }
563
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300564 /*
565 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800566 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300567 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300568 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800569 if (!outname && !(operating_mode & OP_PREPROCESS)) {
570 outname = filename_set_extension(inname, ofmt->extension);
571 if (!strcmp(outname, inname)) {
572 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800573 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 -0800574 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300575 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800576
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800577 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700578
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400580 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700581
H. Peter Anvin34754622018-11-28 12:36:53 -0800582 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700584
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400585 if (depend_missing_ok)
586 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700587
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800588 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 while ((line = preproc->getline()))
591 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800592 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800593 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400594 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000595 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700596 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000597 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000599
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800600 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700601 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800603 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 } else
605 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000606
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700607 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000608
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800609 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
610 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800611
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 while ((line = preproc->getline())) {
613 /*
614 * We generate %line directives if needed for later programs
615 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000616 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 int altline = src_get(&linnum, &file_name);
618 if (altline) {
619 if (altline == 1 && lineinc == 1)
620 nasm_fputs("", ofile);
621 else {
622 lineinc = (altline != -1 || lineinc != 1);
623 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000624 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 file_name);
626 }
627 prior_linnum = linnum;
628 }
629 nasm_fputs(line, ofile);
630 nasm_free(line);
631 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800632 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800633 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 if (ofile)
635 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700636 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400638 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400639 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400641 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700642 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400643 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800644 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400646 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400647 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700649 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200650
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400651 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800652 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400653 cleanup_labels();
654 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800655 if (ferror(ofile))
656 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400657 }
658
659 if (ofile) {
660 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700661 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400662 remove(outname);
663 ofile = NULL;
664 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000665 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000666
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800667 preproc->cleanup_session();
668
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700669 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400670 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700671
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000672 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000674
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 raa_free(offsets);
676 saa_free(forwrefs);
677 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000678 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700679 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800680 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000681
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700682 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000683}
684
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685/*
686 * Get a parameter for a command line option.
687 * First arg must be in the form of e.g. -f...
688 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800689static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000690{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800691 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400692 if (p[2]) /* the parameter's in the option */
693 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800695 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000697 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800698 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699 return NULL;
700}
701
H. Peter Anvindc242712007-11-18 11:55:10 -0800702/*
703 * Copy a filename
704 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800705static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800706{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800707 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700708 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800709
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800710 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800711}
712
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700713/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700714 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700715 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700716static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700717{
718 const char *p;
719 char *os, *q;
720
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400721 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700722 size_t nbs = 0;
723
724 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400725 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700726
727 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400728 switch (*p) {
729 case ' ':
730 case '\t':
731 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
732 n += nbs + 2;
733 nbs = 0;
734 break;
735 case '$':
736 case '#':
737 nbs = 0;
738 n += 2;
739 break;
740 case '\\':
741 nbs++;
742 n++;
743 break;
744 default:
745 nbs = 0;
746 n++;
747 break;
748 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700749 }
750
751 /* Convert N backslashes at the end of filename to 2N backslashes */
752 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400753 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700754
755 os = q = nasm_malloc(n);
756
757 nbs = 0;
758 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400759 switch (*p) {
760 case ' ':
761 case '\t':
762 while (nbs--)
763 *q++ = '\\';
764 *q++ = '\\';
765 *q++ = *p;
766 break;
767 case '$':
768 *q++ = *p;
769 *q++ = *p;
770 nbs = 0;
771 break;
772 case '#':
773 *q++ = '\\';
774 *q++ = *p;
775 nbs = 0;
776 break;
777 case '\\':
778 *q++ = *p;
779 nbs++;
780 break;
781 default:
782 *q++ = *p;
783 nbs = 0;
784 break;
785 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700786 }
787 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400788 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700789
790 *q = '\0';
791
792 return os;
793}
794
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700795/*
796 * Convert a string to a Watcom make-safe form
797 */
798static char *quote_for_wmake(const char *str)
799{
800 const char *p;
801 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700802 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700803
804 size_t n = 1; /* Terminating zero */
805
806 if (!str)
807 return NULL;
808
809 for (p = str; *p; p++) {
810 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700811 case ' ':
812 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700813 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700814 quote = true;
815 n++;
816 break;
817 case '\"':
818 quote = true;
819 n += 2;
820 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700821 case '$':
822 case '#':
823 n += 2;
824 break;
825 default:
826 n++;
827 break;
828 }
829 }
830
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700831 if (quote)
832 n += 2;
833
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700834 os = q = nasm_malloc(n);
835
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700836 if (quote)
837 *q++ = '\"';
838
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700839 for (p = str; *p; p++) {
840 switch (*p) {
841 case '$':
842 case '#':
843 *q++ = '$';
844 *q++ = *p;
845 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700846 case '\"':
847 *q++ = *p;
848 *q++ = *p;
849 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700850 default:
851 *q++ = *p;
852 break;
853 }
854 }
855
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700856 if (quote)
857 *q++ = '\"';
858
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700859 *q = '\0';
860
861 return os;
862}
863
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100864enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800865 OPT_BOGUS,
866 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700867 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800868 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700869 OPT_MANGLE,
870 OPT_INCLUDE,
871 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700872 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700873 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800874 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700875 OPT_NO_LINE,
876 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100877};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700878enum need_arg {
879 ARG_NO,
880 ARG_YES,
881 ARG_MAYBE
882};
883
H. Peter Anvin3366e312018-02-07 14:14:36 -0800884struct textargs {
885 const char *label;
886 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700887 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700888 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800889};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800890static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700891 {"v", OPT_VERSION, ARG_NO, 0},
892 {"version", OPT_VERSION, ARG_NO, 0},
893 {"help", OPT_HELP, ARG_NO, 0},
894 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
895 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
896 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
897 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
898 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
899 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
900 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
901 {"include", OPT_INCLUDE, ARG_YES, 0},
902 {"pragma", OPT_PRAGMA, ARG_YES, 0},
903 {"before", OPT_BEFORE, ARG_YES, 0},
904 {"limit-", OPT_LIMIT, ARG_YES, 0},
905 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
906 {"no-line", OPT_NO_LINE, ARG_NO, 0},
907 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
908 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000909};
910
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400911static void show_version(void)
912{
913 printf("NASM version %s compiled on %s%s\n",
914 nasm_version, nasm_date, nasm_compile_options);
915 exit(0);
916}
917
H. Peter Anvin423e3812007-11-15 17:12:29 -0800918static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700919static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000920{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000921 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800922 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000923
924 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800925 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700928 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400929 /* These parameters take values */
930 if (!(param = get_param(p, q, &advance)))
931 return advance;
932 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 switch (p[1]) {
935 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 if (pass == 1)
937 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700939
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400940 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800942 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700944
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 if (pass == 1) {
947 ofmt = ofmt_find(param, &ofmt_alias);
948 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800949 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700950 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 }
952 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700953
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400954 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800955 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700956 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700957
H. Peter Anvin55568c12016-10-03 19:46:49 -0700958 if (!*param) {
959 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300960 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700961 } else {
962 while (*param) {
963 switch (*param) {
964 case '0': case '1': case '2': case '3': case '4':
965 case '5': case '6': case '7': case '8': case '9':
966 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700967
Chang S. Baea5786342018-08-15 23:22:21 +0300968 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
969 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700970 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300971 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 else
Chang S. Baea5786342018-08-15 23:22:21 +0300973 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700975
H. Peter Anvin55568c12016-10-03 19:46:49 -0700976 case 'v':
977 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 param++;
979 opt_verbose_info = true;
980 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700981
H. Peter Anvin55568c12016-10-03 19:46:49 -0700982 case 'x':
983 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300984 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700986
H. Peter Anvin55568c12016-10-03 19:46:49 -0700987 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700988 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700989 *param);
990 break;
991 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400992 }
Chang S. Baea5786342018-08-15 23:22:21 +0300993 if (optimizing.level > MAX_OPTIMIZE)
994 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 }
997 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 case 'p': /* pre-include */
1000 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 if (pass == 2)
1002 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001004
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 case 'd': /* pre-define */
1006 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001007 if (pass == 2)
1008 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001009 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001010
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 case 'u': /* un-define */
1012 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001013 if (pass == 2)
1014 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001015 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001016
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001017 case 'i': /* include search path */
1018 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001019 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001020 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001021 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001022
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001023 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001025 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001028 case 'L': /* listing options */
1029 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001030 while (*param)
1031 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001032 }
1033 break;
1034
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001035 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001036 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001037 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001038 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001039
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001040 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001041 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001042 using_debug_info = true;
1043 debug_format = param;
1044 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001045 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001046
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001047 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001049 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1050 errfmt = &errfmt_msvc;
1051 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1052 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001053 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001054 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001059 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001060 using_debug_info = true;
1061 if (p[2])
1062 debug_format = nasm_skip_spaces(p + 2);
1063 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001067 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001068 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001072 /* legacy option */
1073 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 exit(0);
1075 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001076
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001078 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001079 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001080 nasm_ctype_tasm_mode();
1081 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001083
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001085 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001087
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001088 case 'e': /* preprocess only */
1089 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001090 if (pass == 1)
1091 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001093
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001094 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001095 if (pass == 1)
1096 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001098
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001099 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001100 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001101 if (pass == 2)
1102 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001103 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001106 if (pass == 1) {
1107 switch (p[2]) {
1108 case 'W':
1109 quote_for_make = quote_for_wmake;
1110 break;
1111 case 'D':
1112 case 'F':
1113 case 'T':
1114 case 'Q':
1115 advance = true;
1116 break;
1117 default:
1118 break;
1119 }
1120 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001121 switch (p[2]) {
1122 case 0:
1123 operating_mode = OP_DEPEND;
1124 break;
1125 case 'G':
1126 operating_mode = OP_DEPEND;
1127 depend_missing_ok = true;
1128 break;
1129 case 'P':
1130 depend_emit_phony = true;
1131 break;
1132 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001133 operating_mode |= OP_DEPEND;
1134 if (q && (q[0] != '-' || q[1] == '\0')) {
1135 depend_file = q;
1136 advance = true;
1137 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001138 break;
1139 case 'F':
1140 depend_file = q;
1141 advance = true;
1142 break;
1143 case 'T':
1144 depend_target = q;
1145 advance = true;
1146 break;
1147 case 'Q':
1148 depend_target = quote_for_make(q);
1149 advance = true;
1150 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001151 case 'W':
1152 /* handled in pass 1 */
1153 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001154 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001155 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001156 break;
1157 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001158 }
1159 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001160 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001161 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001162 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 case '-':
1166 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001167 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001168 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001169 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001170 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001171
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001172 p += 2;
1173
1174 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001175 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 break;
1177 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001178
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001179 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001180 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001181 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001182 olen = strlen(tx->label);
1183
1184 if (olen > plen)
1185 continue;
1186
1187 if (nasm_memicmp(p, tx->label, olen))
1188 continue;
1189
1190 if (tx->label[olen-1] == '-')
1191 break; /* Incomplete option */
1192
1193 if (!p[olen] || p[olen] == '=')
1194 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001196
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001197 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001198 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001199 }
1200
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001201 opt = tx->opt;
1202
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001203 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001204 if (param)
1205 *param++ = '\0';
1206
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001207 switch (tx->need_arg) {
1208 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001209 if (!param) {
1210 param = q;
1211 advance = true;
1212 }
1213
1214 /* Note: a null string is a valid parameter */
1215 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001216 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001217 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001219 break;
1220
1221 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001222 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001223 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001224 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001225 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001226 break;
1227
1228 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1229 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001230 }
1231
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001232 switch (opt) {
1233 case OPT_BOGUS:
1234 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001235 case OPT_VERSION:
1236 show_version();
1237 break;
1238 case OPT_ABORT_ON_PANIC:
1239 abort_on_panic = true;
1240 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001241 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001242 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001243 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001244 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001245 case OPT_INCLUDE:
1246 if (pass == 2)
1247 preproc->pre_include(q);
1248 break;
1249 case OPT_PRAGMA:
1250 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001251 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001252 break;
1253 case OPT_BEFORE:
1254 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001255 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001256 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001257 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001258 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001259 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001260 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001261 case OPT_KEEP_ALL:
1262 keep_all = true;
1263 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001264 case OPT_NO_LINE:
1265 pp_noline = true;
1266 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001267 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001268 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001269 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001270 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001271 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001272 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001273 default:
1274 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001276
1277 if (eqsave)
1278 *eqsave = '='; /* Restore = argument separator */
1279
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 break;
1281 }
1282
1283 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001284 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 break;
1286 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001287 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001288 /* In theory we could allow multiple input files... */
1289 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001290 }
1291
1292 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001293}
1294
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295#define ARG_BUF_DELTA 128
1296
H. Peter Anvin55568c12016-10-03 19:46:49 -07001297static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001299 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001300 int bufsize, prevargsize;
1301
1302 bufsize = prevargsize = ARG_BUF_DELTA;
1303 buffer = nasm_malloc(ARG_BUF_DELTA);
1304 prevarg = nasm_malloc(ARG_BUF_DELTA);
1305 prevarg[0] = '\0';
1306
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 p = buffer;
1309 while (1) { /* Loop to handle long lines */
1310 q = fgets(p, bufsize - (p - buffer), rfile);
1311 if (!q)
1312 break;
1313 p += strlen(p);
1314 if (p > buffer && p[-1] == '\n')
1315 break;
1316 if (p - buffer > bufsize - 10) {
1317 int offset;
1318 offset = p - buffer;
1319 bufsize += ARG_BUF_DELTA;
1320 buffer = nasm_realloc(buffer, bufsize);
1321 p = buffer + offset;
1322 }
1323 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 if (!q && p == buffer) {
1326 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001327 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 nasm_free(buffer);
1329 nasm_free(prevarg);
1330 return;
1331 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001332
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 /*
1334 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1335 * them are present at the end of the line.
1336 */
1337 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001339 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001342 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001343
H. Peter Anvin55568c12016-10-03 19:46:49 -07001344 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001346
Charles Crayne192d5b52007-10-18 19:02:42 -07001347 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 prevargsize += ARG_BUF_DELTA;
1349 prevarg = nasm_realloc(prevarg, prevargsize);
1350 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001351 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352 }
1353}
1354
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001355/* Function to process args from a string of args, rather than the
1356 * argv array. Used by the environment variable and response file
1357 * processing.
1358 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001359static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001361 char *p, *q, *arg, *prevarg;
1362 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001363
1364 p = args;
1365 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001367 arg = NULL;
1368 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 q = p;
1370 while (*p && *p != separator)
1371 p++;
1372 while (*p == separator)
1373 *p++ = '\0';
1374 prevarg = arg;
1375 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001376 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001378 }
1379 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001380 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001381}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001382
H. Peter Anvin55568c12016-10-03 19:46:49 -07001383static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001384{
1385 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001386 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001387 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001388 perror(file);
1389 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001390 }
1391 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001393 }
1394 fclose(f);
1395}
1396
H. Peter Anvin55568c12016-10-03 19:46:49 -07001397static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001398{
1399 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001400 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001401
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001402 /*
1403 * Initialize all the warnings to their default state, including
1404 * warning index 0 used for "always on".
1405 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001406 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001407
1408 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001409 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001410 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001411 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001412 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001414 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001416 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001417
1418 /*
1419 * Now process the actual command line.
1420 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001422 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 argv++;
1424 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001425 /*
1426 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 * arguments like the environment variable. This allows us
1428 * to have multiple arguments on a single line, which is
1429 * different to the -@resp file processing below for regular
1430 * NASM.
1431 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001432 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 argc--;
1434 argv++;
1435 }
1436 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001437 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001438 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001439 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001440 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001441 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001443 } else {
1444 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1445 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 }
1447 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001448 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001449 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001450 }
1451
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001452 /*
1453 * Look for basic command line typos. This definitely doesn't
1454 * catch all errors, but it might help cases of fumbled fingers.
1455 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001456 if (pass != 2)
1457 return;
1458
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001459 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001460 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001461 else if ((errname && !strcmp(inname, errname)) ||
1462 (outname && !strcmp(inname, outname)) ||
1463 (listname && !strcmp(inname, listname)) ||
1464 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001465 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001466
1467 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001468 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001469 if (!error_file) {
1470 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001471 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001472 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001473 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001474}
1475
H. Peter Anvin29651542018-12-18 19:14:40 -08001476static void forward_refs(insn *instruction)
1477{
1478 int i;
1479 struct forwrefinfo *fwinf;
1480
1481 instruction->forw_ref = false;
1482
1483 if (!optimizing.level)
1484 return; /* For -O0 don't bother */
1485
1486 if (!forwref)
1487 return;
1488
1489 if (forwref->lineno != globallineno)
1490 return;
1491
1492 instruction->forw_ref = true;
1493 do {
1494 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1495 forwref = saa_rstruct(forwrefs);
1496 } while (forwref && forwref->lineno == globallineno);
1497
1498 if (!pass_first())
1499 return;
1500
1501 for (i = 0; i < instruction->operands; i++) {
1502 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1503 fwinf = saa_wstruct(forwrefs);
1504 fwinf->lineno = globallineno;
1505 fwinf->operand = i;
1506 }
1507 }
1508}
1509
1510static void process_insn(insn *instruction)
1511{
1512 int32_t n;
1513 int64_t l;
1514
1515 if (!instruction->times)
1516 return; /* Nothing to do... */
1517
1518 nasm_assert(instruction->times > 0);
1519
1520 /*
1521 * NOTE: insn_size() can change instruction->times
1522 * (usually to 1) when called.
1523 */
1524 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001525 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001526 for (n = 1; n <= instruction->times; n++) {
1527 l = insn_size(location.segment, location.offset,
1528 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001529 /* l == -1 -> invalid instruction */
1530 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001531 increment_offset(l);
1532 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001533 if (list_option('p')) {
1534 struct out_data dummy;
1535 memset(&dummy, 0, sizeof dummy);
1536 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1537 dummy.offset = start;
1538 dummy.size = location.offset - start;
1539 lfmt->output(&dummy);
1540 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001541 } else {
1542 l = assemble(location.segment, location.offset,
1543 globalbits, instruction);
1544 /* We can't get an invalid instruction here */
1545 increment_offset(l);
1546
1547 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001548 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001549 for (n = 2; n <= instruction->times; n++) {
1550 l = assemble(location.segment, location.offset,
1551 globalbits, instruction);
1552 increment_offset(l);
1553 }
1554 lfmt->downlevel(LIST_TIMES);
1555 }
1556 }
1557}
1558
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001559static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001561 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001563 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001564 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001565
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001566 switch (cmd_sb) {
1567 case 16:
1568 break;
1569 case 32:
1570 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001571 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001572 break;
1573 case 64:
1574 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001575 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001576 break;
1577 default:
1578 panic();
1579 break;
1580 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001581
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001582 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001583
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001584 if (listname && !keep_all) {
1585 /* Remove the list file in case we die before the output pass */
1586 remove(listname);
1587 }
1588
1589 while (!terminate_after_phase && !pass_final()) {
1590 _passn++;
1591 if (pass_type() != PASS_OPT || !global_offset_changed)
1592 _pass_type++;
1593 global_offset_changed = 0;
1594
1595 /*
1596 * Create a warning buffer list unless we are in
1597 * pass 2 (everything will be emitted immediately in pass 2.)
1598 */
1599 if (warn_list) {
1600 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001601 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001602 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001603
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001604 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001605 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001606
H. Peter Anvincac0b192017-03-28 16:12:30 -07001607 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001609 if (listname) {
1610 if (pass_final() || list_on_every_pass()) {
1611 active_list_options = list_options;
1612 lfmt->init(listname);
1613 } else if (active_list_options) {
1614 /*
1615 * Looks like we used the list engine on a previous pass,
1616 * but now it is turned off, presumably via %pragma -p
1617 */
1618 lfmt->cleanup();
1619 if (!keep_all)
1620 remove(listname);
1621 active_list_options = 0;
1622 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001623 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001624
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001625 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001626 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 saa_rewind(forwrefs);
1628 forwref = saa_rstruct(forwrefs);
1629 raa_free(offsets);
1630 offsets = raa_init();
1631 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001632 location.segment = NO_SEG;
1633 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001634 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001635 location.known = true;
1636 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001637 switch_segment(ofmt->section(NULL, &globalbits));
1638 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001639
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001641
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001643 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001644 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001645 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001647 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001648 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001649 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001650 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001651 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001652 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001653
H. Peter Anvinc7131682017-03-07 17:45:01 -08001654 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001655 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001656 forward_refs(&output_ins);
1657 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001658 cleanup_insn(&output_ins);
1659
1660 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001663
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001664 preproc->cleanup_pass();
1665
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001666 if (global_offset_changed) {
1667 switch (pass_type()) {
1668 case PASS_OPT:
1669 /*
1670 * This is the only pass type that can be executed more
1671 * than once, and therefore has the ability to stall.
1672 */
1673 if (global_offset_changed < prev_offset_changed) {
1674 prev_offset_changed = global_offset_changed;
1675 stall_count = 0;
1676 } else {
1677 stall_count++;
1678 }
1679
1680 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1681 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1682 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001683 nasm_nonfatalf(ERR_UNDEAD,
1684 "unable to find valid values for all labels "
1685 "after %"PRId64" passes; "
1686 "stalled for %"PRId64", giving up.",
1687 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001688 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001689 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001690 }
1691 break;
1692
1693 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001694 /*!
1695 *!phase [off] phase error during stabilization
1696 *! warns about symbols having changed values during
1697 *! the second-to-last assembly pass. This is not
1698 *! inherently fatal, but may be a source of bugs.
1699 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001700 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1701 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001702 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001703 break;
1704
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001705 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001706 nasm_nonfatalf(ERR_UNDEAD,
1707 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001708 break;
1709
1710 default:
1711 /* This is normal, we'll keep going... */
1712 break;
1713 }
1714 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001715
1716 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001717 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001718
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001719 if (opt_verbose_info && pass_final()) {
1720 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001721 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001722 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001723
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001724 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001725 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001726}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001727
Ed Berosetfa771012002-06-09 20:56:40 +00001728/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001729 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001730 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001731static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001732{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001733 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001734
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001735 if ((severity & ERR_MASK) >= ERR_FATAL)
1736 return 0; /* Fatal errors are never suppressible */
1737
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001738 /* Warnings MUST HAVE a warning category specifier! */
1739 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001740
1741 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001742 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001743
1744 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001745}
1746
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001747static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001748{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001749 errflags type = severity & ERR_MASK;
1750
H. Peter Anvin8f622462017-04-02 19:02:29 -07001751 /*
1752 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001753 * We can never skip fatal errors as by definition they cannot be
1754 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001755 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001756 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001757 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001758
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001759 /*
1760 * ERR_LISTMSG messages are always skipped; the list file
1761 * receives them anyway as this function is not consulted
1762 * for sending to the list file.
1763 */
1764 if (type == ERR_LISTMSG)
1765 return true;
1766
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001767 /* This message not applicable unless pass_final */
1768 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001769}
1770
Ed Berosetfa771012002-06-09 20:56:40 +00001771/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001772 * check for suppressed message (usually warnings or notes)
1773 *
1774 * @param severity the severity of the warning or error
1775 * @return true if we should abort error/warning printing
1776 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001777static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001778{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001779 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001780 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001781 return false;
1782
1783 /* This error/warning is pointless if we are dead anyway */
1784 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1785 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001786
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001787 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1788 return true;
1789
1790 if (preproc && !(severity & ERR_PP_LISTMACRO))
1791 return preproc->suppress_error(severity);
1792
1793 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001794}
1795
1796/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001797 * Return the true error type (the ERR_MASK part) of the given
1798 * severity, accounting for warnings that may need to be promoted to
1799 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001800 *
1801 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001802 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001803 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001804static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001805{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001806 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1807 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001808
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001809 type = severity & ERR_MASK;
1810
1811 /* Promote warning to error? */
1812 if (type == ERR_WARNING) {
1813 uint8_t state = warning_state[warn_index(severity)];
1814 if ((state & warn_is_err) == warn_is_err)
1815 type = ERR_NONFATAL;
1816 }
1817
1818 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001819}
1820
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001821/*
1822 * The various error type prefixes
1823 */
1824static const char * const error_pfx_table[ERR_MASK+1] = {
1825 ";;; ", "debug: ", "info: ", "warning: ",
1826 "error: ", "fatal: ", "critical: ", "panic: "
1827};
1828static const char no_file_name[] = "nasm"; /* What to print if no file name */
1829
1830/*
1831 * For fatal/critical/panic errors, kill this process.
1832 */
1833static fatal_func die_hard(errflags true_type, errflags severity)
1834{
1835 fflush(NULL);
1836
1837 if (true_type == ERR_PANIC && abort_on_panic)
1838 abort();
1839
1840 if (ofile) {
1841 fclose(ofile);
1842 if (!keep_all)
1843 remove(outname);
1844 ofile = NULL;
1845 }
1846
1847 if (severity & ERR_USAGE)
1848 usage();
1849
1850 /* Terminate immediately */
1851 exit(true_type - ERR_FATAL + 1);
1852}
1853
1854/*
1855 * error reporting for critical and panic errors: minimize
1856 * the amount of system dependencies for getting a message out,
1857 * and in particular try to avoid memory allocations.
1858 */
1859fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1860{
1861 const char *currentfile = no_file_name;
1862 int32_t lineno = 0;
1863 errflags true_type = severity & ERR_MASK;
1864 static bool been_here = false;
1865
1866 if (unlikely(been_here))
1867 abort(); /* Recursive error... just die */
1868
1869 been_here = true;
1870
1871 if (!(severity & ERR_NOFILE)) {
1872 src_get(&lineno, &currentfile);
1873 if (!currentfile) {
1874 currentfile =
1875 inname && inname[0] ? inname :
1876 outname && outname[0] ? outname :
1877 no_file_name;
1878 lineno = 0;
1879 }
1880 }
1881
1882 fputs(error_pfx_table[severity], error_file);
1883 fputs(currentfile, error_file);
1884 if (lineno) {
1885 fprintf(error_file, "%s%"PRId32"%s",
1886 errfmt->beforeline, lineno, errfmt->afterline);
1887 }
1888 fputs(errfmt->beforemsg, error_file);
1889 vfprintf(error_file, fmt, args);
1890 fputc('\n', error_file);
1891
1892 die_hard(true_type, severity);
1893}
1894
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001895/**
Ed Berosetfa771012002-06-09 20:56:40 +00001896 * common error reporting
1897 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001898 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001899 * specific error message to error_file and may or may not return. It
1900 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001901 *
1902 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001903 * @param fmt the printf style format string
1904 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001905void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001906{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001907 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001908 char warnsuf[64];
1909 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001910 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001911 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001912 const char *currentfile = NULL;
1913 int32_t lineno = 0;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001914
1915 if (true_type >= ERR_CRITICAL)
1916 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001917
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001918 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001919 return;
1920
1921 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001922 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001923 if (!currentfile) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001924 currentfile =
H. Peter Anvin77016c82018-12-10 22:29:49 -08001925 inname && inname[0] ? inname :
1926 outname && outname[0] ? outname :
1927 NULL;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001928 lineno = 0;
H. Peter Anvin77016c82018-12-10 22:29:49 -08001929 }
1930 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001931
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001932 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001933 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001934 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001935 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001936
1937 vsnprintf(msg, sizeof msg, fmt, args);
1938 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001939 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1940 /*
1941 * It's a warning without ERR_HERE defined, and we are not already
1942 * unwinding the macros that led us here.
1943 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001944 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001945 (true_type >= ERR_NONFATAL) ? "error=" : "",
1946 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001947 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001948
H. Peter Anvinddb29062018-12-11 00:06:29 -08001949 *linestr = 0;
1950 if (lineno) {
1951 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1952 errfmt->beforeline, lineno, errfmt->afterline);
1953 }
1954
H. Peter Anvin77016c82018-12-10 22:29:49 -08001955 if (!skip_this_pass(severity)) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001956 const char *file = currentfile ? currentfile : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001957 const char *here = "";
1958
1959 if (severity & ERR_HERE)
1960 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001961
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001962 if (warn_list && true_type < ERR_NONFATAL &&
1963 !(pass_first() && (severity & ERR_PASS1)))
1964 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001965 /*
1966 * Buffer up warnings until we either get an error
1967 * or we are on the code-generation pass.
1968 */
1969 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1970 file, linestr, errfmt->beforemsg,
1971 pfx, msg, here, warnsuf);
1972 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001973 /*
1974 * If we have buffered warnings, and this is a non-warning,
1975 * output them now.
1976 */
1977 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001978 strlist_write(warn_list, "\n", error_file);
1979 strlist_free(&warn_list);
1980 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001981
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001982 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1983 file, linestr, errfmt->beforemsg,
1984 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001985
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001986 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001987 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001989 /* Are we recursing from error_list_macros? */
1990 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001991 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001992
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001993 /*
1994 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001995 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001996 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001997 if (severity & ERR_HERE) {
1998 if (lineno)
1999 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
2000 pfx, msg, currentfile, lineno, warnsuf);
2001 else if (currentfile)
2002 lfmt->error(severity, "%s%s in file %s%s",
2003 pfx, msg, currentfile, warnsuf);
2004 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002005 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08002006 pfx, msg, warnsuf);
2007 } else {
2008 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
2009 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002010
H. Peter Anvin8f622462017-04-02 19:02:29 -07002011 if (skip_this_pass(severity))
2012 return;
2013
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002014 /* error_list_macros can for obvious reasons not work with ERR_HERE */
2015 if (!(severity & ERR_HERE))
H. Peter Anvin2201ceb2019-08-27 17:19:07 -07002016 if (preproc)
2017 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002018
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002019 if (true_type >= ERR_FATAL)
2020 die_hard(true_type, severity);
2021 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002022 terminate_after_phase = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002023}
2024
H. Peter Anvin734b1882002-04-30 21:01:08 +00002025static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002026{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002027 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002028}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002029
H. Peter Anvin322bee02019-08-10 01:38:06 -07002030static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002031{
2032 int i;
2033
H. Peter Anvin322bee02019-08-10 01:38:06 -07002034 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002035 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2036 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002037 _progname, _progname);
2038 fputs(
2039 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002040 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002041 "\n"
2042 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002043 " -v (or --v) print the NASM version number and exit\n"
2044 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002045 "\n"
2046 " -o outfile write output to outfile\n"
2047 " --keep-all output files will not be removed even if an error happens\n"
2048 "\n"
2049 " -Xformat specifiy error reporting format (gnu or vc)\n"
2050 " -s redirect error messages to stdout\n"
2051 " -Zfile redirect error messages to file\n"
2052 "\n"
2053 " -M generate Makefile dependencies on stdout\n"
2054 " -MG d:o, missing files assumed generated\n"
2055 " -MF file set Makefile dependency file\n"
2056 " -MD file assemble and generate dependencies\n"
2057 " -MT file dependency target name\n"
2058 " -MQ file dependency target name (quoted)\n"
2059 " -MP emit phony targets\n"
2060 "\n"
2061 " -f format select output file format\n"
2062 , out);
2063 ofmt_list(ofmt, out);
2064 fputs(
2065 "\n"
2066 " -g generate debugging information\n"
2067 " -F format select a debugging format (output format dependent)\n"
2068 " -gformat same as -g -F format\n"
2069 , out);
2070 dfmt_list(out);
2071 fputs(
2072 "\n"
2073 " -l listfile write listing to a list file\n"
2074 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002075 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002076 " -Ld show byte and repeat counts in decimal, not hex\n"
2077 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002078 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002079 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002080 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002081 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002082 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002083 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002084 "\n"
2085 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2086 " -O0 no optimization\n"
2087 " -O1 minimal optimization\n"
2088 " -Ox multipass optimization (default)\n"
2089 " -Ov display the number of passes executed at the end\n"
2090 " -t assemble in limited SciTech TASM compatible mode\n"
2091 "\n"
2092 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2093 " -a don't preprocess (assemble only)\n"
2094 " -Ipath add a pathname to the include file path\n"
2095 " -Pfile pre-include a file (also --include)\n"
2096 " -Dmacro[=str] pre-define a macro\n"
2097 " -Umacro undefine a macro\n"
2098 " --pragma str pre-executes a specific %%pragma\n"
2099 " --before str add line (usually a preprocessor statement) before the input\n"
2100 " --no-line ignore %line directives in input\n"
2101 "\n"
2102 " --prefix str prepend the given string to the names of all extern,\n"
2103 " common and global symbols (also --gprefix)\n"
2104 " --suffix str append the given string to the names of all extern,\n"
2105 " common and global symbols (also --gprefix)\n"
2106 " --lprefix str prepend the given string to local symbols\n"
2107 " --lpostfix str append the given string to local symbols\n"
2108 "\n"
2109 " -w+x enable warning x (also -Wx)\n"
2110 " -w-x disable warning x (also -Wno-x)\n"
2111 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2112 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2113 , out);
2114
2115 fprintf(out, " %-20s %s\n",
2116 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2117
2118 for (i = 1; i < WARN_IDX_ALL; i++) {
2119 const char *me = warning_name[i];
2120 const char *prev = warning_name[i-1];
2121 const char *next = warning_name[i+1];
2122
2123 if (prev) {
2124 int prev_len = strlen(prev);
2125 const char *dash = me;
2126
2127 while ((dash = strchr(dash+1, '-'))) {
2128 int prefix_len = dash - me; /* Not including final dash */
2129 if (strncmp(next, me, prefix_len+1)) {
2130 /* Only one or last option with this prefix */
2131 break;
2132 }
2133 if (prefix_len >= prev_len ||
2134 strncmp(prev, me, prefix_len) ||
2135 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2136 /* This prefix is different from the previous option */
2137 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2138 prefix_len, me, prefix_len+1, me);
2139 }
2140 }
2141 }
2142
2143 fprintf(out, " %-20s %s%s\n",
2144 warning_name[i], warning_help[i],
2145 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2146 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2147 }
2148
2149 fputs(
2150 "\n"
2151 " --limit-X val set execution limit X\n"
2152 , out);
2153
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002154
2155 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002156 fprintf(out, " %-20s %s [",
2157 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002158 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002159 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002160 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002161 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002162 }
2163 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002164}