blob: 288cb0e1ff3a34b0b744c27c3fb1f9a62cb21d05 [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 Anvin6b18bcc2008-02-16 14:54:10 -080046#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48#include "nasm.h"
49#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080050#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070061#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080064#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
H. Peter Anvin31387b22010-07-15 18:28:52 -070066/*
67 * This is the maximum number of optimization passes to do. If we ever
68 * find a case where the optimizer doesn't naturally converge, we might
69 * have to drop this value so the assembler doesn't appear to just hang.
70 */
71#define MAX_OPTIMIZE (INT_MAX >> 1)
72
H. Peter Anvine2c80182005-01-15 22:15:51 +000073struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000074 int lineno;
75 int operand;
76};
77
H. Peter Anvin55568c12016-10-03 19:46:49 -070078static void parse_cmdline(int, char **, int);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070079static void assemble_file(char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080080static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080081static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040082static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
83static void nasm_verror_vc(int severity, const char *fmt, va_list args);
84static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin6867acc2007-10-10 14:58:45 -070090bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080091int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080092static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000093int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080094int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000095
H. Peter Anvin9bd15062009-07-18 21:07:17 -040096static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080097
Keith Kaniosa6dfa782007-04-13 16:47:53 +000098static char inname[FILENAME_MAX];
99static char outname[FILENAME_MAX];
100static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700101static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000103/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800104const struct ofmt *ofmt = &OF_DEFAULT;
105const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000109
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400110FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700111int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100112static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400113
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800114iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400115static iflag_t cmd_cpu;
116
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800117struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800118bool in_absolute; /* Flag we are in ABSOLUTE seg */
119struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000120
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000122
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700124static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125
H. Peter Anvine7469712016-02-18 02:20:59 -0800126static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400127
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400128#define OP_NORMAL (1u << 0)
129#define OP_PREPROCESS (1u << 1)
130#define OP_DEPEND (1u << 2)
131
132static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400133
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700134/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700135static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700136static bool depend_missing_ok = false;
137static const char *depend_target = NULL;
138static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700140static bool want_usage;
141static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800142bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143
H. Peter Anvin55340992012-09-09 17:09:00 -0700144static char *quote_for_make(const char *str);
145
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400146static int64_t get_curr_offs(void)
147{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800148 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400149}
150
151static void set_curr_offs(int64_t l_off)
152{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800153 if (in_absolute)
154 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400155 else
156 offsets = raa_write(offsets, location.segment, l_off);
157}
158
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000159static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000160{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000161 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800163 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000164 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000165 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000166}
167
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800168/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800169static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800170{
171 int64_t t;
172 int64_t y = tm->tm_year;
173
174 /* See IEEE 1003.1:2004, section 4.14 */
175
176 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
177 t += tm->tm_yday;
178 t *= 24;
179 t += tm->tm_hour;
180 t *= 60;
181 t += tm->tm_min;
182 t *= 60;
183 t += tm->tm_sec;
184
185 return t;
186}
187
188static void define_macros_early(void)
189{
190 char temp[128];
191 struct tm lt, *lt_p, gm, *gm_p;
192 int64_t posix_time;
193
194 lt_p = localtime(&official_compile_time);
195 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400196 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800197
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400198 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
199 preproc->pre_define(temp);
200 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
201 preproc->pre_define(temp);
202 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
203 preproc->pre_define(temp);
204 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
205 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800206 }
207
208 gm_p = gmtime(&official_compile_time);
209 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400210 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800211
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400212 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
213 preproc->pre_define(temp);
214 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
215 preproc->pre_define(temp);
216 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
217 preproc->pre_define(temp);
218 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
219 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800220 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700221
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800222 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800223 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800224 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800225 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800226 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400227 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800228
229 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
231 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800232 }
233}
234
235static void define_macros_late(void)
236{
237 char temp[128];
238
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400239 /*
240 * In case if output format is defined by alias
241 * we have to put shortname of the alias itself here
242 * otherwise ABI backward compatibility gets broken.
243 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400244 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400245 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400246 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800247}
248
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700249static void emit_dependencies(StrList *list)
250{
251 FILE *deps;
252 int linepos, len;
253 StrList *l, *nl;
254
255 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700256 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400257 if (!deps) {
258 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
259 "unable to write dependency file `%s'", depend_file);
260 return;
261 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700262 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400263 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700264 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700265
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700266 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400267 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700268 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400269 len = strlen(file);
270 if (linepos + len > 62 && linepos > 1) {
271 fprintf(deps, " \\\n ");
272 linepos = 1;
273 }
274 fprintf(deps, " %s", file);
275 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700276 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700277 }
278 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700279
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400280 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400281 if (depend_emit_phony)
282 fprintf(deps, "%s:\n\n", l->str);
283 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700284 }
285
286 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400287 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700288}
289
H. Peter Anvin038d8612007-04-12 16:54:50 +0000290int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000291{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700292 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700293
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800294 time(&official_compile_time);
295
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400296 iflag_set(&cpu, IF_PLEVEL);
297 iflag_set(&cmd_cpu, IF_PLEVEL);
298
Charles Crayne2581c862008-09-10 19:21:52 -0700299 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700300 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400301 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000302
H. Peter Anvin97e15752007-09-25 08:47:47 -0700303 error_file = stderr;
304
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700305 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700306 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700307
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000308 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000309 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000310
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400312 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000313
H. Peter Anvin55568c12016-10-03 19:46:49 -0700314 parse_cmdline(argc, argv, 1);
315 if (terminate_after_phase) {
316 if (want_usage)
317 usage();
318 return 1;
319 }
320
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700321 /*
322 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700323 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700324 */
325 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800326 define_macros_early();
327
H. Peter Anvin55568c12016-10-03 19:46:49 -0700328 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 if (terminate_after_phase) {
330 if (want_usage)
331 usage();
332 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000333 }
334
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800335 /* Save away the default state of warnings */
336 memcpy(warning_state_init, warning_state, sizeof warning_state);
337
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800338 if (!using_debug_info) {
339 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800340 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800341 } else if (!debug_format) {
342 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800343 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800344 } else {
345 dfmt = dfmt_find(ofmt, debug_format);
346 if (!dfmt) {
347 nasm_fatal(ERR_NOFILE | ERR_USAGE,
348 "unrecognized debug format `%s' for"
349 " output format `%s'",
350 debug_format, ofmt->shortname);
351 }
352 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000353
H. Peter Anvin76690a12002-04-30 20:52:49 +0000354 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400355 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000356
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000357 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800358 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000359
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400360 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700361 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400362 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700363
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400364 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000365 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700366
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400367 if (depend_missing_ok)
368 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700369
H. Peter Anvin130736c2016-02-17 20:27:41 -0800370 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400372 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000373 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 while ((line = preproc->getline()))
375 nasm_free(line);
376 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400377 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700379 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000380 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700384 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800386 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 "unable to open output file `%s'",
388 outname);
389 } else
390 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000391
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700392 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000393
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400394 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800395 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800396
397 /* Revert all warnings to the default state */
398 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700399
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 while ((line = preproc->getline())) {
401 /*
402 * We generate %line directives if needed for later programs
403 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000404 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 int altline = src_get(&linnum, &file_name);
406 if (altline) {
407 if (altline == 1 && lineinc == 1)
408 nasm_fputs("", ofile);
409 else {
410 lineinc = (altline != -1 || lineinc != 1);
411 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000412 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 file_name);
414 }
415 prior_linnum = linnum;
416 }
417 nasm_fputs(line, ofile);
418 nasm_free(line);
419 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 preproc->cleanup(0);
421 if (ofile)
422 fclose(ofile);
423 if (ofile && terminate_after_phase)
424 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400425 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400426 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400428 if (operating_mode & OP_NORMAL) {
429 /*
430 * We must call ofmt->filename _anyway_, even if the user
431 * has specified their own output file, because some
432 * formats (eg OBJ and COFF) use ofmt->filename to find out
433 * the name of the input file and then put that inside the
434 * file.
435 */
436 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700438 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400439 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800440 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400441 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400443 /*
444 * We must call init_labels() before ofmt->init() since
445 * some object formats will want to define labels in their
446 * init routines. (eg OS/2 defines the FLAT group)
447 */
448 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400451 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400453 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200454
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400455 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800456 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400457 cleanup_labels();
458 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700459 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400460 nasm_error(ERR_NONFATAL|ERR_NOFILE,
461 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700462 terminate_after_phase = true;
463 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400464 }
465
466 if (ofile) {
467 fclose(ofile);
468 if (terminate_after_phase)
469 remove(outname);
470 ofile = NULL;
471 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000472 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700474 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400475 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700476
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000477 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000479
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 raa_free(offsets);
481 saa_free(forwrefs);
482 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000483 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700484 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700486 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000487}
488
H. Peter Anvineba20a72002-04-30 20:53:55 +0000489/*
490 * Get a parameter for a command line option.
491 * First arg must be in the form of e.g. -f...
492 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800493static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000494{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800495 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400496 if (p[2]) /* the parameter's in the option */
497 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800499 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400502 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504 return NULL;
505}
506
H. Peter Anvindc242712007-11-18 11:55:10 -0800507/*
508 * Copy a filename
509 */
510static void copy_filename(char *dst, const char *src)
511{
512 size_t len = strlen(src);
513
514 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800515 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400516 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800517 }
518 strncpy(dst, src, FILENAME_MAX);
519}
520
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700521/*
522 * Convert a string to Make-safe form
523 */
524static char *quote_for_make(const char *str)
525{
526 const char *p;
527 char *os, *q;
528
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400529 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700530 size_t nbs = 0;
531
532 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400533 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700534
535 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400536 switch (*p) {
537 case ' ':
538 case '\t':
539 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
540 n += nbs + 2;
541 nbs = 0;
542 break;
543 case '$':
544 case '#':
545 nbs = 0;
546 n += 2;
547 break;
548 case '\\':
549 nbs++;
550 n++;
551 break;
552 default:
553 nbs = 0;
554 n++;
555 break;
556 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700557 }
558
559 /* Convert N backslashes at the end of filename to 2N backslashes */
560 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400561 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700562
563 os = q = nasm_malloc(n);
564
565 nbs = 0;
566 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400567 switch (*p) {
568 case ' ':
569 case '\t':
570 while (nbs--)
571 *q++ = '\\';
572 *q++ = '\\';
573 *q++ = *p;
574 break;
575 case '$':
576 *q++ = *p;
577 *q++ = *p;
578 nbs = 0;
579 break;
580 case '#':
581 *q++ = '\\';
582 *q++ = *p;
583 nbs = 0;
584 break;
585 case '\\':
586 *q++ = *p;
587 nbs++;
588 break;
589 default:
590 *q++ = *p;
591 nbs = 0;
592 break;
593 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700594 }
595 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400596 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700597
598 *q = '\0';
599
600 return os;
601}
602
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000604 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000606};
607
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100608enum text_options {
609 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800610 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100611};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800612static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 {"prefix", OPT_PREFIX},
614 {"postfix", OPT_POSTFIX},
615 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616};
617
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400618static void show_version(void)
619{
620 printf("NASM version %s compiled on %s%s\n",
621 nasm_version, nasm_date, nasm_compile_options);
622 exit(0);
623}
624
H. Peter Anvin423e3812007-11-15 17:12:29 -0800625static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700626static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000628 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800629 int i;
630 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000631
632 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800633 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000634
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400636 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
637 /* These parameters take values */
638 if (!(param = get_param(p, q, &advance)))
639 return advance;
640 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800641
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 switch (p[1]) {
643 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700644 if (pass == 1)
645 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700647
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400648 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700649 if (pass == 2)
650 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400651 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700652
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400653 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700654 if (pass == 1) {
655 ofmt = ofmt_find(param, &ofmt_alias);
656 if (!ofmt) {
657 nasm_fatal(ERR_NOFILE | ERR_USAGE,
658 "unrecognised output format `%s' - "
659 "use -hf for a list", param);
660 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400661 }
662 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700663
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400664 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700665 if (pass == 2) {
666 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700667
H. Peter Anvin55568c12016-10-03 19:46:49 -0700668 if (!*param) {
669 /* Naked -O == -Ox */
670 optimizing = MAX_OPTIMIZE;
671 } else {
672 while (*param) {
673 switch (*param) {
674 case '0': case '1': case '2': case '3': case '4':
675 case '5': case '6': case '7': case '8': case '9':
676 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700677
H. Peter Anvin55568c12016-10-03 19:46:49 -0700678 /* -O0 -> optimizing == -1, 0.98 behaviour */
679 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
680 if (opt < 2)
681 optimizing = opt - 1;
682 else
683 optimizing = opt;
684 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700685
H. Peter Anvin55568c12016-10-03 19:46:49 -0700686 case 'v':
687 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400688 param++;
689 opt_verbose_info = true;
690 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700691
H. Peter Anvin55568c12016-10-03 19:46:49 -0700692 case 'x':
693 param++;
694 optimizing = MAX_OPTIMIZE;
695 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700696
H. Peter Anvin55568c12016-10-03 19:46:49 -0700697 default:
698 nasm_fatal(0,
699 "unknown optimization option -O%c\n",
700 *param);
701 break;
702 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700704 if (optimizing > MAX_OPTIMIZE)
705 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400706 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 }
708 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800709
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400710 case 'p': /* pre-include */
711 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700712 if (pass == 2)
713 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400714 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800715
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400716 case 'd': /* pre-define */
717 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700718 if (pass == 2)
719 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400720 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800721
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 case 'u': /* un-define */
723 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700724 if (pass == 2)
725 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'i': /* include search path */
729 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700730 if (pass == 2)
731 preproc->include_path(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 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700735 if (pass == 2)
736 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700740 if (pass == 1)
741 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800743
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700745 if (pass == 2) {
746 using_debug_info = true;
747 debug_format = param;
748 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700752 if (pass == 1) {
753 if (nasm_stricmp("vc", param) == 0)
754 nasm_set_verror(nasm_verror_vc);
755 else if (nasm_stricmp("gnu", param) == 0)
756 nasm_set_verror(nasm_verror_gnu);
757 else
758 nasm_fatal(ERR_NOFILE | ERR_USAGE,
759 "unrecognized error reporting format `%s'",
760 param);
761 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800763
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700765 if (pass == 2) {
766 using_debug_info = true;
767 if (p[2])
768 debug_format = nasm_skip_spaces(p + 2);
769 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 case 'h':
773 printf
774 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
775 "[-l listfile]\n"
776 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400777 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800778 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700780 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700782 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400783 " -MG d:o, missing files assumed generated\n"
784 " -MF <file> set Makefile dependency file\n"
785 " -MD <file> assemble and generate dependencies\n"
786 " -MT <file> dependency target name\n"
787 " -MQ <file> dependency target name (quoted)\n"
788 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700789 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800791 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800793 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400794 " -o outfile write output to an outfile\n\n"
795 " -f format select an output format\n\n"
796 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 " -I<path> adds a pathname to the include file path\n");
798 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700799 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400800 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400801 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400802 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 " -P<file> pre-includes a file\n"
804 " -D<macro>[=<value>] pre-defines a macro\n"
805 " -U<macro> undefines a macro\n"
806 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800807 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400808 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800809 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
810 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400811 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800812 " these options prepend or append the given string\n"
813 " to all extern and global variables\n"
814 "\n"
815 "Response files should contain command line parameters,\n"
816 "one per line.\n"
817 "\n"
818 "Warnings for the -W/-w options:\n");
819 for (i = 0; i <= ERR_WARN_ALL; i++)
820 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700821 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800822 i == ERR_WARN_ALL ? "\n" :
823 warnings[i].enabled ? " (default on)" :
824 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800826 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 " (`*' denotes default):\n");
828 ofmt_list(ofmt, stdout);
829 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800830 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 printf("For a list of debug formats, use -f <form> -y.\n");
832 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400833 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800835
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 case 'y':
837 printf("\nvalid debug formats for '%s' output format are"
838 " ('*' denotes default):\n", ofmt->shortname);
839 dfmt_list(ofmt, stdout);
840 exit(0);
841 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700844 if (pass == 2)
845 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400849 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'e': /* preprocess only */
853 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700854 if (pass == 1)
855 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800857
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700859 if (pass == 1)
860 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800862
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800863 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700865 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800866 if (!set_warning_status(param)) {
867 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
868 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700869 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400870 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800871 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800872
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700874 if (pass == 2) {
875 switch (p[2]) {
876 case 0:
877 operating_mode = OP_DEPEND;
878 break;
879 case 'G':
880 operating_mode = OP_DEPEND;
881 depend_missing_ok = true;
882 break;
883 case 'P':
884 depend_emit_phony = true;
885 break;
886 case 'D':
887 operating_mode = OP_NORMAL;
888 depend_file = q;
889 advance = true;
890 break;
891 case 'F':
892 depend_file = q;
893 advance = true;
894 break;
895 case 'T':
896 depend_target = q;
897 advance = true;
898 break;
899 case 'Q':
900 depend_target = quote_for_make(q);
901 advance = true;
902 break;
903 default:
904 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
905 "unknown dependency option `-M%c'", p[2]);
906 break;
907 }
908 if (advance && (!q || !q[0])) {
909 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
910 "option `-M%c' requires a parameter", p[2]);
911 break;
912 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000915
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 case '-':
917 {
918 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000919
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 if (p[2] == 0) { /* -- => stop processing options */
921 stopoptions = 1;
922 break;
923 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400924
925 if (!nasm_stricmp(p, "--v"))
926 show_version();
927
Andy Willis3f546032016-09-13 00:02:21 +0300928 if (!nasm_stricmp(p, "--version"))
929 show_version();
930
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 for (s = 0; textopts[s].label; s++) {
932 if (!nasm_stricmp(p + 2, textopts[s].label)) {
933 break;
934 }
935 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 switch (s) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 case OPT_PREFIX:
939 case OPT_POSTFIX:
940 {
941 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400942 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 ERR_USAGE,
944 "option `--%s' requires an argument",
945 p + 2);
946 break;
947 } else {
948 advance = 1, param = q;
949 }
950
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100951 switch (s) {
952 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700953 if (pass == 2)
954 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100956 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (pass == 2)
958 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100959 break;
960 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700961 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 break;
963 }
964 break;
965 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 default:
968 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400969 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 "unrecognised option `--%s'", p + 2);
971 break;
972 }
973 }
974 break;
975 }
976
977 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700978 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
979 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 break;
981 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700982 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400984 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800986 } else {
987 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400988 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000989 }
990
991 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000992}
993
H. Peter Anvineba20a72002-04-30 20:53:55 +0000994#define ARG_BUF_DELTA 128
995
H. Peter Anvin55568c12016-10-03 19:46:49 -0700996static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000997{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000998 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000999 int bufsize, prevargsize;
1000
1001 bufsize = prevargsize = ARG_BUF_DELTA;
1002 buffer = nasm_malloc(ARG_BUF_DELTA);
1003 prevarg = nasm_malloc(ARG_BUF_DELTA);
1004 prevarg[0] = '\0';
1005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 p = buffer;
1008 while (1) { /* Loop to handle long lines */
1009 q = fgets(p, bufsize - (p - buffer), rfile);
1010 if (!q)
1011 break;
1012 p += strlen(p);
1013 if (p > buffer && p[-1] == '\n')
1014 break;
1015 if (p - buffer > bufsize - 10) {
1016 int offset;
1017 offset = p - buffer;
1018 bufsize += ARG_BUF_DELTA;
1019 buffer = nasm_realloc(buffer, bufsize);
1020 p = buffer + offset;
1021 }
1022 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001023
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 if (!q && p == buffer) {
1025 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001026 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 nasm_free(buffer);
1028 nasm_free(prevarg);
1029 return;
1030 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001031
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 /*
1033 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1034 * them are present at the end of the line.
1035 */
1036 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001037
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001038 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001040
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001041 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001042
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001045
Charles Crayne192d5b52007-10-18 19:02:42 -07001046 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 prevargsize += ARG_BUF_DELTA;
1048 prevarg = nasm_realloc(prevarg, prevargsize);
1049 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001050 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001051 }
1052}
1053
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001054/* Function to process args from a string of args, rather than the
1055 * argv array. Used by the environment variable and response file
1056 * processing.
1057 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001058static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001060 char *p, *q, *arg, *prevarg;
1061 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001062
1063 p = args;
1064 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001066 arg = NULL;
1067 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 q = p;
1069 while (*p && *p != separator)
1070 p++;
1071 while (*p == separator)
1072 *p++ = '\0';
1073 prevarg = arg;
1074 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001075 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001077 }
1078 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001079 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001080}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081
H. Peter Anvin55568c12016-10-03 19:46:49 -07001082static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001083{
1084 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001085 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001086 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001087 perror(file);
1088 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001089 }
1090 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001091 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001092 }
1093 fclose(f);
1094}
1095
H. Peter Anvin55568c12016-10-03 19:46:49 -07001096static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001097{
1098 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001099 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001100 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001101
Charles Craynefcce07f2007-09-30 22:15:36 -07001102 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001103
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001104 /* Initialize all the warnings to their default state */
1105 for (i = 0; i < ERR_WARN_ALL; i++) {
1106 warning_state_init[i] = warning_state[i] =
1107 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1108 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001109
1110 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001111 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001112 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001113 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001114 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001116 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001118 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001119
1120 /*
1121 * Now process the actual command line.
1122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001124 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 argv++;
1126 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001127 /*
1128 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 * arguments like the environment variable. This allows us
1130 * to have multiple arguments on a single line, which is
1131 * different to the -@resp file processing below for regular
1132 * NASM.
1133 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001134 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 argc--;
1136 argv++;
1137 }
1138 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001139 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001140 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001141 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001142 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001143 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 fclose(rfile);
1145 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001146 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 "unable to open response file `%s'", p);
1148 }
1149 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001150 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001151 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001152 }
1153
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001154 /*
1155 * Look for basic command line typos. This definitely doesn't
1156 * catch all errors, but it might help cases of fumbled fingers.
1157 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001158 if (pass != 2)
1159 return;
1160
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001162 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001163 "no input file specified");
1164 else if (!strcmp(inname, errname) ||
1165 !strcmp(inname, outname) ||
1166 !strcmp(inname, listname) ||
1167 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001168 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001169 "file `%s' is both input and output file",
1170 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001171
H. Peter Anvin70653092007-10-19 14:42:29 -07001172 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001173 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001174 if (!error_file) {
1175 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001176 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001177 "cannot open file `%s' for error messages",
1178 errname);
1179 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001180 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001181}
1182
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001183static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001184{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001185 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001187 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001188 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001189 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001190 uint64_t prev_offset_changed;
1191 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001192
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001193 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001194 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195
Charles Craynec1905c22008-09-11 18:54:06 -07001196 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001197 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001199
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001200 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001201 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1202 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001204 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205
H. Peter Anvincac0b192017-03-28 16:12:30 -07001206 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 cpu = cmd_cpu;
1208 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001209 lfmt->init(listname);
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001210 } else if (passn == 1 && *listname) {
1211 /* Remove the list file in case we die before the output pass */
1212 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001214 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001215 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001216 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001217 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 saa_rewind(forwrefs);
1219 forwref = saa_rstruct(forwrefs);
1220 raa_free(offsets);
1221 offsets = raa_init();
1222 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001223 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001224
1225 /* Revert all warnings to the default state */
1226 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001229 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001230 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001231 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 while ((line = preproc->getline())) {
1234 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001235
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001236 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001237 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001238 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001239 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001240 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001241 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001242
H. Peter Anvinc7131682017-03-07 17:45:01 -08001243 /* Not a directive, or even something that starts with [ */
1244
1245 parse_line(pass1, line, &output_ins, def_label);
1246
1247 if (optimizing > 0) {
1248 if (forwref != NULL && globallineno == forwref->lineno) {
1249 output_ins.forw_ref = true;
1250 do {
1251 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1252 forwref = saa_rstruct(forwrefs);
1253 } while (forwref != NULL
1254 && forwref->lineno == globallineno);
1255 } else
1256 output_ins.forw_ref = false;
1257
1258 if (output_ins.forw_ref) {
1259 if (passn == 1) {
1260 for (i = 0; i < output_ins.operands; i++) {
1261 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1262 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1263 fwinf->lineno = globallineno;
1264 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001265 }
1266 }
1267 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001268 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001269 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001270
H. Peter Anvinc7131682017-03-07 17:45:01 -08001271 /* forw_ref */
1272 if (output_ins.opcode == I_EQU) {
1273 if (pass1 == 1) {
1274 /*
1275 * Special `..' EQUs get processed in pass two,
1276 * except `..@' macro-processor EQUs which are done
1277 * in the normal place.
1278 */
1279 if (!output_ins.label)
1280 nasm_error(ERR_NONFATAL,
1281 "EQU not preceded by label");
1282
1283 else if (output_ins.label[0] != '.' ||
1284 output_ins.label[1] != '.' ||
1285 output_ins.label[2] == '@') {
1286 if (output_ins.operands == 1 &&
1287 (output_ins.oprs[0].type & IMMEDIATE) &&
1288 output_ins.oprs[0].wrt == NO_SEG) {
1289 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1290 def_label(output_ins.label,
1291 output_ins.oprs[0].segment,
1292 output_ins.oprs[0].offset, NULL,
1293 false, isext);
1294 } else if (output_ins.operands == 2
1295 && (output_ins.oprs[0].type & IMMEDIATE)
1296 && (output_ins.oprs[0].type & COLON)
1297 && output_ins.oprs[0].segment == NO_SEG
1298 && output_ins.oprs[0].wrt == NO_SEG
1299 && (output_ins.oprs[1].type & IMMEDIATE)
1300 && output_ins.oprs[1].segment == NO_SEG
1301 && output_ins.oprs[1].wrt == NO_SEG) {
1302 def_label(output_ins.label,
1303 output_ins.oprs[0].offset | SEG_ABS,
1304 output_ins.oprs[1].offset,
1305 NULL, false, false);
1306 } else
1307 nasm_error(ERR_NONFATAL,
1308 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001309 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001310 } else {
1311 /*
1312 * Special `..' EQUs get processed here, except
1313 * `..@' macro processor EQUs which are done above.
1314 */
1315 if (output_ins.label[0] == '.' &&
1316 output_ins.label[1] == '.' &&
1317 output_ins.label[2] != '@') {
1318 if (output_ins.operands == 1 &&
1319 (output_ins.oprs[0].type & IMMEDIATE)) {
1320 define_label(output_ins.label,
1321 output_ins.oprs[0].segment,
1322 output_ins.oprs[0].offset,
1323 NULL, false, false);
1324 } else if (output_ins.operands == 2
1325 && (output_ins.oprs[0].type & IMMEDIATE)
1326 && (output_ins.oprs[0].type & COLON)
1327 && output_ins.oprs[0].segment == NO_SEG
1328 && (output_ins.oprs[1].type & IMMEDIATE)
1329 && output_ins.oprs[1].segment == NO_SEG) {
1330 define_label(output_ins.label,
1331 output_ins.oprs[0].offset | SEG_ABS,
1332 output_ins.oprs[1].offset,
1333 NULL, false, false);
1334 } else
1335 nasm_error(ERR_NONFATAL,
1336 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001337 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001338 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001339 } else { /* instruction isn't an EQU */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340
H. Peter Anvinc7131682017-03-07 17:45:01 -08001341 if (pass1 == 1) {
H. Peter Anvincac0b192017-03-28 16:12:30 -07001342 int64_t l = insn_size(location.segment, offs, globalbits,
H. Peter Anvinc7131682017-03-07 17:45:01 -08001343 &output_ins);
1344 l *= output_ins.times;
1345
1346 /* if (using_debug_info) && output_ins.opcode != -1) */
1347 if (using_debug_info)
1348 { /* fbk 03/25/01 */
1349 /* this is done here so we can do debug type info */
1350 int32_t typeinfo =
1351 TYS_ELEMENTS(output_ins.operands);
1352 switch (output_ins.opcode) {
1353 case I_RESB:
1354 typeinfo =
1355 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001356 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001357 case I_RESW:
1358 typeinfo =
1359 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001360 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001361 case I_RESD:
1362 typeinfo =
1363 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001364 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001365 case I_RESQ:
1366 typeinfo =
1367 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001368 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001369 case I_REST:
1370 typeinfo =
1371 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001372 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001373 case I_RESO:
1374 typeinfo =
1375 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001376 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001377 case I_RESY:
1378 typeinfo =
1379 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001380 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001381 case I_DB:
1382 typeinfo |= TY_BYTE;
1383 break;
1384 case I_DW:
1385 typeinfo |= TY_WORD;
1386 break;
1387 case I_DD:
1388 if (output_ins.eops_float)
1389 typeinfo |= TY_FLOAT;
1390 else
1391 typeinfo |= TY_DWORD;
1392 break;
1393 case I_DQ:
1394 typeinfo |= TY_QWORD;
1395 break;
1396 case I_DT:
1397 typeinfo |= TY_TBYTE;
1398 break;
1399 case I_DO:
1400 typeinfo |= TY_OWORD;
1401 break;
1402 case I_DY:
1403 typeinfo |= TY_YWORD;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001404 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001405 default:
H. Peter Anvinc7131682017-03-07 17:45:01 -08001406 typeinfo = TY_LABEL;
1407
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001408 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001409
1410 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001411 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001412 if (l != -1) {
1413 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001414 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001416 /*
1417 * else l == -1 => invalid instruction, which will be
1418 * flagged as an error on pass 2
1419 */
1420
1421 } else {
H. Peter Anvincac0b192017-03-28 16:12:30 -07001422 offs += assemble(location.segment, offs, globalbits, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001423 set_curr_offs(offs);
1424
1425 }
1426 } /* not an EQU */
1427 cleanup_insn(&output_ins);
1428
1429 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001431 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001433
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001434 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001435 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001436 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 if (pass1 == 1)
1439 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001440
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001441 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001443 } else if (global_offset_changed &&
1444 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001445 prev_offset_changed = global_offset_changed;
1446 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001447 } else {
1448 stall_count++;
1449 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001450
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 if (terminate_after_phase)
1452 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001453
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001454 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001455 /* We get here if the labels don't converge
1456 * Example: FOO equ FOO + 1
1457 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001458 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001459 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001460 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001461 nasm_error(ERR_NONFATAL,
1462 "Possible causes: recursive EQUs, macro abuse.");
1463 break;
1464 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001465 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001466
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001468 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001469 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001470 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001471 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1472 }
1473}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001474
Ed Berosetfa771012002-06-09 20:56:40 +00001475/**
1476 * gnu style error reporting
1477 * This function prints an error message to error_file in the
1478 * style used by GNU. An example would be:
1479 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001480 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001481 * which the error occurs (or is detected) and "error:" is one of
1482 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001483 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001484 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001485 *
1486 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001487 * @param fmt the printf style format string
1488 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001489static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001490{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001491 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001492 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001493
Ed Berosetfa771012002-06-09 20:56:40 +00001494 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001496
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001497 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001498 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001499
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001500 if (!skip_this_pass(severity)) {
1501 if (currentfile) {
1502 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001503 } else {
1504 fputs("nasm: ", error_file);
1505 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001506 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001507
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001508 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001509}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001510
Ed Berosetfa771012002-06-09 20:56:40 +00001511/**
1512 * MS style error reporting
1513 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001514 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001515 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001516 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001517 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001518 * which the error occurs (or is detected) and "error:" is one of
1519 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001520 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001521 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001522 *
1523 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001524 * @param fmt the printf style format string
1525 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001526static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001527{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001528 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001529 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001530
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 if (is_suppressed_warning(severity))
1532 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001533
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001534 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001536
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001537 if (!skip_this_pass(severity)) {
1538 if (currentfile) {
1539 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001540 } else {
1541 fputs("nasm: ", error_file);
1542 }
Ed Berosetfa771012002-06-09 20:56:40 +00001543 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001544
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001545 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001546}
1547
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001548/*
1549 * check to see if this is a suppressable warning
1550 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001551static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001552{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001553 /* Not a warning at all */
1554 if ((severity & ERR_MASK) != ERR_WARNING)
1555 return false;
1556
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001557 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001558}
1559
Ed Berosetfa771012002-06-09 20:56:40 +00001560/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001561 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001562 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001563 * not in pass 1
1564 *
1565 * @param severity the severity of the warning or error
1566 * @return true if we should abort error/warning printing
1567 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001568static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001569{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001570 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001571 if (is_valid_warning(severity))
1572 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1573 else
1574 return false;
1575}
1576
1577static bool warning_is_error(int severity)
1578{
1579 if (is_valid_warning(severity))
1580 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001581 else
1582 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001583}
1584
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001585static bool skip_this_pass(int severity)
1586{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001587 /*
1588 * See if it's a pass-specific error or warning which should be skipped.
1589 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1590 * they cannot be resumed from.
1591 */
1592 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001593 return false;
1594
H. Peter Anvin934f0472016-05-09 12:00:19 -07001595 /*
1596 * passn is 1 on the very first pass only.
1597 * pass0 is 2 on the code-generation (final) pass only.
1598 * These are the passes we care about in this case.
1599 */
1600 return (((severity & ERR_PASS1) && passn != 1) ||
1601 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001602}
1603
Ed Berosetfa771012002-06-09 20:56:40 +00001604/**
1605 * common error reporting
1606 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001607 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001608 * specific error message to error_file and may or may not return. It
1609 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001610 *
1611 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001612 * @param fmt the printf style format string
1613 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001614static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001615{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001616 char msg[1024];
1617 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001618
H. Peter Anvin7df04172008-06-10 18:27:38 -07001619 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001621 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
1623 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001624 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 break;
1626 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001627 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 break;
1629 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001630 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 break;
1632 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001633 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001635 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001636 pfx = "";
1637 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001638 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001639
H. Peter Anvin934f0472016-05-09 12:00:19 -07001640 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001641 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001642 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001643 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1644 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001645
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001646 if (!skip_this_pass(severity))
1647 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001648
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001649 /* Are we recursing from error_list_macros? */
1650 if (severity & ERR_PP_LISTMACRO)
1651 return;
1652
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001653 /*
1654 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001655 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001656 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001657 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001658
H. Peter Anvin8f622462017-04-02 19:02:29 -07001659 if (skip_this_pass(severity))
1660 return;
1661
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001662 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001663 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001664
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001665 preproc->error_list_macros(severity);
1666
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001667 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 case ERR_DEBUG:
1669 /* no further action, by definition */
1670 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001671 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001672 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001673 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001674 terminate_after_phase = true;
1675 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001677 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 break;
1679 case ERR_FATAL:
1680 if (ofile) {
1681 fclose(ofile);
1682 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001683 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 }
1685 if (want_usage)
1686 usage();
1687 exit(1); /* instantly die */
1688 break; /* placate silly compilers */
1689 case ERR_PANIC:
1690 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001691 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001692 if (ofile) {
1693 fclose(ofile);
1694 remove(outname);
1695 ofile = NULL;
1696 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 exit(3);
1698 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001699 }
1700}
1701
H. Peter Anvin734b1882002-04-30 21:01:08 +00001702static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001703{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001704 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001705}