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