blob: 5290b07876eae8544061df28757af627eeb60eef [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;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700120int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100121static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400122
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800123iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400124static iflag_t cmd_cpu;
125
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800126struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800127bool in_absolute; /* Flag we are in ABSOLUTE seg */
128struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000129
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000131
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700133static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134
H. Peter Anvine7469712016-02-18 02:20:59 -0800135static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400136
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400137#define OP_NORMAL (1u << 0)
138#define OP_PREPROCESS (1u << 1)
139#define OP_DEPEND (1u << 2)
140
141static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400142
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700143/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700144static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700145static bool depend_missing_ok = false;
146static const char *depend_target = NULL;
147static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700148StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000149
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700150static bool want_usage;
151static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800152bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000153
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700154static char *quote_for_pmake(const char *str);
155static char *quote_for_wmake(const char *str);
156static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700157
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700158/*
159 * Execution limits that can be set via a command-line option or %pragma
160 */
161
H. Peter Anvin79561022018-06-15 17:51:39 -0700162#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700163
H. Peter Anvin79561022018-06-15 17:51:39 -0700164int64_t nasm_limit[LIMIT_MAX+1] =
165{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700166
167struct limit_info {
168 const char *name;
169 const char *help;
170};
171static const struct limit_info limit_info[LIMIT_MAX+1] = {
172 { "passes", "total number of passes" },
173 { "stalled-passes", "number of passes without forward progress" },
174 { "macro-levels", "levels of macro expansion"},
175 { "rep", "%rep count" },
H. Peter Anvin79561022018-06-15 17:51:39 -0700176 { "eval", "expression evaluation descent"},
177 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700178};
179
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700180enum directive_result
181nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700182{
183 int i;
184 int64_t val;
185 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700186 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700187
188 for (i = 0; i <= LIMIT_MAX; i++) {
189 if (!nasm_stricmp(limit, limit_info[i].name))
190 break;
191 }
192 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700193 if (passn == 0)
194 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
195 else
196 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
197 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700198 return DIRR_ERROR;
199 }
200
201 if (!nasm_stricmp(valstr, "unlimited")) {
202 val = LIMIT_MAX_VAL;
203 } else {
204 val = readnum(valstr, &rn_error);
205 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700206 if (passn == 0)
207 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
208 else
209 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
210 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700211 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700212 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700213 if (val > LIMIT_MAX_VAL)
214 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700215 }
216
217 nasm_limit[i] = val;
218 return DIRR_OK;
219}
220
H. Peter Anvin892c4812018-05-30 14:43:46 -0700221int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400222{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700223 location.segment = segment;
224 if (segment == NO_SEG) {
225 location.offset = absolute.offset;
226 in_absolute = true;
227 } else {
228 location.offset = raa_read(offsets, segment);
229 in_absolute = false;
230 }
231 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400232}
233
234static void set_curr_offs(int64_t l_off)
235{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800236 if (in_absolute)
237 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400238 else
239 offsets = raa_write(offsets, location.segment, l_off);
240}
241
H. Peter Anvin892c4812018-05-30 14:43:46 -0700242static void increment_offset(int64_t delta)
243{
244 if (unlikely(delta == 0))
245 return;
246
247 location.offset += delta;
248 set_curr_offs(location.offset);
249}
250
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000251static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000252{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000253 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800255 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000256 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000258}
259
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260static void define_macros_early(void)
261{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700262 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700265 if (oct->have_local) {
266 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400267 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700268 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400269 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700270 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400271 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700272 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400273 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800274 }
275
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700276 if (oct->have_gm) {
277 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400278 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700279 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400280 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700281 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400282 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700283 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400284 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800285 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700286
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700287 if (oct->have_posix) {
288 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400289 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800290 }
291}
292
293static void define_macros_late(void)
294{
295 char temp[128];
296
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400297 /*
298 * In case if output format is defined by alias
299 * we have to put shortname of the alias itself here
300 * otherwise ABI backward compatibility gets broken.
301 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400302 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400303 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400304 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800305}
306
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700307static void emit_dependencies(StrList *list)
308{
309 FILE *deps;
310 int linepos, len;
311 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700312 bool wmake = (quote_for_make == quote_for_wmake);
313 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700314
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700315 wrapstr = wmake ? " &\n " : " \\\n ";
316 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700317
318 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700319 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400320 if (!deps) {
321 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
322 "unable to write dependency file `%s'", depend_file);
323 return;
324 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400326 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700327 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700328
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700329 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400330 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700331 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400332 len = strlen(file);
333 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700334 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400335 linepos = 1;
336 }
337 fprintf(deps, " %s", file);
338 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700339 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700340 }
341 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700342
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400343 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700344 if (depend_emit_phony) {
345 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700346 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700347 nasm_free(file);
348 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400349 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700350 }
351
352 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400353 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700354}
355
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700356/* Convert a struct tm to a POSIX-style time constant */
357static int64_t make_posix_time(const struct tm *tm)
358{
359 int64_t t;
360 int64_t y = tm->tm_year;
361
362 /* See IEEE 1003.1:2004, section 4.14 */
363
364 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
365 t += tm->tm_yday;
366 t *= 24;
367 t += tm->tm_hour;
368 t *= 60;
369 t += tm->tm_min;
370 t *= 60;
371 t += tm->tm_sec;
372
373 return t;
374}
375
376static void timestamp(void)
377{
378 struct compile_time * const oct = &official_compile_time;
379 const struct tm *tp, *best_gm;
380
381 time(&oct->t);
382
383 best_gm = NULL;
384
385 tp = localtime(&oct->t);
386 if (tp) {
387 oct->local = *tp;
388 best_gm = &oct->local;
389 oct->have_local = true;
390 }
391
392 tp = gmtime(&oct->t);
393 if (tp) {
394 oct->gm = *tp;
395 best_gm = &oct->gm;
396 oct->have_gm = true;
397 if (!oct->have_local)
398 oct->local = oct->gm;
399 } else {
400 oct->gm = oct->local;
401 }
402
403 if (best_gm) {
404 oct->posix = make_posix_time(best_gm);
405 oct->have_posix = true;
406 }
407}
408
H. Peter Anvin038d8612007-04-12 16:54:50 +0000409int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000410{
H. Peter Anvina771be82017-08-16 14:53:18 -0700411 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700412
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700413 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800414
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800415 iflag_set_default_cpu(&cpu);
416 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400417
Charles Crayne2581c862008-09-10 19:21:52 -0700418 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700419 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400420 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421
H. Peter Anvin97e15752007-09-25 08:47:47 -0700422 error_file = stderr;
423
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700424 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700425 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700426
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700427 /*
428 * We must call init_labels() before the command line parsing,
429 * because we may be setting prefixes/suffixes from the command
430 * line.
431 */
432 init_labels();
433
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000434 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000435 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000436
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400438 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000439
H. Peter Anvin55568c12016-10-03 19:46:49 -0700440 parse_cmdline(argc, argv, 1);
441 if (terminate_after_phase) {
442 if (want_usage)
443 usage();
444 return 1;
445 }
446
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700447 /*
448 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700449 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700450 */
451 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800452 define_macros_early();
453
H. Peter Anvin55568c12016-10-03 19:46:49 -0700454 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 if (terminate_after_phase) {
456 if (want_usage)
457 usage();
458 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000459 }
460
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800461 /* Save away the default state of warnings */
462 memcpy(warning_state_init, warning_state, sizeof warning_state);
463
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800464 if (!using_debug_info) {
465 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800466 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800467 } else if (!debug_format) {
468 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800469 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800470 } else {
471 dfmt = dfmt_find(ofmt, debug_format);
472 if (!dfmt) {
473 nasm_fatal(ERR_NOFILE | ERR_USAGE,
474 "unrecognized debug format `%s' for"
475 " output format `%s'",
476 debug_format, ofmt->shortname);
477 }
478 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000479
H. Peter Anvin76690a12002-04-30 20:52:49 +0000480 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400481 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000482
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800483 /* no output file name? */
484 if (!outname)
485 outname = filename_set_extension(inname, ofmt->extension);
486
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000487 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800488 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000489
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400490 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700491
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700492 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400493 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700494
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400495 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000496 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700497
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400498 if (depend_missing_ok)
499 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700500
H. Peter Anvin130736c2016-02-17 20:27:41 -0800501 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 while ((line = preproc->getline()))
504 nasm_free(line);
505 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400506 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000507 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700508 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000509 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000511
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800512 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700513 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800515 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 "unable to open output file `%s'",
517 outname);
518 } else
519 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000520
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700521 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000522
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400523 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800524 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800525
526 /* Revert all warnings to the default state */
527 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 while ((line = preproc->getline())) {
530 /*
531 * We generate %line directives if needed for later programs
532 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000533 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 int altline = src_get(&linnum, &file_name);
535 if (altline) {
536 if (altline == 1 && lineinc == 1)
537 nasm_fputs("", ofile);
538 else {
539 lineinc = (altline != -1 || lineinc != 1);
540 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000541 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 file_name);
543 }
544 prior_linnum = linnum;
545 }
546 nasm_fputs(line, ofile);
547 nasm_free(line);
548 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 preproc->cleanup(0);
550 if (ofile)
551 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700552 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400554 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400555 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400557 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700558 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400559 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800560 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400561 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400563 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400564 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400566 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200567
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400568 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800569 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400570 cleanup_labels();
571 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700572 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400573 nasm_error(ERR_NONFATAL|ERR_NOFILE,
574 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700575 terminate_after_phase = true;
576 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400577 }
578
579 if (ofile) {
580 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700581 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400582 remove(outname);
583 ofile = NULL;
584 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000585 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000586
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700587 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400588 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700589
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000590 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000592
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 raa_free(offsets);
594 saa_free(forwrefs);
595 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000596 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700597 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000598
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700599 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000600}
601
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602/*
603 * Get a parameter for a command line option.
604 * First arg must be in the form of e.g. -f...
605 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800606static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800608 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400609 if (p[2]) /* the parameter's in the option */
610 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800612 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400615 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617 return NULL;
618}
619
H. Peter Anvindc242712007-11-18 11:55:10 -0800620/*
621 * Copy a filename
622 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800623static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800624{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800625 if (*dst)
626 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800627
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800628 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800629}
630
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700631/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700632 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700633 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700634static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700635{
636 const char *p;
637 char *os, *q;
638
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400639 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700640 size_t nbs = 0;
641
642 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400643 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700644
645 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400646 switch (*p) {
647 case ' ':
648 case '\t':
649 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
650 n += nbs + 2;
651 nbs = 0;
652 break;
653 case '$':
654 case '#':
655 nbs = 0;
656 n += 2;
657 break;
658 case '\\':
659 nbs++;
660 n++;
661 break;
662 default:
663 nbs = 0;
664 n++;
665 break;
666 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700667 }
668
669 /* Convert N backslashes at the end of filename to 2N backslashes */
670 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400671 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700672
673 os = q = nasm_malloc(n);
674
675 nbs = 0;
676 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400677 switch (*p) {
678 case ' ':
679 case '\t':
680 while (nbs--)
681 *q++ = '\\';
682 *q++ = '\\';
683 *q++ = *p;
684 break;
685 case '$':
686 *q++ = *p;
687 *q++ = *p;
688 nbs = 0;
689 break;
690 case '#':
691 *q++ = '\\';
692 *q++ = *p;
693 nbs = 0;
694 break;
695 case '\\':
696 *q++ = *p;
697 nbs++;
698 break;
699 default:
700 *q++ = *p;
701 nbs = 0;
702 break;
703 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700704 }
705 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400706 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700707
708 *q = '\0';
709
710 return os;
711}
712
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700713/*
714 * Convert a string to a Watcom make-safe form
715 */
716static char *quote_for_wmake(const char *str)
717{
718 const char *p;
719 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700720 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700721
722 size_t n = 1; /* Terminating zero */
723
724 if (!str)
725 return NULL;
726
727 for (p = str; *p; p++) {
728 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700729 case ' ':
730 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700731 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700732 quote = true;
733 n++;
734 break;
735 case '\"':
736 quote = true;
737 n += 2;
738 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700739 case '$':
740 case '#':
741 n += 2;
742 break;
743 default:
744 n++;
745 break;
746 }
747 }
748
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700749 if (quote)
750 n += 2;
751
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700752 os = q = nasm_malloc(n);
753
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700754 if (quote)
755 *q++ = '\"';
756
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700757 for (p = str; *p; p++) {
758 switch (*p) {
759 case '$':
760 case '#':
761 *q++ = '$';
762 *q++ = *p;
763 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 case '\"':
765 *q++ = *p;
766 *q++ = *p;
767 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700768 default:
769 *q++ = *p;
770 break;
771 }
772 }
773
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700774 if (quote)
775 *q++ = '\"';
776
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700777 *q = '\0';
778
779 return os;
780}
781
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100782enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800783 OPT_BOGUS,
784 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700785 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800786 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700787 OPT_MANGLE,
788 OPT_INCLUDE,
789 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700790 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700791 OPT_LIMIT,
792 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100793};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800794struct textargs {
795 const char *label;
796 enum text_options opt;
797 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700798 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800799};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800800static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700801 {"v", OPT_VERSION, false, 0},
802 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700803 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700804 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
805 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
806 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
807 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
808 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
809 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
810 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700811 {"include", OPT_INCLUDE, true, 0},
812 {"pragma", OPT_PRAGMA, true, 0},
813 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700814 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700815 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700816 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000817};
818
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400819static void show_version(void)
820{
821 printf("NASM version %s compiled on %s%s\n",
822 nasm_version, nasm_date, nasm_compile_options);
823 exit(0);
824}
825
H. Peter Anvin423e3812007-11-15 17:12:29 -0800826static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700827static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000828{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000829 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800830 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000831
832 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800833 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000834
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
837 /* These parameters take values */
838 if (!(param = get_param(p, q, &advance)))
839 return advance;
840 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 switch (p[1]) {
843 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700844 if (pass == 1)
845 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700847
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400848 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700849 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800850 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700852
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400853 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700854 if (pass == 1) {
855 ofmt = ofmt_find(param, &ofmt_alias);
856 if (!ofmt) {
857 nasm_fatal(ERR_NOFILE | ERR_USAGE,
858 "unrecognised output format `%s' - "
859 "use -hf for a list", param);
860 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400861 }
862 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700863
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700865 if (pass == 2) {
866 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700867
H. Peter Anvin55568c12016-10-03 19:46:49 -0700868 if (!*param) {
869 /* Naked -O == -Ox */
870 optimizing = MAX_OPTIMIZE;
871 } else {
872 while (*param) {
873 switch (*param) {
874 case '0': case '1': case '2': case '3': case '4':
875 case '5': case '6': case '7': case '8': case '9':
876 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700877
H. Peter Anvin55568c12016-10-03 19:46:49 -0700878 /* -O0 -> optimizing == -1, 0.98 behaviour */
879 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
880 if (opt < 2)
881 optimizing = opt - 1;
882 else
883 optimizing = opt;
884 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700885
H. Peter Anvin55568c12016-10-03 19:46:49 -0700886 case 'v':
887 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 param++;
889 opt_verbose_info = true;
890 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700891
H. Peter Anvin55568c12016-10-03 19:46:49 -0700892 case 'x':
893 param++;
894 optimizing = MAX_OPTIMIZE;
895 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700896
H. Peter Anvin55568c12016-10-03 19:46:49 -0700897 default:
898 nasm_fatal(0,
899 "unknown optimization option -O%c\n",
900 *param);
901 break;
902 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700904 if (optimizing > MAX_OPTIMIZE)
905 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400907 }
908 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800909
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 case 'p': /* pre-include */
911 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700912 if (pass == 2)
913 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400914 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800915
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 case 'd': /* pre-define */
917 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700918 if (pass == 2)
919 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400920 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800921
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400922 case 'u': /* un-define */
923 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700924 if (pass == 2)
925 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400926 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800927
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 case 'i': /* include search path */
929 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700930 if (pass == 2)
931 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800933
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400934 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700935 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800936 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400937 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800938
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800941 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400942 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800943
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400944 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700945 if (pass == 2) {
946 using_debug_info = true;
947 debug_format = param;
948 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400949 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800950
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700952 if (pass == 1) {
953 if (nasm_stricmp("vc", param) == 0)
954 nasm_set_verror(nasm_verror_vc);
955 else if (nasm_stricmp("gnu", param) == 0)
956 nasm_set_verror(nasm_verror_gnu);
957 else
958 nasm_fatal(ERR_NOFILE | ERR_USAGE,
959 "unrecognized error reporting format `%s'",
960 param);
961 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800963
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700965 if (pass == 2) {
966 using_debug_info = true;
967 if (p[2])
968 debug_format = nasm_skip_spaces(p + 2);
969 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800971
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700973 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400974 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800976
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 case 'y':
978 printf("\nvalid debug formats for '%s' output format are"
979 " ('*' denotes default):\n", ofmt->shortname);
980 dfmt_list(ofmt, stdout);
981 exit(0);
982 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 if (pass == 2)
986 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400990 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 case 'e': /* preprocess only */
994 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700995 if (pass == 1)
996 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001000 if (pass == 1)
1001 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001003
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001004 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001006 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001007 if (!set_warning_status(param)) {
1008 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1009 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001010 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001012 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001015 if (pass == 1) {
1016 switch (p[2]) {
1017 case 'W':
1018 quote_for_make = quote_for_wmake;
1019 break;
1020 case 'D':
1021 case 'F':
1022 case 'T':
1023 case 'Q':
1024 advance = true;
1025 break;
1026 default:
1027 break;
1028 }
1029 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001030 switch (p[2]) {
1031 case 0:
1032 operating_mode = OP_DEPEND;
1033 break;
1034 case 'G':
1035 operating_mode = OP_DEPEND;
1036 depend_missing_ok = true;
1037 break;
1038 case 'P':
1039 depend_emit_phony = true;
1040 break;
1041 case 'D':
1042 operating_mode = OP_NORMAL;
1043 depend_file = q;
1044 advance = true;
1045 break;
1046 case 'F':
1047 depend_file = q;
1048 advance = true;
1049 break;
1050 case 'T':
1051 depend_target = q;
1052 advance = true;
1053 break;
1054 case 'Q':
1055 depend_target = quote_for_make(q);
1056 advance = true;
1057 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001058 case 'W':
1059 /* handled in pass 1 */
1060 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001061 default:
1062 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1063 "unknown dependency option `-M%c'", p[2]);
1064 break;
1065 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001066 }
1067 if (advance && (!q || !q[0])) {
1068 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1069 "option `-M%c' requires a parameter", p[2]);
1070 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001071 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case '-':
1075 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001076 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001077 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001078 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001079
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001080 p += 2;
1081
1082 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001083 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 break;
1085 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001086
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001087 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001088 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001089 olen = strlen(tx->label);
1090
1091 if (olen > plen)
1092 continue;
1093
1094 if (nasm_memicmp(p, tx->label, olen))
1095 continue;
1096
1097 if (tx->label[olen-1] == '-')
1098 break; /* Incomplete option */
1099
1100 if (!p[olen] || p[olen] == '=')
1101 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001103
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001104 if (!tx->label) {
1105 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1106 "unrecognized option `--%s'", p);
1107 }
1108
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001109 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001110 if (param)
1111 *param++ = '\0';
1112
H. Peter Anvin3366e312018-02-07 14:14:36 -08001113 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001114 if (!param) {
1115 param = q;
1116 advance = true;
1117 }
1118
1119 /* Note: a null string is a valid parameter */
1120 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001121 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001122 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001123 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 break;
1125 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001126 } else {
1127 if (param) {
1128 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1129 "option `--%s' does not take an argument",
1130 p);
1131
1132 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001133 }
1134
1135 switch (tx->opt) {
1136 case OPT_VERSION:
1137 show_version();
1138 break;
1139 case OPT_ABORT_ON_PANIC:
1140 abort_on_panic = true;
1141 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001142 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001143 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001144 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001145 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001146 case OPT_INCLUDE:
1147 if (pass == 2)
1148 preproc->pre_include(q);
1149 break;
1150 case OPT_PRAGMA:
1151 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001152 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001153 break;
1154 case OPT_BEFORE:
1155 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001156 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001157 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 case OPT_LIMIT:
1159 if (pass == 2)
1160 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001161 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001162 case OPT_KEEP_ALL:
1163 keep_all = true;
1164 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 case OPT_HELP:
1166 help(0);
1167 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001168 default:
1169 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001171
1172 if (eqsave)
1173 *eqsave = '='; /* Restore = argument separator */
1174
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 break;
1176 }
1177
1178 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001179 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1180 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 break;
1182 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001183 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001184 /* In theory we could allow multiple input files... */
1185 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001186 }
1187
1188 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001189}
1190
H. Peter Anvineba20a72002-04-30 20:53:55 +00001191#define ARG_BUF_DELTA 128
1192
H. Peter Anvin55568c12016-10-03 19:46:49 -07001193static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001194{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001195 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196 int bufsize, prevargsize;
1197
1198 bufsize = prevargsize = ARG_BUF_DELTA;
1199 buffer = nasm_malloc(ARG_BUF_DELTA);
1200 prevarg = nasm_malloc(ARG_BUF_DELTA);
1201 prevarg[0] = '\0';
1202
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 p = buffer;
1205 while (1) { /* Loop to handle long lines */
1206 q = fgets(p, bufsize - (p - buffer), rfile);
1207 if (!q)
1208 break;
1209 p += strlen(p);
1210 if (p > buffer && p[-1] == '\n')
1211 break;
1212 if (p - buffer > bufsize - 10) {
1213 int offset;
1214 offset = p - buffer;
1215 bufsize += ARG_BUF_DELTA;
1216 buffer = nasm_realloc(buffer, bufsize);
1217 p = buffer + offset;
1218 }
1219 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 if (!q && p == buffer) {
1222 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001223 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 nasm_free(buffer);
1225 nasm_free(prevarg);
1226 return;
1227 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001228
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 /*
1230 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1231 * them are present at the end of the line.
1232 */
1233 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001234
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001235 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001237
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001238 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001239
H. Peter Anvin55568c12016-10-03 19:46:49 -07001240 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001242
Charles Crayne192d5b52007-10-18 19:02:42 -07001243 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 prevargsize += ARG_BUF_DELTA;
1245 prevarg = nasm_realloc(prevarg, prevargsize);
1246 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001247 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001248 }
1249}
1250
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001251/* Function to process args from a string of args, rather than the
1252 * argv array. Used by the environment variable and response file
1253 * processing.
1254 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001255static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001257 char *p, *q, *arg, *prevarg;
1258 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001259
1260 p = args;
1261 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001263 arg = NULL;
1264 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 q = p;
1266 while (*p && *p != separator)
1267 p++;
1268 while (*p == separator)
1269 *p++ = '\0';
1270 prevarg = arg;
1271 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001272 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001274 }
1275 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001276 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001277}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001278
H. Peter Anvin55568c12016-10-03 19:46:49 -07001279static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001280{
1281 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001282 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001283 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001284 perror(file);
1285 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001286 }
1287 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001288 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001289 }
1290 fclose(f);
1291}
1292
H. Peter Anvin55568c12016-10-03 19:46:49 -07001293static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294{
1295 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001296 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001297 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001298
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001299 /* Initialize all the warnings to their default state */
1300 for (i = 0; i < ERR_WARN_ALL; i++) {
1301 warning_state_init[i] = warning_state[i] =
1302 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1303 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001304
1305 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001306 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001307 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001308 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001309 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001311 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001313 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001314
1315 /*
1316 * Now process the actual command line.
1317 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001319 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 argv++;
1321 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001322 /*
1323 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 * arguments like the environment variable. This allows us
1325 * to have multiple arguments on a single line, which is
1326 * different to the -@resp file processing below for regular
1327 * NASM.
1328 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001329 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 argc--;
1331 argv++;
1332 }
1333 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001334 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001335 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001336 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001337 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001338 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 fclose(rfile);
1340 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001341 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 "unable to open response file `%s'", p);
1343 }
1344 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001345 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001346 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001347 }
1348
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001349 /*
1350 * Look for basic command line typos. This definitely doesn't
1351 * catch all errors, but it might help cases of fumbled fingers.
1352 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001353 if (pass != 2)
1354 return;
1355
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001356 if (!inname)
1357 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001358
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001359 else if ((errname && !strcmp(inname, errname)) ||
1360 (outname && !strcmp(inname, outname)) ||
1361 (listname && !strcmp(inname, listname)) ||
1362 (depend_file && !strcmp(inname, depend_file)))
1363 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1364
1365 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001366 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001367 if (!error_file) {
1368 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001369 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001370 "cannot open file `%s' for error messages",
1371 errname);
1372 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001373 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001374}
1375
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001376static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001378 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001380 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001381 uint64_t prev_offset_changed;
H. Peter Anvin79561022018-06-15 17:51:39 -07001382 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001383
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001384 switch (cmd_sb) {
1385 case 16:
1386 break;
1387 case 32:
1388 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1389 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1390 break;
1391 case 64:
1392 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1393 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1394 break;
1395 default:
1396 panic();
1397 break;
1398 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001400 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001401 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001402 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001403 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1404 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001405
H. Peter Anvincac0b192017-03-28 16:12:30 -07001406 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 cpu = cmd_cpu;
1408 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001409 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001410 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001411 /* Remove the list file in case we die before the output pass */
1412 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001414 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001415 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001416 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 saa_rewind(forwrefs);
1418 forwref = saa_rstruct(forwrefs);
1419 raa_free(offsets);
1420 offsets = raa_init();
1421 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001422 location.segment = NO_SEG;
1423 location.offset = 0;
1424 if (passn == 1)
1425 location.known = true;
1426 ofmt->reset();
1427 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001428 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001429
1430 /* Revert all warnings to the default state */
1431 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001432
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001434
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 while ((line = preproc->getline())) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001436 if (++globallineno > nasm_limit[LIMIT_LINES])
1437 nasm_fatal(0,
1438 "overall line count exceeds the maximum %"PRId64"\n",
1439 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001440
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001441 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001442 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001443 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001444 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001445 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001446 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001447
H. Peter Anvinc7131682017-03-07 17:45:01 -08001448 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001449 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001450
1451 if (optimizing > 0) {
1452 if (forwref != NULL && globallineno == forwref->lineno) {
1453 output_ins.forw_ref = true;
1454 do {
1455 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1456 forwref = saa_rstruct(forwrefs);
1457 } while (forwref != NULL
1458 && forwref->lineno == globallineno);
1459 } else
1460 output_ins.forw_ref = false;
1461
1462 if (output_ins.forw_ref) {
1463 if (passn == 1) {
1464 for (i = 0; i < output_ins.operands; i++) {
1465 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1466 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1467 fwinf->lineno = globallineno;
1468 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001469 }
1470 }
1471 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001472 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001473 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001474
H. Peter Anvinc7131682017-03-07 17:45:01 -08001475 /* forw_ref */
1476 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001477 if (!output_ins.label)
1478 nasm_error(ERR_NONFATAL,
1479 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001480
H. Peter Anvin98578072018-06-01 18:02:54 -07001481 if (output_ins.operands == 1 &&
1482 (output_ins.oprs[0].type & IMMEDIATE) &&
1483 output_ins.oprs[0].wrt == NO_SEG) {
1484 define_label(output_ins.label,
1485 output_ins.oprs[0].segment,
1486 output_ins.oprs[0].offset, false);
1487 } else if (output_ins.operands == 2
1488 && (output_ins.oprs[0].type & IMMEDIATE)
1489 && (output_ins.oprs[0].type & COLON)
1490 && output_ins.oprs[0].segment == NO_SEG
1491 && output_ins.oprs[0].wrt == NO_SEG
1492 && (output_ins.oprs[1].type & IMMEDIATE)
1493 && output_ins.oprs[1].segment == NO_SEG
1494 && output_ins.oprs[1].wrt == NO_SEG) {
1495 define_label(output_ins.label,
1496 output_ins.oprs[0].offset | SEG_ABS,
1497 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001498 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001499 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001500 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001501 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001502 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001504 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001505
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001506 for (n = 1; n <= output_ins.times; n++) {
1507 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001508 int64_t l = insn_size(location.segment,
1509 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001510 globalbits, &output_ins);
1511
1512 /* if (using_debug_info) && output_ins.opcode != -1) */
1513 if (using_debug_info)
1514 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001515 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001516 int32_t typeinfo =
1517 TYS_ELEMENTS(output_ins.operands);
1518 switch (output_ins.opcode) {
1519 case I_RESB:
1520 typeinfo =
1521 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1522 break;
1523 case I_RESW:
1524 typeinfo =
1525 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1526 break;
1527 case I_RESD:
1528 typeinfo =
1529 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1530 break;
1531 case I_RESQ:
1532 typeinfo =
1533 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1534 break;
1535 case I_REST:
1536 typeinfo =
1537 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1538 break;
1539 case I_RESO:
1540 typeinfo =
1541 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1542 break;
1543 case I_RESY:
1544 typeinfo =
1545 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1546 break;
1547 case I_RESZ:
1548 typeinfo =
1549 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1550 break;
1551 case I_DB:
1552 typeinfo |= TY_BYTE;
1553 break;
1554 case I_DW:
1555 typeinfo |= TY_WORD;
1556 break;
1557 case I_DD:
1558 if (output_ins.eops_float)
1559 typeinfo |= TY_FLOAT;
1560 else
1561 typeinfo |= TY_DWORD;
1562 break;
1563 case I_DQ:
1564 typeinfo |= TY_QWORD;
1565 break;
1566 case I_DT:
1567 typeinfo |= TY_TBYTE;
1568 break;
1569 case I_DO:
1570 typeinfo |= TY_OWORD;
1571 break;
1572 case I_DY:
1573 typeinfo |= TY_YWORD;
1574 break;
1575 case I_DZ:
1576 typeinfo |= TY_ZWORD;
1577 break;
1578 default:
1579 typeinfo = TY_LABEL;
1580 break;
1581 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001582
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001583 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001584 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001585
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001586 /*
1587 * For INCBIN, let the code in assemble
1588 * handle TIMES, so we don't have to read the
1589 * input file over and over.
1590 */
1591 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001592 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001593 }
1594 /*
1595 * else l == -1 => invalid instruction, which will be
1596 * flagged as an error on pass 2
1597 */
1598 } else {
1599 if (n == 2)
1600 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001601 increment_offset(assemble(location.segment,
1602 location.offset,
1603 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001605 } /* not an EQU */
1606 }
1607 if (output_ins.times > 1)
1608 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001609
H. Peter Anvinc7131682017-03-07 17:45:01 -08001610 cleanup_insn(&output_ins);
1611
1612 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001615
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001616 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001617 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001618 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 if (pass1 == 1)
1621 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001623 /*
1624 * Always run at least two optimization passes (pass0 == 0);
1625 * things like subsections will fail miserably without that.
1626 * Once we commit to a stabilization pass (pass0 == 1), we can't
1627 * go back, and if something goes bad, we can only hope
1628 * that we don't end up with a phase error at the end.
1629 */
1630 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001632 } else if (global_offset_changed &&
1633 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001634 prev_offset_changed = global_offset_changed;
1635 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001636 } else {
1637 stall_count++;
1638 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001639
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001640 if (terminate_after_phase)
1641 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001642
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001643 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1644 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001645 /* We get here if the labels don't converge
1646 * Example: FOO equ FOO + 1
1647 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001648 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001649 "Can't find valid values for all labels "
H. Peter Anvin79561022018-06-15 17:51:39 -07001650 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001651 nasm_error(ERR_NONFATAL,
1652 "Possible causes: recursive EQUs, macro abuse.");
1653 break;
1654 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001655 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001658 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001659 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001660 /* -On and -Ov switches */
H. Peter Anvin79561022018-06-15 17:51:39 -07001661 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1662 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001663 }
1664}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001665
Ed Berosetfa771012002-06-09 20:56:40 +00001666/**
1667 * gnu style error reporting
1668 * This function prints an error message to error_file in the
1669 * style used by GNU. An example would be:
1670 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001671 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001672 * which the error occurs (or is detected) and "error:" is one of
1673 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001674 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001675 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001676 *
1677 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001678 * @param fmt the printf style format string
1679 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001680static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001681{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001682 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001683 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001684
Ed Berosetfa771012002-06-09 20:56:40 +00001685 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001687
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001688 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001689 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001690 if (!currentfile || (severity & ERR_TOPFILE)) {
1691 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1692 lineno = 0;
1693 }
1694 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001695
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001696 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001697 if (!lineno)
1698 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001699 else
1700 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001701 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001702
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001703 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001704}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001705
Ed Berosetfa771012002-06-09 20:56:40 +00001706/**
1707 * MS style error reporting
1708 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001709 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001710 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001711 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001712 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001713 * which the error occurs (or is detected) and "error:" is one of
1714 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001715 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001716 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001717 *
1718 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001719 * @param fmt the printf style format string
1720 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001721static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001722{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001723 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001724 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001725
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 if (is_suppressed_warning(severity))
1727 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001728
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001729 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001731
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001732 if (!skip_this_pass(severity)) {
1733 if (currentfile) {
1734 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001735 } else {
1736 fputs("nasm: ", error_file);
1737 }
Ed Berosetfa771012002-06-09 20:56:40 +00001738 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001739
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001740 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001741}
1742
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001743/*
1744 * check to see if this is a suppressable warning
1745 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001746static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001747{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001748 /* Not a warning at all */
1749 if ((severity & ERR_MASK) != ERR_WARNING)
1750 return false;
1751
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001752 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001753}
1754
Ed Berosetfa771012002-06-09 20:56:40 +00001755/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001756 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001757 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001758 * not in pass 1
1759 *
1760 * @param severity the severity of the warning or error
1761 * @return true if we should abort error/warning printing
1762 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001763static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001764{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001765 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001766 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001767 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1768 else
1769 return false;
1770}
1771
1772static bool warning_is_error(int severity)
1773{
1774 if (is_valid_warning(severity))
1775 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001776 else
1777 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001778}
1779
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001780static bool skip_this_pass(int severity)
1781{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001782 /*
1783 * See if it's a pass-specific error or warning which should be skipped.
1784 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1785 * they cannot be resumed from.
1786 */
1787 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001788 return false;
1789
H. Peter Anvin934f0472016-05-09 12:00:19 -07001790 /*
1791 * passn is 1 on the very first pass only.
1792 * pass0 is 2 on the code-generation (final) pass only.
1793 * These are the passes we care about in this case.
1794 */
1795 return (((severity & ERR_PASS1) && passn != 1) ||
1796 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001797}
1798
Ed Berosetfa771012002-06-09 20:56:40 +00001799/**
1800 * common error reporting
1801 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001802 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001803 * specific error message to error_file and may or may not return. It
1804 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001805 *
1806 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001807 * @param fmt the printf style format string
1808 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001809static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001810{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001811 char msg[1024];
1812 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001813
H. Peter Anvin7df04172008-06-10 18:27:38 -07001814 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001816 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 break;
1818 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001819 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
1821 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001822 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
1824 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001825 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 break;
1827 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001828 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001830 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001831 pfx = "";
1832 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001833 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001834
H. Peter Anvin934f0472016-05-09 12:00:19 -07001835 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001836 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001837 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001838 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1839 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001840
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001841 if (!skip_this_pass(severity))
1842 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001843
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001844 /* Are we recursing from error_list_macros? */
1845 if (severity & ERR_PP_LISTMACRO)
1846 return;
1847
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001848 /*
1849 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001850 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001851 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001852 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001853
H. Peter Anvin8f622462017-04-02 19:02:29 -07001854 if (skip_this_pass(severity))
1855 return;
1856
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001857 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001858 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001859
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001860 preproc->error_list_macros(severity);
1861
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001862 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 case ERR_DEBUG:
1864 /* no further action, by definition */
1865 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001866 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001867 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001868 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001869 terminate_after_phase = true;
1870 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001872 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 break;
1874 case ERR_FATAL:
1875 if (ofile) {
1876 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001877 if (!keep_all)
1878 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001879 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 }
1881 if (want_usage)
1882 usage();
1883 exit(1); /* instantly die */
1884 break; /* placate silly compilers */
1885 case ERR_PANIC:
1886 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001887
1888 if (abort_on_panic)
1889 abort(); /* halt, catch fire, dump core/stop debugger */
1890
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001891 if (ofile) {
1892 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001893 if (!keep_all)
1894 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001895 ofile = NULL;
1896 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 exit(3);
1898 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001899 }
1900}
1901
H. Peter Anvin734b1882002-04-30 21:01:08 +00001902static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001903{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001904 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001905}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001906
1907static void help(const char xopt)
1908{
1909 int i;
1910
1911 printf
1912 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1913 "[-l listfile]\n"
1914 " [options...] [--] filename\n"
1915 " or nasm -v (or --v) for version info\n\n"
1916 "\n"
1917 "Response files should contain command line parameters,\n"
1918 "one per line.\n"
1919 "\n"
1920 " -t assemble in SciTech TASM compatible mode\n");
1921 printf
1922 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1923 " -a don't preprocess (assemble only)\n"
1924 " -M generate Makefile dependencies on stdout\n"
1925 " -MG d:o, missing files assumed generated\n"
1926 " -MF file set Makefile dependency file\n"
1927 " -MD file assemble and generate dependencies\n"
1928 " -MT file dependency target name\n"
1929 " -MQ file dependency target name (quoted)\n"
1930 " -MP emit phony target\n\n"
1931 " -Zfile redirect error messages to file\n"
1932 " -s redirect error messages to stdout\n\n"
1933 " -g generate debugging information\n\n"
1934 " -F format select a debugging format\n\n"
1935 " -gformat same as -g -F format\n\n"
1936 " -o outfile write output to an outfile\n\n"
1937 " -f format select an output format\n\n"
1938 " -l listfile write listing to a listfile\n\n"
1939 " -Ipath add a pathname to the include file path\n");
1940 printf
1941 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1942 " -O0 no optimization\n"
1943 " -O1 minimal optimization\n"
1944 " -Ox multipass optimization (default)\n"
1945 " -Pfile pre-include a file (also --include)\n"
1946 " -Dmacro[=str] pre-define a macro\n"
1947 " -Umacro undefine a macro\n"
1948 " -Xformat specifiy error reporting format (gnu or vc)\n"
1949 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1950 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1951 " -w[+-]error[=foo]\n"
1952 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001953 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001954 " --pragma str pre-executes a specific %%pragma\n"
1955 " --before str add line (usually a preprocessor statement) before the input\n"
1956 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001957 " to all extern, common and global symbols (also --gprefix)\n"
1958 " --postfix str append the given string to all the given string\n"
1959 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001960 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001961 " --lpostfix str append the given string to all other symbols\n"
1962 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001963 " --limit-X val set execution limit X\n");
1964
1965 for (i = 0; i <= LIMIT_MAX; i++) {
1966 printf(" %-15s %s (default ",
1967 limit_info[i].name, limit_info[i].help);
1968 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001969 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001970 } else {
1971 printf("unlimited)\n");
1972 }
1973 }
1974
1975 printf("\nWarnings for the -W/-w options:\n");
1976
1977 for (i = 0; i <= ERR_WARN_ALL; i++)
1978 printf(" %-23s %s%s\n",
1979 warnings[i].name, warnings[i].help,
1980 i == ERR_WARN_ALL ? "\n" :
1981 warnings[i].enabled ? " (default on)" :
1982 " (default off)");
1983
1984 if (xopt == 'f') {
1985 printf("valid output formats for -f are"
1986 " (`*' denotes default):\n");
1987 ofmt_list(ofmt, stdout);
1988 } else {
1989 printf("For a list of valid output formats, use -hf.\n");
1990 printf("For a list of debug formats, use -f <format> -y.\n");
1991 }
1992}