blob: 73ab9fe0232de28d7a3f68467798916c4b4e2454 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvinc7131682017-03-07 17:45:01 -08003 * Copyright 1996-2017 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 Anvin9e1f5282008-05-29 21:38:00 -070078static void assemble_file(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);
85
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080086static bool using_debug_info, opt_verbose_info;
87static const char *debug_format;
88
H. Peter Anvin6867acc2007-10-10 14:58:45 -070089bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080090int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080091static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000092int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080093int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000094
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -070095struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080096
Keith Kaniosa6dfa782007-04-13 16:47:53 +000097static char inname[FILENAME_MAX];
98static char outname[FILENAME_MAX];
99static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700100static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000102/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800103const struct ofmt *ofmt = &OF_DEFAULT;
104const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400105const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000108
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400109FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700110int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100111static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400112
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800113iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400114static iflag_t cmd_cpu;
115
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800116struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800117bool in_absolute; /* Flag we are in ABSOLUTE seg */
118struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000119
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000121
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700123static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
H. Peter Anvine7469712016-02-18 02:20:59 -0800125static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400126
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400127#define OP_NORMAL (1u << 0)
128#define OP_PREPROCESS (1u << 1)
129#define OP_DEPEND (1u << 2)
130
131static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400132
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700133/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700134static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700135static bool depend_missing_ok = false;
136static const char *depend_target = NULL;
137static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700139static bool want_usage;
140static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800141bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142
H. Peter Anvin55340992012-09-09 17:09:00 -0700143static char *quote_for_make(const char *str);
144
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400145static int64_t get_curr_offs(void)
146{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800147 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400148}
149
150static void set_curr_offs(int64_t l_off)
151{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800152 if (in_absolute)
153 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400154 else
155 offsets = raa_write(offsets, location.segment, l_off);
156}
157
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000158static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000159{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000160 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000161 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800162 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000163 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000165}
166
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800167static void define_macros_early(void)
168{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700169 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800170 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800171
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700172 if (oct->have_local) {
173 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400174 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700175 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400176 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700177 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400178 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700179 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400180 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800181 }
182
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700183 if (oct->have_gm) {
184 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400185 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700186 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400187 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700188 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400189 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700190 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400191 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800192 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700193
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700194 if (oct->have_posix) {
195 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400196 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800197 }
198}
199
200static void define_macros_late(void)
201{
202 char temp[128];
203
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400204 /*
205 * In case if output format is defined by alias
206 * we have to put shortname of the alias itself here
207 * otherwise ABI backward compatibility gets broken.
208 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400209 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400210 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400211 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800212}
213
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700214static void emit_dependencies(StrList *list)
215{
216 FILE *deps;
217 int linepos, len;
218 StrList *l, *nl;
219
220 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700221 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400222 if (!deps) {
223 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
224 "unable to write dependency file `%s'", depend_file);
225 return;
226 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700227 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400228 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700229 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700230
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700231 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400232 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700233 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400234 len = strlen(file);
235 if (linepos + len > 62 && linepos > 1) {
236 fprintf(deps, " \\\n ");
237 linepos = 1;
238 }
239 fprintf(deps, " %s", file);
240 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700241 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700242 }
243 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700244
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400245 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400246 if (depend_emit_phony)
247 fprintf(deps, "%s:\n\n", l->str);
248 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700249 }
250
251 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400252 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700253}
254
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700255/* Convert a struct tm to a POSIX-style time constant */
256static int64_t make_posix_time(const struct tm *tm)
257{
258 int64_t t;
259 int64_t y = tm->tm_year;
260
261 /* See IEEE 1003.1:2004, section 4.14 */
262
263 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
264 t += tm->tm_yday;
265 t *= 24;
266 t += tm->tm_hour;
267 t *= 60;
268 t += tm->tm_min;
269 t *= 60;
270 t += tm->tm_sec;
271
272 return t;
273}
274
275static void timestamp(void)
276{
277 struct compile_time * const oct = &official_compile_time;
278 const struct tm *tp, *best_gm;
279
280 time(&oct->t);
281
282 best_gm = NULL;
283
284 tp = localtime(&oct->t);
285 if (tp) {
286 oct->local = *tp;
287 best_gm = &oct->local;
288 oct->have_local = true;
289 }
290
291 tp = gmtime(&oct->t);
292 if (tp) {
293 oct->gm = *tp;
294 best_gm = &oct->gm;
295 oct->have_gm = true;
296 if (!oct->have_local)
297 oct->local = oct->gm;
298 } else {
299 oct->gm = oct->local;
300 }
301
302 if (best_gm) {
303 oct->posix = make_posix_time(best_gm);
304 oct->have_posix = true;
305 }
306}
307
H. Peter Anvin038d8612007-04-12 16:54:50 +0000308int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000309{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700310 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700311
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700312 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800313
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400314 iflag_set(&cpu, IF_PLEVEL);
315 iflag_set(&cmd_cpu, IF_PLEVEL);
316
Charles Crayne2581c862008-09-10 19:21:52 -0700317 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700318 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400319 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000320
H. Peter Anvin97e15752007-09-25 08:47:47 -0700321 error_file = stderr;
322
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700323 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700324 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700325
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000326 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000327 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000328
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400330 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000331
H. Peter Anvin55568c12016-10-03 19:46:49 -0700332 parse_cmdline(argc, argv, 1);
333 if (terminate_after_phase) {
334 if (want_usage)
335 usage();
336 return 1;
337 }
338
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700339 /*
340 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700341 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700342 */
343 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800344 define_macros_early();
345
H. Peter Anvin55568c12016-10-03 19:46:49 -0700346 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000347 if (terminate_after_phase) {
348 if (want_usage)
349 usage();
350 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 }
352
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800353 /* Save away the default state of warnings */
354 memcpy(warning_state_init, warning_state, sizeof warning_state);
355
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800356 if (!using_debug_info) {
357 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800358 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800359 } else if (!debug_format) {
360 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800361 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800362 } else {
363 dfmt = dfmt_find(ofmt, debug_format);
364 if (!dfmt) {
365 nasm_fatal(ERR_NOFILE | ERR_USAGE,
366 "unrecognized debug format `%s' for"
367 " output format `%s'",
368 debug_format, ofmt->shortname);
369 }
370 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000371
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400373 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000375 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800376 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000377
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400378 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400380 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700381
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400382 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000383 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700384
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400385 if (depend_missing_ok)
386 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700387
H. Peter Anvin130736c2016-02-17 20:27:41 -0800388 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400390 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 while ((line = preproc->getline()))
393 nasm_free(line);
394 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400395 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000396 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700397 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000398 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000400
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700402 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800404 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 "unable to open output file `%s'",
406 outname);
407 } else
408 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700410 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000411
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400412 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800413 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800414
415 /* Revert all warnings to the default state */
416 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700417
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 while ((line = preproc->getline())) {
419 /*
420 * We generate %line directives if needed for later programs
421 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000422 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 int altline = src_get(&linnum, &file_name);
424 if (altline) {
425 if (altline == 1 && lineinc == 1)
426 nasm_fputs("", ofile);
427 else {
428 lineinc = (altline != -1 || lineinc != 1);
429 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000430 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 file_name);
432 }
433 prior_linnum = linnum;
434 }
435 nasm_fputs(line, ofile);
436 nasm_free(line);
437 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 preproc->cleanup(0);
439 if (ofile)
440 fclose(ofile);
441 if (ofile && terminate_after_phase)
442 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400443 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400444 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400446 if (operating_mode & OP_NORMAL) {
447 /*
448 * We must call ofmt->filename _anyway_, even if the user
449 * has specified their own output file, because some
450 * formats (eg OBJ and COFF) use ofmt->filename to find out
451 * the name of the input file and then put that inside the
452 * file.
453 */
454 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700456 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400457 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800458 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400459 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400461 /*
462 * We must call init_labels() before ofmt->init() since
463 * some object formats will want to define labels in their
464 * init routines. (eg OS/2 defines the FLAT group)
465 */
466 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400469 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400471 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200472
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400473 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800474 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400475 cleanup_labels();
476 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700477 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400478 nasm_error(ERR_NONFATAL|ERR_NOFILE,
479 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700480 terminate_after_phase = true;
481 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400482 }
483
484 if (ofile) {
485 fclose(ofile);
486 if (terminate_after_phase)
487 remove(outname);
488 ofile = NULL;
489 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000491
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700492 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400493 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700494
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000497
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 raa_free(offsets);
499 saa_free(forwrefs);
500 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000501 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700502 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000503
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700504 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000505}
506
H. Peter Anvineba20a72002-04-30 20:53:55 +0000507/*
508 * Get a parameter for a command line option.
509 * First arg must be in the form of e.g. -f...
510 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800511static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800513 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400514 if (p[2]) /* the parameter's in the option */
515 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800517 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400520 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522 return NULL;
523}
524
H. Peter Anvindc242712007-11-18 11:55:10 -0800525/*
526 * Copy a filename
527 */
528static void copy_filename(char *dst, const char *src)
529{
530 size_t len = strlen(src);
531
532 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800533 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400534 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800535 }
536 strncpy(dst, src, FILENAME_MAX);
537}
538
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700539/*
540 * Convert a string to Make-safe form
541 */
542static char *quote_for_make(const char *str)
543{
544 const char *p;
545 char *os, *q;
546
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400547 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700548 size_t nbs = 0;
549
550 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400551 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700552
553 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400554 switch (*p) {
555 case ' ':
556 case '\t':
557 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
558 n += nbs + 2;
559 nbs = 0;
560 break;
561 case '$':
562 case '#':
563 nbs = 0;
564 n += 2;
565 break;
566 case '\\':
567 nbs++;
568 n++;
569 break;
570 default:
571 nbs = 0;
572 n++;
573 break;
574 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700575 }
576
577 /* Convert N backslashes at the end of filename to 2N backslashes */
578 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400579 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700580
581 os = q = nasm_malloc(n);
582
583 nbs = 0;
584 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400585 switch (*p) {
586 case ' ':
587 case '\t':
588 while (nbs--)
589 *q++ = '\\';
590 *q++ = '\\';
591 *q++ = *p;
592 break;
593 case '$':
594 *q++ = *p;
595 *q++ = *p;
596 nbs = 0;
597 break;
598 case '#':
599 *q++ = '\\';
600 *q++ = *p;
601 nbs = 0;
602 break;
603 case '\\':
604 *q++ = *p;
605 nbs++;
606 break;
607 default:
608 *q++ = *p;
609 nbs = 0;
610 break;
611 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700612 }
613 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400614 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700615
616 *q = '\0';
617
618 return os;
619}
620
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000622 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000624};
625
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100626enum text_options {
627 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800628 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100629};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800630static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 {"prefix", OPT_PREFIX},
632 {"postfix", OPT_POSTFIX},
633 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000634};
635
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400636static void show_version(void)
637{
638 printf("NASM version %s compiled on %s%s\n",
639 nasm_version, nasm_date, nasm_compile_options);
640 exit(0);
641}
642
H. Peter Anvin423e3812007-11-15 17:12:29 -0800643static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700644static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000646 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800647 int i;
648 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000649
650 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800651 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400654 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
655 /* These parameters take values */
656 if (!(param = get_param(p, q, &advance)))
657 return advance;
658 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 switch (p[1]) {
661 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700662 if (pass == 1)
663 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700665
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400666 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700667 if (pass == 2)
668 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400669 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700670
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400671 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700672 if (pass == 1) {
673 ofmt = ofmt_find(param, &ofmt_alias);
674 if (!ofmt) {
675 nasm_fatal(ERR_NOFILE | ERR_USAGE,
676 "unrecognised output format `%s' - "
677 "use -hf for a list", param);
678 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400679 }
680 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700681
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400682 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700683 if (pass == 2) {
684 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700685
H. Peter Anvin55568c12016-10-03 19:46:49 -0700686 if (!*param) {
687 /* Naked -O == -Ox */
688 optimizing = MAX_OPTIMIZE;
689 } else {
690 while (*param) {
691 switch (*param) {
692 case '0': case '1': case '2': case '3': case '4':
693 case '5': case '6': case '7': case '8': case '9':
694 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700695
H. Peter Anvin55568c12016-10-03 19:46:49 -0700696 /* -O0 -> optimizing == -1, 0.98 behaviour */
697 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
698 if (opt < 2)
699 optimizing = opt - 1;
700 else
701 optimizing = opt;
702 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700703
H. Peter Anvin55568c12016-10-03 19:46:49 -0700704 case 'v':
705 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400706 param++;
707 opt_verbose_info = true;
708 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700709
H. Peter Anvin55568c12016-10-03 19:46:49 -0700710 case 'x':
711 param++;
712 optimizing = MAX_OPTIMIZE;
713 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700714
H. Peter Anvin55568c12016-10-03 19:46:49 -0700715 default:
716 nasm_fatal(0,
717 "unknown optimization option -O%c\n",
718 *param);
719 break;
720 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400721 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700722 if (optimizing > MAX_OPTIMIZE)
723 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400725 }
726 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'p': /* pre-include */
729 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700730 if (pass == 2)
731 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400732 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800733
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 case 'd': /* pre-define */
735 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700736 if (pass == 2)
737 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800739
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 case 'u': /* un-define */
741 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700742 if (pass == 2)
743 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'i': /* include search path */
747 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700748 if (pass == 2)
749 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400750 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800751
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400752 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700753 if (pass == 2)
754 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700758 if (pass == 1)
759 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400762 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700763 if (pass == 2) {
764 using_debug_info = true;
765 debug_format = param;
766 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400767 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800768
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400769 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700770 if (pass == 1) {
771 if (nasm_stricmp("vc", param) == 0)
772 nasm_set_verror(nasm_verror_vc);
773 else if (nasm_stricmp("gnu", param) == 0)
774 nasm_set_verror(nasm_verror_gnu);
775 else
776 nasm_fatal(ERR_NOFILE | ERR_USAGE,
777 "unrecognized error reporting format `%s'",
778 param);
779 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800781
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700783 if (pass == 2) {
784 using_debug_info = true;
785 if (p[2])
786 debug_format = nasm_skip_spaces(p + 2);
787 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800789
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 case 'h':
791 printf
792 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
793 "[-l listfile]\n"
794 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400795 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800796 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700798 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700800 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400801 " -MG d:o, missing files assumed generated\n"
802 " -MF <file> set Makefile dependency file\n"
803 " -MD <file> assemble and generate dependencies\n"
804 " -MT <file> dependency target name\n"
805 " -MQ <file> dependency target name (quoted)\n"
806 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700807 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800809 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800811 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400812 " -o outfile write output to an outfile\n\n"
813 " -f format select an output format\n\n"
814 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 " -I<path> adds a pathname to the include file path\n");
816 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700817 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400818 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400819 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400820 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 " -P<file> pre-includes a file\n"
822 " -D<macro>[=<value>] pre-defines a macro\n"
823 " -U<macro> undefines a macro\n"
824 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800825 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400826 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800827 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
828 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400829 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800830 " these options prepend or append the given string\n"
831 " to all extern and global variables\n"
832 "\n"
833 "Response files should contain command line parameters,\n"
834 "one per line.\n"
835 "\n"
836 "Warnings for the -W/-w options:\n");
837 for (i = 0; i <= ERR_WARN_ALL; i++)
838 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700839 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800840 i == ERR_WARN_ALL ? "\n" :
841 warnings[i].enabled ? " (default on)" :
842 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800844 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 " (`*' denotes default):\n");
846 ofmt_list(ofmt, stdout);
847 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800848 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 printf("For a list of debug formats, use -f <form> -y.\n");
850 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 case 'y':
855 printf("\nvalid debug formats for '%s' output format are"
856 " ('*' denotes default):\n", ofmt->shortname);
857 dfmt_list(ofmt, stdout);
858 exit(0);
859 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700862 if (pass == 2)
863 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800865
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400867 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800869
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400870 case 'e': /* preprocess only */
871 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872 if (pass == 1)
873 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800875
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700877 if (pass == 1)
878 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800880
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800881 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400882 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700883 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800884 if (!set_warning_status(param)) {
885 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
886 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700887 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800889 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800890
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700892 if (pass == 2) {
893 switch (p[2]) {
894 case 0:
895 operating_mode = OP_DEPEND;
896 break;
897 case 'G':
898 operating_mode = OP_DEPEND;
899 depend_missing_ok = true;
900 break;
901 case 'P':
902 depend_emit_phony = true;
903 break;
904 case 'D':
905 operating_mode = OP_NORMAL;
906 depend_file = q;
907 advance = true;
908 break;
909 case 'F':
910 depend_file = q;
911 advance = true;
912 break;
913 case 'T':
914 depend_target = q;
915 advance = true;
916 break;
917 case 'Q':
918 depend_target = quote_for_make(q);
919 advance = true;
920 break;
921 default:
922 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
923 "unknown dependency option `-M%c'", p[2]);
924 break;
925 }
926 if (advance && (!q || !q[0])) {
927 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
928 "option `-M%c' requires a parameter", p[2]);
929 break;
930 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400931 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 case '-':
935 {
936 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000937
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 if (p[2] == 0) { /* -- => stop processing options */
939 stopoptions = 1;
940 break;
941 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400942
943 if (!nasm_stricmp(p, "--v"))
944 show_version();
945
Andy Willis3f546032016-09-13 00:02:21 +0300946 if (!nasm_stricmp(p, "--version"))
947 show_version();
948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 for (s = 0; textopts[s].label; s++) {
950 if (!nasm_stricmp(p + 2, textopts[s].label)) {
951 break;
952 }
953 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 switch (s) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 case OPT_PREFIX:
957 case OPT_POSTFIX:
958 {
959 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400960 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 ERR_USAGE,
962 "option `--%s' requires an argument",
963 p + 2);
964 break;
965 } else {
966 advance = 1, param = q;
967 }
968
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100969 switch (s) {
970 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700971 if (pass == 2)
972 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100974 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700975 if (pass == 2)
976 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100977 break;
978 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700979 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 break;
981 }
982 break;
983 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800984
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 default:
986 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400987 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 "unrecognised option `--%s'", p + 2);
989 break;
990 }
991 }
992 break;
993 }
994
995 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700996 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
997 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
999 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001000 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001002 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001004 } else {
1005 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001007 }
1008
1009 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001010}
1011
H. Peter Anvineba20a72002-04-30 20:53:55 +00001012#define ARG_BUF_DELTA 128
1013
H. Peter Anvin55568c12016-10-03 19:46:49 -07001014static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001016 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001017 int bufsize, prevargsize;
1018
1019 bufsize = prevargsize = ARG_BUF_DELTA;
1020 buffer = nasm_malloc(ARG_BUF_DELTA);
1021 prevarg = nasm_malloc(ARG_BUF_DELTA);
1022 prevarg[0] = '\0';
1023
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 p = buffer;
1026 while (1) { /* Loop to handle long lines */
1027 q = fgets(p, bufsize - (p - buffer), rfile);
1028 if (!q)
1029 break;
1030 p += strlen(p);
1031 if (p > buffer && p[-1] == '\n')
1032 break;
1033 if (p - buffer > bufsize - 10) {
1034 int offset;
1035 offset = p - buffer;
1036 bufsize += ARG_BUF_DELTA;
1037 buffer = nasm_realloc(buffer, bufsize);
1038 p = buffer + offset;
1039 }
1040 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001041
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 if (!q && p == buffer) {
1043 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001044 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 nasm_free(buffer);
1046 nasm_free(prevarg);
1047 return;
1048 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001049
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 /*
1051 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1052 * them are present at the end of the line.
1053 */
1054 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001056 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001059 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
H. Peter Anvin55568c12016-10-03 19:46:49 -07001061 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063
Charles Crayne192d5b52007-10-18 19:02:42 -07001064 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 prevargsize += ARG_BUF_DELTA;
1066 prevarg = nasm_realloc(prevarg, prevargsize);
1067 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001068 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001069 }
1070}
1071
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001072/* Function to process args from a string of args, rather than the
1073 * argv array. Used by the environment variable and response file
1074 * processing.
1075 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001076static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001078 char *p, *q, *arg, *prevarg;
1079 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001080
1081 p = args;
1082 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001084 arg = NULL;
1085 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 q = p;
1087 while (*p && *p != separator)
1088 p++;
1089 while (*p == separator)
1090 *p++ = '\0';
1091 prevarg = arg;
1092 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001093 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001095 }
1096 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001097 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001098}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001099
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001101{
1102 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001103 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001104 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001105 perror(file);
1106 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001107 }
1108 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001109 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001110 }
1111 fclose(f);
1112}
1113
H. Peter Anvin55568c12016-10-03 19:46:49 -07001114static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001115{
1116 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001117 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001118 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001119
Charles Craynefcce07f2007-09-30 22:15:36 -07001120 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001121
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001122 /* Initialize all the warnings to their default state */
1123 for (i = 0; i < ERR_WARN_ALL; i++) {
1124 warning_state_init[i] = warning_state[i] =
1125 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1126 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127
1128 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001129 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001130 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001131 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001134 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137
1138 /*
1139 * Now process the actual command line.
1140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001142 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 argv++;
1144 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001145 /*
1146 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 * arguments like the environment variable. This allows us
1148 * to have multiple arguments on a single line, which is
1149 * different to the -@resp file processing below for regular
1150 * NASM.
1151 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001152 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 argc--;
1154 argv++;
1155 }
1156 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001159 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001161 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 fclose(rfile);
1163 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001164 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 "unable to open response file `%s'", p);
1166 }
1167 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001168 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001169 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001170 }
1171
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 /*
1173 * Look for basic command line typos. This definitely doesn't
1174 * catch all errors, but it might help cases of fumbled fingers.
1175 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001176 if (pass != 2)
1177 return;
1178
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001179 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001180 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001181 "no input file specified");
1182 else if (!strcmp(inname, errname) ||
1183 !strcmp(inname, outname) ||
1184 !strcmp(inname, listname) ||
1185 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001186 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001187 "file `%s' is both input and output file",
1188 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001189
H. Peter Anvin70653092007-10-19 14:42:29 -07001190 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001191 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001192 if (!error_file) {
1193 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001194 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001195 "cannot open file `%s' for error messages",
1196 errname);
1197 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001198 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001199}
1200
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001201static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001202{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001203 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001205 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001206 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001207 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001208 uint64_t prev_offset_changed;
1209 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001210
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001211 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001212 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213
Charles Craynec1905c22008-09-11 18:54:06 -07001214 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001215 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001217
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001218 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001219 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1220 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001221
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001222 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223
H. Peter Anvincac0b192017-03-28 16:12:30 -07001224 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 cpu = cmd_cpu;
1226 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001227 lfmt->init(listname);
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001228 } else if (passn == 1 && *listname) {
1229 /* Remove the list file in case we die before the output pass */
1230 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001232 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001233 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001234 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001235 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 saa_rewind(forwrefs);
1237 forwref = saa_rstruct(forwrefs);
1238 raa_free(offsets);
1239 offsets = raa_init();
1240 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001241 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001242
1243 /* Revert all warnings to the default state */
1244 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001247 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001248 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001249 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001250
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 while ((line = preproc->getline())) {
1252 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001254 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001255 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001256 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001257 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001258 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001259 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001260
H. Peter Anvinc7131682017-03-07 17:45:01 -08001261 /* Not a directive, or even something that starts with [ */
1262
1263 parse_line(pass1, line, &output_ins, def_label);
1264
1265 if (optimizing > 0) {
1266 if (forwref != NULL && globallineno == forwref->lineno) {
1267 output_ins.forw_ref = true;
1268 do {
1269 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1270 forwref = saa_rstruct(forwrefs);
1271 } while (forwref != NULL
1272 && forwref->lineno == globallineno);
1273 } else
1274 output_ins.forw_ref = false;
1275
1276 if (output_ins.forw_ref) {
1277 if (passn == 1) {
1278 for (i = 0; i < output_ins.operands; i++) {
1279 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1280 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1281 fwinf->lineno = globallineno;
1282 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001283 }
1284 }
1285 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001286 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001287 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001288
H. Peter Anvinc7131682017-03-07 17:45:01 -08001289 /* forw_ref */
1290 if (output_ins.opcode == I_EQU) {
1291 if (pass1 == 1) {
1292 /*
1293 * Special `..' EQUs get processed in pass two,
1294 * except `..@' macro-processor EQUs which are done
1295 * in the normal place.
1296 */
1297 if (!output_ins.label)
1298 nasm_error(ERR_NONFATAL,
1299 "EQU not preceded by label");
1300
1301 else if (output_ins.label[0] != '.' ||
1302 output_ins.label[1] != '.' ||
1303 output_ins.label[2] == '@') {
1304 if (output_ins.operands == 1 &&
1305 (output_ins.oprs[0].type & IMMEDIATE) &&
1306 output_ins.oprs[0].wrt == NO_SEG) {
1307 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1308 def_label(output_ins.label,
1309 output_ins.oprs[0].segment,
1310 output_ins.oprs[0].offset, NULL,
1311 false, isext);
1312 } else if (output_ins.operands == 2
1313 && (output_ins.oprs[0].type & IMMEDIATE)
1314 && (output_ins.oprs[0].type & COLON)
1315 && output_ins.oprs[0].segment == NO_SEG
1316 && output_ins.oprs[0].wrt == NO_SEG
1317 && (output_ins.oprs[1].type & IMMEDIATE)
1318 && output_ins.oprs[1].segment == NO_SEG
1319 && output_ins.oprs[1].wrt == NO_SEG) {
1320 def_label(output_ins.label,
1321 output_ins.oprs[0].offset | SEG_ABS,
1322 output_ins.oprs[1].offset,
1323 NULL, false, false);
1324 } else
1325 nasm_error(ERR_NONFATAL,
1326 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001327 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001328 } else {
1329 /*
1330 * Special `..' EQUs get processed here, except
1331 * `..@' macro processor EQUs which are done above.
1332 */
1333 if (output_ins.label[0] == '.' &&
1334 output_ins.label[1] == '.' &&
1335 output_ins.label[2] != '@') {
1336 if (output_ins.operands == 1 &&
1337 (output_ins.oprs[0].type & IMMEDIATE)) {
1338 define_label(output_ins.label,
1339 output_ins.oprs[0].segment,
1340 output_ins.oprs[0].offset,
1341 NULL, false, false);
1342 } else if (output_ins.operands == 2
1343 && (output_ins.oprs[0].type & IMMEDIATE)
1344 && (output_ins.oprs[0].type & COLON)
1345 && output_ins.oprs[0].segment == NO_SEG
1346 && (output_ins.oprs[1].type & IMMEDIATE)
1347 && output_ins.oprs[1].segment == NO_SEG) {
1348 define_label(output_ins.label,
1349 output_ins.oprs[0].offset | SEG_ABS,
1350 output_ins.oprs[1].offset,
1351 NULL, false, false);
1352 } else
1353 nasm_error(ERR_NONFATAL,
1354 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001355 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001356 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001357 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001358 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001360 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001361
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001362 for (n = 1; n <= output_ins.times; n++) {
1363 if (pass1 == 1) {
1364 int64_t l = insn_size(location.segment, offs,
1365 globalbits, &output_ins);
1366
1367 /* if (using_debug_info) && output_ins.opcode != -1) */
1368 if (using_debug_info)
1369 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001370 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001371 int32_t typeinfo =
1372 TYS_ELEMENTS(output_ins.operands);
1373 switch (output_ins.opcode) {
1374 case I_RESB:
1375 typeinfo =
1376 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1377 break;
1378 case I_RESW:
1379 typeinfo =
1380 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1381 break;
1382 case I_RESD:
1383 typeinfo =
1384 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1385 break;
1386 case I_RESQ:
1387 typeinfo =
1388 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1389 break;
1390 case I_REST:
1391 typeinfo =
1392 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1393 break;
1394 case I_RESO:
1395 typeinfo =
1396 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1397 break;
1398 case I_RESY:
1399 typeinfo =
1400 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1401 break;
1402 case I_RESZ:
1403 typeinfo =
1404 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1405 break;
1406 case I_DB:
1407 typeinfo |= TY_BYTE;
1408 break;
1409 case I_DW:
1410 typeinfo |= TY_WORD;
1411 break;
1412 case I_DD:
1413 if (output_ins.eops_float)
1414 typeinfo |= TY_FLOAT;
1415 else
1416 typeinfo |= TY_DWORD;
1417 break;
1418 case I_DQ:
1419 typeinfo |= TY_QWORD;
1420 break;
1421 case I_DT:
1422 typeinfo |= TY_TBYTE;
1423 break;
1424 case I_DO:
1425 typeinfo |= TY_OWORD;
1426 break;
1427 case I_DY:
1428 typeinfo |= TY_YWORD;
1429 break;
1430 case I_DZ:
1431 typeinfo |= TY_ZWORD;
1432 break;
1433 default:
1434 typeinfo = TY_LABEL;
1435 break;
1436 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001437
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001438 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001439 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001440
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001441 /*
1442 * For INCBIN, let the code in assemble
1443 * handle TIMES, so we don't have to read the
1444 * input file over and over.
1445 */
1446 if (l != -1) {
1447 offs += l;
1448 set_curr_offs(offs);
1449 }
1450 /*
1451 * else l == -1 => invalid instruction, which will be
1452 * flagged as an error on pass 2
1453 */
1454 } else {
1455 if (n == 2)
1456 lfmt->uplevel(LIST_TIMES);
1457 offs += assemble(location.segment, offs,
1458 globalbits, &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001459 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001461 } /* not an EQU */
1462 }
1463 if (output_ins.times > 1)
1464 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001465
H. Peter Anvinc7131682017-03-07 17:45:01 -08001466 cleanup_insn(&output_ins);
1467
1468 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001470 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001472
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001473 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001474 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001475 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001476
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 if (pass1 == 1)
1478 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001479
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001480 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001482 } else if (global_offset_changed &&
1483 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001484 prev_offset_changed = global_offset_changed;
1485 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001486 } else {
1487 stall_count++;
1488 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001489
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 if (terminate_after_phase)
1491 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001492
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001493 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001494 /* We get here if the labels don't converge
1495 * Example: FOO equ FOO + 1
1496 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001497 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001498 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001499 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001500 nasm_error(ERR_NONFATAL,
1501 "Possible causes: recursive EQUs, macro abuse.");
1502 break;
1503 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001504 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001505
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001507 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001508 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001509 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001510 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1511 }
1512}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001513
Ed Berosetfa771012002-06-09 20:56:40 +00001514/**
1515 * gnu style error reporting
1516 * This function prints an error message to error_file in the
1517 * style used by GNU. An example would be:
1518 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001519 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001520 * which the error occurs (or is detected) and "error:" is one of
1521 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001522 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001523 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001524 *
1525 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001526 * @param fmt the printf style format string
1527 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001528static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001529{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001530 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001531 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001532
Ed Berosetfa771012002-06-09 20:56:40 +00001533 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001535
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001536 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001537 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001538
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001539 if (!skip_this_pass(severity)) {
1540 if (currentfile) {
1541 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001542 } else {
1543 fputs("nasm: ", error_file);
1544 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001545 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001546
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001547 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001548}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001549
Ed Berosetfa771012002-06-09 20:56:40 +00001550/**
1551 * MS style error reporting
1552 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001553 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001554 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001555 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001556 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001557 * which the error occurs (or is detected) and "error:" is one of
1558 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001559 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001560 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001561 *
1562 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001563 * @param fmt the printf style format string
1564 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001565static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001566{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001567 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001568 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 if (is_suppressed_warning(severity))
1571 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001572
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001573 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001575
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001576 if (!skip_this_pass(severity)) {
1577 if (currentfile) {
1578 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001579 } else {
1580 fputs("nasm: ", error_file);
1581 }
Ed Berosetfa771012002-06-09 20:56:40 +00001582 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001583
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001584 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001585}
1586
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001587/*
1588 * check to see if this is a suppressable warning
1589 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001590static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001591{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001592 /* Not a warning at all */
1593 if ((severity & ERR_MASK) != ERR_WARNING)
1594 return false;
1595
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001596 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001597}
1598
Ed Berosetfa771012002-06-09 20:56:40 +00001599/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001600 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001601 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001602 * not in pass 1
1603 *
1604 * @param severity the severity of the warning or error
1605 * @return true if we should abort error/warning printing
1606 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001607static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001608{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001609 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001610 if (is_valid_warning(severity))
1611 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1612 else
1613 return false;
1614}
1615
1616static bool warning_is_error(int severity)
1617{
1618 if (is_valid_warning(severity))
1619 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001620 else
1621 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001622}
1623
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001624static bool skip_this_pass(int severity)
1625{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001626 /*
1627 * See if it's a pass-specific error or warning which should be skipped.
1628 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1629 * they cannot be resumed from.
1630 */
1631 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001632 return false;
1633
H. Peter Anvin934f0472016-05-09 12:00:19 -07001634 /*
1635 * passn is 1 on the very first pass only.
1636 * pass0 is 2 on the code-generation (final) pass only.
1637 * These are the passes we care about in this case.
1638 */
1639 return (((severity & ERR_PASS1) && passn != 1) ||
1640 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001641}
1642
Ed Berosetfa771012002-06-09 20:56:40 +00001643/**
1644 * common error reporting
1645 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001646 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001647 * specific error message to error_file and may or may not return. It
1648 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001649 *
1650 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001651 * @param fmt the printf style format string
1652 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001653static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001654{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001655 char msg[1024];
1656 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001657
H. Peter Anvin7df04172008-06-10 18:27:38 -07001658 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001660 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 break;
1662 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001663 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
1665 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001666 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 break;
1668 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001669 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 break;
1671 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001672 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001674 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001675 pfx = "";
1676 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001678
H. Peter Anvin934f0472016-05-09 12:00:19 -07001679 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001680 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001681 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001682 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1683 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001684
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001685 if (!skip_this_pass(severity))
1686 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001687
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001688 /* Are we recursing from error_list_macros? */
1689 if (severity & ERR_PP_LISTMACRO)
1690 return;
1691
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001692 /*
1693 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001694 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001695 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001696 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001697
H. Peter Anvin8f622462017-04-02 19:02:29 -07001698 if (skip_this_pass(severity))
1699 return;
1700
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001701 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001702 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001703
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001704 preproc->error_list_macros(severity);
1705
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001706 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 case ERR_DEBUG:
1708 /* no further action, by definition */
1709 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001710 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001711 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001712 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001713 terminate_after_phase = true;
1714 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001716 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 break;
1718 case ERR_FATAL:
1719 if (ofile) {
1720 fclose(ofile);
1721 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001722 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 }
1724 if (want_usage)
1725 usage();
1726 exit(1); /* instantly die */
1727 break; /* placate silly compilers */
1728 case ERR_PANIC:
1729 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001730 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001731 if (ofile) {
1732 fclose(ofile);
1733 remove(outname);
1734 ofile = NULL;
1735 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 exit(3);
1737 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001738 }
1739}
1740
H. Peter Anvin734b1882002-04-30 21:01:08 +00001741static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001742{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001743 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001744}