yyscript.y 30 KB

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