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

/mingw-w64-v2.0.999/binutils/src/ld/deffilep.y

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