blob: 0b2e13f1354d08351cbc5bfea97955e51853ee26 [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>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080046#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48#include "nasm.h"
49#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
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 Anvin69550ea2016-05-09 12:05:56 -0700174 {"ptr", "non-NASM keyword used in other assemblers", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000175};
176
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700177static bool want_usage;
178static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800179bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
H. Peter Anvin55340992012-09-09 17:09:00 -0700181static char *quote_for_make(const char *str);
182
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400183static int64_t get_curr_offs(void)
184{
185 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
186}
187
188static void set_curr_offs(int64_t l_off)
189{
190 if (in_abs_seg)
191 abs_offset = l_off;
192 else
193 offsets = raa_write(offsets, location.segment, l_off);
194}
195
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000196static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000198 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800200 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000202 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203}
204
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800205/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800206static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800207{
208 int64_t t;
209 int64_t y = tm->tm_year;
210
211 /* See IEEE 1003.1:2004, section 4.14 */
212
213 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
214 t += tm->tm_yday;
215 t *= 24;
216 t += tm->tm_hour;
217 t *= 60;
218 t += tm->tm_min;
219 t *= 60;
220 t += tm->tm_sec;
221
222 return t;
223}
224
225static void define_macros_early(void)
226{
227 char temp[128];
228 struct tm lt, *lt_p, gm, *gm_p;
229 int64_t posix_time;
230
231 lt_p = localtime(&official_compile_time);
232 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400233 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400235 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
238 preproc->pre_define(temp);
239 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
240 preproc->pre_define(temp);
241 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
242 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243 }
244
245 gm_p = gmtime(&official_compile_time);
246 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400247 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800248
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400249 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
252 preproc->pre_define(temp);
253 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
254 preproc->pre_define(temp);
255 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
256 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700258
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800260 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800262 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
266 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400267 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
268 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 }
270}
271
272static void define_macros_late(void)
273{
274 char temp[128];
275
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400276 /*
277 * In case if output format is defined by alias
278 * we have to put shortname of the alias itself here
279 * otherwise ABI backward compatibility gets broken.
280 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400281 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400282 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400283 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800284}
285
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700286static void emit_dependencies(StrList *list)
287{
288 FILE *deps;
289 int linepos, len;
290 StrList *l, *nl;
291
292 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700293 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400294 if (!deps) {
295 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
296 "unable to write dependency file `%s'", depend_file);
297 return;
298 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400300 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700301 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700302
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700303 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400304 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700305 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400306 len = strlen(file);
307 if (linepos + len > 62 && linepos > 1) {
308 fprintf(deps, " \\\n ");
309 linepos = 1;
310 }
311 fprintf(deps, " %s", file);
312 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700313 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700314 }
315 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700316
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400317 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 if (depend_emit_phony)
319 fprintf(deps, "%s:\n\n", l->str);
320 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321 }
322
323 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325}
326
H. Peter Anvin038d8612007-04-12 16:54:50 +0000327int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700329 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700330
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331 time(&official_compile_time);
332
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400333 iflag_set(&cpu, IF_PLEVEL);
334 iflag_set(&cmd_cpu, IF_PLEVEL);
335
Charles Crayne2581c862008-09-10 19:21:52 -0700336 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700337 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400338 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
H. Peter Anvin97e15752007-09-25 08:47:47 -0700340 error_file = stderr;
341
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700342 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700343 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700344
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000346 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400349 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000350
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700351 /*
352 * Define some macros dependent on the runtime, but not
353 * on the command line. This only works because the only
354 * alternative preprocessor is preproc_nop...
355 */
356 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800357 define_macros_early();
358
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 parse_cmdline(argc, argv);
360
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 if (terminate_after_phase) {
362 if (want_usage)
363 usage();
364 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000365 }
366
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800367 if (!using_debug_info) {
368 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800369 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800370 } else if (!debug_format) {
371 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800372 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800373 } else {
374 dfmt = dfmt_find(ofmt, debug_format);
375 if (!dfmt) {
376 nasm_fatal(ERR_NOFILE | ERR_USAGE,
377 "unrecognized debug format `%s' for"
378 " output format `%s'",
379 debug_format, ofmt->shortname);
380 }
381 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000382
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400384 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000385
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000386 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800387 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000388
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400389 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700390 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400391 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700392
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400393 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000394 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700395
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400396 if (depend_missing_ok)
397 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700398
H. Peter Anvin130736c2016-02-17 20:27:41 -0800399 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400401 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 while ((line = preproc->getline()))
404 nasm_free(line);
405 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400406 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000407 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700408 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000409 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000411
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700413 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800415 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 "unable to open output file `%s'",
417 outname);
418 } else
419 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000420
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700421 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400423 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800424 preproc->reset(inname, 3, depend_ptr);
425 memcpy(warning_on, warning_on_global,
426 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 while ((line = preproc->getline())) {
429 /*
430 * We generate %line directives if needed for later programs
431 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000432 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 int altline = src_get(&linnum, &file_name);
434 if (altline) {
435 if (altline == 1 && lineinc == 1)
436 nasm_fputs("", ofile);
437 else {
438 lineinc = (altline != -1 || lineinc != 1);
439 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000440 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 file_name);
442 }
443 prior_linnum = linnum;
444 }
445 nasm_fputs(line, ofile);
446 nasm_free(line);
447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 preproc->cleanup(0);
449 if (ofile)
450 fclose(ofile);
451 if (ofile && terminate_after_phase)
452 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400453 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400454 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400456 if (operating_mode & OP_NORMAL) {
457 /*
458 * We must call ofmt->filename _anyway_, even if the user
459 * has specified their own output file, because some
460 * formats (eg OBJ and COFF) use ofmt->filename to find out
461 * the name of the input file and then put that inside the
462 * file.
463 */
464 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700466 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400467 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800468 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400469 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400471 /*
472 * We must call init_labels() before ofmt->init() since
473 * some object formats will want to define labels in their
474 * init routines. (eg OS/2 defines the FLAT group)
475 */
476 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400478 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400479 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200482
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800484 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 cleanup_labels();
486 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700487 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400488 nasm_error(ERR_NONFATAL|ERR_NOFILE,
489 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700490 terminate_after_phase = true;
491 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400492 }
493
494 if (ofile) {
495 fclose(ofile);
496 if (terminate_after_phase)
497 remove(outname);
498 ofile = NULL;
499 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000501
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700502 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400503 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700504
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 raa_free(offsets);
509 saa_free(forwrefs);
510 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000511 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700512 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000513
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700514 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000515}
516
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517/*
518 * Get a parameter for a command line option.
519 * First arg must be in the form of e.g. -f...
520 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800521static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800523 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400524 if (p[2]) /* the parameter's in the option */
525 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800527 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400530 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532 return NULL;
533}
534
H. Peter Anvindc242712007-11-18 11:55:10 -0800535/*
536 * Copy a filename
537 */
538static void copy_filename(char *dst, const char *src)
539{
540 size_t len = strlen(src);
541
542 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800543 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400544 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800545 }
546 strncpy(dst, src, FILENAME_MAX);
547}
548
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700549/*
550 * Convert a string to Make-safe form
551 */
552static char *quote_for_make(const char *str)
553{
554 const char *p;
555 char *os, *q;
556
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700558 size_t nbs = 0;
559
560 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400561 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700562
563 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400564 switch (*p) {
565 case ' ':
566 case '\t':
567 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
568 n += nbs + 2;
569 nbs = 0;
570 break;
571 case '$':
572 case '#':
573 nbs = 0;
574 n += 2;
575 break;
576 case '\\':
577 nbs++;
578 n++;
579 break;
580 default:
581 nbs = 0;
582 n++;
583 break;
584 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700585 }
586
587 /* Convert N backslashes at the end of filename to 2N backslashes */
588 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400589 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700590
591 os = q = nasm_malloc(n);
592
593 nbs = 0;
594 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400595 switch (*p) {
596 case ' ':
597 case '\t':
598 while (nbs--)
599 *q++ = '\\';
600 *q++ = '\\';
601 *q++ = *p;
602 break;
603 case '$':
604 *q++ = *p;
605 *q++ = *p;
606 nbs = 0;
607 break;
608 case '#':
609 *q++ = '\\';
610 *q++ = *p;
611 nbs = 0;
612 break;
613 case '\\':
614 *q++ = *p;
615 nbs++;
616 break;
617 default:
618 *q++ = *p;
619 nbs = 0;
620 break;
621 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700622 }
623 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400624 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700625
626 *q = '\0';
627
628 return os;
629}
630
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000632 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000634};
635
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100636enum text_options {
637 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800638 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100639};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800640static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 {"prefix", OPT_PREFIX},
642 {"postfix", OPT_POSTFIX},
643 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000644};
645
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400646static void show_version(void)
647{
648 printf("NASM version %s compiled on %s%s\n",
649 nasm_version, nasm_date, nasm_compile_options);
650 exit(0);
651}
652
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653static bool stopoptions = false;
654static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000655{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000656 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800657 int i;
658 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700659 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000660
661 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800662 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000663
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400665 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
666 /* These parameters take values */
667 if (!(param = get_param(p, q, &advance)))
668 return advance;
669 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800670
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 switch (p[1]) {
672 case 's':
673 error_file = stdout;
674 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700675
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400676 case 'o': /* output file */
677 copy_filename(outname, param);
678 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700679
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400680 case 'f': /* output format */
681 ofmt = ofmt_find(param, &ofmt_alias);
682 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800683 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400684 "unrecognised output format `%s' - "
685 "use -hf for a list", param);
686 }
687 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700688
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400689 case 'O': /* Optimization level */
690 {
691 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700692
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400693 if (!*param) {
694 /* Naked -O == -Ox */
695 optimizing = MAX_OPTIMIZE;
696 } else {
697 while (*param) {
698 switch (*param) {
699 case '0': case '1': case '2': case '3': case '4':
700 case '5': case '6': case '7': case '8': case '9':
701 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700702
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 /* -O0 -> optimizing == -1, 0.98 behaviour */
704 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
705 if (opt < 2)
706 optimizing = opt - 1;
707 else
708 optimizing = opt;
709 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700710
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400711 case 'v':
712 case '+':
713 param++;
714 opt_verbose_info = true;
715 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700716
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 case 'x':
718 param++;
719 optimizing = MAX_OPTIMIZE;
720 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700721
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800723 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 "unknown optimization option -O%c\n",
725 *param);
726 break;
727 }
728 }
729 if (optimizing > MAX_OPTIMIZE)
730 optimizing = MAX_OPTIMIZE;
731 }
732 break;
733 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'p': /* pre-include */
736 case 'P':
737 preproc->pre_include(param);
738 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800739
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 case 'd': /* pre-define */
741 case 'D':
742 preproc->pre_define(param);
743 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800744
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400745 case 'u': /* un-define */
746 case 'U':
747 preproc->pre_undefine(param);
748 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800749
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400750 case 'i': /* include search path */
751 case 'I':
752 preproc->include_path(param);
753 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 case 'l': /* listing file */
756 copy_filename(listname, param);
757 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800758
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400759 case 'Z': /* error messages file */
760 copy_filename(errname, param);
761 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800762
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400763 case 'F': /* specify debug format */
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400764 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800765 debug_format = param;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400766 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800767
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400768 case 'X': /* specify error reporting format */
769 if (nasm_stricmp("vc", param) == 0)
770 nasm_set_verror(nasm_verror_vc);
771 else if (nasm_stricmp("gnu", param) == 0)
772 nasm_set_verror(nasm_verror_gnu);
773 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800774 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400775 "unrecognized error reporting format `%s'",
776 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800778
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700780 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800781 if (p[2])
782 debug_format = nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800784
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 case 'h':
786 printf
787 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
788 "[-l listfile]\n"
789 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400790 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800791 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700793 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700795 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400796 " -MG d:o, missing files assumed generated\n"
797 " -MF <file> set Makefile dependency file\n"
798 " -MD <file> assemble and generate dependencies\n"
799 " -MT <file> dependency target name\n"
800 " -MQ <file> dependency target name (quoted)\n"
801 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700802 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800804 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800806 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400807 " -o outfile write output to an outfile\n\n"
808 " -f format select an output format\n\n"
809 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 " -I<path> adds a pathname to the include file path\n");
811 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700812 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400813 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400814 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400815 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 " -P<file> pre-includes a file\n"
817 " -D<macro>[=<value>] pre-defines a macro\n"
818 " -U<macro> undefines a macro\n"
819 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800820 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400821 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400822 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400823 "--prefix,--postfix\n"
824 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100825 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800826 "Warnings:\n");
827 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700829 warnings[i].name, warnings[i].help,
830 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 printf
832 ("\nresponse files should contain command line parameters"
833 ", one per line.\n");
834 if (p[2] == 'f') {
835 printf("\nvalid output formats for -f are"
836 " (`*' denotes default):\n");
837 ofmt_list(ofmt, stdout);
838 } else {
839 printf("\nFor a list of valid output formats, use -hf.\n");
840 printf("For a list of debug formats, use -f <form> -y.\n");
841 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 case 'y':
846 printf("\nvalid debug formats for '%s' output format are"
847 " ('*' denotes default):\n", ofmt->shortname);
848 dfmt_list(ofmt, stdout);
849 exit(0);
850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700853 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400857 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800859
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 case 'e': /* preprocess only */
861 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400862 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800864
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400865 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400866 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800868
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400869 case 'W':
870 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
871 do_warn = false;
872 param += 3;
873 } else {
874 do_warn = true;
875 }
876 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800877
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800879 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400880 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400882 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400884 do_warn = (param[0] == '+');
885 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400886
887set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400888 for (i = 0; i <= ERR_WARN_MAX; i++) {
889 if (!nasm_stricmp(param, warnings[i].name))
890 break;
891 }
892 if (i <= ERR_WARN_MAX) {
893 warning_on_global[i] = do_warn;
894 } else if (!nasm_stricmp(param, "all")) {
895 for (i = 1; i <= ERR_WARN_MAX; i++)
896 warning_on_global[i] = do_warn;
897 } else if (!nasm_stricmp(param, "none")) {
898 for (i = 1; i <= ERR_WARN_MAX; i++)
899 warning_on_global[i] = !do_warn;
900 } else {
H. Peter Anvinb4f734f2016-05-09 12:13:08 -0700901 /* Ignore invalid warning names; forward compatibility */
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400902 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 switch (p[2]) {
907 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400908 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400909 break;
910 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400911 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400912 depend_missing_ok = true;
913 break;
914 case 'P':
915 depend_emit_phony = true;
916 break;
917 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300918 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400919 depend_file = q;
920 advance = true;
921 break;
922 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400923 depend_file = q;
924 advance = true;
925 break;
926 case 'T':
927 depend_target = q;
928 advance = true;
929 break;
930 case 'Q':
931 depend_target = quote_for_make(q);
932 advance = true;
933 break;
934 default:
935 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
936 "unknown dependency option `-M%c'", p[2]);
937 break;
938 }
939 if (advance && (!q || !q[0])) {
940 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
941 "option `-M%c' requires a parameter", p[2]);
942 break;
943 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 case '-':
947 {
948 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000949
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 if (p[2] == 0) { /* -- => stop processing options */
951 stopoptions = 1;
952 break;
953 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400954
955 if (!nasm_stricmp(p, "--v"))
956 show_version();
957
Andy Willis3f546032016-09-13 00:02:21 +0300958 if (!nasm_stricmp(p, "--version"))
959 show_version();
960
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 for (s = 0; textopts[s].label; s++) {
962 if (!nasm_stricmp(p + 2, textopts[s].label)) {
963 break;
964 }
965 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 switch (s) {
968
969 case OPT_PREFIX:
970 case OPT_POSTFIX:
971 {
972 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400973 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 ERR_USAGE,
975 "option `--%s' requires an argument",
976 p + 2);
977 break;
978 } else {
979 advance = 1, param = q;
980 }
981
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100982 switch (s) {
983 case OPT_PREFIX:
984 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100986 case OPT_POSTFIX:
987 strlcpy(lpostfix, param, POSTFIX_MAX);
988 break;
989 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800990 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100991 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 break;
993 }
994 break;
995 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800996
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 default:
998 {
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 `--%s'", p + 2);
1001 break;
1002 }
1003 }
1004 break;
1005 }
1006
1007 default:
1008 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001009 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 "unrecognised option `-%c'", p[1]);
1011 break;
1012 }
1013 } else {
1014 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001015 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001017 } else {
1018 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001019 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001020 }
1021
1022 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001023}
1024
H. Peter Anvineba20a72002-04-30 20:53:55 +00001025#define ARG_BUF_DELTA 128
1026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001028{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001029 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030 int bufsize, prevargsize;
1031
1032 bufsize = prevargsize = ARG_BUF_DELTA;
1033 buffer = nasm_malloc(ARG_BUF_DELTA);
1034 prevarg = nasm_malloc(ARG_BUF_DELTA);
1035 prevarg[0] = '\0';
1036
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 p = buffer;
1039 while (1) { /* Loop to handle long lines */
1040 q = fgets(p, bufsize - (p - buffer), rfile);
1041 if (!q)
1042 break;
1043 p += strlen(p);
1044 if (p > buffer && p[-1] == '\n')
1045 break;
1046 if (p - buffer > bufsize - 10) {
1047 int offset;
1048 offset = p - buffer;
1049 bufsize += ARG_BUF_DELTA;
1050 buffer = nasm_realloc(buffer, bufsize);
1051 p = buffer + offset;
1052 }
1053 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (!q && p == buffer) {
1056 if (prevarg[0])
1057 process_arg(prevarg, NULL);
1058 nasm_free(buffer);
1059 nasm_free(prevarg);
1060 return;
1061 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 /*
1064 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1065 * them are present at the end of the line.
1066 */
1067 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001069 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001071
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001072 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 if (process_arg(prevarg, p))
1075 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001076
Charles Crayne192d5b52007-10-18 19:02:42 -07001077 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 prevargsize += ARG_BUF_DELTA;
1079 prevarg = nasm_realloc(prevarg, prevargsize);
1080 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001081 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001082 }
1083}
1084
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001085/* Function to process args from a string of args, rather than the
1086 * argv array. Used by the environment variable and response file
1087 * processing.
1088 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001089static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001091 char *p, *q, *arg, *prevarg;
1092 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093
1094 p = args;
1095 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001097 arg = NULL;
1098 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 q = p;
1100 while (*p && *p != separator)
1101 p++;
1102 while (*p == separator)
1103 *p++ = '\0';
1104 prevarg = arg;
1105 arg = q;
1106 if (process_arg(prevarg, arg))
1107 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001108 }
1109 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001111}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001112
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001113static void process_response_file(const char *file)
1114{
1115 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001116 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001117 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001118 perror(file);
1119 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001120 }
1121 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001122 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001123 }
1124 fclose(f);
1125}
1126
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001127static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001128{
1129 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001130 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001131 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001132
Charles Craynefcce07f2007-09-30 22:15:36 -07001133 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001134
H. Peter Anvin972079f2008-09-30 17:01:23 -07001135 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137
1138 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001139 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001140 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001141 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001142 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 envcopy = nasm_strdup(envreal);
1144 process_args(envcopy);
1145 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001146 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001147
1148 /*
1149 * Now process the actual command line.
1150 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001152 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 argv++;
1154 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001155 /*
1156 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 * arguments like the environment variable. This allows us
1158 * to have multiple arguments on a single line, which is
1159 * different to the -@resp file processing below for regular
1160 * NASM.
1161 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001162 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 argc--;
1164 argv++;
1165 }
1166 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001167 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001168 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001169 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001170 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 process_respfile(rfile);
1172 fclose(rfile);
1173 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001174 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 "unable to open response file `%s'", p);
1176 }
1177 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001178 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1179 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001180 }
1181
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001182 /*
1183 * Look for basic command line typos. This definitely doesn't
1184 * catch all errors, but it might help cases of fumbled fingers.
1185 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001186 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001187 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001188 "no input file specified");
1189 else if (!strcmp(inname, errname) ||
1190 !strcmp(inname, outname) ||
1191 !strcmp(inname, listname) ||
1192 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001193 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001194 "file `%s' is both input and output file",
1195 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001196
H. Peter Anvin70653092007-10-19 14:42:29 -07001197 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001198 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001199 if (!error_file) {
1200 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001201 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001202 "cannot open file `%s' for error messages",
1203 errname);
1204 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001205 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001206}
1207
H. Peter Anvin70055962007-10-11 00:05:31 -07001208static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001209
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001210static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001211{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001212 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001214 int i, validid;
1215 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001216 int32_t seg;
1217 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001220 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001221
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001222 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001223 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001224
Charles Craynec1905c22008-09-11 18:54:06 -07001225 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001226 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 int pass1, pass2;
1228 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001230 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001231 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1232 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001233
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001234 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001235
Keith Kaniosb7a89542007-04-12 02:40:54 +00001236 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 cpu = cmd_cpu;
1238 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001239 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001241 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001242 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001244 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001245 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 saa_rewind(forwrefs);
1247 forwref = saa_rstruct(forwrefs);
1248 raa_free(offsets);
1249 offsets = raa_init();
1250 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001251 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001252 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001253
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001255 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001256 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001257 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001258
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001260 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001262
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001263 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001264 * Here we parse our directives; this is not handled by the
1265 * 'real' parser. This really should be a separate function.
1266 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001268 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001269 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001270 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001271
H. Peter Anvin70055962007-10-11 00:05:31 -07001272 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001273 case D_SEGMENT: /* [SEGMENT n] */
1274 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 seg = ofmt->section(value, pass2, &sb);
1276 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001277 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001278 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 value);
1280 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001281 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 location.segment = seg;
1283 }
1284 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001285 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001286 if (*value) {
1287 stdscan_reset();
1288 stdscan_set(value);
1289 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001290 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001291 if (e) {
1292 unsigned int align = (unsigned int)e->value;
1293 if ((uint64_t)e->value > 0x7fffffff) {
1294 /*
1295 * FIXME: Please make some sane message here
1296 * ofmt should have some 'check' method which
1297 * would report segment alignment bounds.
1298 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001299 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001300 "incorrect segment alignment `%s'", value);
1301 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001302 nasm_error(ERR_NONFATAL,
1303 "segment alignment `%s' is not power of two",
1304 value);
1305 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001306
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001307 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001308 if (location.segment != NO_SEG)
1309 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001310 }
1311 }
1312 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001313 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (*value == '$')
1315 value++; /* skip initial $ if present */
1316 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 q = value;
1318 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001320 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 *q++ = '\0';
1322 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001324 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001326 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001328 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001331 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333 }
1334 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001335 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 "identifier expected after EXTERN");
1337 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338 }
1339 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 *q++ = '\0';
1341 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 special = NULL;
1344 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1345 int temp = pass0;
1346 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001347 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001349 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 } /* else pass0 == 1 */
1353 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001354 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001355 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001357 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (*value == '$')
1359 value++; /* skip initial $ if present */
1360 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001361 q = value;
1362 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 *q++ = '\0';
1366 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001370 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001371 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001372 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001375 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001377 }
1378 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001379 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 "identifier expected after GLOBAL");
1381 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001382 }
1383 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 *q++ = '\0';
1385 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001386 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001388 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 } /* pass == 1 */
1390 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001391 case D_COMMON: /* [COMMON symbol size:special] */
1392 {
1393 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001394
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (*value == '$')
1396 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001397 p = value;
1398 validid = true;
1399 if (!isidstart(*p))
1400 validid = false;
1401 while (*p && !nasm_isspace(*p)) {
1402 if (!isidchar(*p))
1403 validid = false;
1404 p++;
1405 }
1406 if (!validid) {
1407 nasm_error(ERR_NONFATAL,
1408 "identifier expected after COMMON");
1409 break;
1410 }
1411 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001412 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001413 q = p;
1414 while (*q && *q != ':')
1415 q++;
1416 if (*q == ':') {
1417 *q++ = '\0';
1418 special = q;
1419 } else {
1420 special = NULL;
1421 }
1422 size = readnum(p, &rn_error);
1423 if (rn_error) {
1424 nasm_error(ERR_NONFATAL,
1425 "invalid size specified"
1426 " in COMMON declaration");
1427 break;
1428 }
1429 } else {
1430 nasm_error(ERR_NONFATAL,
1431 "no size specified in"
1432 " COMMON declaration");
1433 break;
1434 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001435
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001436 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001437 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001438 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001439 if (special)
1440 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 }
1442 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001443 }
1444 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001446 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001448 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 if (e) {
1450 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001451 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 1 ? ERR_NONFATAL : ERR_PANIC,
1453 "cannot use non-relocatable expression as "
1454 "ABSOLUTE address");
1455 else {
1456 abs_seg = reloc_seg(e);
1457 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001458 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001459 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 abs_offset = 0x100; /* don't go near zero in case of / */
1461 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001462 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001464 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 location.segment = NO_SEG;
1466 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001467 case D_DEBUG: /* [DEBUG] */
1468 {
1469 char debugid[128];
1470 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001471
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 p = value;
1473 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001474 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001475 if (!isidstart(*p)) {
1476 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001477 } else {
1478 while (*p && !nasm_isspace(*p)) {
1479 if (q >= debugid + sizeof debugid - 1) {
1480 overlong = true;
1481 break;
1482 }
1483 if (!isidchar(*p))
1484 badid = true;
1485 *q++ = *p++;
1486 }
1487 *q = 0;
1488 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001489 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1491 "identifier expected after DEBUG");
1492 break;
1493 }
1494 if (overlong) {
1495 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1496 "DEBUG identifier too long");
1497 break;
1498 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001499 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001500 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001501 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001503 }
1504 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001505 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001506 switch(*value) {
1507 case '-': validid = 0; value++; break;
1508 case '+': validid = 1; value++; break;
1509 case '*': validid = 2; value++; break;
1510 default: validid = 1; break;
1511 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 for (i = 1; i <= ERR_WARN_MAX; i++)
1514 if (!nasm_stricmp(value, warnings[i].name))
1515 break;
1516 if (i <= ERR_WARN_MAX) {
1517 switch(validid) {
1518 case 0:
1519 warning_on[i] = false;
1520 break;
1521 case 1:
1522 warning_on[i] = true;
1523 break;
1524 case 2:
1525 warning_on[i] = warning_on_global[i];
1526 break;
1527 }
H. Peter Anvinb4f734f2016-05-09 12:13:08 -07001528 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001530 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 cpu = get_cpu(value);
1532 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001533 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001534 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 if (*value == '+') {
1536 user_nolist = 0;
1537 } else {
1538 if (*value == '-') {
1539 user_nolist = 1;
1540 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001541 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 }
1543 }
1544 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001545 case D_DEFAULT: /* [DEFAULT] */
1546 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001547 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001548 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001549 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001550 switch ((int)tokval.t_integer) {
1551 case S_REL:
1552 globalrel = 1;
1553 break;
1554 case S_ABS:
1555 globalrel = 0;
1556 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001557 case P_BND:
1558 globalbnd = 1;
1559 break;
1560 case P_NOBND:
1561 globalbnd = 0;
1562 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001563 default:
1564 err = 1;
1565 break;
1566 }
1567 } else {
1568 err = 1;
1569 }
1570 break;
1571 case D_FLOAT:
1572 if (float_option(value)) {
1573 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1574 "unknown 'float' directive: %s",
1575 value);
1576 }
1577 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001579 if (ofmt->directive(d, value, pass2))
1580 break;
1581 /* else fall through */
1582 case D_unknown:
1583 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1584 "unrecognised directive [%s]",
1585 directive);
1586 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001588 if (err) {
1589 nasm_error(ERR_NONFATAL,
1590 "invalid parameter to [%s] directive",
1591 directive);
1592 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001594 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595
Charles Crayne2581c862008-09-10 19:21:52 -07001596 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001600 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 forwref = saa_rstruct(forwrefs);
1602 } while (forwref != NULL
1603 && forwref->lineno == globallineno);
1604 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001605 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001606
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001607 if (output_ins.forw_ref) {
1608 if (passn == 1) {
1609 for (i = 0; i < output_ins.operands; i++) {
1610 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1611 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1612 fwinf->lineno = globallineno;
1613 fwinf->operand = i;
1614 }
1615 }
1616 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001617 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 /* forw_ref */
1621 if (output_ins.opcode == I_EQU) {
1622 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001623 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 * Special `..' EQUs get processed in pass two,
1625 * except `..@' macro-processor EQUs which are done
1626 * in the normal place.
1627 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001628 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001629 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632 else if (output_ins.label[0] != '.' ||
1633 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 output_ins.label[2] == '@') {
1635 if (output_ins.operands == 1 &&
1636 (output_ins.oprs[0].type & IMMEDIATE) &&
1637 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001638 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 def_label(output_ins.label,
1640 output_ins.oprs[0].segment,
1641 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001642 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001644 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001646 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001648 && (output_ins.oprs[1].type & IMMEDIATE)
1649 && output_ins.oprs[1].segment == NO_SEG
1650 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001652 output_ins.oprs[0].offset | SEG_ABS,
1653 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001654 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001656 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001659 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001660 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 * Special `..' EQUs get processed here, except
1662 * `..@' macro processor EQUs which are done above.
1663 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 output_ins.label[1] == '.' &&
1666 output_ins.label[2] != '@') {
1667 if (output_ins.operands == 1 &&
1668 (output_ins.oprs[0].type & IMMEDIATE)) {
1669 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 output_ins.oprs[0].segment,
1671 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001672 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001674 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001676 && output_ins.oprs[0].segment == NO_SEG
1677 && (output_ins.oprs[1].type & IMMEDIATE)
1678 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001680 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001681 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001682 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001684 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001686 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001687 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001689
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001691
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001692 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001693 &output_ins);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001694 l *= output_ins.times;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 /* if (using_debug_info) && output_ins.opcode != -1) */
1697 if (using_debug_info)
1698 { /* fbk 03/25/01 */
1699 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001700 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 TYS_ELEMENTS(output_ins.operands);
1702 switch (output_ins.opcode) {
1703 case I_RESB:
1704 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001705 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 break;
1707 case I_RESW:
1708 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001709 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 break;
1711 case I_RESD:
1712 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001713 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 break;
1715 case I_RESQ:
1716 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001717 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 break;
1719 case I_REST:
1720 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001721 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001723 case I_RESO:
1724 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001725 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001726 break;
1727 case I_RESY:
1728 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001729 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001730 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 case I_DB:
1732 typeinfo |= TY_BYTE;
1733 break;
1734 case I_DW:
1735 typeinfo |= TY_WORD;
1736 break;
1737 case I_DD:
1738 if (output_ins.eops_float)
1739 typeinfo |= TY_FLOAT;
1740 else
1741 typeinfo |= TY_DWORD;
1742 break;
1743 case I_DQ:
1744 typeinfo |= TY_QWORD;
1745 break;
1746 case I_DT:
1747 typeinfo |= TY_TBYTE;
1748 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001749 case I_DO:
1750 typeinfo |= TY_OWORD;
1751 break;
1752 case I_DY:
1753 typeinfo |= TY_YWORD;
1754 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 default:
1756 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001757
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001759
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001760 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001761 }
1762 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001764 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001765 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 * else l == -1 => invalid instruction, which will be
1768 * flagged as an error on pass 2
1769 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001770
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001771 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001773 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001774 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 }
1777 } /* not an EQU */
1778 cleanup_insn(&output_ins);
1779 }
1780 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001781 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001783
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001784 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001785 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001786 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001787
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 if (pass1 == 1)
1789 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001790
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001791 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001793 } else if (global_offset_changed &&
1794 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001795 prev_offset_changed = global_offset_changed;
1796 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001797 } else {
1798 stall_count++;
1799 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001800
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001801 if (terminate_after_phase)
1802 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001803
1804 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001805 /* We get here if the labels don't converge
1806 * Example: FOO equ FOO + 1
1807 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001808 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001809 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001810 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001811 nasm_error(ERR_NONFATAL,
1812 "Possible causes: recursive EQUs, macro abuse.");
1813 break;
1814 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001815 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001818 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001819 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001820 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001821 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1822 }
1823}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001824
H. Peter Anvin70055962007-10-11 00:05:31 -07001825static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001827 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001829 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001831 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001833 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001834 q = strchr(buf, ']');
1835 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001836 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001837
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001838 /* stip off the comments */
1839 p = strchr(buf, ';');
1840 if (p) {
1841 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001842 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001843 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001844 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001845
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001846 /* no brace, no trailing spaces */
1847 *q = '\0';
1848 nasm_zap_spaces_rev(--q);
1849
1850 /* directive */
1851 p = nasm_skip_spaces(++buf);
1852 q = nasm_skip_word(p);
1853 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001854 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001855 *q = '\0';
1856 *directive = p;
1857
1858 /* and value finally */
1859 p = nasm_skip_spaces(++q);
1860 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001861
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001862 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001863}
1864
Ed Berosetfa771012002-06-09 20:56:40 +00001865/**
1866 * gnu style error reporting
1867 * This function prints an error message to error_file in the
1868 * style used by GNU. An example would be:
1869 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001870 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001871 * which the error occurs (or is detected) and "error:" is one of
1872 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001873 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001874 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001875 *
1876 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001877 * @param fmt the printf style format string
1878 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001879static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001880{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001881 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001882 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001883
Ed Berosetfa771012002-06-09 20:56:40 +00001884 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001886
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001887 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001888 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001889
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001890 if (!skip_this_pass(severity)) {
1891 if (currentfile) {
1892 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001893 } else {
1894 fputs("nasm: ", error_file);
1895 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001896 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001897
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001898 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001899}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001900
Ed Berosetfa771012002-06-09 20:56:40 +00001901/**
1902 * MS style error reporting
1903 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001904 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001905 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001906 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001907 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001908 * which the error occurs (or is detected) and "error:" is one of
1909 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001910 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001911 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001912 *
1913 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001914 * @param fmt the printf style format string
1915 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001916static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001917{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001918 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001919 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001920
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 if (is_suppressed_warning(severity))
1922 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001923
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001924 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001926
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001927 if (!skip_this_pass(severity)) {
1928 if (currentfile) {
1929 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001930 } else {
1931 fputs("nasm: ", error_file);
1932 }
Ed Berosetfa771012002-06-09 20:56:40 +00001933 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001934
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001935 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001936}
1937
1938/**
1939 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001940 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001941 * not in pass 1
1942 *
1943 * @param severity the severity of the warning or error
1944 * @return true if we should abort error/warning printing
1945 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001946static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001947{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001948 /* Not a warning at all */
1949 if ((severity & ERR_MASK) != ERR_WARNING)
1950 return false;
1951
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001952 /* Might be a warning but suppresed explicitly */
1953 if (severity & ERR_WARN_MASK)
1954 return !warning_on[WARN_IDX(severity)];
1955 else
1956 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001957}
1958
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001959static bool skip_this_pass(int severity)
1960{
H. Peter Anvin934f0472016-05-09 12:00:19 -07001961 /* See if it's a pass-specific warning which should be skipped. */
1962
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001963 if ((severity & ERR_MASK) > ERR_WARNING)
1964 return false;
1965
H. Peter Anvin934f0472016-05-09 12:00:19 -07001966 /*
1967 * passn is 1 on the very first pass only.
1968 * pass0 is 2 on the code-generation (final) pass only.
1969 * These are the passes we care about in this case.
1970 */
1971 return (((severity & ERR_PASS1) && passn != 1) ||
1972 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001973}
1974
Ed Berosetfa771012002-06-09 20:56:40 +00001975/**
1976 * common error reporting
1977 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001978 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001979 * specific error message to error_file and may or may not return. It
1980 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001981 *
1982 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001983 * @param fmt the printf style format string
1984 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001985static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001986{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001987 char msg[1024];
1988 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001989
H. Peter Anvin7df04172008-06-10 18:27:38 -07001990 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001992 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 break;
1994 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001995 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 break;
1997 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001998 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 break;
2000 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002001 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 break;
2003 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002004 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002006 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002007 pfx = "";
2008 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002009 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010
H. Peter Anvin934f0472016-05-09 12:00:19 -07002011 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002012 if ((severity & (ERR_WARN_MASK|ERR_PP_LISTMACRO)) == ERR_WARN_MASK) {
H. Peter Anvin934f0472016-05-09 12:00:19 -07002013 char *p = strchr(msg, '\0');
2014 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
2015 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002016
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002017 if (!skip_this_pass(severity))
2018 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002019
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002020 /* Are we recursing from error_list_macros? */
2021 if (severity & ERR_PP_LISTMACRO)
2022 return;
2023
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002024 /*
2025 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002026 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002027 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07002028 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002029
2030 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002031 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002032
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002033 preproc->error_list_macros(severity);
2034
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002035 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 case ERR_DEBUG:
2037 /* no further action, by definition */
2038 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002039 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002040 /* Treat warnings as errors */
2041 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2042 terminate_after_phase = true;
2043 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002045 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 break;
2047 case ERR_FATAL:
2048 if (ofile) {
2049 fclose(ofile);
2050 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002051 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002052 }
2053 if (want_usage)
2054 usage();
2055 exit(1); /* instantly die */
2056 break; /* placate silly compilers */
2057 case ERR_PANIC:
2058 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002059 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002060 if (ofile) {
2061 fclose(ofile);
2062 remove(outname);
2063 ofile = NULL;
2064 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 exit(3);
2066 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002067 }
2068}
2069
H. Peter Anvin734b1882002-04-30 21:01:08 +00002070static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002071{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002072 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002073}
2074
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002075static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002076{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002077 iflag_t r;
2078
2079 iflag_clear_all(&r);
2080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002082 iflag_set(&r, IF_8086);
2083 else if (!strcmp(value, "186"))
2084 iflag_set(&r, IF_186);
2085 else if (!strcmp(value, "286"))
2086 iflag_set(&r, IF_286);
2087 else if (!strcmp(value, "386"))
2088 iflag_set(&r, IF_386);
2089 else if (!strcmp(value, "486"))
2090 iflag_set(&r, IF_486);
2091 else if (!strcmp(value, "586") ||
2092 !nasm_stricmp(value, "pentium"))
2093 iflag_set(&r, IF_PENT);
2094 else if (!strcmp(value, "686") ||
2095 !nasm_stricmp(value, "ppro") ||
2096 !nasm_stricmp(value, "pentiumpro") ||
2097 !nasm_stricmp(value, "p2"))
2098 iflag_set(&r, IF_P6);
2099 else if (!nasm_stricmp(value, "p3") ||
2100 !nasm_stricmp(value, "katmai"))
2101 iflag_set(&r, IF_KATMAI);
2102 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2103 !nasm_stricmp(value, "willamette"))
2104 iflag_set(&r, IF_WILLAMETTE);
2105 else if (!nasm_stricmp(value, "prescott"))
2106 iflag_set(&r, IF_PRESCOTT);
2107 else if (!nasm_stricmp(value, "x64") ||
2108 !nasm_stricmp(value, "x86-64"))
2109 iflag_set(&r, IF_X86_64);
2110 else if (!nasm_stricmp(value, "ia64") ||
2111 !nasm_stricmp(value, "ia-64") ||
2112 !nasm_stricmp(value, "itanium")||
2113 !nasm_stricmp(value, "itanic") ||
2114 !nasm_stricmp(value, "merced"))
2115 iflag_set(&r, IF_IA64);
2116 else {
2117 iflag_set(&r, IF_PLEVEL);
2118 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2119 "unknown 'cpu' type");
2120 }
2121 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002122}
2123
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002124static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002125{
2126 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002127
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 if ((i = atoi(value)) == 16)
2129 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002130 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002131 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002132 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 "cannot specify 32-bit segment on processor below a 386");
2134 i = 16;
2135 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002136 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002137 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002138 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002139 "cannot specify 64-bit segment on processor below an x86-64");
2140 i = 16;
2141 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002142 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002143 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002144 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 value);
2146 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002147 }
2148 return i;
2149}