blob: f7f02ac89356ed0a3e993dc11c3f1d85da5d4d1e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
94
H. Peter Anvin6867acc2007-10-10 14:58:45 -070095bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080096int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080097static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000098int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080099int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000100
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700101struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800102
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800103const char *inname;
104const char *outname;
105static const char *listname;
106static const char *errname;
107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static int globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700109#define GLOBALLINENO_MAX INT32_MAX
110
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000111/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800112const struct ofmt *ofmt = &OF_DEFAULT;
113const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400114const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000117
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400118FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700119int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100120static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400121
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800122iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123static iflag_t cmd_cpu;
124
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800125struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800126bool in_absolute; /* Flag we are in ABSOLUTE seg */
127struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000128
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000130
H. Peter Anvine2c80182005-01-15 22:15:51 +0000131static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700132static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133
H. Peter Anvine7469712016-02-18 02:20:59 -0800134static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400135
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400136#define OP_NORMAL (1u << 0)
137#define OP_PREPROCESS (1u << 1)
138#define OP_DEPEND (1u << 2)
139
140static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400141
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700142/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700143static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144static bool depend_missing_ok = false;
145static const char *depend_target = NULL;
146static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700147StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000148
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700149static bool want_usage;
150static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800151bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000152
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700153static char *quote_for_pmake(const char *str);
154static char *quote_for_wmake(const char *str);
155static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700156
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700157/*
158 * Execution limits that can be set via a command-line option or %pragma
159 */
160
161#define LIMIT_MAX_VAL (INT_MAX >> 1) /* Effectively unlimited */
162
163int nasm_limit[LIMIT_MAX+1] =
164{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000 };
165
166struct limit_info {
167 const char *name;
168 const char *help;
169};
170static const struct limit_info limit_info[LIMIT_MAX+1] = {
171 { "passes", "total number of passes" },
172 { "stalled-passes", "number of passes without forward progress" },
173 { "macro-levels", "levels of macro expansion"},
174 { "rep", "%rep count" },
175 { "eval", "expression evaluation descent"}
176};
177
178enum directive_result nasm_set_limit(const char *limit, const char *valstr)
179{
180 int i;
181 int64_t val;
182 bool rn_error;
183
184 for (i = 0; i <= LIMIT_MAX; i++) {
185 if (!nasm_stricmp(limit, limit_info[i].name))
186 break;
187 }
188 if (i > LIMIT_MAX) {
189 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA,
190 "unknown limit: `%s'", limit);
191 return DIRR_ERROR;
192 }
193
194 if (!nasm_stricmp(valstr, "unlimited")) {
195 val = LIMIT_MAX_VAL;
196 } else {
197 val = readnum(valstr, &rn_error);
198 if (rn_error || val < 0) {
199 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA,
200 "invalid limit value: `%s'", limit);
201 return DIRR_ERROR;
202 } else if (val > LIMIT_MAX_VAL) {
203 val = LIMIT_MAX_VAL;
204 }
205 }
206
207 nasm_limit[i] = val;
208 return DIRR_OK;
209}
210
H. Peter Anvin892c4812018-05-30 14:43:46 -0700211int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400212{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700213 location.segment = segment;
214 if (segment == NO_SEG) {
215 location.offset = absolute.offset;
216 in_absolute = true;
217 } else {
218 location.offset = raa_read(offsets, segment);
219 in_absolute = false;
220 }
221 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400222}
223
224static void set_curr_offs(int64_t l_off)
225{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800226 if (in_absolute)
227 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400228 else
229 offsets = raa_write(offsets, location.segment, l_off);
230}
231
H. Peter Anvin892c4812018-05-30 14:43:46 -0700232static void increment_offset(int64_t delta)
233{
234 if (unlikely(delta == 0))
235 return;
236
237 location.offset += delta;
238 set_curr_offs(location.offset);
239}
240
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000241static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000242{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000243 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800245 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000246 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000248}
249
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800250static void define_macros_early(void)
251{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700252 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800253 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700255 if (oct->have_local) {
256 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400257 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700258 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400259 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700260 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700262 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400263 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 }
265
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266 if (oct->have_gm) {
267 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700269 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400270 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700273 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400274 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700276
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 if (oct->have_posix) {
278 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280 }
281}
282
283static void define_macros_late(void)
284{
285 char temp[128];
286
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400287 /*
288 * In case if output format is defined by alias
289 * we have to put shortname of the alias itself here
290 * otherwise ABI backward compatibility gets broken.
291 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400292 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400293 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400294 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800295}
296
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700297static void emit_dependencies(StrList *list)
298{
299 FILE *deps;
300 int linepos, len;
301 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700302 bool wmake = (quote_for_make == quote_for_wmake);
303 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700304
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700305 wrapstr = wmake ? " &\n " : " \\\n ";
306 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700307
308 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700309 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400310 if (!deps) {
311 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
312 "unable to write dependency file `%s'", depend_file);
313 return;
314 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700315 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400316 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700317 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700318
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700319 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400320 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700321 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400322 len = strlen(file);
323 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700324 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400325 linepos = 1;
326 }
327 fprintf(deps, " %s", file);
328 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700329 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700330 }
331 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700332
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400333 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700334 if (depend_emit_phony) {
335 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700336 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700337 nasm_free(file);
338 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400339 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700340 }
341
342 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400343 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700344}
345
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700346/* Convert a struct tm to a POSIX-style time constant */
347static int64_t make_posix_time(const struct tm *tm)
348{
349 int64_t t;
350 int64_t y = tm->tm_year;
351
352 /* See IEEE 1003.1:2004, section 4.14 */
353
354 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
355 t += tm->tm_yday;
356 t *= 24;
357 t += tm->tm_hour;
358 t *= 60;
359 t += tm->tm_min;
360 t *= 60;
361 t += tm->tm_sec;
362
363 return t;
364}
365
366static void timestamp(void)
367{
368 struct compile_time * const oct = &official_compile_time;
369 const struct tm *tp, *best_gm;
370
371 time(&oct->t);
372
373 best_gm = NULL;
374
375 tp = localtime(&oct->t);
376 if (tp) {
377 oct->local = *tp;
378 best_gm = &oct->local;
379 oct->have_local = true;
380 }
381
382 tp = gmtime(&oct->t);
383 if (tp) {
384 oct->gm = *tp;
385 best_gm = &oct->gm;
386 oct->have_gm = true;
387 if (!oct->have_local)
388 oct->local = oct->gm;
389 } else {
390 oct->gm = oct->local;
391 }
392
393 if (best_gm) {
394 oct->posix = make_posix_time(best_gm);
395 oct->have_posix = true;
396 }
397}
398
H. Peter Anvin038d8612007-04-12 16:54:50 +0000399int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000400{
H. Peter Anvina771be82017-08-16 14:53:18 -0700401 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700402
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700403 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800404
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800405 iflag_set_default_cpu(&cpu);
406 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400407
Charles Crayne2581c862008-09-10 19:21:52 -0700408 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700409 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400410 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000411
H. Peter Anvin97e15752007-09-25 08:47:47 -0700412 error_file = stderr;
413
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700414 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700415 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700416
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000417 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000418 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400421 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422
H. Peter Anvin55568c12016-10-03 19:46:49 -0700423 parse_cmdline(argc, argv, 1);
424 if (terminate_after_phase) {
425 if (want_usage)
426 usage();
427 return 1;
428 }
429
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700430 /*
431 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700432 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700433 */
434 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800435 define_macros_early();
436
H. Peter Anvin55568c12016-10-03 19:46:49 -0700437 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 if (terminate_after_phase) {
439 if (want_usage)
440 usage();
441 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442 }
443
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800444 /* Save away the default state of warnings */
445 memcpy(warning_state_init, warning_state, sizeof warning_state);
446
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800447 if (!using_debug_info) {
448 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800449 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800450 } else if (!debug_format) {
451 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800452 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800453 } else {
454 dfmt = dfmt_find(ofmt, debug_format);
455 if (!dfmt) {
456 nasm_fatal(ERR_NOFILE | ERR_USAGE,
457 "unrecognized debug format `%s' for"
458 " output format `%s'",
459 debug_format, ofmt->shortname);
460 }
461 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000462
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400464 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000465
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800466 /* no output file name? */
467 if (!outname)
468 outname = filename_set_extension(inname, ofmt->extension);
469
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000470 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800471 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000472
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400473 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700474
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700475 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400476 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700477
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400478 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000479 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700480
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400481 if (depend_missing_ok)
482 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700483
H. Peter Anvin130736c2016-02-17 20:27:41 -0800484 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 while ((line = preproc->getline()))
487 nasm_free(line);
488 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400489 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000490 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700491 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000492 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800495 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700496 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800498 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 "unable to open output file `%s'",
500 outname);
501 } else
502 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000503
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700504 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000505
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400506 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800507 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800508
509 /* Revert all warnings to the default state */
510 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700511
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 while ((line = preproc->getline())) {
513 /*
514 * We generate %line directives if needed for later programs
515 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000516 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 int altline = src_get(&linnum, &file_name);
518 if (altline) {
519 if (altline == 1 && lineinc == 1)
520 nasm_fputs("", ofile);
521 else {
522 lineinc = (altline != -1 || lineinc != 1);
523 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000524 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 file_name);
526 }
527 prior_linnum = linnum;
528 }
529 nasm_fputs(line, ofile);
530 nasm_free(line);
531 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 preproc->cleanup(0);
533 if (ofile)
534 fclose(ofile);
535 if (ofile && terminate_after_phase)
536 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400537 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400538 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400540 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700541 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400542 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800543 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400544 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400546 /*
547 * We must call init_labels() before ofmt->init() since
548 * some object formats will want to define labels in their
549 * init routines. (eg OS/2 defines the FLAT group)
550 */
551 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400553 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400554 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400556 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200557
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400558 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800559 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400560 cleanup_labels();
561 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700562 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400563 nasm_error(ERR_NONFATAL|ERR_NOFILE,
564 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700565 terminate_after_phase = true;
566 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400567 }
568
569 if (ofile) {
570 fclose(ofile);
571 if (terminate_after_phase)
572 remove(outname);
573 ofile = NULL;
574 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000575 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000576
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700577 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400578 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000580 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000582
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 raa_free(offsets);
584 saa_free(forwrefs);
585 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000586 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700587 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000588
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700589 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000590}
591
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592/*
593 * Get a parameter for a command line option.
594 * First arg must be in the form of e.g. -f...
595 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800596static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800598 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400599 if (p[2]) /* the parameter's in the option */
600 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800602 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400605 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607 return NULL;
608}
609
H. Peter Anvindc242712007-11-18 11:55:10 -0800610/*
611 * Copy a filename
612 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800613static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800614{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800615 if (*dst)
616 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800617
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800618 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800619}
620
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700621/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700622 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700623 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700624static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700625{
626 const char *p;
627 char *os, *q;
628
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400629 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700630 size_t nbs = 0;
631
632 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400633 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700634
635 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400636 switch (*p) {
637 case ' ':
638 case '\t':
639 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
640 n += nbs + 2;
641 nbs = 0;
642 break;
643 case '$':
644 case '#':
645 nbs = 0;
646 n += 2;
647 break;
648 case '\\':
649 nbs++;
650 n++;
651 break;
652 default:
653 nbs = 0;
654 n++;
655 break;
656 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700657 }
658
659 /* Convert N backslashes at the end of filename to 2N backslashes */
660 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400661 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700662
663 os = q = nasm_malloc(n);
664
665 nbs = 0;
666 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400667 switch (*p) {
668 case ' ':
669 case '\t':
670 while (nbs--)
671 *q++ = '\\';
672 *q++ = '\\';
673 *q++ = *p;
674 break;
675 case '$':
676 *q++ = *p;
677 *q++ = *p;
678 nbs = 0;
679 break;
680 case '#':
681 *q++ = '\\';
682 *q++ = *p;
683 nbs = 0;
684 break;
685 case '\\':
686 *q++ = *p;
687 nbs++;
688 break;
689 default:
690 *q++ = *p;
691 nbs = 0;
692 break;
693 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700694 }
695 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400696 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700697
698 *q = '\0';
699
700 return os;
701}
702
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700703/*
704 * Convert a string to a Watcom make-safe form
705 */
706static char *quote_for_wmake(const char *str)
707{
708 const char *p;
709 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700710 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700711
712 size_t n = 1; /* Terminating zero */
713
714 if (!str)
715 return NULL;
716
717 for (p = str; *p; p++) {
718 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700719 case ' ':
720 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700721 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700722 quote = true;
723 n++;
724 break;
725 case '\"':
726 quote = true;
727 n += 2;
728 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700729 case '$':
730 case '#':
731 n += 2;
732 break;
733 default:
734 n++;
735 break;
736 }
737 }
738
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700739 if (quote)
740 n += 2;
741
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700742 os = q = nasm_malloc(n);
743
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700744 if (quote)
745 *q++ = '\"';
746
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700747 for (p = str; *p; p++) {
748 switch (*p) {
749 case '$':
750 case '#':
751 *q++ = '$';
752 *q++ = *p;
753 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700754 case '\"':
755 *q++ = *p;
756 *q++ = *p;
757 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700758 default:
759 *q++ = *p;
760 break;
761 }
762 }
763
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 if (quote)
765 *q++ = '\"';
766
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700767 *q = '\0';
768
769 return os;
770}
771
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100772enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800773 OPT_BOGUS,
774 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700775 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800776 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700777 OPT_MANGLE,
778 OPT_INCLUDE,
779 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700780 OPT_BEFORE,
781 OPT_LIMIT
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100782};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800783struct textargs {
784 const char *label;
785 enum text_options opt;
786 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700787 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800788};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800789static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700790 {"v", OPT_VERSION, false, 0},
791 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700792 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700793 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
794 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
795 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
796 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
797 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
798 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
799 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700800 {"include", OPT_INCLUDE, true, 0},
801 {"pragma", OPT_PRAGMA, true, 0},
802 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700803 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700804 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000805};
806
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400807static void show_version(void)
808{
809 printf("NASM version %s compiled on %s%s\n",
810 nasm_version, nasm_date, nasm_compile_options);
811 exit(0);
812}
813
H. Peter Anvin423e3812007-11-15 17:12:29 -0800814static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700815static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000816{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000817 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800818 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000819
820 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800821 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400824 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
825 /* These parameters take values */
826 if (!(param = get_param(p, q, &advance)))
827 return advance;
828 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800829
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 switch (p[1]) {
831 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700832 if (pass == 1)
833 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700835
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700837 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800838 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400839 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700840
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400841 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700842 if (pass == 1) {
843 ofmt = ofmt_find(param, &ofmt_alias);
844 if (!ofmt) {
845 nasm_fatal(ERR_NOFILE | ERR_USAGE,
846 "unrecognised output format `%s' - "
847 "use -hf for a list", param);
848 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400849 }
850 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700853 if (pass == 2) {
854 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700855
H. Peter Anvin55568c12016-10-03 19:46:49 -0700856 if (!*param) {
857 /* Naked -O == -Ox */
858 optimizing = MAX_OPTIMIZE;
859 } else {
860 while (*param) {
861 switch (*param) {
862 case '0': case '1': case '2': case '3': case '4':
863 case '5': case '6': case '7': case '8': case '9':
864 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700865
H. Peter Anvin55568c12016-10-03 19:46:49 -0700866 /* -O0 -> optimizing == -1, 0.98 behaviour */
867 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
868 if (opt < 2)
869 optimizing = opt - 1;
870 else
871 optimizing = opt;
872 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700873
H. Peter Anvin55568c12016-10-03 19:46:49 -0700874 case 'v':
875 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 param++;
877 opt_verbose_info = true;
878 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700879
H. Peter Anvin55568c12016-10-03 19:46:49 -0700880 case 'x':
881 param++;
882 optimizing = MAX_OPTIMIZE;
883 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700884
H. Peter Anvin55568c12016-10-03 19:46:49 -0700885 default:
886 nasm_fatal(0,
887 "unknown optimization option -O%c\n",
888 *param);
889 break;
890 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400891 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700892 if (optimizing > MAX_OPTIMIZE)
893 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400894 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 }
896 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800897
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 case 'p': /* pre-include */
899 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700900 if (pass == 2)
901 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800903
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400904 case 'd': /* pre-define */
905 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906 if (pass == 2)
907 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800909
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 case 'u': /* un-define */
911 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700912 if (pass == 2)
913 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400914 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800915
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 case 'i': /* include search path */
917 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700918 if (pass == 2)
919 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400920 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800921
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400922 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700923 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800924 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800926
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700928 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800929 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800931
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700933 if (pass == 2) {
934 using_debug_info = true;
935 debug_format = param;
936 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400937 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800938
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 if (pass == 1) {
941 if (nasm_stricmp("vc", param) == 0)
942 nasm_set_verror(nasm_verror_vc);
943 else if (nasm_stricmp("gnu", param) == 0)
944 nasm_set_verror(nasm_verror_gnu);
945 else
946 nasm_fatal(ERR_NOFILE | ERR_USAGE,
947 "unrecognized error reporting format `%s'",
948 param);
949 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700953 if (pass == 2) {
954 using_debug_info = true;
955 if (p[2])
956 debug_format = nasm_skip_spaces(p + 2);
957 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800959
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700961 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400962 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800964
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 case 'y':
966 printf("\nvalid debug formats for '%s' output format are"
967 " ('*' denotes default):\n", ofmt->shortname);
968 dfmt_list(ofmt, stdout);
969 exit(0);
970 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800971
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700973 if (pass == 2)
974 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800976
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400978 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800980
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400981 case 'e': /* preprocess only */
982 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700983 if (pass == 1)
984 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800986
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700988 if (pass == 1)
989 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800991
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800992 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800995 if (!set_warning_status(param)) {
996 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
997 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700998 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001000 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001003 if (pass == 1) {
1004 switch (p[2]) {
1005 case 'W':
1006 quote_for_make = quote_for_wmake;
1007 break;
1008 case 'D':
1009 case 'F':
1010 case 'T':
1011 case 'Q':
1012 advance = true;
1013 break;
1014 default:
1015 break;
1016 }
1017 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001018 switch (p[2]) {
1019 case 0:
1020 operating_mode = OP_DEPEND;
1021 break;
1022 case 'G':
1023 operating_mode = OP_DEPEND;
1024 depend_missing_ok = true;
1025 break;
1026 case 'P':
1027 depend_emit_phony = true;
1028 break;
1029 case 'D':
1030 operating_mode = OP_NORMAL;
1031 depend_file = q;
1032 advance = true;
1033 break;
1034 case 'F':
1035 depend_file = q;
1036 advance = true;
1037 break;
1038 case 'T':
1039 depend_target = q;
1040 advance = true;
1041 break;
1042 case 'Q':
1043 depend_target = quote_for_make(q);
1044 advance = true;
1045 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001046 case 'W':
1047 /* handled in pass 1 */
1048 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001049 default:
1050 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1051 "unknown dependency option `-M%c'", p[2]);
1052 break;
1053 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001054 }
1055 if (advance && (!q || !q[0])) {
1056 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1057 "option `-M%c' requires a parameter", p[2]);
1058 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001059 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 case '-':
1063 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001064 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001065 size_t olen, plen;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001066
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001067 p += 2;
1068
1069 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001070 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 break;
1072 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001073
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001074 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001075 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001076 olen = strlen(tx->label);
1077
1078 if (olen > plen)
1079 continue;
1080
1081 if (nasm_memicmp(p, tx->label, olen))
1082 continue;
1083
1084 if (tx->label[olen-1] == '-')
1085 break; /* Incomplete option */
1086
1087 if (!p[olen] || p[olen] == '=')
1088 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001090
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001091 if (!tx->label) {
1092 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1093 "unrecognized option `--%s'", p);
1094 }
1095
1096 param = strchr(p+olen, '=');
1097 if (param)
1098 *param++ = '\0';
1099
H. Peter Anvin3366e312018-02-07 14:14:36 -08001100 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001101 if (!param) {
1102 param = q;
1103 advance = true;
1104 }
1105
1106 /* Note: a null string is a valid parameter */
1107 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001108 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001109 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001110 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
1112 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001113 } else {
1114 if (param) {
1115 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1116 "option `--%s' does not take an argument",
1117 p);
1118
1119 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001120 }
1121
1122 switch (tx->opt) {
1123 case OPT_VERSION:
1124 show_version();
1125 break;
1126 case OPT_ABORT_ON_PANIC:
1127 abort_on_panic = true;
1128 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001129 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001130 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001131 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001132 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001133 case OPT_INCLUDE:
1134 if (pass == 2)
1135 preproc->pre_include(q);
1136 break;
1137 case OPT_PRAGMA:
1138 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001139 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001140 break;
1141 case OPT_BEFORE:
1142 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001143 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001144 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001145 case OPT_LIMIT:
1146 if (pass == 2)
1147 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001148 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001149 case OPT_HELP:
1150 help(0);
1151 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001152 default:
1153 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 }
1155 break;
1156 }
1157
1158 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001159 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1160 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 break;
1162 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001163 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001164 /* In theory we could allow multiple input files... */
1165 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001166 }
1167
1168 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001169}
1170
H. Peter Anvineba20a72002-04-30 20:53:55 +00001171#define ARG_BUF_DELTA 128
1172
H. Peter Anvin55568c12016-10-03 19:46:49 -07001173static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001174{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001175 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001176 int bufsize, prevargsize;
1177
1178 bufsize = prevargsize = ARG_BUF_DELTA;
1179 buffer = nasm_malloc(ARG_BUF_DELTA);
1180 prevarg = nasm_malloc(ARG_BUF_DELTA);
1181 prevarg[0] = '\0';
1182
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 p = buffer;
1185 while (1) { /* Loop to handle long lines */
1186 q = fgets(p, bufsize - (p - buffer), rfile);
1187 if (!q)
1188 break;
1189 p += strlen(p);
1190 if (p > buffer && p[-1] == '\n')
1191 break;
1192 if (p - buffer > bufsize - 10) {
1193 int offset;
1194 offset = p - buffer;
1195 bufsize += ARG_BUF_DELTA;
1196 buffer = nasm_realloc(buffer, bufsize);
1197 p = buffer + offset;
1198 }
1199 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001200
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 if (!q && p == buffer) {
1202 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001203 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 nasm_free(buffer);
1205 nasm_free(prevarg);
1206 return;
1207 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 /*
1210 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1211 * them are present at the end of the line.
1212 */
1213 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001215 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001217
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001218 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001219
H. Peter Anvin55568c12016-10-03 19:46:49 -07001220 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001222
Charles Crayne192d5b52007-10-18 19:02:42 -07001223 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 prevargsize += ARG_BUF_DELTA;
1225 prevarg = nasm_realloc(prevarg, prevargsize);
1226 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001227 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001228 }
1229}
1230
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001231/* Function to process args from a string of args, rather than the
1232 * argv array. Used by the environment variable and response file
1233 * processing.
1234 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001235static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001237 char *p, *q, *arg, *prevarg;
1238 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001239
1240 p = args;
1241 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001243 arg = NULL;
1244 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 q = p;
1246 while (*p && *p != separator)
1247 p++;
1248 while (*p == separator)
1249 *p++ = '\0';
1250 prevarg = arg;
1251 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001252 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001254 }
1255 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001256 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001257}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001258
H. Peter Anvin55568c12016-10-03 19:46:49 -07001259static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001260{
1261 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001262 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001263 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001264 perror(file);
1265 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001266 }
1267 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001268 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001269 }
1270 fclose(f);
1271}
1272
H. Peter Anvin55568c12016-10-03 19:46:49 -07001273static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001274{
1275 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001276 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001277 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001278
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001279 /* Initialize all the warnings to their default state */
1280 for (i = 0; i < ERR_WARN_ALL; i++) {
1281 warning_state_init[i] = warning_state[i] =
1282 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1283 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001284
1285 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001286 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001287 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001288 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001289 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001291 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001293 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001294
1295 /*
1296 * Now process the actual command line.
1297 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001299 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 argv++;
1301 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001302 /*
1303 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 * arguments like the environment variable. This allows us
1305 * to have multiple arguments on a single line, which is
1306 * different to the -@resp file processing below for regular
1307 * NASM.
1308 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001309 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 argc--;
1311 argv++;
1312 }
1313 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001314 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001315 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001316 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001317 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001318 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 fclose(rfile);
1320 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001321 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 "unable to open response file `%s'", p);
1323 }
1324 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001325 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001326 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001327 }
1328
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001329 /*
1330 * Look for basic command line typos. This definitely doesn't
1331 * catch all errors, but it might help cases of fumbled fingers.
1332 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001333 if (pass != 2)
1334 return;
1335
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001336 if (!inname)
1337 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001338
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001339 else if ((errname && !strcmp(inname, errname)) ||
1340 (outname && !strcmp(inname, outname)) ||
1341 (listname && !strcmp(inname, listname)) ||
1342 (depend_file && !strcmp(inname, depend_file)))
1343 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1344
1345 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001346 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001347 if (!error_file) {
1348 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001349 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001350 "cannot open file `%s' for error messages",
1351 errname);
1352 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001353 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001354}
1355
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001356static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001357{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001358 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001360 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001361 uint64_t prev_offset_changed;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001362 int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001363
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001364 switch (cmd_sb) {
1365 case 16:
1366 break;
1367 case 32:
1368 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1369 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1370 break;
1371 case 64:
1372 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1373 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1374 break;
1375 default:
1376 panic();
1377 break;
1378 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001379
H. Peter Anvin73482482018-06-11 14:54:14 -07001380 /* Any segment numbers allocated before this point are permanent */
1381 seg_alloc_setup_done();
1382
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001383 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001384 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001385 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001386 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1387 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001388
H. Peter Anvincac0b192017-03-28 16:12:30 -07001389 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 cpu = cmd_cpu;
1391 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001392 lfmt->init(listname);
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001393 } else if (passn == 1 && listname) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001394 /* Remove the list file in case we die before the output pass */
1395 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001397 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001398 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin892c4812018-05-30 14:43:46 -07001399 seg_alloc_reset();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001400 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 saa_rewind(forwrefs);
1402 forwref = saa_rstruct(forwrefs);
1403 raa_free(offsets);
1404 offsets = raa_init();
1405 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001406 location.segment = NO_SEG;
1407 location.offset = 0;
1408 if (passn == 1)
1409 location.known = true;
1410 ofmt->reset();
1411 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001412 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001413
1414 /* Revert all warnings to the default state */
1415 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001418
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001420 if (globallineno++ == GLOBALLINENO_MAX)
1421 nasm_error(ERR_FATAL,
1422 "overall line number reaches the maximum %d\n",
1423 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001425 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001426 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001427 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001429 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001430 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001431
H. Peter Anvinc7131682017-03-07 17:45:01 -08001432 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001433 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001434
1435 if (optimizing > 0) {
1436 if (forwref != NULL && globallineno == forwref->lineno) {
1437 output_ins.forw_ref = true;
1438 do {
1439 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1440 forwref = saa_rstruct(forwrefs);
1441 } while (forwref != NULL
1442 && forwref->lineno == globallineno);
1443 } else
1444 output_ins.forw_ref = false;
1445
1446 if (output_ins.forw_ref) {
1447 if (passn == 1) {
1448 for (i = 0; i < output_ins.operands; i++) {
1449 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1450 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1451 fwinf->lineno = globallineno;
1452 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001453 }
1454 }
1455 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001456 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001457 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001458
H. Peter Anvinc7131682017-03-07 17:45:01 -08001459 /* forw_ref */
1460 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001461 if (!output_ins.label)
1462 nasm_error(ERR_NONFATAL,
1463 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001464
H. Peter Anvin98578072018-06-01 18:02:54 -07001465 if (output_ins.operands == 1 &&
1466 (output_ins.oprs[0].type & IMMEDIATE) &&
1467 output_ins.oprs[0].wrt == NO_SEG) {
1468 define_label(output_ins.label,
1469 output_ins.oprs[0].segment,
1470 output_ins.oprs[0].offset, false);
1471 } else if (output_ins.operands == 2
1472 && (output_ins.oprs[0].type & IMMEDIATE)
1473 && (output_ins.oprs[0].type & COLON)
1474 && output_ins.oprs[0].segment == NO_SEG
1475 && output_ins.oprs[0].wrt == NO_SEG
1476 && (output_ins.oprs[1].type & IMMEDIATE)
1477 && output_ins.oprs[1].segment == NO_SEG
1478 && output_ins.oprs[1].wrt == NO_SEG) {
1479 define_label(output_ins.label,
1480 output_ins.oprs[0].offset | SEG_ABS,
1481 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001482 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001483 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001484 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001485 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001486 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001488 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001489
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001490 for (n = 1; n <= output_ins.times; n++) {
1491 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001492 int64_t l = insn_size(location.segment,
1493 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001494 globalbits, &output_ins);
1495
1496 /* if (using_debug_info) && output_ins.opcode != -1) */
1497 if (using_debug_info)
1498 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001499 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001500 int32_t typeinfo =
1501 TYS_ELEMENTS(output_ins.operands);
1502 switch (output_ins.opcode) {
1503 case I_RESB:
1504 typeinfo =
1505 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1506 break;
1507 case I_RESW:
1508 typeinfo =
1509 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1510 break;
1511 case I_RESD:
1512 typeinfo =
1513 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1514 break;
1515 case I_RESQ:
1516 typeinfo =
1517 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1518 break;
1519 case I_REST:
1520 typeinfo =
1521 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1522 break;
1523 case I_RESO:
1524 typeinfo =
1525 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1526 break;
1527 case I_RESY:
1528 typeinfo =
1529 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1530 break;
1531 case I_RESZ:
1532 typeinfo =
1533 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1534 break;
1535 case I_DB:
1536 typeinfo |= TY_BYTE;
1537 break;
1538 case I_DW:
1539 typeinfo |= TY_WORD;
1540 break;
1541 case I_DD:
1542 if (output_ins.eops_float)
1543 typeinfo |= TY_FLOAT;
1544 else
1545 typeinfo |= TY_DWORD;
1546 break;
1547 case I_DQ:
1548 typeinfo |= TY_QWORD;
1549 break;
1550 case I_DT:
1551 typeinfo |= TY_TBYTE;
1552 break;
1553 case I_DO:
1554 typeinfo |= TY_OWORD;
1555 break;
1556 case I_DY:
1557 typeinfo |= TY_YWORD;
1558 break;
1559 case I_DZ:
1560 typeinfo |= TY_ZWORD;
1561 break;
1562 default:
1563 typeinfo = TY_LABEL;
1564 break;
1565 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001566
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001567 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001568 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001569
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001570 /*
1571 * For INCBIN, let the code in assemble
1572 * handle TIMES, so we don't have to read the
1573 * input file over and over.
1574 */
1575 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001576 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001577 }
1578 /*
1579 * else l == -1 => invalid instruction, which will be
1580 * flagged as an error on pass 2
1581 */
1582 } else {
1583 if (n == 2)
1584 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001585 increment_offset(assemble(location.segment,
1586 location.offset,
1587 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001589 } /* not an EQU */
1590 }
1591 if (output_ins.times > 1)
1592 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001593
H. Peter Anvinc7131682017-03-07 17:45:01 -08001594 cleanup_insn(&output_ins);
1595
1596 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001599
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001600 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001601 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001602 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001603
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 if (pass1 == 1)
1605 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001606
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001607 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001609 } else if (global_offset_changed &&
1610 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001611 prev_offset_changed = global_offset_changed;
1612 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001613 } else {
1614 stall_count++;
1615 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001616
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001617 if (terminate_after_phase)
1618 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001619
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001620 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1621 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001622 /* We get here if the labels don't converge
1623 * Example: FOO equ FOO + 1
1624 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001625 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001626 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001627 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001628 nasm_error(ERR_NONFATAL,
1629 "Possible causes: recursive EQUs, macro abuse.");
1630 break;
1631 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001632 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001635 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001636 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001637 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001638 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1639 }
1640}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001641
Ed Berosetfa771012002-06-09 20:56:40 +00001642/**
1643 * gnu style error reporting
1644 * This function prints an error message to error_file in the
1645 * style used by GNU. An example would be:
1646 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001647 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001648 * which the error occurs (or is detected) and "error:" is one of
1649 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001650 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001651 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001652 *
1653 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001654 * @param fmt the printf style format string
1655 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001656static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001657{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001658 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001659 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001660
Ed Berosetfa771012002-06-09 20:56:40 +00001661 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001663
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001664 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001665 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001666 if (!currentfile || (severity & ERR_TOPFILE)) {
1667 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1668 lineno = 0;
1669 }
1670 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001671
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001672 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001673 if (!lineno)
1674 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001675 else
1676 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001677 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001678
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001679 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001680}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001681
Ed Berosetfa771012002-06-09 20:56:40 +00001682/**
1683 * MS style error reporting
1684 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001685 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001686 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001687 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001688 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001689 * which the error occurs (or is detected) and "error:" is one of
1690 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001691 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001692 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001693 *
1694 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001695 * @param fmt the printf style format string
1696 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001697static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001698{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001699 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001700 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001701
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 if (is_suppressed_warning(severity))
1703 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001704
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001705 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001707
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001708 if (!skip_this_pass(severity)) {
1709 if (currentfile) {
1710 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001711 } else {
1712 fputs("nasm: ", error_file);
1713 }
Ed Berosetfa771012002-06-09 20:56:40 +00001714 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001715
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001716 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001717}
1718
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001719/*
1720 * check to see if this is a suppressable warning
1721 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001722static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001723{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001724 /* Not a warning at all */
1725 if ((severity & ERR_MASK) != ERR_WARNING)
1726 return false;
1727
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001728 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001729}
1730
Ed Berosetfa771012002-06-09 20:56:40 +00001731/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001732 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001733 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001734 * not in pass 1
1735 *
1736 * @param severity the severity of the warning or error
1737 * @return true if we should abort error/warning printing
1738 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001739static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001740{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001741 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001742 if (is_valid_warning(severity))
1743 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1744 else
1745 return false;
1746}
1747
1748static bool warning_is_error(int severity)
1749{
1750 if (is_valid_warning(severity))
1751 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001752 else
1753 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001754}
1755
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001756static bool skip_this_pass(int severity)
1757{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001758 /*
1759 * See if it's a pass-specific error or warning which should be skipped.
1760 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1761 * they cannot be resumed from.
1762 */
1763 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001764 return false;
1765
H. Peter Anvin934f0472016-05-09 12:00:19 -07001766 /*
1767 * passn is 1 on the very first pass only.
1768 * pass0 is 2 on the code-generation (final) pass only.
1769 * These are the passes we care about in this case.
1770 */
1771 return (((severity & ERR_PASS1) && passn != 1) ||
1772 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001773}
1774
Ed Berosetfa771012002-06-09 20:56:40 +00001775/**
1776 * common error reporting
1777 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001778 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001779 * specific error message to error_file and may or may not return. It
1780 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001781 *
1782 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001783 * @param fmt the printf style format string
1784 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001785static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001786{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001787 char msg[1024];
1788 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001789
H. Peter Anvin7df04172008-06-10 18:27:38 -07001790 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001792 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 break;
1794 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001795 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
1797 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001798 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
1800 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001801 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 break;
1803 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001804 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001806 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001807 pfx = "";
1808 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001809 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001810
H. Peter Anvin934f0472016-05-09 12:00:19 -07001811 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001812 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001813 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001814 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1815 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001816
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001817 if (!skip_this_pass(severity))
1818 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001819
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001820 /* Are we recursing from error_list_macros? */
1821 if (severity & ERR_PP_LISTMACRO)
1822 return;
1823
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001824 /*
1825 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001826 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001827 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001828 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001829
H. Peter Anvin8f622462017-04-02 19:02:29 -07001830 if (skip_this_pass(severity))
1831 return;
1832
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001833 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001834 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001835
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001836 preproc->error_list_macros(severity);
1837
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001838 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 case ERR_DEBUG:
1840 /* no further action, by definition */
1841 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001842 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001843 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001844 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001845 terminate_after_phase = true;
1846 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 break;
1850 case ERR_FATAL:
1851 if (ofile) {
1852 fclose(ofile);
1853 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001854 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 }
1856 if (want_usage)
1857 usage();
1858 exit(1); /* instantly die */
1859 break; /* placate silly compilers */
1860 case ERR_PANIC:
1861 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001862
1863 if (abort_on_panic)
1864 abort(); /* halt, catch fire, dump core/stop debugger */
1865
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001866 if (ofile) {
1867 fclose(ofile);
1868 remove(outname);
1869 ofile = NULL;
1870 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 exit(3);
1872 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001873 }
1874}
1875
H. Peter Anvin734b1882002-04-30 21:01:08 +00001876static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001877{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001878 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001879}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001880
1881static void help(const char xopt)
1882{
1883 int i;
1884
1885 printf
1886 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1887 "[-l listfile]\n"
1888 " [options...] [--] filename\n"
1889 " or nasm -v (or --v) for version info\n\n"
1890 "\n"
1891 "Response files should contain command line parameters,\n"
1892 "one per line.\n"
1893 "\n"
1894 " -t assemble in SciTech TASM compatible mode\n");
1895 printf
1896 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1897 " -a don't preprocess (assemble only)\n"
1898 " -M generate Makefile dependencies on stdout\n"
1899 " -MG d:o, missing files assumed generated\n"
1900 " -MF file set Makefile dependency file\n"
1901 " -MD file assemble and generate dependencies\n"
1902 " -MT file dependency target name\n"
1903 " -MQ file dependency target name (quoted)\n"
1904 " -MP emit phony target\n\n"
1905 " -Zfile redirect error messages to file\n"
1906 " -s redirect error messages to stdout\n\n"
1907 " -g generate debugging information\n\n"
1908 " -F format select a debugging format\n\n"
1909 " -gformat same as -g -F format\n\n"
1910 " -o outfile write output to an outfile\n\n"
1911 " -f format select an output format\n\n"
1912 " -l listfile write listing to a listfile\n\n"
1913 " -Ipath add a pathname to the include file path\n");
1914 printf
1915 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1916 " -O0 no optimization\n"
1917 " -O1 minimal optimization\n"
1918 " -Ox multipass optimization (default)\n"
1919 " -Pfile pre-include a file (also --include)\n"
1920 " -Dmacro[=str] pre-define a macro\n"
1921 " -Umacro undefine a macro\n"
1922 " -Xformat specifiy error reporting format (gnu or vc)\n"
1923 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1924 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1925 " -w[+-]error[=foo]\n"
1926 " promote [specific] warnings to errors\n"
1927 " -h show invocation summary and exit\n\n"
1928 " --pragma str pre-executes a specific %%pragma\n"
1929 " --before str add line (usually a preprocessor statement) before the input\n"
1930 " --prefix str prepend the given string to all the given string\n"
1931 " to all extern, common and global symbols\n"
1932 " --suffix str append the given string to all the given string\n"
1933 " to all extern, common and global symbols\n"
1934 " --lprefix str prepend the given string to all other symbols\n"
1935 " --limit-X val set execution limit X\n");
1936
1937 for (i = 0; i <= LIMIT_MAX; i++) {
1938 printf(" %-15s %s (default ",
1939 limit_info[i].name, limit_info[i].help);
1940 if (nasm_limit[i] < LIMIT_MAX_VAL) {
1941 printf("%d)\n", nasm_limit[i]);
1942 } else {
1943 printf("unlimited)\n");
1944 }
1945 }
1946
1947 printf("\nWarnings for the -W/-w options:\n");
1948
1949 for (i = 0; i <= ERR_WARN_ALL; i++)
1950 printf(" %-23s %s%s\n",
1951 warnings[i].name, warnings[i].help,
1952 i == ERR_WARN_ALL ? "\n" :
1953 warnings[i].enabled ? " (default on)" :
1954 " (default off)");
1955
1956 if (xopt == 'f') {
1957 printf("valid output formats for -f are"
1958 " (`*' denotes default):\n");
1959 ofmt_list(ofmt, stdout);
1960 } else {
1961 printf("For a list of valid output formats, use -hf.\n");
1962 printf("For a list of debug formats, use -f <format> -y.\n");
1963 }
1964}