blob: 7ae5ae58fed7e21f6cad46601212de808f854ed0 [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 Anvin130736c2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-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 Anvin338656c2016-02-17 20:59:22 -0800104const struct ofmt *ofmt = &OF_DEFAULT;
105const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000109
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400110FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700111int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
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 Anvincd7893d2016-02-18 01:25:46 -0800121struct 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
H. Peter Anvine7469712016-02-18 02:20:59 -0800131static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400132
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 Anvin130736c2016-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 Anvina7bc15d2016-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 Anvina7bc15d2016-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 Anvin76690a12002-04-30 20:52:49 +0000379
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000380 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800381 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000382
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400383 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700384 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400385 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700386
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400387 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700389
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400390 if (depend_missing_ok)
391 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700392
H. Peter Anvin130736c2016-02-17 20:27:41 -0800393 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400395 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 while ((line = preproc->getline()))
398 nasm_free(line);
399 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400400 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000401 char *line;
402 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000403 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000405
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 if (*outname) {
407 ofile = fopen(outname, "w");
408 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800409 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 "unable to open output file `%s'",
411 outname);
412 } else
413 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000414
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700415 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400417 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800418 preproc->reset(inname, 3, depend_ptr);
419 memcpy(warning_on, warning_on_global,
420 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700421
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 while ((line = preproc->getline())) {
423 /*
424 * We generate %line directives if needed for later programs
425 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000426 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 int altline = src_get(&linnum, &file_name);
428 if (altline) {
429 if (altline == 1 && lineinc == 1)
430 nasm_fputs("", ofile);
431 else {
432 lineinc = (altline != -1 || lineinc != 1);
433 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000434 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 file_name);
436 }
437 prior_linnum = linnum;
438 }
439 nasm_fputs(line, ofile);
440 nasm_free(line);
441 }
442 nasm_free(file_name);
443 preproc->cleanup(0);
444 if (ofile)
445 fclose(ofile);
446 if (ofile && terminate_after_phase)
447 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400448 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400449 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400451 if (operating_mode & OP_NORMAL) {
452 /*
453 * We must call ofmt->filename _anyway_, even if the user
454 * has specified their own output file, because some
455 * formats (eg OBJ and COFF) use ofmt->filename to find out
456 * the name of the input file and then put that inside the
457 * file.
458 */
459 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400461 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
462 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800463 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400464 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 /*
467 * We must call init_labels() before ofmt->init() since
468 * some object formats will want to define labels in their
469 * init routines. (eg OS/2 defines the FLAT group)
470 */
471 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400473 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400476 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200477
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400478 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800479 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400480 cleanup_labels();
481 fflush(ofile);
482 if (ferror(ofile))
483 nasm_error(ERR_NONFATAL|ERR_NOFILE,
484 "write error on output file `%s'", outname);
485 }
486
487 if (ofile) {
488 fclose(ofile);
489 if (terminate_after_phase)
490 remove(outname);
491 ofile = NULL;
492 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700495 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400496 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700497
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000498 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000500
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 raa_free(offsets);
502 saa_free(forwrefs);
503 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000504 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700506 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000507}
508
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509/*
510 * Get a parameter for a command line option.
511 * First arg must be in the form of e.g. -f...
512 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800513static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800515 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400516 if (p[2]) /* the parameter's in the option */
517 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800519 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400522 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524 return NULL;
525}
526
H. Peter Anvindc242712007-11-18 11:55:10 -0800527/*
528 * Copy a filename
529 */
530static void copy_filename(char *dst, const char *src)
531{
532 size_t len = strlen(src);
533
534 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800535 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400536 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800537 }
538 strncpy(dst, src, FILENAME_MAX);
539}
540
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541/*
542 * Convert a string to Make-safe form
543 */
544static char *quote_for_make(const char *str)
545{
546 const char *p;
547 char *os, *q;
548
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400549 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700550 size_t nbs = 0;
551
552 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400553 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700554
555 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400556 switch (*p) {
557 case ' ':
558 case '\t':
559 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
560 n += nbs + 2;
561 nbs = 0;
562 break;
563 case '$':
564 case '#':
565 nbs = 0;
566 n += 2;
567 break;
568 case '\\':
569 nbs++;
570 n++;
571 break;
572 default:
573 nbs = 0;
574 n++;
575 break;
576 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700577 }
578
579 /* Convert N backslashes at the end of filename to 2N backslashes */
580 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400581 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700582
583 os = q = nasm_malloc(n);
584
585 nbs = 0;
586 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400587 switch (*p) {
588 case ' ':
589 case '\t':
590 while (nbs--)
591 *q++ = '\\';
592 *q++ = '\\';
593 *q++ = *p;
594 break;
595 case '$':
596 *q++ = *p;
597 *q++ = *p;
598 nbs = 0;
599 break;
600 case '#':
601 *q++ = '\\';
602 *q++ = *p;
603 nbs = 0;
604 break;
605 case '\\':
606 *q++ = *p;
607 nbs++;
608 break;
609 default:
610 *q++ = *p;
611 nbs = 0;
612 break;
613 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700614 }
615 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400616 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700617
618 *q = '\0';
619
620 return os;
621}
622
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000624 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000626};
627
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100628enum text_options {
629 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800630 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100631};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800632static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 {"prefix", OPT_PREFIX},
634 {"postfix", OPT_POSTFIX},
635 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000636};
637
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400638static void show_version(void)
639{
640 printf("NASM version %s compiled on %s%s\n",
641 nasm_version, nasm_date, nasm_compile_options);
642 exit(0);
643}
644
H. Peter Anvin423e3812007-11-15 17:12:29 -0800645static bool stopoptions = false;
646static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800649 int i;
650 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700651 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000652
653 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800654 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000655
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400657 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
658 /* These parameters take values */
659 if (!(param = get_param(p, q, &advance)))
660 return advance;
661 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 switch (p[1]) {
664 case 's':
665 error_file = stdout;
666 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700667
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400668 case 'o': /* output file */
669 copy_filename(outname, param);
670 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'f': /* output format */
673 ofmt = ofmt_find(param, &ofmt_alias);
674 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800675 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400676 "unrecognised output format `%s' - "
677 "use -hf for a list", param);
678 }
679 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700680
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400681 case 'O': /* Optimization level */
682 {
683 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700684
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400685 if (!*param) {
686 /* Naked -O == -Ox */
687 optimizing = MAX_OPTIMIZE;
688 } else {
689 while (*param) {
690 switch (*param) {
691 case '0': case '1': case '2': case '3': case '4':
692 case '5': case '6': case '7': case '8': case '9':
693 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700694
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400695 /* -O0 -> optimizing == -1, 0.98 behaviour */
696 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
697 if (opt < 2)
698 optimizing = opt - 1;
699 else
700 optimizing = opt;
701 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700702
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 case 'v':
704 case '+':
705 param++;
706 opt_verbose_info = true;
707 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700708
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400709 case 'x':
710 param++;
711 optimizing = MAX_OPTIMIZE;
712 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700713
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400714 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800715 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400716 "unknown optimization option -O%c\n",
717 *param);
718 break;
719 }
720 }
721 if (optimizing > MAX_OPTIMIZE)
722 optimizing = MAX_OPTIMIZE;
723 }
724 break;
725 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800726
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 case 'p': /* pre-include */
728 case 'P':
729 preproc->pre_include(param);
730 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800731
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400732 case 'd': /* pre-define */
733 case 'D':
734 preproc->pre_define(param);
735 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800736
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 case 'u': /* un-define */
738 case 'U':
739 preproc->pre_undefine(param);
740 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800741
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 case 'i': /* include search path */
743 case 'I':
744 preproc->include_path(param);
745 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800746
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 case 'l': /* listing file */
748 copy_filename(listname, param);
749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'Z': /* error messages file */
752 copy_filename(errname, param);
753 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 case 'F': /* specify debug format */
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800757 debug_format = param;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400758 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800759
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 case 'X': /* specify error reporting format */
761 if (nasm_stricmp("vc", param) == 0)
762 nasm_set_verror(nasm_verror_vc);
763 else if (nasm_stricmp("gnu", param) == 0)
764 nasm_set_verror(nasm_verror_gnu);
765 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800766 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400767 "unrecognized error reporting format `%s'",
768 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800770
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700772 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800773 if (p[2])
774 debug_format = nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 case 'h':
778 printf
779 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
780 "[-l listfile]\n"
781 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400782 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800783 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700785 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700787 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400788 " -MG d:o, missing files assumed generated\n"
789 " -MF <file> set Makefile dependency file\n"
790 " -MD <file> assemble and generate dependencies\n"
791 " -MT <file> dependency target name\n"
792 " -MQ <file> dependency target name (quoted)\n"
793 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700794 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800796 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800798 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400799 " -o outfile write output to an outfile\n\n"
800 " -f format select an output format\n\n"
801 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 " -I<path> adds a pathname to the include file path\n");
803 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700804 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400805 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400806 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400807 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 " -P<file> pre-includes a file\n"
809 " -D<macro>[=<value>] pre-defines a macro\n"
810 " -U<macro> undefines a macro\n"
811 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800812 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400813 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400814 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400815 "--prefix,--postfix\n"
816 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100817 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800818 "Warnings:\n");
819 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700821 warnings[i].name, warnings[i].help,
822 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 printf
824 ("\nresponse files should contain command line parameters"
825 ", one per line.\n");
826 if (p[2] == 'f') {
827 printf("\nvalid output formats for -f are"
828 " (`*' denotes default):\n");
829 ofmt_list(ofmt, stdout);
830 } else {
831 printf("\nFor a list of valid output formats, use -hf.\n");
832 printf("For a list of debug formats, use -f <form> -y.\n");
833 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400834 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800836
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 case 'y':
838 printf("\nvalid debug formats for '%s' output format are"
839 " ('*' denotes default):\n", ofmt->shortname);
840 dfmt_list(ofmt, stdout);
841 exit(0);
842 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800843
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700845 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400849 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'e': /* preprocess only */
853 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400854 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800856
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400858 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800860
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400861 case 'W':
862 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
863 do_warn = false;
864 param += 3;
865 } else {
866 do_warn = true;
867 }
868 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800869
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400872 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400874 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 do_warn = (param[0] == '+');
877 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400878
879set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400880 for (i = 0; i <= ERR_WARN_MAX; i++) {
881 if (!nasm_stricmp(param, warnings[i].name))
882 break;
883 }
884 if (i <= ERR_WARN_MAX) {
885 warning_on_global[i] = do_warn;
886 } else if (!nasm_stricmp(param, "all")) {
887 for (i = 1; i <= ERR_WARN_MAX; i++)
888 warning_on_global[i] = do_warn;
889 } else if (!nasm_stricmp(param, "none")) {
890 for (i = 1; i <= ERR_WARN_MAX; i++)
891 warning_on_global[i] = !do_warn;
892 } else {
893 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
894 "invalid warning `%s'", param);
895 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800897
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400899 switch (p[2]) {
900 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400901 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 break;
903 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400904 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400905 depend_missing_ok = true;
906 break;
907 case 'P':
908 depend_emit_phony = true;
909 break;
910 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300911 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400912 depend_file = q;
913 advance = true;
914 break;
915 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 depend_file = q;
917 advance = true;
918 break;
919 case 'T':
920 depend_target = q;
921 advance = true;
922 break;
923 case 'Q':
924 depend_target = quote_for_make(q);
925 advance = true;
926 break;
927 default:
928 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
929 "unknown dependency option `-M%c'", p[2]);
930 break;
931 }
932 if (advance && (!q || !q[0])) {
933 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
934 "option `-M%c' requires a parameter", p[2]);
935 break;
936 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 case '-':
940 {
941 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000942
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 if (p[2] == 0) { /* -- => stop processing options */
944 stopoptions = 1;
945 break;
946 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400947
948 if (!nasm_stricmp(p, "--v"))
949 show_version();
950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 for (s = 0; textopts[s].label; s++) {
952 if (!nasm_stricmp(p + 2, textopts[s].label)) {
953 break;
954 }
955 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000956
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 switch (s) {
958
959 case OPT_PREFIX:
960 case OPT_POSTFIX:
961 {
962 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400963 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 ERR_USAGE,
965 "option `--%s' requires an argument",
966 p + 2);
967 break;
968 } else {
969 advance = 1, param = q;
970 }
971
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100972 switch (s) {
973 case OPT_PREFIX:
974 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100976 case OPT_POSTFIX:
977 strlcpy(lpostfix, param, POSTFIX_MAX);
978 break;
979 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800980 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100981 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 break;
983 }
984 break;
985 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800986
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 default:
988 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400989 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 "unrecognised option `--%s'", p + 2);
991 break;
992 }
993 }
994 break;
995 }
996
997 default:
998 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400999 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 "unrecognised option `-%c'", p[1]);
1001 break;
1002 }
1003 } else {
1004 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001005 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001007 } else {
1008 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001009 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001010 }
1011
1012 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001013}
1014
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015#define ARG_BUF_DELTA 128
1016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001018{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001019 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001020 int bufsize, prevargsize;
1021
1022 bufsize = prevargsize = ARG_BUF_DELTA;
1023 buffer = nasm_malloc(ARG_BUF_DELTA);
1024 prevarg = nasm_malloc(ARG_BUF_DELTA);
1025 prevarg[0] = '\0';
1026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 p = buffer;
1029 while (1) { /* Loop to handle long lines */
1030 q = fgets(p, bufsize - (p - buffer), rfile);
1031 if (!q)
1032 break;
1033 p += strlen(p);
1034 if (p > buffer && p[-1] == '\n')
1035 break;
1036 if (p - buffer > bufsize - 10) {
1037 int offset;
1038 offset = p - buffer;
1039 bufsize += ARG_BUF_DELTA;
1040 buffer = nasm_realloc(buffer, bufsize);
1041 p = buffer + offset;
1042 }
1043 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (!q && p == buffer) {
1046 if (prevarg[0])
1047 process_arg(prevarg, NULL);
1048 nasm_free(buffer);
1049 nasm_free(prevarg);
1050 return;
1051 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 /*
1054 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1055 * them are present at the end of the line.
1056 */
1057 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001059 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001062 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 if (process_arg(prevarg, p))
1065 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066
Charles Crayne192d5b52007-10-18 19:02:42 -07001067 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 prevargsize += ARG_BUF_DELTA;
1069 prevarg = nasm_realloc(prevarg, prevargsize);
1070 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001071 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001072 }
1073}
1074
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001075/* Function to process args from a string of args, rather than the
1076 * argv array. Used by the environment variable and response file
1077 * processing.
1078 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001079static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001081 char *p, *q, *arg, *prevarg;
1082 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001083
1084 p = args;
1085 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087 arg = NULL;
1088 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 q = p;
1090 while (*p && *p != separator)
1091 p++;
1092 while (*p == separator)
1093 *p++ = '\0';
1094 prevarg = arg;
1095 arg = q;
1096 if (process_arg(prevarg, arg))
1097 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001098 }
1099 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001101}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001102
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001103static void process_response_file(const char *file)
1104{
1105 char str[2048];
1106 FILE *f = fopen(file, "r");
1107 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001108 perror(file);
1109 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001110 }
1111 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001112 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001113 }
1114 fclose(f);
1115}
1116
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001117static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001118{
1119 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001120 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001121 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001122
Charles Craynefcce07f2007-09-30 22:15:36 -07001123 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001124
H. Peter Anvin972079f2008-09-30 17:01:23 -07001125 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001126 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127
1128 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001129 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001130 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001131 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 envcopy = nasm_strdup(envreal);
1134 process_args(envcopy);
1135 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137
1138 /*
1139 * Now process the actual command line.
1140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001142 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 argv++;
1144 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001145 /*
1146 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 * arguments like the environment variable. This allows us
1148 * to have multiple arguments on a single line, which is
1149 * different to the -@resp file processing below for regular
1150 * NASM.
1151 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001152 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 argc--;
1154 argv++;
1155 }
1156 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001159 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 process_respfile(rfile);
1162 fclose(rfile);
1163 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001164 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 "unable to open response file `%s'", p);
1166 }
1167 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001168 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1169 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001170 }
1171
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 /*
1173 * Look for basic command line typos. This definitely doesn't
1174 * catch all errors, but it might help cases of fumbled fingers.
1175 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001176 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001177 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001178 "no input file specified");
1179 else if (!strcmp(inname, errname) ||
1180 !strcmp(inname, outname) ||
1181 !strcmp(inname, listname) ||
1182 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001183 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001184 "file `%s' is both input and output file",
1185 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001186
H. Peter Anvin70653092007-10-19 14:42:29 -07001187 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001188 error_file = fopen(errname, "w");
1189 if (!error_file) {
1190 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001191 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001192 "cannot open file `%s' for error messages",
1193 errname);
1194 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001195 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001196}
1197
H. Peter Anvin70055962007-10-11 00:05:31 -07001198static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001199
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001200static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001202 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001204 int i, validid;
1205 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001206 int32_t seg;
1207 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001210 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001211
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001212 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001213 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214
Charles Craynec1905c22008-09-11 18:54:06 -07001215 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001216 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 int pass1, pass2;
1218 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001219
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001220 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001221 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1222 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001223
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001224 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225
Keith Kaniosb7a89542007-04-12 02:40:54 +00001226 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 cpu = cmd_cpu;
1228 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001229 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001231 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001232 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001234 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001235 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 saa_rewind(forwrefs);
1237 forwref = saa_rstruct(forwrefs);
1238 raa_free(offsets);
1239 offsets = raa_init();
1240 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001241 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001242 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001245 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001246 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001247 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001248
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001250 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001253 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001254 * Here we parse our directives; this is not handled by the
1255 * 'real' parser. This really should be a separate function.
1256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001258 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001259 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001260 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001261
H. Peter Anvin70055962007-10-11 00:05:31 -07001262 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001263 case D_SEGMENT: /* [SEGMENT n] */
1264 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 seg = ofmt->section(value, pass2, &sb);
1266 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001267 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001268 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 value);
1270 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001271 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 location.segment = seg;
1273 }
1274 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001275 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001276 if (*value) {
1277 stdscan_reset();
1278 stdscan_set(value);
1279 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001280 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001281 if (e) {
1282 unsigned int align = (unsigned int)e->value;
1283 if ((uint64_t)e->value > 0x7fffffff) {
1284 /*
1285 * FIXME: Please make some sane message here
1286 * ofmt should have some 'check' method which
1287 * would report segment alignment bounds.
1288 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001289 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001290 "incorrect segment alignment `%s'", value);
1291 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001292 nasm_error(ERR_NONFATAL,
1293 "segment alignment `%s' is not power of two",
1294 value);
1295 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001296
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001297 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001298 if (location.segment != NO_SEG)
1299 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001300 }
1301 }
1302 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001303 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 if (*value == '$')
1305 value++; /* skip initial $ if present */
1306 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001307 q = value;
1308 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 *q++ = '\0';
1312 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001314 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001315 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001316 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001318 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001321 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 }
1324 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001325 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 "identifier expected after EXTERN");
1327 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001328 }
1329 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 *q++ = '\0';
1331 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001332 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 special = NULL;
1334 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1335 int temp = pass0;
1336 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001337 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001339 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 } /* else pass0 == 1 */
1343 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001344 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001345 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001347 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 if (*value == '$')
1349 value++; /* skip initial $ if present */
1350 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 q = value;
1352 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 *q++ = '\0';
1356 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001359 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001360 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001361 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001365 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 }
1368 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001369 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 "identifier expected after GLOBAL");
1371 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001372 }
1373 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 *q++ = '\0';
1375 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001376 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001378 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 } /* pass == 1 */
1380 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001381 case D_COMMON: /* [COMMON symbol size:special] */
1382 {
1383 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (*value == '$')
1386 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001387 p = value;
1388 validid = true;
1389 if (!isidstart(*p))
1390 validid = false;
1391 while (*p && !nasm_isspace(*p)) {
1392 if (!isidchar(*p))
1393 validid = false;
1394 p++;
1395 }
1396 if (!validid) {
1397 nasm_error(ERR_NONFATAL,
1398 "identifier expected after COMMON");
1399 break;
1400 }
1401 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001402 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001403 q = p;
1404 while (*q && *q != ':')
1405 q++;
1406 if (*q == ':') {
1407 *q++ = '\0';
1408 special = q;
1409 } else {
1410 special = NULL;
1411 }
1412 size = readnum(p, &rn_error);
1413 if (rn_error) {
1414 nasm_error(ERR_NONFATAL,
1415 "invalid size specified"
1416 " in COMMON declaration");
1417 break;
1418 }
1419 } else {
1420 nasm_error(ERR_NONFATAL,
1421 "no size specified in"
1422 " COMMON declaration");
1423 break;
1424 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001426 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001427 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001428 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001429 if (special)
1430 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 }
1432 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001433 }
1434 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001436 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001438 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 if (e) {
1440 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001441 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 1 ? ERR_NONFATAL : ERR_PANIC,
1443 "cannot use non-relocatable expression as "
1444 "ABSOLUTE address");
1445 else {
1446 abs_seg = reloc_seg(e);
1447 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001448 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001449 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 abs_offset = 0x100; /* don't go near zero in case of / */
1451 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001452 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001454 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 location.segment = NO_SEG;
1456 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001457 case D_DEBUG: /* [DEBUG] */
1458 {
1459 char debugid[128];
1460 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 p = value;
1463 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001464 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001465 if (!isidstart(*p)) {
1466 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001467 } else {
1468 while (*p && !nasm_isspace(*p)) {
1469 if (q >= debugid + sizeof debugid - 1) {
1470 overlong = true;
1471 break;
1472 }
1473 if (!isidchar(*p))
1474 badid = true;
1475 *q++ = *p++;
1476 }
1477 *q = 0;
1478 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001479 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001480 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1481 "identifier expected after DEBUG");
1482 break;
1483 }
1484 if (overlong) {
1485 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1486 "DEBUG identifier too long");
1487 break;
1488 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001489 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001490 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001491 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001493 }
1494 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001495 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001496 switch(*value) {
1497 case '-': validid = 0; value++; break;
1498 case '+': validid = 1; value++; break;
1499 case '*': validid = 2; value++; break;
1500 default: validid = 1; break;
1501 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001503 for (i = 1; i <= ERR_WARN_MAX; i++)
1504 if (!nasm_stricmp(value, warnings[i].name))
1505 break;
1506 if (i <= ERR_WARN_MAX) {
1507 switch(validid) {
1508 case 0:
1509 warning_on[i] = false;
1510 break;
1511 case 1:
1512 warning_on[i] = true;
1513 break;
1514 case 2:
1515 warning_on[i] = warning_on_global[i];
1516 break;
1517 }
1518 } else
1519 nasm_error(ERR_NONFATAL,
1520 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001522 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 cpu = get_cpu(value);
1524 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001525 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001526 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 if (*value == '+') {
1528 user_nolist = 0;
1529 } else {
1530 if (*value == '-') {
1531 user_nolist = 1;
1532 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001533 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 }
1535 }
1536 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001537 case D_DEFAULT: /* [DEFAULT] */
1538 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001539 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001540 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001541 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001542 switch ((int)tokval.t_integer) {
1543 case S_REL:
1544 globalrel = 1;
1545 break;
1546 case S_ABS:
1547 globalrel = 0;
1548 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001549 case P_BND:
1550 globalbnd = 1;
1551 break;
1552 case P_NOBND:
1553 globalbnd = 0;
1554 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001555 default:
1556 err = 1;
1557 break;
1558 }
1559 } else {
1560 err = 1;
1561 }
1562 break;
1563 case D_FLOAT:
1564 if (float_option(value)) {
1565 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1566 "unknown 'float' directive: %s",
1567 value);
1568 }
1569 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001571 if (ofmt->directive(d, value, pass2))
1572 break;
1573 /* else fall through */
1574 case D_unknown:
1575 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1576 "unrecognised directive [%s]",
1577 directive);
1578 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001580 if (err) {
1581 nasm_error(ERR_NONFATAL,
1582 "invalid parameter to [%s] directive",
1583 directive);
1584 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001586 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587
Charles Crayne2581c862008-09-10 19:21:52 -07001588 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001590 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001592 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 forwref = saa_rstruct(forwrefs);
1594 } while (forwref != NULL
1595 && forwref->lineno == globallineno);
1596 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001597 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001598
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001599 if (output_ins.forw_ref) {
1600 if (passn == 1) {
1601 for (i = 0; i < output_ins.operands; i++) {
1602 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1603 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1604 fwinf->lineno = globallineno;
1605 fwinf->operand = i;
1606 }
1607 }
1608 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001609 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 /* forw_ref */
1613 if (output_ins.opcode == I_EQU) {
1614 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 * Special `..' EQUs get processed in pass two,
1617 * except `..@' macro-processor EQUs which are done
1618 * in the normal place.
1619 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001620 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001621 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001623
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001624 else if (output_ins.label[0] != '.' ||
1625 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 output_ins.label[2] == '@') {
1627 if (output_ins.operands == 1 &&
1628 (output_ins.oprs[0].type & IMMEDIATE) &&
1629 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001630 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 def_label(output_ins.label,
1632 output_ins.oprs[0].segment,
1633 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001634 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001636 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001640 && (output_ins.oprs[1].type & IMMEDIATE)
1641 && output_ins.oprs[1].segment == NO_SEG
1642 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001644 output_ins.oprs[0].offset | SEG_ABS,
1645 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001646 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001648 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001651 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 * Special `..' EQUs get processed here, except
1654 * `..@' macro processor EQUs which are done above.
1655 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 output_ins.label[1] == '.' &&
1658 output_ins.label[2] != '@') {
1659 if (output_ins.operands == 1 &&
1660 (output_ins.oprs[0].type & IMMEDIATE)) {
1661 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001662 output_ins.oprs[0].segment,
1663 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001664 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001666 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001668 && output_ins.oprs[0].segment == NO_SEG
1669 && (output_ins.oprs[1].type & IMMEDIATE)
1670 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001672 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001674 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001676 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001678 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001681
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001683
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001684 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001685 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 /* if (using_debug_info) && output_ins.opcode != -1) */
1688 if (using_debug_info)
1689 { /* fbk 03/25/01 */
1690 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001691 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 TYS_ELEMENTS(output_ins.operands);
1693 switch (output_ins.opcode) {
1694 case I_RESB:
1695 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001696 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 break;
1698 case I_RESW:
1699 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001700 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 break;
1702 case I_RESD:
1703 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001704 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 break;
1706 case I_RESQ:
1707 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001708 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 break;
1710 case I_REST:
1711 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001712 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001714 case I_RESO:
1715 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001716 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001717 break;
1718 case I_RESY:
1719 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001720 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001721 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 case I_DB:
1723 typeinfo |= TY_BYTE;
1724 break;
1725 case I_DW:
1726 typeinfo |= TY_WORD;
1727 break;
1728 case I_DD:
1729 if (output_ins.eops_float)
1730 typeinfo |= TY_FLOAT;
1731 else
1732 typeinfo |= TY_DWORD;
1733 break;
1734 case I_DQ:
1735 typeinfo |= TY_QWORD;
1736 break;
1737 case I_DT:
1738 typeinfo |= TY_TBYTE;
1739 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001740 case I_DO:
1741 typeinfo |= TY_OWORD;
1742 break;
1743 case I_DY:
1744 typeinfo |= TY_YWORD;
1745 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 default:
1747 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001748
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001750
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001751 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001752 }
1753 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001755 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001756 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001757 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 * else l == -1 => invalid instruction, which will be
1759 * flagged as an error on pass 2
1760 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001761
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001762 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001764 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001765 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 }
1768 } /* not an EQU */
1769 cleanup_insn(&output_ins);
1770 }
1771 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001772 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001774
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001775 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001776 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001777 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001778
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 if (pass1 == 1)
1780 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001781
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001782 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001784 } else if (global_offset_changed &&
1785 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001786 prev_offset_changed = global_offset_changed;
1787 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001788 } else {
1789 stall_count++;
1790 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001791
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001792 if (terminate_after_phase)
1793 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001794
1795 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001796 /* We get here if the labels don't converge
1797 * Example: FOO equ FOO + 1
1798 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001799 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001800 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001801 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001802 nasm_error(ERR_NONFATAL,
1803 "Possible causes: recursive EQUs, macro abuse.");
1804 break;
1805 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001806 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001809 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001810 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001811 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001812 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1813 }
1814}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001815
H. Peter Anvin70055962007-10-11 00:05:31 -07001816static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001817{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001818 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001820 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001821
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001822 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001824 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001825 q = strchr(buf, ']');
1826 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001827 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001828
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001829 /* stip off the comments */
1830 p = strchr(buf, ';');
1831 if (p) {
1832 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001833 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001834 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001835 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001836
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001837 /* no brace, no trailing spaces */
1838 *q = '\0';
1839 nasm_zap_spaces_rev(--q);
1840
1841 /* directive */
1842 p = nasm_skip_spaces(++buf);
1843 q = nasm_skip_word(p);
1844 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001845 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001846 *q = '\0';
1847 *directive = p;
1848
1849 /* and value finally */
1850 p = nasm_skip_spaces(++q);
1851 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001852
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001853 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001854}
1855
Ed Berosetfa771012002-06-09 20:56:40 +00001856/**
1857 * gnu style error reporting
1858 * This function prints an error message to error_file in the
1859 * style used by GNU. An example would be:
1860 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001861 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001862 * which the error occurs (or is detected) and "error:" is one of
1863 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001864 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001865 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001866 *
1867 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001868 * @param fmt the printf style format string
1869 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001870static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001871{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001872 char *currentfile = NULL;
1873 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001874
Ed Berosetfa771012002-06-09 20:56:40 +00001875 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001877
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001878 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001880
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001881 if (!skip_this_pass(severity)) {
1882 if (currentfile) {
1883 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1884 nasm_free(currentfile);
1885 } else {
1886 fputs("nasm: ", error_file);
1887 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001888 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001889
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001890 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001891}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001892
Ed Berosetfa771012002-06-09 20:56:40 +00001893/**
1894 * MS style error reporting
1895 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001896 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001897 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001898 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001899 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001900 * which the error occurs (or is detected) and "error:" is one of
1901 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001902 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001903 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001904 *
1905 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001906 * @param fmt the printf style format string
1907 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001908static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001909{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001910 char *currentfile = NULL;
1911 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001912
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 if (is_suppressed_warning(severity))
1914 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001915
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001916 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001918
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001919 if (!skip_this_pass(severity)) {
1920 if (currentfile) {
1921 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1922 nasm_free(currentfile);
1923 } else {
1924 fputs("nasm: ", error_file);
1925 }
Ed Berosetfa771012002-06-09 20:56:40 +00001926 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001927
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001928 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001929}
1930
1931/**
1932 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001933 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001934 * not in pass 1
1935 *
1936 * @param severity the severity of the warning or error
1937 * @return true if we should abort error/warning printing
1938 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001939static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001940{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001941 /* Not a warning at all */
1942 if ((severity & ERR_MASK) != ERR_WARNING)
1943 return false;
1944
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001945 /* Might be a warning but suppresed explicitly */
1946 if (severity & ERR_WARN_MASK)
1947 return !warning_on[WARN_IDX(severity)];
1948 else
1949 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001950}
1951
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001952static bool skip_this_pass(int severity)
1953{
1954 /* See if it's a pass-one only warning and we're not in pass one. */
1955 if ((severity & ERR_MASK) > ERR_WARNING)
1956 return false;
1957
1958 if (((severity & ERR_PASS1) && pass0 != 1) ||
1959 ((severity & ERR_PASS2) && pass0 != 2))
1960 return true;
1961
1962 return false;
1963}
1964
Ed Berosetfa771012002-06-09 20:56:40 +00001965/**
1966 * common error reporting
1967 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001968 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001969 * specific error message to error_file and may or may not return. It
1970 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001971 *
1972 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001973 * @param fmt the printf style format string
1974 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001975static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001976{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001977 char msg[1024];
1978 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001979
H. Peter Anvin7df04172008-06-10 18:27:38 -07001980 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001982 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 break;
1984 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001985 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 break;
1987 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 break;
1990 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001991 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 break;
1993 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001994 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001996 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001997 pfx = "";
1998 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001999 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002000
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002001 vsnprintf(msg, sizeof msg, fmt, args);
2002
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002003 if (!skip_this_pass(severity))
2004 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002005
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002006 /*
2007 * Don't suppress this with skip_this_pass(), or we don't get
2008 * preprocessor warnings in the list file
2009 */
2010 if ((severity & ERR_MASK) >= ERR_WARNING)
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002011 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002012
2013 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002014 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002015
2016 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 case ERR_DEBUG:
2018 /* no further action, by definition */
2019 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002020 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002021 /* Treat warnings as errors */
2022 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2023 terminate_after_phase = true;
2024 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002026 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 break;
2028 case ERR_FATAL:
2029 if (ofile) {
2030 fclose(ofile);
2031 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002032 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 }
2034 if (want_usage)
2035 usage();
2036 exit(1); /* instantly die */
2037 break; /* placate silly compilers */
2038 case ERR_PANIC:
2039 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002040 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002041 if (ofile) {
2042 fclose(ofile);
2043 remove(outname);
2044 ofile = NULL;
2045 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 exit(3);
2047 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002048 }
2049}
2050
H. Peter Anvin734b1882002-04-30 21:01:08 +00002051static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002052{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002053 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002054}
2055
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002056static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002057{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002058 iflag_t r;
2059
2060 iflag_clear_all(&r);
2061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002063 iflag_set(&r, IF_8086);
2064 else if (!strcmp(value, "186"))
2065 iflag_set(&r, IF_186);
2066 else if (!strcmp(value, "286"))
2067 iflag_set(&r, IF_286);
2068 else if (!strcmp(value, "386"))
2069 iflag_set(&r, IF_386);
2070 else if (!strcmp(value, "486"))
2071 iflag_set(&r, IF_486);
2072 else if (!strcmp(value, "586") ||
2073 !nasm_stricmp(value, "pentium"))
2074 iflag_set(&r, IF_PENT);
2075 else if (!strcmp(value, "686") ||
2076 !nasm_stricmp(value, "ppro") ||
2077 !nasm_stricmp(value, "pentiumpro") ||
2078 !nasm_stricmp(value, "p2"))
2079 iflag_set(&r, IF_P6);
2080 else if (!nasm_stricmp(value, "p3") ||
2081 !nasm_stricmp(value, "katmai"))
2082 iflag_set(&r, IF_KATMAI);
2083 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2084 !nasm_stricmp(value, "willamette"))
2085 iflag_set(&r, IF_WILLAMETTE);
2086 else if (!nasm_stricmp(value, "prescott"))
2087 iflag_set(&r, IF_PRESCOTT);
2088 else if (!nasm_stricmp(value, "x64") ||
2089 !nasm_stricmp(value, "x86-64"))
2090 iflag_set(&r, IF_X86_64);
2091 else if (!nasm_stricmp(value, "ia64") ||
2092 !nasm_stricmp(value, "ia-64") ||
2093 !nasm_stricmp(value, "itanium")||
2094 !nasm_stricmp(value, "itanic") ||
2095 !nasm_stricmp(value, "merced"))
2096 iflag_set(&r, IF_IA64);
2097 else {
2098 iflag_set(&r, IF_PLEVEL);
2099 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2100 "unknown 'cpu' type");
2101 }
2102 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002103}
2104
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002105static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002106{
2107 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002108
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 if ((i = atoi(value)) == 16)
2110 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002111 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002112 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002113 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 "cannot specify 32-bit segment on processor below a 386");
2115 i = 16;
2116 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002117 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002118 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002119 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002120 "cannot specify 64-bit segment on processor below an x86-64");
2121 i = 16;
2122 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002123 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002124 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002125 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002126 value);
2127 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002128 }
2129 return i;
2130}