PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/osprey/cygnus/ld/deffilep.y

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