PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sh-elf-binutils-2.21/binutils-2.21/ld/deffilep.y

#
Happy | 1180 lines | 1035 code | 145 blank | 0 comment | 0 complexity | c77f4a015da4cfc89e8b0c1fe932c451 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0, GPL-3.0
  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, const char *);
  72. static void def_heapsize (int, int);
  73. static void def_import (const char *, const char *, const char *, const char *,
  74. int, const char *);
  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 EQUAL
  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. %type <id> opt_equalequal_name
  106. %%
  107. start: start command
  108. | command
  109. ;
  110. command:
  111. NAME opt_name opt_base { def_image_name ($2, $3, 0); }
  112. | LIBRARY opt_name opt_base { def_image_name ($2, $3, 1); }
  113. | DESCRIPTION ID { def_description ($2);}
  114. | STACKSIZE_K NUMBER opt_number { def_stacksize ($2, $3);}
  115. | HEAPSIZE NUMBER opt_number { def_heapsize ($2, $3);}
  116. | CODE attr_list { def_section ("CODE", $2);}
  117. | DATAU attr_list { def_section ("DATA", $2);}
  118. | SECTIONS seclist
  119. | EXPORTS explist
  120. | IMPORTS implist
  121. | VERSIONK NUMBER { def_version ($2, 0);}
  122. | VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
  123. | DIRECTIVE ID { def_directive ($2);}
  124. | ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
  125. ;
  126. explist:
  127. /* EMPTY */
  128. | expline
  129. | explist expline
  130. ;
  131. expline:
  132. /* The opt_comma is necessary to support both the usual
  133. DEF file syntax as well as .drectve syntax which
  134. mandates <expsym>,<expoptlist>. */
  135. dot_name opt_equal_name opt_ordinal opt_comma exp_opt_list opt_comma opt_equalequal_name
  136. { def_exports ($1, $2, $3, $5, $7); }
  137. ;
  138. exp_opt_list:
  139. /* The opt_comma is necessary to support both the usual
  140. DEF file syntax as well as .drectve syntax which
  141. allows for comma separated opt list. */
  142. exp_opt opt_comma exp_opt_list { $$ = $1 | $3; }
  143. | { $$ = 0; }
  144. ;
  145. exp_opt:
  146. NONAMEU { $$ = 1; }
  147. | NONAMEL { $$ = 1; }
  148. | CONSTANTU { $$ = 2; }
  149. | CONSTANTL { $$ = 2; }
  150. | DATAU { $$ = 4; }
  151. | DATAL { $$ = 4; }
  152. | PRIVATEU { $$ = 8; }
  153. | PRIVATEL { $$ = 8; }
  154. ;
  155. implist:
  156. implist impline
  157. | impline
  158. ;
  159. impline:
  160. ID '=' ID '.' ID '.' ID opt_equalequal_name
  161. { def_import ($1, $3, $5, $7, -1, $8); }
  162. | ID '=' ID '.' ID '.' NUMBER opt_equalequal_name
  163. { def_import ($1, $3, $5, 0, $7, $8); }
  164. | ID '=' ID '.' ID opt_equalequal_name
  165. { def_import ($1, $3, 0, $5, -1, $6); }
  166. | ID '=' ID '.' NUMBER opt_equalequal_name
  167. { def_import ($1, $3, 0, 0, $5, $6); }
  168. | ID '.' ID '.' ID opt_equalequal_name
  169. { def_import( 0, $1, $3, $5, -1, $6); }
  170. | ID '.' ID opt_equalequal_name
  171. { def_import ( 0, $1, 0, $3, -1, $4); }
  172. ;
  173. seclist:
  174. seclist secline
  175. | secline
  176. ;
  177. secline:
  178. ID attr_list { def_section ($1, $2);}
  179. | ID ID { def_section_alt ($1, $2);}
  180. ;
  181. attr_list:
  182. attr_list opt_comma attr { $$ = $1 | $3; }
  183. | attr { $$ = $1; }
  184. ;
  185. opt_comma:
  186. ','
  187. |
  188. ;
  189. opt_number: ',' NUMBER { $$=$2;}
  190. | { $$=-1;}
  191. ;
  192. attr:
  193. READ { $$ = 1;}
  194. | WRITE { $$ = 2;}
  195. | EXECUTE { $$=4;}
  196. | SHARED { $$=8;}
  197. ;
  198. opt_name: ID { $$ = $1; }
  199. | '.' ID
  200. {
  201. char *name = xmalloc (strlen ($2) + 2);
  202. sprintf (name, ".%s", $2);
  203. $$ = name;
  204. }
  205. | ID '.' ID
  206. {
  207. char *name = xmalloc (strlen ($1) + 1 + strlen ($3) + 1);
  208. sprintf (name, "%s.%s", $1, $3);
  209. $$ = name;
  210. }
  211. | { $$ = ""; }
  212. ;
  213. opt_equalequal_name: EQUAL ID { $$ = $2; }
  214. | { $$ = 0; }
  215. ;
  216. opt_ordinal:
  217. '@' NUMBER { $$ = $2;}
  218. | { $$ = -1;}
  219. ;
  220. opt_equal_name:
  221. '=' dot_name { $$ = $2; }
  222. | { $$ = 0; }
  223. ;
  224. opt_base: BASE '=' NUMBER { $$ = $3;}
  225. | { $$ = -1;}
  226. ;
  227. dot_name: ID { $$ = $1; }
  228. | '.' ID
  229. {
  230. char *name = xmalloc (strlen ($2) + 2);
  231. sprintf (name, ".%s", $2);
  232. $$ = name;
  233. }
  234. | dot_name '.' ID
  235. {
  236. char *name = xmalloc (strlen ($1) + 1 + strlen ($3) + 1);
  237. sprintf (name, "%s.%s", $1, $3);
  238. $$ = name;
  239. }
  240. ;
  241. anylang_id: ID { $$ = $1; }
  242. | '.' ID
  243. {
  244. char *id = xmalloc (strlen ($2) + 2);
  245. sprintf (id, ".%s", $2);
  246. $$ = id;
  247. }
  248. | anylang_id '.' opt_digits opt_id
  249. {
  250. char *id = xmalloc (strlen ($1) + 1 + strlen ($3) + strlen ($4) + 1);
  251. sprintf (id, "%s.%s%s", $1, $3, $4);
  252. $$ = id;
  253. }
  254. ;
  255. opt_digits: DIGITS { $$ = $1; }
  256. | { $$ = ""; }
  257. ;
  258. opt_id: ID { $$ = $1; }
  259. | { $$ = ""; }
  260. ;
  261. NUMBER: DIGITS { $$ = strtoul ($1, 0, 0); }
  262. %%
  263. /*****************************************************************************
  264. API
  265. *****************************************************************************/
  266. static FILE *the_file;
  267. static const char *def_filename;
  268. static int linenumber;
  269. static def_file *def;
  270. static int saw_newline;
  271. struct directive
  272. {
  273. struct directive *next;
  274. char *name;
  275. int len;
  276. };
  277. static struct directive *directives = 0;
  278. def_file *
  279. def_file_empty (void)
  280. {
  281. def_file *rv = xmalloc (sizeof (def_file));
  282. memset (rv, 0, sizeof (def_file));
  283. rv->is_dll = -1;
  284. rv->base_address = (bfd_vma) -1;
  285. rv->stack_reserve = rv->stack_commit = -1;
  286. rv->heap_reserve = rv->heap_commit = -1;
  287. rv->version_major = rv->version_minor = -1;
  288. return rv;
  289. }
  290. def_file *
  291. def_file_parse (const char *filename, def_file *add_to)
  292. {
  293. struct directive *d;
  294. the_file = fopen (filename, "r");
  295. def_filename = filename;
  296. linenumber = 1;
  297. if (!the_file)
  298. {
  299. perror (filename);
  300. return 0;
  301. }
  302. if (add_to)
  303. {
  304. def = add_to;
  305. }
  306. else
  307. {
  308. def = def_file_empty ();
  309. }
  310. saw_newline = 1;
  311. if (def_parse ())
  312. {
  313. def_file_free (def);
  314. fclose (the_file);
  315. return 0;
  316. }
  317. fclose (the_file);
  318. for (d = directives; d; d = d->next)
  319. {
  320. #if TRACE
  321. printf ("Adding directive %08x `%s'\n", d->name, d->name);
  322. #endif
  323. def_file_add_directive (def, d->name, d->len);
  324. }
  325. return def;
  326. }
  327. void
  328. def_file_free (def_file *fdef)
  329. {
  330. int i;
  331. if (!fdef)
  332. return;
  333. if (fdef->name)
  334. free (fdef->name);
  335. if (fdef->description)
  336. free (fdef->description);
  337. if (fdef->section_defs)
  338. {
  339. for (i = 0; i < fdef->num_section_defs; i++)
  340. {
  341. if (fdef->section_defs[i].name)
  342. free (fdef->section_defs[i].name);
  343. if (fdef->section_defs[i].class)
  344. free (fdef->section_defs[i].class);
  345. }
  346. free (fdef->section_defs);
  347. }
  348. if (fdef->exports)
  349. {
  350. for (i = 0; i < fdef->num_exports; i++)
  351. {
  352. if (fdef->exports[i].internal_name
  353. && fdef->exports[i].internal_name != fdef->exports[i].name)
  354. free (fdef->exports[i].internal_name);
  355. if (fdef->exports[i].name)
  356. free (fdef->exports[i].name);
  357. if (fdef->exports[i].its_name)
  358. free (fdef->exports[i].its_name);
  359. }
  360. free (fdef->exports);
  361. }
  362. if (fdef->imports)
  363. {
  364. for (i = 0; i < fdef->num_imports; i++)
  365. {
  366. if (fdef->imports[i].internal_name
  367. && fdef->imports[i].internal_name != fdef->imports[i].name)
  368. free (fdef->imports[i].internal_name);
  369. if (fdef->imports[i].name)
  370. free (fdef->imports[i].name);
  371. if (fdef->imports[i].its_name)
  372. free (fdef->imports[i].its_name);
  373. }
  374. free (fdef->imports);
  375. }
  376. while (fdef->modules)
  377. {
  378. def_file_module *m = fdef->modules;
  379. fdef->modules = fdef->modules->next;
  380. free (m);
  381. }
  382. while (fdef->aligncomms)
  383. {
  384. def_file_aligncomm *c = fdef->aligncomms;
  385. fdef->aligncomms = fdef->aligncomms->next;
  386. free (c->symbol_name);
  387. free (c);
  388. }
  389. free (fdef);
  390. }
  391. #ifdef DEF_FILE_PRINT
  392. void
  393. def_file_print (FILE *file, def_file *fdef)
  394. {
  395. int i;
  396. fprintf (file, ">>>> def_file at 0x%08x\n", fdef);
  397. if (fdef->name)
  398. fprintf (file, " name: %s\n", fdef->name ? fdef->name : "(unspecified)");
  399. if (fdef->is_dll != -1)
  400. fprintf (file, " is dll: %s\n", fdef->is_dll ? "yes" : "no");
  401. if (fdef->base_address != (bfd_vma) -1)
  402. fprintf (file, " base address: 0x%08x\n", fdef->base_address);
  403. if (fdef->description)
  404. fprintf (file, " description: `%s'\n", fdef->description);
  405. if (fdef->stack_reserve != -1)
  406. fprintf (file, " stack reserve: 0x%08x\n", fdef->stack_reserve);
  407. if (fdef->stack_commit != -1)
  408. fprintf (file, " stack commit: 0x%08x\n", fdef->stack_commit);
  409. if (fdef->heap_reserve != -1)
  410. fprintf (file, " heap reserve: 0x%08x\n", fdef->heap_reserve);
  411. if (fdef->heap_commit != -1)
  412. fprintf (file, " heap commit: 0x%08x\n", fdef->heap_commit);
  413. if (fdef->num_section_defs > 0)
  414. {
  415. fprintf (file, " section defs:\n");
  416. for (i = 0; i < fdef->num_section_defs; i++)
  417. {
  418. fprintf (file, " name: `%s', class: `%s', flags:",
  419. fdef->section_defs[i].name, fdef->section_defs[i].class);
  420. if (fdef->section_defs[i].flag_read)
  421. fprintf (file, " R");
  422. if (fdef->section_defs[i].flag_write)
  423. fprintf (file, " W");
  424. if (fdef->section_defs[i].flag_execute)
  425. fprintf (file, " X");
  426. if (fdef->section_defs[i].flag_shared)
  427. fprintf (file, " S");
  428. fprintf (file, "\n");
  429. }
  430. }
  431. if (fdef->num_exports > 0)
  432. {
  433. fprintf (file, " exports:\n");
  434. for (i = 0; i < fdef->num_exports; i++)
  435. {
  436. fprintf (file, " name: `%s', int: `%s', ordinal: %d, flags:",
  437. fdef->exports[i].name, fdef->exports[i].internal_name,
  438. fdef->exports[i].ordinal);
  439. if (fdef->exports[i].flag_private)
  440. fprintf (file, " P");
  441. if (fdef->exports[i].flag_constant)
  442. fprintf (file, " C");
  443. if (fdef->exports[i].flag_noname)
  444. fprintf (file, " N");
  445. if (fdef->exports[i].flag_data)
  446. fprintf (file, " D");
  447. fprintf (file, "\n");
  448. }
  449. }
  450. if (fdef->num_imports > 0)
  451. {
  452. fprintf (file, " imports:\n");
  453. for (i = 0; i < fdef->num_imports; i++)
  454. {
  455. fprintf (file, " int: %s, from: `%s', name: `%s', ordinal: %d\n",
  456. fdef->imports[i].internal_name,
  457. fdef->imports[i].module,
  458. fdef->imports[i].name,
  459. fdef->imports[i].ordinal);
  460. }
  461. }
  462. if (fdef->version_major != -1)
  463. fprintf (file, " version: %d.%d\n", fdef->version_major, fdef->version_minor);
  464. fprintf (file, "<<<< def_file at 0x%08x\n", fdef);
  465. }
  466. #endif
  467. def_file_export *
  468. def_file_add_export (def_file *fdef,
  469. const char *external_name,
  470. const char *internal_name,
  471. int ordinal,
  472. const char *its_name)
  473. {
  474. def_file_export *e;
  475. int max_exports = ROUND_UP(fdef->num_exports, 32);
  476. if (fdef->num_exports >= max_exports)
  477. {
  478. max_exports = ROUND_UP(fdef->num_exports + 1, 32);
  479. if (fdef->exports)
  480. fdef->exports = xrealloc (fdef->exports,
  481. max_exports * sizeof (def_file_export));
  482. else
  483. fdef->exports = xmalloc (max_exports * sizeof (def_file_export));
  484. }
  485. e = fdef->exports + fdef->num_exports;
  486. memset (e, 0, sizeof (def_file_export));
  487. if (internal_name && !external_name)
  488. external_name = internal_name;
  489. if (external_name && !internal_name)
  490. internal_name = external_name;
  491. e->name = xstrdup (external_name);
  492. e->internal_name = xstrdup (internal_name);
  493. e->its_name = (its_name ? xstrdup (its_name) : NULL);
  494. e->ordinal = ordinal;
  495. fdef->num_exports++;
  496. return e;
  497. }
  498. def_file_module *
  499. def_get_module (def_file *fdef, const char *name)
  500. {
  501. def_file_module *s;
  502. for (s = fdef->modules; s; s = s->next)
  503. if (strcmp (s->name, name) == 0)
  504. return s;
  505. return NULL;
  506. }
  507. static def_file_module *
  508. def_stash_module (def_file *fdef, const char *name)
  509. {
  510. def_file_module *s;
  511. if ((s = def_get_module (fdef, name)) != NULL)
  512. return s;
  513. s = xmalloc (sizeof (def_file_module) + strlen (name));
  514. s->next = fdef->modules;
  515. fdef->modules = s;
  516. s->user_data = 0;
  517. strcpy (s->name, name);
  518. return s;
  519. }
  520. def_file_import *
  521. def_file_add_import (def_file *fdef,
  522. const char *name,
  523. const char *module,
  524. int ordinal,
  525. const char *internal_name,
  526. const char *its_name)
  527. {
  528. def_file_import *i;
  529. int max_imports = ROUND_UP (fdef->num_imports, 16);
  530. if (fdef->num_imports >= max_imports)
  531. {
  532. max_imports = ROUND_UP (fdef->num_imports+1, 16);
  533. if (fdef->imports)
  534. fdef->imports = xrealloc (fdef->imports,
  535. max_imports * sizeof (def_file_import));
  536. else
  537. fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
  538. }
  539. i = fdef->imports + fdef->num_imports;
  540. memset (i, 0, sizeof (def_file_import));
  541. if (name)
  542. i->name = xstrdup (name);
  543. if (module)
  544. i->module = def_stash_module (fdef, module);
  545. i->ordinal = ordinal;
  546. if (internal_name)
  547. i->internal_name = xstrdup (internal_name);
  548. else
  549. i->internal_name = i->name;
  550. i->its_name = (its_name ? xstrdup (its_name) : NULL);
  551. fdef->num_imports++;
  552. return i;
  553. }
  554. struct
  555. {
  556. char *param;
  557. int token;
  558. }
  559. diropts[] =
  560. {
  561. { "-heap", HEAPSIZE },
  562. { "-stack", STACKSIZE_K },
  563. { "-attr", SECTIONS },
  564. { "-export", EXPORTS },
  565. { "-aligncomm", ALIGNCOMM },
  566. { 0, 0 }
  567. };
  568. void
  569. def_file_add_directive (def_file *my_def, const char *param, int len)
  570. {
  571. def_file *save_def = def;
  572. const char *pend = param + len;
  573. char * tend = (char *) param;
  574. int i;
  575. def = my_def;
  576. while (param < pend)
  577. {
  578. while (param < pend
  579. && (ISSPACE (*param) || *param == '\n' || *param == 0))
  580. param++;
  581. if (param == pend)
  582. break;
  583. /* Scan forward until we encounter any of:
  584. - the end of the buffer
  585. - the start of a new option
  586. - a newline seperating options
  587. - a NUL seperating options. */
  588. for (tend = (char *) (param + 1);
  589. (tend < pend
  590. && !(ISSPACE (tend[-1]) && *tend == '-')
  591. && *tend != '\n' && *tend != 0);
  592. tend++)
  593. ;
  594. for (i = 0; diropts[i].param; i++)
  595. {
  596. len = strlen (diropts[i].param);
  597. if (tend - param >= len
  598. && strncmp (param, diropts[i].param, len) == 0
  599. && (param[len] == ':' || param[len] == ' '))
  600. {
  601. lex_parse_string_end = tend;
  602. lex_parse_string = param + len + 1;
  603. lex_forced_token = diropts[i].token;
  604. saw_newline = 0;
  605. if (def_parse ())
  606. continue;
  607. break;
  608. }
  609. }
  610. if (!diropts[i].param)
  611. {
  612. char saved;
  613. saved = * tend;
  614. * tend = 0;
  615. /* xgettext:c-format */
  616. einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
  617. * tend = saved;
  618. }
  619. lex_parse_string = 0;
  620. param = tend;
  621. }
  622. def = save_def;
  623. }
  624. /* Parser Callbacks. */
  625. static void
  626. def_image_name (const char *name, int base, int is_dll)
  627. {
  628. /* If a LIBRARY or NAME statement is specified without a name, there is nothing
  629. to do here. We retain the output filename specified on command line. */
  630. if (*name)
  631. {
  632. const char* image_name = lbasename (name);
  633. if (image_name != name)
  634. einfo ("%s:%d: Warning: path components stripped from %s, '%s'\n",
  635. def_filename, linenumber, is_dll ? "LIBRARY" : "NAME",
  636. name);
  637. if (def->name)
  638. free (def->name);
  639. /* Append the default suffix, if none specified. */
  640. if (strchr (image_name, '.') == 0)
  641. {
  642. const char * suffix = is_dll ? ".dll" : ".exe";
  643. def->name = xmalloc (strlen (image_name) + strlen (suffix) + 1);
  644. sprintf (def->name, "%s%s", image_name, suffix);
  645. }
  646. else
  647. def->name = xstrdup (image_name);
  648. }
  649. /* Honor a BASE address statement, even if LIBRARY string is empty. */
  650. def->base_address = base;
  651. def->is_dll = is_dll;
  652. }
  653. static void
  654. def_description (const char *text)
  655. {
  656. int len = def->description ? strlen (def->description) : 0;
  657. len += strlen (text) + 1;
  658. if (def->description)
  659. {
  660. def->description = xrealloc (def->description, len);
  661. strcat (def->description, text);
  662. }
  663. else
  664. {
  665. def->description = xmalloc (len);
  666. strcpy (def->description, text);
  667. }
  668. }
  669. static void
  670. def_stacksize (int reserve, int commit)
  671. {
  672. def->stack_reserve = reserve;
  673. def->stack_commit = commit;
  674. }
  675. static void
  676. def_heapsize (int reserve, int commit)
  677. {
  678. def->heap_reserve = reserve;
  679. def->heap_commit = commit;
  680. }
  681. static void
  682. def_section (const char *name, int attr)
  683. {
  684. def_file_section *s;
  685. int max_sections = ROUND_UP (def->num_section_defs, 4);
  686. if (def->num_section_defs >= max_sections)
  687. {
  688. max_sections = ROUND_UP (def->num_section_defs+1, 4);
  689. if (def->section_defs)
  690. def->section_defs = xrealloc (def->section_defs,
  691. max_sections * sizeof (def_file_import));
  692. else
  693. def->section_defs = xmalloc (max_sections * sizeof (def_file_import));
  694. }
  695. s = def->section_defs + def->num_section_defs;
  696. memset (s, 0, sizeof (def_file_section));
  697. s->name = xstrdup (name);
  698. if (attr & 1)
  699. s->flag_read = 1;
  700. if (attr & 2)
  701. s->flag_write = 1;
  702. if (attr & 4)
  703. s->flag_execute = 1;
  704. if (attr & 8)
  705. s->flag_shared = 1;
  706. def->num_section_defs++;
  707. }
  708. static void
  709. def_section_alt (const char *name, const char *attr)
  710. {
  711. int aval = 0;
  712. for (; *attr; attr++)
  713. {
  714. switch (*attr)
  715. {
  716. case 'R':
  717. case 'r':
  718. aval |= 1;
  719. break;
  720. case 'W':
  721. case 'w':
  722. aval |= 2;
  723. break;
  724. case 'X':
  725. case 'x':
  726. aval |= 4;
  727. break;
  728. case 'S':
  729. case 's':
  730. aval |= 8;
  731. break;
  732. }
  733. }
  734. def_section (name, aval);
  735. }
  736. static void
  737. def_exports (const char *external_name,
  738. const char *internal_name,
  739. int ordinal,
  740. int flags,
  741. const char *its_name)
  742. {
  743. def_file_export *dfe;
  744. if (!internal_name && external_name)
  745. internal_name = external_name;
  746. #if TRACE
  747. printf ("def_exports, ext=%s int=%s\n", external_name, internal_name);
  748. #endif
  749. dfe = def_file_add_export (def, external_name, internal_name, ordinal,
  750. its_name);
  751. if (flags & 1)
  752. dfe->flag_noname = 1;
  753. if (flags & 2)
  754. dfe->flag_constant = 1;
  755. if (flags & 4)
  756. dfe->flag_data = 1;
  757. if (flags & 8)
  758. dfe->flag_private = 1;
  759. }
  760. static void
  761. def_import (const char *internal_name,
  762. const char *module,
  763. const char *dllext,
  764. const char *name,
  765. int ordinal,
  766. const char *its_name)
  767. {
  768. char *buf = 0;
  769. const char *ext = dllext ? dllext : "dll";
  770. buf = xmalloc (strlen (module) + strlen (ext) + 2);
  771. sprintf (buf, "%s.%s", module, ext);
  772. module = buf;
  773. def_file_add_import (def, name, module, ordinal, internal_name, its_name);
  774. if (buf)
  775. free (buf);
  776. }
  777. static void
  778. def_version (int major, int minor)
  779. {
  780. def->version_major = major;
  781. def->version_minor = minor;
  782. }
  783. static void
  784. def_directive (char *str)
  785. {
  786. struct directive *d = xmalloc (sizeof (struct directive));
  787. d->next = directives;
  788. directives = d;
  789. d->name = xstrdup (str);
  790. d->len = strlen (str);
  791. }
  792. static void
  793. def_aligncomm (char *str, int align)
  794. {
  795. def_file_aligncomm *c = xmalloc (sizeof (def_file_aligncomm));
  796. c->symbol_name = xstrdup (str);
  797. c->alignment = (unsigned int) align;
  798. c->next = def->aligncomms;
  799. def->aligncomms = c;
  800. }
  801. static int
  802. def_error (const char *err)
  803. {
  804. einfo ("%P: %s:%d: %s\n",
  805. def_filename ? def_filename : "<unknown-file>", linenumber, err);
  806. return 0;
  807. }
  808. /* Lexical Scanner. */
  809. #undef TRACE
  810. #define TRACE 0
  811. /* Never freed, but always reused as needed, so no real leak. */
  812. static char *buffer = 0;
  813. static int buflen = 0;
  814. static int bufptr = 0;
  815. static void
  816. put_buf (char c)
  817. {
  818. if (bufptr == buflen)
  819. {
  820. buflen += 50; /* overly reasonable, eh? */
  821. if (buffer)
  822. buffer = xrealloc (buffer, buflen + 1);
  823. else
  824. buffer = xmalloc (buflen + 1);
  825. }
  826. buffer[bufptr++] = c;
  827. buffer[bufptr] = 0; /* not optimal, but very convenient. */
  828. }
  829. static struct
  830. {
  831. char *name;
  832. int token;
  833. }
  834. tokens[] =
  835. {
  836. { "BASE", BASE },
  837. { "CODE", CODE },
  838. { "CONSTANT", CONSTANTU },
  839. { "constant", CONSTANTL },
  840. { "DATA", DATAU },
  841. { "data", DATAL },
  842. { "DESCRIPTION", DESCRIPTION },
  843. { "DIRECTIVE", DIRECTIVE },
  844. { "EXECUTE", EXECUTE },
  845. { "EXPORTS", EXPORTS },
  846. { "HEAPSIZE", HEAPSIZE },
  847. { "IMPORTS", IMPORTS },
  848. { "LIBRARY", LIBRARY },
  849. { "NAME", NAME },
  850. { "NONAME", NONAMEU },
  851. { "noname", NONAMEL },
  852. { "PRIVATE", PRIVATEU },
  853. { "private", PRIVATEL },
  854. { "READ", READ },
  855. { "SECTIONS", SECTIONS },
  856. { "SEGMENTS", SECTIONS },
  857. { "SHARED", SHARED },
  858. { "STACKSIZE", STACKSIZE_K },
  859. { "VERSION", VERSIONK },
  860. { "WRITE", WRITE },
  861. { 0, 0 }
  862. };
  863. static int
  864. def_getc (void)
  865. {
  866. int rv;
  867. if (lex_parse_string)
  868. {
  869. if (lex_parse_string >= lex_parse_string_end)
  870. rv = EOF;
  871. else
  872. rv = *lex_parse_string++;
  873. }
  874. else
  875. {
  876. rv = fgetc (the_file);
  877. }
  878. if (rv == '\n')
  879. saw_newline = 1;
  880. return rv;
  881. }
  882. static int
  883. def_ungetc (int c)
  884. {
  885. if (lex_parse_string)
  886. {
  887. lex_parse_string--;
  888. return c;
  889. }
  890. else
  891. return ungetc (c, the_file);
  892. }
  893. static int
  894. def_lex (void)
  895. {
  896. int c, i, q;
  897. if (lex_forced_token)
  898. {
  899. i = lex_forced_token;
  900. lex_forced_token = 0;
  901. #if TRACE
  902. printf ("lex: forcing token %d\n", i);
  903. #endif
  904. return i;
  905. }
  906. c = def_getc ();
  907. /* Trim leading whitespace. */
  908. while (c != EOF && (c == ' ' || c == '\t') && saw_newline)
  909. c = def_getc ();
  910. if (c == EOF)
  911. {
  912. #if TRACE
  913. printf ("lex: EOF\n");
  914. #endif
  915. return 0;
  916. }
  917. if (saw_newline && c == ';')
  918. {
  919. do
  920. {
  921. c = def_getc ();
  922. }
  923. while (c != EOF && c != '\n');
  924. if (c == '\n')
  925. return def_lex ();
  926. return 0;
  927. }
  928. /* Must be something else. */
  929. saw_newline = 0;
  930. if (ISDIGIT (c))
  931. {
  932. bufptr = 0;
  933. while (c != EOF && (ISXDIGIT (c) || (c == 'x')))
  934. {
  935. put_buf (c);
  936. c = def_getc ();
  937. }
  938. if (c != EOF)
  939. def_ungetc (c);
  940. yylval.digits = xstrdup (buffer);
  941. #if TRACE
  942. printf ("lex: `%s' returns DIGITS\n", buffer);
  943. #endif
  944. return DIGITS;
  945. }
  946. if (ISALPHA (c) || strchr ("$:-_?@", c))
  947. {
  948. bufptr = 0;
  949. q = c;
  950. put_buf (c);
  951. c = def_getc ();
  952. if (q == '@')
  953. {
  954. if (ISBLANK (c) ) /* '@' followed by whitespace. */
  955. return (q);
  956. else if (ISDIGIT (c)) /* '@' followed by digit. */
  957. {
  958. def_ungetc (c);
  959. return (q);
  960. }
  961. #if TRACE
  962. printf ("lex: @ returns itself\n");
  963. #endif
  964. }
  965. while (c != EOF && (ISALNUM (c) || strchr ("$:-_?/@<>", c)))
  966. {
  967. put_buf (c);
  968. c = def_getc ();
  969. }
  970. if (c != EOF)
  971. def_ungetc (c);
  972. if (ISALPHA (q)) /* Check for tokens. */
  973. {
  974. for (i = 0; tokens[i].name; i++)
  975. if (strcmp (tokens[i].name, buffer) == 0)
  976. {
  977. #if TRACE
  978. printf ("lex: `%s' is a string token\n", buffer);
  979. #endif
  980. return tokens[i].token;
  981. }
  982. }
  983. #if TRACE
  984. printf ("lex: `%s' returns ID\n", buffer);
  985. #endif
  986. yylval.id = xstrdup (buffer);
  987. return ID;
  988. }
  989. if (c == '\'' || c == '"')
  990. {
  991. q = c;
  992. c = def_getc ();
  993. bufptr = 0;
  994. while (c != EOF && c != q)
  995. {
  996. put_buf (c);
  997. c = def_getc ();
  998. }
  999. yylval.id = xstrdup (buffer);
  1000. #if TRACE
  1001. printf ("lex: `%s' returns ID\n", buffer);
  1002. #endif
  1003. return ID;
  1004. }
  1005. if ( c == '=')
  1006. {
  1007. c = def_getc ();
  1008. if (c == '=')
  1009. {
  1010. #if TRACE
  1011. printf ("lex: `==' returns EQUAL\n");
  1012. #endif
  1013. return EQUAL;
  1014. }
  1015. def_ungetc (c);
  1016. #if TRACE
  1017. printf ("lex: `=' returns itself\n");
  1018. #endif
  1019. return '=';
  1020. }
  1021. if (c == '.' || c == ',')
  1022. {
  1023. #if TRACE
  1024. printf ("lex: `%c' returns itself\n", c);
  1025. #endif
  1026. return c;
  1027. }
  1028. if (c == '\n')
  1029. {
  1030. linenumber++;
  1031. saw_newline = 1;
  1032. }
  1033. /*printf ("lex: 0x%02x ignored\n", c); */
  1034. return def_lex ();
  1035. }