blob: bb9e2068d7642fe5d0d56719e68eeed015dc2db6 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter 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
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin215186f2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvinbd464742016-02-17 20:47:01 -080082static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040083static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
84static void nasm_verror_vc(int severity, const char *fmt, va_list args);
85static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086static void usage(void);
87
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080088static bool using_debug_info, opt_verbose_info;
89static const char *debug_format;
90
H. Peter Anvin6867acc2007-10-10 14:58:45 -070091bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080092int pass0, passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000093int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080094int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000095
H. Peter Anvin9bd15062009-07-18 21:07:17 -040096static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080097
Keith Kaniosa6dfa782007-04-13 16:47:53 +000098static char inname[FILENAME_MAX];
99static char outname[FILENAME_MAX];
100static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700101static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000103/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400104struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400105struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000109
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400110FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700111int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
112static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400113
114static iflag_t cpu;
115static iflag_t cmd_cpu;
116
Charles Craynec1905c22008-09-11 18:54:06 -0700117int64_t global_offset_changed; /* referenced in labels.c */
118int64_t prev_offset_changed;
119int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120
H. Peter Anvin12e46512007-10-03 21:30:57 -0700121static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123int32_t abs_seg; /* ABSOLUTE segment basis */
124int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000125
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000127
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700129static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400131static struct preproc_ops *preproc;
132
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400133#define OP_NORMAL (1u << 0)
134#define OP_PREPROCESS (1u << 1)
135#define OP_DEPEND (1u << 2)
136
137static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400138
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700140static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700141static bool depend_missing_ok = false;
142static const char *depend_target = NULL;
143static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800147 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200149
H. Peter Anvin972079f2008-09-30 17:01:23 -0700150static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
151static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000152
H. Peter Anvin972079f2008-09-30 17:01:23 -0700153static const struct warning {
154 const char *name;
155 const char *help;
156 bool enabled;
157} warnings[ERR_WARN_MAX+1] = {
158 {"error", "treat warnings as errors", false},
159 {"macro-params", "macro calls with wrong parameter count", true},
160 {"macro-selfref", "cyclic macro references", false},
161 {"macro-defaults", "macros with more default than optional parameters", true},
162 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700163 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700164 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
165 {"float-overflow", "floating point overflow", true},
166 {"float-denorm", "floating point denormal", false},
167 {"float-underflow", "floating point underflow", false},
168 {"float-toolong", "too many digits in floating-point number", true},
169 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800170 {"lock", "lock prefix on unlockable instructions", true},
171 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800172 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800173 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000174};
175
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700176static bool want_usage;
177static bool terminate_after_phase;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800178bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000179
H. Peter Anvin55340992012-09-09 17:09:00 -0700180static char *quote_for_make(const char *str);
181
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400182static int64_t get_curr_offs(void)
183{
184 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
185}
186
187static void set_curr_offs(int64_t l_off)
188{
189 if (in_abs_seg)
190 abs_offset = l_off;
191 else
192 offsets = raa_write(offsets, location.segment, l_off);
193}
194
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000195static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000196{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000197 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800199 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000201 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000202}
203
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800204/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800205static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800206{
207 int64_t t;
208 int64_t y = tm->tm_year;
209
210 /* See IEEE 1003.1:2004, section 4.14 */
211
212 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
213 t += tm->tm_yday;
214 t *= 24;
215 t += tm->tm_hour;
216 t *= 60;
217 t += tm->tm_min;
218 t *= 60;
219 t += tm->tm_sec;
220
221 return t;
222}
223
224static void define_macros_early(void)
225{
226 char temp[128];
227 struct tm lt, *lt_p, gm, *gm_p;
228 int64_t posix_time;
229
230 lt_p = localtime(&official_compile_time);
231 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400232 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800233
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400234 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
239 preproc->pre_define(temp);
240 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
241 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800242 }
243
244 gm_p = gmtime(&official_compile_time);
245 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800247
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400248 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
253 preproc->pre_define(temp);
254 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
255 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700257
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800259 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800261 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400263 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264
265 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400266 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
267 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800268 }
269}
270
271static void define_macros_late(void)
272{
273 char temp[128];
274
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400275 /*
276 * In case if output format is defined by alias
277 * we have to put shortname of the alias itself here
278 * otherwise ABI backward compatibility gets broken.
279 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400280 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400281 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400282 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800283}
284
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700285static void emit_dependencies(StrList *list)
286{
287 FILE *deps;
288 int linepos, len;
289 StrList *l, *nl;
290
291 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400292 deps = fopen(depend_file, "w");
293 if (!deps) {
294 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
295 "unable to write dependency file `%s'", depend_file);
296 return;
297 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400299 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700301
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700302 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400303 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700304 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400305 len = strlen(file);
306 if (linepos + len > 62 && linepos > 1) {
307 fprintf(deps, " \\\n ");
308 linepos = 1;
309 }
310 fprintf(deps, " %s", file);
311 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700312 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700313 }
314 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700315
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400316 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400317 if (depend_emit_phony)
318 fprintf(deps, "%s:\n\n", l->str);
319 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700320 }
321
322 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400323 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700324}
325
H. Peter Anvin038d8612007-04-12 16:54:50 +0000326int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000327{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700328 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700329
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800330 time(&official_compile_time);
331
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400332 iflag_set(&cpu, IF_PLEVEL);
333 iflag_set(&cmd_cpu, IF_PLEVEL);
334
Charles Crayne2581c862008-09-10 19:21:52 -0700335 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700336 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400337 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000338
H. Peter Anvin97e15752007-09-25 08:47:47 -0700339 error_file = stderr;
340
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700341 tolower_init();
342
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400347 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000348
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349 /* Define some macros dependent on the runtime, but not
350 on the command line. */
351 define_macros_early();
352
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 parse_cmdline(argc, argv);
354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 if (terminate_after_phase) {
356 if (want_usage)
357 usage();
358 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 }
360
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800361 if (!using_debug_info) {
362 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvin335c4852016-02-17 20:55:08 -0800363 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800364 } else if (!debug_format) {
365 /* Default debug format for this backend */
H. Peter Anvin335c4852016-02-17 20:55:08 -0800366 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800367 } else {
368 dfmt = dfmt_find(ofmt, debug_format);
369 if (!dfmt) {
370 nasm_fatal(ERR_NOFILE | ERR_USAGE,
371 "unrecognized debug format `%s' for"
372 " output format `%s'",
373 debug_format, ofmt->shortname);
374 }
375 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000376
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400378 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700379 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000382 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800383 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000384
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400385 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700386 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400387 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700388
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400389 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000390 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700391
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400392 if (depend_missing_ok)
393 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700394
H. Peter Anvin215186f2016-02-17 20:27:41 -0800395 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400397 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 while ((line = preproc->getline()))
400 nasm_free(line);
401 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400402 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000403 char *line;
404 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000405 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (*outname) {
409 ofile = fopen(outname, "w");
410 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800411 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 "unable to open output file `%s'",
413 outname);
414 } else
415 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700417 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000418
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400419 /* pass = 1; */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800420 preproc->reset(inname, 3, depend_ptr);
421 memcpy(warning_on, warning_on_global,
422 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 while ((line = preproc->getline())) {
425 /*
426 * We generate %line directives if needed for later programs
427 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000428 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 int altline = src_get(&linnum, &file_name);
430 if (altline) {
431 if (altline == 1 && lineinc == 1)
432 nasm_fputs("", ofile);
433 else {
434 lineinc = (altline != -1 || lineinc != 1);
435 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000436 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 file_name);
438 }
439 prior_linnum = linnum;
440 }
441 nasm_fputs(line, ofile);
442 nasm_free(line);
443 }
444 nasm_free(file_name);
445 preproc->cleanup(0);
446 if (ofile)
447 fclose(ofile);
448 if (ofile && terminate_after_phase)
449 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400450 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400451 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400453 if (operating_mode & OP_NORMAL) {
454 /*
455 * We must call ofmt->filename _anyway_, even if the user
456 * has specified their own output file, because some
457 * formats (eg OBJ and COFF) use ofmt->filename to find out
458 * the name of the input file and then put that inside the
459 * file.
460 */
461 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400463 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
464 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800465 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 /*
469 * We must call init_labels() before ofmt->init() since
470 * some object formats will want to define labels in their
471 * init routines. (eg OS/2 defines the FLAT group)
472 */
473 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400475 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400476 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400478 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200479
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400480 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800481 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400482 cleanup_labels();
483 fflush(ofile);
484 if (ferror(ofile))
485 nasm_error(ERR_NONFATAL|ERR_NOFILE,
486 "write error on output file `%s'", outname);
487 }
488
489 if (ofile) {
490 fclose(ofile);
491 if (terminate_after_phase)
492 remove(outname);
493 ofile = NULL;
494 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000496
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700497 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400498 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700499
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 raa_free(offsets);
504 saa_free(forwrefs);
505 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000506 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000507
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700508 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000509}
510
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511/*
512 * Get a parameter for a command line option.
513 * First arg must be in the form of e.g. -f...
514 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800515static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000516{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800517 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400518 if (p[2]) /* the parameter's in the option */
519 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800521 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400524 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526 return NULL;
527}
528
H. Peter Anvindc242712007-11-18 11:55:10 -0800529/*
530 * Copy a filename
531 */
532static void copy_filename(char *dst, const char *src)
533{
534 size_t len = strlen(src);
535
536 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800537 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400538 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800539 }
540 strncpy(dst, src, FILENAME_MAX);
541}
542
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700543/*
544 * Convert a string to Make-safe form
545 */
546static char *quote_for_make(const char *str)
547{
548 const char *p;
549 char *os, *q;
550
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400551 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700552 size_t nbs = 0;
553
554 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400555 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700556
557 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400558 switch (*p) {
559 case ' ':
560 case '\t':
561 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
562 n += nbs + 2;
563 nbs = 0;
564 break;
565 case '$':
566 case '#':
567 nbs = 0;
568 n += 2;
569 break;
570 case '\\':
571 nbs++;
572 n++;
573 break;
574 default:
575 nbs = 0;
576 n++;
577 break;
578 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579 }
580
581 /* Convert N backslashes at the end of filename to 2N backslashes */
582 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400583 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700584
585 os = q = nasm_malloc(n);
586
587 nbs = 0;
588 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400589 switch (*p) {
590 case ' ':
591 case '\t':
592 while (nbs--)
593 *q++ = '\\';
594 *q++ = '\\';
595 *q++ = *p;
596 break;
597 case '$':
598 *q++ = *p;
599 *q++ = *p;
600 nbs = 0;
601 break;
602 case '#':
603 *q++ = '\\';
604 *q++ = *p;
605 nbs = 0;
606 break;
607 case '\\':
608 *q++ = *p;
609 nbs++;
610 break;
611 default:
612 *q++ = *p;
613 nbs = 0;
614 break;
615 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700616 }
617 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400618 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700619
620 *q = '\0';
621
622 return os;
623}
624
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000626 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000628};
629
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100630enum text_options {
631 OPT_PREFIX,
H. Peter Anvin53f15592016-03-01 22:43:51 -0800632 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100633};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634struct textargs textopts[] = {
635 {"prefix", OPT_PREFIX},
636 {"postfix", OPT_POSTFIX},
637 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000638};
639
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400640static void show_version(void)
641{
642 printf("NASM version %s compiled on %s%s\n",
643 nasm_version, nasm_date, nasm_compile_options);
644 exit(0);
645}
646
H. Peter Anvin423e3812007-11-15 17:12:29 -0800647static bool stopoptions = false;
648static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000650 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800651 int i;
652 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700653 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000654
655 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800656 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000657
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400659 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
660 /* These parameters take values */
661 if (!(param = get_param(p, q, &advance)))
662 return advance;
663 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800664
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 switch (p[1]) {
666 case 's':
667 error_file = stdout;
668 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700669
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 case 'o': /* output file */
671 copy_filename(outname, param);
672 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700673
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400674 case 'f': /* output format */
675 ofmt = ofmt_find(param, &ofmt_alias);
676 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800677 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400678 "unrecognised output format `%s' - "
679 "use -hf for a list", param);
680 }
681 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700682
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400683 case 'O': /* Optimization level */
684 {
685 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700686
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400687 if (!*param) {
688 /* Naked -O == -Ox */
689 optimizing = MAX_OPTIMIZE;
690 } else {
691 while (*param) {
692 switch (*param) {
693 case '0': case '1': case '2': case '3': case '4':
694 case '5': case '6': case '7': case '8': case '9':
695 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700696
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400697 /* -O0 -> optimizing == -1, 0.98 behaviour */
698 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
699 if (opt < 2)
700 optimizing = opt - 1;
701 else
702 optimizing = opt;
703 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700704
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400705 case 'v':
706 case '+':
707 param++;
708 opt_verbose_info = true;
709 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700710
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400711 case 'x':
712 param++;
713 optimizing = MAX_OPTIMIZE;
714 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700715
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400716 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800717 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400718 "unknown optimization option -O%c\n",
719 *param);
720 break;
721 }
722 }
723 if (optimizing > MAX_OPTIMIZE)
724 optimizing = MAX_OPTIMIZE;
725 }
726 break;
727 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'p': /* pre-include */
730 case 'P':
731 preproc->pre_include(param);
732 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800733
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 case 'd': /* pre-define */
735 case 'D':
736 preproc->pre_define(param);
737 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'u': /* un-define */
740 case 'U':
741 preproc->pre_undefine(param);
742 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800743
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 case 'i': /* include search path */
745 case 'I':
746 preproc->include_path(param);
747 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800748
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400749 case 'l': /* listing file */
750 copy_filename(listname, param);
751 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800752
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400753 case 'Z': /* error messages file */
754 copy_filename(errname, param);
755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 case 'F': /* specify debug format */
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400758 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800759 debug_format = param;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400762 case 'X': /* specify error reporting format */
763 if (nasm_stricmp("vc", param) == 0)
764 nasm_set_verror(nasm_verror_vc);
765 else if (nasm_stricmp("gnu", param) == 0)
766 nasm_set_verror(nasm_verror_gnu);
767 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800768 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400769 "unrecognized error reporting format `%s'",
770 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800772
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700774 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800775 if (p[2])
776 debug_format = nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800778
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 case 'h':
780 printf
781 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
782 "[-l listfile]\n"
783 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400784 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800785 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700787 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700789 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400790 " -MG d:o, missing files assumed generated\n"
791 " -MF <file> set Makefile dependency file\n"
792 " -MD <file> assemble and generate dependencies\n"
793 " -MT <file> dependency target name\n"
794 " -MQ <file> dependency target name (quoted)\n"
795 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700796 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800798 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800800 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400801 " -o outfile write output to an outfile\n\n"
802 " -f format select an output format\n\n"
803 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 " -I<path> adds a pathname to the include file path\n");
805 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700806 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400807 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400808 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400809 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 " -P<file> pre-includes a file\n"
811 " -D<macro>[=<value>] pre-defines a macro\n"
812 " -U<macro> undefines a macro\n"
813 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800814 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400815 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400816 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400817 "--prefix,--postfix\n"
818 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100819 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800820 "Warnings:\n");
821 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700823 warnings[i].name, warnings[i].help,
824 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 printf
826 ("\nresponse files should contain command line parameters"
827 ", one per line.\n");
828 if (p[2] == 'f') {
829 printf("\nvalid output formats for -f are"
830 " (`*' denotes default):\n");
831 ofmt_list(ofmt, stdout);
832 } else {
833 printf("\nFor a list of valid output formats, use -hf.\n");
834 printf("For a list of debug formats, use -f <form> -y.\n");
835 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 'y':
840 printf("\nvalid debug formats for '%s' output format are"
841 " ('*' denotes default):\n", ofmt->shortname);
842 dfmt_list(ofmt, stdout);
843 exit(0);
844 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700847 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400851 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'e': /* preprocess only */
855 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400856 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800858
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400859 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400860 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800862
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 case 'W':
864 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
865 do_warn = false;
866 param += 3;
867 } else {
868 do_warn = true;
869 }
870 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400874 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400878 do_warn = (param[0] == '+');
879 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400880
881set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400882 for (i = 0; i <= ERR_WARN_MAX; i++) {
883 if (!nasm_stricmp(param, warnings[i].name))
884 break;
885 }
886 if (i <= ERR_WARN_MAX) {
887 warning_on_global[i] = do_warn;
888 } else if (!nasm_stricmp(param, "all")) {
889 for (i = 1; i <= ERR_WARN_MAX; i++)
890 warning_on_global[i] = do_warn;
891 } else if (!nasm_stricmp(param, "none")) {
892 for (i = 1; i <= ERR_WARN_MAX; i++)
893 warning_on_global[i] = !do_warn;
894 } else {
895 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
896 "invalid warning `%s'", param);
897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800899
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 switch (p[2]) {
902 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400903 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400904 break;
905 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400906 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400907 depend_missing_ok = true;
908 break;
909 case 'P':
910 depend_emit_phony = true;
911 break;
912 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300913 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400914 depend_file = q;
915 advance = true;
916 break;
917 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400918 depend_file = q;
919 advance = true;
920 break;
921 case 'T':
922 depend_target = q;
923 advance = true;
924 break;
925 case 'Q':
926 depend_target = quote_for_make(q);
927 advance = true;
928 break;
929 default:
930 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
931 "unknown dependency option `-M%c'", p[2]);
932 break;
933 }
934 if (advance && (!q || !q[0])) {
935 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
936 "option `-M%c' requires a parameter", p[2]);
937 break;
938 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 case '-':
942 {
943 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000944
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 if (p[2] == 0) { /* -- => stop processing options */
946 stopoptions = 1;
947 break;
948 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400949
950 if (!nasm_stricmp(p, "--v"))
951 show_version();
952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 for (s = 0; textopts[s].label; s++) {
954 if (!nasm_stricmp(p + 2, textopts[s].label)) {
955 break;
956 }
957 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000958
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 switch (s) {
960
961 case OPT_PREFIX:
962 case OPT_POSTFIX:
963 {
964 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400965 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 ERR_USAGE,
967 "option `--%s' requires an argument",
968 p + 2);
969 break;
970 } else {
971 advance = 1, param = q;
972 }
973
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100974 switch (s) {
975 case OPT_PREFIX:
976 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100978 case OPT_POSTFIX:
979 strlcpy(lpostfix, param, POSTFIX_MAX);
980 break;
981 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800982 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100983 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 break;
985 }
986 break;
987 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 default:
990 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400991 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 "unrecognised option `--%s'", p + 2);
993 break;
994 }
995 }
996 break;
997 }
998
999 default:
1000 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001001 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 "unrecognised option `-%c'", p[1]);
1003 break;
1004 }
1005 } else {
1006 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001007 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001009 } else {
1010 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001012 }
1013
1014 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001015}
1016
H. Peter Anvineba20a72002-04-30 20:53:55 +00001017#define ARG_BUF_DELTA 128
1018
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001020{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001021 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001022 int bufsize, prevargsize;
1023
1024 bufsize = prevargsize = ARG_BUF_DELTA;
1025 buffer = nasm_malloc(ARG_BUF_DELTA);
1026 prevarg = nasm_malloc(ARG_BUF_DELTA);
1027 prevarg[0] = '\0';
1028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 p = buffer;
1031 while (1) { /* Loop to handle long lines */
1032 q = fgets(p, bufsize - (p - buffer), rfile);
1033 if (!q)
1034 break;
1035 p += strlen(p);
1036 if (p > buffer && p[-1] == '\n')
1037 break;
1038 if (p - buffer > bufsize - 10) {
1039 int offset;
1040 offset = p - buffer;
1041 bufsize += ARG_BUF_DELTA;
1042 buffer = nasm_realloc(buffer, bufsize);
1043 p = buffer + offset;
1044 }
1045 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 if (!q && p == buffer) {
1048 if (prevarg[0])
1049 process_arg(prevarg, NULL);
1050 nasm_free(buffer);
1051 nasm_free(prevarg);
1052 return;
1053 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 /*
1056 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1057 * them are present at the end of the line.
1058 */
1059 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001061 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001064 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 if (process_arg(prevarg, p))
1067 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068
Charles Crayne192d5b52007-10-18 19:02:42 -07001069 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 prevargsize += ARG_BUF_DELTA;
1071 prevarg = nasm_realloc(prevarg, prevargsize);
1072 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001073 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001074 }
1075}
1076
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001077/* Function to process args from a string of args, rather than the
1078 * argv array. Used by the environment variable and response file
1079 * processing.
1080 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001081static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083 char *p, *q, *arg, *prevarg;
1084 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001085
1086 p = args;
1087 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001089 arg = NULL;
1090 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 q = p;
1092 while (*p && *p != separator)
1093 p++;
1094 while (*p == separator)
1095 *p++ = '\0';
1096 prevarg = arg;
1097 arg = q;
1098 if (process_arg(prevarg, arg))
1099 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001100 }
1101 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001103}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001104
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001105static void process_response_file(const char *file)
1106{
1107 char str[2048];
1108 FILE *f = fopen(file, "r");
1109 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001110 perror(file);
1111 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001112 }
1113 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001114 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001115 }
1116 fclose(f);
1117}
1118
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001119static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001120{
1121 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001122 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001123 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001124
Charles Craynefcce07f2007-09-30 22:15:36 -07001125 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001126
H. Peter Anvin972079f2008-09-30 17:01:23 -07001127 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001128 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001129
1130 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001131 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001133 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001134 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 envcopy = nasm_strdup(envreal);
1136 process_args(envcopy);
1137 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001138 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001139
1140 /*
1141 * Now process the actual command line.
1142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001144 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 argv++;
1146 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001147 /*
1148 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 * arguments like the environment variable. This allows us
1150 * to have multiple arguments on a single line, which is
1151 * different to the -@resp file processing below for regular
1152 * NASM.
1153 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001154 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 argc--;
1156 argv++;
1157 }
1158 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001159 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001161 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001162 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 process_respfile(rfile);
1164 fclose(rfile);
1165 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001166 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 "unable to open response file `%s'", p);
1168 }
1169 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001170 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1171 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001172 }
1173
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001174 /*
1175 * Look for basic command line typos. This definitely doesn't
1176 * catch all errors, but it might help cases of fumbled fingers.
1177 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001178 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001179 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001180 "no input file specified");
1181 else if (!strcmp(inname, errname) ||
1182 !strcmp(inname, outname) ||
1183 !strcmp(inname, listname) ||
1184 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001185 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001186 "file `%s' is both input and output file",
1187 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001188
H. Peter Anvin70653092007-10-19 14:42:29 -07001189 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001190 error_file = fopen(errname, "w");
1191 if (!error_file) {
1192 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001193 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001194 "cannot open file `%s' for error messages",
1195 errname);
1196 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001197 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001198}
1199
H. Peter Anvin70055962007-10-11 00:05:31 -07001200static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001201
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001202static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001203{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001204 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001206 int i, validid;
1207 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001208 int32_t seg;
1209 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001210 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001212 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001213
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001214 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001215 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001216
Charles Craynec1905c22008-09-11 18:54:06 -07001217 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001218 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 int pass1, pass2;
1220 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001222 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001223 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1224 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001226 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227
Keith Kaniosb7a89542007-04-12 02:40:54 +00001228 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 cpu = cmd_cpu;
1230 if (pass0 == 2) {
H. Peter Anvin172b8402016-02-18 01:16:18 -08001231 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001233 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001234 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001236 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001237 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 saa_rewind(forwrefs);
1239 forwref = saa_rstruct(forwrefs);
1240 raa_free(offsets);
1241 offsets = raa_init();
1242 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08001243 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001244 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001247 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001248 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001249 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001250
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001252 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001255 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 * Here we parse our directives; this is not handled by the
1257 * 'real' parser. This really should be a separate function.
1258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001260 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001261 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001262 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001263
H. Peter Anvin70055962007-10-11 00:05:31 -07001264 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001265 case D_SEGMENT: /* [SEGMENT n] */
1266 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 seg = ofmt->section(value, pass2, &sb);
1268 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001269 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001270 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 value);
1272 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001273 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 location.segment = seg;
1275 }
1276 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001277 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001278 if (*value) {
1279 stdscan_reset();
1280 stdscan_set(value);
1281 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001282 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001283 if (e) {
1284 unsigned int align = (unsigned int)e->value;
1285 if ((uint64_t)e->value > 0x7fffffff) {
1286 /*
1287 * FIXME: Please make some sane message here
1288 * ofmt should have some 'check' method which
1289 * would report segment alignment bounds.
1290 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001291 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001292 "incorrect segment alignment `%s'", value);
1293 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001294 nasm_error(ERR_NONFATAL,
1295 "segment alignment `%s' is not power of two",
1296 value);
1297 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001298
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001299 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001300 if (location.segment != NO_SEG)
1301 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001302 }
1303 }
1304 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001305 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 if (*value == '$')
1307 value++; /* skip initial $ if present */
1308 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309 q = value;
1310 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 *q++ = '\0';
1314 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001315 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001316 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001318 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001320 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001323 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 }
1326 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001327 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 "identifier expected after EXTERN");
1329 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001330 }
1331 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 *q++ = '\0';
1333 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001334 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 special = NULL;
1336 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1337 int temp = pass0;
1338 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001339 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001341 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001343 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 } /* else pass0 == 1 */
1345 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001346 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001347 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001349 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 if (*value == '$')
1351 value++; /* skip initial $ if present */
1352 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001353 q = value;
1354 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 *q++ = '\0';
1358 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001359 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001361 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001364 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001367 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 }
1370 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001371 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 "identifier expected after GLOBAL");
1373 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001374 }
1375 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 *q++ = '\0';
1377 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001378 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001380 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 } /* pass == 1 */
1382 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001383 case D_COMMON: /* [COMMON symbol size:special] */
1384 {
1385 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001386
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 if (*value == '$')
1388 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001389 p = value;
1390 validid = true;
1391 if (!isidstart(*p))
1392 validid = false;
1393 while (*p && !nasm_isspace(*p)) {
1394 if (!isidchar(*p))
1395 validid = false;
1396 p++;
1397 }
1398 if (!validid) {
1399 nasm_error(ERR_NONFATAL,
1400 "identifier expected after COMMON");
1401 break;
1402 }
1403 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001404 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001405 q = p;
1406 while (*q && *q != ':')
1407 q++;
1408 if (*q == ':') {
1409 *q++ = '\0';
1410 special = q;
1411 } else {
1412 special = NULL;
1413 }
1414 size = readnum(p, &rn_error);
1415 if (rn_error) {
1416 nasm_error(ERR_NONFATAL,
1417 "invalid size specified"
1418 " in COMMON declaration");
1419 break;
1420 }
1421 } else {
1422 nasm_error(ERR_NONFATAL,
1423 "no size specified in"
1424 " COMMON declaration");
1425 break;
1426 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001427
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001428 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001429 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001430 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001431 if (special)
1432 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 }
1434 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001435 }
1436 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001438 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001440 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 if (e) {
1442 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001443 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 1 ? ERR_NONFATAL : ERR_PANIC,
1445 "cannot use non-relocatable expression as "
1446 "ABSOLUTE address");
1447 else {
1448 abs_seg = reloc_seg(e);
1449 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001451 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 abs_offset = 0x100; /* don't go near zero in case of / */
1453 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001454 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001456 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 location.segment = NO_SEG;
1458 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001459 case D_DEBUG: /* [DEBUG] */
1460 {
1461 char debugid[128];
1462 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 p = value;
1465 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001466 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001467 if (!isidstart(*p)) {
1468 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001469 } else {
1470 while (*p && !nasm_isspace(*p)) {
1471 if (q >= debugid + sizeof debugid - 1) {
1472 overlong = true;
1473 break;
1474 }
1475 if (!isidchar(*p))
1476 badid = true;
1477 *q++ = *p++;
1478 }
1479 *q = 0;
1480 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001481 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001482 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1483 "identifier expected after DEBUG");
1484 break;
1485 }
1486 if (overlong) {
1487 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1488 "DEBUG identifier too long");
1489 break;
1490 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001491 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001492 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001493 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001495 }
1496 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001497 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001498 switch(*value) {
1499 case '-': validid = 0; value++; break;
1500 case '+': validid = 1; value++; break;
1501 case '*': validid = 2; value++; break;
1502 default: validid = 1; break;
1503 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001505 for (i = 1; i <= ERR_WARN_MAX; i++)
1506 if (!nasm_stricmp(value, warnings[i].name))
1507 break;
1508 if (i <= ERR_WARN_MAX) {
1509 switch(validid) {
1510 case 0:
1511 warning_on[i] = false;
1512 break;
1513 case 1:
1514 warning_on[i] = true;
1515 break;
1516 case 2:
1517 warning_on[i] = warning_on_global[i];
1518 break;
1519 }
1520 } else
1521 nasm_error(ERR_NONFATAL,
1522 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001524 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 cpu = get_cpu(value);
1526 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001528 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 if (*value == '+') {
1530 user_nolist = 0;
1531 } else {
1532 if (*value == '-') {
1533 user_nolist = 1;
1534 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001535 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 }
1537 }
1538 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001539 case D_DEFAULT: /* [DEFAULT] */
1540 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001541 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001542 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001543 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001544 switch ((int)tokval.t_integer) {
1545 case S_REL:
1546 globalrel = 1;
1547 break;
1548 case S_ABS:
1549 globalrel = 0;
1550 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001551 case P_BND:
1552 globalbnd = 1;
1553 break;
1554 case P_NOBND:
1555 globalbnd = 0;
1556 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001557 default:
1558 err = 1;
1559 break;
1560 }
1561 } else {
1562 err = 1;
1563 }
1564 break;
1565 case D_FLOAT:
1566 if (float_option(value)) {
1567 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1568 "unknown 'float' directive: %s",
1569 value);
1570 }
1571 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001573 if (ofmt->directive(d, value, pass2))
1574 break;
1575 /* else fall through */
1576 case D_unknown:
1577 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1578 "unrecognised directive [%s]",
1579 directive);
1580 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001582 if (err) {
1583 nasm_error(ERR_NONFATAL,
1584 "invalid parameter to [%s] directive",
1585 directive);
1586 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001588 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589
Charles Crayne2581c862008-09-10 19:21:52 -07001590 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001594 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 forwref = saa_rstruct(forwrefs);
1596 } while (forwref != NULL
1597 && forwref->lineno == globallineno);
1598 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001599 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001600
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001601 if (output_ins.forw_ref) {
1602 if (passn == 1) {
1603 for (i = 0; i < output_ins.operands; i++) {
1604 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1605 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1606 fwinf->lineno = globallineno;
1607 fwinf->operand = i;
1608 }
1609 }
1610 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001611 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 /* forw_ref */
1615 if (output_ins.opcode == I_EQU) {
1616 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 * Special `..' EQUs get processed in pass two,
1619 * except `..@' macro-processor EQUs which are done
1620 * in the normal place.
1621 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001623 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001626 else if (output_ins.label[0] != '.' ||
1627 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 output_ins.label[2] == '@') {
1629 if (output_ins.operands == 1 &&
1630 (output_ins.oprs[0].type & IMMEDIATE) &&
1631 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001632 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 def_label(output_ins.label,
1634 output_ins.oprs[0].segment,
1635 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001636 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001640 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001642 && (output_ins.oprs[1].type & IMMEDIATE)
1643 && output_ins.oprs[1].segment == NO_SEG
1644 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001646 output_ins.oprs[0].offset | SEG_ABS,
1647 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001648 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001650 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001653 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 * Special `..' EQUs get processed here, except
1656 * `..@' macro processor EQUs which are done above.
1657 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 output_ins.label[1] == '.' &&
1660 output_ins.label[2] != '@') {
1661 if (output_ins.operands == 1 &&
1662 (output_ins.oprs[0].type & IMMEDIATE)) {
1663 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664 output_ins.oprs[0].segment,
1665 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001666 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001668 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001670 && output_ins.oprs[0].segment == NO_SEG
1671 && (output_ins.oprs[1].type & IMMEDIATE)
1672 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001674 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001675 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001676 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001678 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001680 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001681 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001685
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001686 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001687 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001688
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 /* if (using_debug_info) && output_ins.opcode != -1) */
1690 if (using_debug_info)
1691 { /* fbk 03/25/01 */
1692 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001693 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 TYS_ELEMENTS(output_ins.operands);
1695 switch (output_ins.opcode) {
1696 case I_RESB:
1697 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001698 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 break;
1700 case I_RESW:
1701 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001702 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 case I_RESD:
1705 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001706 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 break;
1708 case I_RESQ:
1709 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001710 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 break;
1712 case I_REST:
1713 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001714 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001716 case I_RESO:
1717 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001718 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001719 break;
1720 case I_RESY:
1721 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001722 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001723 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 case I_DB:
1725 typeinfo |= TY_BYTE;
1726 break;
1727 case I_DW:
1728 typeinfo |= TY_WORD;
1729 break;
1730 case I_DD:
1731 if (output_ins.eops_float)
1732 typeinfo |= TY_FLOAT;
1733 else
1734 typeinfo |= TY_DWORD;
1735 break;
1736 case I_DQ:
1737 typeinfo |= TY_QWORD;
1738 break;
1739 case I_DT:
1740 typeinfo |= TY_TBYTE;
1741 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001742 case I_DO:
1743 typeinfo |= TY_OWORD;
1744 break;
1745 case I_DY:
1746 typeinfo |= TY_YWORD;
1747 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 default:
1749 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001752
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001753 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001754 }
1755 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001757 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001758 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001759 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 * else l == -1 => invalid instruction, which will be
1761 * flagged as an error on pass 2
1762 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001763
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001764 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001766 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001767 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001768
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 }
1770 } /* not an EQU */
1771 cleanup_insn(&output_ins);
1772 }
1773 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001774 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001776
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001777 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001778 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001779 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 if (pass1 == 1)
1782 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001784 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001786 } else if (global_offset_changed &&
1787 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001788 prev_offset_changed = global_offset_changed;
1789 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001790 } else {
1791 stall_count++;
1792 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001793
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001794 if (terminate_after_phase)
1795 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001796
1797 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001798 /* We get here if the labels don't converge
1799 * Example: FOO equ FOO + 1
1800 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001801 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001802 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001803 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001804 nasm_error(ERR_NONFATAL,
1805 "Possible causes: recursive EQUs, macro abuse.");
1806 break;
1807 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001808 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001809
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 preproc->cleanup(0);
H. Peter Anvin172b8402016-02-18 01:16:18 -08001811 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001812 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001813 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001814 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1815 }
1816}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001817
H. Peter Anvin70055962007-10-11 00:05:31 -07001818static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001820 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001822 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001823
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001824 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001826 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001827 q = strchr(buf, ']');
1828 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001829 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001830
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001831 /* stip off the comments */
1832 p = strchr(buf, ';');
1833 if (p) {
1834 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001835 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001836 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001837 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001838
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001839 /* no brace, no trailing spaces */
1840 *q = '\0';
1841 nasm_zap_spaces_rev(--q);
1842
1843 /* directive */
1844 p = nasm_skip_spaces(++buf);
1845 q = nasm_skip_word(p);
1846 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001847 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001848 *q = '\0';
1849 *directive = p;
1850
1851 /* and value finally */
1852 p = nasm_skip_spaces(++q);
1853 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001854
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001855 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001856}
1857
Ed Berosetfa771012002-06-09 20:56:40 +00001858/**
1859 * gnu style error reporting
1860 * This function prints an error message to error_file in the
1861 * style used by GNU. An example would be:
1862 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001863 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001864 * which the error occurs (or is detected) and "error:" is one of
1865 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001866 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001867 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001868 *
1869 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001870 * @param fmt the printf style format string
1871 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001872static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001873{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001874 char *currentfile = NULL;
1875 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001876
Ed Berosetfa771012002-06-09 20:56:40 +00001877 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001879
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001880 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001882
H. Peter Anvinbd464742016-02-17 20:47:01 -08001883 if (!skip_this_pass(severity)) {
1884 if (currentfile) {
1885 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1886 nasm_free(currentfile);
1887 } else {
1888 fputs("nasm: ", error_file);
1889 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001890 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001891
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001892 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001893}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001894
Ed Berosetfa771012002-06-09 20:56:40 +00001895/**
1896 * MS style error reporting
1897 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001898 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001899 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001900 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001901 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001902 * which the error occurs (or is detected) and "error:" is one of
1903 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001904 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001905 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001906 *
1907 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001908 * @param fmt the printf style format string
1909 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001910static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001911{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001912 char *currentfile = NULL;
1913 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001914
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 if (is_suppressed_warning(severity))
1916 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001917
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001918 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001920
H. Peter Anvinbd464742016-02-17 20:47:01 -08001921 if (!skip_this_pass(severity)) {
1922 if (currentfile) {
1923 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1924 nasm_free(currentfile);
1925 } else {
1926 fputs("nasm: ", error_file);
1927 }
Ed Berosetfa771012002-06-09 20:56:40 +00001928 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001929
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001930 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001931}
1932
1933/**
1934 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001935 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001936 * not in pass 1
1937 *
1938 * @param severity the severity of the warning or error
1939 * @return true if we should abort error/warning printing
1940 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001941static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001942{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001943 /* Not a warning at all */
1944 if ((severity & ERR_MASK) != ERR_WARNING)
1945 return false;
1946
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001947 /* Might be a warning but suppresed explicitly */
1948 if (severity & ERR_WARN_MASK)
1949 return !warning_on[WARN_IDX(severity)];
1950 else
1951 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001952}
1953
H. Peter Anvinbd464742016-02-17 20:47:01 -08001954static bool skip_this_pass(int severity)
1955{
H. Peter Anvin934f0472016-05-09 12:00:19 -07001956 /* See if it's a pass-specific warning which should be skipped. */
1957
H. Peter Anvinbd464742016-02-17 20:47:01 -08001958 if ((severity & ERR_MASK) > ERR_WARNING)
1959 return false;
1960
H. Peter Anvinbd464742016-02-17 20:47:01 -08001961
H. Peter Anvin934f0472016-05-09 12:00:19 -07001962 /*
1963 * passn is 1 on the very first pass only.
1964 * pass0 is 2 on the code-generation (final) pass only.
1965 * These are the passes we care about in this case.
1966 */
1967 return (((severity & ERR_PASS1) && passn != 1) ||
1968 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvinbd464742016-02-17 20:47:01 -08001969}
1970
Ed Berosetfa771012002-06-09 20:56:40 +00001971/**
1972 * common error reporting
1973 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001974 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001975 * specific error message to error_file and may or may not return. It
1976 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001977 *
1978 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001979 * @param fmt the printf style format string
1980 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001981static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001982{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001983 char msg[1024];
1984 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001985
H. Peter Anvin7df04172008-06-10 18:27:38 -07001986 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 break;
1990 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001991 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 break;
1993 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001994 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 break;
1996 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001997 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 break;
1999 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002000 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002002 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002003 pfx = "";
2004 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002005 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002006
H. Peter Anvin934f0472016-05-09 12:00:19 -07002007 vsnprintf(msg, sizeof msg - 64, fmt, args);
2008 if (severity & ERR_WARN_MASK) {
2009 char *p = strchr(msg, '\0');
2010 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
2011 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002012
H. Peter Anvinbd464742016-02-17 20:47:01 -08002013 if (!skip_this_pass(severity))
2014 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002015
H. Peter Anvinbd464742016-02-17 20:47:01 -08002016 /*
2017 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002018 * pass1 or preprocessor warnings in the list file
H. Peter Anvinbd464742016-02-17 20:47:01 -08002019 */
2020 if ((severity & ERR_MASK) >= ERR_WARNING)
H. Peter Anvin172b8402016-02-18 01:16:18 -08002021 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002022
2023 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002024 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002025
2026 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 case ERR_DEBUG:
2028 /* no further action, by definition */
2029 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002030 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002031 /* Treat warnings as errors */
2032 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2033 terminate_after_phase = true;
2034 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002036 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 break;
2038 case ERR_FATAL:
2039 if (ofile) {
2040 fclose(ofile);
2041 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002042 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043 }
2044 if (want_usage)
2045 usage();
2046 exit(1); /* instantly die */
2047 break; /* placate silly compilers */
2048 case ERR_PANIC:
2049 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002050 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvinbd464742016-02-17 20:47:01 -08002051 if (ofile) {
2052 fclose(ofile);
2053 remove(outname);
2054 ofile = NULL;
2055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 exit(3);
2057 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002058 }
2059}
2060
H. Peter Anvin734b1882002-04-30 21:01:08 +00002061static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002062{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002063 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002064}
2065
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002066static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002067{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002068 iflag_t r;
2069
2070 iflag_clear_all(&r);
2071
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002073 iflag_set(&r, IF_8086);
2074 else if (!strcmp(value, "186"))
2075 iflag_set(&r, IF_186);
2076 else if (!strcmp(value, "286"))
2077 iflag_set(&r, IF_286);
2078 else if (!strcmp(value, "386"))
2079 iflag_set(&r, IF_386);
2080 else if (!strcmp(value, "486"))
2081 iflag_set(&r, IF_486);
2082 else if (!strcmp(value, "586") ||
2083 !nasm_stricmp(value, "pentium"))
2084 iflag_set(&r, IF_PENT);
2085 else if (!strcmp(value, "686") ||
2086 !nasm_stricmp(value, "ppro") ||
2087 !nasm_stricmp(value, "pentiumpro") ||
2088 !nasm_stricmp(value, "p2"))
2089 iflag_set(&r, IF_P6);
2090 else if (!nasm_stricmp(value, "p3") ||
2091 !nasm_stricmp(value, "katmai"))
2092 iflag_set(&r, IF_KATMAI);
2093 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2094 !nasm_stricmp(value, "willamette"))
2095 iflag_set(&r, IF_WILLAMETTE);
2096 else if (!nasm_stricmp(value, "prescott"))
2097 iflag_set(&r, IF_PRESCOTT);
2098 else if (!nasm_stricmp(value, "x64") ||
2099 !nasm_stricmp(value, "x86-64"))
2100 iflag_set(&r, IF_X86_64);
2101 else if (!nasm_stricmp(value, "ia64") ||
2102 !nasm_stricmp(value, "ia-64") ||
2103 !nasm_stricmp(value, "itanium")||
2104 !nasm_stricmp(value, "itanic") ||
2105 !nasm_stricmp(value, "merced"))
2106 iflag_set(&r, IF_IA64);
2107 else {
2108 iflag_set(&r, IF_PLEVEL);
2109 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2110 "unknown 'cpu' type");
2111 }
2112 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002113}
2114
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002115static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002116{
2117 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002118
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 if ((i = atoi(value)) == 16)
2120 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002121 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002122 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002123 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 "cannot specify 32-bit segment on processor below a 386");
2125 i = 16;
2126 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002127 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002128 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002129 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002130 "cannot specify 64-bit segment on processor below an x86-64");
2131 i = 16;
2132 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002133 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002134 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002135 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 value);
2137 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002138 }
2139 return i;
2140}