blob: 9e36314018dacffecf3e9107cff403ba635f64eb [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 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#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -070078static void assemble_file(const char *, StrList *);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvina3d96d02018-06-15 17:51:39 -070097int pass0;
98int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080099static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000100int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800101int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700103struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800104
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800105const char *inname;
106const char *outname;
107static const char *listname;
108static const char *errname;
109
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700110static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300120struct optimization optimizing =
121 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100122static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400125static iflag_t cmd_cpu;
126
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800127struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800128bool in_absolute; /* Flag we are in ABSOLUTE seg */
129struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000132
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700134static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135
H. Peter Anvine7469712016-02-18 02:20:59 -0800136static const struct preproc_ops *preproc;
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800137static StrList *include_path;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400138
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400139#define OP_NORMAL (1u << 0)
140#define OP_PREPROCESS (1u << 1)
141#define OP_DEPEND (1u << 2)
142
143static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400144
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700145/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700146static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700147static bool depend_missing_ok = false;
148static const char *depend_target = NULL;
149static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700150StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000151
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700152static bool want_usage;
153static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800154bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700156static char *quote_for_pmake(const char *str);
157static char *quote_for_wmake(const char *str);
158static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700160/*
161 * Execution limits that can be set via a command-line option or %pragma
162 */
163
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700164#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700165
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700166int64_t nasm_limit[LIMIT_MAX+1] =
167{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700168
169struct limit_info {
170 const char *name;
171 const char *help;
172};
173static const struct limit_info limit_info[LIMIT_MAX+1] = {
174 { "passes", "total number of passes" },
175 { "stalled-passes", "number of passes without forward progress" },
176 { "macro-levels", "levels of macro expansion"},
177 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700178 { "eval", "expression evaluation descent"},
179 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180};
181
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700182enum directive_result
183nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700184{
185 int i;
186 int64_t val;
187 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700188 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189
190 for (i = 0; i <= LIMIT_MAX; i++) {
191 if (!nasm_stricmp(limit, limit_info[i].name))
192 break;
193 }
194 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700195 if (passn == 0)
196 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
197 else
198 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
199 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700200 return DIRR_ERROR;
201 }
202
203 if (!nasm_stricmp(valstr, "unlimited")) {
204 val = LIMIT_MAX_VAL;
205 } else {
206 val = readnum(valstr, &rn_error);
207 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700208 if (passn == 0)
209 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
210 else
211 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
212 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700215 if (val > LIMIT_MAX_VAL)
216 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700217 }
218
219 nasm_limit[i] = val;
220 return DIRR_OK;
221}
222
H. Peter Anvin892c4812018-05-30 14:43:46 -0700223int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400224{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700225 location.segment = segment;
226 if (segment == NO_SEG) {
227 location.offset = absolute.offset;
228 in_absolute = true;
229 } else {
230 location.offset = raa_read(offsets, segment);
231 in_absolute = false;
232 }
233 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400234}
235
236static void set_curr_offs(int64_t l_off)
237{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800238 if (in_absolute)
239 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400240 else
241 offsets = raa_write(offsets, location.segment, l_off);
242}
243
H. Peter Anvin892c4812018-05-30 14:43:46 -0700244static void increment_offset(int64_t delta)
245{
246 if (unlikely(delta == 0))
247 return;
248
249 location.offset += delta;
250 set_curr_offs(location.offset);
251}
252
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000253static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000255 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800257 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000258 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000260}
261
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800262/*
263 * Define system-defined macros that are not part of
264 * macros/standard.mac.
265 */
266static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800267{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700268 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800270
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 if (oct->have_local) {
272 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400273 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700274 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400275 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700276 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400277 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700278 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280 }
281
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 if (oct->have_gm) {
283 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400284 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700285 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400286 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700287 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400288 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700289 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700292
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 if (oct->have_posix) {
294 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400295 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800297
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400298 /*
299 * In case if output format is defined by alias
300 * we have to put shortname of the alias itself here
301 * otherwise ABI backward compatibility gets broken.
302 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400303 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400304 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400305 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800306
307 /*
308 * Output-format specific macros.
309 */
310 if (ofmt->stdmac)
311 preproc->extra_stdmac(ofmt->stdmac);
312
313 /*
314 * Debug format, if any
315 */
316 if (dfmt != &null_debug_form) {
317 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
318 preproc->pre_define(temp);
319 }
320}
321
322/*
323 * Initialize the preprocessor, set up the include path, and define
324 * the system-included macros. This is called between passes 1 and 2
325 * of parsing the command options; ofmt and dfmt are defined at this
326 * point.
327 *
328 * Command-line specified preprocessor directives (-p, -d, -u,
329 * --pragma, --before) are processed after this function.
330 */
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300331static void preproc_init(StrList **ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800332{
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300333 struct strlist_entry *l;
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800334
335 preproc->init();
336 define_macros();
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300337
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300338 list_for_each(l, (*ipath)->head)
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300339 preproc->include_path(l->str);
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300340
341 strlist_free(*ipath);
342 *ipath = strlist_allocate();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800343}
344
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700345static void emit_dependencies(StrList *list)
346{
347 FILE *deps;
348 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700349 bool wmake = (quote_for_make == quote_for_wmake);
350 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700351 struct strlist_entry *l;
352
353 if (!list)
354 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700355
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700356 wrapstr = wmake ? " &\n " : " \\\n ";
357 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700358
359 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700360 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400361 if (!deps) {
362 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
363 "unable to write dependency file `%s'", depend_file);
364 return;
365 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700366 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400367 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700368 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700369
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700370 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700371 list_for_each(l, list->head) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700372 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400373 len = strlen(file);
374 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700375 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400376 linepos = 1;
377 }
378 fprintf(deps, " %s", file);
379 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700380 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381 }
382 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700383
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700384 list_for_each(l, list->head) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700385 if (depend_emit_phony) {
386 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700387 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700388 nasm_free(file);
389 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390 }
391
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700392 strlist_free(list);
393
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700394 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700396}
397
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700398/* Convert a struct tm to a POSIX-style time constant */
399static int64_t make_posix_time(const struct tm *tm)
400{
401 int64_t t;
402 int64_t y = tm->tm_year;
403
404 /* See IEEE 1003.1:2004, section 4.14 */
405
406 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
407 t += tm->tm_yday;
408 t *= 24;
409 t += tm->tm_hour;
410 t *= 60;
411 t += tm->tm_min;
412 t *= 60;
413 t += tm->tm_sec;
414
415 return t;
416}
417
418static void timestamp(void)
419{
420 struct compile_time * const oct = &official_compile_time;
421 const struct tm *tp, *best_gm;
422
423 time(&oct->t);
424
425 best_gm = NULL;
426
427 tp = localtime(&oct->t);
428 if (tp) {
429 oct->local = *tp;
430 best_gm = &oct->local;
431 oct->have_local = true;
432 }
433
434 tp = gmtime(&oct->t);
435 if (tp) {
436 oct->gm = *tp;
437 best_gm = &oct->gm;
438 oct->have_gm = true;
439 if (!oct->have_local)
440 oct->local = oct->gm;
441 } else {
442 oct->gm = oct->local;
443 }
444
445 if (best_gm) {
446 oct->posix = make_posix_time(best_gm);
447 oct->have_posix = true;
448 }
449}
450
H. Peter Anvin038d8612007-04-12 16:54:50 +0000451int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700453 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800454
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800455 iflag_set_default_cpu(&cpu);
456 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400457
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300458 include_path = strlist_allocate();
459
Charles Crayne2581c862008-09-10 19:21:52 -0700460 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700461 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400462 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463
H. Peter Anvin97e15752007-09-25 08:47:47 -0700464 error_file = stderr;
465
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700466 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700467 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700468
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700469 /*
470 * We must call init_labels() before the command line parsing,
471 * because we may be setting prefixes/suffixes from the command
472 * line.
473 */
474 init_labels();
475
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000476 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000477 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000478
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000479 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400480 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000481
H. Peter Anvin55568c12016-10-03 19:46:49 -0700482 parse_cmdline(argc, argv, 1);
483 if (terminate_after_phase) {
484 if (want_usage)
485 usage();
486 return 1;
487 }
488
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800489 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800490 if (!using_debug_info) {
491 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800492 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800493 } else if (!debug_format) {
494 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800495 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800496 } else {
497 dfmt = dfmt_find(ofmt, debug_format);
498 if (!dfmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700499 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800500 "unrecognized debug format `%s' for"
501 " output format `%s'",
502 debug_format, ofmt->shortname);
503 }
504 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000505
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300506 preproc_init(&include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800507
508 parse_cmdline(argc, argv, 2);
509 if (terminate_after_phase) {
510 if (want_usage)
511 usage();
512 return 1;
513 }
514
515 /* Save away the default state of warnings */
516 memcpy(warning_state_init, warning_state, sizeof warning_state);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000517
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300518 /*
519 * If no output file name provided and this
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300520 * is a preprocess mode, we're perfectly
521 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300522 */
523 if (!outname) {
524 if (!(operating_mode & OP_PREPROCESS))
525 outname = filename_set_extension(inname, ofmt->extension);
526 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800527
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700528 if (depend_file || (operating_mode & OP_DEPEND))
529 depend_list = strlist_allocate();
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700530
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700531 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400532 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700533
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400534 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700536
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400537 if (depend_missing_ok)
538 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700539
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700540 preproc->reset(inname, 0, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 while ((line = preproc->getline()))
543 nasm_free(line);
544 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400545 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000546 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700547 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000548 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000550
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800551 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700552 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700554 nasm_fatal_fl(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 "unable to open output file `%s'",
556 outname);
557 } else
558 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000559
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700560 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000561
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400562 /* pass = 1; */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700563 preproc->reset(inname, 3, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800564
565 /* Revert all warnings to the default state */
566 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 while ((line = preproc->getline())) {
569 /*
570 * We generate %line directives if needed for later programs
571 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000572 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 int altline = src_get(&linnum, &file_name);
574 if (altline) {
575 if (altline == 1 && lineinc == 1)
576 nasm_fputs("", ofile);
577 else {
578 lineinc = (altline != -1 || lineinc != 1);
579 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000580 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 file_name);
582 }
583 prior_linnum = linnum;
584 }
585 nasm_fputs(line, ofile);
586 nasm_free(line);
587 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 preproc->cleanup(0);
589 if (ofile)
590 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700591 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400593 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400594 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400596 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700597 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400598 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700599 nasm_fatal_fl(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400600 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400602 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400603 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700605 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200606
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400607 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800608 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400609 cleanup_labels();
610 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700611 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400612 nasm_error(ERR_NONFATAL|ERR_NOFILE,
613 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700614 terminate_after_phase = true;
615 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400616 }
617
618 if (ofile) {
619 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700620 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400621 remove(outname);
622 ofile = NULL;
623 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000624 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000625
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700626 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400627 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700628
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000629 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000631
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 raa_free(offsets);
633 saa_free(forwrefs);
634 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000635 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700636 src_free();
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300637 strlist_free(include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000638
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700639 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000640}
641
H. Peter Anvineba20a72002-04-30 20:53:55 +0000642/*
643 * Get a parameter for a command line option.
644 * First arg must be in the form of e.g. -f...
645 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800646static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800648 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400649 if (p[2]) /* the parameter's in the option */
650 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800652 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000654 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400655 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000657 return NULL;
658}
659
H. Peter Anvindc242712007-11-18 11:55:10 -0800660/*
661 * Copy a filename
662 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800663static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800664{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800665 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700666 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800667
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800668 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800669}
670
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700671/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700672 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700673 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700674static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700675{
676 const char *p;
677 char *os, *q;
678
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400679 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700680 size_t nbs = 0;
681
682 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400683 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700684
685 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400686 switch (*p) {
687 case ' ':
688 case '\t':
689 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
690 n += nbs + 2;
691 nbs = 0;
692 break;
693 case '$':
694 case '#':
695 nbs = 0;
696 n += 2;
697 break;
698 case '\\':
699 nbs++;
700 n++;
701 break;
702 default:
703 nbs = 0;
704 n++;
705 break;
706 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700707 }
708
709 /* Convert N backslashes at the end of filename to 2N backslashes */
710 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400711 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700712
713 os = q = nasm_malloc(n);
714
715 nbs = 0;
716 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400717 switch (*p) {
718 case ' ':
719 case '\t':
720 while (nbs--)
721 *q++ = '\\';
722 *q++ = '\\';
723 *q++ = *p;
724 break;
725 case '$':
726 *q++ = *p;
727 *q++ = *p;
728 nbs = 0;
729 break;
730 case '#':
731 *q++ = '\\';
732 *q++ = *p;
733 nbs = 0;
734 break;
735 case '\\':
736 *q++ = *p;
737 nbs++;
738 break;
739 default:
740 *q++ = *p;
741 nbs = 0;
742 break;
743 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700744 }
745 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400746 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700747
748 *q = '\0';
749
750 return os;
751}
752
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700753/*
754 * Convert a string to a Watcom make-safe form
755 */
756static char *quote_for_wmake(const char *str)
757{
758 const char *p;
759 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700760 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700761
762 size_t n = 1; /* Terminating zero */
763
764 if (!str)
765 return NULL;
766
767 for (p = str; *p; p++) {
768 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700769 case ' ':
770 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700771 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700772 quote = true;
773 n++;
774 break;
775 case '\"':
776 quote = true;
777 n += 2;
778 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700779 case '$':
780 case '#':
781 n += 2;
782 break;
783 default:
784 n++;
785 break;
786 }
787 }
788
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700789 if (quote)
790 n += 2;
791
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700792 os = q = nasm_malloc(n);
793
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700794 if (quote)
795 *q++ = '\"';
796
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700797 for (p = str; *p; p++) {
798 switch (*p) {
799 case '$':
800 case '#':
801 *q++ = '$';
802 *q++ = *p;
803 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700804 case '\"':
805 *q++ = *p;
806 *q++ = *p;
807 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700808 default:
809 *q++ = *p;
810 break;
811 }
812 }
813
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700814 if (quote)
815 *q++ = '\"';
816
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700817 *q = '\0';
818
819 return os;
820}
821
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100822enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800823 OPT_BOGUS,
824 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700825 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800826 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700827 OPT_MANGLE,
828 OPT_INCLUDE,
829 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700830 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700831 OPT_LIMIT,
832 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100833};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800834struct textargs {
835 const char *label;
836 enum text_options opt;
837 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700838 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800839};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800840static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700841 {"v", OPT_VERSION, false, 0},
842 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700843 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700844 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
845 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
846 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
847 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
848 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
849 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
850 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700851 {"include", OPT_INCLUDE, true, 0},
852 {"pragma", OPT_PRAGMA, true, 0},
853 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700854 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700855 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700856 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000857};
858
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400859static void show_version(void)
860{
861 printf("NASM version %s compiled on %s%s\n",
862 nasm_version, nasm_date, nasm_compile_options);
863 exit(0);
864}
865
H. Peter Anvin423e3812007-11-15 17:12:29 -0800866static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700867static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000869 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000871
872 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000874
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
877 /* These parameters take values */
878 if (!(param = get_param(p, q, &advance)))
879 return advance;
880 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800881
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 switch (p[1]) {
883 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700884 if (pass == 1)
885 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700887
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700889 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800890 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400891 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700892
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700894 if (pass == 1) {
895 ofmt = ofmt_find(param, &ofmt_alias);
896 if (!ofmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700897 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700898 "unrecognised output format `%s' - "
899 "use -hf for a list", param);
900 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 }
902 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700903
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400904 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800905 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700907
H. Peter Anvin55568c12016-10-03 19:46:49 -0700908 if (!*param) {
909 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300910 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700911 } else {
912 while (*param) {
913 switch (*param) {
914 case '0': case '1': case '2': case '3': case '4':
915 case '5': case '6': case '7': case '8': case '9':
916 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700917
Chang S. Baea5786342018-08-15 23:22:21 +0300918 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
919 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700920 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300921 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700922 else
Chang S. Baea5786342018-08-15 23:22:21 +0300923 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700924 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700925
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 case 'v':
927 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 param++;
929 opt_verbose_info = true;
930 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700931
H. Peter Anvin55568c12016-10-03 19:46:49 -0700932 case 'x':
933 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300934 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700935 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700936
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700938 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700939 *param);
940 break;
941 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400942 }
Chang S. Baea5786342018-08-15 23:22:21 +0300943 if (optimizing.level > MAX_OPTIMIZE)
944 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 }
947 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800948
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400949 case 'p': /* pre-include */
950 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700951 if (pass == 2)
952 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400953 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800954
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 case 'd': /* pre-define */
956 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (pass == 2)
958 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400959 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800960
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400961 case 'u': /* un-define */
962 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700963 if (pass == 2)
964 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400965 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800966
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400967 case 'i': /* include search path */
968 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800969 if (pass == 1)
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300970 strlist_add_string(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400971 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800972
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400973 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800975 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800980 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800984 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 using_debug_info = true;
986 debug_format = param;
987 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400988 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800989
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400990 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 if (pass == 1) {
992 if (nasm_stricmp("vc", param) == 0)
993 nasm_set_verror(nasm_verror_vc);
994 else if (nasm_stricmp("gnu", param) == 0)
995 nasm_set_verror(nasm_verror_gnu);
996 else
H. Peter Anvinc5136902018-06-15 18:20:17 -0700997 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700998 "unrecognized error reporting format `%s'",
999 param);
1000 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001004 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001005 using_debug_info = true;
1006 if (p[2])
1007 debug_format = nasm_skip_spaces(p + 2);
1008 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001012 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001013 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 case 'y':
1017 printf("\nvalid debug formats for '%s' output format are"
1018 " ('*' denotes default):\n", ofmt->shortname);
1019 dfmt_list(ofmt, stdout);
1020 exit(0);
1021 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 if (pass == 2)
1025 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001029 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001031
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001032 case 'e': /* preprocess only */
1033 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001034 if (pass == 1)
1035 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001037
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001038 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001039 if (pass == 1)
1040 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001042
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001043 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001044 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001046 if (!set_warning_status(param)) {
1047 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1048 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001049 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001050 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001051 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001054 if (pass == 1) {
1055 switch (p[2]) {
1056 case 'W':
1057 quote_for_make = quote_for_wmake;
1058 break;
1059 case 'D':
1060 case 'F':
1061 case 'T':
1062 case 'Q':
1063 advance = true;
1064 break;
1065 default:
1066 break;
1067 }
1068 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001069 switch (p[2]) {
1070 case 0:
1071 operating_mode = OP_DEPEND;
1072 break;
1073 case 'G':
1074 operating_mode = OP_DEPEND;
1075 depend_missing_ok = true;
1076 break;
1077 case 'P':
1078 depend_emit_phony = true;
1079 break;
1080 case 'D':
1081 operating_mode = OP_NORMAL;
1082 depend_file = q;
1083 advance = true;
1084 break;
1085 case 'F':
1086 depend_file = q;
1087 advance = true;
1088 break;
1089 case 'T':
1090 depend_target = q;
1091 advance = true;
1092 break;
1093 case 'Q':
1094 depend_target = quote_for_make(q);
1095 advance = true;
1096 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001097 case 'W':
1098 /* handled in pass 1 */
1099 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100 default:
1101 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1102 "unknown dependency option `-M%c'", p[2]);
1103 break;
1104 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001105 }
1106 if (advance && (!q || !q[0])) {
1107 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1108 "option `-M%c' requires a parameter", p[2]);
1109 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001110 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 case '-':
1114 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001115 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001116 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001117 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001118
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001119 p += 2;
1120
1121 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001122 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 break;
1124 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001125
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001126 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001127 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001128 olen = strlen(tx->label);
1129
1130 if (olen > plen)
1131 continue;
1132
1133 if (nasm_memicmp(p, tx->label, olen))
1134 continue;
1135
1136 if (tx->label[olen-1] == '-')
1137 break; /* Incomplete option */
1138
1139 if (!p[olen] || p[olen] == '=')
1140 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001142
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001143 if (!tx->label) {
1144 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1145 "unrecognized option `--%s'", p);
1146 }
1147
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001148 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001149 if (param)
1150 *param++ = '\0';
1151
H. Peter Anvin3366e312018-02-07 14:14:36 -08001152 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001153 if (!param) {
1154 param = q;
1155 advance = true;
1156 }
1157
1158 /* Note: a null string is a valid parameter */
1159 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001160 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001161 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001162 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 break;
1164 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 } else {
1166 if (param) {
1167 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1168 "option `--%s' does not take an argument",
1169 p);
1170
1171 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001172 }
1173
1174 switch (tx->opt) {
1175 case OPT_VERSION:
1176 show_version();
1177 break;
1178 case OPT_ABORT_ON_PANIC:
1179 abort_on_panic = true;
1180 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001181 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001182 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001183 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001184 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001185 case OPT_INCLUDE:
1186 if (pass == 2)
1187 preproc->pre_include(q);
1188 break;
1189 case OPT_PRAGMA:
1190 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001191 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001192 break;
1193 case OPT_BEFORE:
1194 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001195 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001196 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001197 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001198 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001199 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001200 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001201 case OPT_KEEP_ALL:
1202 keep_all = true;
1203 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001204 case OPT_HELP:
1205 help(0);
1206 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001207 default:
1208 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001210
1211 if (eqsave)
1212 *eqsave = '='; /* Restore = argument separator */
1213
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 break;
1215 }
1216
1217 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001218 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1219 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 break;
1221 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001222 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001223 /* In theory we could allow multiple input files... */
1224 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001225 }
1226
1227 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001228}
1229
H. Peter Anvineba20a72002-04-30 20:53:55 +00001230#define ARG_BUF_DELTA 128
1231
H. Peter Anvin55568c12016-10-03 19:46:49 -07001232static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001233{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001234 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235 int bufsize, prevargsize;
1236
1237 bufsize = prevargsize = ARG_BUF_DELTA;
1238 buffer = nasm_malloc(ARG_BUF_DELTA);
1239 prevarg = nasm_malloc(ARG_BUF_DELTA);
1240 prevarg[0] = '\0';
1241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 p = buffer;
1244 while (1) { /* Loop to handle long lines */
1245 q = fgets(p, bufsize - (p - buffer), rfile);
1246 if (!q)
1247 break;
1248 p += strlen(p);
1249 if (p > buffer && p[-1] == '\n')
1250 break;
1251 if (p - buffer > bufsize - 10) {
1252 int offset;
1253 offset = p - buffer;
1254 bufsize += ARG_BUF_DELTA;
1255 buffer = nasm_realloc(buffer, bufsize);
1256 p = buffer + offset;
1257 }
1258 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001259
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 if (!q && p == buffer) {
1261 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001262 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 nasm_free(buffer);
1264 nasm_free(prevarg);
1265 return;
1266 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 /*
1269 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1270 * them are present at the end of the line.
1271 */
1272 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001273
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001274 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001277 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001278
H. Peter Anvin55568c12016-10-03 19:46:49 -07001279 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001281
Charles Crayne192d5b52007-10-18 19:02:42 -07001282 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 prevargsize += ARG_BUF_DELTA;
1284 prevarg = nasm_realloc(prevarg, prevargsize);
1285 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001286 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001287 }
1288}
1289
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001290/* Function to process args from a string of args, rather than the
1291 * argv array. Used by the environment variable and response file
1292 * processing.
1293 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001294static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001296 char *p, *q, *arg, *prevarg;
1297 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001298
1299 p = args;
1300 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001302 arg = NULL;
1303 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 q = p;
1305 while (*p && *p != separator)
1306 p++;
1307 while (*p == separator)
1308 *p++ = '\0';
1309 prevarg = arg;
1310 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001311 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001313 }
1314 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001315 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001316}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001317
H. Peter Anvin55568c12016-10-03 19:46:49 -07001318static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001319{
1320 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001321 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001322 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001323 perror(file);
1324 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001325 }
1326 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001327 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001328 }
1329 fclose(f);
1330}
1331
H. Peter Anvin55568c12016-10-03 19:46:49 -07001332static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001333{
1334 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001335 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001336 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001337
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001338 /* Initialize all the warnings to their default state */
1339 for (i = 0; i < ERR_WARN_ALL; i++) {
1340 warning_state_init[i] = warning_state[i] =
1341 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1342 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001343
1344 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001345 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001346 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001347 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001348 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001350 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001352 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001353
1354 /*
1355 * Now process the actual command line.
1356 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001358 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 argv++;
1360 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001361 /*
1362 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 * arguments like the environment variable. This allows us
1364 * to have multiple arguments on a single line, which is
1365 * different to the -@resp file processing below for regular
1366 * NASM.
1367 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001368 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 argc--;
1370 argv++;
1371 }
1372 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001373 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001374 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001375 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001376 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001377 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 fclose(rfile);
1379 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001380 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 "unable to open response file `%s'", p);
1382 }
1383 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001384 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001385 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001386 }
1387
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001388 /*
1389 * Look for basic command line typos. This definitely doesn't
1390 * catch all errors, but it might help cases of fumbled fingers.
1391 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392 if (pass != 2)
1393 return;
1394
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001395 if (!inname)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001396 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001397
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001398 else if ((errname && !strcmp(inname, errname)) ||
1399 (outname && !strcmp(inname, outname)) ||
1400 (listname && !strcmp(inname, listname)) ||
1401 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001402 nasm_fatal_fl(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001403
1404 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001405 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001406 if (!error_file) {
1407 error_file = stderr; /* Revert to default! */
H. Peter Anvinc5136902018-06-15 18:20:17 -07001408 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001409 "cannot open file `%s' for error messages",
1410 errname);
1411 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001412 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001413}
1414
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001415static void assemble_file(const char *fname, StrList *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001416{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001417 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001419 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001420 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001421 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001422
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001423 switch (cmd_sb) {
1424 case 16:
1425 break;
1426 case 32:
1427 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001428 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001429 break;
1430 case 64:
1431 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001432 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001433 break;
1434 default:
1435 panic();
1436 break;
1437 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001438
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001439 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001440 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001441 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001442 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1443 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001444
H. Peter Anvincac0b192017-03-28 16:12:30 -07001445 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 cpu = cmd_cpu;
1447 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001448 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001449 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001450 /* Remove the list file in case we die before the output pass */
1451 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001453 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001454 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001455 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 saa_rewind(forwrefs);
1457 forwref = saa_rstruct(forwrefs);
1458 raa_free(offsets);
1459 offsets = raa_init();
1460 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001461 location.segment = NO_SEG;
1462 location.offset = 0;
1463 if (passn == 1)
1464 location.known = true;
1465 ofmt->reset();
1466 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001467 preproc->reset(fname, pass1, pass1 == 2 ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001468
1469 /* Revert all warnings to the default state */
1470 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001471
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001473
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001475 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001476 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001477 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001479 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001480 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001481 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001482 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001483 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001484 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001485
H. Peter Anvinc7131682017-03-07 17:45:01 -08001486 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001487 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001488
Chang S. Baea5786342018-08-15 23:22:21 +03001489 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001490 if (forwref != NULL && globallineno == forwref->lineno) {
1491 output_ins.forw_ref = true;
1492 do {
1493 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1494 forwref = saa_rstruct(forwrefs);
1495 } while (forwref != NULL
1496 && forwref->lineno == globallineno);
1497 } else
1498 output_ins.forw_ref = false;
1499
1500 if (output_ins.forw_ref) {
1501 if (passn == 1) {
1502 for (i = 0; i < output_ins.operands; i++) {
1503 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1504 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1505 fwinf->lineno = globallineno;
1506 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001507 }
1508 }
1509 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001510 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001511 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001512
H. Peter Anvinc7131682017-03-07 17:45:01 -08001513 /* forw_ref */
1514 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001515 if (!output_ins.label) {
1516 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1517 } else if (output_ins.operands == 1 &&
1518 (output_ins.oprs[0].type & IMMEDIATE) &&
1519 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001520 define_label(output_ins.label,
1521 output_ins.oprs[0].segment,
1522 output_ins.oprs[0].offset, false);
1523 } else if (output_ins.operands == 2
1524 && (output_ins.oprs[0].type & IMMEDIATE)
1525 && (output_ins.oprs[0].type & COLON)
1526 && output_ins.oprs[0].segment == NO_SEG
1527 && output_ins.oprs[0].wrt == NO_SEG
1528 && (output_ins.oprs[1].type & IMMEDIATE)
1529 && output_ins.oprs[1].segment == NO_SEG
1530 && output_ins.oprs[1].wrt == NO_SEG) {
1531 define_label(output_ins.label,
1532 output_ins.oprs[0].offset | SEG_ABS,
1533 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001534 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001535 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001536 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001537 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001538 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001540 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001541
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001542 for (n = 1; n <= output_ins.times; n++) {
1543 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001544 int64_t l = insn_size(location.segment,
1545 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001546 globalbits, &output_ins);
1547
1548 /* if (using_debug_info) && output_ins.opcode != -1) */
1549 if (using_debug_info)
1550 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001551 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001552 int32_t typeinfo =
1553 TYS_ELEMENTS(output_ins.operands);
1554 switch (output_ins.opcode) {
1555 case I_RESB:
1556 typeinfo =
1557 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1558 break;
1559 case I_RESW:
1560 typeinfo =
1561 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1562 break;
1563 case I_RESD:
1564 typeinfo =
1565 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1566 break;
1567 case I_RESQ:
1568 typeinfo =
1569 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1570 break;
1571 case I_REST:
1572 typeinfo =
1573 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1574 break;
1575 case I_RESO:
1576 typeinfo =
1577 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1578 break;
1579 case I_RESY:
1580 typeinfo =
1581 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1582 break;
1583 case I_RESZ:
1584 typeinfo =
1585 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1586 break;
1587 case I_DB:
1588 typeinfo |= TY_BYTE;
1589 break;
1590 case I_DW:
1591 typeinfo |= TY_WORD;
1592 break;
1593 case I_DD:
1594 if (output_ins.eops_float)
1595 typeinfo |= TY_FLOAT;
1596 else
1597 typeinfo |= TY_DWORD;
1598 break;
1599 case I_DQ:
1600 typeinfo |= TY_QWORD;
1601 break;
1602 case I_DT:
1603 typeinfo |= TY_TBYTE;
1604 break;
1605 case I_DO:
1606 typeinfo |= TY_OWORD;
1607 break;
1608 case I_DY:
1609 typeinfo |= TY_YWORD;
1610 break;
1611 case I_DZ:
1612 typeinfo |= TY_ZWORD;
1613 break;
1614 default:
1615 typeinfo = TY_LABEL;
1616 break;
1617 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001618
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001619 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001620 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001621
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001622 /*
1623 * For INCBIN, let the code in assemble
1624 * handle TIMES, so we don't have to read the
1625 * input file over and over.
1626 */
1627 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001628 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001629 }
1630 /*
1631 * else l == -1 => invalid instruction, which will be
1632 * flagged as an error on pass 2
1633 */
1634 } else {
1635 if (n == 2)
1636 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001637 increment_offset(assemble(location.segment,
1638 location.offset,
1639 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001641 } /* not an EQU */
1642 }
1643 if (output_ins.times > 1)
1644 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001645
H. Peter Anvinc7131682017-03-07 17:45:01 -08001646 cleanup_insn(&output_ins);
1647
1648 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001651
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001652 if (global_offset_changed && !terminate_after_phase) {
1653 switch (pass0) {
1654 case 1:
1655 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1656 "phase error during stabilization pass, hoping for the best");
1657 break;
1658
1659 case 2:
1660 nasm_error(ERR_NONFATAL,
1661 "phase error during code generation pass");
1662 break;
1663
1664 default:
1665 /* This is normal, we'll keep going... */
1666 break;
1667 }
1668 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001669
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 if (pass1 == 1)
1671 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001673 /*
1674 * Always run at least two optimization passes (pass0 == 0);
1675 * things like subsections will fail miserably without that.
1676 * Once we commit to a stabilization pass (pass0 == 1), we can't
1677 * go back, and if something goes bad, we can only hope
1678 * that we don't end up with a phase error at the end.
1679 */
1680 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001682 } else if (global_offset_changed &&
1683 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001684 prev_offset_changed = global_offset_changed;
1685 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001686 } else {
1687 stall_count++;
1688 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001689
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001690 if (terminate_after_phase)
1691 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001692
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001693 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1694 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001695 /* We get here if the labels don't converge
1696 * Example: FOO equ FOO + 1
1697 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001698 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001699 "Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001700 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001701 nasm_error(ERR_NONFATAL,
1702 "Possible causes: recursive EQUs, macro abuse.");
1703 break;
1704 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001708 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001709 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001710 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001711 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1712 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001713 }
1714}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001715
Ed Berosetfa771012002-06-09 20:56:40 +00001716/**
1717 * gnu style error reporting
1718 * This function prints an error message to error_file in the
1719 * style used by GNU. An example would be:
1720 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001721 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001722 * which the error occurs (or is detected) and "error:" is one of
1723 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001724 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001725 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001726 *
1727 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001728 * @param fmt the printf style format string
1729 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001730static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001731{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001732 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001733 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001734
Ed Berosetfa771012002-06-09 20:56:40 +00001735 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001737
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001738 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001739 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001740 if (!currentfile || (severity & ERR_TOPFILE)) {
1741 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1742 lineno = 0;
1743 }
1744 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001745
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001746 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001747 if (!lineno)
1748 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001749 else
1750 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001751 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001752
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001753 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001754}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001755
Ed Berosetfa771012002-06-09 20:56:40 +00001756/**
1757 * MS style error reporting
1758 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001759 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001760 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001761 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001762 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001763 * which the error occurs (or is detected) and "error:" is one of
1764 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001765 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001766 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001767 *
1768 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001769 * @param fmt the printf style format string
1770 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001771static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001772{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001773 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001774 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 if (is_suppressed_warning(severity))
1777 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001778
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001779 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001781
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001782 if (!skip_this_pass(severity)) {
1783 if (currentfile) {
1784 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001785 } else {
1786 fputs("nasm: ", error_file);
1787 }
Ed Berosetfa771012002-06-09 20:56:40 +00001788 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001789
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001790 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001791}
1792
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001793/*
1794 * check to see if this is a suppressable warning
1795 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001796static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001797{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001798 /* Not a warning at all */
1799 if ((severity & ERR_MASK) != ERR_WARNING)
1800 return false;
1801
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001802 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001803}
1804
Ed Berosetfa771012002-06-09 20:56:40 +00001805/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001806 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001807 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001808 * not in pass 1
1809 *
1810 * @param severity the severity of the warning or error
1811 * @return true if we should abort error/warning printing
1812 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001813static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001814{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001815 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001816 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001817 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1818 else
1819 return false;
1820}
1821
1822static bool warning_is_error(int severity)
1823{
1824 if (is_valid_warning(severity))
1825 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001826 else
1827 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001828}
1829
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001830static bool skip_this_pass(int severity)
1831{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001832 /*
1833 * See if it's a pass-specific error or warning which should be skipped.
1834 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1835 * they cannot be resumed from.
1836 */
1837 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001838 return false;
1839
H. Peter Anvin934f0472016-05-09 12:00:19 -07001840 /*
1841 * passn is 1 on the very first pass only.
1842 * pass0 is 2 on the code-generation (final) pass only.
1843 * These are the passes we care about in this case.
1844 */
1845 return (((severity & ERR_PASS1) && passn != 1) ||
1846 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001847}
1848
Ed Berosetfa771012002-06-09 20:56:40 +00001849/**
1850 * common error reporting
1851 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001852 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001853 * specific error message to error_file and may or may not return. It
1854 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001855 *
1856 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001857 * @param fmt the printf style format string
1858 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001859static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001860{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001861 char msg[1024];
1862 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001863
H. Peter Anvin7df04172008-06-10 18:27:38 -07001864 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001866 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
1868 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001869 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 break;
1871 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001872 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 break;
1874 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001875 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 break;
1877 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001878 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001880 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001881 pfx = "";
1882 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001883 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001884
H. Peter Anvin934f0472016-05-09 12:00:19 -07001885 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001886 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001887 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001888 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1889 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001890
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001891 if (!skip_this_pass(severity))
1892 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001893
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001894 /* Are we recursing from error_list_macros? */
1895 if (severity & ERR_PP_LISTMACRO)
1896 return;
1897
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001898 /*
1899 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001900 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001901 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001902 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001903
H. Peter Anvin8f622462017-04-02 19:02:29 -07001904 if (skip_this_pass(severity))
1905 return;
1906
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001907 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001908 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001909
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001910 preproc->error_list_macros(severity);
1911
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001912 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 case ERR_DEBUG:
1914 /* no further action, by definition */
1915 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001916 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001917 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001918 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001919 terminate_after_phase = true;
1920 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 break;
1924 case ERR_FATAL:
1925 if (ofile) {
1926 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001927 if (!keep_all)
1928 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001929 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 }
1931 if (want_usage)
1932 usage();
1933 exit(1); /* instantly die */
1934 break; /* placate silly compilers */
1935 case ERR_PANIC:
1936 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001937
1938 if (abort_on_panic)
1939 abort(); /* halt, catch fire, dump core/stop debugger */
1940
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001941 if (ofile) {
1942 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001943 if (!keep_all)
1944 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001945 ofile = NULL;
1946 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 exit(3);
1948 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001949 }
1950}
1951
H. Peter Anvin734b1882002-04-30 21:01:08 +00001952static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001953{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001954 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001955}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001956
1957static void help(const char xopt)
1958{
1959 int i;
1960
1961 printf
1962 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1963 "[-l listfile]\n"
1964 " [options...] [--] filename\n"
1965 " or nasm -v (or --v) for version info\n\n"
1966 "\n"
1967 "Response files should contain command line parameters,\n"
1968 "one per line.\n"
1969 "\n"
1970 " -t assemble in SciTech TASM compatible mode\n");
1971 printf
1972 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1973 " -a don't preprocess (assemble only)\n"
1974 " -M generate Makefile dependencies on stdout\n"
1975 " -MG d:o, missing files assumed generated\n"
1976 " -MF file set Makefile dependency file\n"
1977 " -MD file assemble and generate dependencies\n"
1978 " -MT file dependency target name\n"
1979 " -MQ file dependency target name (quoted)\n"
1980 " -MP emit phony target\n\n"
1981 " -Zfile redirect error messages to file\n"
1982 " -s redirect error messages to stdout\n\n"
1983 " -g generate debugging information\n\n"
1984 " -F format select a debugging format\n\n"
1985 " -gformat same as -g -F format\n\n"
1986 " -o outfile write output to an outfile\n\n"
1987 " -f format select an output format\n\n"
1988 " -l listfile write listing to a listfile\n\n"
1989 " -Ipath add a pathname to the include file path\n");
1990 printf
1991 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1992 " -O0 no optimization\n"
1993 " -O1 minimal optimization\n"
1994 " -Ox multipass optimization (default)\n"
1995 " -Pfile pre-include a file (also --include)\n"
1996 " -Dmacro[=str] pre-define a macro\n"
1997 " -Umacro undefine a macro\n"
1998 " -Xformat specifiy error reporting format (gnu or vc)\n"
1999 " -w+foo enable warning foo (equiv. -Wfoo)\n"
2000 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
2001 " -w[+-]error[=foo]\n"
2002 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002003 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002004 " --pragma str pre-executes a specific %%pragma\n"
2005 " --before str add line (usually a preprocessor statement) before the input\n"
2006 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002007 " to all extern, common and global symbols (also --gprefix)\n"
2008 " --postfix str append the given string to all the given string\n"
2009 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002010 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002011 " --lpostfix str append the given string to all other symbols\n"
2012 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002013 " --limit-X val set execution limit X\n");
2014
2015 for (i = 0; i <= LIMIT_MAX; i++) {
2016 printf(" %-15s %s (default ",
2017 limit_info[i].name, limit_info[i].help);
2018 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07002019 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002020 } else {
2021 printf("unlimited)\n");
2022 }
2023 }
2024
2025 printf("\nWarnings for the -W/-w options:\n");
2026
2027 for (i = 0; i <= ERR_WARN_ALL; i++)
2028 printf(" %-23s %s%s\n",
2029 warnings[i].name, warnings[i].help,
2030 i == ERR_WARN_ALL ? "\n" :
2031 warnings[i].enabled ? " (default on)" :
2032 " (default off)");
2033
2034 if (xopt == 'f') {
2035 printf("valid output formats for -f are"
2036 " (`*' denotes default):\n");
2037 ofmt_list(ofmt, stdout);
2038 } else {
2039 printf("For a list of valid output formats, use -hf.\n");
2040 printf("For a list of debug formats, use -f <format> -y.\n");
2041 }
2042}