tasm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Program entry point, command line parsing
  3. *
  4. * Copyright (C) 2001-2008 Peter Johnson
  5. * Copyright (C) 2007-2008 Samuel Thibault
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <util.h>
  29. #include <ctype.h>
  30. #include <libyasm/compat-queue.h>
  31. #include <libyasm/bitvect.h>
  32. #include <libyasm.h>
  33. #ifdef HAVE_LIBGEN_H
  34. #include <libgen.h>
  35. #endif
  36. #include "tasm-options.h"
  37. #if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
  38. #include "yasm-plugin.h"
  39. #endif
  40. #include "license.c"
  41. #define DEFAULT_OBJFMT_MODULE "bin"
  42. #if defined(CMAKE_BUILD) && !defined(BUILD_SHARED_LIBS)
  43. void yasm_init_plugin(void);
  44. #endif
  45. /*@null@*/ /*@only@*/ static char *obj_filename = NULL, *in_filename = NULL;
  46. /*@null@*/ /*@only@*/ static char *list_filename = NULL, *xref_filename = NULL;
  47. /*@null@*/ /*@only@*/ static char *machine_name = NULL;
  48. static int special_options = 0;
  49. static int segment_ordering = 0;
  50. static int cross_reference = 0;
  51. static int floating_point = 0;
  52. static int listing = 0;
  53. static int expanded_listing = 0;
  54. static int case_sensitivity = 0;
  55. static int valid_length = -1;
  56. /*@null@*/ /*@dependent@*/ static yasm_arch *cur_arch = NULL;
  57. /*@null@*/ /*@dependent@*/ static const yasm_arch_module *
  58. cur_arch_module = NULL;
  59. /*@null@*/ /*@dependent@*/ static const yasm_parser_module *
  60. cur_parser_module = NULL;
  61. /*@null@*/ /*@dependent@*/ static yasm_preproc *cur_preproc = NULL;
  62. /*@null@*/ /*@dependent@*/ static const yasm_preproc_module *
  63. cur_preproc_module = NULL;
  64. /*@null@*/ static char *objfmt_keyword = NULL;
  65. /*@null@*/ /*@dependent@*/ static const yasm_objfmt_module *
  66. cur_objfmt_module = NULL;
  67. /*@null@*/ /*@dependent@*/ static const yasm_dbgfmt_module *
  68. cur_dbgfmt_module = NULL;
  69. /*@null@*/ /*@dependent@*/ static yasm_listfmt *cur_listfmt = NULL;
  70. /*@null@*/ /*@dependent@*/ static const yasm_listfmt_module *
  71. cur_listfmt_module = NULL;
  72. static int warning_error = 0; /* warnings being treated as errors */
  73. static FILE *errfile;
  74. /*@null@*/ /*@only@*/ static char *error_filename = NULL;
  75. /*@null@*/ /*@dependent@*/ static FILE *open_file(const char *filename,
  76. const char *mode);
  77. static void check_errors(/*@only@*/ yasm_errwarns *errwarns,
  78. /*@only@*/ yasm_object *object,
  79. /*@only@*/ yasm_linemap *linemap);
  80. static void cleanup(/*@null@*/ /*@only@*/ yasm_object *object);
  81. /* Forward declarations: cmd line parser handlers */
  82. static int opt_special_handler(char *cmd, /*@null@*/ char *param, int extra);
  83. static int opt_segment_ordering_handler(char *cmd, /*@null@*/ char *param, int extra);
  84. static int opt_cross_reference_handler(char *cmd, /*@null@*/ char *param, int extra);
  85. static int opt_floating_point_handler(char *cmd, /*@null@*/ char *param, int extra);
  86. static int opt_ignore(char *cmd, /*@null@*/ char *param, int extra);
  87. static int opt_listing_handler(char *cmd, /*@null@*/ char *param, int extra);
  88. static int opt_case_handler(char *cmd, /*@null@*/ char *param, int extra);
  89. static int opt_valid_length_handler(char *cmd, /*@null@*/ char *param, int extra);
  90. static int opt_warning_handler(char *cmd, /*@null@*/ char *param, int extra);
  91. static int opt_preproc_option(char *cmd, /*@null@*/ char *param, int extra);
  92. static int opt_exe_handler(char *cmd, /*@null@*/ char *param, int extra);
  93. static /*@only@*/ char *replace_extension(const char *orig, /*@null@*/
  94. const char *ext, const char *def);
  95. static void print_error(const char *fmt, ...);
  96. static /*@exits@*/ void handle_yasm_int_error(const char *file,
  97. unsigned int line,
  98. const char *message);
  99. static /*@exits@*/ void handle_yasm_fatal(const char *message, va_list va);
  100. static const char *handle_yasm_gettext(const char *msgid);
  101. static void print_yasm_error(const char *filename, unsigned long line,
  102. const char *msg, /*@null@*/ const char *xref_fn,
  103. unsigned long xref_line,
  104. /*@null@*/ const char *xref_msg);
  105. static void print_yasm_warning(const char *filename, unsigned long line,
  106. const char *msg);
  107. static void apply_preproc_builtins(void);
  108. static void apply_preproc_standard_macros(const yasm_stdmac *stdmacs);
  109. static void apply_preproc_saved_options(void);
  110. static void print_list_keyword_desc(const char *name, const char *keyword);
  111. /* values for special_options */
  112. #define SPECIAL_SHOW_HELP 0x01
  113. #define SPECIAL_SHOW_VERSION 0x02
  114. #define SPECIAL_SHOW_LICENSE 0x04
  115. #define SEGMENT_ORDERING_ALPHABETIC 0x01
  116. #define SEGMENT_ORDERING_SOURCE 0x02
  117. #define FP_EMULATED 0x01
  118. #define FP_REAL 0x02
  119. #define CASE_ALL 0x01
  120. #define CASE_GLOBALS 0x02
  121. #define CASE_NONE 0x04
  122. #define DEBUG_FULL 0x01
  123. #define DEBUG_LINES 0x02
  124. #define DEBUG_NONE 0x04
  125. /* command line options */
  126. static opt_option options[] =
  127. {
  128. { "version", 0, opt_special_handler, SPECIAL_SHOW_VERSION,
  129. N_("show version text"), NULL },
  130. { "license", 0, opt_special_handler, SPECIAL_SHOW_LICENSE,
  131. N_("show license text"), NULL },
  132. { "a", 0, opt_segment_ordering_handler, SEGMENT_ORDERING_ALPHABETIC,
  133. N_("Alphabetic segment ordering"), NULL },
  134. { "s", 0, opt_segment_ordering_handler, SEGMENT_ORDERING_SOURCE,
  135. N_("Source segment ordering"), NULL },
  136. { "c", 0, opt_cross_reference_handler, 0,
  137. N_("Generate cross-reference in listing"), NULL },
  138. { "d", 1, opt_preproc_option, 2,
  139. N_("pre-define a macro, optionally to value"), N_("macro[=value]") },
  140. { "e", 0, opt_floating_point_handler, FP_EMULATED,
  141. N_("Emulated floating-point instructions (not supported)"), NULL },
  142. { "r", 0, opt_floating_point_handler, FP_REAL,
  143. N_("Real floating-point instructions"), NULL },
  144. { "h", 0, opt_special_handler, SPECIAL_SHOW_HELP,
  145. N_("show help text"), NULL },
  146. { "?", 0, opt_special_handler, SPECIAL_SHOW_HELP,
  147. N_("show help text"), NULL },
  148. { "i", 1, opt_preproc_option, 0,
  149. N_("add include path"), N_("path") },
  150. { "j", 1, opt_ignore, 0,
  151. N_("Jam in an assemble directive CMD (eg. /jIDEAL) (not supported)"), NULL },
  152. { "k", 1, opt_ignore, 0,
  153. N_("Hash table capacity (ignored)"), N_("# symbols") },
  154. { "l", 0, opt_listing_handler, 0,
  155. N_("Generate listing"), N_("l=normal listing, la=expanded listing") },
  156. { "ml", 0, opt_case_handler, CASE_ALL,
  157. N_("Case sensitivity on all symbols"), NULL },
  158. { "mx", 0, opt_case_handler, CASE_GLOBALS,
  159. N_("Case sensitivity on global symbols"), NULL },
  160. { "mu", 0, opt_case_handler, CASE_NONE,
  161. N_("No case sensitivity on symbols"), NULL },
  162. { "mv", 0, opt_valid_length_handler, 0,
  163. N_("Set maximum valid length for symbols"), N_("length") },
  164. { "m", 1, opt_ignore, 0,
  165. N_("Allow multiple passes to resolve forward reference (ignored)"), N_("number of passes") },
  166. { "n", 0, opt_ignore, 0,
  167. N_("Suppress symbol tables in listing"), NULL },
  168. { "o", 0, opt_ignore, 0,
  169. N_("Object code"), N_("os: standard, o: standard w/overlays, op: Phar Lap, oi: IBM") },
  170. { "p", 0, opt_ignore, 0,
  171. N_("Check for code segment overrides in protected mode"), NULL },
  172. { "q", 0, opt_ignore, 0,
  173. N_("Suppress OBJ records not needed for linking (ignored)"), NULL },
  174. { "t", 0, opt_ignore, 0,
  175. N_("Suppress messages if successful assembly"), NULL },
  176. { "u", 0, opt_ignore, 0,
  177. N_("Set version emulation"), N_("Version") },
  178. { "w", 1, opt_warning_handler, 0,
  179. N_("Set warning level"), N_("w0=none, w1=w2=warnings on, w-xxx/w+xxx=disable/enable warning xxx") },
  180. { "x", 0, opt_ignore, 0,
  181. N_("Include false conditionals in listing"), NULL },
  182. { "zi", 0, opt_ignore, DEBUG_FULL,
  183. N_("Full debug info"), NULL },
  184. { "zd", 0, opt_ignore, DEBUG_LINES,
  185. N_("Line numbers debug info"), NULL },
  186. { "zn", 0, opt_ignore, DEBUG_NONE,
  187. N_("No debug info"), NULL },
  188. { "z", 0, opt_ignore, 0,
  189. N_("Display source line with error message (ignored)"), NULL },
  190. { "b", 0, opt_exe_handler, 0,
  191. N_("Build a (very) basic .exe file"), NULL },
  192. };
  193. /* version message */
  194. /*@observer@*/ static const char *version_msg[] = {
  195. PACKAGE_STRING,
  196. "Compiled on " __DATE__ ".",
  197. "Copyright (c) 2001-2010 Peter Johnson and other Yasm developers.",
  198. "Run yasm --license for licensing overview and summary."
  199. };
  200. /* help messages */
  201. /*@observer@*/ static const char *help_head = N_(
  202. "usage: tasm [option]* source [,object] [,listing] [,xref] \n"
  203. "Options:\n");
  204. /*@observer@*/ static const char *help_tail = N_(
  205. "\n"
  206. "source is asm source to be assembled.\n"
  207. "\n"
  208. "Sample invocation:\n"
  209. " tasm /zi source.asm\n"
  210. "\n"
  211. "Report bugs to bug-yasm@tortall.net\n");
  212. /* parsed command line storage until appropriate modules have been loaded */
  213. typedef STAILQ_HEAD(constcharparam_head, constcharparam) constcharparam_head;
  214. typedef struct constcharparam {
  215. STAILQ_ENTRY(constcharparam) link;
  216. const char *param;
  217. int id;
  218. } constcharparam;
  219. static constcharparam_head preproc_options;
  220. static int
  221. do_assemble(void)
  222. {
  223. yasm_object *object;
  224. const char *base_filename;
  225. /*@null@*/ FILE *obj = NULL;
  226. yasm_arch_create_error arch_error;
  227. yasm_linemap *linemap;
  228. yasm_errwarns *errwarns = yasm_errwarns_create();
  229. int i, matched;
  230. /* Initialize line map */
  231. linemap = yasm_linemap_create();
  232. yasm_linemap_set(linemap, in_filename, 0, 1, 1);
  233. /* determine the object filename if not specified */
  234. if (!obj_filename) {
  235. if (in_filename == NULL)
  236. /* Default to yasm.out if no obj filename specified */
  237. obj_filename = yasm__xstrdup("yasm.out");
  238. else {
  239. /* replace (or add) extension to base filename */
  240. yasm__splitpath(in_filename, &base_filename);
  241. if (base_filename[0] == '\0')
  242. obj_filename = yasm__xstrdup("yasm.out");
  243. else
  244. obj_filename = replace_extension(base_filename,
  245. "obj",
  246. "yasm.out");
  247. }
  248. }
  249. cur_arch = yasm_arch_create(cur_arch_module, machine_name,
  250. cur_parser_module->keyword, &arch_error);
  251. if (!cur_arch) {
  252. switch (arch_error) {
  253. case YASM_ARCH_CREATE_BAD_MACHINE:
  254. print_error(_("%s: `%s' is not a valid %s for %s `%s'"),
  255. _("FATAL"), machine_name, _("machine"),
  256. _("architecture"), cur_arch_module->keyword);
  257. break;
  258. case YASM_ARCH_CREATE_BAD_PARSER:
  259. print_error(_("%s: `%s' is not a valid %s for %s `%s'"),
  260. _("FATAL"), cur_parser_module->keyword,
  261. _("parser"), _("architecture"),
  262. cur_arch_module->keyword);
  263. break;
  264. default:
  265. print_error(_("%s: unknown architecture error"), _("FATAL"));
  266. }
  267. return EXIT_FAILURE;
  268. }
  269. /* Create object */
  270. object = yasm_object_create(in_filename, obj_filename, cur_arch,
  271. cur_objfmt_module, cur_dbgfmt_module);
  272. if (!object) {
  273. yasm_error_class eclass;
  274. unsigned long xrefline;
  275. /*@only@*/ /*@null@*/ char *estr, *xrefstr;
  276. yasm_error_fetch(&eclass, &estr, &xrefline, &xrefstr);
  277. print_error("%s: %s", _("FATAL"), estr);
  278. yasm_xfree(estr);
  279. yasm_xfree(xrefstr);
  280. cleanup(object);
  281. return EXIT_FAILURE;
  282. }
  283. /* Get a fresh copy of objfmt_module as it may have changed. */
  284. cur_objfmt_module = ((yasm_objfmt_base *)object->objfmt)->module;
  285. /* Check to see if the requested preprocessor is in the allowed list
  286. * for the active parser.
  287. */
  288. matched = 0;
  289. for (i=0; cur_parser_module->preproc_keywords[i]; i++)
  290. if (yasm__strcasecmp(cur_parser_module->preproc_keywords[i],
  291. cur_preproc_module->keyword) == 0)
  292. matched = 1;
  293. if (!matched) {
  294. print_error(_("%s: `%s' is not a valid %s for %s `%s'"), _("FATAL"),
  295. cur_preproc_module->keyword, _("preprocessor"),
  296. _("parser"), cur_parser_module->keyword);
  297. cleanup(object);
  298. return EXIT_FAILURE;
  299. }
  300. cur_preproc = yasm_preproc_create(cur_preproc_module, in_filename,
  301. object->symtab, linemap, errwarns);
  302. apply_preproc_builtins();
  303. apply_preproc_standard_macros(cur_parser_module->stdmacs);
  304. apply_preproc_standard_macros(cur_objfmt_module->stdmacs);
  305. apply_preproc_saved_options();
  306. /* Get initial x86 BITS setting from object format */
  307. if (strcmp(cur_arch_module->keyword, "x86") == 0) {
  308. yasm_arch_set_var(cur_arch, "mode_bits",
  309. cur_objfmt_module->default_x86_mode_bits);
  310. }
  311. /* Parse! */
  312. cur_parser_module->do_parse(object, cur_preproc, list_filename != NULL,
  313. linemap, errwarns);
  314. check_errors(errwarns, object, linemap);
  315. /* Finalize parse */
  316. yasm_object_finalize(object, errwarns);
  317. check_errors(errwarns, object, linemap);
  318. /* Optimize */
  319. yasm_object_optimize(object, errwarns);
  320. check_errors(errwarns, object, linemap);
  321. /* generate any debugging information */
  322. yasm_dbgfmt_generate(object, linemap, errwarns);
  323. check_errors(errwarns, object, linemap);
  324. /* open the object file for output (if not already opened by dbg objfmt) */
  325. if (!obj && strcmp(cur_objfmt_module->keyword, "dbg") != 0) {
  326. obj = open_file(obj_filename, "wb");
  327. if (!obj) {
  328. cleanup(object);
  329. return EXIT_FAILURE;
  330. }
  331. }
  332. /* Write the object file */
  333. yasm_objfmt_output(object, obj?obj:stderr,
  334. strcmp(cur_dbgfmt_module->keyword, "null"), errwarns);
  335. /* Close object file */
  336. if (obj)
  337. fclose(obj);
  338. /* If we had an error at this point, we also need to delete the output
  339. * object file (to make sure it's not left newer than the source).
  340. */
  341. if (yasm_errwarns_num_errors(errwarns, warning_error) > 0)
  342. remove(obj_filename);
  343. check_errors(errwarns, object, linemap);
  344. /* Open and write the list file */
  345. if (list_filename) {
  346. FILE *list = open_file(list_filename, "wt");
  347. if (!list) {
  348. cleanup(object);
  349. return EXIT_FAILURE;
  350. }
  351. /* Initialize the list format */
  352. cur_listfmt = yasm_listfmt_create(cur_listfmt_module, in_filename,
  353. obj_filename);
  354. yasm_listfmt_output(cur_listfmt, list, linemap, cur_arch);
  355. fclose(list);
  356. }
  357. yasm_errwarns_output_all(errwarns, linemap, warning_error,
  358. print_yasm_error, print_yasm_warning);
  359. yasm_linemap_destroy(linemap);
  360. yasm_errwarns_destroy(errwarns);
  361. cleanup(object);
  362. return EXIT_SUCCESS;
  363. }
  364. /* main function */
  365. /*@-globstate -unrecog@*/
  366. int
  367. main(int argc, char *argv[])
  368. {
  369. size_t i;
  370. errfile = stderr;
  371. #if defined(HAVE_SETLOCALE) && defined(HAVE_LC_MESSAGES)
  372. setlocale(LC_MESSAGES, "");
  373. #endif
  374. #if defined(LOCALEDIR)
  375. bindtextdomain(PACKAGE, LOCALEDIR);
  376. #endif
  377. textdomain(PACKAGE);
  378. /* Initialize errwarn handling */
  379. yasm_internal_error_ = handle_yasm_int_error;
  380. yasm_fatal = handle_yasm_fatal;
  381. yasm_gettext_hook = handle_yasm_gettext;
  382. yasm_errwarn_initialize();
  383. /* Initialize BitVector (needed for intnum/floatnum). */
  384. if (BitVector_Boot() != ErrCode_Ok) {
  385. print_error(_("%s: could not initialize BitVector"), _("FATAL"));
  386. return EXIT_FAILURE;
  387. }
  388. /* Initialize intnum and floatnum */
  389. yasm_intnum_initialize();
  390. yasm_floatnum_initialize();
  391. #ifdef CMAKE_BUILD
  392. /* Load standard modules */
  393. #ifdef BUILD_SHARED_LIBS
  394. if (!load_plugin("yasmstd")) {
  395. print_error(_("%s: could not load standard modules"), _("FATAL"));
  396. return EXIT_FAILURE;
  397. }
  398. #else
  399. yasm_init_plugin();
  400. #endif
  401. #endif
  402. /* Initialize parameter storage */
  403. STAILQ_INIT(&preproc_options);
  404. if (parse_cmdline(argc, argv, options, NELEMS(options), print_error))
  405. return EXIT_FAILURE;
  406. switch (special_options) {
  407. case SPECIAL_SHOW_HELP:
  408. /* Does gettext calls internally */
  409. help_msg(help_head, help_tail, options, NELEMS(options));
  410. return EXIT_SUCCESS;
  411. case SPECIAL_SHOW_VERSION:
  412. for (i=0; i<NELEMS(version_msg); i++)
  413. printf("%s\n", version_msg[i]);
  414. return EXIT_SUCCESS;
  415. case SPECIAL_SHOW_LICENSE:
  416. for (i=0; i<NELEMS(license_msg); i++)
  417. printf("%s\n", license_msg[i]);
  418. return EXIT_SUCCESS;
  419. }
  420. /* Open error file if specified. */
  421. if (error_filename) {
  422. errfile = open_file(error_filename, "wt");
  423. if (!errfile)
  424. return EXIT_FAILURE;
  425. }
  426. /* If not already specified, default to bin as the object format. */
  427. if (!cur_objfmt_module) {
  428. if (!objfmt_keyword)
  429. objfmt_keyword = yasm__xstrdup(DEFAULT_OBJFMT_MODULE);
  430. cur_objfmt_module = yasm_load_objfmt(objfmt_keyword);
  431. if (!cur_objfmt_module) {
  432. print_error(_("%s: could not load default %s"), _("FATAL"),
  433. _("object format"));
  434. return EXIT_FAILURE;
  435. }
  436. }
  437. /* TASM's architecture is x86 */
  438. cur_arch_module = yasm_load_arch("x86");
  439. if (!cur_arch_module) {
  440. print_error(_("%s: could not load %s"), _("FATAL"),
  441. _("architecture"));
  442. return EXIT_FAILURE;
  443. }
  444. machine_name =
  445. yasm__xstrdup(cur_arch_module->default_machine_keyword);
  446. /* Check for arch help */
  447. if (machine_name && strcmp(machine_name, "help") == 0) {
  448. const yasm_arch_machine *m = cur_arch_module->machines;
  449. printf(_("Available %s for %s `%s':\n"), _("machines"),
  450. _("architecture"), cur_arch_module->keyword);
  451. while (m->keyword && m->name) {
  452. print_list_keyword_desc(m->name, m->keyword);
  453. m++;
  454. }
  455. return EXIT_SUCCESS;
  456. }
  457. cur_parser_module = yasm_load_parser("tasm");
  458. if (!cur_parser_module) {
  459. print_error(_("%s: could not load %s"), _("FATAL"),
  460. _("parser"));
  461. cleanup(NULL);
  462. return EXIT_FAILURE;
  463. }
  464. /* If not already specified, default to the parser's default preproc. */
  465. if (!cur_preproc_module) {
  466. cur_preproc_module =
  467. yasm_load_preproc(cur_parser_module->default_preproc_keyword);
  468. if (!cur_preproc_module) {
  469. print_error(_("%s: could not load default %s"), _("FATAL"),
  470. _("preprocessor"));
  471. cleanup(NULL);
  472. return EXIT_FAILURE;
  473. }
  474. }
  475. /* Determine input filename and open input file. */
  476. if (!in_filename) {
  477. print_error(_("No input files specified"));
  478. return EXIT_FAILURE;
  479. }
  480. /* If list file enabled, make sure we have a list format loaded. */
  481. if (list_filename) {
  482. /* use nasm as the list format. */
  483. cur_listfmt_module = yasm_load_listfmt("nasm");
  484. }
  485. /* If not already specified, default to null as the debug format. */
  486. if (!cur_dbgfmt_module) {
  487. cur_dbgfmt_module = yasm_load_dbgfmt("null");
  488. if (!cur_dbgfmt_module) {
  489. print_error(_("%s: could not load default %s"), _("FATAL"),
  490. _("debug format"));
  491. return EXIT_FAILURE;
  492. }
  493. }
  494. return do_assemble();
  495. }
  496. /*@=globstate =unrecog@*/
  497. /* Open the object file. Returns 0 on failure. */
  498. static FILE *
  499. open_file(const char *filename, const char *mode)
  500. {
  501. FILE *f;
  502. f = fopen(filename, mode);
  503. if (!f)
  504. print_error(_("could not open file `%s'"), filename);
  505. return f;
  506. }
  507. static void
  508. check_errors(yasm_errwarns *errwarns, yasm_object *object,
  509. yasm_linemap *linemap)
  510. {
  511. if (yasm_errwarns_num_errors(errwarns, warning_error) > 0) {
  512. yasm_errwarns_output_all(errwarns, linemap, warning_error,
  513. print_yasm_error, print_yasm_warning);
  514. yasm_linemap_destroy(linemap);
  515. yasm_errwarns_destroy(errwarns);
  516. cleanup(object);
  517. exit(EXIT_FAILURE);
  518. }
  519. }
  520. /* Define DO_FREE to 1 to enable deallocation of all data structures.
  521. * Useful for detecting memory leaks, but slows down execution unnecessarily
  522. * (as the OS will free everything we miss here).
  523. */
  524. #define DO_FREE 1
  525. /* Cleans up all allocated structures. */
  526. static void
  527. cleanup(yasm_object *object)
  528. {
  529. if (DO_FREE) {
  530. if (cur_listfmt)
  531. yasm_listfmt_destroy(cur_listfmt);
  532. if (cur_preproc)
  533. yasm_preproc_destroy(cur_preproc);
  534. if (object)
  535. yasm_object_destroy(object);
  536. yasm_floatnum_cleanup();
  537. yasm_intnum_cleanup();
  538. yasm_errwarn_cleanup();
  539. BitVector_Shutdown();
  540. }
  541. if (DO_FREE) {
  542. if (in_filename)
  543. yasm_xfree(in_filename);
  544. if (obj_filename)
  545. yasm_xfree(obj_filename);
  546. if (list_filename)
  547. yasm_xfree(list_filename);
  548. if (xref_filename)
  549. yasm_xfree(xref_filename);
  550. if (machine_name)
  551. yasm_xfree(machine_name);
  552. if (objfmt_keyword)
  553. yasm_xfree(objfmt_keyword);
  554. }
  555. if (errfile != stderr && errfile != stdout)
  556. fclose(errfile);
  557. #if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
  558. unload_plugins();
  559. #endif
  560. }
  561. /*
  562. * Command line options handlers
  563. */
  564. static char ** const filenames[] = {
  565. &in_filename, &obj_filename, &list_filename, &xref_filename, NULL
  566. }, ** const * cur_filename = &filenames[0];
  567. static int filename_handler(char *param) {
  568. if (!*cur_filename) {
  569. print_error(_("error: too many files on command line."));
  570. return 1;
  571. }
  572. if (*param)
  573. **cur_filename = yasm__xstrdup(param);
  574. return 0;
  575. }
  576. int
  577. not_an_option_handler(char *param) {
  578. char *c, *d = param;
  579. while ((c = strchr(d, ','))) {
  580. *c = '\0';
  581. if (filename_handler(d))
  582. return 1;
  583. d = c + 1;
  584. cur_filename++;
  585. }
  586. filename_handler(d);
  587. return 0;
  588. }
  589. static int
  590. opt_special_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  591. {
  592. if (special_options == 0)
  593. special_options = extra;
  594. return 0;
  595. }
  596. static int
  597. opt_segment_ordering_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  598. {
  599. segment_ordering = extra;
  600. return 0;
  601. }
  602. static int
  603. opt_cross_reference_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  604. {
  605. cross_reference = 1;
  606. return 0;
  607. }
  608. static int
  609. opt_floating_point_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  610. {
  611. floating_point = extra;
  612. return 0;
  613. }
  614. static int
  615. opt_ignore(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  616. {
  617. return 0;
  618. }
  619. static int
  620. opt_listing_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  621. {
  622. if (param && param[0]) {
  623. if (param[0] != 'a')
  624. return 1;
  625. expanded_listing = 1;
  626. }
  627. listing = 1;
  628. return 0;
  629. }
  630. static int
  631. opt_case_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  632. {
  633. case_sensitivity = extra;
  634. return 0;
  635. }
  636. static int
  637. opt_valid_length_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra)
  638. {
  639. valid_length = atoi(param);
  640. return 0;
  641. }
  642. static int
  643. opt_warning_handler(char *cmd, /*@unused@*/ char *param, int extra)
  644. {
  645. /* is it disabling the warning instead of enabling? */
  646. void (*action)(yasm_warn_class wclass) = NULL;
  647. if (cmd[0] == '0') {
  648. /* /w0, disable warnings */
  649. yasm_warn_disable_all();
  650. return 0;
  651. }
  652. if (cmd[0] == '1' || cmd[0] == '2') {
  653. /* /w[12], enable warnings */
  654. yasm_warn_enable(YASM_WARN_UNREC_CHAR);
  655. yasm_warn_enable(YASM_WARN_ORPHAN_LABEL);
  656. yasm_warn_enable(YASM_WARN_UNINIT_CONTENTS);
  657. return 0;
  658. }
  659. /* detect no- prefix to disable the warning */
  660. if (cmd[0] == '-') {
  661. action = yasm_warn_disable;
  662. } else if (cmd[0] == '+') {
  663. action = yasm_warn_enable;
  664. } else return 1;
  665. /* skip past '+/-' */
  666. cmd++;
  667. if (cmd[0] == '\0')
  668. /* just /w- or /w+, so definitely not valid */
  669. return 1;
  670. else if (strcmp(cmd, "error") == 0)
  671. warning_error = (action == yasm_warn_enable);
  672. else if (strcmp(cmd, "unrecognized-char") == 0)
  673. action(YASM_WARN_UNREC_CHAR);
  674. else if (strcmp(cmd, "orphan-labels") == 0)
  675. action(YASM_WARN_ORPHAN_LABEL);
  676. else if (strcmp(cmd, "uninit-contents") == 0)
  677. action(YASM_WARN_UNINIT_CONTENTS);
  678. else if (strcmp(cmd, "size-override") == 0)
  679. action(YASM_WARN_SIZE_OVERRIDE);
  680. else
  681. return 1;
  682. return 0;
  683. }
  684. static int
  685. opt_preproc_option(/*@unused@*/ char *cmd, char *param, int extra)
  686. {
  687. constcharparam *cp;
  688. cp = yasm_xmalloc(sizeof(constcharparam));
  689. cp->param = param;
  690. cp->id = extra;
  691. STAILQ_INSERT_TAIL(&preproc_options, cp, link);
  692. return 0;
  693. }
  694. static int
  695. opt_exe_handler(char *cmd, /*@unused@*/ char *param, int extra)
  696. {
  697. objfmt_keyword = yasm__xstrdup("dosexe");
  698. return 0;
  699. }
  700. static void
  701. apply_preproc_builtins()
  702. {
  703. char *predef;
  704. if (!objfmt_keyword)
  705. objfmt_keyword = yasm__xstrdup(DEFAULT_OBJFMT_MODULE);
  706. /* Define standard YASM assembly-time macro constants */
  707. predef = yasm_xmalloc(strlen("__YASM_OBJFMT__=")
  708. + strlen(objfmt_keyword) + 1);
  709. strcpy(predef, "__YASM_OBJFMT__=");
  710. strcat(predef, objfmt_keyword);
  711. yasm_preproc_define_builtin(cur_preproc, predef);
  712. yasm_xfree(predef);
  713. }
  714. static void
  715. apply_preproc_standard_macros(const yasm_stdmac *stdmacs)
  716. {
  717. int i, matched;
  718. if (!stdmacs)
  719. return;
  720. matched = -1;
  721. for (i=0; stdmacs[i].parser; i++)
  722. if (yasm__strcasecmp(stdmacs[i].parser,
  723. cur_parser_module->keyword) == 0 &&
  724. yasm__strcasecmp(stdmacs[i].preproc,
  725. cur_preproc_module->keyword) == 0)
  726. matched = i;
  727. if (matched >= 0 && stdmacs[matched].macros)
  728. yasm_preproc_add_standard(cur_preproc, stdmacs[matched].macros);
  729. }
  730. static void
  731. apply_preproc_saved_options()
  732. {
  733. constcharparam *cp, *cpnext;
  734. void (*funcs[3])(yasm_preproc *, const char *);
  735. funcs[0] = cur_preproc_module->add_include_file;
  736. funcs[1] = cur_preproc_module->predefine_macro;
  737. funcs[2] = cur_preproc_module->undefine_macro;
  738. STAILQ_FOREACH(cp, &preproc_options, link) {
  739. if (0 <= cp->id && cp->id < 3 && funcs[cp->id])
  740. funcs[cp->id](cur_preproc, cp->param);
  741. }
  742. cp = STAILQ_FIRST(&preproc_options);
  743. while (cp != NULL) {
  744. cpnext = STAILQ_NEXT(cp, link);
  745. yasm_xfree(cp);
  746. cp = cpnext;
  747. }
  748. STAILQ_INIT(&preproc_options);
  749. }
  750. /* Replace extension on a filename (or append one if none is present).
  751. * If output filename would be identical to input (same extension out as in),
  752. * returns (copy of) def.
  753. * A NULL ext means the trailing '.' should NOT be included, whereas a "" ext
  754. * means the trailing '.' should be included.
  755. */
  756. static char *
  757. replace_extension(const char *orig, /*@null@*/ const char *ext,
  758. const char *def)
  759. {
  760. char *out, *outext;
  761. size_t deflen, outlen;
  762. /* allocate enough space for full existing name + extension */
  763. outlen = strlen(orig) + 2;
  764. if (ext)
  765. outlen += strlen(ext) + 1;
  766. deflen = strlen(def) + 1;
  767. if (outlen < deflen)
  768. outlen = deflen;
  769. out = yasm_xmalloc(outlen);
  770. strcpy(out, orig);
  771. outext = strrchr(out, '.');
  772. if (outext) {
  773. /* Existing extension: make sure it's not the same as the replacement
  774. * (as we don't want to overwrite the source file).
  775. */
  776. outext++; /* advance past '.' */
  777. if (ext && strcmp(outext, ext) == 0) {
  778. outext = NULL; /* indicate default should be used */
  779. print_error(
  780. _("file name already ends in `.%s': output will be in `%s'"),
  781. ext, def);
  782. }
  783. } else {
  784. /* No extension: make sure the output extension is not empty
  785. * (again, we don't want to overwrite the source file).
  786. */
  787. if (!ext)
  788. print_error(
  789. _("file name already has no extension: output will be in `%s'"),
  790. def);
  791. else {
  792. outext = strrchr(out, '\0'); /* point to end of the string */
  793. *outext++ = '.'; /* append '.' */
  794. }
  795. }
  796. /* replace extension or use default name */
  797. if (outext) {
  798. if (!ext) {
  799. /* Back up and replace '.' with string terminator */
  800. outext--;
  801. *outext = '\0';
  802. } else
  803. strcpy(outext, ext);
  804. } else
  805. strcpy(out, def);
  806. return out;
  807. }
  808. void
  809. print_list_keyword_desc(const char *name, const char *keyword)
  810. {
  811. printf("%4s%-12s%s\n", "", keyword, name);
  812. }
  813. static void
  814. print_error(const char *fmt, ...)
  815. {
  816. va_list va;
  817. fprintf(errfile, "tasm: ");
  818. va_start(va, fmt);
  819. vfprintf(errfile, fmt, va);
  820. va_end(va);
  821. fputc('\n', errfile);
  822. }
  823. static /*@exits@*/ void
  824. handle_yasm_int_error(const char *file, unsigned int line, const char *message)
  825. {
  826. fprintf(stderr, _("INTERNAL ERROR at %s, line %u: %s\n"), file, line,
  827. gettext(message));
  828. #ifdef HAVE_ABORT
  829. abort();
  830. #else
  831. exit(EXIT_FAILURE);
  832. #endif
  833. }
  834. static /*@exits@*/ void
  835. handle_yasm_fatal(const char *fmt, va_list va)
  836. {
  837. fprintf(errfile, "**%s**: ", _("Fatal"));
  838. vfprintf(errfile, gettext(fmt), va);
  839. fputc('\n', errfile);
  840. exit(EXIT_FAILURE);
  841. }
  842. static const char *
  843. handle_yasm_gettext(const char *msgid)
  844. {
  845. return gettext(msgid);
  846. }
  847. static void
  848. print_yasm_error(const char *filename, unsigned long line, const char *msg,
  849. const char *xref_fn, unsigned long xref_line,
  850. const char *xref_msg)
  851. {
  852. if (line)
  853. fprintf(errfile, "**%s** %s(%lu) %s\n", _("Error"), filename, line, msg);
  854. else
  855. fprintf(errfile, "**%s** %s %s\n", _("Error"), filename, msg);
  856. if (/* xref_fn && */ xref_msg) {
  857. if (xref_line)
  858. fprintf(errfile, "**%s** %s(%lu) %s\n", _("Error"), filename, xref_line, xref_msg);
  859. else
  860. fprintf(errfile, "**%s** %s %s\n", _("Error"), filename, xref_msg);
  861. }
  862. }
  863. static void
  864. print_yasm_warning(const char *filename, unsigned long line, const char *msg)
  865. {
  866. if (line)
  867. fprintf(errfile, "*%s* %s(%lu) %s\n", _("Warning"), filename, line, msg);
  868. else
  869. fprintf(errfile, "*%s* %s %s\n", _("Warning"), filename, msg);
  870. }