1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
30 #include "script-sections.h"
32 // Support for the SECTIONS clause in linker scripts.
37 // An element in a SECTIONS clause.
39 class Sections_element
45 virtual ~Sections_element()
49 print(FILE* f) const = 0;
52 // An assignment in a SECTIONS clause outside of an output section.
54 class Sections_element_assignment : public Sections_element
57 Sections_element_assignment(const char* name, size_t namelen,
58 Expression* val, bool provide, bool hidden)
59 : assignment_(name, namelen, val, provide, hidden)
66 this->assignment_.print(f);
70 Symbol_assignment assignment_;
73 // An assertion in a SECTIONS clause outside of an output section.
75 class Sections_element_assertion : public Sections_element
78 Sections_element_assertion(Expression* check, const char* message,
80 : assertion_(check, message, messagelen)
87 this->assertion_.print(f);
91 Script_assertion assertion_;
94 // An element in an output section in a SECTIONS clause.
96 class Output_section_element
99 Output_section_element()
102 virtual ~Output_section_element()
106 print(FILE* f) const = 0;
109 // A symbol assignment in an output section.
111 class Output_section_element_assignment : public Output_section_element
114 Output_section_element_assignment(const char* name, size_t namelen,
115 Expression* val, bool provide,
117 : assignment_(name, namelen, val, provide, hidden)
124 this->assignment_.print(f);
128 Symbol_assignment assignment_;
131 // An assertion in an output section.
133 class Output_section_element_assertion : public Output_section_element
136 Output_section_element_assertion(Expression* check, const char* message,
138 : assertion_(check, message, messagelen)
145 this->assertion_.print(f);
149 Script_assertion assertion_;
152 // A data item in an output section.
154 class Output_section_element_data : public Output_section_element
157 Output_section_element_data(int size, bool is_signed, Expression* val)
158 : size_(size), is_signed_(is_signed), val_(val)
165 // The size in bytes.
167 // Whether the value is signed.
173 // Print for debugging.
176 Output_section_element_data::print(FILE* f) const
191 if (this->is_signed_)
199 fprintf(f, " %s(", s);
200 this->val_->print(f);
204 // A fill value setting in an output section.
206 class Output_section_element_fill : public Output_section_element
209 Output_section_element_fill(Expression* val)
216 fprintf(f, " FILL(");
217 this->val_->print(f);
222 // The new fill value.
226 // An input section specification in an output section
228 class Output_section_element_input : public Output_section_element
231 // Note that an Input_section_spec holds some pointers to vectors.
232 // This constructor takes ownership of them. The parser is
233 // implemented such that this works.
234 Output_section_element_input(const Input_section_spec* spec, bool keep);
237 print(FILE* f) const;
240 // An input section pattern.
241 struct Input_section_pattern
246 Input_section_pattern(const char* patterna, size_t patternlena,
248 : pattern(patterna, patternlena), sort(sorta)
252 typedef std::vector<Input_section_pattern> Input_section_patterns;
254 typedef std::vector<std::string> Filename_exclusions;
256 // The file name pattern.
257 std::string filename_pattern_;
258 // How the file names should be sorted. This may only be
259 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
260 Sort_wildcard filename_sort_;
261 // The list of file names to exclude.
262 Filename_exclusions filename_exclusions_;
263 // The list of input section patterns.
264 Input_section_patterns input_section_patterns_;
265 // Whether to keep this section when garbage collecting.
269 // Construct Output_section_element_input. The parser records strings
270 // as pointers into a copy of the script file, which will go away when
271 // parsing is complete. We make sure they are in std::string objects.
273 Output_section_element_input::Output_section_element_input(
274 const Input_section_spec* spec,
276 : filename_pattern_(spec->file.name.value, spec->file.name.length),
277 filename_sort_(spec->file.sort),
278 filename_exclusions_(),
279 input_section_patterns_(),
282 if (spec->input_sections.exclude != NULL)
284 for (String_list::const_iterator p =
285 spec->input_sections.exclude->begin();
286 p != spec->input_sections.exclude->end();
288 this->filename_exclusions_.push_back(*p);
291 if (spec->input_sections.sections != NULL)
293 Input_section_patterns& isp(this->input_section_patterns_);
294 for (String_sort_list::const_iterator p =
295 spec->input_sections.sections->begin();
296 p != spec->input_sections.sections->end();
298 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
303 // Print for debugging.
306 Output_section_element_input::print(FILE* f) const
313 if (!this->filename_pattern_.empty())
315 bool need_close_paren = false;
316 switch (this->filename_sort_)
318 case SORT_WILDCARD_NONE:
320 case SORT_WILDCARD_BY_NAME:
321 fprintf(f, "SORT_BY_NAME(");
322 need_close_paren = true;
328 fprintf(f, "%s", this->filename_pattern_.c_str());
330 if (need_close_paren)
334 if (!this->input_section_patterns_.empty()
335 || !this->filename_exclusions_.empty())
339 bool need_space = false;
340 if (!this->filename_exclusions_.empty())
342 fprintf(f, "EXCLUDE_FILE(");
343 bool need_comma = false;
344 for (Filename_exclusions::const_iterator p =
345 this->filename_exclusions_.begin();
346 p != this->filename_exclusions_.end();
351 fprintf(f, "%s", p->c_str());
358 for (Input_section_patterns::const_iterator p =
359 this->input_section_patterns_.begin();
360 p != this->input_section_patterns_.end();
366 int close_parens = 0;
369 case SORT_WILDCARD_NONE:
371 case SORT_WILDCARD_BY_NAME:
372 fprintf(f, "SORT_BY_NAME(");
375 case SORT_WILDCARD_BY_ALIGNMENT:
376 fprintf(f, "SORT_BY_ALIGNMENT(");
379 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
380 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
383 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
384 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
391 fprintf(f, "%s", p->pattern.c_str());
393 for (int i = 0; i < close_parens; ++i)
408 // An output section.
410 class Output_section_definition : public Sections_element
413 Output_section_definition(const char* name, size_t namelen,
414 const Parser_output_section_header* header);
416 // Finish the output section with the information in the trailer.
418 finish(const Parser_output_section_trailer* trailer);
420 // Add a symbol to be defined.
422 add_symbol_assignment(const char* name, size_t length, Expression* value,
423 bool provide, bool hidden);
426 add_assertion(Expression* check, const char* message, size_t messagelen);
428 // Add a data item to the current output section.
430 add_data(int size, bool is_signed, Expression* val);
432 // Add a setting for the fill value.
434 add_fill(Expression* val);
436 // Add an input section specification.
438 add_input_section(const Input_section_spec* spec, bool keep);
440 // Print the contents to the FILE. This is for debugging.
445 typedef std::vector<Output_section_element*> Output_section_elements;
447 // The output section name.
449 // The address. This may be NULL.
450 Expression* address_;
451 // The load address. This may be NULL.
452 Expression* load_address_;
453 // The alignment. This may be NULL.
455 // The input section alignment. This may be NULL.
456 Expression* subalign_;
457 // The fill value. This may be NULL.
459 // The list of elements defining the section.
460 Output_section_elements elements_;
465 Output_section_definition::Output_section_definition(
468 const Parser_output_section_header* header)
469 : name_(name, namelen),
470 address_(header->address),
471 load_address_(header->load_address),
472 align_(header->align),
473 subalign_(header->subalign),
479 // Finish an output section.
482 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
484 this->fill_ = trailer->fill;
487 // Add a symbol to be defined.
490 Output_section_definition::add_symbol_assignment(const char* name,
496 Output_section_element* p = new Output_section_element_assignment(name,
501 this->elements_.push_back(p);
507 Output_section_definition::add_assertion(Expression* check,
511 Output_section_element* p = new Output_section_element_assertion(check,
514 this->elements_.push_back(p);
517 // Add a data item to the current output section.
520 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
522 Output_section_element* p = new Output_section_element_data(size, is_signed,
524 this->elements_.push_back(p);
527 // Add a setting for the fill value.
530 Output_section_definition::add_fill(Expression* val)
532 Output_section_element* p = new Output_section_element_fill(val);
533 this->elements_.push_back(p);
536 // Add an input section specification.
539 Output_section_definition::add_input_section(const Input_section_spec* spec,
542 Output_section_element* p = new Output_section_element_input(spec, keep);
543 this->elements_.push_back(p);
546 // Print for debugging.
549 Output_section_definition::print(FILE* f) const
551 fprintf(f, " %s ", this->name_.c_str());
553 if (this->address_ != NULL)
555 this->address_->print(f);
561 if (this->load_address_ != NULL)
564 this->load_address_->print(f);
568 if (this->align_ != NULL)
570 fprintf(f, "ALIGN(");
571 this->align_->print(f);
575 if (this->subalign_ != NULL)
577 fprintf(f, "SUBALIGN(");
578 this->subalign_->print(f);
584 for (Output_section_elements::const_iterator p = this->elements_.begin();
585 p != this->elements_.end();
591 if (this->fill_ != NULL)
594 this->fill_->print(f);
600 // Class Script_sections.
602 Script_sections::Script_sections()
603 : saw_sections_clause_(false),
604 in_sections_clause_(false),
605 sections_elements_(NULL),
606 output_section_(NULL)
610 // Start a SECTIONS clause.
613 Script_sections::start_sections()
615 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
616 this->saw_sections_clause_ = true;
617 this->in_sections_clause_ = true;
618 if (this->sections_elements_ == NULL)
619 this->sections_elements_ = new Sections_elements;
622 // Finish a SECTIONS clause.
625 Script_sections::finish_sections()
627 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
628 this->in_sections_clause_ = false;
631 // Add a symbol to be defined.
634 Script_sections::add_symbol_assignment(const char* name, size_t length,
635 Expression* val, bool provide,
638 if (this->output_section_ != NULL)
639 this->output_section_->add_symbol_assignment(name, length, val,
643 Sections_element* p = new Sections_element_assignment(name, length,
646 this->sections_elements_->push_back(p);
653 Script_sections::add_assertion(Expression* check, const char* message,
656 if (this->output_section_ != NULL)
657 this->output_section_->add_assertion(check, message, messagelen);
660 Sections_element* p = new Sections_element_assertion(check, message,
662 this->sections_elements_->push_back(p);
666 // Start processing entries for an output section.
669 Script_sections::start_output_section(
672 const Parser_output_section_header *header)
674 Output_section_definition* posd = new Output_section_definition(name,
677 this->sections_elements_->push_back(posd);
678 gold_assert(this->output_section_ == NULL);
679 this->output_section_ = posd;
682 // Stop processing entries for an output section.
685 Script_sections::finish_output_section(
686 const Parser_output_section_trailer* trailer)
688 gold_assert(this->output_section_ != NULL);
689 this->output_section_->finish(trailer);
690 this->output_section_ = NULL;
693 // Add a data item to the current output section.
696 Script_sections::add_data(int size, bool is_signed, Expression* val)
698 gold_assert(this->output_section_ != NULL);
699 this->output_section_->add_data(size, is_signed, val);
702 // Add a fill value setting to the current output section.
705 Script_sections::add_fill(Expression* val)
707 gold_assert(this->output_section_ != NULL);
708 this->output_section_->add_fill(val);
711 // Add an input section specification to the current output section.
714 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
716 gold_assert(this->output_section_ != NULL);
717 this->output_section_->add_input_section(spec, keep);
720 // Print the SECTIONS clause to F for debugging.
723 Script_sections::print(FILE* f) const
725 if (!this->saw_sections_clause_)
728 fprintf(f, "SECTIONS {\n");
730 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
731 p != this->sections_elements_->end();
738 } // End namespace gold.