blob: b647556b50dd02947967608f2c763a28ee4e1c92 [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;
94
H. Peter Anvin6867acc2007-10-10 14:58:45 -070095bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080096int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080097static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000098int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080099int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000100
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700101struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800102
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800103const char *inname;
104const char *outname;
105static const char *listname;
106static const char *errname;
107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static int globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700109#define GLOBALLINENO_MAX INT32_MAX
110
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000111/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800112const struct ofmt *ofmt = &OF_DEFAULT;
113const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400114const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000117
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400118FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700119int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100120static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400121
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800122iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123static iflag_t cmd_cpu;
124
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800125struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800126bool in_absolute; /* Flag we are in ABSOLUTE seg */
127struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000128
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000130
H. Peter Anvine2c80182005-01-15 22:15:51 +0000131static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700132static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133
H. Peter Anvine7469712016-02-18 02:20:59 -0800134static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400135
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400136#define OP_NORMAL (1u << 0)
137#define OP_PREPROCESS (1u << 1)
138#define OP_DEPEND (1u << 2)
139
140static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400141
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700142/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700143static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144static bool depend_missing_ok = false;
145static const char *depend_target = NULL;
146static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700147StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000148
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700149static bool want_usage;
150static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800151bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000152
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700153static char *quote_for_pmake(const char *str);
154static char *quote_for_wmake(const char *str);
155static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700156
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700157/*
158 * Execution limits that can be set via a command-line option or %pragma
159 */
160
161#define LIMIT_MAX_VAL (INT_MAX >> 1) /* Effectively unlimited */
162
163int nasm_limit[LIMIT_MAX+1] =
164{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000 };
165
166struct limit_info {
167 const char *name;
168 const char *help;
169};
170static const struct limit_info limit_info[LIMIT_MAX+1] = {
171 { "passes", "total number of passes" },
172 { "stalled-passes", "number of passes without forward progress" },
173 { "macro-levels", "levels of macro expansion"},
174 { "rep", "%rep count" },
175 { "eval", "expression evaluation descent"}
176};
177
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700178enum directive_result
179nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180{
181 int i;
182 int64_t val;
183 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700184 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700185
186 for (i = 0; i <= LIMIT_MAX; i++) {
187 if (!nasm_stricmp(limit, limit_info[i].name))
188 break;
189 }
190 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700191 if (passn == 0)
192 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
193 else
194 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
195 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700196 return DIRR_ERROR;
197 }
198
199 if (!nasm_stricmp(valstr, "unlimited")) {
200 val = LIMIT_MAX_VAL;
201 } else {
202 val = readnum(valstr, &rn_error);
203 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700204 if (passn == 0)
205 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
206 else
207 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
208 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700209 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700210 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700211 if (val > LIMIT_MAX_VAL)
212 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 }
214
215 nasm_limit[i] = val;
216 return DIRR_OK;
217}
218
H. Peter Anvin892c4812018-05-30 14:43:46 -0700219int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400220{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700221 location.segment = segment;
222 if (segment == NO_SEG) {
223 location.offset = absolute.offset;
224 in_absolute = true;
225 } else {
226 location.offset = raa_read(offsets, segment);
227 in_absolute = false;
228 }
229 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400230}
231
232static void set_curr_offs(int64_t l_off)
233{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800234 if (in_absolute)
235 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400236 else
237 offsets = raa_write(offsets, location.segment, l_off);
238}
239
H. Peter Anvin892c4812018-05-30 14:43:46 -0700240static void increment_offset(int64_t delta)
241{
242 if (unlikely(delta == 0))
243 return;
244
245 location.offset += delta;
246 set_curr_offs(location.offset);
247}
248
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000249static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000250{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000251 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800253 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000256}
257
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258static void define_macros_early(void)
259{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700260 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700263 if (oct->have_local) {
264 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400265 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266 strftime(temp, sizeof temp, "__DATE_NUM__=%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, "__TIME__=\"%H:%M:%S\"", &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_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400271 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800272 }
273
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700274 if (oct->have_gm) {
275 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400276 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%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_TIME__=\"%H:%M:%S\"", &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_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400282 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800283 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700284
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700285 if (oct->have_posix) {
286 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400287 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800288 }
289}
290
291static void define_macros_late(void)
292{
293 char temp[128];
294
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400295 /*
296 * In case if output format is defined by alias
297 * we have to put shortname of the alias itself here
298 * otherwise ABI backward compatibility gets broken.
299 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400300 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400301 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400302 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800303}
304
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700305static void emit_dependencies(StrList *list)
306{
307 FILE *deps;
308 int linepos, len;
309 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700310 bool wmake = (quote_for_make == quote_for_wmake);
311 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700312
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700313 wrapstr = wmake ? " &\n " : " \\\n ";
314 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700315
316 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700317 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 if (!deps) {
319 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
320 "unable to write dependency file `%s'", depend_file);
321 return;
322 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700323 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700326
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700327 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400328 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700329 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400330 len = strlen(file);
331 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700332 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400333 linepos = 1;
334 }
335 fprintf(deps, " %s", file);
336 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700337 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700338 }
339 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700340
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400341 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700342 if (depend_emit_phony) {
343 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700344 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700345 nasm_free(file);
346 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400347 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700348 }
349
350 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400351 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700352}
353
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700354/* Convert a struct tm to a POSIX-style time constant */
355static int64_t make_posix_time(const struct tm *tm)
356{
357 int64_t t;
358 int64_t y = tm->tm_year;
359
360 /* See IEEE 1003.1:2004, section 4.14 */
361
362 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
363 t += tm->tm_yday;
364 t *= 24;
365 t += tm->tm_hour;
366 t *= 60;
367 t += tm->tm_min;
368 t *= 60;
369 t += tm->tm_sec;
370
371 return t;
372}
373
374static void timestamp(void)
375{
376 struct compile_time * const oct = &official_compile_time;
377 const struct tm *tp, *best_gm;
378
379 time(&oct->t);
380
381 best_gm = NULL;
382
383 tp = localtime(&oct->t);
384 if (tp) {
385 oct->local = *tp;
386 best_gm = &oct->local;
387 oct->have_local = true;
388 }
389
390 tp = gmtime(&oct->t);
391 if (tp) {
392 oct->gm = *tp;
393 best_gm = &oct->gm;
394 oct->have_gm = true;
395 if (!oct->have_local)
396 oct->local = oct->gm;
397 } else {
398 oct->gm = oct->local;
399 }
400
401 if (best_gm) {
402 oct->posix = make_posix_time(best_gm);
403 oct->have_posix = true;
404 }
405}
406
H. Peter Anvin038d8612007-04-12 16:54:50 +0000407int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408{
H. Peter Anvina771be82017-08-16 14:53:18 -0700409 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700410
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700411 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800412
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800413 iflag_set_default_cpu(&cpu);
414 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400415
Charles Crayne2581c862008-09-10 19:21:52 -0700416 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700417 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400418 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419
H. Peter Anvin97e15752007-09-25 08:47:47 -0700420 error_file = stderr;
421
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700422 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700423 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700424
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000426 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000427
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400429 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000430
H. Peter Anvin55568c12016-10-03 19:46:49 -0700431 parse_cmdline(argc, argv, 1);
432 if (terminate_after_phase) {
433 if (want_usage)
434 usage();
435 return 1;
436 }
437
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700438 /*
439 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700440 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700441 */
442 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800443 define_macros_early();
444
H. Peter Anvin55568c12016-10-03 19:46:49 -0700445 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 if (terminate_after_phase) {
447 if (want_usage)
448 usage();
449 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000450 }
451
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800452 /* Save away the default state of warnings */
453 memcpy(warning_state_init, warning_state, sizeof warning_state);
454
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800455 if (!using_debug_info) {
456 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800457 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800458 } else if (!debug_format) {
459 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800460 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800461 } else {
462 dfmt = dfmt_find(ofmt, debug_format);
463 if (!dfmt) {
464 nasm_fatal(ERR_NOFILE | ERR_USAGE,
465 "unrecognized debug format `%s' for"
466 " output format `%s'",
467 debug_format, ofmt->shortname);
468 }
469 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000470
H. Peter Anvin76690a12002-04-30 20:52:49 +0000471 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400472 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000473
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800474 /* no output file name? */
475 if (!outname)
476 outname = filename_set_extension(inname, ofmt->extension);
477
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000478 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800479 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000480
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700482
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700483 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400484 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700485
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400486 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000487 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700488
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400489 if (depend_missing_ok)
490 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700491
H. Peter Anvin130736c2016-02-17 20:27:41 -0800492 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 while ((line = preproc->getline()))
495 nasm_free(line);
496 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400497 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000498 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700499 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000500 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000502
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800503 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700504 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800506 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 "unable to open output file `%s'",
508 outname);
509 } else
510 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000511
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700512 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000513
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400514 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800515 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800516
517 /* Revert all warnings to the default state */
518 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700519
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 while ((line = preproc->getline())) {
521 /*
522 * We generate %line directives if needed for later programs
523 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000524 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 int altline = src_get(&linnum, &file_name);
526 if (altline) {
527 if (altline == 1 && lineinc == 1)
528 nasm_fputs("", ofile);
529 else {
530 lineinc = (altline != -1 || lineinc != 1);
531 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000532 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 file_name);
534 }
535 prior_linnum = linnum;
536 }
537 nasm_fputs(line, ofile);
538 nasm_free(line);
539 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 preproc->cleanup(0);
541 if (ofile)
542 fclose(ofile);
543 if (ofile && terminate_after_phase)
544 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400545 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400546 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400548 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700549 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400550 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800551 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400552 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400554 /*
555 * We must call init_labels() before ofmt->init() since
556 * some object formats will want to define labels in their
557 * init routines. (eg OS/2 defines the FLAT group)
558 */
559 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400561 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400562 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400564 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200565
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400566 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800567 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400568 cleanup_labels();
569 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700570 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400571 nasm_error(ERR_NONFATAL|ERR_NOFILE,
572 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700573 terminate_after_phase = true;
574 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400575 }
576
577 if (ofile) {
578 fclose(ofile);
579 if (terminate_after_phase)
580 remove(outname);
581 ofile = NULL;
582 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000583 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000584
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700585 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400586 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700587
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000588 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000590
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 raa_free(offsets);
592 saa_free(forwrefs);
593 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000594 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700595 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700597 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598}
599
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600/*
601 * Get a parameter for a command line option.
602 * First arg must be in the form of e.g. -f...
603 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800604static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800606 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400607 if (p[2]) /* the parameter's in the option */
608 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800610 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400613 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615 return NULL;
616}
617
H. Peter Anvindc242712007-11-18 11:55:10 -0800618/*
619 * Copy a filename
620 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800621static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800622{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800623 if (*dst)
624 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800625
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800626 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800627}
628
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700629/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700630 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700631 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700632static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700633{
634 const char *p;
635 char *os, *q;
636
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400637 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700638 size_t nbs = 0;
639
640 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400641 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700642
643 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400644 switch (*p) {
645 case ' ':
646 case '\t':
647 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
648 n += nbs + 2;
649 nbs = 0;
650 break;
651 case '$':
652 case '#':
653 nbs = 0;
654 n += 2;
655 break;
656 case '\\':
657 nbs++;
658 n++;
659 break;
660 default:
661 nbs = 0;
662 n++;
663 break;
664 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700665 }
666
667 /* Convert N backslashes at the end of filename to 2N backslashes */
668 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400669 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700670
671 os = q = nasm_malloc(n);
672
673 nbs = 0;
674 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400675 switch (*p) {
676 case ' ':
677 case '\t':
678 while (nbs--)
679 *q++ = '\\';
680 *q++ = '\\';
681 *q++ = *p;
682 break;
683 case '$':
684 *q++ = *p;
685 *q++ = *p;
686 nbs = 0;
687 break;
688 case '#':
689 *q++ = '\\';
690 *q++ = *p;
691 nbs = 0;
692 break;
693 case '\\':
694 *q++ = *p;
695 nbs++;
696 break;
697 default:
698 *q++ = *p;
699 nbs = 0;
700 break;
701 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700702 }
703 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400704 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700705
706 *q = '\0';
707
708 return os;
709}
710
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700711/*
712 * Convert a string to a Watcom make-safe form
713 */
714static char *quote_for_wmake(const char *str)
715{
716 const char *p;
717 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700718 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700719
720 size_t n = 1; /* Terminating zero */
721
722 if (!str)
723 return NULL;
724
725 for (p = str; *p; p++) {
726 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700727 case ' ':
728 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700729 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700730 quote = true;
731 n++;
732 break;
733 case '\"':
734 quote = true;
735 n += 2;
736 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700737 case '$':
738 case '#':
739 n += 2;
740 break;
741 default:
742 n++;
743 break;
744 }
745 }
746
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700747 if (quote)
748 n += 2;
749
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700750 os = q = nasm_malloc(n);
751
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700752 if (quote)
753 *q++ = '\"';
754
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700755 for (p = str; *p; p++) {
756 switch (*p) {
757 case '$':
758 case '#':
759 *q++ = '$';
760 *q++ = *p;
761 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700762 case '\"':
763 *q++ = *p;
764 *q++ = *p;
765 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700766 default:
767 *q++ = *p;
768 break;
769 }
770 }
771
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700772 if (quote)
773 *q++ = '\"';
774
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700775 *q = '\0';
776
777 return os;
778}
779
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100780enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800781 OPT_BOGUS,
782 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700783 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800784 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700785 OPT_MANGLE,
786 OPT_INCLUDE,
787 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700788 OPT_BEFORE,
789 OPT_LIMIT
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100790};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800791struct textargs {
792 const char *label;
793 enum text_options opt;
794 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700795 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800796};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800797static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700798 {"v", OPT_VERSION, false, 0},
799 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700800 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700801 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
802 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
803 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
804 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
805 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
806 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
807 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700808 {"include", OPT_INCLUDE, true, 0},
809 {"pragma", OPT_PRAGMA, true, 0},
810 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700811 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700812 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000813};
814
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400815static void show_version(void)
816{
817 printf("NASM version %s compiled on %s%s\n",
818 nasm_version, nasm_date, nasm_compile_options);
819 exit(0);
820}
821
H. Peter Anvin423e3812007-11-15 17:12:29 -0800822static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700823static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000824{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000825 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800826 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000827
828 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800829 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400832 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
833 /* These parameters take values */
834 if (!(param = get_param(p, q, &advance)))
835 return advance;
836 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 switch (p[1]) {
839 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700840 if (pass == 1)
841 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700843
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400844 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700845 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800846 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700848
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400849 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700850 if (pass == 1) {
851 ofmt = ofmt_find(param, &ofmt_alias);
852 if (!ofmt) {
853 nasm_fatal(ERR_NOFILE | ERR_USAGE,
854 "unrecognised output format `%s' - "
855 "use -hf for a list", param);
856 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 }
858 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700859
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700861 if (pass == 2) {
862 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700863
H. Peter Anvin55568c12016-10-03 19:46:49 -0700864 if (!*param) {
865 /* Naked -O == -Ox */
866 optimizing = MAX_OPTIMIZE;
867 } else {
868 while (*param) {
869 switch (*param) {
870 case '0': case '1': case '2': case '3': case '4':
871 case '5': case '6': case '7': case '8': case '9':
872 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700873
H. Peter Anvin55568c12016-10-03 19:46:49 -0700874 /* -O0 -> optimizing == -1, 0.98 behaviour */
875 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
876 if (opt < 2)
877 optimizing = opt - 1;
878 else
879 optimizing = opt;
880 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700881
H. Peter Anvin55568c12016-10-03 19:46:49 -0700882 case 'v':
883 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400884 param++;
885 opt_verbose_info = true;
886 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700887
H. Peter Anvin55568c12016-10-03 19:46:49 -0700888 case 'x':
889 param++;
890 optimizing = MAX_OPTIMIZE;
891 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700892
H. Peter Anvin55568c12016-10-03 19:46:49 -0700893 default:
894 nasm_fatal(0,
895 "unknown optimization option -O%c\n",
896 *param);
897 break;
898 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400899 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700900 if (optimizing > MAX_OPTIMIZE)
901 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 }
904 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800905
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 case 'p': /* pre-include */
907 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700908 if (pass == 2)
909 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800911
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400912 case 'd': /* pre-define */
913 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700914 if (pass == 2)
915 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800917
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400918 case 'u': /* un-define */
919 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700920 if (pass == 2)
921 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400922 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800923
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400924 case 'i': /* include search path */
925 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 if (pass == 2)
927 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800929
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800932 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800934
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800937 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800939
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400940 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 if (pass == 2) {
942 using_debug_info = true;
943 debug_format = param;
944 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800946
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400947 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700948 if (pass == 1) {
949 if (nasm_stricmp("vc", param) == 0)
950 nasm_set_verror(nasm_verror_vc);
951 else if (nasm_stricmp("gnu", param) == 0)
952 nasm_set_verror(nasm_verror_gnu);
953 else
954 nasm_fatal(ERR_NOFILE | ERR_USAGE,
955 "unrecognized error reporting format `%s'",
956 param);
957 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800959
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700961 if (pass == 2) {
962 using_debug_info = true;
963 if (p[2])
964 debug_format = nasm_skip_spaces(p + 2);
965 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700969 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400970 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800972
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 case 'y':
974 printf("\nvalid debug formats for '%s' output format are"
975 " ('*' denotes default):\n", ofmt->shortname);
976 dfmt_list(ofmt, stdout);
977 exit(0);
978 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800979
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700981 if (pass == 2)
982 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800984
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400986 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400989 case 'e': /* preprocess only */
990 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 if (pass == 1)
992 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700996 if (pass == 1)
997 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800999
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001000 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001001 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001002 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001003 if (!set_warning_status(param)) {
1004 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1005 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001006 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001007 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001008 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001011 if (pass == 1) {
1012 switch (p[2]) {
1013 case 'W':
1014 quote_for_make = quote_for_wmake;
1015 break;
1016 case 'D':
1017 case 'F':
1018 case 'T':
1019 case 'Q':
1020 advance = true;
1021 break;
1022 default:
1023 break;
1024 }
1025 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 switch (p[2]) {
1027 case 0:
1028 operating_mode = OP_DEPEND;
1029 break;
1030 case 'G':
1031 operating_mode = OP_DEPEND;
1032 depend_missing_ok = true;
1033 break;
1034 case 'P':
1035 depend_emit_phony = true;
1036 break;
1037 case 'D':
1038 operating_mode = OP_NORMAL;
1039 depend_file = q;
1040 advance = true;
1041 break;
1042 case 'F':
1043 depend_file = q;
1044 advance = true;
1045 break;
1046 case 'T':
1047 depend_target = q;
1048 advance = true;
1049 break;
1050 case 'Q':
1051 depend_target = quote_for_make(q);
1052 advance = true;
1053 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001054 case 'W':
1055 /* handled in pass 1 */
1056 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001057 default:
1058 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1059 "unknown dependency option `-M%c'", p[2]);
1060 break;
1061 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001062 }
1063 if (advance && (!q || !q[0])) {
1064 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1065 "option `-M%c' requires a parameter", p[2]);
1066 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001067 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 case '-':
1071 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001072 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001073 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001074 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001075
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001076 p += 2;
1077
1078 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001079 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 break;
1081 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001082
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001083 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001084 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001085 olen = strlen(tx->label);
1086
1087 if (olen > plen)
1088 continue;
1089
1090 if (nasm_memicmp(p, tx->label, olen))
1091 continue;
1092
1093 if (tx->label[olen-1] == '-')
1094 break; /* Incomplete option */
1095
1096 if (!p[olen] || p[olen] == '=')
1097 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001099
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001100 if (!tx->label) {
1101 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1102 "unrecognized option `--%s'", p);
1103 }
1104
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001105 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001106 if (param)
1107 *param++ = '\0';
1108
H. Peter Anvin3366e312018-02-07 14:14:36 -08001109 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001110 if (!param) {
1111 param = q;
1112 advance = true;
1113 }
1114
1115 /* Note: a null string is a valid parameter */
1116 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001117 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001118 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001119 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 break;
1121 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001122 } else {
1123 if (param) {
1124 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1125 "option `--%s' does not take an argument",
1126 p);
1127
1128 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001129 }
1130
1131 switch (tx->opt) {
1132 case OPT_VERSION:
1133 show_version();
1134 break;
1135 case OPT_ABORT_ON_PANIC:
1136 abort_on_panic = true;
1137 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001138 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001139 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001140 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001141 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001142 case OPT_INCLUDE:
1143 if (pass == 2)
1144 preproc->pre_include(q);
1145 break;
1146 case OPT_PRAGMA:
1147 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001148 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001149 break;
1150 case OPT_BEFORE:
1151 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001152 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001153 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001154 case OPT_LIMIT:
1155 if (pass == 2)
1156 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001157 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 case OPT_HELP:
1159 help(0);
1160 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001161 default:
1162 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001164
1165 if (eqsave)
1166 *eqsave = '='; /* Restore = argument separator */
1167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 break;
1169 }
1170
1171 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001172 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1173 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 break;
1175 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001176 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001177 /* In theory we could allow multiple input files... */
1178 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001179 }
1180
1181 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001182}
1183
H. Peter Anvineba20a72002-04-30 20:53:55 +00001184#define ARG_BUF_DELTA 128
1185
H. Peter Anvin55568c12016-10-03 19:46:49 -07001186static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001187{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001188 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001189 int bufsize, prevargsize;
1190
1191 bufsize = prevargsize = ARG_BUF_DELTA;
1192 buffer = nasm_malloc(ARG_BUF_DELTA);
1193 prevarg = nasm_malloc(ARG_BUF_DELTA);
1194 prevarg[0] = '\0';
1195
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 p = buffer;
1198 while (1) { /* Loop to handle long lines */
1199 q = fgets(p, bufsize - (p - buffer), rfile);
1200 if (!q)
1201 break;
1202 p += strlen(p);
1203 if (p > buffer && p[-1] == '\n')
1204 break;
1205 if (p - buffer > bufsize - 10) {
1206 int offset;
1207 offset = p - buffer;
1208 bufsize += ARG_BUF_DELTA;
1209 buffer = nasm_realloc(buffer, bufsize);
1210 p = buffer + offset;
1211 }
1212 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 if (!q && p == buffer) {
1215 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001216 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 nasm_free(buffer);
1218 nasm_free(prevarg);
1219 return;
1220 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 /*
1223 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1224 * them are present at the end of the line.
1225 */
1226 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001227
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001228 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001230
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001231 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001232
H. Peter Anvin55568c12016-10-03 19:46:49 -07001233 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235
Charles Crayne192d5b52007-10-18 19:02:42 -07001236 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 prevargsize += ARG_BUF_DELTA;
1238 prevarg = nasm_realloc(prevarg, prevargsize);
1239 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001240 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241 }
1242}
1243
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001244/* Function to process args from a string of args, rather than the
1245 * argv array. Used by the environment variable and response file
1246 * processing.
1247 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001248static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001250 char *p, *q, *arg, *prevarg;
1251 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001252
1253 p = args;
1254 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001256 arg = NULL;
1257 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 q = p;
1259 while (*p && *p != separator)
1260 p++;
1261 while (*p == separator)
1262 *p++ = '\0';
1263 prevarg = arg;
1264 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001265 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001267 }
1268 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001269 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001270}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001271
H. Peter Anvin55568c12016-10-03 19:46:49 -07001272static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001273{
1274 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001275 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001276 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001277 perror(file);
1278 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001279 }
1280 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001281 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001282 }
1283 fclose(f);
1284}
1285
H. Peter Anvin55568c12016-10-03 19:46:49 -07001286static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001287{
1288 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001289 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001290 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001291
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001292 /* Initialize all the warnings to their default state */
1293 for (i = 0; i < ERR_WARN_ALL; i++) {
1294 warning_state_init[i] = warning_state[i] =
1295 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1296 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001297
1298 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001299 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001300 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001301 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001302 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001304 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001306 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001307
1308 /*
1309 * Now process the actual command line.
1310 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001312 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 argv++;
1314 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001315 /*
1316 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 * arguments like the environment variable. This allows us
1318 * to have multiple arguments on a single line, which is
1319 * different to the -@resp file processing below for regular
1320 * NASM.
1321 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001322 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 argc--;
1324 argv++;
1325 }
1326 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001327 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001328 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001329 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001330 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001331 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 fclose(rfile);
1333 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001334 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 "unable to open response file `%s'", p);
1336 }
1337 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001338 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001339 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001340 }
1341
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001342 /*
1343 * Look for basic command line typos. This definitely doesn't
1344 * catch all errors, but it might help cases of fumbled fingers.
1345 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001346 if (pass != 2)
1347 return;
1348
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001349 if (!inname)
1350 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001351
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001352 else if ((errname && !strcmp(inname, errname)) ||
1353 (outname && !strcmp(inname, outname)) ||
1354 (listname && !strcmp(inname, listname)) ||
1355 (depend_file && !strcmp(inname, depend_file)))
1356 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1357
1358 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001359 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001360 if (!error_file) {
1361 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001362 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001363 "cannot open file `%s' for error messages",
1364 errname);
1365 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001366 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001367}
1368
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001369static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001370{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001371 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001373 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001374 uint64_t prev_offset_changed;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001375 int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001376
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001377 switch (cmd_sb) {
1378 case 16:
1379 break;
1380 case 32:
1381 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1382 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1383 break;
1384 case 64:
1385 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1386 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1387 break;
1388 default:
1389 panic();
1390 break;
1391 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392
H. Peter Anvin73482482018-06-11 14:54:14 -07001393 /* Any segment numbers allocated before this point are permanent */
1394 seg_alloc_setup_done();
1395
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001396 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001397 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001398 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001399 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1400 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401
H. Peter Anvincac0b192017-03-28 16:12:30 -07001402 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 cpu = cmd_cpu;
1404 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001405 lfmt->init(listname);
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001406 } else if (passn == 1 && listname) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001407 /* Remove the list file in case we die before the output pass */
1408 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001410 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001411 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin892c4812018-05-30 14:43:46 -07001412 seg_alloc_reset();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001413 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 saa_rewind(forwrefs);
1415 forwref = saa_rstruct(forwrefs);
1416 raa_free(offsets);
1417 offsets = raa_init();
1418 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001419 location.segment = NO_SEG;
1420 location.offset = 0;
1421 if (passn == 1)
1422 location.known = true;
1423 ofmt->reset();
1424 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001425 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001426
1427 /* Revert all warnings to the default state */
1428 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001429
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001433 if (globallineno++ == GLOBALLINENO_MAX)
1434 nasm_error(ERR_FATAL,
1435 "overall line number reaches the maximum %d\n",
1436 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001437
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001438 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001439 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001440 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001441 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001442 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001443 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001444
H. Peter Anvinc7131682017-03-07 17:45:01 -08001445 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001446 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001447
1448 if (optimizing > 0) {
1449 if (forwref != NULL && globallineno == forwref->lineno) {
1450 output_ins.forw_ref = true;
1451 do {
1452 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1453 forwref = saa_rstruct(forwrefs);
1454 } while (forwref != NULL
1455 && forwref->lineno == globallineno);
1456 } else
1457 output_ins.forw_ref = false;
1458
1459 if (output_ins.forw_ref) {
1460 if (passn == 1) {
1461 for (i = 0; i < output_ins.operands; i++) {
1462 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1463 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1464 fwinf->lineno = globallineno;
1465 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001466 }
1467 }
1468 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001469 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001470 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001471
H. Peter Anvinc7131682017-03-07 17:45:01 -08001472 /* forw_ref */
1473 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001474 if (!output_ins.label)
1475 nasm_error(ERR_NONFATAL,
1476 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001477
H. Peter Anvin98578072018-06-01 18:02:54 -07001478 if (output_ins.operands == 1 &&
1479 (output_ins.oprs[0].type & IMMEDIATE) &&
1480 output_ins.oprs[0].wrt == NO_SEG) {
1481 define_label(output_ins.label,
1482 output_ins.oprs[0].segment,
1483 output_ins.oprs[0].offset, false);
1484 } else if (output_ins.operands == 2
1485 && (output_ins.oprs[0].type & IMMEDIATE)
1486 && (output_ins.oprs[0].type & COLON)
1487 && output_ins.oprs[0].segment == NO_SEG
1488 && output_ins.oprs[0].wrt == NO_SEG
1489 && (output_ins.oprs[1].type & IMMEDIATE)
1490 && output_ins.oprs[1].segment == NO_SEG
1491 && output_ins.oprs[1].wrt == NO_SEG) {
1492 define_label(output_ins.label,
1493 output_ins.oprs[0].offset | SEG_ABS,
1494 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001495 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001496 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001497 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001498 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001499 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001501 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001502
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001503 for (n = 1; n <= output_ins.times; n++) {
1504 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001505 int64_t l = insn_size(location.segment,
1506 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001507 globalbits, &output_ins);
1508
1509 /* if (using_debug_info) && output_ins.opcode != -1) */
1510 if (using_debug_info)
1511 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001512 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001513 int32_t typeinfo =
1514 TYS_ELEMENTS(output_ins.operands);
1515 switch (output_ins.opcode) {
1516 case I_RESB:
1517 typeinfo =
1518 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1519 break;
1520 case I_RESW:
1521 typeinfo =
1522 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1523 break;
1524 case I_RESD:
1525 typeinfo =
1526 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1527 break;
1528 case I_RESQ:
1529 typeinfo =
1530 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1531 break;
1532 case I_REST:
1533 typeinfo =
1534 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1535 break;
1536 case I_RESO:
1537 typeinfo =
1538 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1539 break;
1540 case I_RESY:
1541 typeinfo =
1542 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1543 break;
1544 case I_RESZ:
1545 typeinfo =
1546 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1547 break;
1548 case I_DB:
1549 typeinfo |= TY_BYTE;
1550 break;
1551 case I_DW:
1552 typeinfo |= TY_WORD;
1553 break;
1554 case I_DD:
1555 if (output_ins.eops_float)
1556 typeinfo |= TY_FLOAT;
1557 else
1558 typeinfo |= TY_DWORD;
1559 break;
1560 case I_DQ:
1561 typeinfo |= TY_QWORD;
1562 break;
1563 case I_DT:
1564 typeinfo |= TY_TBYTE;
1565 break;
1566 case I_DO:
1567 typeinfo |= TY_OWORD;
1568 break;
1569 case I_DY:
1570 typeinfo |= TY_YWORD;
1571 break;
1572 case I_DZ:
1573 typeinfo |= TY_ZWORD;
1574 break;
1575 default:
1576 typeinfo = TY_LABEL;
1577 break;
1578 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001579
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001580 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001581 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001582
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001583 /*
1584 * For INCBIN, let the code in assemble
1585 * handle TIMES, so we don't have to read the
1586 * input file over and over.
1587 */
1588 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001589 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001590 }
1591 /*
1592 * else l == -1 => invalid instruction, which will be
1593 * flagged as an error on pass 2
1594 */
1595 } else {
1596 if (n == 2)
1597 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001598 increment_offset(assemble(location.segment,
1599 location.offset,
1600 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001602 } /* not an EQU */
1603 }
1604 if (output_ins.times > 1)
1605 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001606
H. Peter Anvinc7131682017-03-07 17:45:01 -08001607 cleanup_insn(&output_ins);
1608
1609 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001612
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001613 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001614 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001615 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 if (pass1 == 1)
1618 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001620 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001622 } else if (global_offset_changed &&
1623 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001624 prev_offset_changed = global_offset_changed;
1625 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001626 } else {
1627 stall_count++;
1628 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001629
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001630 if (terminate_after_phase)
1631 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001632
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001633 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1634 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001635 /* We get here if the labels don't converge
1636 * Example: FOO equ FOO + 1
1637 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001638 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001639 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001640 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001641 nasm_error(ERR_NONFATAL,
1642 "Possible causes: recursive EQUs, macro abuse.");
1643 break;
1644 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001645 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001648 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001649 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001650 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001651 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1652 }
1653}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654
Ed Berosetfa771012002-06-09 20:56:40 +00001655/**
1656 * gnu style error reporting
1657 * This function prints an error message to error_file in the
1658 * style used by GNU. An example would be:
1659 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001660 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001661 * which the error occurs (or is detected) and "error:" is one of
1662 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001663 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001664 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001665 *
1666 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001667 * @param fmt the printf style format string
1668 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001669static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001671 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001672 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001673
Ed Berosetfa771012002-06-09 20:56:40 +00001674 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001676
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001677 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001678 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001679 if (!currentfile || (severity & ERR_TOPFILE)) {
1680 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1681 lineno = 0;
1682 }
1683 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001684
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001685 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001686 if (!lineno)
1687 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001688 else
1689 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001690 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001691
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001692 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001693}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001694
Ed Berosetfa771012002-06-09 20:56:40 +00001695/**
1696 * MS style error reporting
1697 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001698 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001699 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001700 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001701 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001702 * which the error occurs (or is detected) and "error:" is one of
1703 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001704 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001705 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001706 *
1707 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001708 * @param fmt the printf style format string
1709 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001710static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001711{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001712 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001713 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001714
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 if (is_suppressed_warning(severity))
1716 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001717
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001718 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001720
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001721 if (!skip_this_pass(severity)) {
1722 if (currentfile) {
1723 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001724 } else {
1725 fputs("nasm: ", error_file);
1726 }
Ed Berosetfa771012002-06-09 20:56:40 +00001727 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001728
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001729 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001730}
1731
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001732/*
1733 * check to see if this is a suppressable warning
1734 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001735static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001736{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001737 /* Not a warning at all */
1738 if ((severity & ERR_MASK) != ERR_WARNING)
1739 return false;
1740
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001741 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001742}
1743
Ed Berosetfa771012002-06-09 20:56:40 +00001744/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001745 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001746 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001747 * not in pass 1
1748 *
1749 * @param severity the severity of the warning or error
1750 * @return true if we should abort error/warning printing
1751 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001752static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001753{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001754 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001755 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001756 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1757 else
1758 return false;
1759}
1760
1761static bool warning_is_error(int severity)
1762{
1763 if (is_valid_warning(severity))
1764 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001765 else
1766 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001767}
1768
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001769static bool skip_this_pass(int severity)
1770{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001771 /*
1772 * See if it's a pass-specific error or warning which should be skipped.
1773 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1774 * they cannot be resumed from.
1775 */
1776 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001777 return false;
1778
H. Peter Anvin934f0472016-05-09 12:00:19 -07001779 /*
1780 * passn is 1 on the very first pass only.
1781 * pass0 is 2 on the code-generation (final) pass only.
1782 * These are the passes we care about in this case.
1783 */
1784 return (((severity & ERR_PASS1) && passn != 1) ||
1785 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001786}
1787
Ed Berosetfa771012002-06-09 20:56:40 +00001788/**
1789 * common error reporting
1790 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001791 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001792 * specific error message to error_file and may or may not return. It
1793 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001794 *
1795 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001796 * @param fmt the printf style format string
1797 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001798static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001799{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001800 char msg[1024];
1801 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001802
H. Peter Anvin7df04172008-06-10 18:27:38 -07001803 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001805 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
1807 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001808 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 break;
1810 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001811 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 break;
1813 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001814 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 break;
1816 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001817 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001819 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001820 pfx = "";
1821 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001822 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001823
H. Peter Anvin934f0472016-05-09 12:00:19 -07001824 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001825 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001826 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001827 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1828 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001829
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001830 if (!skip_this_pass(severity))
1831 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001832
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001833 /* Are we recursing from error_list_macros? */
1834 if (severity & ERR_PP_LISTMACRO)
1835 return;
1836
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001837 /*
1838 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001839 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001840 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001841 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001842
H. Peter Anvin8f622462017-04-02 19:02:29 -07001843 if (skip_this_pass(severity))
1844 return;
1845
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001846 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001848
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001849 preproc->error_list_macros(severity);
1850
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001851 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 case ERR_DEBUG:
1853 /* no further action, by definition */
1854 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001855 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001856 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001857 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001858 terminate_after_phase = true;
1859 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001861 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 break;
1863 case ERR_FATAL:
1864 if (ofile) {
1865 fclose(ofile);
1866 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001867 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 }
1869 if (want_usage)
1870 usage();
1871 exit(1); /* instantly die */
1872 break; /* placate silly compilers */
1873 case ERR_PANIC:
1874 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001875
1876 if (abort_on_panic)
1877 abort(); /* halt, catch fire, dump core/stop debugger */
1878
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001879 if (ofile) {
1880 fclose(ofile);
1881 remove(outname);
1882 ofile = NULL;
1883 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 exit(3);
1885 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001886 }
1887}
1888
H. Peter Anvin734b1882002-04-30 21:01:08 +00001889static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001890{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001891 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001892}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001893
1894static void help(const char xopt)
1895{
1896 int i;
1897
1898 printf
1899 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1900 "[-l listfile]\n"
1901 " [options...] [--] filename\n"
1902 " or nasm -v (or --v) for version info\n\n"
1903 "\n"
1904 "Response files should contain command line parameters,\n"
1905 "one per line.\n"
1906 "\n"
1907 " -t assemble in SciTech TASM compatible mode\n");
1908 printf
1909 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1910 " -a don't preprocess (assemble only)\n"
1911 " -M generate Makefile dependencies on stdout\n"
1912 " -MG d:o, missing files assumed generated\n"
1913 " -MF file set Makefile dependency file\n"
1914 " -MD file assemble and generate dependencies\n"
1915 " -MT file dependency target name\n"
1916 " -MQ file dependency target name (quoted)\n"
1917 " -MP emit phony target\n\n"
1918 " -Zfile redirect error messages to file\n"
1919 " -s redirect error messages to stdout\n\n"
1920 " -g generate debugging information\n\n"
1921 " -F format select a debugging format\n\n"
1922 " -gformat same as -g -F format\n\n"
1923 " -o outfile write output to an outfile\n\n"
1924 " -f format select an output format\n\n"
1925 " -l listfile write listing to a listfile\n\n"
1926 " -Ipath add a pathname to the include file path\n");
1927 printf
1928 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1929 " -O0 no optimization\n"
1930 " -O1 minimal optimization\n"
1931 " -Ox multipass optimization (default)\n"
1932 " -Pfile pre-include a file (also --include)\n"
1933 " -Dmacro[=str] pre-define a macro\n"
1934 " -Umacro undefine a macro\n"
1935 " -Xformat specifiy error reporting format (gnu or vc)\n"
1936 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1937 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1938 " -w[+-]error[=foo]\n"
1939 " promote [specific] warnings to errors\n"
1940 " -h show invocation summary and exit\n\n"
1941 " --pragma str pre-executes a specific %%pragma\n"
1942 " --before str add line (usually a preprocessor statement) before the input\n"
1943 " --prefix str prepend the given string to all the given string\n"
1944 " to all extern, common and global symbols\n"
1945 " --suffix str append the given string to all the given string\n"
1946 " to all extern, common and global symbols\n"
1947 " --lprefix str prepend the given string to all other symbols\n"
1948 " --limit-X val set execution limit X\n");
1949
1950 for (i = 0; i <= LIMIT_MAX; i++) {
1951 printf(" %-15s %s (default ",
1952 limit_info[i].name, limit_info[i].help);
1953 if (nasm_limit[i] < LIMIT_MAX_VAL) {
1954 printf("%d)\n", nasm_limit[i]);
1955 } else {
1956 printf("unlimited)\n");
1957 }
1958 }
1959
1960 printf("\nWarnings for the -W/-w options:\n");
1961
1962 for (i = 0; i <= ERR_WARN_ALL; i++)
1963 printf(" %-23s %s%s\n",
1964 warnings[i].name, warnings[i].help,
1965 i == ERR_WARN_ALL ? "\n" :
1966 warnings[i].enabled ? " (default on)" :
1967 " (default off)");
1968
1969 if (xopt == 'f') {
1970 printf("valid output formats for -f are"
1971 " (`*' denotes default):\n");
1972 ofmt_list(ofmt, stdout);
1973 } else {
1974 printf("For a list of valid output formats, use -hf.\n");
1975 printf("For a list of debug formats, use -f <format> -y.\n");
1976 }
1977}