script-c.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* script-c.h -- C interface for linker scripts in 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 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. };
  118. /* The information we build for a single wildcard specification. */
  119. struct Wildcard_section
  120. {
  121. /* The wildcard spec itself. */
  122. struct Parser_string name;
  123. /* How the entries should be sorted. */
  124. enum Sort_wildcard sort;
  125. };
  126. /* A vector of Wildcard_section entries. */
  127. #ifdef __cplusplus
  128. typedef std::vector<Wildcard_section> String_sort_list;
  129. typedef String_sort_list* String_sort_list_ptr;
  130. #else
  131. typedef void* String_sort_list_ptr;
  132. #endif
  133. /* A list of wildcard specifications, which may include EXCLUDE_FILE
  134. clauses. */
  135. struct Wildcard_sections
  136. {
  137. /* Wildcard specs. */
  138. String_sort_list_ptr sections;
  139. /* Exclusions. */
  140. String_list_ptr exclude;
  141. };
  142. /* A complete input section specification. */
  143. struct Input_section_spec
  144. {
  145. /* The file name. */
  146. struct Wildcard_section file;
  147. /* The list of sections. */
  148. struct Wildcard_sections input_sections;
  149. };
  150. /* Information for a program header. */
  151. struct Phdr_info
  152. {
  153. /* A boolean value: whether to include the file header. */
  154. int includes_filehdr;
  155. /* A boolean value: whether to include the program headers. */
  156. int includes_phdrs;
  157. /* A boolean value: whether the flags field is valid. */
  158. int is_flags_valid;
  159. /* The value to use for the flags. */
  160. unsigned int flags;
  161. /* The load address. */
  162. Expression_ptr load_address;
  163. };
  164. struct Version_dependency_list;
  165. struct Version_expression_list;
  166. struct Version_tree;
  167. #ifdef __cplusplus
  168. extern "C" {
  169. #endif
  170. /* The bison parser definitions. */
  171. #include "yyscript.h"
  172. /* The bison parser function. */
  173. extern int
  174. yyparse(void* closure);
  175. /* Called by the bison parser skeleton to return the next token. */
  176. extern int
  177. yylex(YYSTYPE*, void* closure);
  178. /* Called by the bison parser skeleton to report an error. */
  179. extern void
  180. yyerror(void* closure, const char*);
  181. /* Called by the bison parser to add an external symbol (a symbol in
  182. an EXTERN declaration) to the link. */
  183. extern void
  184. script_add_extern(void* closure, const char*, size_t);
  185. /* Called by the bison parser to add a file to the link. */
  186. extern void
  187. script_add_file(void* closure, const char*, size_t);
  188. /* Called by the bison parser to add a library to the link. */
  189. extern void
  190. script_add_library(void* closure, const char*, size_t);
  191. /* Called by the bison parser to start and stop a group. */
  192. extern void
  193. script_start_group(void* closure);
  194. extern void
  195. script_end_group(void* closure);
  196. /* Called by the bison parser to start and end an AS_NEEDED list. */
  197. extern void
  198. script_start_as_needed(void* closure);
  199. extern void
  200. script_end_as_needed(void* closure);
  201. /* Called by the bison parser to set the entry symbol. */
  202. extern void
  203. script_set_entry(void* closure, const char*, size_t);
  204. /* Called by the bison parser to set whether to define common symbols. */
  205. extern void
  206. script_set_common_allocation(void* closure, int);
  207. /* Called by the bison parser to parse an OPTION. */
  208. extern void
  209. script_parse_option(void* closure, const char*, size_t);
  210. /* Called by the bison parser to handle OUTPUT_FORMAT. This return 0
  211. if the parse should be aborted. */
  212. extern int
  213. script_check_output_format(void* closure, const char*, size_t,
  214. const char*, size_t, const char*, size_t);
  215. /* Called by the bison parser to handle TARGET. */
  216. extern void
  217. script_set_target(void* closure, const char*, size_t);
  218. /* Called by the bison parser to handle SEARCH_DIR. */
  219. extern void
  220. script_add_search_dir(void* closure, const char*, size_t);
  221. /* Called by the bison parser to push the lexer into expression
  222. mode. */
  223. extern void
  224. script_push_lex_into_expression_mode(void* closure);
  225. /* Called by the bison parser to push the lexer into version
  226. mode. */
  227. extern void
  228. script_push_lex_into_version_mode(void* closure);
  229. /* Called by the bison parser to pop the lexer mode. */
  230. extern void
  231. script_pop_lex_mode(void* closure);
  232. /* Called by the bison parser to get the value of a symbol. This is
  233. called for a reference to a symbol, but is not called for something
  234. like "sym += 10". Uses of the special symbol "." can just call
  235. script_exp_string. */
  236. extern Expression_ptr
  237. script_symbol(void* closure, const char*, size_t);
  238. /* Called by the bison parser to set a symbol to a value. PROVIDE is
  239. non-zero if the symbol should be provided--only defined if there is
  240. an undefined reference. HIDDEN is non-zero if the symbol should be
  241. hidden. */
  242. extern void
  243. script_set_symbol(void* closure, const char*, size_t, Expression_ptr,
  244. int provide, int hidden);
  245. /* Called by the bison parser to add an assertion. */
  246. extern void
  247. script_add_assertion(void* closure, Expression_ptr, const char* message,
  248. size_t messagelen);
  249. /* Called by the bison parser to start a SECTIONS clause. */
  250. extern void
  251. script_start_sections(void* closure);
  252. /* Called by the bison parser to finish a SECTIONS clause. */
  253. extern void
  254. script_finish_sections(void* closure);
  255. /* Called by the bison parser to start handling input section
  256. specifications for an output section. */
  257. extern void
  258. script_start_output_section(void* closure, const char* name, size_t namelen,
  259. const struct Parser_output_section_header*);
  260. /* Called by the bison parser when done handling input section
  261. specifications for an output section. */
  262. extern void
  263. script_finish_output_section(void* closure,
  264. const struct Parser_output_section_trailer*);
  265. /* Called by the bison parser to handle a data statement (LONG, BYTE,
  266. etc.) in an output section. */
  267. extern void
  268. script_add_data(void* closure, int data_token, Expression_ptr val);
  269. /* Called by the bison parser to set the fill value in an output
  270. section. */
  271. extern void
  272. script_add_fill(void* closure, Expression_ptr val);
  273. /* Called by the bison parser to add an input section specification to
  274. an output section. The KEEP parameter is non-zero if this is
  275. within a KEEP clause, meaning that the garbage collector should not
  276. discard it. */
  277. extern void
  278. script_add_input_section(void* closure, const struct Input_section_spec*,
  279. int keep);
  280. /* Create a new list of string and sort entries. */
  281. extern String_sort_list_ptr
  282. script_new_string_sort_list(const struct Wildcard_section*);
  283. /* Add an entry to a list of string and sort entries. */
  284. extern String_sort_list_ptr
  285. script_string_sort_list_add(String_sort_list_ptr,
  286. const struct Wildcard_section*);
  287. /* Create a new list of strings. */
  288. extern String_list_ptr
  289. script_new_string_list(const char*, size_t);
  290. /* Add an element to a list of strings. */
  291. extern String_list_ptr
  292. script_string_list_push_back(String_list_ptr, const char*, size_t);
  293. /* Concatenate two string lists. */
  294. extern String_list_ptr
  295. script_string_list_append(String_list_ptr, String_list_ptr);
  296. /* Define a new program header. */
  297. extern void
  298. script_add_phdr(void* closure, const char* name, size_t namelen,
  299. unsigned int type, const struct Phdr_info*);
  300. /* Convert a program header string to a type. */
  301. extern unsigned int
  302. script_phdr_string_to_type(void* closure, const char*, size_t);
  303. /* Handle DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
  304. extern void
  305. script_data_segment_align(void* closure);
  306. extern void
  307. script_data_segment_relro_end(void* closure);
  308. /* Record the fact that a SEGMENT_START expression is seen. */
  309. extern void
  310. script_saw_segment_start_expression(void* closure);
  311. /* Called by the bison parser for MEMORY regions. */
  312. extern void
  313. script_add_memory(void*, const char*, size_t, unsigned int,
  314. Expression_ptr, Expression_ptr);
  315. extern unsigned int
  316. script_parse_memory_attr(void*, const char*, size_t, int);
  317. extern void
  318. script_set_section_region(void*, const char*, size_t, int);
  319. extern void
  320. script_include_directive(int, void *, const char*, size_t);
  321. /* Called by the bison parser for expressions. */
  322. extern Expression_ptr
  323. script_exp_unary_minus(Expression_ptr);
  324. extern Expression_ptr
  325. script_exp_unary_logical_not(Expression_ptr);
  326. extern Expression_ptr
  327. script_exp_unary_bitwise_not(Expression_ptr);
  328. extern Expression_ptr
  329. script_exp_binary_mult(Expression_ptr, Expression_ptr);
  330. extern Expression_ptr
  331. script_exp_binary_div(Expression_ptr, Expression_ptr);
  332. extern Expression_ptr
  333. script_exp_binary_mod(Expression_ptr, Expression_ptr);
  334. extern Expression_ptr
  335. script_exp_binary_add(Expression_ptr, Expression_ptr);
  336. extern Expression_ptr
  337. script_exp_binary_sub(Expression_ptr, Expression_ptr);
  338. extern Expression_ptr
  339. script_exp_binary_lshift(Expression_ptr, Expression_ptr);
  340. extern Expression_ptr
  341. script_exp_binary_rshift(Expression_ptr, Expression_ptr);
  342. extern Expression_ptr
  343. script_exp_binary_eq(Expression_ptr, Expression_ptr);
  344. extern Expression_ptr
  345. script_exp_binary_ne(Expression_ptr, Expression_ptr);
  346. extern Expression_ptr
  347. script_exp_binary_le(Expression_ptr, Expression_ptr);
  348. extern Expression_ptr
  349. script_exp_binary_ge(Expression_ptr, Expression_ptr);
  350. extern Expression_ptr
  351. script_exp_binary_lt(Expression_ptr, Expression_ptr);
  352. extern Expression_ptr
  353. script_exp_binary_gt(Expression_ptr, Expression_ptr);
  354. extern Expression_ptr
  355. script_exp_binary_bitwise_and(Expression_ptr, Expression_ptr);
  356. extern Expression_ptr
  357. script_exp_binary_bitwise_xor(Expression_ptr, Expression_ptr);
  358. extern Expression_ptr
  359. script_exp_binary_bitwise_or(Expression_ptr, Expression_ptr);
  360. extern Expression_ptr
  361. script_exp_binary_logical_and(Expression_ptr, Expression_ptr);
  362. extern Expression_ptr
  363. script_exp_binary_logical_or(Expression_ptr, Expression_ptr);
  364. extern Expression_ptr
  365. script_exp_trinary_cond(Expression_ptr, Expression_ptr, Expression_ptr);
  366. extern Expression_ptr
  367. script_exp_integer(uint64_t);
  368. extern Expression_ptr
  369. script_exp_string(const char*, size_t);
  370. extern Expression_ptr
  371. script_exp_function_max(Expression_ptr, Expression_ptr);
  372. extern Expression_ptr
  373. script_exp_function_min(Expression_ptr, Expression_ptr);
  374. extern Expression_ptr
  375. script_exp_function_defined(const char*, size_t);
  376. extern Expression_ptr
  377. script_exp_function_sizeof_headers(void);
  378. extern Expression_ptr
  379. script_exp_function_alignof(const char*, size_t);
  380. extern Expression_ptr
  381. script_exp_function_sizeof(const char*, size_t);
  382. extern Expression_ptr
  383. script_exp_function_addr(const char*, size_t);
  384. extern Expression_ptr
  385. script_exp_function_loadaddr(const char*, size_t);
  386. extern Expression_ptr
  387. script_exp_function_origin(void*, const char*, size_t);
  388. extern Expression_ptr
  389. script_exp_function_length(void*, const char*, size_t);
  390. extern Expression_ptr
  391. script_exp_function_constant(const char*, size_t);
  392. extern Expression_ptr
  393. script_exp_function_absolute(Expression_ptr);
  394. extern Expression_ptr
  395. script_exp_function_align(Expression_ptr, Expression_ptr);
  396. extern Expression_ptr
  397. script_exp_function_data_segment_align(Expression_ptr, Expression_ptr);
  398. extern Expression_ptr
  399. script_exp_function_data_segment_relro_end(Expression_ptr, Expression_ptr);
  400. extern Expression_ptr
  401. script_exp_function_data_segment_end(Expression_ptr);
  402. extern Expression_ptr
  403. script_exp_function_segment_start(const char*, size_t, Expression_ptr);
  404. extern Expression_ptr
  405. script_exp_function_assert(Expression_ptr, const char*, size_t);
  406. extern void
  407. script_register_vers_node(void* closure,
  408. const char* tag,
  409. int taglen,
  410. struct Version_tree*,
  411. struct Version_dependency_list*);
  412. extern struct Version_dependency_list*
  413. script_add_vers_depend(void* closure,
  414. struct Version_dependency_list* existing_dependencies,
  415. const char* depend_to_add, int deplen);
  416. extern struct Version_expression_list*
  417. script_new_vers_pattern(void* closure,
  418. struct Version_expression_list*,
  419. const char*, int, int);
  420. extern struct Version_expression_list*
  421. script_merge_expressions(struct Version_expression_list* a,
  422. struct Version_expression_list* b);
  423. extern struct Version_tree*
  424. script_new_vers_node(void* closure,
  425. struct Version_expression_list* global,
  426. struct Version_expression_list* local);
  427. extern void
  428. version_script_push_lang(void* closure, const char* lang, int langlen);
  429. extern void
  430. version_script_pop_lang(void* closure);
  431. #ifdef __cplusplus
  432. } // End extern "C"
  433. #endif
  434. #ifdef __cplusplus
  435. } // End namespace gold.
  436. #endif
  437. #endif /* !defined(GOLD_SCRIPT_C_H) */