yyscript.y 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /* yyscript.y -- linker script grammar for gold. */
  2. /* Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <iant@google.com>.
  4. This file is part of gold.
  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. /* This is a bison grammar to parse a subset of the original GNU ld
  18. linker script language. */
  19. %{
  20. #include "config.h"
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "script-c.h"
  26. %}
  27. /* We need to use a pure parser because we might be multi-threaded.
  28. We pass some arguments through the parser to the lexer. */
  29. %pure-parser
  30. %parse-param {void* closure}
  31. %lex-param {void* closure}
  32. /* Since we require bison anyhow, we take advantage of it. */
  33. %error-verbose
  34. /* The values associated with tokens. */
  35. %union {
  36. /* A string. */
  37. struct Parser_string string;
  38. /* A number. */
  39. uint64_t integer;
  40. /* An expression. */
  41. Expression_ptr expr;
  42. /* An output section header. */
  43. struct Parser_output_section_header output_section_header;
  44. /* An output section trailer. */
  45. struct Parser_output_section_trailer output_section_trailer;
  46. /* A section constraint. */
  47. enum Section_constraint constraint;
  48. /* A complete input section specification. */
  49. struct Input_section_spec input_section_spec;
  50. /* A list of wildcard specifications, with exclusions. */
  51. struct Wildcard_sections wildcard_sections;
  52. /* A single wildcard specification. */
  53. struct Wildcard_section wildcard_section;
  54. /* A list of strings. */
  55. String_list_ptr string_list;
  56. /* Information for a program header. */
  57. struct Phdr_info phdr_info;
  58. /* Used for version scripts and within VERSION {}. */
  59. struct Version_dependency_list* deplist;
  60. struct Version_expression_list* versyms;
  61. struct Version_tree* versnode;
  62. enum Script_section_type section_type;
  63. }
  64. /* Operators, including a precedence table for expressions. */
  65. %right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
  66. %right '?' ':'
  67. %left OROR
  68. %left ANDAND
  69. %left '|'
  70. %left '^'
  71. %left '&'
  72. %left EQ NE
  73. %left '<' '>' LE GE
  74. %left LSHIFT RSHIFT
  75. %left '+' '-'
  76. %left '*' '/' '%'
  77. /* A fake operator used to indicate unary operator precedence. */
  78. %right UNARY
  79. /* Constants. */
  80. %token <string> STRING
  81. %token <string> QUOTED_STRING
  82. %token <integer> INTEGER
  83. /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
  84. GNU linker, with the keywords which only appear in MRI mode
  85. removed. Not all these keywords are actually used in this grammar.
  86. In most cases the keyword is recognized as the token name in upper
  87. case. The comments indicate where this is not the case. */
  88. %token ABSOLUTE
  89. %token ADDR
  90. %token ALIGN_K /* ALIGN */
  91. %token ALIGNOF
  92. %token ASSERT_K /* ASSERT */
  93. %token AS_NEEDED
  94. %token AT
  95. %token BIND
  96. %token BLOCK
  97. %token BYTE
  98. %token CONSTANT
  99. %token CONSTRUCTORS
  100. %token COPY
  101. %token CREATE_OBJECT_SYMBOLS
  102. %token DATA_SEGMENT_ALIGN
  103. %token DATA_SEGMENT_END
  104. %token DATA_SEGMENT_RELRO_END
  105. %token DEFINED
  106. %token DSECT
  107. %token ENTRY
  108. %token EXCLUDE_FILE
  109. %token EXTERN
  110. %token FILL
  111. %token FLOAT
  112. %token FORCE_COMMON_ALLOCATION
  113. %token GLOBAL /* global */
  114. %token GROUP
  115. %token HLL
  116. %token INCLUDE
  117. %token INHIBIT_COMMON_ALLOCATION
  118. %token INFO
  119. %token INPUT
  120. %token KEEP
  121. %token LEN
  122. %token LENGTH /* LENGTH, l, len */
  123. %token LOADADDR
  124. %token LOCAL /* local */
  125. %token LONG
  126. %token MAP
  127. %token MAX_K /* MAX */
  128. %token MEMORY
  129. %token MIN_K /* MIN */
  130. %token NEXT
  131. %token NOCROSSREFS
  132. %token NOFLOAT
  133. %token NOLOAD
  134. %token ONLY_IF_RO
  135. %token ONLY_IF_RW
  136. %token ORG
  137. %token ORIGIN /* ORIGIN, o, org */
  138. %token OUTPUT
  139. %token OUTPUT_ARCH
  140. %token OUTPUT_FORMAT
  141. %token OVERLAY
  142. %token PHDRS
  143. %token PROVIDE
  144. %token PROVIDE_HIDDEN
  145. %token QUAD
  146. %token SEARCH_DIR
  147. %token SECTIONS
  148. %token SEGMENT_START
  149. %token SHORT
  150. %token SIZEOF
  151. %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
  152. %token SORT_BY_ALIGNMENT
  153. %token SORT_BY_NAME
  154. %token SPECIAL
  155. %token SQUAD
  156. %token STARTUP
  157. %token SUBALIGN
  158. %token SYSLIB
  159. %token TARGET_K /* TARGET */
  160. %token TRUNCATE
  161. %token VERSIONK /* VERSION */
  162. /* Keywords, part 2. These are keywords that are unique to gold,
  163. and not present in the old GNU linker. As before, unless the
  164. comments say otherwise, the keyword is recognized as the token
  165. name in upper case. */
  166. %token OPTION
  167. /* Special tokens used to tell the grammar what type of tokens we are
  168. parsing. The token stream always begins with one of these tokens.
  169. We do this because version scripts can appear embedded within
  170. linker scripts, and because --defsym uses the expression
  171. parser. */
  172. %token PARSING_LINKER_SCRIPT
  173. %token PARSING_VERSION_SCRIPT
  174. %token PARSING_DEFSYM
  175. %token PARSING_DYNAMIC_LIST
  176. %token PARSING_SECTIONS_BLOCK
  177. %token PARSING_SECTION_COMMANDS
  178. %token PARSING_MEMORY_DEF
  179. /* Non-terminal types, where needed. */
  180. %type <expr> parse_exp exp
  181. %type <expr> opt_at opt_align opt_subalign opt_fill
  182. %type <output_section_header> section_header opt_address_and_section_type
  183. %type <section_type> section_type
  184. %type <output_section_trailer> section_trailer
  185. %type <constraint> opt_constraint
  186. %type <string_list> opt_phdr
  187. %type <integer> data_length
  188. %type <input_section_spec> input_section_no_keep
  189. %type <wildcard_sections> wildcard_sections
  190. %type <wildcard_section> wildcard_file wildcard_section
  191. %type <string_list> exclude_names
  192. %type <string> wildcard_name
  193. %type <integer> phdr_type memory_attr
  194. %type <phdr_info> phdr_info
  195. %type <versyms> vers_defns
  196. %type <versnode> vers_tag
  197. %type <deplist> verdep
  198. %type <string> string
  199. %%
  200. /* Read the special token to see what to read next. */
  201. top:
  202. PARSING_LINKER_SCRIPT linker_script
  203. | PARSING_VERSION_SCRIPT version_script
  204. | PARSING_DEFSYM defsym_expr
  205. | PARSING_DYNAMIC_LIST dynamic_list_expr
  206. | PARSING_SECTIONS_BLOCK sections_block
  207. | PARSING_SECTION_COMMANDS section_cmds
  208. | PARSING_MEMORY_DEF memory_defs
  209. ;
  210. /* A file contains a list of commands. */
  211. linker_script:
  212. linker_script file_cmd
  213. | /* empty */
  214. ;
  215. /* A command which may appear at top level of a linker script. */
  216. file_cmd:
  217. EXTERN '(' extern_name_list ')'
  218. | FORCE_COMMON_ALLOCATION
  219. { script_set_common_allocation(closure, 1); }
  220. | GROUP
  221. { script_start_group(closure); }
  222. '(' input_list ')'
  223. { script_end_group(closure); }
  224. | INHIBIT_COMMON_ALLOCATION
  225. { script_set_common_allocation(closure, 0); }
  226. | INPUT '(' input_list ')'
  227. | MEMORY '{' memory_defs '}'
  228. | OPTION '(' string ')'
  229. { script_parse_option(closure, $3.value, $3.length); }
  230. | OUTPUT_FORMAT '(' string ')'
  231. {
  232. if (!script_check_output_format(closure, $3.value, $3.length,
  233. NULL, 0, NULL, 0))
  234. YYABORT;
  235. }
  236. | OUTPUT_FORMAT '(' string ',' string ',' string ')'
  237. {
  238. if (!script_check_output_format(closure, $3.value, $3.length,
  239. $5.value, $5.length,
  240. $7.value, $7.length))
  241. YYABORT;
  242. }
  243. | PHDRS '{' phdrs_defs '}'
  244. | SEARCH_DIR '(' string ')'
  245. { script_add_search_dir(closure, $3.value, $3.length); }
  246. | SECTIONS '{'
  247. { script_start_sections(closure); }
  248. sections_block '}'
  249. { script_finish_sections(closure); }
  250. | TARGET_K '(' string ')'
  251. { script_set_target(closure, $3.value, $3.length); }
  252. | VERSIONK '{'
  253. { script_push_lex_into_version_mode(closure); }
  254. version_script '}'
  255. { script_pop_lex_mode(closure); }
  256. | ENTRY '(' string ')'
  257. { script_set_entry(closure, $3.value, $3.length); }
  258. | assignment end
  259. | ASSERT_K '(' parse_exp ',' string ')'
  260. { script_add_assertion(closure, $3, $5.value, $5.length); }
  261. | INCLUDE string
  262. { script_include_directive(PARSING_LINKER_SCRIPT, closure,
  263. $2.value, $2.length); }
  264. | ignore_cmd
  265. | ';'
  266. ;
  267. /* Top level commands which we ignore. The GNU linker uses these to
  268. select the output format, but we don't offer a choice. Ignoring
  269. these is more-or-less OK since most scripts simply explicitly
  270. choose the default. */
  271. ignore_cmd:
  272. OUTPUT_ARCH '(' string ')'
  273. ;
  274. /* A list of external undefined symbols. We put the lexer into
  275. expression mode so that commas separate names; this is what the GNU
  276. linker does. */
  277. extern_name_list:
  278. { script_push_lex_into_expression_mode(closure); }
  279. extern_name_list_body
  280. { script_pop_lex_mode(closure); }
  281. ;
  282. extern_name_list_body:
  283. string
  284. { script_add_extern(closure, $1.value, $1.length); }
  285. | extern_name_list_body string
  286. { script_add_extern(closure, $2.value, $2.length); }
  287. | extern_name_list_body ',' string
  288. { script_add_extern(closure, $3.value, $3.length); }
  289. ;
  290. /* A list of input file names. */
  291. input_list:
  292. input_list_element
  293. | input_list opt_comma input_list_element
  294. ;
  295. /* An input file name. */
  296. input_list_element:
  297. string
  298. { script_add_file(closure, $1.value, $1.length); }
  299. | '-' STRING
  300. { script_add_library(closure, $2.value, $2.length); }
  301. | AS_NEEDED
  302. { script_start_as_needed(closure); }
  303. '(' input_list ')'
  304. { script_end_as_needed(closure); }
  305. ;
  306. /* Commands in a SECTIONS block. */
  307. sections_block:
  308. sections_block section_block_cmd
  309. | /* empty */
  310. ;
  311. /* A command which may appear within a SECTIONS block. */
  312. section_block_cmd:
  313. ENTRY '(' string ')'
  314. { script_set_entry(closure, $3.value, $3.length); }
  315. | assignment end
  316. | ASSERT_K '(' parse_exp ',' string ')'
  317. { script_add_assertion(closure, $3, $5.value, $5.length); }
  318. | INCLUDE string
  319. { script_include_directive(PARSING_SECTIONS_BLOCK, closure,
  320. $2.value, $2.length); }
  321. | string section_header
  322. { script_start_output_section(closure, $1.value, $1.length, &$2); }
  323. '{' section_cmds '}' section_trailer
  324. { script_finish_output_section(closure, &$7); }
  325. ;
  326. /* The header of an output section in a SECTIONS block--everything
  327. after the name. */
  328. section_header:
  329. { script_push_lex_into_expression_mode(closure); }
  330. opt_address_and_section_type opt_at opt_align opt_subalign
  331. { script_pop_lex_mode(closure); }
  332. opt_constraint
  333. {
  334. $$.address = $2.address;
  335. $$.section_type = $2.section_type;
  336. $$.load_address = $3;
  337. $$.align = $4;
  338. $$.subalign = $5;
  339. $$.constraint = $7;
  340. }
  341. ;
  342. /* The optional address followed by the optional section type. This
  343. is a separate nonterminal to avoid a shift/reduce conflict on
  344. '(' in section_header. */
  345. opt_address_and_section_type:
  346. ':'
  347. {
  348. $$.address = NULL;
  349. $$.section_type = SCRIPT_SECTION_TYPE_NONE;
  350. }
  351. | '(' ')' ':'
  352. {
  353. $$.address = NULL;
  354. $$.section_type = SCRIPT_SECTION_TYPE_NONE;
  355. }
  356. | exp ':'
  357. {
  358. $$.address = $1;
  359. $$.section_type = SCRIPT_SECTION_TYPE_NONE;
  360. }
  361. | exp '(' ')' ':'
  362. {
  363. $$.address = $1;
  364. $$.section_type = SCRIPT_SECTION_TYPE_NONE;
  365. }
  366. | '(' section_type ')' ':'
  367. {
  368. $$.address = NULL;
  369. $$.section_type = $2;
  370. }
  371. | exp '(' section_type ')' ':'
  372. {
  373. $$.address = $1;
  374. $$.section_type = $3;
  375. }
  376. ;
  377. /* We only support NOLOAD. */
  378. section_type:
  379. NOLOAD
  380. { $$ = SCRIPT_SECTION_TYPE_NOLOAD; }
  381. | DSECT
  382. {
  383. yyerror(closure, "DSECT section type is unsupported");
  384. $$ = SCRIPT_SECTION_TYPE_DSECT;
  385. }
  386. | COPY
  387. {
  388. yyerror(closure, "COPY section type is unsupported");
  389. $$ = SCRIPT_SECTION_TYPE_COPY;
  390. }
  391. | INFO
  392. {
  393. yyerror(closure, "INFO section type is unsupported");
  394. $$ = SCRIPT_SECTION_TYPE_INFO;
  395. }
  396. | OVERLAY
  397. {
  398. yyerror(closure, "OVERLAY section type is unsupported");
  399. $$ = SCRIPT_SECTION_TYPE_OVERLAY;
  400. }
  401. ;
  402. /* The address at which an output section should be loaded. */
  403. opt_at:
  404. /* empty */
  405. { $$ = NULL; }
  406. | AT '(' exp ')'
  407. { $$ = $3; }
  408. ;
  409. /* The alignment of an output section. */
  410. opt_align:
  411. /* empty */
  412. { $$ = NULL; }
  413. | ALIGN_K '(' exp ')'
  414. { $$ = $3; }
  415. ;
  416. /* The input section alignment within an output section. */
  417. opt_subalign:
  418. /* empty */
  419. { $$ = NULL; }
  420. | SUBALIGN '(' exp ')'
  421. { $$ = $3; }
  422. ;
  423. /* A section constraint. */
  424. opt_constraint:
  425. /* empty */
  426. { $$ = CONSTRAINT_NONE; }
  427. | ONLY_IF_RO
  428. { $$ = CONSTRAINT_ONLY_IF_RO; }
  429. | ONLY_IF_RW
  430. { $$ = CONSTRAINT_ONLY_IF_RW; }
  431. | SPECIAL
  432. { $$ = CONSTRAINT_SPECIAL; }
  433. ;
  434. /* The trailer of an output section in a SECTIONS block. */
  435. section_trailer:
  436. opt_memspec opt_at_memspec opt_phdr opt_fill opt_comma
  437. {
  438. $$.fill = $4;
  439. $$.phdrs = $3;
  440. }
  441. ;
  442. /* A memory specification for an output section. */
  443. opt_memspec:
  444. '>' string
  445. { script_set_section_region(closure, $2.value, $2.length, 1); }
  446. | /* empty */
  447. ;
  448. /* A memory specification for where to load an output section. */
  449. opt_at_memspec:
  450. AT '>' string
  451. { script_set_section_region(closure, $3.value, $3.length, 0); }
  452. | /* empty */
  453. ;
  454. /* The program segment an output section should go into. */
  455. opt_phdr:
  456. opt_phdr ':' string
  457. { $$ = script_string_list_push_back($1, $3.value, $3.length); }
  458. | /* empty */
  459. { $$ = NULL; }
  460. ;
  461. /* The value to use to fill an output section. FIXME: This does not
  462. handle a string of arbitrary length. */
  463. opt_fill:
  464. '=' parse_exp
  465. { $$ = $2; }
  466. | /* empty */
  467. { $$ = NULL; }
  468. ;
  469. /* Commands which may appear within the description of an output
  470. section in a SECTIONS block. */
  471. section_cmds:
  472. /* empty */
  473. | section_cmds section_cmd
  474. ;
  475. /* A command which may appear within the description of an output
  476. section in a SECTIONS block. */
  477. section_cmd:
  478. assignment end
  479. | input_section_spec
  480. | data_length '(' parse_exp ')'
  481. { script_add_data(closure, $1, $3); }
  482. | ASSERT_K '(' parse_exp ',' string ')'
  483. { script_add_assertion(closure, $3, $5.value, $5.length); }
  484. | FILL '(' parse_exp ')'
  485. { script_add_fill(closure, $3); }
  486. | CONSTRUCTORS
  487. {
  488. /* The GNU linker uses CONSTRUCTORS for the a.out object
  489. file format. It does nothing when using ELF. Since
  490. some ELF linker scripts use it although it does
  491. nothing, we accept it and ignore it. */
  492. }
  493. | SORT_BY_NAME '(' CONSTRUCTORS ')'
  494. | INCLUDE string
  495. { script_include_directive(PARSING_SECTION_COMMANDS, closure,
  496. $2.value, $2.length); }
  497. | ';'
  498. ;
  499. /* The length of data which may appear within the description of an
  500. output section in a SECTIONS block. */
  501. data_length:
  502. QUAD
  503. { $$ = QUAD; }
  504. | SQUAD
  505. { $$ = SQUAD; }
  506. | LONG
  507. { $$ = LONG; }
  508. | SHORT
  509. { $$ = SHORT; }
  510. | BYTE
  511. { $$ = BYTE; }
  512. ;
  513. /* An input section specification. This may appear within the
  514. description of an output section in a SECTIONS block. */
  515. input_section_spec:
  516. input_section_no_keep
  517. { script_add_input_section(closure, &$1, 0); }
  518. | KEEP '(' input_section_no_keep ')'
  519. { script_add_input_section(closure, &$3, 1); }
  520. ;
  521. /* An input section specification within a KEEP clause. */
  522. input_section_no_keep:
  523. string
  524. {
  525. $$.file.name = $1;
  526. $$.file.sort = SORT_WILDCARD_NONE;
  527. $$.input_sections.sections = NULL;
  528. $$.input_sections.exclude = NULL;
  529. }
  530. | wildcard_file '(' wildcard_sections ')'
  531. {
  532. $$.file = $1;
  533. $$.input_sections = $3;
  534. }
  535. ;
  536. /* A wildcard file specification. */
  537. wildcard_file:
  538. wildcard_name
  539. {
  540. $$.name = $1;
  541. $$.sort = SORT_WILDCARD_NONE;
  542. }
  543. | SORT_BY_NAME '(' wildcard_name ')'
  544. {
  545. $$.name = $3;
  546. $$.sort = SORT_WILDCARD_BY_NAME;
  547. }
  548. ;
  549. /* A list of wild card section specifications. */
  550. wildcard_sections:
  551. wildcard_sections opt_comma wildcard_section
  552. {
  553. $$.sections = script_string_sort_list_add($1.sections, &$3);
  554. $$.exclude = $1.exclude;
  555. }
  556. | wildcard_section
  557. {
  558. $$.sections = script_new_string_sort_list(&$1);
  559. $$.exclude = NULL;
  560. }
  561. | wildcard_sections opt_comma EXCLUDE_FILE '(' exclude_names ')'
  562. {
  563. $$.sections = $1.sections;
  564. $$.exclude = script_string_list_append($1.exclude, $5);
  565. }
  566. | EXCLUDE_FILE '(' exclude_names ')'
  567. {
  568. $$.sections = NULL;
  569. $$.exclude = $3;
  570. }
  571. ;
  572. /* A single wild card specification. */
  573. wildcard_section:
  574. wildcard_name
  575. {
  576. $$.name = $1;
  577. $$.sort = SORT_WILDCARD_NONE;
  578. }
  579. | SORT_BY_NAME '(' wildcard_section ')'
  580. {
  581. $$.name = $3.name;
  582. switch ($3.sort)
  583. {
  584. case SORT_WILDCARD_NONE:
  585. $$.sort = SORT_WILDCARD_BY_NAME;
  586. break;
  587. case SORT_WILDCARD_BY_NAME:
  588. case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
  589. break;
  590. case SORT_WILDCARD_BY_ALIGNMENT:
  591. case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
  592. $$.sort = SORT_WILDCARD_BY_NAME_BY_ALIGNMENT;
  593. break;
  594. default:
  595. abort();
  596. }
  597. }
  598. | SORT_BY_ALIGNMENT '(' wildcard_section ')'
  599. {
  600. $$.name = $3.name;
  601. switch ($3.sort)
  602. {
  603. case SORT_WILDCARD_NONE:
  604. $$.sort = SORT_WILDCARD_BY_ALIGNMENT;
  605. break;
  606. case SORT_WILDCARD_BY_ALIGNMENT:
  607. case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
  608. break;
  609. case SORT_WILDCARD_BY_NAME:
  610. case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
  611. $$.sort = SORT_WILDCARD_BY_ALIGNMENT_BY_NAME;
  612. break;
  613. default:
  614. abort();
  615. }
  616. }
  617. ;
  618. /* A list of file names to exclude. */
  619. exclude_names:
  620. exclude_names opt_comma wildcard_name
  621. { $$ = script_string_list_push_back($1, $3.value, $3.length); }
  622. | wildcard_name
  623. { $$ = script_new_string_list($1.value, $1.length); }
  624. ;
  625. /* A single wildcard name. We recognize '*' and '?' specially since
  626. they are expression tokens. */
  627. wildcard_name:
  628. string
  629. { $$ = $1; }
  630. | '*'
  631. {
  632. $$.value = "*";
  633. $$.length = 1;
  634. }
  635. | '?'
  636. {
  637. $$.value = "?";
  638. $$.length = 1;
  639. }
  640. ;
  641. /* A list of MEMORY definitions. */
  642. memory_defs:
  643. memory_defs opt_comma memory_def
  644. | /* empty */
  645. ;
  646. /* A single MEMORY definition. */
  647. memory_def:
  648. string memory_attr ':' memory_origin '=' parse_exp opt_comma memory_length '=' parse_exp
  649. { script_add_memory(closure, $1.value, $1.length, $2, $6, $10); }
  650. |
  651. INCLUDE string
  652. { script_include_directive(PARSING_MEMORY_DEF, closure,
  653. $2.value, $2.length); }
  654. |
  655. ;
  656. /* The (optional) attributes of a MEMORY region. */
  657. memory_attr:
  658. '(' string ')'
  659. { $$ = script_parse_memory_attr(closure, $2.value, $2.length, 0); }
  660. | /* Inverted attributes. */
  661. '(' '!' string ')'
  662. { $$ = script_parse_memory_attr(closure, $3.value, $3.length, 1); }
  663. | /* empty */
  664. { $$ = 0; }
  665. ;
  666. memory_origin:
  667. ORIGIN
  668. |
  669. ORG
  670. |
  671. 'o'
  672. ;
  673. memory_length:
  674. LENGTH
  675. |
  676. LEN
  677. |
  678. 'l'
  679. ;
  680. /* A list of program header definitions. */
  681. phdrs_defs:
  682. phdrs_defs phdr_def
  683. | /* empty */
  684. ;
  685. /* A program header definition. */
  686. phdr_def:
  687. string phdr_type phdr_info ';'
  688. { script_add_phdr(closure, $1.value, $1.length, $2, &$3); }
  689. ;
  690. /* A program header type. The GNU linker accepts a general expression
  691. here, but that would be a pain because we would have to dig into
  692. the expression structure. It's unlikely that anybody uses anything
  693. other than a string or a number here, so that is all we expect. */
  694. phdr_type:
  695. string
  696. { $$ = script_phdr_string_to_type(closure, $1.value, $1.length); }
  697. | INTEGER
  698. { $$ = $1; }
  699. ;
  700. /* Additional information for a program header. */
  701. phdr_info:
  702. /* empty */
  703. { memset(&$$, 0, sizeof(struct Phdr_info)); }
  704. | string phdr_info
  705. {
  706. $$ = $2;
  707. if ($1.length == 7 && strncmp($1.value, "FILEHDR", 7) == 0)
  708. $$.includes_filehdr = 1;
  709. else
  710. yyerror(closure, "PHDRS syntax error");
  711. }
  712. | PHDRS phdr_info
  713. {
  714. $$ = $2;
  715. $$.includes_phdrs = 1;
  716. }
  717. | string '(' INTEGER ')' phdr_info
  718. {
  719. $$ = $5;
  720. if ($1.length == 5 && strncmp($1.value, "FLAGS", 5) == 0)
  721. {
  722. $$.is_flags_valid = 1;
  723. $$.flags = $3;
  724. }
  725. else
  726. yyerror(closure, "PHDRS syntax error");
  727. }
  728. | AT '(' parse_exp ')' phdr_info
  729. {
  730. $$ = $5;
  731. $$.load_address = $3;
  732. }
  733. ;
  734. /* Set a symbol to a value. */
  735. assignment:
  736. string '=' parse_exp
  737. { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
  738. | string PLUSEQ parse_exp
  739. {
  740. Expression_ptr s = script_exp_string($1.value, $1.length);
  741. Expression_ptr e = script_exp_binary_add(s, $3);
  742. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  743. }
  744. | string MINUSEQ parse_exp
  745. {
  746. Expression_ptr s = script_exp_string($1.value, $1.length);
  747. Expression_ptr e = script_exp_binary_sub(s, $3);
  748. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  749. }
  750. | string MULTEQ parse_exp
  751. {
  752. Expression_ptr s = script_exp_string($1.value, $1.length);
  753. Expression_ptr e = script_exp_binary_mult(s, $3);
  754. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  755. }
  756. | string DIVEQ parse_exp
  757. {
  758. Expression_ptr s = script_exp_string($1.value, $1.length);
  759. Expression_ptr e = script_exp_binary_div(s, $3);
  760. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  761. }
  762. | string LSHIFTEQ parse_exp
  763. {
  764. Expression_ptr s = script_exp_string($1.value, $1.length);
  765. Expression_ptr e = script_exp_binary_lshift(s, $3);
  766. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  767. }
  768. | string RSHIFTEQ parse_exp
  769. {
  770. Expression_ptr s = script_exp_string($1.value, $1.length);
  771. Expression_ptr e = script_exp_binary_rshift(s, $3);
  772. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  773. }
  774. | string ANDEQ parse_exp
  775. {
  776. Expression_ptr s = script_exp_string($1.value, $1.length);
  777. Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
  778. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  779. }
  780. | string OREQ parse_exp
  781. {
  782. Expression_ptr s = script_exp_string($1.value, $1.length);
  783. Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
  784. script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
  785. }
  786. | PROVIDE '(' string '=' parse_exp ')'
  787. { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
  788. | PROVIDE_HIDDEN '(' string '=' parse_exp ')'
  789. { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
  790. ;
  791. /* Parse an expression, putting the lexer into the right mode. */
  792. parse_exp:
  793. { script_push_lex_into_expression_mode(closure); }
  794. exp
  795. {
  796. script_pop_lex_mode(closure);
  797. $$ = $2;
  798. }
  799. ;
  800. /* An expression. */
  801. exp:
  802. '(' exp ')'
  803. { $$ = $2; }
  804. | '-' exp %prec UNARY
  805. { $$ = script_exp_unary_minus($2); }
  806. | '!' exp %prec UNARY
  807. { $$ = script_exp_unary_logical_not($2); }
  808. | '~' exp %prec UNARY
  809. { $$ = script_exp_unary_bitwise_not($2); }
  810. | '+' exp %prec UNARY
  811. { $$ = $2; }
  812. | exp '*' exp
  813. { $$ = script_exp_binary_mult($1, $3); }
  814. | exp '/' exp
  815. { $$ = script_exp_binary_div($1, $3); }
  816. | exp '%' exp
  817. { $$ = script_exp_binary_mod($1, $3); }
  818. | exp '+' exp
  819. { $$ = script_exp_binary_add($1, $3); }
  820. | exp '-' exp
  821. { $$ = script_exp_binary_sub($1, $3); }
  822. | exp LSHIFT exp
  823. { $$ = script_exp_binary_lshift($1, $3); }
  824. | exp RSHIFT exp
  825. { $$ = script_exp_binary_rshift($1, $3); }
  826. | exp EQ exp
  827. { $$ = script_exp_binary_eq($1, $3); }
  828. | exp NE exp
  829. { $$ = script_exp_binary_ne($1, $3); }
  830. | exp LE exp
  831. { $$ = script_exp_binary_le($1, $3); }
  832. | exp GE exp
  833. { $$ = script_exp_binary_ge($1, $3); }
  834. | exp '<' exp
  835. { $$ = script_exp_binary_lt($1, $3); }
  836. | exp '>' exp
  837. { $$ = script_exp_binary_gt($1, $3); }
  838. | exp '&' exp
  839. { $$ = script_exp_binary_bitwise_and($1, $3); }
  840. | exp '^' exp
  841. { $$ = script_exp_binary_bitwise_xor($1, $3); }
  842. | exp '|' exp
  843. { $$ = script_exp_binary_bitwise_or($1, $3); }
  844. | exp ANDAND exp
  845. { $$ = script_exp_binary_logical_and($1, $3); }
  846. | exp OROR exp
  847. { $$ = script_exp_binary_logical_or($1, $3); }
  848. | exp '?' exp ':' exp
  849. { $$ = script_exp_trinary_cond($1, $3, $5); }
  850. | INTEGER
  851. { $$ = script_exp_integer($1); }
  852. | string
  853. { $$ = script_symbol(closure, $1.value, $1.length); }
  854. | MAX_K '(' exp ',' exp ')'
  855. { $$ = script_exp_function_max($3, $5); }
  856. | MIN_K '(' exp ',' exp ')'
  857. { $$ = script_exp_function_min($3, $5); }
  858. | DEFINED '(' string ')'
  859. { $$ = script_exp_function_defined($3.value, $3.length); }
  860. | SIZEOF_HEADERS
  861. { $$ = script_exp_function_sizeof_headers(); }
  862. | ALIGNOF '(' string ')'
  863. { $$ = script_exp_function_alignof($3.value, $3.length); }
  864. | SIZEOF '(' string ')'
  865. { $$ = script_exp_function_sizeof($3.value, $3.length); }
  866. | ADDR '(' string ')'
  867. { $$ = script_exp_function_addr($3.value, $3.length); }
  868. | LOADADDR '(' string ')'
  869. { $$ = script_exp_function_loadaddr($3.value, $3.length); }
  870. | ORIGIN '(' string ')'
  871. { $$ = script_exp_function_origin(closure, $3.value, $3.length); }
  872. | LENGTH '(' string ')'
  873. { $$ = script_exp_function_length(closure, $3.value, $3.length); }
  874. | CONSTANT '(' string ')'
  875. { $$ = script_exp_function_constant($3.value, $3.length); }
  876. | ABSOLUTE '(' exp ')'
  877. { $$ = script_exp_function_absolute($3); }
  878. | ALIGN_K '(' exp ')'
  879. { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
  880. | ALIGN_K '(' exp ',' exp ')'
  881. { $$ = script_exp_function_align($3, $5); }
  882. | BLOCK '(' exp ')'
  883. { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
  884. | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
  885. {
  886. script_data_segment_align(closure);
  887. $$ = script_exp_function_data_segment_align($3, $5);
  888. }
  889. | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
  890. {
  891. script_data_segment_relro_end(closure);
  892. $$ = script_exp_function_data_segment_relro_end($3, $5);
  893. }
  894. | DATA_SEGMENT_END '(' exp ')'
  895. { $$ = script_exp_function_data_segment_end($3); }
  896. | SEGMENT_START '(' string ',' exp ')'
  897. {
  898. $$ = script_exp_function_segment_start($3.value, $3.length, $5);
  899. /* We need to take note of any SEGMENT_START expressions
  900. because they change the behaviour of -Ttext, -Tdata and
  901. -Tbss options. */
  902. script_saw_segment_start_expression(closure);
  903. }
  904. | ASSERT_K '(' exp ',' string ')'
  905. { $$ = script_exp_function_assert($3, $5.value, $5.length); }
  906. ;
  907. /* Handle the --defsym option. */
  908. defsym_expr:
  909. string '=' parse_exp
  910. { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
  911. ;
  912. /* Handle the --dynamic-list option. A dynamic list has the format
  913. { sym1; sym2; extern "C++" { namespace::sym3 }; };
  914. We store the symbol we see in the "local" list; that is where
  915. Command_line::in_dynamic_list() will look to do its check.
  916. TODO(csilvers): More than one of these brace-lists can appear, and
  917. should just be merged and treated as a single list. */
  918. dynamic_list_expr: dynamic_list_nodes ;
  919. dynamic_list_nodes:
  920. dynamic_list_node
  921. | dynamic_list_nodes dynamic_list_node
  922. ;
  923. dynamic_list_node:
  924. '{' vers_defns ';' '}' ';'
  925. { script_new_vers_node (closure, NULL, $2); }
  926. ;
  927. /* A version script. */
  928. version_script:
  929. vers_nodes
  930. ;
  931. vers_nodes:
  932. vers_node
  933. | vers_nodes vers_node
  934. ;
  935. vers_node:
  936. '{' vers_tag '}' ';'
  937. {
  938. script_register_vers_node (closure, NULL, 0, $2, NULL);
  939. }
  940. | string '{' vers_tag '}' ';'
  941. {
  942. script_register_vers_node (closure, $1.value, $1.length, $3,
  943. NULL);
  944. }
  945. | string '{' vers_tag '}' verdep ';'
  946. {
  947. script_register_vers_node (closure, $1.value, $1.length, $3, $5);
  948. }
  949. ;
  950. verdep:
  951. string
  952. {
  953. $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
  954. }
  955. | verdep string
  956. {
  957. $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
  958. }
  959. ;
  960. vers_tag:
  961. /* empty */
  962. { $$ = script_new_vers_node (closure, NULL, NULL); }
  963. | vers_defns ';'
  964. { $$ = script_new_vers_node (closure, $1, NULL); }
  965. | GLOBAL ':' vers_defns ';'
  966. { $$ = script_new_vers_node (closure, $3, NULL); }
  967. | LOCAL ':' vers_defns ';'
  968. { $$ = script_new_vers_node (closure, NULL, $3); }
  969. | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
  970. { $$ = script_new_vers_node (closure, $3, $7); }
  971. ;
  972. /* Here is one of the rare places we care about the distinction
  973. between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
  974. matching on the pattern, so we pass in true for the exact_match
  975. parameter. For STRING, we do glob matching and pass in false. */
  976. vers_defns:
  977. STRING
  978. {
  979. $$ = script_new_vers_pattern (closure, NULL, $1.value,
  980. $1.length, 0);
  981. }
  982. | QUOTED_STRING
  983. {
  984. $$ = script_new_vers_pattern (closure, NULL, $1.value,
  985. $1.length, 1);
  986. }
  987. | vers_defns ';' STRING
  988. {
  989. $$ = script_new_vers_pattern (closure, $1, $3.value,
  990. $3.length, 0);
  991. }
  992. | vers_defns ';' QUOTED_STRING
  993. {
  994. $$ = script_new_vers_pattern (closure, $1, $3.value,
  995. $3.length, 1);
  996. }
  997. | /* Push string on the language stack. */
  998. EXTERN string '{'
  999. { version_script_push_lang (closure, $2.value, $2.length); }
  1000. vers_defns opt_semicolon '}'
  1001. {
  1002. $$ = $5;
  1003. version_script_pop_lang(closure);
  1004. }
  1005. | /* Push string on the language stack. This is more complicated
  1006. than the other cases because we need to merge the linked-list
  1007. state from the pre-EXTERN defns and the post-EXTERN defns. */
  1008. vers_defns ';' EXTERN string '{'
  1009. { version_script_push_lang (closure, $4.value, $4.length); }
  1010. vers_defns opt_semicolon '}'
  1011. {
  1012. $$ = script_merge_expressions ($1, $7);
  1013. version_script_pop_lang(closure);
  1014. }
  1015. | EXTERN // "extern" as a symbol name
  1016. {
  1017. $$ = script_new_vers_pattern (closure, NULL, "extern",
  1018. sizeof("extern") - 1, 1);
  1019. }
  1020. | vers_defns ';' EXTERN
  1021. {
  1022. $$ = script_new_vers_pattern (closure, $1, "extern",
  1023. sizeof("extern") - 1, 1);
  1024. }
  1025. ;
  1026. /* A string can be either a STRING or a QUOTED_STRING. Almost all the
  1027. time we don't care, and we use this rule. */
  1028. string:
  1029. STRING
  1030. { $$ = $1; }
  1031. | QUOTED_STRING
  1032. { $$ = $1; }
  1033. ;
  1034. /* Some statements require a terminator, which may be a semicolon or a
  1035. comma. */
  1036. end:
  1037. ';'
  1038. | ','
  1039. ;
  1040. /* An optional semicolon. */
  1041. opt_semicolon:
  1042. ';'
  1043. | /* empty */
  1044. ;
  1045. /* An optional comma. */
  1046. opt_comma:
  1047. ','
  1048. | /* empty */
  1049. ;
  1050. %%