PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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