Parse a SECTIONS clause in a linker script.
[external/binutils.git] / gold / script-c.h
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 /* The information we store for an output section header in the bison
65    parser.  */
66
67 struct Parser_output_section_header
68 {
69   /* The address.  This may be NULL.  */
70   Expression_ptr address;
71   /* The load address, from the AT specifier.  This may be NULL.  */
72   Expression_ptr load_address;
73   /* The alignment, from the ALIGN specifier.  This may be NULL.  */
74   Expression_ptr align;
75   /* The input section alignment, from the SUBALIGN specifier.  This
76      may be NULL.  */
77   Expression_ptr subalign;
78 };
79
80 /* The information we store for an output section trailer in the bison
81    parser.  */
82
83 struct Parser_output_section_trailer
84 {
85   /* The fill value.  This may be NULL.  */
86   Expression_ptr fill;
87 };
88
89 /* We keep vectors of strings.  In order to manage this in both C and
90    C++, we use a pointer to a vector.  This assumes that all pointers
91    look the same.  */
92
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
100 /* The different sorts we can find in a linker script.  */
101
102 enum Sort_wildcard
103 {
104   SORT_WILDCARD_NONE,
105   SORT_WILDCARD_BY_NAME,
106   SORT_WILDCARD_BY_ALIGNMENT,
107   SORT_WILDCARD_BY_NAME_BY_ALIGNMENT,
108   SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
109 };
110
111 /* The information we build for a single wildcard specification.  */
112
113 struct Wildcard_section
114 {
115   /* The wildcard spec itself.  */
116   struct Parser_string name;
117   /* How the entries should be sorted.  */
118   enum Sort_wildcard sort;
119 };
120
121 /* A vector of Wildcard_section entries.  */
122
123 #ifdef __cplusplus
124 typedef std::vector<Wildcard_section> String_sort_list;
125 typedef String_sort_list* String_sort_list_ptr;
126 #else
127 typedef void* String_sort_list_ptr;
128 #endif
129
130 /* A list of wildcard specifications, which may include EXCLUDE_FILE
131    clauses.  */
132
133 struct Wildcard_sections
134 {
135   /* Wildcard specs.  */
136   String_sort_list_ptr sections;
137   /* Exclusions.  */
138   String_list_ptr exclude;
139 };
140
141 /* A complete input section specification.  */
142
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
151 struct Version_dependency_list;
152 struct Version_expression_list;
153 struct Version_tree;
154
155 #ifdef __cplusplus
156 extern "C" {
157 #endif
158
159 /* The bison parser definitions.  */
160
161 #include "yyscript.h"
162
163 /* The bison parser function.  */
164
165 extern int
166 yyparse(void* closure);
167
168 /* Called by the bison parser skeleton to return the next token.  */
169
170 extern int
171 yylex(YYSTYPE*, void* closure);
172
173 /* Called by the bison parser skeleton to report an error.  */
174
175 extern void
176 yyerror(void* closure, const char*);
177
178 /* Called by the bison parser to add a file to the link.  */
179
180 extern void
181 script_add_file(void* closure, const char*, size_t);
182
183 /* Called by the bison parser to start and stop a group.  */
184
185 extern void
186 script_start_group(void* closure);
187 extern void
188 script_end_group(void* closure);
189
190 /* Called by the bison parser to start and end an AS_NEEDED list.  */
191
192 extern void
193 script_start_as_needed(void* closure);
194 extern void
195 script_end_as_needed(void* closure);
196
197 /* Called by the bison parser to set the entry symbol.  */
198
199 extern void
200 script_set_entry(void* closure, const char*, size_t);
201
202 /* Called by the bison parser to parse an OPTION.  */
203
204 extern void
205 script_parse_option(void* closure, const char*, size_t);
206
207 /* Called by the bison parser to push the lexer into expression
208    mode.  */
209
210 extern void
211 script_push_lex_into_expression_mode(void* closure);
212
213 /* Called by the bison parser to push the lexer into version
214    mode.  */
215
216 extern void
217 script_push_lex_into_version_mode(void* closure);
218
219 /* Called by the bison parser to pop the lexer mode.  */
220
221 extern void
222 script_pop_lex_mode(void* closure);
223
224 /* Called by the bison parser to set a symbol to a value.  PROVIDE is
225    non-zero if the symbol should be provided--only defined if there is
226    an undefined reference.  HIDDEN is non-zero if the symbol should be
227    hidden.  */
228
229 extern void
230 script_set_symbol(void* closure, const char*, size_t, Expression_ptr,
231                   int provide, int hidden);
232
233 /* Called by the bison parser to add an assertion.  */
234
235 extern void
236 script_add_assertion(void* closure, Expression_ptr, const char* message,
237                      size_t messagelen);
238
239 /* Called by the bison parser to start a SECTIONS clause.  */
240
241 extern void
242 script_start_sections(void* closure);
243
244 /* Called by the bison parser to finish a SECTIONS clause.  */
245
246 extern void
247 script_finish_sections(void* closure);
248
249 /* Called by the bison parser to start handling input section
250    specifications for an output section.  */
251
252 extern void
253 script_start_output_section(void* closure, const char* name, size_t namelen,
254                             const struct Parser_output_section_header*);
255
256 /* Called by the bison parser when done handling input section
257    specifications for an output section.  */
258
259 extern void
260 script_finish_output_section(void* closure,
261                              const struct Parser_output_section_trailer*);
262
263 /* Called by the bison parser to handle a data statement (LONG, BYTE,
264    etc.) in an output section.  */
265
266 extern void
267 script_add_data(void* closure, int data_token, Expression_ptr val);
268
269 /* Called by the bison parser to set the fill value in an output
270    section.  */
271
272 extern void
273 script_add_fill(void* closure, Expression_ptr val);
274
275 /* Called by the bison parser to add an input section specification to
276    an output section.  The KEEP parameter is non-zero if this is
277    within a KEEP clause, meaning that the garbage collector should not
278    discard it.  */
279
280 extern void
281 script_add_input_section(void* closure, const struct Input_section_spec*,
282                          int keep);
283
284 /* Create a new list of string and sort entries.  */
285
286 extern String_sort_list_ptr
287 script_new_string_sort_list(const struct Wildcard_section*);
288
289 /* Add an entry to a list of string and sort entries.  */
290
291 extern String_sort_list_ptr
292 script_string_sort_list_add(String_sort_list_ptr,
293                             const struct Wildcard_section*);
294
295 /* Create a new list of strings.  */
296
297 extern String_list_ptr
298 script_new_string_list(const char*, size_t);
299
300 /* Add an element to a list of strings.  */
301
302 extern String_list_ptr
303 script_string_list_push_back(String_list_ptr, const char*, size_t);
304
305 /* Concatenate two string lists.  */
306
307 extern String_list_ptr
308 script_string_list_append(String_list_ptr, String_list_ptr);
309
310 /* Called by the bison parser for expressions.  */
311
312 extern Expression_ptr
313 script_exp_unary_minus(Expression_ptr);
314 extern Expression_ptr
315 script_exp_unary_logical_not(Expression_ptr);
316 extern Expression_ptr
317 script_exp_unary_bitwise_not(Expression_ptr);
318 extern Expression_ptr
319 script_exp_binary_mult(Expression_ptr, Expression_ptr);
320 extern Expression_ptr
321 script_exp_binary_div(Expression_ptr, Expression_ptr);
322 extern Expression_ptr
323 script_exp_binary_mod(Expression_ptr, Expression_ptr);
324 extern Expression_ptr
325 script_exp_binary_add(Expression_ptr, Expression_ptr);
326 extern Expression_ptr
327 script_exp_binary_sub(Expression_ptr, Expression_ptr);
328 extern Expression_ptr
329 script_exp_binary_lshift(Expression_ptr, Expression_ptr);
330 extern Expression_ptr
331 script_exp_binary_rshift(Expression_ptr, Expression_ptr);
332 extern Expression_ptr
333 script_exp_binary_eq(Expression_ptr, Expression_ptr);
334 extern Expression_ptr
335 script_exp_binary_ne(Expression_ptr, Expression_ptr);
336 extern Expression_ptr
337 script_exp_binary_le(Expression_ptr, Expression_ptr);
338 extern Expression_ptr
339 script_exp_binary_ge(Expression_ptr, Expression_ptr);
340 extern Expression_ptr
341 script_exp_binary_lt(Expression_ptr, Expression_ptr);
342 extern Expression_ptr
343 script_exp_binary_gt(Expression_ptr, Expression_ptr);
344 extern Expression_ptr
345 script_exp_binary_bitwise_and(Expression_ptr, Expression_ptr);
346 extern Expression_ptr
347 script_exp_binary_bitwise_xor(Expression_ptr, Expression_ptr);
348 extern Expression_ptr
349 script_exp_binary_bitwise_or(Expression_ptr, Expression_ptr);
350 extern Expression_ptr
351 script_exp_binary_logical_and(Expression_ptr, Expression_ptr);
352 extern Expression_ptr
353 script_exp_binary_logical_or(Expression_ptr, Expression_ptr);
354 extern Expression_ptr
355 script_exp_trinary_cond(Expression_ptr, Expression_ptr, Expression_ptr);
356 extern Expression_ptr
357 script_exp_integer(uint64_t);
358 extern Expression_ptr
359 script_exp_string(const char*, size_t);
360 extern Expression_ptr
361 script_exp_function_max(Expression_ptr, Expression_ptr);
362 extern Expression_ptr
363 script_exp_function_min(Expression_ptr, Expression_ptr);
364 extern Expression_ptr
365 script_exp_function_defined(const char*, size_t);
366 extern Expression_ptr
367 script_exp_function_sizeof_headers(void);
368 extern Expression_ptr
369 script_exp_function_alignof(const char*, size_t);
370 extern Expression_ptr
371 script_exp_function_sizeof(const char*, size_t);
372 extern Expression_ptr
373 script_exp_function_addr(const char*, size_t);
374 extern Expression_ptr
375 script_exp_function_loadaddr(const char*, size_t);
376 extern Expression_ptr
377 script_exp_function_origin(const char*, size_t);
378 extern Expression_ptr
379 script_exp_function_length(const char*, size_t);
380 extern Expression_ptr
381 script_exp_function_constant(const char*, size_t);
382 extern Expression_ptr
383 script_exp_function_absolute(Expression_ptr);
384 extern Expression_ptr
385 script_exp_function_align(Expression_ptr, Expression_ptr);
386 extern Expression_ptr
387 script_exp_function_data_segment_align(Expression_ptr, Expression_ptr);
388 extern Expression_ptr
389 script_exp_function_data_segment_relro_end(Expression_ptr, Expression_ptr);
390 extern Expression_ptr
391 script_exp_function_data_segment_end(Expression_ptr);
392 extern Expression_ptr
393 script_exp_function_segment_start(const char*, size_t, Expression_ptr);
394 extern Expression_ptr
395 script_exp_function_assert(Expression_ptr, const char*, size_t);
396
397 extern void
398 script_register_vers_node(void* closure,
399                           const char* tag,
400                           int taglen,
401                           struct Version_tree *,
402                           struct Version_dependency_list *);
403
404 extern struct Version_dependency_list *
405 script_add_vers_depend(void* closure,
406                        struct Version_dependency_list *existing_dependencies,
407                        const char *depend_to_add, int deplen);
408
409 extern struct Version_expression_list *
410 script_new_vers_pattern(void* closure,
411                         struct Version_expression_list *,
412                         const char *, int, int);
413
414 extern struct Version_expression_list *
415 script_merge_expressions(struct Version_expression_list *a,
416                          struct Version_expression_list *b);
417
418 extern struct Version_tree *
419 script_new_vers_node(void* closure,
420                      struct Version_expression_list *global,
421                      struct Version_expression_list *local);
422
423 extern void
424 version_script_push_lang(void* closure, const char* lang, int langlen);
425
426 extern void
427 version_script_pop_lang(void* closure);
428
429 #ifdef __cplusplus
430 } // End extern "C"
431 #endif
432
433 #ifdef __cplusplus
434 } // End namespace gold.
435 #endif
436
437 #endif /* !defined(GOLD_SCRIPT_C_H) */