ldlex.l 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. %option nounput noyywrap
  2. %{
  3. /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
  4. Written by Steve Chamberlain of Cygnus Support.
  5. This file is part of the GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "bfd.h"
  19. #include "safe-ctype.h"
  20. #include "bfdlink.h"
  21. #include "ctf-api.h"
  22. #include "ld.h"
  23. #include "ldmisc.h"
  24. #include "ldexp.h"
  25. #include "ldlang.h"
  26. #include <ldgram.h>
  27. #include "ldfile.h"
  28. #include "ldlex.h"
  29. #include "ldmain.h"
  30. #include "libiberty.h"
  31. /* The type of top-level parser input.
  32. yylex and yyparse (indirectly) both check this. */
  33. input_type parser_input;
  34. /* Line number in the current input file. */
  35. unsigned int lineno;
  36. /* The string we are currently lexing, or NULL if we are reading a
  37. file. */
  38. const char *lex_string = NULL;
  39. /* Support for flex reading from more than one input file (stream).
  40. `include_stack' is flex's input state for each open file;
  41. `file_name_stack' is the file names. `lineno_stack' is the current
  42. line numbers.
  43. If `include_stack_ptr' is 0, we haven't started reading anything yet.
  44. Otherwise, stack elements 0 through `include_stack_ptr - 1' are valid. */
  45. #undef YY_INPUT
  46. #define YY_INPUT(buf,result,max_size) result = yy_input (buf, max_size)
  47. #ifndef YY_NO_UNPUT
  48. #define YY_NO_UNPUT
  49. #endif
  50. #define MAX_INCLUDE_DEPTH 10
  51. static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  52. static const char *file_name_stack[MAX_INCLUDE_DEPTH];
  53. static unsigned int lineno_stack[MAX_INCLUDE_DEPTH];
  54. static unsigned int sysrooted_stack[MAX_INCLUDE_DEPTH];
  55. static unsigned int include_stack_ptr = 0;
  56. static int vers_node_nesting = 0;
  57. static int yy_input (char *, int);
  58. static void comment (void);
  59. static void lex_warn_invalid (char *where, char *what);
  60. /* STATES
  61. EXPRESSION in an expression
  62. SCRIPT in a script
  63. INPUTLIST in a script, a filename-list
  64. MRI in an MRI script
  65. WILD inside the braces of an output section or overlay,
  66. for input section wildcards
  67. VERS_START starting a Sun style mapfile
  68. VERS_SCRIPT a Sun style mapfile
  69. VERS_NODE a node within a Sun style mapfile
  70. */
  71. #define RTOKEN(x) { yylval.token = x; return x; }
  72. %}
  73. %a 4000
  74. %o 5000
  75. WILDCHAR [_a-zA-Z0-9\/\.\\\$\~\-\+\:\[\]\,\=\?\*\^\!]
  76. FILENAMECHAR [_a-zA-Z0-9\/\.\\\$\~\-\+\:\[\]\,\=]
  77. NOCFILENAMECHAR [_a-zA-Z0-9\/\.\\\$\~\-\+\:\[\]]
  78. SYMBOLNAMECHAR [_a-zA-Z0-9\/\.\\\$\~]
  79. FILENAMECHAR1 [_a-zA-Z\/\.\\\$\~]
  80. SYMBOLNAMECHAR1 [_a-zA-Z\.\\\$]
  81. WHITE [ \t\n\r]+
  82. V_TAG [.$_a-zA-Z][._a-zA-Z0-9]*
  83. V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
  84. %s SCRIPT
  85. %s INPUTLIST
  86. %s EXPRESSION
  87. %s MRI
  88. %s WILD
  89. %s VERS_START
  90. %s VERS_SCRIPT
  91. %s VERS_NODE
  92. %%
  93. if (parser_input != input_selected)
  94. {
  95. /* The first token of the input determines the initial parser state. */
  96. input_type t = parser_input;
  97. parser_input = input_selected;
  98. switch (t)
  99. {
  100. case input_script: return INPUT_SCRIPT; break;
  101. case input_mri_script: return INPUT_MRI_SCRIPT; break;
  102. case input_version_script: return INPUT_VERSION_SCRIPT; break;
  103. case input_dynamic_list: return INPUT_DYNAMIC_LIST; break;
  104. case input_defsym: return INPUT_DEFSYM; break;
  105. default: abort ();
  106. }
  107. }
  108. <SCRIPT,EXPRESSION,VERS_START,VERS_NODE,VERS_SCRIPT,INPUTLIST>"/*" {
  109. comment (); }
  110. <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
  111. yylval.integer = bfd_scan_vma (yytext + 1, 0, 16);
  112. yylval.bigint.str = NULL;
  113. return INT;
  114. }
  115. <MRI,EXPRESSION>([0-9A-Fa-f])+(H|h|X|x|B|b|O|o|D|d) {
  116. int ibase ;
  117. switch (yytext[yyleng - 1]) {
  118. case 'X':
  119. case 'x':
  120. case 'H':
  121. case 'h':
  122. ibase = 16;
  123. break;
  124. case 'O':
  125. case 'o':
  126. ibase = 8;
  127. break;
  128. case 'B':
  129. case 'b':
  130. ibase = 2;
  131. break;
  132. default:
  133. ibase = 10;
  134. }
  135. yylval.integer = bfd_scan_vma (yytext, 0,
  136. ibase);
  137. yylval.bigint.str = NULL;
  138. return INT;
  139. }
  140. <SCRIPT,MRI,EXPRESSION>((("$"|0[xX])([0-9A-Fa-f])+)|(([0-9])+))(M|K|m|k)? {
  141. char *s = yytext;
  142. int ibase = 0;
  143. if (*s == '$')
  144. {
  145. ++s;
  146. ibase = 16;
  147. }
  148. yylval.integer = bfd_scan_vma (s, 0, ibase);
  149. yylval.bigint.str = NULL;
  150. if (yytext[yyleng - 1] == 'M'
  151. || yytext[yyleng - 1] == 'm')
  152. {
  153. yylval.integer *= 1024 * 1024;
  154. }
  155. else if (yytext[yyleng - 1] == 'K'
  156. || yytext[yyleng - 1]=='k')
  157. {
  158. yylval.integer *= 1024;
  159. }
  160. else if (yytext[0] == '0'
  161. && (yytext[1] == 'x'
  162. || yytext[1] == 'X'))
  163. {
  164. yylval.bigint.str = xstrdup (yytext + 2);
  165. }
  166. return INT;
  167. }
  168. /* Some tokens that only appear in expressions must be enabled for
  169. states other than EXPRESSION, since parser lookahead means they
  170. must be recognised before the parser switches the lexer out of
  171. SCRIPT or WILD state into EXPRESSION state.
  172. This sort of thing happens for example with NAME in ldgram.y
  173. "section" rule, which is immediately followed by ldlex_expression.
  174. However, if you follow the grammar from "sec_or_group_p1" you see
  175. "assignment" appearing in "statement_anywhere". Now,
  176. "assignment" also has NAME as its first token, just like
  177. "section". So the parser can't know whether it is in the
  178. "section" or the "assignment" rule until it has scanned the next
  179. token to find an assignment operator. Thus the next token after
  180. NAME in the "section" rule may be lexed before the lexer is
  181. switched to EXPRESSION state, and there are quite a number of
  182. optional components. The first token in all those components
  183. must be able to be lexed in SCRIPT state, as well as the
  184. assignment operators. In fact, due to "opt_exp_with_type",
  185. anything that can appear on the left hand side of "exp" might
  186. need to be lexed in SCRIPT state.
  187. MRI mode tends to cover everything in MRI scripts.
  188. */
  189. <MRI,WILD>"]" { RTOKEN(']'); }
  190. <MRI,WILD>"[" { RTOKEN('['); }
  191. <SCRIPT,EXPRESSION,MRI,WILD>"<<=" { RTOKEN(LSHIFTEQ); }
  192. <SCRIPT,EXPRESSION,MRI,WILD>">>=" { RTOKEN(RSHIFTEQ); }
  193. <EXPRESSION,MRI>"||" { RTOKEN(OROR); }
  194. <EXPRESSION,MRI>"==" { RTOKEN(EQ); }
  195. <EXPRESSION,MRI>"!=" { RTOKEN(NE); }
  196. <EXPRESSION,MRI>">=" { RTOKEN(GE); }
  197. <EXPRESSION,MRI>"<=" { RTOKEN(LE); }
  198. <EXPRESSION,MRI>"<<" { RTOKEN(LSHIFT); }
  199. <EXPRESSION,MRI>">>" { RTOKEN(RSHIFT); }
  200. <SCRIPT,EXPRESSION,MRI,WILD>"+=" { RTOKEN(PLUSEQ); }
  201. <SCRIPT,EXPRESSION,MRI,WILD>"-=" { RTOKEN(MINUSEQ); }
  202. <SCRIPT,EXPRESSION,MRI,WILD>"*=" { RTOKEN(MULTEQ); }
  203. <SCRIPT,EXPRESSION,MRI,WILD>"/=" { RTOKEN(DIVEQ); }
  204. <SCRIPT,EXPRESSION,MRI,WILD>"&=" { RTOKEN(ANDEQ); }
  205. <SCRIPT,EXPRESSION,MRI,WILD>"|=" { RTOKEN(OREQ); }
  206. <EXPRESSION,MRI>"&&" { RTOKEN(ANDAND); }
  207. <SCRIPT,EXPRESSION,MRI>">" { RTOKEN('>'); }
  208. <SCRIPT,EXPRESSION,MRI,INPUTLIST>"," { RTOKEN(','); }
  209. <EXPRESSION,MRI,WILD>"&" { RTOKEN('&'); }
  210. <EXPRESSION,MRI>"|" { RTOKEN('|'); }
  211. <SCRIPT,EXPRESSION,MRI>"~" { RTOKEN('~'); }
  212. <SCRIPT,EXPRESSION,MRI>"!" { RTOKEN('!'); }
  213. <EXPRESSION,MRI>"?" { RTOKEN('?'); }
  214. <EXPRESSION,MRI>"*" { RTOKEN('*'); }
  215. <SCRIPT,EXPRESSION,MRI>"+" { RTOKEN('+'); }
  216. <SCRIPT,EXPRESSION,MRI>"-" { RTOKEN('-'); }
  217. <EXPRESSION,MRI>"/" { RTOKEN('/'); }
  218. <EXPRESSION,MRI>"%" { RTOKEN('%'); }
  219. <EXPRESSION,MRI>"<" { RTOKEN('<'); }
  220. <SCRIPT,EXPRESSION,MRI,WILD>"=" { RTOKEN('='); }
  221. <SCRIPT,EXPRESSION,MRI,WILD>"}" { RTOKEN('}'); }
  222. <SCRIPT,EXPRESSION,MRI,WILD>"{" { RTOKEN('{'); }
  223. <SCRIPT,EXPRESSION,MRI,WILD,INPUTLIST>")" { RTOKEN(')'); }
  224. <SCRIPT,EXPRESSION,MRI,WILD,INPUTLIST>"(" { RTOKEN('('); }
  225. <SCRIPT,EXPRESSION,MRI>":" { RTOKEN(':'); }
  226. <SCRIPT,EXPRESSION,MRI,WILD>";" { RTOKEN(';'); }
  227. <SCRIPT>"MEMORY" { RTOKEN(MEMORY); }
  228. <SCRIPT>"REGION_ALIAS" { RTOKEN(REGION_ALIAS); }
  229. <SCRIPT>"LD_FEATURE" { RTOKEN(LD_FEATURE); }
  230. <SCRIPT,EXPRESSION>"ORIGIN" { RTOKEN(ORIGIN); }
  231. <SCRIPT>"VERSION" { RTOKEN(VERSIONK); }
  232. <SCRIPT,EXPRESSION>"BLOCK" { RTOKEN(BLOCK); }
  233. <SCRIPT,EXPRESSION>"BIND" { RTOKEN(BIND); }
  234. <SCRIPT,EXPRESSION>"LENGTH" { RTOKEN(LENGTH); }
  235. <SCRIPT,EXPRESSION>"ALIGN" { RTOKEN(ALIGN_K); }
  236. <SCRIPT,EXPRESSION>"DATA_SEGMENT_ALIGN" { RTOKEN(DATA_SEGMENT_ALIGN); }
  237. <SCRIPT,EXPRESSION>"DATA_SEGMENT_RELRO_END" { RTOKEN(DATA_SEGMENT_RELRO_END); }
  238. <SCRIPT,EXPRESSION>"DATA_SEGMENT_END" { RTOKEN(DATA_SEGMENT_END); }
  239. <SCRIPT,EXPRESSION>"ADDR" { RTOKEN(ADDR); }
  240. <SCRIPT,EXPRESSION>"LOADADDR" { RTOKEN(LOADADDR); }
  241. <SCRIPT,EXPRESSION>"ALIGNOF" { RTOKEN(ALIGNOF); }
  242. <SCRIPT,EXPRESSION>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
  243. <SCRIPT,EXPRESSION>"MAX" { RTOKEN(MAX_K); }
  244. <SCRIPT,EXPRESSION>"MIN" { RTOKEN(MIN_K); }
  245. <SCRIPT,EXPRESSION>"LOG2CEIL" { RTOKEN(LOG2CEIL); }
  246. <SCRIPT,EXPRESSION,WILD>"ASSERT" { RTOKEN(ASSERT_K); }
  247. <SCRIPT>"ENTRY" { RTOKEN(ENTRY); }
  248. <SCRIPT,MRI>"EXTERN" { RTOKEN(EXTERN); }
  249. <SCRIPT,EXPRESSION>"NEXT" { RTOKEN(NEXT); }
  250. <SCRIPT,EXPRESSION>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS); }
  251. <SCRIPT,EXPRESSION>"SEGMENT_START" { RTOKEN(SEGMENT_START); }
  252. <SCRIPT>"MAP" { RTOKEN(MAP); }
  253. <SCRIPT,EXPRESSION>"SIZEOF" { RTOKEN(SIZEOF); }
  254. <SCRIPT>"TARGET" { RTOKEN(TARGET_K); }
  255. <SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR); }
  256. <SCRIPT>"OUTPUT" { RTOKEN(OUTPUT); }
  257. <SCRIPT>"INPUT" { RTOKEN(INPUT); }
  258. <SCRIPT>"GROUP" { RTOKEN(GROUP); }
  259. <INPUTLIST>"AS_NEEDED" { RTOKEN(AS_NEEDED); }
  260. <SCRIPT,EXPRESSION>"DEFINED" { RTOKEN(DEFINED); }
  261. <WILD>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS); }
  262. <WILD>"CONSTRUCTORS" { RTOKEN(CONSTRUCTORS); }
  263. <SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION); }
  264. <SCRIPT>"FORCE_GROUP_ALLOCATION" { RTOKEN(FORCE_GROUP_ALLOCATION); }
  265. <SCRIPT>"INHIBIT_COMMON_ALLOCATION" { RTOKEN(INHIBIT_COMMON_ALLOCATION); }
  266. <SCRIPT>"SECTIONS" { RTOKEN(SECTIONS); }
  267. <SCRIPT>"INSERT" { RTOKEN(INSERT_K); }
  268. <SCRIPT>"AFTER" { RTOKEN(AFTER); }
  269. <SCRIPT>"BEFORE" { RTOKEN(BEFORE); }
  270. <WILD>"FILL" { RTOKEN(FILL); }
  271. <SCRIPT>"STARTUP" { RTOKEN(STARTUP); }
  272. <SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT); }
  273. <SCRIPT>"OUTPUT_ARCH" { RTOKEN(OUTPUT_ARCH); }
  274. <SCRIPT>"HLL" { RTOKEN(HLL); }
  275. <SCRIPT>"SYSLIB" { RTOKEN(SYSLIB); }
  276. <SCRIPT>"FLOAT" { RTOKEN(FLOAT); }
  277. <WILD>"QUAD" { RTOKEN(QUAD); }
  278. <WILD>"SQUAD" { RTOKEN(SQUAD); }
  279. <WILD>"LONG" { RTOKEN(LONG); }
  280. <WILD>"SHORT" { RTOKEN(SHORT); }
  281. <WILD>"BYTE" { RTOKEN(BYTE); }
  282. <SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT); }
  283. <SCRIPT,EXPRESSION>"NOCROSSREFS" { RTOKEN(NOCROSSREFS); }
  284. <SCRIPT,EXPRESSION>"NOCROSSREFS_TO" { RTOKEN(NOCROSSREFS_TO); }
  285. <SCRIPT,EXPRESSION>"OVERLAY" { RTOKEN(OVERLAY); }
  286. <WILD>"SORT_BY_NAME" { RTOKEN(SORT_BY_NAME); }
  287. <WILD>"SORT_BY_ALIGNMENT" { RTOKEN(SORT_BY_ALIGNMENT); }
  288. <WILD>"SORT" { RTOKEN(SORT_BY_NAME); }
  289. <WILD>"SORT_BY_INIT_PRIORITY" { RTOKEN(SORT_BY_INIT_PRIORITY); }
  290. <WILD>"SORT_NONE" { RTOKEN(SORT_NONE); }
  291. <EXPRESSION>"NOLOAD" { RTOKEN(NOLOAD); }
  292. <EXPRESSION>"READONLY" { RTOKEN(READONLY); }
  293. <EXPRESSION>"DSECT" { RTOKEN(DSECT); }
  294. <EXPRESSION>"COPY" { RTOKEN(COPY); }
  295. <EXPRESSION>"INFO" { RTOKEN(INFO); }
  296. <EXPRESSION>"TYPE" { RTOKEN(TYPE); }
  297. <SCRIPT,EXPRESSION>"ONLY_IF_RO" { RTOKEN(ONLY_IF_RO); }
  298. <SCRIPT,EXPRESSION>"ONLY_IF_RW" { RTOKEN(ONLY_IF_RW); }
  299. <SCRIPT,EXPRESSION>"SPECIAL" { RTOKEN(SPECIAL); }
  300. <SCRIPT>"o" { RTOKEN(ORIGIN); }
  301. <SCRIPT>"org" { RTOKEN(ORIGIN); }
  302. <SCRIPT>"l" { RTOKEN(LENGTH); }
  303. <SCRIPT>"len" { RTOKEN(LENGTH); }
  304. <WILD>"INPUT_SECTION_FLAGS" { RTOKEN(INPUT_SECTION_FLAGS); }
  305. <SCRIPT,EXPRESSION,WILD,MRI>"INCLUDE" { RTOKEN(INCLUDE);}
  306. <SCRIPT>"PHDRS" { RTOKEN(PHDRS); }
  307. <SCRIPT,EXPRESSION,WILD>"AT" { RTOKEN(AT);}
  308. <SCRIPT,EXPRESSION>"ALIGN_WITH_INPUT" { RTOKEN(ALIGN_WITH_INPUT);}
  309. <SCRIPT,EXPRESSION>"SUBALIGN" { RTOKEN(SUBALIGN);}
  310. <SCRIPT,EXPRESSION,WILD>"HIDDEN" { RTOKEN(HIDDEN); }
  311. <SCRIPT,EXPRESSION,WILD>"PROVIDE" { RTOKEN(PROVIDE); }
  312. <SCRIPT,EXPRESSION,WILD>"PROVIDE_HIDDEN" { RTOKEN(PROVIDE_HIDDEN); }
  313. <WILD>"KEEP" { RTOKEN(KEEP); }
  314. <WILD>"EXCLUDE_FILE" { RTOKEN(EXCLUDE_FILE); }
  315. <SCRIPT,EXPRESSION>"CONSTANT" { RTOKEN(CONSTANT);}
  316. <MRI>"#".*\n? { ++ lineno; }
  317. <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
  318. <MRI>"*".* { /* Mri comment line */ }
  319. <MRI>";".* { /* Mri comment line */ }
  320. <MRI>"END" { RTOKEN(ENDWORD); }
  321. <MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
  322. <MRI>"ALIGNMOD" { RTOKEN(ALIGNMOD);}
  323. <MRI>"ALIGN" { RTOKEN(ALIGN_K);}
  324. <MRI>"CHIP" { RTOKEN(CHIP); }
  325. <MRI>"BASE" { RTOKEN(BASE); }
  326. <MRI>"ALIAS" { RTOKEN(ALIAS); }
  327. <MRI>"TRUNCATE" { RTOKEN(TRUNCATE); }
  328. <MRI>"LOAD" { RTOKEN(LOAD); }
  329. <MRI>"PUBLIC" { RTOKEN(PUBLIC); }
  330. <MRI>"ORDER" { RTOKEN(ORDER); }
  331. <MRI>"NAME" { RTOKEN(NAMEWORD); }
  332. <MRI>"FORMAT" { RTOKEN(FORMAT); }
  333. <MRI>"CASE" { RTOKEN(CASE); }
  334. <MRI>"START" { RTOKEN(START); }
  335. <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
  336. <MRI>"SECT" { RTOKEN(SECT); }
  337. <MRI>"end" { RTOKEN(ENDWORD); }
  338. <MRI>"absolute" { RTOKEN(ABSOLUTE); }
  339. <MRI>"alignmod" { RTOKEN(ALIGNMOD);}
  340. <MRI>"align" { RTOKEN(ALIGN_K);}
  341. <MRI>"chip" { RTOKEN(CHIP); }
  342. <MRI>"base" { RTOKEN(BASE); }
  343. <MRI>"alias" { RTOKEN(ALIAS); }
  344. <MRI>"truncate" { RTOKEN(TRUNCATE); }
  345. <MRI>"load" { RTOKEN(LOAD); }
  346. <MRI>"public" { RTOKEN(PUBLIC); }
  347. <MRI>"order" { RTOKEN(ORDER); }
  348. <MRI>"name" { RTOKEN(NAMEWORD); }
  349. <MRI>"format" { RTOKEN(FORMAT); }
  350. <MRI>"case" { RTOKEN(CASE); }
  351. <MRI>"extern" { RTOKEN(EXTERN); }
  352. <MRI>"start" { RTOKEN(START); }
  353. <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
  354. <MRI>"sect" { RTOKEN(SECT); }
  355. <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}* {
  356. /* Filename without commas, needed to parse mri stuff */
  357. yylval.name = xstrdup (yytext);
  358. return NAME;
  359. }
  360. <SCRIPT,INPUTLIST>{FILENAMECHAR1}{FILENAMECHAR}* {
  361. yylval.name = xstrdup (yytext);
  362. return NAME;
  363. }
  364. <INPUTLIST>"="{FILENAMECHAR1}{FILENAMECHAR}* {
  365. /* Filename to be prefixed by --sysroot or when non-sysrooted, nothing. */
  366. yylval.name = xstrdup (yytext);
  367. return NAME;
  368. }
  369. <INPUTLIST>"-l"{FILENAMECHAR}+ {
  370. yylval.name = xstrdup (yytext + 2);
  371. return LNAME;
  372. }
  373. <EXPRESSION>{SYMBOLNAMECHAR1}{SYMBOLNAMECHAR}* {
  374. yylval.name = xstrdup (yytext);
  375. return NAME;
  376. }
  377. /* The following rule is to prevent a fill expression on the output
  378. section before /DISCARD/ interpreting the '/' as a divide. */
  379. <EXPRESSION>"/DISCARD/" {
  380. yylval.name = xstrdup (yytext);
  381. return NAME;
  382. }
  383. <WILD>{WILDCHAR}* {
  384. /* Annoyingly, this pattern can match comments, and we have
  385. longest match issues to consider. So if the first two
  386. characters are a comment opening, put the input back and
  387. try again. */
  388. if (yytext[0] == '/' && yytext[1] == '*')
  389. {
  390. yyless (2);
  391. comment ();
  392. }
  393. else
  394. {
  395. yylval.name = xstrdup (yytext);
  396. return NAME;
  397. }
  398. }
  399. <SCRIPT,EXPRESSION,WILD,VERS_NODE,INPUTLIST>"\""[^\"]*"\"" {
  400. /* No matter the state, quotes give what's inside. */
  401. yylval.name = xmemdup (yytext + 1, yyleng - 2, yyleng - 1);
  402. return NAME;
  403. }
  404. <SCRIPT,EXPRESSION,WILD,VERS_START,VERS_NODE,VERS_SCRIPT,INPUTLIST>"\n" {
  405. lineno++; }
  406. <MRI,SCRIPT,EXPRESSION,WILD,VERS_START,VERS_NODE,VERS_SCRIPT,INPUTLIST>[ \t\r]+ {
  407. /* Eat up whitespace */ }
  408. <SCRIPT,EXPRESSION,WILD,VERS_START,VERS_NODE,VERS_SCRIPT>#.* {
  409. /* Eat up comments */ }
  410. <VERS_NODE,VERS_SCRIPT>[:,;] { return *yytext; }
  411. <VERS_NODE>global { RTOKEN(GLOBAL); }
  412. <VERS_NODE>local { RTOKEN(LOCAL); }
  413. <VERS_NODE>extern { RTOKEN(EXTERN); }
  414. <VERS_NODE>{V_IDENTIFIER} { yylval.name = xstrdup (yytext);
  415. return VERS_IDENTIFIER; }
  416. <VERS_SCRIPT>{V_TAG} { yylval.name = xstrdup (yytext);
  417. return VERS_TAG; }
  418. <VERS_START>"{" { BEGIN(VERS_SCRIPT); return *yytext; }
  419. <VERS_SCRIPT>"{" { BEGIN(VERS_NODE);
  420. vers_node_nesting = 0;
  421. return *yytext;
  422. }
  423. <VERS_SCRIPT>"}" { return *yytext; }
  424. <VERS_NODE>"{" { vers_node_nesting++; return *yytext; }
  425. <VERS_NODE>"}" { if (--vers_node_nesting < 0)
  426. BEGIN(VERS_SCRIPT);
  427. return *yytext;
  428. }
  429. <<EOF>> {
  430. include_stack_ptr--;
  431. if (include_stack_ptr == 0)
  432. {
  433. lineno = 0;
  434. yyterminate ();
  435. }
  436. else
  437. yy_switch_to_buffer (include_stack[include_stack_ptr]);
  438. lineno = lineno_stack[include_stack_ptr];
  439. input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
  440. return END;
  441. }
  442. <SCRIPT,WILD,MRI,VERS_START,VERS_SCRIPT,VERS_NODE>. lex_warn_invalid (" in script", yytext);
  443. <EXPRESSION>. lex_warn_invalid (" in expression", yytext);
  444. %%
  445. /* Switch flex to reading script file NAME, open on FILE,
  446. saving the current input info on the include stack. */
  447. void
  448. lex_push_file (FILE *file, const char *name, unsigned int sysrooted)
  449. {
  450. if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
  451. {
  452. einfo (_("%F:includes nested too deeply\n"));
  453. }
  454. file_name_stack[include_stack_ptr] = name;
  455. lineno_stack[include_stack_ptr] = lineno;
  456. sysrooted_stack[include_stack_ptr] = input_flags.sysrooted;
  457. include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
  458. include_stack_ptr++;
  459. lineno = 1;
  460. input_flags.sysrooted = sysrooted;
  461. yyin = file;
  462. yy_switch_to_buffer (yy_create_buffer (yyin, YY_BUF_SIZE));
  463. }
  464. /* Return a newly created flex input buffer containing STRING,
  465. which is SIZE bytes long. */
  466. static YY_BUFFER_STATE
  467. yy_create_string_buffer (const char *string, size_t size)
  468. {
  469. YY_BUFFER_STATE b;
  470. b = xmalloc (sizeof (struct yy_buffer_state));
  471. b->yy_input_file = 0;
  472. b->yy_buf_size = size;
  473. /* yy_ch_buf has to be 2 characters longer than the size given because
  474. we need to put in 2 end-of-buffer characters. */
  475. b->yy_ch_buf = xmalloc ((size_t) b->yy_buf_size + 3);
  476. b->yy_ch_buf[0] = '\n';
  477. strcpy (b->yy_ch_buf+1, string);
  478. b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
  479. b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
  480. b->yy_n_chars = size+1;
  481. b->yy_buf_pos = &b->yy_ch_buf[1];
  482. b->yy_is_our_buffer = 1;
  483. b->yy_is_interactive = 0;
  484. b->yy_at_bol = 1;
  485. b->yy_fill_buffer = 0;
  486. /* flex 2.4.7 changed the interface. FIXME: We should not be using
  487. a flex internal interface in the first place! */
  488. #ifdef YY_BUFFER_NEW
  489. b->yy_buffer_status = YY_BUFFER_NEW;
  490. #else
  491. b->yy_eof_status = EOF_NOT_SEEN;
  492. #endif
  493. return b;
  494. }
  495. /* Switch flex to reading from STRING, saving the current input info
  496. on the include stack. */
  497. void
  498. lex_redirect (const char *string, const char *fake_filename, unsigned int count)
  499. {
  500. YY_BUFFER_STATE tmp;
  501. yy_init = 0;
  502. if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
  503. {
  504. einfo (_("%F: macros nested too deeply\n"));
  505. }
  506. file_name_stack[include_stack_ptr] = fake_filename;
  507. lineno_stack[include_stack_ptr] = lineno;
  508. include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
  509. include_stack_ptr++;
  510. lineno = count;
  511. tmp = yy_create_string_buffer (string, strlen (string));
  512. yy_switch_to_buffer (tmp);
  513. }
  514. /* Functions to switch to a different flex start condition,
  515. saving the current start condition on `state_stack'. */
  516. static int state_stack[MAX_INCLUDE_DEPTH * 2];
  517. static int *state_stack_p = state_stack;
  518. void
  519. ldlex_script (void)
  520. {
  521. *(state_stack_p)++ = yy_start;
  522. BEGIN (SCRIPT);
  523. }
  524. void
  525. ldlex_inputlist (void)
  526. {
  527. *(state_stack_p)++ = yy_start;
  528. BEGIN (INPUTLIST);
  529. }
  530. void
  531. ldlex_mri_script (void)
  532. {
  533. *(state_stack_p)++ = yy_start;
  534. BEGIN (MRI);
  535. }
  536. void
  537. ldlex_version_script (void)
  538. {
  539. *(state_stack_p)++ = yy_start;
  540. BEGIN (VERS_START);
  541. }
  542. void
  543. ldlex_version_file (void)
  544. {
  545. *(state_stack_p)++ = yy_start;
  546. BEGIN (VERS_SCRIPT);
  547. }
  548. void
  549. ldlex_expression (void)
  550. {
  551. *(state_stack_p)++ = yy_start;
  552. BEGIN (EXPRESSION);
  553. }
  554. void
  555. ldlex_wild (void)
  556. {
  557. *(state_stack_p)++ = yy_start;
  558. BEGIN (WILD);
  559. }
  560. void
  561. ldlex_popstate (void)
  562. {
  563. yy_start = *(--state_stack_p);
  564. }
  565. /* In cases where the parser needs to look ahead and the context
  566. changes from expression to script or vice-versa, throw away a
  567. NAME. What constitutes a NAME depends on context. */
  568. void
  569. ldlex_backup (void)
  570. {
  571. yyless (0);
  572. }
  573. /* Return the current file name, or the previous file if no file is
  574. current. */
  575. const char*
  576. ldlex_filename (void)
  577. {
  578. return file_name_stack[include_stack_ptr - (include_stack_ptr != 0)];
  579. }
  580. /* Place up to MAX_SIZE characters in BUF and return
  581. either the number of characters read, or 0 to indicate EOF. */
  582. static int
  583. yy_input (char *buf, int max_size)
  584. {
  585. int result = 0;
  586. if (YY_CURRENT_BUFFER->yy_input_file)
  587. {
  588. if (yyin)
  589. {
  590. result = fread (buf, 1, max_size, yyin);
  591. if (result < max_size && ferror (yyin))
  592. einfo (_("%F%P: read in flex scanner failed\n"));
  593. }
  594. }
  595. return result;
  596. }
  597. /* Eat the rest of a C-style comment. */
  598. static void
  599. comment (void)
  600. {
  601. int c;
  602. while (1)
  603. {
  604. c = input();
  605. while (c != '*' && c != 0)
  606. {
  607. if (c == '\n')
  608. lineno++;
  609. c = input();
  610. }
  611. if (c == '*')
  612. {
  613. c = input();
  614. while (c == '*')
  615. c = input();
  616. if (c == '/')
  617. break; /* found the end */
  618. }
  619. if (c == '\n')
  620. lineno++;
  621. if (c == 0)
  622. {
  623. einfo (_("%F%P: EOF in comment\n"));
  624. break;
  625. }
  626. }
  627. }
  628. /* Warn the user about a garbage character WHAT in the input
  629. in context WHERE. */
  630. static void
  631. lex_warn_invalid (char *where, char *what)
  632. {
  633. char buf[5];
  634. /* If we have found an input file whose format we do not recognize,
  635. and we are therefore treating it as a linker script, and we find
  636. an invalid character, then most likely this is a real object file
  637. of some different format. Treat it as such. */
  638. if (ldfile_assumed_script)
  639. {
  640. bfd_set_error (bfd_error_file_not_recognized);
  641. einfo (_("%F%s: file not recognized: %E\n"), ldlex_filename ());
  642. }
  643. if (! ISPRINT (*what))
  644. {
  645. sprintf (buf, "\\%03o", *(unsigned char *) what);
  646. what = buf;
  647. }
  648. einfo (_("%P:%pS: ignoring invalid character `%s'%s\n"), NULL, what, where);
  649. }