script-c.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* script-c.h -- C interface for linker scripts in 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 file exists so that both the bison parser and script.cc can
  18. include it, so that they can communicate back and forth. */
  19. #ifndef GOLD_SCRIPT_C_H
  20. #define GOLD_SCRIPT_C_H
  21. #ifdef __cplusplus
  22. #include <vector>
  23. #include <string>
  24. #endif
  25. #ifdef __cplusplus
  26. // For the C++ code we declare the various supporting structures in
  27. // the gold namespace. For the C code we declare it at the top level.
  28. // The namespace level should not affect the layout of the structure.
  29. namespace gold
  30. {
  31. #endif
  32. /* A string value for the bison parser. */
  33. struct Parser_string
  34. {
  35. const char* value;
  36. size_t length;
  37. };
  38. /* The expression functions deal with pointers to Expression objects.
  39. Since the bison parser generates C code, this is a hack to keep the
  40. C++ code type safe. This hacks assumes that all pointers look
  41. alike. */
  42. #ifdef __cplusplus
  43. class Expression;
  44. typedef Expression* Expression_ptr;
  45. #else
  46. typedef void* Expression_ptr;
  47. #endif
  48. /* Script_section type. */
  49. enum Script_section_type
  50. {
  51. /* No section type. */
  52. SCRIPT_SECTION_TYPE_NONE,
  53. SCRIPT_SECTION_TYPE_NOLOAD,
  54. SCRIPT_SECTION_TYPE_DSECT,
  55. SCRIPT_SECTION_TYPE_COPY,
  56. SCRIPT_SECTION_TYPE_INFO,
  57. SCRIPT_SECTION_TYPE_OVERLAY
  58. };
  59. /* A constraint for whether to use a particular output section
  60. definition. */
  61. enum Section_constraint
  62. {
  63. /* No constraint. */
  64. CONSTRAINT_NONE,
  65. /* Only if all input sections are read-only. */
  66. CONSTRAINT_ONLY_IF_RO,
  67. /* Only if at least input section is writable. */
  68. CONSTRAINT_ONLY_IF_RW,
  69. /* Special constraint. */
  70. CONSTRAINT_SPECIAL
  71. };
  72. /* The information we store for an output section header in the bison
  73. parser. */
  74. struct Parser_output_section_header
  75. {
  76. /* The address. This may be NULL. */
  77. Expression_ptr address;
  78. /* Section type. May be NULL string. */
  79. enum Script_section_type section_type;
  80. /* The load address, from the AT specifier. This may be NULL. */
  81. Expression_ptr load_address;
  82. /* The alignment, from the ALIGN specifier. This may be NULL. */
  83. Expression_ptr align;
  84. /* The input section alignment, from the SUBALIGN specifier. This
  85. may be NULL. */
  86. Expression_ptr subalign;
  87. /* A constraint on this output section. */
  88. enum Section_constraint constraint;
  89. };
  90. /* We keep vectors of strings. In order to manage this in both C and
  91. C++, we use a pointer to a vector. This assumes that all pointers
  92. look the same. */
  93. #ifdef __cplusplus
  94. typedef std::vector<std::string> String_list;
  95. typedef String_list* String_list_ptr;
  96. #else
  97. typedef void* String_list_ptr;
  98. #endif
  99. /* The information we store for an output section trailer in the bison
  100. parser. */
  101. struct Parser_output_section_trailer
  102. {
  103. /* The fill value. This may be NULL. */
  104. Expression_ptr fill;
  105. /* The program segments this section should go into. This may be
  106. NULL. */
  107. String_list_ptr phdrs;
  108. };
  109. /* The different sorts we can find in a linker script. */
  110. enum Sort_wildcard
  111. {
  112. SORT_WILDCARD_NONE,
  113. SORT_WILDCARD_BY_NAME,
  114. SORT_WILDCARD_BY_ALIGNMENT,
  115. SORT_WILDCARD_BY_NAME_BY_ALIGNMENT,
  116. SORT_WILDCARD_BY_ALIGNMENT_BY_NAME,
  117. SORT_WILDCARD_BY_INIT_PRIORITY
  118. };
  119. /* The information we build for a single wildcard specification. */
  120. struct Wildcard_section
  121. {
  122. /* The wildcard spec itself. */
  123. struct Parser_string name;
  124. /* How the entries should be sorted. */
  125. enum Sort_wildcard sort;
  126. };
  127. /* A vector of Wildcard_section entries. */
  128. #ifdef __cplusplus
  129. typedef std::vector<Wildcard_section> String_sort_list;
  130. typedef String_sort_list* String_sort_list_ptr;
  131. #else
  132. typedef void* String_sort_list_ptr;
  133. #endif
  134. /* A list of wildcard specifications, which may include EXCLUDE_FILE
  135. clauses. */
  136. struct Wildcard_sections
  137. {
  138. /* Wildcard specs. */
  139. String_sort_list_ptr sections;
  140. /* Exclusions. */
  141. String_list_ptr exclude;
  142. };
  143. /* A complete input section specification. */
  144. struct Input_section_spec
  145. {
  146. /* The file name. */
  147. struct Wildcard_section file;
  148. /* The list of sections. */
  149. struct Wildcard_sections input_sections;
  150. };
  151. /* Information for a program header. */
  152. struct Phdr_info
  153. {
  154. /* A boolean value: whether to include the file header. */
  155. int includes_filehdr;
  156. /* A boolean value: whether to include the program headers. */
  157. int includes_phdrs;
  158. /* A boolean value: whether the flags field is valid. */
  159. int is_flags_valid;
  160. /* The value to use for the flags. */
  161. unsigned int flags;
  162. /* The load address. */
  163. Expression_ptr load_address;
  164. };
  165. struct Version_dependency_list;
  166. struct Version_expression_list;
  167. struct Version_tree;
  168. #ifdef __cplusplus
  169. extern "C" {
  170. #endif
  171. /* The bison parser definitions. */
  172. #include "yyscript.h"
  173. /* The bison parser function. */
  174. extern int
  175. yyparse(void* closure);
  176. /* Called by the bison parser skeleton to return the next token. */
  177. extern int
  178. yylex(YYSTYPE*, void* closure);
  179. /* Called by the bison parser skeleton to report an error. */
  180. extern void
  181. yyerror(void* closure, const char*);
  182. /* Called by the bison parser to add an external symbol (a symbol in
  183. an EXTERN declaration) to the link. */
  184. extern void
  185. script_add_extern(void* closure, const char*, size_t);
  186. /* Called by the bison parser to add a file to the link. */
  187. extern void
  188. script_add_file(void* closure, const char*, size_t);
  189. /* Called by the bison parser to add a library to the link. */
  190. extern void
  191. script_add_library(void* closure, const char*, size_t);
  192. /* Called by the bison parser to start and stop a group. */
  193. extern void
  194. script_start_group(void* closure);
  195. extern void
  196. script_end_group(void* closure);
  197. /* Called by the bison parser to start and end an AS_NEEDED list. */
  198. extern void
  199. script_start_as_needed(void* closure);
  200. extern void
  201. script_end_as_needed(void* closure);
  202. /* Called by the bison parser to set the entry symbol. */
  203. extern void
  204. script_set_entry(void* closure, const char*, size_t);
  205. /* Called by the bison parser to set whether to define common symbols. */
  206. extern void
  207. script_set_common_allocation(void* closure, int);
  208. /* Called by the bison parser to parse an OPTION. */
  209. extern void
  210. script_parse_option(void* closure, const char*, size_t);
  211. /* Called by the bison parser to handle OUTPUT_FORMAT. This return 0
  212. if the parse should be aborted. */
  213. extern int
  214. script_check_output_format(void* closure, const char*, size_t,
  215. const char*, size_t, const char*, size_t);
  216. /* Called by the bison parser to handle TARGET. */
  217. extern void
  218. script_set_target(void* closure, const char*, size_t);
  219. /* Called by the bison parser to handle SEARCH_DIR. */
  220. extern void
  221. script_add_search_dir(void* closure, const char*, size_t);
  222. /* Called by the bison parser to push the lexer into expression
  223. mode. */
  224. extern void
  225. script_push_lex_into_expression_mode(void* closure);
  226. /* Called by the bison parser to push the lexer into version
  227. mode. */
  228. extern void
  229. script_push_lex_into_version_mode(void* closure);
  230. /* Called by the bison parser to pop the lexer mode. */
  231. extern void
  232. script_pop_lex_mode(void* closure);
  233. /* Called by the bison parser to get the value of a symbol. This is
  234. called for a reference to a symbol, but is not called for something
  235. like "sym += 10". Uses of the special symbol "." can just call
  236. script_exp_string. */
  237. extern Expression_ptr
  238. script_symbol(void* closure, const char*, size_t);
  239. /* Called by the bison parser to set a symbol to a value. PROVIDE is
  240. non-zero if the symbol should be provided--only defined if there is
  241. an undefined reference. HIDDEN is non-zero if the symbol should be
  242. hidden. */
  243. extern void
  244. script_set_symbol(void* closure, const char*, size_t, Expression_ptr,
  245. int provide, int hidden);
  246. /* Called by the bison parser to add an assertion. */
  247. extern void
  248. script_add_assertion(void* closure, Expression_ptr, const char* message,
  249. size_t messagelen);
  250. /* Called by the bison parser to start a SECTIONS clause. */
  251. extern void
  252. script_start_sections(void* closure);
  253. /* Called by the bison parser to finish a SECTIONS clause. */
  254. extern void
  255. script_finish_sections(void* closure);
  256. /* Called by the bison parser to start handling input section
  257. specifications for an output section. */
  258. extern void
  259. script_start_output_section(void* closure, const char* name, size_t namelen,
  260. const struct Parser_output_section_header*);
  261. /* Called by the bison parser when done handling input section
  262. specifications for an output section. */
  263. extern void
  264. script_finish_output_section(void* closure,
  265. const struct Parser_output_section_trailer*);
  266. /* Called by the bison parser to handle a data statement (LONG, BYTE,
  267. etc.) in an output section. */
  268. extern void
  269. script_add_data(void* closure, int data_token, Expression_ptr val);
  270. /* Called by the bison parser to set the fill value in an output
  271. section. */
  272. extern void
  273. script_add_fill(void* closure, Expression_ptr val);
  274. /* Called by the bison parser to add an input section specification to
  275. an output section. The KEEP parameter is non-zero if this is
  276. within a KEEP clause, meaning that the garbage collector should not
  277. discard it. */
  278. extern void
  279. script_add_input_section(void* closure, const struct Input_section_spec*,
  280. int keep);
  281. /* Create a new list of string and sort entries. */
  282. extern String_sort_list_ptr
  283. script_new_string_sort_list(const struct Wildcard_section*);
  284. /* Add an entry to a list of string and sort entries. */
  285. extern String_sort_list_ptr
  286. script_string_sort_list_add(String_sort_list_ptr,
  287. const struct Wildcard_section*);
  288. /* Create a new list of strings. */
  289. extern String_list_ptr
  290. script_new_string_list(const char*, size_t);
  291. /* Add an element to a list of strings. */
  292. extern String_list_ptr
  293. script_string_list_push_back(String_list_ptr, const char*, size_t);
  294. /* Concatenate two string lists. */
  295. extern String_list_ptr
  296. script_string_list_append(String_list_ptr, String_list_ptr);
  297. /* Define a new program header. */
  298. extern void
  299. script_add_phdr(void* closure, const char* name, size_t namelen,
  300. unsigned int type, const struct Phdr_info*);
  301. /* Convert a program header string to a type. */
  302. extern unsigned int
  303. script_phdr_string_to_type(void* closure, const char*, size_t);
  304. /* Handle DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
  305. extern void
  306. script_data_segment_align(void* closure);
  307. extern void
  308. script_data_segment_relro_end(void* closure);
  309. /* Record the fact that a SEGMENT_START expression is seen. */
  310. extern void
  311. script_saw_segment_start_expression(void* closure);
  312. /* Called by the bison parser for MEMORY regions. */
  313. extern void
  314. script_add_memory(void*, const char*, size_t, unsigned int,
  315. Expression_ptr, Expression_ptr);
  316. extern unsigned int
  317. script_parse_memory_attr(void*, const char*, size_t, int);
  318. extern void
  319. script_set_section_region(void*, const char*, size_t, int);
  320. extern void
  321. script_include_directive(int, void *, const char*, size_t);
  322. /* Called by the bison parser for expressions. */
  323. extern Expression_ptr
  324. script_exp_unary_minus(Expression_ptr);
  325. extern Expression_ptr
  326. script_exp_unary_logical_not(Expression_ptr);
  327. extern Expression_ptr
  328. script_exp_unary_bitwise_not(Expression_ptr);
  329. extern Expression_ptr
  330. script_exp_binary_mult(Expression_ptr, Expression_ptr);
  331. extern Expression_ptr
  332. script_exp_binary_div(Expression_ptr, Expression_ptr);
  333. extern Expression_ptr
  334. script_exp_binary_mod(Expression_ptr, Expression_ptr);
  335. extern Expression_ptr
  336. script_exp_binary_add(Expression_ptr, Expression_ptr);
  337. extern Expression_ptr
  338. script_exp_binary_sub(Expression_ptr, Expression_ptr);
  339. extern Expression_ptr
  340. script_exp_binary_lshift(Expression_ptr, Expression_ptr);
  341. extern Expression_ptr
  342. script_exp_binary_rshift(Expression_ptr, Expression_ptr);
  343. extern Expression_ptr
  344. script_exp_binary_eq(Expression_ptr, Expression_ptr);
  345. extern Expression_ptr
  346. script_exp_binary_ne(Expression_ptr, Expression_ptr);
  347. extern Expression_ptr
  348. script_exp_binary_le(Expression_ptr, Expression_ptr);
  349. extern Expression_ptr
  350. script_exp_binary_ge(Expression_ptr, Expression_ptr);
  351. extern Expression_ptr
  352. script_exp_binary_lt(Expression_ptr, Expression_ptr);
  353. extern Expression_ptr
  354. script_exp_binary_gt(Expression_ptr, Expression_ptr);
  355. extern Expression_ptr
  356. script_exp_binary_bitwise_and(Expression_ptr, Expression_ptr);
  357. extern Expression_ptr
  358. script_exp_binary_bitwise_xor(Expression_ptr, Expression_ptr);
  359. extern Expression_ptr
  360. script_exp_binary_bitwise_or(Expression_ptr, Expression_ptr);
  361. extern Expression_ptr
  362. script_exp_binary_logical_and(Expression_ptr, Expression_ptr);
  363. extern Expression_ptr
  364. script_exp_binary_logical_or(Expression_ptr, Expression_ptr);
  365. extern Expression_ptr
  366. script_exp_trinary_cond(Expression_ptr, Expression_ptr, Expression_ptr);
  367. extern Expression_ptr
  368. script_exp_integer(uint64_t);
  369. extern Expression_ptr
  370. script_exp_string(const char*, size_t);
  371. extern Expression_ptr
  372. script_exp_function_max(Expression_ptr, Expression_ptr);
  373. extern Expression_ptr
  374. script_exp_function_min(Expression_ptr, Expression_ptr);
  375. extern Expression_ptr
  376. script_exp_function_defined(const char*, size_t);
  377. extern Expression_ptr
  378. script_exp_function_sizeof_headers(void);
  379. extern Expression_ptr
  380. script_exp_function_alignof(const char*, size_t);
  381. extern Expression_ptr
  382. script_exp_function_sizeof(const char*, size_t);
  383. extern Expression_ptr
  384. script_exp_function_addr(const char*, size_t);
  385. extern Expression_ptr
  386. script_exp_function_loadaddr(const char*, size_t);
  387. extern Expression_ptr
  388. script_exp_function_origin(void*, const char*, size_t);
  389. extern Expression_ptr
  390. script_exp_function_length(void*, const char*, size_t);
  391. extern Expression_ptr
  392. script_exp_function_constant(const char*, size_t);
  393. extern Expression_ptr
  394. script_exp_function_absolute(Expression_ptr);
  395. extern Expression_ptr
  396. script_exp_function_align(Expression_ptr, Expression_ptr);
  397. extern Expression_ptr
  398. script_exp_function_data_segment_align(Expression_ptr, Expression_ptr);
  399. extern Expression_ptr
  400. script_exp_function_data_segment_relro_end(Expression_ptr, Expression_ptr);
  401. extern Expression_ptr
  402. script_exp_function_data_segment_end(Expression_ptr);
  403. extern Expression_ptr
  404. script_exp_function_segment_start(const char*, size_t, Expression_ptr);
  405. extern Expression_ptr
  406. script_exp_function_assert(Expression_ptr, const char*, size_t);
  407. extern void
  408. script_register_vers_node(void* closure,
  409. const char* tag,
  410. int taglen,
  411. struct Version_tree*,
  412. struct Version_dependency_list*);
  413. extern struct Version_dependency_list*
  414. script_add_vers_depend(void* closure,
  415. struct Version_dependency_list* existing_dependencies,
  416. const char* depend_to_add, int deplen);
  417. extern struct Version_expression_list*
  418. script_new_vers_pattern(void* closure,
  419. struct Version_expression_list*,
  420. const char*, int, int);
  421. extern struct Version_expression_list*
  422. script_merge_expressions(struct Version_expression_list* a,
  423. struct Version_expression_list* b);
  424. extern struct Version_tree*
  425. script_new_vers_node(void* closure,
  426. struct Version_expression_list* global,
  427. struct Version_expression_list* local);
  428. extern void
  429. version_script_push_lang(void* closure, const char* lang, int langlen);
  430. extern void
  431. version_script_pop_lang(void* closure);
  432. #ifdef __cplusplus
  433. } // End extern "C"
  434. #endif
  435. #ifdef __cplusplus
  436. } // End namespace gold.
  437. #endif
  438. #endif /* !defined(GOLD_SCRIPT_C_H) */