blob: 8e405466ea9ca86ee13a146842400db127a04b5a [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
85
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080086static bool using_debug_info, opt_verbose_info;
87static const char *debug_format;
88
H. Peter Anvin3366e312018-02-07 14:14:36 -080089#ifndef ABORT_ON_PANIC
90# define ABORT_ON_PANIC 0
91#endif
92static bool abort_on_panic = ABORT_ON_PANIC;
93
H. Peter Anvin6867acc2007-10-10 14:58:45 -070094bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080095int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080096static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000097int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080098int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000099
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700100struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800101
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800102const char *inname;
103const char *outname;
104static const char *listname;
105static const char *errname;
106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static int globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700108#define GLOBALLINENO_MAX INT32_MAX
109
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000110/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800111const struct ofmt *ofmt = &OF_DEFAULT;
112const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400113const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000116
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400117FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700118int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100119static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400120
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800121iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400122static iflag_t cmd_cpu;
123
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800124struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800125bool in_absolute; /* Flag we are in ABSOLUTE seg */
126struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000127
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000129
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700131static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000132
H. Peter Anvine7469712016-02-18 02:20:59 -0800133static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400134
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400135#define OP_NORMAL (1u << 0)
136#define OP_PREPROCESS (1u << 1)
137#define OP_DEPEND (1u << 2)
138
139static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400140
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700141/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700142static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700143static bool depend_missing_ok = false;
144static const char *depend_target = NULL;
145static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700146StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700148static bool want_usage;
149static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800150bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000151
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700152static char *quote_for_pmake(const char *str);
153static char *quote_for_wmake(const char *str);
154static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700155
H. Peter Anvin892c4812018-05-30 14:43:46 -0700156int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400157{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700158 location.segment = segment;
159 if (segment == NO_SEG) {
160 location.offset = absolute.offset;
161 in_absolute = true;
162 } else {
163 location.offset = raa_read(offsets, segment);
164 in_absolute = false;
165 }
166 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400167}
168
169static void set_curr_offs(int64_t l_off)
170{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800171 if (in_absolute)
172 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400173 else
174 offsets = raa_write(offsets, location.segment, l_off);
175}
176
H. Peter Anvin892c4812018-05-30 14:43:46 -0700177static void increment_offset(int64_t delta)
178{
179 if (unlikely(delta == 0))
180 return;
181
182 location.offset += delta;
183 set_curr_offs(location.offset);
184}
185
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000186static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000187{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000188 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000189 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800190 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000191 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193}
194
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800195static void define_macros_early(void)
196{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700197 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800198 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800199
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700200 if (oct->have_local) {
201 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400202 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700203 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400204 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700205 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400206 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700207 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400208 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800209 }
210
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700211 if (oct->have_gm) {
212 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400213 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700214 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400215 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700216 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400217 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700218 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400219 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800220 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700221
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700222 if (oct->have_posix) {
223 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400224 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800225 }
226}
227
228static void define_macros_late(void)
229{
230 char temp[128];
231
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400232 /*
233 * In case if output format is defined by alias
234 * we have to put shortname of the alias itself here
235 * otherwise ABI backward compatibility gets broken.
236 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400237 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400238 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240}
241
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700242static void emit_dependencies(StrList *list)
243{
244 FILE *deps;
245 int linepos, len;
246 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700247 bool wmake = (quote_for_make == quote_for_wmake);
248 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700249
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700250 wrapstr = wmake ? " &\n " : " \\\n ";
251 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700252
253 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700254 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400255 if (!deps) {
256 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
257 "unable to write dependency file `%s'", depend_file);
258 return;
259 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700260 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400261 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700262 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700263
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700264 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400265 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700266 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400267 len = strlen(file);
268 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700269 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400270 linepos = 1;
271 }
272 fprintf(deps, " %s", file);
273 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700274 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700275 }
276 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700277
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400278 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700279 if (depend_emit_phony) {
280 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700281 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700282 nasm_free(file);
283 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400284 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700285 }
286
287 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400288 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700289}
290
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700291/* Convert a struct tm to a POSIX-style time constant */
292static int64_t make_posix_time(const struct tm *tm)
293{
294 int64_t t;
295 int64_t y = tm->tm_year;
296
297 /* See IEEE 1003.1:2004, section 4.14 */
298
299 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
300 t += tm->tm_yday;
301 t *= 24;
302 t += tm->tm_hour;
303 t *= 60;
304 t += tm->tm_min;
305 t *= 60;
306 t += tm->tm_sec;
307
308 return t;
309}
310
311static void timestamp(void)
312{
313 struct compile_time * const oct = &official_compile_time;
314 const struct tm *tp, *best_gm;
315
316 time(&oct->t);
317
318 best_gm = NULL;
319
320 tp = localtime(&oct->t);
321 if (tp) {
322 oct->local = *tp;
323 best_gm = &oct->local;
324 oct->have_local = true;
325 }
326
327 tp = gmtime(&oct->t);
328 if (tp) {
329 oct->gm = *tp;
330 best_gm = &oct->gm;
331 oct->have_gm = true;
332 if (!oct->have_local)
333 oct->local = oct->gm;
334 } else {
335 oct->gm = oct->local;
336 }
337
338 if (best_gm) {
339 oct->posix = make_posix_time(best_gm);
340 oct->have_posix = true;
341 }
342}
343
H. Peter Anvin038d8612007-04-12 16:54:50 +0000344int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000345{
H. Peter Anvina771be82017-08-16 14:53:18 -0700346 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700347
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700348 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800350 iflag_set_default_cpu(&cpu);
351 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400352
Charles Crayne2581c862008-09-10 19:21:52 -0700353 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700354 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400355 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
H. Peter Anvin97e15752007-09-25 08:47:47 -0700357 error_file = stderr;
358
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700359 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700360 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700361
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000363 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000364
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400366 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000367
H. Peter Anvin55568c12016-10-03 19:46:49 -0700368 parse_cmdline(argc, argv, 1);
369 if (terminate_after_phase) {
370 if (want_usage)
371 usage();
372 return 1;
373 }
374
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700375 /*
376 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700377 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700378 */
379 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800380 define_macros_early();
381
H. Peter Anvin55568c12016-10-03 19:46:49 -0700382 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 if (terminate_after_phase) {
384 if (want_usage)
385 usage();
386 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387 }
388
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800389 /* Save away the default state of warnings */
390 memcpy(warning_state_init, warning_state, sizeof warning_state);
391
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800392 if (!using_debug_info) {
393 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800394 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800395 } else if (!debug_format) {
396 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800397 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800398 } else {
399 dfmt = dfmt_find(ofmt, debug_format);
400 if (!dfmt) {
401 nasm_fatal(ERR_NOFILE | ERR_USAGE,
402 "unrecognized debug format `%s' for"
403 " output format `%s'",
404 debug_format, ofmt->shortname);
405 }
406 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000407
H. Peter Anvin76690a12002-04-30 20:52:49 +0000408 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400409 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800411 /* no output file name? */
412 if (!outname)
413 outname = filename_set_extension(inname, ofmt->extension);
414
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000415 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800416 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000417
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400418 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700419
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700420 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400421 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700422
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400423 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000424 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700425
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400426 if (depend_missing_ok)
427 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700428
H. Peter Anvin130736c2016-02-17 20:27:41 -0800429 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 while ((line = preproc->getline()))
432 nasm_free(line);
433 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400434 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000435 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700436 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000437 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000439
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800440 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700441 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800443 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 "unable to open output file `%s'",
445 outname);
446 } else
447 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700449 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000450
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400451 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800452 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800453
454 /* Revert all warnings to the default state */
455 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700456
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 while ((line = preproc->getline())) {
458 /*
459 * We generate %line directives if needed for later programs
460 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000461 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 int altline = src_get(&linnum, &file_name);
463 if (altline) {
464 if (altline == 1 && lineinc == 1)
465 nasm_fputs("", ofile);
466 else {
467 lineinc = (altline != -1 || lineinc != 1);
468 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000469 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 file_name);
471 }
472 prior_linnum = linnum;
473 }
474 nasm_fputs(line, ofile);
475 nasm_free(line);
476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 preproc->cleanup(0);
478 if (ofile)
479 fclose(ofile);
480 if (ofile && terminate_after_phase)
481 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400482 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700486 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400487 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800488 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400489 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400491 /*
492 * We must call init_labels() before ofmt->init() since
493 * some object formats will want to define labels in their
494 * init routines. (eg OS/2 defines the FLAT group)
495 */
496 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400498 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400499 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400501 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200502
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400503 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800504 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400505 cleanup_labels();
506 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700507 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400508 nasm_error(ERR_NONFATAL|ERR_NOFILE,
509 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700510 terminate_after_phase = true;
511 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400512 }
513
514 if (ofile) {
515 fclose(ofile);
516 if (terminate_after_phase)
517 remove(outname);
518 ofile = NULL;
519 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700522 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400523 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700524
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000525 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000527
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 raa_free(offsets);
529 saa_free(forwrefs);
530 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000531 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700532 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700534 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000535}
536
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537/*
538 * Get a parameter for a command line option.
539 * First arg must be in the form of e.g. -f...
540 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800541static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000542{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800543 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400544 if (p[2]) /* the parameter's in the option */
545 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800547 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000549 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400550 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552 return NULL;
553}
554
H. Peter Anvindc242712007-11-18 11:55:10 -0800555/*
556 * Copy a filename
557 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800558static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800559{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800560 if (*dst)
561 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800562
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800563 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800564}
565
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700566/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700567 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700568 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700569static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700570{
571 const char *p;
572 char *os, *q;
573
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400574 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700575 size_t nbs = 0;
576
577 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400578 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579
580 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400581 switch (*p) {
582 case ' ':
583 case '\t':
584 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
585 n += nbs + 2;
586 nbs = 0;
587 break;
588 case '$':
589 case '#':
590 nbs = 0;
591 n += 2;
592 break;
593 case '\\':
594 nbs++;
595 n++;
596 break;
597 default:
598 nbs = 0;
599 n++;
600 break;
601 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700602 }
603
604 /* Convert N backslashes at the end of filename to 2N backslashes */
605 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400606 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607
608 os = q = nasm_malloc(n);
609
610 nbs = 0;
611 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400612 switch (*p) {
613 case ' ':
614 case '\t':
615 while (nbs--)
616 *q++ = '\\';
617 *q++ = '\\';
618 *q++ = *p;
619 break;
620 case '$':
621 *q++ = *p;
622 *q++ = *p;
623 nbs = 0;
624 break;
625 case '#':
626 *q++ = '\\';
627 *q++ = *p;
628 nbs = 0;
629 break;
630 case '\\':
631 *q++ = *p;
632 nbs++;
633 break;
634 default:
635 *q++ = *p;
636 nbs = 0;
637 break;
638 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700639 }
640 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400641 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700642
643 *q = '\0';
644
645 return os;
646}
647
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700648/*
649 * Convert a string to a Watcom make-safe form
650 */
651static char *quote_for_wmake(const char *str)
652{
653 const char *p;
654 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700655 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700656
657 size_t n = 1; /* Terminating zero */
658
659 if (!str)
660 return NULL;
661
662 for (p = str; *p; p++) {
663 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700664 case ' ':
665 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700666 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700667 quote = true;
668 n++;
669 break;
670 case '\"':
671 quote = true;
672 n += 2;
673 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700674 case '$':
675 case '#':
676 n += 2;
677 break;
678 default:
679 n++;
680 break;
681 }
682 }
683
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700684 if (quote)
685 n += 2;
686
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700687 os = q = nasm_malloc(n);
688
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700689 if (quote)
690 *q++ = '\"';
691
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700692 for (p = str; *p; p++) {
693 switch (*p) {
694 case '$':
695 case '#':
696 *q++ = '$';
697 *q++ = *p;
698 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700699 case '\"':
700 *q++ = *p;
701 *q++ = *p;
702 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700703 default:
704 *q++ = *p;
705 break;
706 }
707 }
708
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700709 if (quote)
710 *q++ = '\"';
711
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700712 *q = '\0';
713
714 return os;
715}
716
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100717enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800718 OPT_BOGUS,
719 OPT_VERSION,
720 OPT_ABORT_ON_PANIC,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100721 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800722 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100723};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800724struct textargs {
725 const char *label;
726 enum text_options opt;
727 bool need_arg;
728};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800729static const struct textargs textopts[] = {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800730 {"v", OPT_VERSION, false},
731 {"version", OPT_VERSION, false},
732 {"abort-on-panic", OPT_ABORT_ON_PANIC, false},
733 {"prefix", OPT_PREFIX, true},
734 {"postfix", OPT_POSTFIX, true},
735 {NULL, OPT_BOGUS, false}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000736};
737
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400738static void show_version(void)
739{
740 printf("NASM version %s compiled on %s%s\n",
741 nasm_version, nasm_date, nasm_compile_options);
742 exit(0);
743}
744
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700746static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000748 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800749 int i;
750 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000751
752 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800753 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000754
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
757 /* These parameters take values */
758 if (!(param = get_param(p, q, &advance)))
759 return advance;
760 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 switch (p[1]) {
763 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700764 if (pass == 1)
765 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700767
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400768 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700769 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800770 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400771 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700772
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400773 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700774 if (pass == 1) {
775 ofmt = ofmt_find(param, &ofmt_alias);
776 if (!ofmt) {
777 nasm_fatal(ERR_NOFILE | ERR_USAGE,
778 "unrecognised output format `%s' - "
779 "use -hf for a list", param);
780 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400781 }
782 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700783
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400784 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700785 if (pass == 2) {
786 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700787
H. Peter Anvin55568c12016-10-03 19:46:49 -0700788 if (!*param) {
789 /* Naked -O == -Ox */
790 optimizing = MAX_OPTIMIZE;
791 } else {
792 while (*param) {
793 switch (*param) {
794 case '0': case '1': case '2': case '3': case '4':
795 case '5': case '6': case '7': case '8': case '9':
796 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700797
H. Peter Anvin55568c12016-10-03 19:46:49 -0700798 /* -O0 -> optimizing == -1, 0.98 behaviour */
799 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
800 if (opt < 2)
801 optimizing = opt - 1;
802 else
803 optimizing = opt;
804 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700805
H. Peter Anvin55568c12016-10-03 19:46:49 -0700806 case 'v':
807 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400808 param++;
809 opt_verbose_info = true;
810 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700811
H. Peter Anvin55568c12016-10-03 19:46:49 -0700812 case 'x':
813 param++;
814 optimizing = MAX_OPTIMIZE;
815 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700816
H. Peter Anvin55568c12016-10-03 19:46:49 -0700817 default:
818 nasm_fatal(0,
819 "unknown optimization option -O%c\n",
820 *param);
821 break;
822 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400823 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700824 if (optimizing > MAX_OPTIMIZE)
825 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400826 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400827 }
828 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800829
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400830 case 'p': /* pre-include */
831 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700832 if (pass == 2)
833 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400834 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800835
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 case 'd': /* pre-define */
837 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700838 if (pass == 2)
839 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 case 'u': /* un-define */
843 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700844 if (pass == 2)
845 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400848 case 'i': /* include search path */
849 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700850 if (pass == 2)
851 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700855 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800856 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800858
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400859 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700860 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800861 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400862 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800863
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700865 if (pass == 2) {
866 using_debug_info = true;
867 debug_format = param;
868 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400869 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872 if (pass == 1) {
873 if (nasm_stricmp("vc", param) == 0)
874 nasm_set_verror(nasm_verror_vc);
875 else if (nasm_stricmp("gnu", param) == 0)
876 nasm_set_verror(nasm_verror_gnu);
877 else
878 nasm_fatal(ERR_NOFILE | ERR_USAGE,
879 "unrecognized error reporting format `%s'",
880 param);
881 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800883
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700885 if (pass == 2) {
886 using_debug_info = true;
887 if (p[2])
888 debug_format = nasm_skip_spaces(p + 2);
889 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800891
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 case 'h':
893 printf
894 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
895 "[-l listfile]\n"
896 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400897 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800898 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700900 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700902 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400903 " -MG d:o, missing files assumed generated\n"
904 " -MF <file> set Makefile dependency file\n"
905 " -MD <file> assemble and generate dependencies\n"
906 " -MT <file> dependency target name\n"
907 " -MQ <file> dependency target name (quoted)\n"
908 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700909 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800911 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800913 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400914 " -o outfile write output to an outfile\n\n"
915 " -f format select an output format\n\n"
916 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 " -I<path> adds a pathname to the include file path\n");
918 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700919 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400920 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400921 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400922 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 " -P<file> pre-includes a file\n"
924 " -D<macro>[=<value>] pre-defines a macro\n"
925 " -U<macro> undefines a macro\n"
926 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800927 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400928 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800929 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
930 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400931 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800932 " these options prepend or append the given string\n"
933 " to all extern and global variables\n"
934 "\n"
935 "Response files should contain command line parameters,\n"
936 "one per line.\n"
937 "\n"
938 "Warnings for the -W/-w options:\n");
939 for (i = 0; i <= ERR_WARN_ALL; i++)
940 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700941 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800942 i == ERR_WARN_ALL ? "\n" :
943 warnings[i].enabled ? " (default on)" :
944 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800946 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 " (`*' denotes default):\n");
948 ofmt_list(ofmt, stdout);
949 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800950 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 printf("For a list of debug formats, use -f <form> -y.\n");
952 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400953 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 case 'y':
957 printf("\nvalid debug formats for '%s' output format are"
958 " ('*' denotes default):\n", ofmt->shortname);
959 dfmt_list(ofmt, stdout);
960 exit(0);
961 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700964 if (pass == 2)
965 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400969 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800971
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400972 case 'e': /* preprocess only */
973 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 if (pass == 1)
975 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 if (pass == 1)
980 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800983 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400984 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800986 if (!set_warning_status(param)) {
987 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
988 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700989 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400990 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800991 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700994 if (pass == 1) {
995 switch (p[2]) {
996 case 'W':
997 quote_for_make = quote_for_wmake;
998 break;
999 case 'D':
1000 case 'F':
1001 case 'T':
1002 case 'Q':
1003 advance = true;
1004 break;
1005 default:
1006 break;
1007 }
1008 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 switch (p[2]) {
1010 case 0:
1011 operating_mode = OP_DEPEND;
1012 break;
1013 case 'G':
1014 operating_mode = OP_DEPEND;
1015 depend_missing_ok = true;
1016 break;
1017 case 'P':
1018 depend_emit_phony = true;
1019 break;
1020 case 'D':
1021 operating_mode = OP_NORMAL;
1022 depend_file = q;
1023 advance = true;
1024 break;
1025 case 'F':
1026 depend_file = q;
1027 advance = true;
1028 break;
1029 case 'T':
1030 depend_target = q;
1031 advance = true;
1032 break;
1033 case 'Q':
1034 depend_target = quote_for_make(q);
1035 advance = true;
1036 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001037 case 'W':
1038 /* handled in pass 1 */
1039 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 default:
1041 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1042 "unknown dependency option `-M%c'", p[2]);
1043 break;
1044 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001045 }
1046 if (advance && (!q || !q[0])) {
1047 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1048 "option `-M%c' requires a parameter", p[2]);
1049 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001050 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 case '-':
1054 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001055 const struct textargs *tx;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (p[2] == 0) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001058 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 break;
1060 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001061
H. Peter Anvin3366e312018-02-07 14:14:36 -08001062 for (tx = textopts; tx->label; tx++) {
1063 if (!nasm_stricmp(p + 2, tx->label))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001066
H. Peter Anvin3366e312018-02-07 14:14:36 -08001067 if (tx->need_arg) {
1068 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001069 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001070 "option `--%s' requires an argument",
1071 p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
1073 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001074 advance = true;
1075 }
1076
1077 switch (tx->opt) {
1078 case OPT_VERSION:
1079 show_version();
1080 break;
1081 case OPT_ABORT_ON_PANIC:
1082 abort_on_panic = true;
1083 break;
1084 case OPT_PREFIX:
1085 if (pass == 2)
1086 strlcpy(lprefix, q, PREFIX_MAX);
1087 break;
1088 case OPT_POSTFIX:
1089 if (pass == 2)
1090 strlcpy(lpostfix, q, POSTFIX_MAX);
1091 break;
1092 case OPT_BOGUS:
1093 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1094 "unrecognized option `--%s'", p + 2);
1095 break;
1096 default:
1097 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 }
1099 break;
1100 }
1101
1102 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001103 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1104 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 break;
1106 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001107 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001108 /* In theory we could allow multiple input files... */
1109 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001110 }
1111
1112 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001113}
1114
H. Peter Anvineba20a72002-04-30 20:53:55 +00001115#define ARG_BUF_DELTA 128
1116
H. Peter Anvin55568c12016-10-03 19:46:49 -07001117static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001118{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001119 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001120 int bufsize, prevargsize;
1121
1122 bufsize = prevargsize = ARG_BUF_DELTA;
1123 buffer = nasm_malloc(ARG_BUF_DELTA);
1124 prevarg = nasm_malloc(ARG_BUF_DELTA);
1125 prevarg[0] = '\0';
1126
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 p = buffer;
1129 while (1) { /* Loop to handle long lines */
1130 q = fgets(p, bufsize - (p - buffer), rfile);
1131 if (!q)
1132 break;
1133 p += strlen(p);
1134 if (p > buffer && p[-1] == '\n')
1135 break;
1136 if (p - buffer > bufsize - 10) {
1137 int offset;
1138 offset = p - buffer;
1139 bufsize += ARG_BUF_DELTA;
1140 buffer = nasm_realloc(buffer, bufsize);
1141 p = buffer + offset;
1142 }
1143 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (!q && p == buffer) {
1146 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001147 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 nasm_free(buffer);
1149 nasm_free(prevarg);
1150 return;
1151 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 /*
1154 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1155 * them are present at the end of the line.
1156 */
1157 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001158
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001159 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001161
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001162 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001163
H. Peter Anvin55568c12016-10-03 19:46:49 -07001164 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001166
Charles Crayne192d5b52007-10-18 19:02:42 -07001167 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 prevargsize += ARG_BUF_DELTA;
1169 prevarg = nasm_realloc(prevarg, prevargsize);
1170 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001171 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001172 }
1173}
1174
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001175/* Function to process args from a string of args, rather than the
1176 * argv array. Used by the environment variable and response file
1177 * processing.
1178 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001179static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001181 char *p, *q, *arg, *prevarg;
1182 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001183
1184 p = args;
1185 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001187 arg = NULL;
1188 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 q = p;
1190 while (*p && *p != separator)
1191 p++;
1192 while (*p == separator)
1193 *p++ = '\0';
1194 prevarg = arg;
1195 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001196 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001198 }
1199 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001200 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001201}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001202
H. Peter Anvin55568c12016-10-03 19:46:49 -07001203static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001204{
1205 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001206 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001207 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001208 perror(file);
1209 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001210 }
1211 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001212 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001213 }
1214 fclose(f);
1215}
1216
H. Peter Anvin55568c12016-10-03 19:46:49 -07001217static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218{
1219 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001220 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001221 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001222
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001223 /* Initialize all the warnings to their default state */
1224 for (i = 0; i < ERR_WARN_ALL; i++) {
1225 warning_state_init[i] = warning_state[i] =
1226 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1227 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001228
1229 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001230 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001231 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001232 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001233 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001235 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001237 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001238
1239 /*
1240 * Now process the actual command line.
1241 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001243 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 argv++;
1245 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001246 /*
1247 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 * arguments like the environment variable. This allows us
1249 * to have multiple arguments on a single line, which is
1250 * different to the -@resp file processing below for regular
1251 * NASM.
1252 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001253 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 argc--;
1255 argv++;
1256 }
1257 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001258 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001259 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001260 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001261 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001262 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 fclose(rfile);
1264 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001265 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 "unable to open response file `%s'", p);
1267 }
1268 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001269 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001270 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001271 }
1272
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001273 /*
1274 * Look for basic command line typos. This definitely doesn't
1275 * catch all errors, but it might help cases of fumbled fingers.
1276 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001277 if (pass != 2)
1278 return;
1279
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001280 if (!inname)
1281 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001282
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001283 else if ((errname && !strcmp(inname, errname)) ||
1284 (outname && !strcmp(inname, outname)) ||
1285 (listname && !strcmp(inname, listname)) ||
1286 (depend_file && !strcmp(inname, depend_file)))
1287 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1288
1289 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001290 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001291 if (!error_file) {
1292 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001293 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001294 "cannot open file `%s' for error messages",
1295 errname);
1296 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001297 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001298}
1299
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001300static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001301{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001302 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001304 int i;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001305 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001306 uint64_t prev_offset_changed;
1307 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001308
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001309 switch (cmd_sb) {
1310 case 16:
1311 break;
1312 case 32:
1313 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1314 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1315 break;
1316 case 64:
1317 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1318 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1319 break;
1320 default:
1321 panic();
1322 break;
1323 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324
Charles Craynec1905c22008-09-11 18:54:06 -07001325 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001326 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001328
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001329 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001330 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1331 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001332
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001333 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001334
H. Peter Anvincac0b192017-03-28 16:12:30 -07001335 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 cpu = cmd_cpu;
1337 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001338 lfmt->init(listname);
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001339 } else if (passn == 1 && listname) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001340 /* Remove the list file in case we die before the output pass */
1341 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001343 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001344 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin892c4812018-05-30 14:43:46 -07001345 seg_alloc_reset();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001346 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 saa_rewind(forwrefs);
1348 forwref = saa_rstruct(forwrefs);
1349 raa_free(offsets);
1350 offsets = raa_init();
1351 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001352 location.segment = NO_SEG;
1353 location.offset = 0;
1354 if (passn == 1)
1355 location.known = true;
1356 ofmt->reset();
1357 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001358 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001359
1360 /* Revert all warnings to the default state */
1361 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001366 if (globallineno++ == GLOBALLINENO_MAX)
1367 nasm_error(ERR_FATAL,
1368 "overall line number reaches the maximum %d\n",
1369 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001371 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001372 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001373 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001374 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001375 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001376 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001377
H. Peter Anvinc7131682017-03-07 17:45:01 -08001378 /* Not a directive, or even something that starts with [ */
1379
1380 parse_line(pass1, line, &output_ins, def_label);
1381
1382 if (optimizing > 0) {
1383 if (forwref != NULL && globallineno == forwref->lineno) {
1384 output_ins.forw_ref = true;
1385 do {
1386 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1387 forwref = saa_rstruct(forwrefs);
1388 } while (forwref != NULL
1389 && forwref->lineno == globallineno);
1390 } else
1391 output_ins.forw_ref = false;
1392
1393 if (output_ins.forw_ref) {
1394 if (passn == 1) {
1395 for (i = 0; i < output_ins.operands; i++) {
1396 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1397 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1398 fwinf->lineno = globallineno;
1399 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001400 }
1401 }
1402 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001403 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001404 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001405
H. Peter Anvinc7131682017-03-07 17:45:01 -08001406 /* forw_ref */
1407 if (output_ins.opcode == I_EQU) {
1408 if (pass1 == 1) {
1409 /*
1410 * Special `..' EQUs get processed in pass two,
1411 * except `..@' macro-processor EQUs which are done
1412 * in the normal place.
1413 */
1414 if (!output_ins.label)
1415 nasm_error(ERR_NONFATAL,
1416 "EQU not preceded by label");
1417
1418 else if (output_ins.label[0] != '.' ||
1419 output_ins.label[1] != '.' ||
1420 output_ins.label[2] == '@') {
1421 if (output_ins.operands == 1 &&
1422 (output_ins.oprs[0].type & IMMEDIATE) &&
1423 output_ins.oprs[0].wrt == NO_SEG) {
1424 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1425 def_label(output_ins.label,
1426 output_ins.oprs[0].segment,
1427 output_ins.oprs[0].offset, NULL,
1428 false, isext);
1429 } else if (output_ins.operands == 2
1430 && (output_ins.oprs[0].type & IMMEDIATE)
1431 && (output_ins.oprs[0].type & COLON)
1432 && output_ins.oprs[0].segment == NO_SEG
1433 && output_ins.oprs[0].wrt == NO_SEG
1434 && (output_ins.oprs[1].type & IMMEDIATE)
1435 && output_ins.oprs[1].segment == NO_SEG
1436 && output_ins.oprs[1].wrt == NO_SEG) {
1437 def_label(output_ins.label,
1438 output_ins.oprs[0].offset | SEG_ABS,
1439 output_ins.oprs[1].offset,
1440 NULL, false, false);
1441 } else
1442 nasm_error(ERR_NONFATAL,
1443 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001444 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001445 } else {
1446 /*
1447 * Special `..' EQUs get processed here, except
1448 * `..@' macro processor EQUs which are done above.
1449 */
1450 if (output_ins.label[0] == '.' &&
1451 output_ins.label[1] == '.' &&
1452 output_ins.label[2] != '@') {
1453 if (output_ins.operands == 1 &&
1454 (output_ins.oprs[0].type & IMMEDIATE)) {
1455 define_label(output_ins.label,
1456 output_ins.oprs[0].segment,
1457 output_ins.oprs[0].offset,
1458 NULL, false, false);
1459 } else if (output_ins.operands == 2
1460 && (output_ins.oprs[0].type & IMMEDIATE)
1461 && (output_ins.oprs[0].type & COLON)
1462 && output_ins.oprs[0].segment == NO_SEG
1463 && (output_ins.oprs[1].type & IMMEDIATE)
1464 && output_ins.oprs[1].segment == NO_SEG) {
1465 define_label(output_ins.label,
1466 output_ins.oprs[0].offset | SEG_ABS,
1467 output_ins.oprs[1].offset,
1468 NULL, false, false);
1469 } else
1470 nasm_error(ERR_NONFATAL,
1471 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001472 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001473 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001474 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001475 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001477 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001478
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001479 for (n = 1; n <= output_ins.times; n++) {
1480 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001481 int64_t l = insn_size(location.segment,
1482 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001483 globalbits, &output_ins);
1484
1485 /* if (using_debug_info) && output_ins.opcode != -1) */
1486 if (using_debug_info)
1487 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001488 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001489 int32_t typeinfo =
1490 TYS_ELEMENTS(output_ins.operands);
1491 switch (output_ins.opcode) {
1492 case I_RESB:
1493 typeinfo =
1494 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1495 break;
1496 case I_RESW:
1497 typeinfo =
1498 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1499 break;
1500 case I_RESD:
1501 typeinfo =
1502 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1503 break;
1504 case I_RESQ:
1505 typeinfo =
1506 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1507 break;
1508 case I_REST:
1509 typeinfo =
1510 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1511 break;
1512 case I_RESO:
1513 typeinfo =
1514 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1515 break;
1516 case I_RESY:
1517 typeinfo =
1518 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1519 break;
1520 case I_RESZ:
1521 typeinfo =
1522 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1523 break;
1524 case I_DB:
1525 typeinfo |= TY_BYTE;
1526 break;
1527 case I_DW:
1528 typeinfo |= TY_WORD;
1529 break;
1530 case I_DD:
1531 if (output_ins.eops_float)
1532 typeinfo |= TY_FLOAT;
1533 else
1534 typeinfo |= TY_DWORD;
1535 break;
1536 case I_DQ:
1537 typeinfo |= TY_QWORD;
1538 break;
1539 case I_DT:
1540 typeinfo |= TY_TBYTE;
1541 break;
1542 case I_DO:
1543 typeinfo |= TY_OWORD;
1544 break;
1545 case I_DY:
1546 typeinfo |= TY_YWORD;
1547 break;
1548 case I_DZ:
1549 typeinfo |= TY_ZWORD;
1550 break;
1551 default:
1552 typeinfo = TY_LABEL;
1553 break;
1554 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001555
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001556 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001557 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001558
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001559 /*
1560 * For INCBIN, let the code in assemble
1561 * handle TIMES, so we don't have to read the
1562 * input file over and over.
1563 */
1564 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001565 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001566 }
1567 /*
1568 * else l == -1 => invalid instruction, which will be
1569 * flagged as an error on pass 2
1570 */
1571 } else {
1572 if (n == 2)
1573 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001574 increment_offset(assemble(location.segment,
1575 location.offset,
1576 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001578 } /* not an EQU */
1579 }
1580 if (output_ins.times > 1)
1581 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001582
H. Peter Anvinc7131682017-03-07 17:45:01 -08001583 cleanup_insn(&output_ins);
1584
1585 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001588
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001589 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001590 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001591 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 if (pass1 == 1)
1594 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001595
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001596 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001598 } else if (global_offset_changed &&
1599 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001600 prev_offset_changed = global_offset_changed;
1601 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001602 } else {
1603 stall_count++;
1604 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001605
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001606 if (terminate_after_phase)
1607 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001608
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001609 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001610 /* We get here if the labels don't converge
1611 * Example: FOO equ FOO + 1
1612 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001613 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001614 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001615 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001616 nasm_error(ERR_NONFATAL,
1617 "Possible causes: recursive EQUs, macro abuse.");
1618 break;
1619 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001620 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001623 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001624 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001625 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001626 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1627 }
1628}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001629
Ed Berosetfa771012002-06-09 20:56:40 +00001630/**
1631 * gnu style error reporting
1632 * This function prints an error message to error_file in the
1633 * style used by GNU. An example would be:
1634 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001635 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001636 * which the error occurs (or is detected) and "error:" is one of
1637 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001638 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001639 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001640 *
1641 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001642 * @param fmt the printf style format string
1643 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001644static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001645{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001646 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001647 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001648
Ed Berosetfa771012002-06-09 20:56:40 +00001649 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001651
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001652 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001653 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001654 if (!currentfile || (severity & ERR_TOPFILE)) {
1655 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1656 lineno = 0;
1657 }
1658 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001659
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001660 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001661 if (!lineno)
1662 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001663 else
1664 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001665 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001666
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001667 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001668}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001669
Ed Berosetfa771012002-06-09 20:56:40 +00001670/**
1671 * MS style error reporting
1672 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001673 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001674 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001675 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001676 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001677 * which the error occurs (or is detected) and "error:" is one of
1678 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001679 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001680 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001681 *
1682 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001683 * @param fmt the printf style format string
1684 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001685static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001686{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001687 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001688 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001689
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (is_suppressed_warning(severity))
1691 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001692
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001693 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001695
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001696 if (!skip_this_pass(severity)) {
1697 if (currentfile) {
1698 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001699 } else {
1700 fputs("nasm: ", error_file);
1701 }
Ed Berosetfa771012002-06-09 20:56:40 +00001702 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001703
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001704 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001705}
1706
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001707/*
1708 * check to see if this is a suppressable warning
1709 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001710static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001711{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001712 /* Not a warning at all */
1713 if ((severity & ERR_MASK) != ERR_WARNING)
1714 return false;
1715
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001716 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001717}
1718
Ed Berosetfa771012002-06-09 20:56:40 +00001719/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001720 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001721 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001722 * not in pass 1
1723 *
1724 * @param severity the severity of the warning or error
1725 * @return true if we should abort error/warning printing
1726 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001727static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001728{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001729 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001730 if (is_valid_warning(severity))
1731 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1732 else
1733 return false;
1734}
1735
1736static bool warning_is_error(int severity)
1737{
1738 if (is_valid_warning(severity))
1739 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001740 else
1741 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001742}
1743
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001744static bool skip_this_pass(int severity)
1745{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001746 /*
1747 * See if it's a pass-specific error or warning which should be skipped.
1748 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1749 * they cannot be resumed from.
1750 */
1751 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001752 return false;
1753
H. Peter Anvin934f0472016-05-09 12:00:19 -07001754 /*
1755 * passn is 1 on the very first pass only.
1756 * pass0 is 2 on the code-generation (final) pass only.
1757 * These are the passes we care about in this case.
1758 */
1759 return (((severity & ERR_PASS1) && passn != 1) ||
1760 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001761}
1762
Ed Berosetfa771012002-06-09 20:56:40 +00001763/**
1764 * common error reporting
1765 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001766 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001767 * specific error message to error_file and may or may not return. It
1768 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001769 *
1770 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001771 * @param fmt the printf style format string
1772 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001773static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001774{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001775 char msg[1024];
1776 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001777
H. Peter Anvin7df04172008-06-10 18:27:38 -07001778 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001780 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 break;
1782 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001783 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 break;
1785 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001786 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 break;
1788 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001789 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
1791 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001792 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001794 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001795 pfx = "";
1796 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001797 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001798
H. Peter Anvin934f0472016-05-09 12:00:19 -07001799 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001800 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001801 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001802 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1803 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001804
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001805 if (!skip_this_pass(severity))
1806 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001807
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001808 /* Are we recursing from error_list_macros? */
1809 if (severity & ERR_PP_LISTMACRO)
1810 return;
1811
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001812 /*
1813 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001814 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001815 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001816 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001817
H. Peter Anvin8f622462017-04-02 19:02:29 -07001818 if (skip_this_pass(severity))
1819 return;
1820
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001821 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001822 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001823
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001824 preproc->error_list_macros(severity);
1825
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001826 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 case ERR_DEBUG:
1828 /* no further action, by definition */
1829 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001830 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001831 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001832 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001833 terminate_after_phase = true;
1834 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001836 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
1838 case ERR_FATAL:
1839 if (ofile) {
1840 fclose(ofile);
1841 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001842 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 }
1844 if (want_usage)
1845 usage();
1846 exit(1); /* instantly die */
1847 break; /* placate silly compilers */
1848 case ERR_PANIC:
1849 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001850
1851 if (abort_on_panic)
1852 abort(); /* halt, catch fire, dump core/stop debugger */
1853
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001854 if (ofile) {
1855 fclose(ofile);
1856 remove(outname);
1857 ofile = NULL;
1858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 exit(3);
1860 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001861 }
1862}
1863
H. Peter Anvin734b1882002-04-30 21:01:08 +00001864static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001865{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001866 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001867}