blob: fb5e9541b302c935247ced3b80548be714b451c8 [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
Charles Craynec1905c22008-09-11 18:54:06 -0700117int64_t global_offset_changed; /* referenced in labels.c */
118int64_t prev_offset_changed;
119int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800121struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800122bool in_absolute; /* Flag we are in ABSOLUTE seg */
123struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000124
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000126
H. Peter Anvine2c80182005-01-15 22:15:51 +0000127static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700128static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129
H. Peter Anvine7469712016-02-18 02:20:59 -0800130static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400131
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400132#define OP_NORMAL (1u << 0)
133#define OP_PREPROCESS (1u << 1)
134#define OP_DEPEND (1u << 2)
135
136static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400137
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700138/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700139static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700140static bool depend_missing_ok = false;
141static const char *depend_target = NULL;
142static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700144static bool want_usage;
145static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800146bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000147
H. Peter Anvin55340992012-09-09 17:09:00 -0700148static char *quote_for_make(const char *str);
149
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400150static int64_t get_curr_offs(void)
151{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800152 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400153}
154
155static void set_curr_offs(int64_t l_off)
156{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800157 if (in_absolute)
158 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400159 else
160 offsets = raa_write(offsets, location.segment, l_off);
161}
162
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000163static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000164{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000165 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000166 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800167 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000168 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000169 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000170}
171
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800172/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800173static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800174{
175 int64_t t;
176 int64_t y = tm->tm_year;
177
178 /* See IEEE 1003.1:2004, section 4.14 */
179
180 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
181 t += tm->tm_yday;
182 t *= 24;
183 t += tm->tm_hour;
184 t *= 60;
185 t += tm->tm_min;
186 t *= 60;
187 t += tm->tm_sec;
188
189 return t;
190}
191
192static void define_macros_early(void)
193{
194 char temp[128];
195 struct tm lt, *lt_p, gm, *gm_p;
196 int64_t posix_time;
197
198 lt_p = localtime(&official_compile_time);
199 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400200 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800201
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400202 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
203 preproc->pre_define(temp);
204 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
205 preproc->pre_define(temp);
206 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
207 preproc->pre_define(temp);
208 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
209 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800210 }
211
212 gm_p = gmtime(&official_compile_time);
213 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400214 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800215
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400216 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
217 preproc->pre_define(temp);
218 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
219 preproc->pre_define(temp);
220 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
221 preproc->pre_define(temp);
222 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
223 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800224 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700225
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800226 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800227 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800228 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800229 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800230 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400231 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800232
233 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400234 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
235 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800236 }
237}
238
239static void define_macros_late(void)
240{
241 char temp[128];
242
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400243 /*
244 * In case if output format is defined by alias
245 * we have to put shortname of the alias itself here
246 * otherwise ABI backward compatibility gets broken.
247 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400248 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400249 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400250 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800251}
252
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700253static void emit_dependencies(StrList *list)
254{
255 FILE *deps;
256 int linepos, len;
257 StrList *l, *nl;
258
259 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700260 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400261 if (!deps) {
262 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
263 "unable to write dependency file `%s'", depend_file);
264 return;
265 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700266 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400267 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700268 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700269
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700270 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400271 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700272 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400273 len = strlen(file);
274 if (linepos + len > 62 && linepos > 1) {
275 fprintf(deps, " \\\n ");
276 linepos = 1;
277 }
278 fprintf(deps, " %s", file);
279 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700280 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700281 }
282 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700283
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400284 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400285 if (depend_emit_phony)
286 fprintf(deps, "%s:\n\n", l->str);
287 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700288 }
289
290 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400291 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700292}
293
H. Peter Anvin038d8612007-04-12 16:54:50 +0000294int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000295{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700297
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800298 time(&official_compile_time);
299
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400300 iflag_set(&cpu, IF_PLEVEL);
301 iflag_set(&cmd_cpu, IF_PLEVEL);
302
Charles Crayne2581c862008-09-10 19:21:52 -0700303 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700304 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400305 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000306
H. Peter Anvin97e15752007-09-25 08:47:47 -0700307 error_file = stderr;
308
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700309 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700310 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700311
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000312 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000313 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000314
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400316 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000317
H. Peter Anvin55568c12016-10-03 19:46:49 -0700318 parse_cmdline(argc, argv, 1);
319 if (terminate_after_phase) {
320 if (want_usage)
321 usage();
322 return 1;
323 }
324
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700325 /*
326 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700327 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700328 */
329 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800330 define_macros_early();
331
H. Peter Anvin55568c12016-10-03 19:46:49 -0700332 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 if (terminate_after_phase) {
334 if (want_usage)
335 usage();
336 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000337 }
338
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800339 if (!using_debug_info) {
340 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800341 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800342 } else if (!debug_format) {
343 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800344 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800345 } else {
346 dfmt = dfmt_find(ofmt, debug_format);
347 if (!dfmt) {
348 nasm_fatal(ERR_NOFILE | ERR_USAGE,
349 "unrecognized debug format `%s' for"
350 " output format `%s'",
351 debug_format, ofmt->shortname);
352 }
353 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000354
H. Peter Anvin76690a12002-04-30 20:52:49 +0000355 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400356 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000358 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800359 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000360
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400361 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700362 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400363 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700364
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400365 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000366 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700367
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400368 if (depend_missing_ok)
369 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700370
H. Peter Anvin130736c2016-02-17 20:27:41 -0800371 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400373 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 while ((line = preproc->getline()))
376 nasm_free(line);
377 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400378 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000379 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700380 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000381 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700385 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800387 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 "unable to open output file `%s'",
389 outname);
390 } else
391 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700393 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000394
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400395 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800396 preproc->reset(inname, 3, depend_ptr);
397 memcpy(warning_on, warning_on_global,
398 (ERR_WARN_MAX+1) * sizeof(bool));
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 Anvin972079f2008-09-30 17:01:23 -0700631 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000632
633 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800634 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000635
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400637 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
638 /* These parameters take values */
639 if (!(param = get_param(p, q, &advance)))
640 return advance;
641 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 switch (p[1]) {
644 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700645 if (pass == 1)
646 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700648
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400649 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700650 if (pass == 2)
651 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400652 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700653
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400654 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700655 if (pass == 1) {
656 ofmt = ofmt_find(param, &ofmt_alias);
657 if (!ofmt) {
658 nasm_fatal(ERR_NOFILE | ERR_USAGE,
659 "unrecognised output format `%s' - "
660 "use -hf for a list", param);
661 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400662 }
663 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700664
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400665 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700666 if (pass == 2) {
667 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700668
H. Peter Anvin55568c12016-10-03 19:46:49 -0700669 if (!*param) {
670 /* Naked -O == -Ox */
671 optimizing = MAX_OPTIMIZE;
672 } else {
673 while (*param) {
674 switch (*param) {
675 case '0': case '1': case '2': case '3': case '4':
676 case '5': case '6': case '7': case '8': case '9':
677 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700678
H. Peter Anvin55568c12016-10-03 19:46:49 -0700679 /* -O0 -> optimizing == -1, 0.98 behaviour */
680 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
681 if (opt < 2)
682 optimizing = opt - 1;
683 else
684 optimizing = opt;
685 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700686
H. Peter Anvin55568c12016-10-03 19:46:49 -0700687 case 'v':
688 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400689 param++;
690 opt_verbose_info = true;
691 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700692
H. Peter Anvin55568c12016-10-03 19:46:49 -0700693 case 'x':
694 param++;
695 optimizing = MAX_OPTIMIZE;
696 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700697
H. Peter Anvin55568c12016-10-03 19:46:49 -0700698 default:
699 nasm_fatal(0,
700 "unknown optimization option -O%c\n",
701 *param);
702 break;
703 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400704 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700705 if (optimizing > MAX_OPTIMIZE)
706 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400708 }
709 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800710
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400711 case 'p': /* pre-include */
712 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700713 if (pass == 2)
714 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400715 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800716
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 case 'd': /* pre-define */
718 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700719 if (pass == 2)
720 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400721 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800722
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400723 case 'u': /* un-define */
724 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700725 if (pass == 2)
726 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'i': /* include search path */
730 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700731 if (pass == 2)
732 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400733 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700736 if (pass == 2)
737 copy_filename(listname, 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 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700741 if (pass == 1)
742 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400743 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800744
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400745 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700746 if (pass == 2) {
747 using_debug_info = true;
748 debug_format = param;
749 }
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 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700753 if (pass == 1) {
754 if (nasm_stricmp("vc", param) == 0)
755 nasm_set_verror(nasm_verror_vc);
756 else if (nasm_stricmp("gnu", param) == 0)
757 nasm_set_verror(nasm_verror_gnu);
758 else
759 nasm_fatal(ERR_NOFILE | ERR_USAGE,
760 "unrecognized error reporting format `%s'",
761 param);
762 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700766 if (pass == 2) {
767 using_debug_info = true;
768 if (p[2])
769 debug_format = nasm_skip_spaces(p + 2);
770 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800772
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 case 'h':
774 printf
775 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
776 "[-l listfile]\n"
777 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400778 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800779 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700781 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700783 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400784 " -MG d:o, missing files assumed generated\n"
785 " -MF <file> set Makefile dependency file\n"
786 " -MD <file> assemble and generate dependencies\n"
787 " -MT <file> dependency target name\n"
788 " -MQ <file> dependency target name (quoted)\n"
789 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700790 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800792 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800794 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400795 " -o outfile write output to an outfile\n\n"
796 " -f format select an output format\n\n"
797 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 " -I<path> adds a pathname to the include file path\n");
799 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700800 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400801 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400802 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400803 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 " -P<file> pre-includes a file\n"
805 " -D<macro>[=<value>] pre-defines a macro\n"
806 " -U<macro> undefines a macro\n"
807 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800808 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400809 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400810 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400811 "--prefix,--postfix\n"
812 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100813 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800814 "Warnings:\n");
815 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700817 warnings[i].name, warnings[i].help,
818 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 printf
820 ("\nresponse files should contain command line parameters"
821 ", one per line.\n");
822 if (p[2] == 'f') {
823 printf("\nvalid output formats for -f are"
824 " (`*' denotes default):\n");
825 ofmt_list(ofmt, stdout);
826 } else {
827 printf("\nFor a list of valid output formats, use -hf.\n");
828 printf("For a list of debug formats, use -f <form> -y.\n");
829 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400830 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 case 'y':
834 printf("\nvalid debug formats for '%s' output format are"
835 " ('*' denotes default):\n", ofmt->shortname);
836 dfmt_list(ofmt, stdout);
837 exit(0);
838 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700841 if (pass == 2)
842 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400846 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800848
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400849 case 'e': /* preprocess only */
850 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700851 if (pass == 1)
852 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700856 if (pass == 1)
857 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800859
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700861 if (pass == 2) {
862 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
863 do_warn = false;
864 param += 3;
865 } else {
866 do_warn = true;
867 }
868 goto set_warning;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400869 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700870 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 case 'w':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700873 if (pass == 2) {
874 if (param[0] != '+' && param[0] != '-') {
875 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
876 "invalid option to `-w'");
877 break;
878 }
879 do_warn = (param[0] == '+');
880 param++;
881 goto set_warning;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700883 break;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400884
885set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400886 for (i = 0; i <= ERR_WARN_MAX; i++) {
887 if (!nasm_stricmp(param, warnings[i].name))
888 break;
889 }
890 if (i <= ERR_WARN_MAX) {
891 warning_on_global[i] = do_warn;
892 } else if (!nasm_stricmp(param, "all")) {
893 for (i = 1; i <= ERR_WARN_MAX; i++)
894 warning_on_global[i] = do_warn;
895 } else if (!nasm_stricmp(param, "none")) {
896 for (i = 1; i <= ERR_WARN_MAX; i++)
897 warning_on_global[i] = !do_warn;
898 } else {
H. Peter Anvinb4f734f2016-05-09 12:13:08 -0700899 /* Ignore invalid warning names; forward compatibility */
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700904 if (pass == 2) {
905 switch (p[2]) {
906 case 0:
907 operating_mode = OP_DEPEND;
908 break;
909 case 'G':
910 operating_mode = OP_DEPEND;
911 depend_missing_ok = true;
912 break;
913 case 'P':
914 depend_emit_phony = true;
915 break;
916 case 'D':
917 operating_mode = OP_NORMAL;
918 depend_file = q;
919 advance = true;
920 break;
921 case 'F':
922 depend_file = q;
923 advance = true;
924 break;
925 case 'T':
926 depend_target = q;
927 advance = true;
928 break;
929 case 'Q':
930 depend_target = quote_for_make(q);
931 advance = true;
932 break;
933 default:
934 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
935 "unknown dependency option `-M%c'", p[2]);
936 break;
937 }
938 if (advance && (!q || !q[0])) {
939 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
940 "option `-M%c' requires a parameter", p[2]);
941 break;
942 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 case '-':
947 {
948 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000949
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 if (p[2] == 0) { /* -- => stop processing options */
951 stopoptions = 1;
952 break;
953 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400954
955 if (!nasm_stricmp(p, "--v"))
956 show_version();
957
Andy Willis3f546032016-09-13 00:02:21 +0300958 if (!nasm_stricmp(p, "--version"))
959 show_version();
960
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 for (s = 0; textopts[s].label; s++) {
962 if (!nasm_stricmp(p + 2, textopts[s].label)) {
963 break;
964 }
965 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 switch (s) {
968
969 case OPT_PREFIX:
970 case OPT_POSTFIX:
971 {
972 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400973 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 ERR_USAGE,
975 "option `--%s' requires an argument",
976 p + 2);
977 break;
978 } else {
979 advance = 1, param = q;
980 }
981
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100982 switch (s) {
983 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700984 if (pass == 2)
985 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100987 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700988 if (pass == 2)
989 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100990 break;
991 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800992 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100993 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 break;
995 }
996 break;
997 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800998
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 default:
1000 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001001 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 "unrecognised option `--%s'", p + 2);
1003 break;
1004 }
1005 }
1006 break;
1007 }
1008
1009 default:
1010 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001011 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 "unrecognised option `-%c'", p[1]);
1013 break;
1014 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001015 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001017 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001019 } else {
1020 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001021 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001022 }
1023
1024 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001025}
1026
H. Peter Anvineba20a72002-04-30 20:53:55 +00001027#define ARG_BUF_DELTA 128
1028
H. Peter Anvin55568c12016-10-03 19:46:49 -07001029static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001031 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001032 int bufsize, prevargsize;
1033
1034 bufsize = prevargsize = ARG_BUF_DELTA;
1035 buffer = nasm_malloc(ARG_BUF_DELTA);
1036 prevarg = nasm_malloc(ARG_BUF_DELTA);
1037 prevarg[0] = '\0';
1038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 p = buffer;
1041 while (1) { /* Loop to handle long lines */
1042 q = fgets(p, bufsize - (p - buffer), rfile);
1043 if (!q)
1044 break;
1045 p += strlen(p);
1046 if (p > buffer && p[-1] == '\n')
1047 break;
1048 if (p - buffer > bufsize - 10) {
1049 int offset;
1050 offset = p - buffer;
1051 bufsize += ARG_BUF_DELTA;
1052 buffer = nasm_realloc(buffer, bufsize);
1053 p = buffer + offset;
1054 }
1055 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (!q && p == buffer) {
1058 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001059 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 nasm_free(buffer);
1061 nasm_free(prevarg);
1062 return;
1063 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 /*
1066 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1067 * them are present at the end of the line.
1068 */
1069 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001071 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001073
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001074 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001075
H. Peter Anvin55568c12016-10-03 19:46:49 -07001076 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001078
Charles Crayne192d5b52007-10-18 19:02:42 -07001079 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 prevargsize += ARG_BUF_DELTA;
1081 prevarg = nasm_realloc(prevarg, prevargsize);
1082 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001083 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001084 }
1085}
1086
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087/* Function to process args from a string of args, rather than the
1088 * argv array. Used by the environment variable and response file
1089 * processing.
1090 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001091static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001093 char *p, *q, *arg, *prevarg;
1094 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001095
1096 p = args;
1097 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001099 arg = NULL;
1100 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 q = p;
1102 while (*p && *p != separator)
1103 p++;
1104 while (*p == separator)
1105 *p++ = '\0';
1106 prevarg = arg;
1107 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001108 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001110 }
1111 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001112 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001113}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001114
H. Peter Anvin55568c12016-10-03 19:46:49 -07001115static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001116{
1117 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001118 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001119 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001120 perror(file);
1121 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001122 }
1123 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001124 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001125 }
1126 fclose(f);
1127}
1128
H. Peter Anvin55568c12016-10-03 19:46:49 -07001129static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001130{
1131 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001132 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001133 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001134
Charles Craynefcce07f2007-09-30 22:15:36 -07001135 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136
H. Peter Anvin972079f2008-09-30 17:01:23 -07001137 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001138 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001139
1140 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001141 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001142 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001143 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001144 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001146 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001148 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001149
1150 /*
1151 * Now process the actual command line.
1152 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001154 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 argv++;
1156 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 /*
1158 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 * arguments like the environment variable. This allows us
1160 * to have multiple arguments on a single line, which is
1161 * different to the -@resp file processing below for regular
1162 * NASM.
1163 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001164 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 argc--;
1166 argv++;
1167 }
1168 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001169 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001170 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001171 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001172 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001173 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 fclose(rfile);
1175 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001176 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 "unable to open response file `%s'", p);
1178 }
1179 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001180 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001181 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001182 }
1183
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001184 /*
1185 * Look for basic command line typos. This definitely doesn't
1186 * catch all errors, but it might help cases of fumbled fingers.
1187 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001188 if (pass != 2)
1189 return;
1190
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001191 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001192 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001193 "no input file specified");
1194 else if (!strcmp(inname, errname) ||
1195 !strcmp(inname, outname) ||
1196 !strcmp(inname, listname) ||
1197 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001198 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001199 "file `%s' is both input and output file",
1200 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001201
H. Peter Anvin70653092007-10-19 14:42:29 -07001202 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001203 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001204 if (!error_file) {
1205 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001206 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001207 "cannot open file `%s' for error messages",
1208 errname);
1209 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001210 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001211}
1212
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001213static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001215 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001217 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001218 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001219 int pass_max;
Martin Lindhe8cc93f52016-11-16 16:48:13 +01001220 int sb;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001221
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001222 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001223 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001224
Charles Craynec1905c22008-09-11 18:54:06 -07001225 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001226 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001228
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001229 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001230 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1231 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001233 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234
Keith Kaniosb7a89542007-04-12 02:40:54 +00001235 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 cpu = cmd_cpu;
1237 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001238 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001240 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001241 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001243 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001244 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 saa_rewind(forwrefs);
1246 forwref = saa_rstruct(forwrefs);
1247 raa_free(offsets);
1248 offsets = raa_init();
1249 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001250 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001251 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001252
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001254 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001255 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001256 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 while ((line = preproc->getline())) {
1259 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001260
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001261 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001262 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001263 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001264 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001265 if (process_directives(line) != D_none)
1266 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001267
H. Peter Anvinc7131682017-03-07 17:45:01 -08001268 /* Not a directive, or even something that starts with [ */
1269
1270 parse_line(pass1, line, &output_ins, def_label);
1271
1272 if (optimizing > 0) {
1273 if (forwref != NULL && globallineno == forwref->lineno) {
1274 output_ins.forw_ref = true;
1275 do {
1276 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1277 forwref = saa_rstruct(forwrefs);
1278 } while (forwref != NULL
1279 && forwref->lineno == globallineno);
1280 } else
1281 output_ins.forw_ref = false;
1282
1283 if (output_ins.forw_ref) {
1284 if (passn == 1) {
1285 for (i = 0; i < output_ins.operands; i++) {
1286 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1287 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1288 fwinf->lineno = globallineno;
1289 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001290 }
1291 }
1292 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001293 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001294 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001295
H. Peter Anvinc7131682017-03-07 17:45:01 -08001296 /* forw_ref */
1297 if (output_ins.opcode == I_EQU) {
1298 if (pass1 == 1) {
1299 /*
1300 * Special `..' EQUs get processed in pass two,
1301 * except `..@' macro-processor EQUs which are done
1302 * in the normal place.
1303 */
1304 if (!output_ins.label)
1305 nasm_error(ERR_NONFATAL,
1306 "EQU not preceded by label");
1307
1308 else if (output_ins.label[0] != '.' ||
1309 output_ins.label[1] != '.' ||
1310 output_ins.label[2] == '@') {
1311 if (output_ins.operands == 1 &&
1312 (output_ins.oprs[0].type & IMMEDIATE) &&
1313 output_ins.oprs[0].wrt == NO_SEG) {
1314 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1315 def_label(output_ins.label,
1316 output_ins.oprs[0].segment,
1317 output_ins.oprs[0].offset, NULL,
1318 false, isext);
1319 } else if (output_ins.operands == 2
1320 && (output_ins.oprs[0].type & IMMEDIATE)
1321 && (output_ins.oprs[0].type & COLON)
1322 && output_ins.oprs[0].segment == NO_SEG
1323 && output_ins.oprs[0].wrt == NO_SEG
1324 && (output_ins.oprs[1].type & IMMEDIATE)
1325 && output_ins.oprs[1].segment == NO_SEG
1326 && output_ins.oprs[1].wrt == NO_SEG) {
1327 def_label(output_ins.label,
1328 output_ins.oprs[0].offset | SEG_ABS,
1329 output_ins.oprs[1].offset,
1330 NULL, false, false);
1331 } else
1332 nasm_error(ERR_NONFATAL,
1333 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001334 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001335 } else {
1336 /*
1337 * Special `..' EQUs get processed here, except
1338 * `..@' macro processor EQUs which are done above.
1339 */
1340 if (output_ins.label[0] == '.' &&
1341 output_ins.label[1] == '.' &&
1342 output_ins.label[2] != '@') {
1343 if (output_ins.operands == 1 &&
1344 (output_ins.oprs[0].type & IMMEDIATE)) {
1345 define_label(output_ins.label,
1346 output_ins.oprs[0].segment,
1347 output_ins.oprs[0].offset,
1348 NULL, false, false);
1349 } else if (output_ins.operands == 2
1350 && (output_ins.oprs[0].type & IMMEDIATE)
1351 && (output_ins.oprs[0].type & COLON)
1352 && output_ins.oprs[0].segment == NO_SEG
1353 && (output_ins.oprs[1].type & IMMEDIATE)
1354 && output_ins.oprs[1].segment == NO_SEG) {
1355 define_label(output_ins.label,
1356 output_ins.oprs[0].offset | SEG_ABS,
1357 output_ins.oprs[1].offset,
1358 NULL, false, false);
1359 } else
1360 nasm_error(ERR_NONFATAL,
1361 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001362 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001363 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001364 } else { /* instruction isn't an EQU */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365
H. Peter Anvinc7131682017-03-07 17:45:01 -08001366 if (pass1 == 1) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001367 int64_t l = insn_size(location.segment, offs, sb,
H. Peter Anvinc7131682017-03-07 17:45:01 -08001368 &output_ins);
1369 l *= output_ins.times;
1370
1371 /* if (using_debug_info) && output_ins.opcode != -1) */
1372 if (using_debug_info)
1373 { /* fbk 03/25/01 */
1374 /* this is done here so we can do debug type info */
1375 int32_t typeinfo =
1376 TYS_ELEMENTS(output_ins.operands);
1377 switch (output_ins.opcode) {
1378 case I_RESB:
1379 typeinfo =
1380 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001381 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001382 case I_RESW:
1383 typeinfo =
1384 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001385 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001386 case I_RESD:
1387 typeinfo =
1388 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001389 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001390 case I_RESQ:
1391 typeinfo =
1392 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001393 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001394 case I_REST:
1395 typeinfo =
1396 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001397 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001398 case I_RESO:
1399 typeinfo =
1400 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001401 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001402 case I_RESY:
1403 typeinfo =
1404 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001405 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001406 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;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001429 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001430 default:
H. Peter Anvinc7131682017-03-07 17:45:01 -08001431 typeinfo = TY_LABEL;
1432
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001433 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001434
1435 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001436 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001437 if (l != -1) {
1438 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001439 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001441 /*
1442 * else l == -1 => invalid instruction, which will be
1443 * flagged as an error on pass 2
1444 */
1445
1446 } else {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001447 offs += assemble(location.segment, offs, sb, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001448 set_curr_offs(offs);
1449
1450 }
1451 } /* not an EQU */
1452 cleanup_insn(&output_ins);
1453
1454 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001456 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001458
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001459 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001460 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001461 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001462
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 if (pass1 == 1)
1464 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001465
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001466 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001468 } else if (global_offset_changed &&
1469 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001470 prev_offset_changed = global_offset_changed;
1471 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001472 } else {
1473 stall_count++;
1474 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001475
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001476 if (terminate_after_phase)
1477 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001478
1479 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001480 /* We get here if the labels don't converge
1481 * Example: FOO equ FOO + 1
1482 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001483 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001484 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001485 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001486 nasm_error(ERR_NONFATAL,
1487 "Possible causes: recursive EQUs, macro abuse.");
1488 break;
1489 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001490 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001491
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001493 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001494 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001495 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001496 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1497 }
1498}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001499
Ed Berosetfa771012002-06-09 20:56:40 +00001500/**
1501 * gnu style error reporting
1502 * This function prints an error message to error_file in the
1503 * style used by GNU. An example would be:
1504 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001505 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001506 * which the error occurs (or is detected) and "error:" is one of
1507 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001508 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001509 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001510 *
1511 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001512 * @param fmt the printf style format string
1513 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001514static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001515{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001516 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001517 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001518
Ed Berosetfa771012002-06-09 20:56:40 +00001519 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001522 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001523 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001524
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001525 if (!skip_this_pass(severity)) {
1526 if (currentfile) {
1527 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001528 } else {
1529 fputs("nasm: ", error_file);
1530 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001531 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001532
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001533 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001534}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001535
Ed Berosetfa771012002-06-09 20:56:40 +00001536/**
1537 * MS style error reporting
1538 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001539 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001540 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001541 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001542 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001543 * which the error occurs (or is detected) and "error:" is one of
1544 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001545 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001546 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001547 *
1548 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001549 * @param fmt the printf style format string
1550 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001551static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001552{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001553 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001554 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001555
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 if (is_suppressed_warning(severity))
1557 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001558
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001559 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001561
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001562 if (!skip_this_pass(severity)) {
1563 if (currentfile) {
1564 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001565 } else {
1566 fputs("nasm: ", error_file);
1567 }
Ed Berosetfa771012002-06-09 20:56:40 +00001568 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001569
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001570 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001571}
1572
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001573/*
1574 * check to see if this is a suppressable warning
1575 */
1576static inline bool is_suppressable_warning(int severity)
1577{
1578 int index;
1579
1580 /* Not a warning at all */
1581 if ((severity & ERR_MASK) != ERR_WARNING)
1582 return false;
1583
1584 index = WARN_IDX(severity);
1585
1586 return index && index <= ERR_WARN_MAX;
1587}
1588
Ed Berosetfa771012002-06-09 20:56:40 +00001589/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001590 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001591 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001592 * not in pass 1
1593 *
1594 * @param severity the severity of the warning or error
1595 * @return true if we should abort error/warning printing
1596 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001597static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001598{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001599 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001600 if (is_suppressable_warning(severity))
1601 return !warning_on[WARN_IDX(severity)];
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001602 else
1603 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001604}
1605
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001606static bool skip_this_pass(int severity)
1607{
H. Peter Anvin934f0472016-05-09 12:00:19 -07001608 /* See if it's a pass-specific warning which should be skipped. */
1609
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001610 if ((severity & ERR_MASK) > ERR_WARNING)
1611 return false;
1612
H. Peter Anvin934f0472016-05-09 12:00:19 -07001613 /*
1614 * passn is 1 on the very first pass only.
1615 * pass0 is 2 on the code-generation (final) pass only.
1616 * These are the passes we care about in this case.
1617 */
1618 return (((severity & ERR_PASS1) && passn != 1) ||
1619 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001620}
1621
Ed Berosetfa771012002-06-09 20:56:40 +00001622/**
1623 * common error reporting
1624 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001625 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001626 * specific error message to error_file and may or may not return. It
1627 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001628 *
1629 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001630 * @param fmt the printf style format string
1631 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001632static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001633{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001634 char msg[1024];
1635 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001636
H. Peter Anvin7df04172008-06-10 18:27:38 -07001637 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001639 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 break;
1641 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001642 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 break;
1644 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001645 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 break;
1647 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001648 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 break;
1650 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001651 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001653 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001654 pfx = "";
1655 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001657
H. Peter Anvin934f0472016-05-09 12:00:19 -07001658 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001659 if (is_suppressable_warning(severity)) {
1660 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001661 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1662 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001663
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001664 if (!skip_this_pass(severity))
1665 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001666
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001667 /* Are we recursing from error_list_macros? */
1668 if (severity & ERR_PP_LISTMACRO)
1669 return;
1670
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001671 /*
1672 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001673 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001674 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001675 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001676
1677 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001678 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001679
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001680 preproc->error_list_macros(severity);
1681
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001682 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 case ERR_DEBUG:
1684 /* no further action, by definition */
1685 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001686 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001687 /* Treat warnings as errors */
1688 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1689 terminate_after_phase = true;
1690 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001692 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 break;
1694 case ERR_FATAL:
1695 if (ofile) {
1696 fclose(ofile);
1697 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001698 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 }
1700 if (want_usage)
1701 usage();
1702 exit(1); /* instantly die */
1703 break; /* placate silly compilers */
1704 case ERR_PANIC:
1705 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001706 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001707 if (ofile) {
1708 fclose(ofile);
1709 remove(outname);
1710 ofile = NULL;
1711 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 exit(3);
1713 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001714 }
1715}
1716
H. Peter Anvin734b1882002-04-30 21:01:08 +00001717static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001718{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001719 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001720}