blob: 42d3c6c62a45bcf9558bb6f552a47efa3c960856 [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 Anvin81b62b92017-12-20 13:33:49 -080078static 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 Anvin79561022018-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 Anvin79561022018-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
H. Peter Anvin34754622018-11-28 12:36:53 -0800139#define OP_NORMAL (1U << 0)
140#define OP_PREPROCESS (1U << 1)
141#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400142
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 Anvin79561022018-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 Anvin79561022018-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 Anvin79561022018-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
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -0800198 errlevel = ERR_WARNING|ERR_PASS1|WARN_UNKNOWN_PRAGMA;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700199 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
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -0800211 errlevel = ERR_WARNING|ERR_PASS1|WARN_BAD_PRAGMA;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700212 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 */
331static void preproc_init(void)
332{
333 StrList *ip, *iptmp;
334
335 preproc->init();
336 define_macros();
337 list_for_each_safe(ip, iptmp, include_path) {
338 preproc->include_path(ip->str);
339 nasm_free(ip);
340 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800341}
342
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700343static void emit_dependencies(StrList *list)
344{
345 FILE *deps;
346 int linepos, len;
347 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700348 bool wmake = (quote_for_make == quote_for_wmake);
349 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700350
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700351 wrapstr = wmake ? " &\n " : " \\\n ";
352 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700353
354 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700355 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400356 if (!deps) {
357 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
358 "unable to write dependency file `%s'", depend_file);
359 return;
360 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700361 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400362 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700363 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700364
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700365 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400366 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700367 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400368 len = strlen(file);
369 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700370 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400371 linepos = 1;
372 }
373 fprintf(deps, " %s", file);
374 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700375 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700376 }
377 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700378
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400379 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700380 if (depend_emit_phony) {
381 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700382 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700383 nasm_free(file);
384 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400385 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700386 }
387
388 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400389 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390}
391
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700392/* Convert a struct tm to a POSIX-style time constant */
393static int64_t make_posix_time(const struct tm *tm)
394{
395 int64_t t;
396 int64_t y = tm->tm_year;
397
398 /* See IEEE 1003.1:2004, section 4.14 */
399
400 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
401 t += tm->tm_yday;
402 t *= 24;
403 t += tm->tm_hour;
404 t *= 60;
405 t += tm->tm_min;
406 t *= 60;
407 t += tm->tm_sec;
408
409 return t;
410}
411
412static void timestamp(void)
413{
414 struct compile_time * const oct = &official_compile_time;
415 const struct tm *tp, *best_gm;
416
417 time(&oct->t);
418
419 best_gm = NULL;
420
421 tp = localtime(&oct->t);
422 if (tp) {
423 oct->local = *tp;
424 best_gm = &oct->local;
425 oct->have_local = true;
426 }
427
428 tp = gmtime(&oct->t);
429 if (tp) {
430 oct->gm = *tp;
431 best_gm = &oct->gm;
432 oct->have_gm = true;
433 if (!oct->have_local)
434 oct->local = oct->gm;
435 } else {
436 oct->gm = oct->local;
437 }
438
439 if (best_gm) {
440 oct->posix = make_posix_time(best_gm);
441 oct->have_posix = true;
442 }
443}
444
H. Peter Anvin038d8612007-04-12 16:54:50 +0000445int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000446{
H. Peter Anvina771be82017-08-16 14:53:18 -0700447 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700448
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700449 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800450
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800451 iflag_set_default_cpu(&cpu);
452 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400453
Charles Crayne2581c862008-09-10 19:21:52 -0700454 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700455 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400456 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000457
H. Peter Anvin97e15752007-09-25 08:47:47 -0700458 error_file = stderr;
459
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700460 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700461 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700462
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700463 /*
464 * We must call init_labels() before the command line parsing,
465 * because we may be setting prefixes/suffixes from the command
466 * line.
467 */
468 init_labels();
469
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000470 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000471 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000472
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000473 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400474 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475
H. Peter Anvin55568c12016-10-03 19:46:49 -0700476 parse_cmdline(argc, argv, 1);
477 if (terminate_after_phase) {
478 if (want_usage)
479 usage();
480 return 1;
481 }
482
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800483 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800484 if (!using_debug_info) {
485 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800486 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800487 } else if (!debug_format) {
488 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800489 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800490 } else {
491 dfmt = dfmt_find(ofmt, debug_format);
492 if (!dfmt) {
493 nasm_fatal(ERR_NOFILE | ERR_USAGE,
494 "unrecognized debug format `%s' for"
495 " output format `%s'",
496 debug_format, ofmt->shortname);
497 }
498 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000499
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800500 preproc_init();
501
502 parse_cmdline(argc, argv, 2);
503 if (terminate_after_phase) {
504 if (want_usage)
505 usage();
506 return 1;
507 }
508
509 /* Save away the default state of warnings */
510 memcpy(warning_state_init, warning_state, sizeof warning_state);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000511
H. Peter Anvin34754622018-11-28 12:36:53 -0800512 /* Dependency filename if we are also doing other things */
513 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
514 if (outname)
515 depend_file = nasm_strcat(outname, ".d");
516 else
517 depend_file = filename_set_extension(inname, ".d");
518 }
519
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300520 /*
521 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800522 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300523 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300524 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800525 if (!outname && !(operating_mode & OP_PREPROCESS)) {
526 outname = filename_set_extension(inname, ofmt->extension);
527 if (!strcmp(outname, inname)) {
528 outname = "nasm.out";
529 nasm_error(ERR_WARNING,
530 "default output file same as input, using `%s' for output\n",
H. Peter Anvinda794322018-11-26 14:15:46 -0800531 outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800532 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300533 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800534
H. Peter Anvin34754622018-11-28 12:36:53 -0800535 depend_ptr = (operating_mode & OP_DEPEND) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700536
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700537 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400538 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700539
H. Peter Anvin34754622018-11-28 12:36:53 -0800540 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000541 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400543 if (depend_missing_ok)
544 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700545
H. Peter Anvin130736c2016-02-17 20:27:41 -0800546 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 while ((line = preproc->getline()))
549 nasm_free(line);
550 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400551 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000552 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700553 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000554 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000556
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800557 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700558 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800560 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 "unable to open output file `%s'",
562 outname);
563 } else
564 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000565
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700566 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000567
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400568 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800569 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800570
571 /* Revert all warnings to the default state */
572 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700573
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 while ((line = preproc->getline())) {
575 /*
576 * We generate %line directives if needed for later programs
577 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000578 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 int altline = src_get(&linnum, &file_name);
580 if (altline) {
581 if (altline == 1 && lineinc == 1)
582 nasm_fputs("", ofile);
583 else {
584 lineinc = (altline != -1 || lineinc != 1);
585 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000586 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 file_name);
588 }
589 prior_linnum = linnum;
590 }
591 nasm_fputs(line, ofile);
592 nasm_free(line);
593 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 preproc->cleanup(0);
595 if (ofile)
596 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700597 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400599 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400600 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400602 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700603 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400604 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800605 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400606 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400608 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400609 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400611 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200612
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400613 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800614 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400615 cleanup_labels();
616 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700617 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400618 nasm_error(ERR_NONFATAL|ERR_NOFILE,
619 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700620 terminate_after_phase = true;
621 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400622 }
623
624 if (ofile) {
625 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700626 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400627 remove(outname);
628 ofile = NULL;
629 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000630 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000631
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700632 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400633 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700634
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000635 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000637
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 raa_free(offsets);
639 saa_free(forwrefs);
640 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000641 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700642 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000643
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700644 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645}
646
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647/*
648 * Get a parameter for a command line option.
649 * First arg must be in the form of e.g. -f...
650 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800651static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400654 if (p[2]) /* the parameter's in the option */
655 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800657 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400660 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000662 return NULL;
663}
664
H. Peter Anvindc242712007-11-18 11:55:10 -0800665/*
666 * Copy a filename
667 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800668static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800669{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800670 if (*dst)
671 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800672
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800673 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800674}
675
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700676/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700677 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700678 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700679static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700680{
681 const char *p;
682 char *os, *q;
683
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400684 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700685 size_t nbs = 0;
686
687 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400688 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700689
690 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400691 switch (*p) {
692 case ' ':
693 case '\t':
694 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
695 n += nbs + 2;
696 nbs = 0;
697 break;
698 case '$':
699 case '#':
700 nbs = 0;
701 n += 2;
702 break;
703 case '\\':
704 nbs++;
705 n++;
706 break;
707 default:
708 nbs = 0;
709 n++;
710 break;
711 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700712 }
713
714 /* Convert N backslashes at the end of filename to 2N backslashes */
715 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400716 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700717
718 os = q = nasm_malloc(n);
719
720 nbs = 0;
721 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400722 switch (*p) {
723 case ' ':
724 case '\t':
725 while (nbs--)
726 *q++ = '\\';
727 *q++ = '\\';
728 *q++ = *p;
729 break;
730 case '$':
731 *q++ = *p;
732 *q++ = *p;
733 nbs = 0;
734 break;
735 case '#':
736 *q++ = '\\';
737 *q++ = *p;
738 nbs = 0;
739 break;
740 case '\\':
741 *q++ = *p;
742 nbs++;
743 break;
744 default:
745 *q++ = *p;
746 nbs = 0;
747 break;
748 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700749 }
750 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400751 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700752
753 *q = '\0';
754
755 return os;
756}
757
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700758/*
759 * Convert a string to a Watcom make-safe form
760 */
761static char *quote_for_wmake(const char *str)
762{
763 const char *p;
764 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700765 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700766
767 size_t n = 1; /* Terminating zero */
768
769 if (!str)
770 return NULL;
771
772 for (p = str; *p; p++) {
773 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700774 case ' ':
775 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700776 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700777 quote = true;
778 n++;
779 break;
780 case '\"':
781 quote = true;
782 n += 2;
783 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700784 case '$':
785 case '#':
786 n += 2;
787 break;
788 default:
789 n++;
790 break;
791 }
792 }
793
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700794 if (quote)
795 n += 2;
796
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700797 os = q = nasm_malloc(n);
798
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700799 if (quote)
800 *q++ = '\"';
801
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700802 for (p = str; *p; p++) {
803 switch (*p) {
804 case '$':
805 case '#':
806 *q++ = '$';
807 *q++ = *p;
808 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700809 case '\"':
810 *q++ = *p;
811 *q++ = *p;
812 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700813 default:
814 *q++ = *p;
815 break;
816 }
817 }
818
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700819 if (quote)
820 *q++ = '\"';
821
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700822 *q = '\0';
823
824 return os;
825}
826
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100827enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800828 OPT_BOGUS,
829 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700830 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800831 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700832 OPT_MANGLE,
833 OPT_INCLUDE,
834 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700835 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700836 OPT_LIMIT,
837 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100838};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800839struct textargs {
840 const char *label;
841 enum text_options opt;
842 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700843 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800844};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800845static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700846 {"v", OPT_VERSION, false, 0},
847 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700848 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700849 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
850 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
851 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
852 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
853 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
854 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
855 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700856 {"include", OPT_INCLUDE, true, 0},
857 {"pragma", OPT_PRAGMA, true, 0},
858 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700859 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700860 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700861 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000862};
863
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400864static void show_version(void)
865{
866 printf("NASM version %s compiled on %s%s\n",
867 nasm_version, nasm_date, nasm_compile_options);
868 exit(0);
869}
870
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000873{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000874 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800875 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000876
877 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000879
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400881 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
882 /* These parameters take values */
883 if (!(param = get_param(p, q, &advance)))
884 return advance;
885 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800886
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 switch (p[1]) {
888 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700889 if (pass == 1)
890 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700892
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700894 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800895 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700897
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700899 if (pass == 1) {
900 ofmt = ofmt_find(param, &ofmt_alias);
901 if (!ofmt) {
902 nasm_fatal(ERR_NOFILE | ERR_USAGE,
903 "unrecognised output format `%s' - "
904 "use -hf for a list", param);
905 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 }
907 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700908
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400909 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800910 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700911 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700912
H. Peter Anvin55568c12016-10-03 19:46:49 -0700913 if (!*param) {
914 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300915 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700916 } else {
917 while (*param) {
918 switch (*param) {
919 case '0': case '1': case '2': case '3': case '4':
920 case '5': case '6': case '7': case '8': case '9':
921 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700922
Chang S. Baea5786342018-08-15 23:22:21 +0300923 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
924 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700925 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300926 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700927 else
Chang S. Baea5786342018-08-15 23:22:21 +0300928 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700930
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 case 'v':
932 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 param++;
934 opt_verbose_info = true;
935 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700936
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 case 'x':
938 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300939 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700941
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 default:
943 nasm_fatal(0,
944 "unknown optimization option -O%c\n",
945 *param);
946 break;
947 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 }
Chang S. Baea5786342018-08-15 23:22:21 +0300949 if (optimizing.level > MAX_OPTIMIZE)
950 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400952 }
953 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800954
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 case 'p': /* pre-include */
956 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (pass == 2)
958 preproc->pre_include(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 'd': /* pre-define */
962 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700963 if (pass == 2)
964 preproc->pre_define(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 'u': /* un-define */
968 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700969 if (pass == 2)
970 preproc->pre_undefine(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 'i': /* include search path */
974 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800975 if (pass == 1)
976 nasm_add_string_to_strlist(&include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400977 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800978
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400979 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700980 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800981 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400982 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400984 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800986 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400989 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800990 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 using_debug_info = true;
992 debug_format = param;
993 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800995
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700997 if (pass == 1) {
998 if (nasm_stricmp("vc", param) == 0)
999 nasm_set_verror(nasm_verror_vc);
1000 else if (nasm_stricmp("gnu", param) == 0)
1001 nasm_set_verror(nasm_verror_gnu);
1002 else
1003 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1004 "unrecognized error reporting format `%s'",
1005 param);
1006 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001010 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 using_debug_info = true;
1012 if (p[2])
1013 debug_format = nasm_skip_spaces(p + 2);
1014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001018 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001019 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 case 'y':
1023 printf("\nvalid debug formats for '%s' output format are"
1024 " ('*' denotes default):\n", ofmt->shortname);
1025 dfmt_list(ofmt, stdout);
1026 exit(0);
1027 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001030 if (pass == 2)
1031 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001033
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001035 show_version();
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 'e': /* preprocess only */
1039 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 if (pass == 1)
1041 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001043
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001044 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 if (pass == 1)
1046 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001048
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001049 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001050 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001051 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001052 if (!set_warning_status(param)) {
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001053 nasm_error(ERR_WARNING|ERR_NOFILE|WARN_UNK_WARNING,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001054 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001055 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001056 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001057 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001060 if (pass == 1) {
1061 switch (p[2]) {
1062 case 'W':
1063 quote_for_make = quote_for_wmake;
1064 break;
1065 case 'D':
1066 case 'F':
1067 case 'T':
1068 case 'Q':
1069 advance = true;
1070 break;
1071 default:
1072 break;
1073 }
1074 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001075 switch (p[2]) {
1076 case 0:
1077 operating_mode = OP_DEPEND;
1078 break;
1079 case 'G':
1080 operating_mode = OP_DEPEND;
1081 depend_missing_ok = true;
1082 break;
1083 case 'P':
1084 depend_emit_phony = true;
1085 break;
1086 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001087 operating_mode |= OP_DEPEND;
1088 if (q && (q[0] != '-' || q[1] == '\0')) {
1089 depend_file = q;
1090 advance = true;
1091 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001092 break;
1093 case 'F':
1094 depend_file = q;
1095 advance = true;
1096 break;
1097 case 'T':
1098 depend_target = q;
1099 advance = true;
1100 break;
1101 case 'Q':
1102 depend_target = quote_for_make(q);
1103 advance = true;
1104 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001105 case 'W':
1106 /* handled in pass 1 */
1107 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001108 default:
1109 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1110 "unknown dependency option `-M%c'", p[2]);
1111 break;
1112 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001113 }
1114 if (advance && (!q || !q[0])) {
1115 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1116 "option `-M%c' requires a parameter", p[2]);
1117 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001118 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001120
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 case '-':
1122 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001123 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001124 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001125 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001126
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001127 p += 2;
1128
1129 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001130 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 break;
1132 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001133
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001134 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001135 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001136 olen = strlen(tx->label);
1137
1138 if (olen > plen)
1139 continue;
1140
1141 if (nasm_memicmp(p, tx->label, olen))
1142 continue;
1143
1144 if (tx->label[olen-1] == '-')
1145 break; /* Incomplete option */
1146
1147 if (!p[olen] || p[olen] == '=')
1148 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001150
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001151 if (!tx->label) {
1152 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1153 "unrecognized option `--%s'", p);
1154 }
1155
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001156 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001157 if (param)
1158 *param++ = '\0';
1159
H. Peter Anvin3366e312018-02-07 14:14:36 -08001160 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001161 if (!param) {
1162 param = q;
1163 advance = true;
1164 }
1165
1166 /* Note: a null string is a valid parameter */
1167 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001168 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001169 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 break;
1172 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001173 } else {
1174 if (param) {
1175 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1176 "option `--%s' does not take an argument",
1177 p);
1178
1179 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001180 }
1181
1182 switch (tx->opt) {
1183 case OPT_VERSION:
1184 show_version();
1185 break;
1186 case OPT_ABORT_ON_PANIC:
1187 abort_on_panic = true;
1188 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001189 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001190 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001191 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001192 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001193 case OPT_INCLUDE:
1194 if (pass == 2)
1195 preproc->pre_include(q);
1196 break;
1197 case OPT_PRAGMA:
1198 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001199 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001200 break;
1201 case OPT_BEFORE:
1202 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001203 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001204 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001205 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001206 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001207 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001208 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001209 case OPT_KEEP_ALL:
1210 keep_all = true;
1211 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001212 case OPT_HELP:
1213 help(0);
1214 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001215 default:
1216 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001218
1219 if (eqsave)
1220 *eqsave = '='; /* Restore = argument separator */
1221
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 break;
1223 }
1224
1225 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001226 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1227 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 break;
1229 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001230 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001231 /* In theory we could allow multiple input files... */
1232 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001233 }
1234
1235 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001236}
1237
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238#define ARG_BUF_DELTA 128
1239
H. Peter Anvin55568c12016-10-03 19:46:49 -07001240static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001242 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001243 int bufsize, prevargsize;
1244
1245 bufsize = prevargsize = ARG_BUF_DELTA;
1246 buffer = nasm_malloc(ARG_BUF_DELTA);
1247 prevarg = nasm_malloc(ARG_BUF_DELTA);
1248 prevarg[0] = '\0';
1249
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 p = buffer;
1252 while (1) { /* Loop to handle long lines */
1253 q = fgets(p, bufsize - (p - buffer), rfile);
1254 if (!q)
1255 break;
1256 p += strlen(p);
1257 if (p > buffer && p[-1] == '\n')
1258 break;
1259 if (p - buffer > bufsize - 10) {
1260 int offset;
1261 offset = p - buffer;
1262 bufsize += ARG_BUF_DELTA;
1263 buffer = nasm_realloc(buffer, bufsize);
1264 p = buffer + offset;
1265 }
1266 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 if (!q && p == buffer) {
1269 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001270 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 nasm_free(buffer);
1272 nasm_free(prevarg);
1273 return;
1274 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001275
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 /*
1277 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1278 * them are present at the end of the line.
1279 */
1280 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001281
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001282 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001284
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001285 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286
H. Peter Anvin55568c12016-10-03 19:46:49 -07001287 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001289
Charles Crayne192d5b52007-10-18 19:02:42 -07001290 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 prevargsize += ARG_BUF_DELTA;
1292 prevarg = nasm_realloc(prevarg, prevargsize);
1293 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001294 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295 }
1296}
1297
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001298/* Function to process args from a string of args, rather than the
1299 * argv array. Used by the environment variable and response file
1300 * processing.
1301 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001302static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001304 char *p, *q, *arg, *prevarg;
1305 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001306
1307 p = args;
1308 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001310 arg = NULL;
1311 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 q = p;
1313 while (*p && *p != separator)
1314 p++;
1315 while (*p == separator)
1316 *p++ = '\0';
1317 prevarg = arg;
1318 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001319 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001321 }
1322 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001323 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001324}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001325
H. Peter Anvin55568c12016-10-03 19:46:49 -07001326static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001327{
1328 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001329 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001330 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001331 perror(file);
1332 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001333 }
1334 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001335 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001336 }
1337 fclose(f);
1338}
1339
H. Peter Anvin55568c12016-10-03 19:46:49 -07001340static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341{
1342 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001343 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001344 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001345
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001346 /* Initialize all the warnings to their default state */
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001347 for (i = 0; i < WARN_ALL; i++) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001348 warning_state_init[i] = warning_state[i] =
1349 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1350 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001351
1352 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001353 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001354 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001355 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001356 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001358 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001360 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001361
1362 /*
1363 * Now process the actual command line.
1364 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001366 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 argv++;
1368 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001369 /*
1370 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 * arguments like the environment variable. This allows us
1372 * to have multiple arguments on a single line, which is
1373 * different to the -@resp file processing below for regular
1374 * NASM.
1375 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001376 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 argc--;
1378 argv++;
1379 }
1380 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001381 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001382 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001383 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001384 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001385 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 fclose(rfile);
1387 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001388 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 "unable to open response file `%s'", p);
1390 }
1391 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001393 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001394 }
1395
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001396 /*
1397 * Look for basic command line typos. This definitely doesn't
1398 * catch all errors, but it might help cases of fumbled fingers.
1399 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001400 if (pass != 2)
1401 return;
1402
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001403 if (!inname)
1404 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001405
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001406 else if ((errname && !strcmp(inname, errname)) ||
1407 (outname && !strcmp(inname, outname)) ||
1408 (listname && !strcmp(inname, listname)) ||
1409 (depend_file && !strcmp(inname, depend_file)))
1410 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1411
1412 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001413 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001414 if (!error_file) {
1415 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001416 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001417 "cannot open file `%s' for error messages",
1418 errname);
1419 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001420 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001421}
1422
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001423static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001424{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001425 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001427 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001428 uint64_t prev_offset_changed;
H. Peter Anvin79561022018-06-15 17:51:39 -07001429 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001430
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001431 switch (cmd_sb) {
1432 case 16:
1433 break;
1434 case 32:
1435 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1436 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1437 break;
1438 case 64:
1439 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1440 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1441 break;
1442 default:
1443 panic();
1444 break;
1445 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001446
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001447 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001448 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001449 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001450 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1451 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001452
H. Peter Anvincac0b192017-03-28 16:12:30 -07001453 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 cpu = cmd_cpu;
1455 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001456 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001457 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001458 /* Remove the list file in case we die before the output pass */
1459 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001461 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001462 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001463 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 saa_rewind(forwrefs);
1465 forwref = saa_rstruct(forwrefs);
1466 raa_free(offsets);
1467 offsets = raa_init();
1468 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001469 location.segment = NO_SEG;
1470 location.offset = 0;
1471 if (passn == 1)
1472 location.known = true;
1473 ofmt->reset();
1474 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001475 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001476
1477 /* Revert all warnings to the default state */
1478 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001479
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001481
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 while ((line = preproc->getline())) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001483 if (++globallineno > nasm_limit[LIMIT_LINES])
1484 nasm_fatal(0,
1485 "overall line count exceeds the maximum %"PRId64"\n",
1486 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001488 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001489 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001490 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001492 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001493 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001494
H. Peter Anvinc7131682017-03-07 17:45:01 -08001495 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001496 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001497
Chang S. Baea5786342018-08-15 23:22:21 +03001498 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001499 if (forwref != NULL && globallineno == forwref->lineno) {
1500 output_ins.forw_ref = true;
1501 do {
1502 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1503 forwref = saa_rstruct(forwrefs);
1504 } while (forwref != NULL
1505 && forwref->lineno == globallineno);
1506 } else
1507 output_ins.forw_ref = false;
1508
1509 if (output_ins.forw_ref) {
1510 if (passn == 1) {
1511 for (i = 0; i < output_ins.operands; i++) {
1512 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1513 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1514 fwinf->lineno = globallineno;
1515 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001516 }
1517 }
1518 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001519 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001520 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001521
H. Peter Anvinc7131682017-03-07 17:45:01 -08001522 /* forw_ref */
1523 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001524 if (!output_ins.label) {
1525 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1526 } else if (output_ins.operands == 1 &&
1527 (output_ins.oprs[0].type & IMMEDIATE) &&
1528 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001529 define_label(output_ins.label,
1530 output_ins.oprs[0].segment,
1531 output_ins.oprs[0].offset, false);
1532 } else if (output_ins.operands == 2
1533 && (output_ins.oprs[0].type & IMMEDIATE)
1534 && (output_ins.oprs[0].type & COLON)
1535 && output_ins.oprs[0].segment == NO_SEG
1536 && output_ins.oprs[0].wrt == NO_SEG
1537 && (output_ins.oprs[1].type & IMMEDIATE)
1538 && output_ins.oprs[1].segment == NO_SEG
1539 && output_ins.oprs[1].wrt == NO_SEG) {
1540 define_label(output_ins.label,
1541 output_ins.oprs[0].offset | SEG_ABS,
1542 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001543 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001544 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001545 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001546 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001547 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001549 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001550
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001551 for (n = 1; n <= output_ins.times; n++) {
1552 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001553 int64_t l = insn_size(location.segment,
1554 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001555 globalbits, &output_ins);
1556
1557 /* if (using_debug_info) && output_ins.opcode != -1) */
1558 if (using_debug_info)
1559 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001560 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001561 int32_t typeinfo =
1562 TYS_ELEMENTS(output_ins.operands);
1563 switch (output_ins.opcode) {
1564 case I_RESB:
1565 typeinfo =
1566 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1567 break;
1568 case I_RESW:
1569 typeinfo =
1570 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1571 break;
1572 case I_RESD:
1573 typeinfo =
1574 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1575 break;
1576 case I_RESQ:
1577 typeinfo =
1578 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1579 break;
1580 case I_REST:
1581 typeinfo =
1582 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1583 break;
1584 case I_RESO:
1585 typeinfo =
1586 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1587 break;
1588 case I_RESY:
1589 typeinfo =
1590 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1591 break;
1592 case I_RESZ:
1593 typeinfo =
1594 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1595 break;
1596 case I_DB:
1597 typeinfo |= TY_BYTE;
1598 break;
1599 case I_DW:
1600 typeinfo |= TY_WORD;
1601 break;
1602 case I_DD:
1603 if (output_ins.eops_float)
1604 typeinfo |= TY_FLOAT;
1605 else
1606 typeinfo |= TY_DWORD;
1607 break;
1608 case I_DQ:
1609 typeinfo |= TY_QWORD;
1610 break;
1611 case I_DT:
1612 typeinfo |= TY_TBYTE;
1613 break;
1614 case I_DO:
1615 typeinfo |= TY_OWORD;
1616 break;
1617 case I_DY:
1618 typeinfo |= TY_YWORD;
1619 break;
1620 case I_DZ:
1621 typeinfo |= TY_ZWORD;
1622 break;
1623 default:
1624 typeinfo = TY_LABEL;
1625 break;
1626 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001627
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001628 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001629 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001630
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001631 /*
1632 * For INCBIN, let the code in assemble
1633 * handle TIMES, so we don't have to read the
1634 * input file over and over.
1635 */
1636 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001637 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001638 }
1639 /*
1640 * else l == -1 => invalid instruction, which will be
1641 * flagged as an error on pass 2
1642 */
1643 } else {
1644 if (n == 2)
1645 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001646 increment_offset(assemble(location.segment,
1647 location.offset,
1648 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001650 } /* not an EQU */
1651 }
1652 if (output_ins.times > 1)
1653 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001654
H. Peter Anvinc7131682017-03-07 17:45:01 -08001655 cleanup_insn(&output_ins);
1656
1657 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001660
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001661 if (global_offset_changed && !terminate_after_phase) {
1662 switch (pass0) {
1663 case 1:
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001664 nasm_error(ERR_WARNING|WARN_PHASE,
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001665 "phase error during stabilization pass, hoping for the best");
1666 break;
1667
1668 case 2:
1669 nasm_error(ERR_NONFATAL,
1670 "phase error during code generation pass");
1671 break;
1672
1673 default:
1674 /* This is normal, we'll keep going... */
1675 break;
1676 }
1677 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001678
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 if (pass1 == 1)
1680 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001681
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001682 /*
1683 * Always run at least two optimization passes (pass0 == 0);
1684 * things like subsections will fail miserably without that.
1685 * Once we commit to a stabilization pass (pass0 == 1), we can't
1686 * go back, and if something goes bad, we can only hope
1687 * that we don't end up with a phase error at the end.
1688 */
1689 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001691 } else if (global_offset_changed &&
1692 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001693 prev_offset_changed = global_offset_changed;
1694 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001695 } else {
1696 stall_count++;
1697 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001698
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001699 if (terminate_after_phase)
1700 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001701
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001702 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1703 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001704 /* We get here if the labels don't converge
1705 * Example: FOO equ FOO + 1
1706 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001707 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001708 "Can't find valid values for all labels "
H. Peter Anvin79561022018-06-15 17:51:39 -07001709 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001710 nasm_error(ERR_NONFATAL,
1711 "Possible causes: recursive EQUs, macro abuse.");
1712 break;
1713 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001714 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001715
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001717 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001718 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001719 /* -On and -Ov switches */
H. Peter Anvin79561022018-06-15 17:51:39 -07001720 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1721 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001722 }
1723}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001724
Ed Berosetfa771012002-06-09 20:56:40 +00001725/**
1726 * gnu style error reporting
1727 * This function prints an error message to error_file in the
1728 * style used by GNU. An example would be:
1729 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001730 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001731 * which the error occurs (or is detected) and "error:" is one of
1732 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001733 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001734 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001735 *
1736 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001737 * @param fmt the printf style format string
1738 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001739static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001740{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001741 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001742 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001743
Ed Berosetfa771012002-06-09 20:56:40 +00001744 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001746
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001747 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001748 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001749 if (!currentfile || (severity & ERR_TOPFILE)) {
1750 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1751 lineno = 0;
1752 }
1753 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001754
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001755 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001756 if (!lineno)
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001757 fprintf(error_file, "%s: ", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001758 else
1759 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001761
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001762 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001763}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001764
Ed Berosetfa771012002-06-09 20:56:40 +00001765/**
1766 * MS style error reporting
1767 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001768 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001769 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001770 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001771 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001772 * which the error occurs (or is detected) and "error:" is one of
1773 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001774 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001775 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001776 *
1777 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001778 * @param fmt the printf style format string
1779 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001780static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001781{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001782 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001783 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 if (is_suppressed_warning(severity))
1786 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001787
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001788 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001790
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001791 if (!skip_this_pass(severity)) {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001792 if (lineno) {
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001793 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001794 } else {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001795 fprintf(error_file , "%s : ", currentfile ? currentfile : "nasm");
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001796 }
Ed Berosetfa771012002-06-09 20:56:40 +00001797 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001798
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001799 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001800}
1801
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001802/*
1803 * check to see if this is a suppressable warning
1804 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001805static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001806{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001807 /* Not a warning at all */
1808 if ((severity & ERR_MASK) != ERR_WARNING)
1809 return false;
1810
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001811 return WARN_IDX(severity) < WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001812}
1813
Ed Berosetfa771012002-06-09 20:56:40 +00001814/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001815 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001816 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001817 * not in pass 1
1818 *
1819 * @param severity the severity of the warning or error
1820 * @return true if we should abort error/warning printing
1821 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001822static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001823{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001824 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001825 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001826 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1827 else
1828 return false;
1829}
1830
1831static bool warning_is_error(int severity)
1832{
1833 if (is_valid_warning(severity))
1834 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001835 else
1836 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001837}
1838
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001839static bool skip_this_pass(int severity)
1840{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001841 /*
1842 * See if it's a pass-specific error or warning which should be skipped.
1843 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1844 * they cannot be resumed from.
1845 */
1846 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001847 return false;
1848
H. Peter Anvin934f0472016-05-09 12:00:19 -07001849 /*
1850 * passn is 1 on the very first pass only.
1851 * pass0 is 2 on the code-generation (final) pass only.
1852 * These are the passes we care about in this case.
1853 */
1854 return (((severity & ERR_PASS1) && passn != 1) ||
1855 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001856}
1857
Ed Berosetfa771012002-06-09 20:56:40 +00001858/**
1859 * common error reporting
1860 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001861 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001862 * specific error message to error_file and may or may not return. It
1863 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001864 *
1865 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001866 * @param fmt the printf style format string
1867 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001868static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001869{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001870 char msg[1024];
1871 const char *pfx;
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001872 bool warn_is_err = warning_is_error(severity);
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001873 bool warn_is_other = WARN_IDX(severity) == WARN_OTHER;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001874
H. Peter Anvin7df04172008-06-10 18:27:38 -07001875 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvind84f9a72018-12-10 13:29:35 -08001876 case ERR_NOTE:
1877 pfx = "note: ";
1878 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 case ERR_WARNING:
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001880 if (!warn_is_err) {
1881 pfx = "warning: ";
1882 break;
1883 }
1884 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001886 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 break;
1888 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001889 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 break;
1891 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001892 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 break;
1894 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001895 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001897 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001898 pfx = "";
1899 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001900 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001901
H. Peter Anvin934f0472016-05-09 12:00:19 -07001902 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001903 if (is_valid_warning(severity) && (warn_is_err || !warn_is_other)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001904 char *p = strchr(msg, '\0');
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001905 snprintf(p, 64, " [-w+%s%s]",
1906 warn_is_err ? "error=" : "",
1907 warnings[WARN_IDX(severity)].name);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001908 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001909
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001910 if (!skip_this_pass(severity))
1911 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001912
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001913 /* Are we recursing from error_list_macros? */
1914 if (severity & ERR_PP_LISTMACRO)
1915 return;
1916
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001917 /*
1918 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001919 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001920 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001921 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001922
H. Peter Anvin8f622462017-04-02 19:02:29 -07001923 if (skip_this_pass(severity))
1924 return;
1925
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001926 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001927 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001928
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001929 preproc->error_list_macros(severity);
1930
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001931 switch (severity & ERR_MASK) {
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001932 case ERR_NOTE:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 case ERR_DEBUG:
1934 /* no further action, by definition */
1935 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001936 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001937 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001938 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001939 terminate_after_phase = true;
1940 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001942 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 break;
1944 case ERR_FATAL:
1945 if (ofile) {
1946 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001947 if (!keep_all)
1948 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001949 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 }
1951 if (want_usage)
1952 usage();
1953 exit(1); /* instantly die */
1954 break; /* placate silly compilers */
1955 case ERR_PANIC:
1956 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001957
1958 if (abort_on_panic)
1959 abort(); /* halt, catch fire, dump core/stop debugger */
1960
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001961 if (ofile) {
1962 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001963 if (!keep_all)
1964 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001965 ofile = NULL;
1966 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 exit(3);
1968 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001969 default:
1970 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001971 }
1972}
1973
H. Peter Anvin734b1882002-04-30 21:01:08 +00001974static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001975{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001976 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001977}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001978
1979static void help(const char xopt)
1980{
1981 int i;
1982
1983 printf
1984 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1985 "[-l listfile]\n"
1986 " [options...] [--] filename\n"
1987 " or nasm -v (or --v) for version info\n\n"
1988 "\n"
1989 "Response files should contain command line parameters,\n"
1990 "one per line.\n"
1991 "\n"
1992 " -t assemble in SciTech TASM compatible mode\n");
1993 printf
1994 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1995 " -a don't preprocess (assemble only)\n"
1996 " -M generate Makefile dependencies on stdout\n"
1997 " -MG d:o, missing files assumed generated\n"
1998 " -MF file set Makefile dependency file\n"
1999 " -MD file assemble and generate dependencies\n"
2000 " -MT file dependency target name\n"
2001 " -MQ file dependency target name (quoted)\n"
2002 " -MP emit phony target\n\n"
2003 " -Zfile redirect error messages to file\n"
2004 " -s redirect error messages to stdout\n\n"
2005 " -g generate debugging information\n\n"
2006 " -F format select a debugging format\n\n"
2007 " -gformat same as -g -F format\n\n"
2008 " -o outfile write output to an outfile\n\n"
2009 " -f format select an output format\n\n"
2010 " -l listfile write listing to a listfile\n\n"
2011 " -Ipath add a pathname to the include file path\n");
2012 printf
2013 (" -Olevel optimize opcodes, immediates and branch offsets\n"
2014 " -O0 no optimization\n"
2015 " -O1 minimal optimization\n"
2016 " -Ox multipass optimization (default)\n"
2017 " -Pfile pre-include a file (also --include)\n"
2018 " -Dmacro[=str] pre-define a macro\n"
2019 " -Umacro undefine a macro\n"
2020 " -Xformat specifiy error reporting format (gnu or vc)\n"
2021 " -w+foo enable warning foo (equiv. -Wfoo)\n"
2022 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
2023 " -w[+-]error[=foo]\n"
2024 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002025 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002026 " --pragma str pre-executes a specific %%pragma\n"
2027 " --before str add line (usually a preprocessor statement) before the input\n"
2028 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002029 " to all extern, common and global symbols (also --gprefix)\n"
2030 " --postfix str append the given string to all the given string\n"
2031 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002032 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002033 " --lpostfix str append the given string to all other symbols\n"
2034 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002035 " --limit-X val set execution limit X\n");
2036
2037 for (i = 0; i <= LIMIT_MAX; i++) {
2038 printf(" %-15s %s (default ",
2039 limit_info[i].name, limit_info[i].help);
2040 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin79561022018-06-15 17:51:39 -07002041 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002042 } else {
2043 printf("unlimited)\n");
2044 }
2045 }
2046
2047 printf("\nWarnings for the -W/-w options:\n");
2048
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08002049 for (i = 0; i <= WARN_ALL; i++)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002050 printf(" %-23s %s%s\n",
2051 warnings[i].name, warnings[i].help,
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08002052 i == WARN_ALL ? "\n" :
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002053 warnings[i].enabled ? " (default on)" :
2054 " (default off)");
2055
2056 if (xopt == 'f') {
2057 printf("valid output formats for -f are"
2058 " (`*' denotes default):\n");
2059 ofmt_list(ofmt, stdout);
2060 } else {
2061 printf("For a list of valid output formats, use -hf.\n");
2062 printf("For a list of debug formats, use -f <format> -y.\n");
2063 }
2064}