Home | History | Annotate | Download | only in gold
      1 /* script-c.h -- C interface for linker scripts in gold.  */
      2 
      3 /* Copyright (C) 2006-2016 Free Software Foundation, Inc.
      4    Written by Ian Lance Taylor <iant (at) 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 /* Script_section type.  */
     65 enum Script_section_type
     66 {
     67   /* No section type.  */
     68   SCRIPT_SECTION_TYPE_NONE,
     69   SCRIPT_SECTION_TYPE_NOLOAD,
     70   SCRIPT_SECTION_TYPE_DSECT,
     71   SCRIPT_SECTION_TYPE_COPY,
     72   SCRIPT_SECTION_TYPE_INFO,
     73   SCRIPT_SECTION_TYPE_OVERLAY
     74 };
     75 
     76 /* A constraint for whether to use a particular output section
     77    definition.  */
     78 
     79 enum Section_constraint
     80 {
     81   /* No constraint.  */
     82   CONSTRAINT_NONE,
     83   /* Only if all input sections are read-only.  */
     84   CONSTRAINT_ONLY_IF_RO,
     85   /* Only if at least input section is writable.  */
     86   CONSTRAINT_ONLY_IF_RW,
     87   /* Special constraint.  */
     88   CONSTRAINT_SPECIAL
     89 };
     90 
     91 /* The information we store for an output section header in the bison
     92    parser.  */
     93 
     94 struct Parser_output_section_header
     95 {
     96   /* The address.  This may be NULL.  */
     97   Expression_ptr address;
     98   /* Section type.  May be NULL string.  */
     99   enum Script_section_type section_type;
    100   /* The load address, from the AT specifier.  This may be NULL.  */
    101   Expression_ptr load_address;
    102   /* The alignment, from the ALIGN specifier.  This may be NULL.  */
    103   Expression_ptr align;
    104   /* The input section alignment, from the SUBALIGN specifier.  This
    105      may be NULL.  */
    106   Expression_ptr subalign;
    107   /* A constraint on this output section.  */
    108   enum Section_constraint constraint;
    109 };
    110 
    111 /* We keep vectors of strings.  In order to manage this in both C and
    112    C++, we use a pointer to a vector.  This assumes that all pointers
    113    look the same.  */
    114 
    115 #ifdef __cplusplus
    116 typedef std::vector<std::string> String_list;
    117 typedef String_list* String_list_ptr;
    118 #else
    119 typedef void* String_list_ptr;
    120 #endif
    121 
    122 /* The information we store for an output section trailer in the bison
    123    parser.  */
    124 
    125 struct Parser_output_section_trailer
    126 {
    127   /* The fill value.  This may be NULL.  */
    128   Expression_ptr fill;
    129   /* The program segments this section should go into.  This may be
    130      NULL.  */
    131   String_list_ptr phdrs;
    132 };
    133 
    134 /* The different sorts we can find in a linker script.  */
    135 
    136 enum Sort_wildcard
    137 {
    138   SORT_WILDCARD_NONE,
    139   SORT_WILDCARD_BY_NAME,
    140   SORT_WILDCARD_BY_ALIGNMENT,
    141   SORT_WILDCARD_BY_NAME_BY_ALIGNMENT,
    142   SORT_WILDCARD_BY_ALIGNMENT_BY_NAME,
    143   SORT_WILDCARD_BY_INIT_PRIORITY
    144 };
    145 
    146 /* The information we build for a single wildcard specification.  */
    147 
    148 struct Wildcard_section
    149 {
    150   /* The wildcard spec itself.  */
    151   struct Parser_string name;
    152   /* How the entries should be sorted.  */
    153   enum Sort_wildcard sort;
    154 };
    155 
    156 /* A vector of Wildcard_section entries.  */
    157 
    158 #ifdef __cplusplus
    159 typedef std::vector<Wildcard_section> String_sort_list;
    160 typedef String_sort_list* String_sort_list_ptr;
    161 #else
    162 typedef void* String_sort_list_ptr;
    163 #endif
    164 
    165 /* A list of wildcard specifications, which may include EXCLUDE_FILE
    166    clauses.  */
    167 
    168 struct Wildcard_sections
    169 {
    170   /* Wildcard specs.  */
    171   String_sort_list_ptr sections;
    172   /* Exclusions.  */
    173   String_list_ptr exclude;
    174 };
    175 
    176 /* A complete input section specification.  */
    177 
    178 struct Input_section_spec
    179 {
    180   /* The file name.  */
    181   struct Wildcard_section file;
    182   /* The list of sections.  */
    183   struct Wildcard_sections input_sections;
    184 };
    185 
    186 /* Information for a program header.  */
    187 
    188 struct Phdr_info
    189 {
    190   /* A boolean value: whether to include the file header.  */
    191   int includes_filehdr;
    192   /* A boolean value: whether to include the program headers.  */
    193   int includes_phdrs;
    194   /* A boolean value: whether the flags field is valid.  */
    195   int is_flags_valid;
    196   /* The value to use for the flags.  */
    197   unsigned int flags;
    198   /* The load address.  */
    199   Expression_ptr load_address;
    200 };
    201 
    202 struct Version_dependency_list;
    203 struct Version_expression_list;
    204 struct Version_tree;
    205 
    206 #ifdef __cplusplus
    207 extern "C" {
    208 #endif
    209 
    210 /* The bison parser definitions.  */
    211 
    212 #include "yyscript.h"
    213 
    214 /* The bison parser function.  */
    215 
    216 extern int
    217 yyparse(void* closure);
    218 
    219 /* Called by the bison parser skeleton to return the next token.  */
    220 
    221 extern int
    222 yylex(YYSTYPE*, void* closure);
    223 
    224 /* Called by the bison parser skeleton to report an error.  */
    225 
    226 extern void
    227 yyerror(void* closure, const char*);
    228 
    229 /* Called by the bison parser to add an external symbol (a symbol in
    230    an EXTERN declaration) to the link.  */
    231 
    232 extern void
    233 script_add_extern(void* closure, const char*, size_t);
    234 
    235 /* Called by the bison parser to add a file to the link.  */
    236 
    237 extern void
    238 script_add_file(void* closure, const char*, size_t);
    239 
    240 /* Called by the bison parser to add a library to the link.  */
    241 
    242 extern void
    243 script_add_library(void* closure, const char*, size_t);
    244 
    245 /* Called by the bison parser to start and stop a group.  */
    246 
    247 extern void
    248 script_start_group(void* closure);
    249 extern void
    250 script_end_group(void* closure);
    251 
    252 /* Called by the bison parser to start and end an AS_NEEDED list.  */
    253 
    254 extern void
    255 script_start_as_needed(void* closure);
    256 extern void
    257 script_end_as_needed(void* closure);
    258 
    259 /* Called by the bison parser to set the entry symbol.  */
    260 
    261 extern void
    262 script_set_entry(void* closure, const char*, size_t);
    263 
    264 /* Called by the bison parser to set whether to define common symbols.  */
    265 
    266 extern void
    267 script_set_common_allocation(void* closure, int);
    268 
    269 /* Called by the bison parser to parse an OPTION.  */
    270 
    271 extern void
    272 script_parse_option(void* closure, const char*, size_t);
    273 
    274 /* Called by the bison parser to handle OUTPUT_FORMAT.  This return 0
    275    if the parse should be aborted.  */
    276 
    277 extern int
    278 script_check_output_format(void* closure, const char*, size_t,
    279 			   const char*, size_t, const char*, size_t);
    280 
    281 /* Called by the bison parser to handle TARGET.  */
    282 extern void
    283 script_set_target(void* closure, const char*, size_t);
    284 
    285 /* Called by the bison parser to handle SEARCH_DIR.  */
    286 
    287 extern void
    288 script_add_search_dir(void* closure, const char*, size_t);
    289 
    290 /* Called by the bison parser to push the lexer into expression
    291    mode.  */
    292 
    293 extern void
    294 script_push_lex_into_expression_mode(void* closure);
    295 
    296 /* Called by the bison parser to push the lexer into version
    297    mode.  */
    298 
    299 extern void
    300 script_push_lex_into_version_mode(void* closure);
    301 
    302 /* Called by the bison parser to pop the lexer mode.  */
    303 
    304 extern void
    305 script_pop_lex_mode(void* closure);
    306 
    307 /* Called by the bison parser to get the value of a symbol.  This is
    308    called for a reference to a symbol, but is not called for something
    309    like "sym += 10".  Uses of the special symbol "." can just call
    310    script_exp_string.  */
    311 
    312 extern Expression_ptr
    313 script_symbol(void* closure, const char*, size_t);
    314 
    315 /* Called by the bison parser to set a symbol to a value.  PROVIDE is
    316    non-zero if the symbol should be provided--only defined if there is
    317    an undefined reference.  HIDDEN is non-zero if the symbol should be
    318    hidden.  */
    319 
    320 extern void
    321 script_set_symbol(void* closure, const char*, size_t, Expression_ptr,
    322 		  int provide, int hidden);
    323 
    324 /* Called by the bison parser to add an assertion.  */
    325 
    326 extern void
    327 script_add_assertion(void* closure, Expression_ptr, const char* message,
    328 		     size_t messagelen);
    329 
    330 /* Called by the bison parser to start a SECTIONS clause.  */
    331 
    332 extern void
    333 script_start_sections(void* closure);
    334 
    335 /* Called by the bison parser to finish a SECTIONS clause.  */
    336 
    337 extern void
    338 script_finish_sections(void* closure);
    339 
    340 /* Called by the bison parser to start handling input section
    341    specifications for an output section.  */
    342 
    343 extern void
    344 script_start_output_section(void* closure, const char* name, size_t namelen,
    345 			    const struct Parser_output_section_header*);
    346 
    347 /* Called by the bison parser when done handling input section
    348    specifications for an output section.  */
    349 
    350 extern void
    351 script_finish_output_section(void* closure,
    352 			     const struct Parser_output_section_trailer*);
    353 
    354 /* Called by the bison parser to handle a data statement (LONG, BYTE,
    355    etc.) in an output section.  */
    356 
    357 extern void
    358 script_add_data(void* closure, int data_token, Expression_ptr val);
    359 
    360 /* Called by the bison parser to set the fill value in an output
    361    section.  */
    362 
    363 extern void
    364 script_add_fill(void* closure, Expression_ptr val);
    365 
    366 /* Called by the bison parser to add an input section specification to
    367    an output section.  The KEEP parameter is non-zero if this is
    368    within a KEEP clause, meaning that the garbage collector should not
    369    discard it.  */
    370 
    371 extern void
    372 script_add_input_section(void* closure, const struct Input_section_spec*,
    373 			 int keep);
    374 
    375 /* Create a new list of string and sort entries.  */
    376 
    377 extern String_sort_list_ptr
    378 script_new_string_sort_list(const struct Wildcard_section*);
    379 
    380 /* Add an entry to a list of string and sort entries.  */
    381 
    382 extern String_sort_list_ptr
    383 script_string_sort_list_add(String_sort_list_ptr,
    384 			    const struct Wildcard_section*);
    385 
    386 /* Create a new list of strings.  */
    387 
    388 extern String_list_ptr
    389 script_new_string_list(const char*, size_t);
    390 
    391 /* Add an element to a list of strings.  */
    392 
    393 extern String_list_ptr
    394 script_string_list_push_back(String_list_ptr, const char*, size_t);
    395 
    396 /* Concatenate two string lists.  */
    397 
    398 extern String_list_ptr
    399 script_string_list_append(String_list_ptr, String_list_ptr);
    400 
    401 /* Define a new program header.  */
    402 
    403 extern void
    404 script_add_phdr(void* closure, const char* name, size_t namelen,
    405 		unsigned int type, const struct Phdr_info*);
    406 
    407 /* Convert a program header string to a type.  */
    408 
    409 extern unsigned int
    410 script_phdr_string_to_type(void* closure, const char*, size_t);
    411 
    412 /* Handle DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END.  */
    413 
    414 extern void
    415 script_data_segment_align(void* closure);
    416 
    417 extern void
    418 script_data_segment_relro_end(void* closure);
    419 
    420 /* Record the fact that a SEGMENT_START expression is seen.  */
    421 
    422 extern void
    423 script_saw_segment_start_expression(void* closure);
    424 
    425 /* Called by the bison parser for MEMORY regions.  */
    426 
    427 extern void
    428 script_add_memory(void*, const char*, size_t, unsigned int,
    429 		  Expression_ptr, Expression_ptr);
    430 
    431 extern unsigned int
    432 script_parse_memory_attr(void*, const char*, size_t, int);
    433 
    434 extern void
    435 script_set_section_region(void*, const char*, size_t, int);
    436 
    437 extern void
    438 script_include_directive(int, void *, const char*, size_t);
    439 
    440 /* Called by the bison parser for expressions.  */
    441 
    442 extern Expression_ptr
    443 script_exp_unary_minus(Expression_ptr);
    444 extern Expression_ptr
    445 script_exp_unary_logical_not(Expression_ptr);
    446 extern Expression_ptr
    447 script_exp_unary_bitwise_not(Expression_ptr);
    448 extern Expression_ptr
    449 script_exp_binary_mult(Expression_ptr, Expression_ptr);
    450 extern Expression_ptr
    451 script_exp_binary_div(Expression_ptr, Expression_ptr);
    452 extern Expression_ptr
    453 script_exp_binary_mod(Expression_ptr, Expression_ptr);
    454 extern Expression_ptr
    455 script_exp_binary_add(Expression_ptr, Expression_ptr);
    456 extern Expression_ptr
    457 script_exp_binary_sub(Expression_ptr, Expression_ptr);
    458 extern Expression_ptr
    459 script_exp_binary_lshift(Expression_ptr, Expression_ptr);
    460 extern Expression_ptr
    461 script_exp_binary_rshift(Expression_ptr, Expression_ptr);
    462 extern Expression_ptr
    463 script_exp_binary_eq(Expression_ptr, Expression_ptr);
    464 extern Expression_ptr
    465 script_exp_binary_ne(Expression_ptr, Expression_ptr);
    466 extern Expression_ptr
    467 script_exp_binary_le(Expression_ptr, Expression_ptr);
    468 extern Expression_ptr
    469 script_exp_binary_ge(Expression_ptr, Expression_ptr);
    470 extern Expression_ptr
    471 script_exp_binary_lt(Expression_ptr, Expression_ptr);
    472 extern Expression_ptr
    473 script_exp_binary_gt(Expression_ptr, Expression_ptr);
    474 extern Expression_ptr
    475 script_exp_binary_bitwise_and(Expression_ptr, Expression_ptr);
    476 extern Expression_ptr
    477 script_exp_binary_bitwise_xor(Expression_ptr, Expression_ptr);
    478 extern Expression_ptr
    479 script_exp_binary_bitwise_or(Expression_ptr, Expression_ptr);
    480 extern Expression_ptr
    481 script_exp_binary_logical_and(Expression_ptr, Expression_ptr);
    482 extern Expression_ptr
    483 script_exp_binary_logical_or(Expression_ptr, Expression_ptr);
    484 extern Expression_ptr
    485 script_exp_trinary_cond(Expression_ptr, Expression_ptr, Expression_ptr);
    486 extern Expression_ptr
    487 script_exp_integer(uint64_t);
    488 extern Expression_ptr
    489 script_exp_string(const char*, size_t);
    490 extern Expression_ptr
    491 script_exp_function_max(Expression_ptr, Expression_ptr);
    492 extern Expression_ptr
    493 script_exp_function_min(Expression_ptr, Expression_ptr);
    494 extern Expression_ptr
    495 script_exp_function_defined(const char*, size_t);
    496 extern Expression_ptr
    497 script_exp_function_sizeof_headers(void);
    498 extern Expression_ptr
    499 script_exp_function_alignof(const char*, size_t);
    500 extern Expression_ptr
    501 script_exp_function_sizeof(const char*, size_t);
    502 extern Expression_ptr
    503 script_exp_function_addr(const char*, size_t);
    504 extern Expression_ptr
    505 script_exp_function_loadaddr(const char*, size_t);
    506 extern Expression_ptr
    507 script_exp_function_origin(void*, const char*, size_t);
    508 extern Expression_ptr
    509 script_exp_function_length(void*, const char*, size_t);
    510 extern Expression_ptr
    511 script_exp_function_constant(const char*, size_t);
    512 extern Expression_ptr
    513 script_exp_function_absolute(Expression_ptr);
    514 extern Expression_ptr
    515 script_exp_function_align(Expression_ptr, Expression_ptr);
    516 extern Expression_ptr
    517 script_exp_function_data_segment_align(Expression_ptr, Expression_ptr);
    518 extern Expression_ptr
    519 script_exp_function_data_segment_relro_end(Expression_ptr, Expression_ptr);
    520 extern Expression_ptr
    521 script_exp_function_data_segment_end(Expression_ptr);
    522 extern Expression_ptr
    523 script_exp_function_segment_start(const char*, size_t, Expression_ptr);
    524 extern Expression_ptr
    525 script_exp_function_assert(Expression_ptr, const char*, size_t);
    526 
    527 extern void
    528 script_register_vers_node(void* closure,
    529 			  const char* tag,
    530 			  int taglen,
    531 			  struct Version_tree*,
    532 			  struct Version_dependency_list*);
    533 
    534 extern struct Version_dependency_list*
    535 script_add_vers_depend(void* closure,
    536 		       struct Version_dependency_list* existing_dependencies,
    537 		       const char* depend_to_add, int deplen);
    538 
    539 extern struct Version_expression_list*
    540 script_new_vers_pattern(void* closure,
    541 			struct Version_expression_list*,
    542 			const char*, int, int);
    543 
    544 extern struct Version_expression_list*
    545 script_merge_expressions(struct Version_expression_list* a,
    546                          struct Version_expression_list* b);
    547 
    548 extern struct Version_tree*
    549 script_new_vers_node(void* closure,
    550 		     struct Version_expression_list* global,
    551 		     struct Version_expression_list* local);
    552 
    553 extern void
    554 version_script_push_lang(void* closure, const char* lang, int langlen);
    555 
    556 extern void
    557 version_script_pop_lang(void* closure);
    558 
    559 #ifdef __cplusplus
    560 } // End extern "C"
    561 #endif
    562 
    563 #ifdef __cplusplus
    564 } // End namespace gold.
    565 #endif
    566 
    567 #endif /* !defined(GOLD_SCRIPT_C_H) */
    568