PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/opensource.apple.com/source/cxxfilt/cxxfilt-3/cxxfilt/ld/deffilep.y

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