PageRenderTime 358ms CodeModel.GetById 201ms RepoModel.GetById 1ms app.codeStats 0ms

/avr-binutils-2.20/binutils-2.20/ld/deffilep.y

#
Happy | 1121 lines | 980 code | 141 blank | 0 comment | 0 complexity | 91490a0ef5cef4ec3c4aef7f5b71b15e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  1. %{ /* deffilep.y - parser for .def files */
  2. /* Copyright 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
  3. 2007, 2009 Free Software Foundation, Inc.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "libiberty.h"
  19. #include "safe-ctype.h"
  20. #include "bfd.h"
  21. #include "ld.h"
  22. #include "ldmisc.h"
  23. #include "deffile.h"
  24. #define TRACE 0
  25. #define ROUND_UP(a, b) (((a)+((b)-1))&~((b)-1))
  26. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  27. as well as gratuitiously global symbol names, so we can have multiple
  28. yacc generated parsers in ld. Note that these are only the variables
  29. produced by yacc. If other parser generators (bison, byacc, etc) produce
  30. additional global names that conflict at link time, then those parser
  31. generators need to be fixed instead of adding those names to this list. */
  32. #define yymaxdepth def_maxdepth
  33. #define yyparse def_parse
  34. #define yylex def_lex
  35. #define yyerror def_error
  36. #define yylval def_lval
  37. #define yychar def_char
  38. #define yydebug def_debug
  39. #define yypact def_pact
  40. #define yyr1 def_r1
  41. #define yyr2 def_r2
  42. #define yydef def_def
  43. #define yychk def_chk
  44. #define yypgo def_pgo
  45. #define yyact def_act
  46. #define yyexca def_exca
  47. #define yyerrflag def_errflag
  48. #define yynerrs def_nerrs
  49. #define yyps def_ps
  50. #define yypv def_pv
  51. #define yys def_s
  52. #define yy_yys def_yys
  53. #define yystate def_state
  54. #define yytmp def_tmp
  55. #define yyv def_v
  56. #define yy_yyv def_yyv
  57. #define yyval def_val
  58. #define yylloc def_lloc
  59. #define yyreds def_reds /* With YYDEBUG defined. */
  60. #define yytoks def_toks /* With YYDEBUG defined. */
  61. #define yylhs def_yylhs
  62. #define yylen def_yylen
  63. #define yydefred def_yydefred
  64. #define yydgoto def_yydgoto
  65. #define yysindex def_yysindex
  66. #define yyrindex def_yyrindex
  67. #define yygindex def_yygindex
  68. #define yytable def_yytable
  69. #define yycheck def_yycheck
  70. static void def_description (const char *);
  71. static void def_exports (const char *, const char *, int, int);
  72. static void def_heapsize (int, int);
  73. static void def_import (const char *, const char *, const char *, const char *,
  74. int);
  75. static void def_image_name (const char *, int, int);
  76. static void def_section (const char *, int);
  77. static void def_section_alt (const char *, const char *);
  78. static void def_stacksize (int, int);
  79. static void def_version (int, int);
  80. static void def_directive (char *);
  81. static void def_aligncomm (char *str, int align);
  82. static int def_parse (void);
  83. static int def_error (const char *);
  84. static int def_lex (void);
  85. static int lex_forced_token = 0;
  86. static const char *lex_parse_string = 0;
  87. static const char *lex_parse_string_end = 0;
  88. %}
  89. %union {
  90. char *id;
  91. int number;
  92. char *digits;
  93. };
  94. %token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL
  95. %token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL
  96. %token PRIVATEU PRIVATEL ALIGNCOMM
  97. %token READ WRITE EXECUTE SHARED NONAMEU NONAMEL DIRECTIVE
  98. %token <id> ID
  99. %token <digits> DIGITS
  100. %type <number> NUMBER
  101. %type <digits> opt_digits
  102. %type <number> opt_base opt_ordinal
  103. %type <number> attr attr_list opt_number exp_opt_list exp_opt
  104. %type <id> opt_name opt_equal_name dot_name anylang_id opt_id
  105. %%
  106. start: start command
  107. | command
  108. ;
  109. command:
  110. NAME opt_name opt_base { def_image_name ($2, $3, 0); }
  111. | LIBRARY opt_name opt_base { def_image_name ($2, $3, 1); }
  112. | DESCRIPTION ID { def_description ($2);}
  113. | STACKSIZE_K NUMBER opt_number { def_stacksize ($2, $3);}
  114. | HEAPSIZE NUMBER opt_number { def_heapsize ($2, $3);}
  115. | CODE attr_list { def_section ("CODE", $2);}
  116. | DATAU attr_list { def_section ("DATA", $2);}
  117. | SECTIONS seclist
  118. | EXPORTS explist
  119. | IMPORTS implist
  120. | VERSIONK NUMBER { def_version ($2, 0);}
  121. | VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
  122. | DIRECTIVE ID { def_directive ($2);}
  123. | ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
  124. ;
  125. explist:
  126. /* EMPTY */
  127. | expline
  128. | explist expline
  129. ;
  130. expline:
  131. /* The opt_comma is necessary to support both the usual
  132. DEF file syntax as well as .drectve syntax which
  133. mandates <expsym>,<expoptlist>. */
  134. dot_name opt_equal_name opt_ordinal opt_comma exp_opt_list
  135. { def_exports ($1, $2, $3, $5); }
  136. ;
  137. exp_opt_list:
  138. /* The opt_comma is necessary to support both the usual
  139. DEF file syntax as well as .drectve syntax which
  140. allows for comma separated opt list. */
  141. exp_opt opt_comma exp_opt_list { $$ = $1 | $3; }
  142. | { $$ = 0; }
  143. ;
  144. exp_opt:
  145. NONAMEU { $$ = 1; }
  146. | NONAMEL { $$ = 1; }
  147. | CONSTANTU { $$ = 2; }
  148. | CONSTANTL { $$ = 2; }
  149. | DATAU { $$ = 4; }
  150. | DATAL { $$ = 4; }
  151. | PRIVATEU { $$ = 8; }
  152. | PRIVATEL { $$ = 8; }
  153. ;
  154. implist:
  155. implist impline
  156. | impline
  157. ;
  158. impline:
  159. ID '=' ID '.' ID '.' ID { def_import ($1, $3, $5, $7, -1); }
  160. | ID '=' ID '.' ID '.' NUMBER { def_import ($1, $3, $5, 0, $7); }
  161. | ID '=' ID '.' ID { def_import ($1, $3, 0, $5, -1); }
  162. | ID '=' ID '.' NUMBER { def_import ($1, $3, 0, 0, $5); }
  163. | ID '.' ID '.' ID { def_import ( 0, $1, $3, $5, -1); }
  164. | ID '.' ID { def_import ( 0, $1, 0, $3, -1); }
  165. ;
  166. seclist:
  167. seclist secline
  168. | secline
  169. ;
  170. secline:
  171. ID attr_list { def_section ($1, $2);}
  172. | ID ID { def_section_alt ($1, $2);}
  173. ;
  174. attr_list:
  175. attr_list opt_comma attr { $$ = $1 | $3; }
  176. | attr { $$ = $1; }
  177. ;
  178. opt_comma:
  179. ','
  180. |
  181. ;
  182. opt_number: ',' NUMBER { $$=$2;}
  183. | { $$=-1;}
  184. ;
  185. attr:
  186. READ { $$ = 1;}
  187. | WRITE { $$ = 2;}
  188. | EXECUTE { $$=4;}
  189. | SHARED { $$=8;}
  190. ;
  191. opt_name: ID { $$ = $1; }
  192. | ID '.' ID
  193. {
  194. char *name = xmalloc (strlen ($1) + 1 + strlen ($3) + 1);
  195. sprintf (name, "%s.%s", $1, $3);
  196. $$ = name;
  197. }
  198. | { $$ = ""; }
  199. ;
  200. opt_ordinal:
  201. '@' NUMBER { $$ = $2;}
  202. | { $$ = -1;}
  203. ;
  204. opt_equal_name:
  205. '=' dot_name { $$ = $2; }
  206. | { $$ = 0; }
  207. ;
  208. opt_base: BASE '=' NUMBER { $$ = $3;}
  209. | { $$ = -1;}
  210. ;
  211. dot_name: ID { $$ = $1; }
  212. | dot_name '.' ID
  213. {
  214. char *name = xmalloc (strlen ($1) + 1 + strlen ($3) + 1);
  215. sprintf (name, "%s.%s", $1, $3);
  216. $$ = name;
  217. }
  218. ;
  219. anylang_id: ID { $$ = $1; }
  220. | anylang_id '.' opt_digits opt_id
  221. {
  222. char *id = xmalloc (strlen ($1) + 1 + strlen ($3) + strlen ($4) + 1);
  223. sprintf (id, "%s.%s%s", $1, $3, $4);
  224. $$ = id;
  225. }
  226. ;
  227. opt_digits: DIGITS { $$ = $1; }
  228. | { $$ = ""; }
  229. ;
  230. opt_id: ID { $$ = $1; }
  231. | { $$ = ""; }
  232. ;
  233. NUMBER: DIGITS { $$ = strtoul ($1, 0, 0); }
  234. %%
  235. /*****************************************************************************
  236. API
  237. *****************************************************************************/
  238. static FILE *the_file;
  239. static const char *def_filename;
  240. static int linenumber;
  241. static def_file *def;
  242. static int saw_newline;
  243. struct directive
  244. {
  245. struct directive *next;
  246. char *name;
  247. int len;
  248. };
  249. static struct directive *directives = 0;
  250. def_file *
  251. def_file_empty (void)
  252. {
  253. def_file *rv = xmalloc (sizeof (def_file));
  254. memset (rv, 0, sizeof (def_file));
  255. rv->is_dll = -1;
  256. rv->base_address = (bfd_vma) -1;
  257. rv->stack_reserve = rv->stack_commit = -1;
  258. rv->heap_reserve = rv->heap_commit = -1;
  259. rv->version_major = rv->version_minor = -1;
  260. return rv;
  261. }
  262. def_file *
  263. def_file_parse (const char *filename, def_file *add_to)
  264. {
  265. struct directive *d;
  266. the_file = fopen (filename, "r");
  267. def_filename = filename;
  268. linenumber = 1;
  269. if (!the_file)
  270. {
  271. perror (filename);
  272. return 0;
  273. }
  274. if (add_to)
  275. {
  276. def = add_to;
  277. }
  278. else
  279. {
  280. def = def_file_empty ();
  281. }
  282. saw_newline = 1;
  283. if (def_parse ())
  284. {
  285. def_file_free (def);
  286. fclose (the_file);
  287. return 0;
  288. }
  289. fclose (the_file);
  290. for (d = directives; d; d = d->next)
  291. {
  292. #if TRACE
  293. printf ("Adding directive %08x `%s'\n", d->name, d->name);
  294. #endif
  295. def_file_add_directive (def, d->name, d->len);
  296. }
  297. return def;
  298. }
  299. void
  300. def_file_free (def_file *def)
  301. {
  302. int i;
  303. if (!def)
  304. return;
  305. if (def->name)
  306. free (def->name);
  307. if (def->description)
  308. free (def->description);
  309. if (def->section_defs)
  310. {
  311. for (i = 0; i < def->num_section_defs; i++)
  312. {
  313. if (def->section_defs[i].name)
  314. free (def->section_defs[i].name);
  315. if (def->section_defs[i].class)
  316. free (def->section_defs[i].class);
  317. }
  318. free (def->section_defs);
  319. }
  320. if (def->exports)
  321. {
  322. for (i = 0; i < def->num_exports; i++)
  323. {
  324. if (def->exports[i].internal_name
  325. && def->exports[i].internal_name != def->exports[i].name)
  326. free (def->exports[i].internal_name);
  327. if (def->exports[i].name)
  328. free (def->exports[i].name);
  329. }
  330. free (def->exports);
  331. }
  332. if (def->imports)
  333. {
  334. for (i = 0; i < def->num_imports; i++)
  335. {
  336. if (def->imports[i].internal_name
  337. && def->imports[i].internal_name != def->imports[i].name)
  338. free (def->imports[i].internal_name);
  339. if (def->imports[i].name)
  340. free (def->imports[i].name);
  341. }
  342. free (def->imports);
  343. }
  344. while (def->modules)
  345. {
  346. def_file_module *m = def->modules;
  347. def->modules = def->modules->next;
  348. free (m);
  349. }
  350. while (def->aligncomms)
  351. {
  352. def_file_aligncomm *c = def->aligncomms;
  353. def->aligncomms = def->aligncomms->next;
  354. free (c->symbol_name);
  355. free (c);
  356. }
  357. free (def);
  358. }
  359. #ifdef DEF_FILE_PRINT
  360. void
  361. def_file_print (FILE *file, def_file *def)
  362. {
  363. int i;
  364. fprintf (file, ">>>> def_file at 0x%08x\n", def);
  365. if (def->name)
  366. fprintf (file, " name: %s\n", def->name ? def->name : "(unspecified)");
  367. if (def->is_dll != -1)
  368. fprintf (file, " is dll: %s\n", def->is_dll ? "yes" : "no");
  369. if (def->base_address != (bfd_vma) -1)
  370. fprintf (file, " base address: 0x%08x\n", def->base_address);
  371. if (def->description)
  372. fprintf (file, " description: `%s'\n", def->description);
  373. if (def->stack_reserve != -1)
  374. fprintf (file, " stack reserve: 0x%08x\n", def->stack_reserve);
  375. if (def->stack_commit != -1)
  376. fprintf (file, " stack commit: 0x%08x\n", def->stack_commit);
  377. if (def->heap_reserve != -1)
  378. fprintf (file, " heap reserve: 0x%08x\n", def->heap_reserve);
  379. if (def->heap_commit != -1)
  380. fprintf (file, " heap commit: 0x%08x\n", def->heap_commit);
  381. if (def->num_section_defs > 0)
  382. {
  383. fprintf (file, " section defs:\n");
  384. for (i = 0; i < def->num_section_defs; i++)
  385. {
  386. fprintf (file, " name: `%s', class: `%s', flags:",
  387. def->section_defs[i].name, def->section_defs[i].class);
  388. if (def->section_defs[i].flag_read)
  389. fprintf (file, " R");
  390. if (def->section_defs[i].flag_write)
  391. fprintf (file, " W");
  392. if (def->section_defs[i].flag_execute)
  393. fprintf (file, " X");
  394. if (def->section_defs[i].flag_shared)
  395. fprintf (file, " S");
  396. fprintf (file, "\n");
  397. }
  398. }
  399. if (def->num_exports > 0)
  400. {
  401. fprintf (file, " exports:\n");
  402. for (i = 0; i < def->num_exports; i++)
  403. {
  404. fprintf (file, " name: `%s', int: `%s', ordinal: %d, flags:",
  405. def->exports[i].name, def->exports[i].internal_name,
  406. def->exports[i].ordinal);
  407. if (def->exports[i].flag_private)
  408. fprintf (file, " P");
  409. if (def->exports[i].flag_constant)
  410. fprintf (file, " C");
  411. if (def->exports[i].flag_noname)
  412. fprintf (file, " N");
  413. if (def->exports[i].flag_data)
  414. fprintf (file, " D");
  415. fprintf (file, "\n");
  416. }
  417. }
  418. if (def->num_imports > 0)
  419. {
  420. fprintf (file, " imports:\n");
  421. for (i = 0; i < def->num_imports; i++)
  422. {
  423. fprintf (file, " int: %s, from: `%s', name: `%s', ordinal: %d\n",
  424. def->imports[i].internal_name,
  425. def->imports[i].module,
  426. def->imports[i].name,
  427. def->imports[i].ordinal);
  428. }
  429. }
  430. if (def->version_major != -1)
  431. fprintf (file, " version: %d.%d\n", def->version_major, def->version_minor);
  432. fprintf (file, "<<<< def_file at 0x%08x\n", def);
  433. }
  434. #endif
  435. def_file_export *
  436. def_file_add_export (def_file *def,
  437. const char *external_name,
  438. const char *internal_name,
  439. int ordinal)
  440. {
  441. def_file_export *e;
  442. int max_exports = ROUND_UP(def->num_exports, 32);
  443. if (def->num_exports >= max_exports)
  444. {
  445. max_exports = ROUND_UP(def->num_exports + 1, 32);
  446. if (def->exports)
  447. def->exports = xrealloc (def->exports,
  448. max_exports * sizeof (def_file_export));
  449. else
  450. def->exports = xmalloc (max_exports * sizeof (def_file_export));
  451. }
  452. e = def->exports + def->num_exports;
  453. memset (e, 0, sizeof (def_file_export));
  454. if (internal_name && !external_name)
  455. external_name = internal_name;
  456. if (external_name && !internal_name)
  457. internal_name = external_name;
  458. e->name = xstrdup (external_name);
  459. e->internal_name = xstrdup (internal_name);
  460. e->ordinal = ordinal;
  461. def->num_exports++;
  462. return e;
  463. }
  464. def_file_module *
  465. def_get_module (def_file *def, const char *name)
  466. {
  467. def_file_module *s;
  468. for (s = def->modules; s; s = s->next)
  469. if (strcmp (s->name, name) == 0)
  470. return s;
  471. return NULL;
  472. }
  473. static def_file_module *
  474. def_stash_module (def_file *def, const char *name)
  475. {
  476. def_file_module *s;
  477. if ((s = def_get_module (def, name)) != NULL)
  478. return s;
  479. s = xmalloc (sizeof (def_file_module) + strlen (name));
  480. s->next = def->modules;
  481. def->modules = s;
  482. s->user_data = 0;
  483. strcpy (s->name, name);
  484. return s;
  485. }
  486. def_file_import *
  487. def_file_add_import (def_file *def,
  488. const char *name,
  489. const char *module,
  490. int ordinal,
  491. const char *internal_name)
  492. {
  493. def_file_import *i;
  494. int max_imports = ROUND_UP (def->num_imports, 16);
  495. if (def->num_imports >= max_imports)
  496. {
  497. max_imports = ROUND_UP (def->num_imports+1, 16);
  498. if (def->imports)
  499. def->imports = xrealloc (def->imports,
  500. max_imports * sizeof (def_file_import));
  501. else
  502. def->imports = xmalloc (max_imports * sizeof (def_file_import));
  503. }
  504. i = def->imports + def->num_imports;
  505. memset (i, 0, sizeof (def_file_import));
  506. if (name)
  507. i->name = xstrdup (name);
  508. if (module)
  509. i->module = def_stash_module (def, module);
  510. i->ordinal = ordinal;
  511. if (internal_name)
  512. i->internal_name = xstrdup (internal_name);
  513. else
  514. i->internal_name = i->name;
  515. def->num_imports++;
  516. return i;
  517. }
  518. struct
  519. {
  520. char *param;
  521. int token;
  522. }
  523. diropts[] =
  524. {
  525. { "-heap", HEAPSIZE },
  526. { "-stack", STACKSIZE_K },
  527. { "-attr", SECTIONS },
  528. { "-export", EXPORTS },
  529. { "-aligncomm", ALIGNCOMM },
  530. { 0, 0 }
  531. };
  532. void
  533. def_file_add_directive (def_file *my_def, const char *param, int len)
  534. {
  535. def_file *save_def = def;
  536. const char *pend = param + len;
  537. char * tend = (char *) param;
  538. int i;
  539. def = my_def;
  540. while (param < pend)
  541. {
  542. while (param < pend
  543. && (ISSPACE (*param) || *param == '\n' || *param == 0))
  544. param++;
  545. if (param == pend)
  546. break;
  547. /* Scan forward until we encounter any of:
  548. - the end of the buffer
  549. - the start of a new option
  550. - a newline seperating options
  551. - a NUL seperating options. */
  552. for (tend = (char *) (param + 1);
  553. (tend < pend
  554. && !(ISSPACE (tend[-1]) && *tend == '-')
  555. && *tend != '\n' && *tend != 0);
  556. tend++)
  557. ;
  558. for (i = 0; diropts[i].param; i++)
  559. {
  560. int len = strlen (diropts[i].param);
  561. if (tend - param >= len
  562. && strncmp (param, diropts[i].param, len) == 0
  563. && (param[len] == ':' || param[len] == ' '))
  564. {
  565. lex_parse_string_end = tend;
  566. lex_parse_string = param + len + 1;
  567. lex_forced_token = diropts[i].token;
  568. saw_newline = 0;
  569. if (def_parse ())
  570. continue;
  571. break;
  572. }
  573. }
  574. if (!diropts[i].param)
  575. {
  576. char saved;
  577. saved = * tend;
  578. * tend = 0;
  579. /* xgettext:c-format */
  580. einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
  581. * tend = saved;
  582. }
  583. lex_parse_string = 0;
  584. param = tend;
  585. }
  586. def = save_def;
  587. }
  588. /* Parser Callbacks. */
  589. static void
  590. def_image_name (const char *name, int base, int is_dll)
  591. {
  592. /* If a LIBRARY or NAME statement is specified without a name, there is nothing
  593. to do here. We retain the output filename specified on command line. */
  594. if (*name)
  595. {
  596. const char* image_name = lbasename (name);
  597. if (image_name != name)
  598. einfo ("%s:%d: Warning: path components stripped from %s, '%s'\n",
  599. def_filename, linenumber, is_dll ? "LIBRARY" : "NAME",
  600. name);
  601. if (def->name)
  602. free (def->name);
  603. /* Append the default suffix, if none specified. */
  604. if (strchr (image_name, '.') == 0)
  605. {
  606. const char * suffix = is_dll ? ".dll" : ".exe";
  607. def->name = xmalloc (strlen (image_name) + strlen (suffix) + 1);
  608. sprintf (def->name, "%s%s", image_name, suffix);
  609. }
  610. else
  611. def->name = xstrdup (image_name);
  612. }
  613. /* Honor a BASE address statement, even if LIBRARY string is empty. */
  614. def->base_address = base;
  615. def->is_dll = is_dll;
  616. }
  617. static void
  618. def_description (const char *text)
  619. {
  620. int len = def->description ? strlen (def->description) : 0;
  621. len += strlen (text) + 1;
  622. if (def->description)
  623. {
  624. def->description = xrealloc (def->description, len);
  625. strcat (def->description, text);
  626. }
  627. else
  628. {
  629. def->description = xmalloc (len);
  630. strcpy (def->description, text);
  631. }
  632. }
  633. static void
  634. def_stacksize (int reserve, int commit)
  635. {
  636. def->stack_reserve = reserve;
  637. def->stack_commit = commit;
  638. }
  639. static void
  640. def_heapsize (int reserve, int commit)
  641. {
  642. def->heap_reserve = reserve;
  643. def->heap_commit = commit;
  644. }
  645. static void
  646. def_section (const char *name, int attr)
  647. {
  648. def_file_section *s;
  649. int max_sections = ROUND_UP (def->num_section_defs, 4);
  650. if (def->num_section_defs >= max_sections)
  651. {
  652. max_sections = ROUND_UP (def->num_section_defs+1, 4);
  653. if (def->section_defs)
  654. def->section_defs = xrealloc (def->section_defs,
  655. max_sections * sizeof (def_file_import));
  656. else
  657. def->section_defs = xmalloc (max_sections * sizeof (def_file_import));
  658. }
  659. s = def->section_defs + def->num_section_defs;
  660. memset (s, 0, sizeof (def_file_section));
  661. s->name = xstrdup (name);
  662. if (attr & 1)
  663. s->flag_read = 1;
  664. if (attr & 2)
  665. s->flag_write = 1;
  666. if (attr & 4)
  667. s->flag_execute = 1;
  668. if (attr & 8)
  669. s->flag_shared = 1;
  670. def->num_section_defs++;
  671. }
  672. static void
  673. def_section_alt (const char *name, const char *attr)
  674. {
  675. int aval = 0;
  676. for (; *attr; attr++)
  677. {
  678. switch (*attr)
  679. {
  680. case 'R':
  681. case 'r':
  682. aval |= 1;
  683. break;
  684. case 'W':
  685. case 'w':
  686. aval |= 2;
  687. break;
  688. case 'X':
  689. case 'x':
  690. aval |= 4;
  691. break;
  692. case 'S':
  693. case 's':
  694. aval |= 8;
  695. break;
  696. }
  697. }
  698. def_section (name, aval);
  699. }
  700. static void
  701. def_exports (const char *external_name,
  702. const char *internal_name,
  703. int ordinal,
  704. int flags)
  705. {
  706. def_file_export *dfe;
  707. if (!internal_name && external_name)
  708. internal_name = external_name;
  709. #if TRACE
  710. printf ("def_exports, ext=%s int=%s\n", external_name, internal_name);
  711. #endif
  712. dfe = def_file_add_export (def, external_name, internal_name, ordinal);
  713. if (flags & 1)
  714. dfe->flag_noname = 1;
  715. if (flags & 2)
  716. dfe->flag_constant = 1;
  717. if (flags & 4)
  718. dfe->flag_data = 1;
  719. if (flags & 8)
  720. dfe->flag_private = 1;
  721. }
  722. static void
  723. def_import (const char *internal_name,
  724. const char *module,
  725. const char *dllext,
  726. const char *name,
  727. int ordinal)
  728. {
  729. char *buf = 0;
  730. const char *ext = dllext ? dllext : "dll";
  731. buf = xmalloc (strlen (module) + strlen (ext) + 2);
  732. sprintf (buf, "%s.%s", module, ext);
  733. module = buf;
  734. def_file_add_import (def, name, module, ordinal, internal_name);
  735. if (buf)
  736. free (buf);
  737. }
  738. static void
  739. def_version (int major, int minor)
  740. {
  741. def->version_major = major;
  742. def->version_minor = minor;
  743. }
  744. static void
  745. def_directive (char *str)
  746. {
  747. struct directive *d = xmalloc (sizeof (struct directive));
  748. d->next = directives;
  749. directives = d;
  750. d->name = xstrdup (str);
  751. d->len = strlen (str);
  752. }
  753. static void
  754. def_aligncomm (char *str, int align)
  755. {
  756. def_file_aligncomm *c = xmalloc (sizeof (def_file_aligncomm));
  757. c->symbol_name = xstrdup (str);
  758. c->alignment = (unsigned int) align;
  759. c->next = def->aligncomms;
  760. def->aligncomms = c;
  761. }
  762. static int
  763. def_error (const char *err)
  764. {
  765. einfo ("%P: %s:%d: %s\n",
  766. def_filename ? def_filename : "<unknown-file>", linenumber, err);
  767. return 0;
  768. }
  769. /* Lexical Scanner. */
  770. #undef TRACE
  771. #define TRACE 0
  772. /* Never freed, but always reused as needed, so no real leak. */
  773. static char *buffer = 0;
  774. static int buflen = 0;
  775. static int bufptr = 0;
  776. static void
  777. put_buf (char c)
  778. {
  779. if (bufptr == buflen)
  780. {
  781. buflen += 50; /* overly reasonable, eh? */
  782. if (buffer)
  783. buffer = xrealloc (buffer, buflen + 1);
  784. else
  785. buffer = xmalloc (buflen + 1);
  786. }
  787. buffer[bufptr++] = c;
  788. buffer[bufptr] = 0; /* not optimal, but very convenient. */
  789. }
  790. static struct
  791. {
  792. char *name;
  793. int token;
  794. }
  795. tokens[] =
  796. {
  797. { "BASE", BASE },
  798. { "CODE", CODE },
  799. { "CONSTANT", CONSTANTU },
  800. { "constant", CONSTANTL },
  801. { "DATA", DATAU },
  802. { "data", DATAL },
  803. { "DESCRIPTION", DESCRIPTION },
  804. { "DIRECTIVE", DIRECTIVE },
  805. { "EXECUTE", EXECUTE },
  806. { "EXPORTS", EXPORTS },
  807. { "HEAPSIZE", HEAPSIZE },
  808. { "IMPORTS", IMPORTS },
  809. { "LIBRARY", LIBRARY },
  810. { "NAME", NAME },
  811. { "NONAME", NONAMEU },
  812. { "noname", NONAMEL },
  813. { "PRIVATE", PRIVATEU },
  814. { "private", PRIVATEL },
  815. { "READ", READ },
  816. { "SECTIONS", SECTIONS },
  817. { "SEGMENTS", SECTIONS },
  818. { "SHARED", SHARED },
  819. { "STACKSIZE", STACKSIZE_K },
  820. { "VERSION", VERSIONK },
  821. { "WRITE", WRITE },
  822. { 0, 0 }
  823. };
  824. static int
  825. def_getc (void)
  826. {
  827. int rv;
  828. if (lex_parse_string)
  829. {
  830. if (lex_parse_string >= lex_parse_string_end)
  831. rv = EOF;
  832. else
  833. rv = *lex_parse_string++;
  834. }
  835. else
  836. {
  837. rv = fgetc (the_file);
  838. }
  839. if (rv == '\n')
  840. saw_newline = 1;
  841. return rv;
  842. }
  843. static int
  844. def_ungetc (int c)
  845. {
  846. if (lex_parse_string)
  847. {
  848. lex_parse_string--;
  849. return c;
  850. }
  851. else
  852. return ungetc (c, the_file);
  853. }
  854. static int
  855. def_lex (void)
  856. {
  857. int c, i, q;
  858. if (lex_forced_token)
  859. {
  860. i = lex_forced_token;
  861. lex_forced_token = 0;
  862. #if TRACE
  863. printf ("lex: forcing token %d\n", i);
  864. #endif
  865. return i;
  866. }
  867. c = def_getc ();
  868. /* Trim leading whitespace. */
  869. while (c != EOF && (c == ' ' || c == '\t') && saw_newline)
  870. c = def_getc ();
  871. if (c == EOF)
  872. {
  873. #if TRACE
  874. printf ("lex: EOF\n");
  875. #endif
  876. return 0;
  877. }
  878. if (saw_newline && c == ';')
  879. {
  880. do
  881. {
  882. c = def_getc ();
  883. }
  884. while (c != EOF && c != '\n');
  885. if (c == '\n')
  886. return def_lex ();
  887. return 0;
  888. }
  889. /* Must be something else. */
  890. saw_newline = 0;
  891. if (ISDIGIT (c))
  892. {
  893. bufptr = 0;
  894. while (c != EOF && (ISXDIGIT (c) || (c == 'x')))
  895. {
  896. put_buf (c);
  897. c = def_getc ();
  898. }
  899. if (c != EOF)
  900. def_ungetc (c);
  901. yylval.digits = xstrdup (buffer);
  902. #if TRACE
  903. printf ("lex: `%s' returns DIGITS\n", buffer);
  904. #endif
  905. return DIGITS;
  906. }
  907. if (ISALPHA (c) || strchr ("$:-_?@", c))
  908. {
  909. bufptr = 0;
  910. q = c;
  911. put_buf (c);
  912. c = def_getc ();
  913. if (q == '@')
  914. {
  915. if (ISBLANK (c) ) /* '@' followed by whitespace. */
  916. return (q);
  917. else if (ISDIGIT (c)) /* '@' followed by digit. */
  918. {
  919. def_ungetc (c);
  920. return (q);
  921. }
  922. #if TRACE
  923. printf ("lex: @ returns itself\n");
  924. #endif
  925. }
  926. while (c != EOF && (ISALNUM (c) || strchr ("$:-_?/@", c)))
  927. {
  928. put_buf (c);
  929. c = def_getc ();
  930. }
  931. if (c != EOF)
  932. def_ungetc (c);
  933. if (ISALPHA (q)) /* Check for tokens. */
  934. {
  935. for (i = 0; tokens[i].name; i++)
  936. if (strcmp (tokens[i].name, buffer) == 0)
  937. {
  938. #if TRACE
  939. printf ("lex: `%s' is a string token\n", buffer);
  940. #endif
  941. return tokens[i].token;
  942. }
  943. }
  944. #if TRACE
  945. printf ("lex: `%s' returns ID\n", buffer);
  946. #endif
  947. yylval.id = xstrdup (buffer);
  948. return ID;
  949. }
  950. if (c == '\'' || c == '"')
  951. {
  952. q = c;
  953. c = def_getc ();
  954. bufptr = 0;
  955. while (c != EOF && c != q)
  956. {
  957. put_buf (c);
  958. c = def_getc ();
  959. }
  960. yylval.id = xstrdup (buffer);
  961. #if TRACE
  962. printf ("lex: `%s' returns ID\n", buffer);
  963. #endif
  964. return ID;
  965. }
  966. if (c == '=' || c == '.' || c == ',')
  967. {
  968. #if TRACE
  969. printf ("lex: `%c' returns itself\n", c);
  970. #endif
  971. return c;
  972. }
  973. if (c == '\n')
  974. {
  975. linenumber++;
  976. saw_newline = 1;
  977. }
  978. /*printf ("lex: 0x%02x ignored\n", c); */
  979. return def_lex ();
  980. }