+ {
+ if ((*p)->match_name(file_name, section_name))
+ {
+ // We found a match for NAME, which means that it should go
+ // into this output section.
+ *slot = &this->output_section_;
+ *psection_type = this->section_type();
+ return this->name_.c_str();
+ }
+ }
+
+ // We don't know about this section name.
+ return NULL;
+}
+
+// Set the section address. Note that the OUTPUT_SECTION_ field will
+// be NULL if no input sections were mapped to this output section.
+// We still have to adjust dot and process symbol assignments.
+
+void
+Output_section_definition::set_section_addresses(Symbol_table* symtab,
+ Layout* layout,
+ uint64_t* dot_value,
+ uint64_t* load_address)
+{
+ uint64_t address;
+ uint64_t old_dot_value = *dot_value;
+ uint64_t old_load_address = *load_address;
+
+ if (this->address_ == NULL)
+ address = *dot_value;
+ else
+ {
+ Output_section* dummy;
+ address = this->address_->eval_with_dot(symtab, layout, true,
+ *dot_value, NULL, &dummy);
+ }
+
+ uint64_t align;
+ if (this->align_ == NULL)
+ {
+ if (this->output_section_ == NULL)
+ align = 0;
+ else
+ align = this->output_section_->addralign();
+ }
+ else
+ {
+ Output_section* align_section;
+ align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
+ NULL, &align_section);
+ if (align_section != NULL)
+ gold_warning(_("alignment of section %s is not absolute"),
+ this->name_.c_str());
+ if (this->output_section_ != NULL)
+ this->output_section_->set_addralign(align);
+ }
+
+ address = align_address(address, align);
+
+ uint64_t start_address = address;
+
+ *dot_value = address;
+
+ // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
+ // forced to zero, regardless of what the linker script wants.
+ if (this->output_section_ != NULL
+ && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
+ || this->output_section_->is_noload()))
+ this->output_section_->set_address(address);
+
+ this->evaluated_address_ = address;
+ this->evaluated_addralign_ = align;
+
+ if (this->load_address_ == NULL)
+ this->evaluated_load_address_ = address;
+ else
+ {
+ Output_section* dummy;
+ uint64_t laddr =
+ this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
+ this->output_section_, &dummy);
+ if (this->output_section_ != NULL)
+ this->output_section_->set_load_address(laddr);
+ this->evaluated_load_address_ = laddr;
+ }
+
+ uint64_t subalign;
+ if (this->subalign_ == NULL)
+ subalign = 0;
+ else
+ {
+ Output_section* subalign_section;
+ subalign = this->subalign_->eval_with_dot(symtab, layout, true,
+ *dot_value, NULL,
+ &subalign_section);
+ if (subalign_section != NULL)
+ gold_warning(_("subalign of section %s is not absolute"),
+ this->name_.c_str());
+ }
+
+ std::string fill;
+ if (this->fill_ != NULL)
+ {
+ // FIXME: The GNU linker supports fill values of arbitrary
+ // length.
+ Output_section* fill_section;
+ uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
+ *dot_value,
+ NULL,
+ &fill_section);
+ if (fill_section != NULL)
+ gold_warning(_("fill of section %s is not absolute"),
+ this->name_.c_str());
+ unsigned char fill_buff[4];
+ elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
+ fill.assign(reinterpret_cast<char*>(fill_buff), 4);
+ }
+
+ Input_section_list input_sections;
+ if (this->output_section_ != NULL)
+ {
+ // Get the list of input sections attached to this output
+ // section. This will leave the output section with only
+ // Output_section_data entries.
+ address += this->output_section_->get_input_sections(address,
+ fill,
+ &input_sections);
+ *dot_value = address;
+ }
+
+ Output_section* dot_section = this->output_section_;
+ for (Output_section_elements::iterator p = this->elements_.begin();
+ p != this->elements_.end();
+ ++p)
+ (*p)->set_section_addresses(symtab, layout, this->output_section_,
+ subalign, dot_value, &dot_section, &fill,
+ &input_sections);
+
+ gold_assert(input_sections.empty());
+
+ if (this->load_address_ == NULL || this->output_section_ == NULL)
+ *load_address = *dot_value;
+ else
+ *load_address = (this->output_section_->load_address()
+ + (*dot_value - start_address));
+
+ if (this->output_section_ != NULL)
+ {
+ if (this->is_relro_)
+ this->output_section_->set_is_relro();
+ else
+ this->output_section_->clear_is_relro();
+
+ // If this is a NOLOAD section, keep dot and load address unchanged.
+ if (this->output_section_->is_noload())
+ {
+ *dot_value = old_dot_value;
+ *load_address = old_load_address;
+ }
+ }
+}
+
+// Check a constraint (ONLY_IF_RO, etc.) on an output section. If
+// this section is constrained, and the input sections do not match,
+// return the constraint, and set *POSD.
+
+Section_constraint
+Output_section_definition::check_constraint(Output_section_definition** posd)
+{
+ switch (this->constraint_)
+ {
+ case CONSTRAINT_NONE:
+ return CONSTRAINT_NONE;
+
+ case CONSTRAINT_ONLY_IF_RO:
+ if (this->output_section_ != NULL
+ && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
+ {
+ *posd = this;
+ return CONSTRAINT_ONLY_IF_RO;
+ }
+ return CONSTRAINT_NONE;
+
+ case CONSTRAINT_ONLY_IF_RW:
+ if (this->output_section_ != NULL
+ && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
+ {
+ *posd = this;
+ return CONSTRAINT_ONLY_IF_RW;
+ }
+ return CONSTRAINT_NONE;
+
+ case CONSTRAINT_SPECIAL:
+ if (this->output_section_ != NULL)
+ gold_error(_("SPECIAL constraints are not implemented"));
+ return CONSTRAINT_NONE;
+
+ default:
+ gold_unreachable();
+ }
+}
+
+// See if this is the alternate output section for a constrained
+// output section. If it is, transfer the Output_section and return
+// true. Otherwise return false.
+
+bool
+Output_section_definition::alternate_constraint(
+ Output_section_definition* posd,
+ Section_constraint constraint)
+{
+ if (this->name_ != posd->name_)
+ return false;
+
+ switch (constraint)
+ {
+ case CONSTRAINT_ONLY_IF_RO:
+ if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
+ return false;
+ break;
+
+ case CONSTRAINT_ONLY_IF_RW:
+ if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
+ return false;
+ break;
+
+ default:
+ gold_unreachable();
+ }
+
+ // We have found the alternate constraint. We just need to move
+ // over the Output_section. When constraints are used properly,
+ // THIS should not have an output_section pointer, as all the input
+ // sections should have matched the other definition.
+
+ if (this->output_section_ != NULL)
+ gold_error(_("mismatched definition for constrained sections"));
+
+ this->output_section_ = posd->output_section_;
+ posd->output_section_ = NULL;
+
+ if (this->is_relro_)
+ this->output_section_->set_is_relro();
+ else
+ this->output_section_->clear_is_relro();
+
+ return true;
+}
+
+// Get the list of segments to use for an allocated section when using
+// a PHDRS clause.
+
+Output_section*
+Output_section_definition::allocate_to_segment(String_list** phdrs_list,
+ bool* orphan)
+{
+ if (this->output_section_ == NULL)
+ return NULL;
+ if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
+ return NULL;
+ *orphan = false;
+ if (this->phdrs_ != NULL)
+ *phdrs_list = this->phdrs_;
+ return this->output_section_;
+}
+
+// Look for an output section by name and return the address, the load
+// address, the alignment, and the size. This is used when an
+// expression refers to an output section which was not actually
+// created. This returns true if the section was found, false
+// otherwise.
+
+bool
+Output_section_definition::get_output_section_info(const char* name,
+ uint64_t* address,
+ uint64_t* load_address,
+ uint64_t* addralign,
+ uint64_t* size) const
+{
+ if (this->name_ != name)
+ return false;
+
+ if (this->output_section_ != NULL)
+ {
+ *address = this->output_section_->address();
+ if (this->output_section_->has_load_address())
+ *load_address = this->output_section_->load_address();
+ else
+ *load_address = *address;
+ *addralign = this->output_section_->addralign();
+ *size = this->output_section_->current_data_size();
+ }
+ else
+ {
+ *address = this->evaluated_address_;
+ *load_address = this->evaluated_load_address_;
+ *addralign = this->evaluated_addralign_;
+ *size = 0;
+ }
+
+ return true;
+}
+
+// Print for debugging.
+
+void
+Output_section_definition::print(FILE* f) const
+{
+ fprintf(f, " %s ", this->name_.c_str());
+
+ if (this->address_ != NULL)
+ {
+ this->address_->print(f);
+ fprintf(f, " ");
+ }
+
+ if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
+ fprintf(f, "(%s) ",
+ this->script_section_type_name(this->script_section_type_));
+
+ fprintf(f, ": ");
+
+ if (this->load_address_ != NULL)
+ {
+ fprintf(f, "AT(");
+ this->load_address_->print(f);
+ fprintf(f, ") ");
+ }
+
+ if (this->align_ != NULL)
+ {
+ fprintf(f, "ALIGN(");
+ this->align_->print(f);
+ fprintf(f, ") ");
+ }
+
+ if (this->subalign_ != NULL)
+ {
+ fprintf(f, "SUBALIGN(");
+ this->subalign_->print(f);
+ fprintf(f, ") ");
+ }
+
+ fprintf(f, "{\n");
+
+ for (Output_section_elements::const_iterator p = this->elements_.begin();
+ p != this->elements_.end();
+ ++p)
+ (*p)->print(f);
+
+ fprintf(f, " }");
+
+ if (this->fill_ != NULL)
+ {
+ fprintf(f, " = ");
+ this->fill_->print(f);
+ }
+
+ if (this->phdrs_ != NULL)
+ {
+ for (String_list::const_iterator p = this->phdrs_->begin();
+ p != this->phdrs_->end();
+ ++p)
+ fprintf(f, " :%s", p->c_str());
+ }
+
+ fprintf(f, "\n");
+}
+
+Script_sections::Section_type
+Output_section_definition::section_type() const
+{
+ switch (this->script_section_type_)
+ {
+ case SCRIPT_SECTION_TYPE_NONE:
+ return Script_sections::ST_NONE;
+ case SCRIPT_SECTION_TYPE_NOLOAD:
+ return Script_sections::ST_NOLOAD;
+ case SCRIPT_SECTION_TYPE_COPY:
+ case SCRIPT_SECTION_TYPE_DSECT:
+ case SCRIPT_SECTION_TYPE_INFO:
+ case SCRIPT_SECTION_TYPE_OVERLAY:
+ // There are not really support so we treat them as ST_NONE. The
+ // parse should have issued errors for them already.
+ return Script_sections::ST_NONE;
+ default:
+ gold_unreachable();
+ }
+}
+
+// Return the name of a script section type.
+
+const char*
+Output_section_definition::script_section_type_name (
+ Script_section_type script_section_type)
+{
+ switch (script_section_type)
+ {
+ case SCRIPT_SECTION_TYPE_NONE:
+ return "NONE";
+ case SCRIPT_SECTION_TYPE_NOLOAD:
+ return "NOLOAD";
+ case SCRIPT_SECTION_TYPE_DSECT:
+ return "DSECT";
+ case SCRIPT_SECTION_TYPE_COPY:
+ return "COPY";
+ case SCRIPT_SECTION_TYPE_INFO:
+ return "INFO";
+ case SCRIPT_SECTION_TYPE_OVERLAY:
+ return "OVERLAY";
+ default:
+ gold_unreachable();
+ }
+}
+
+// An output section created to hold orphaned input sections. These
+// do not actually appear in linker scripts. However, for convenience
+// when setting the output section addresses, we put a marker to these
+// sections in the appropriate place in the list of SECTIONS elements.
+
+class Orphan_output_section : public Sections_element
+{
+ public:
+ Orphan_output_section(Output_section* os)
+ : os_(os)
+ { }
+
+ // Return whether the orphan output section is relro. We can just
+ // check the output section because we always set the flag, if
+ // needed, just after we create the Orphan_output_section.
+ bool
+ is_relro() const
+ { return this->os_->is_relro(); }
+
+ // Initialize OSP with an output section. This should have been
+ // done already.
+ void
+ orphan_section_init(Orphan_section_placement*,
+ Script_sections::Elements_iterator)
+ { gold_unreachable(); }
+
+ // Set section addresses.
+ void
+ set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*);
+
+ // Get the list of segments to use for an allocated section when
+ // using a PHDRS clause.
+ Output_section*
+ allocate_to_segment(String_list**, bool*);
+
+ // Return the associated Output_section.
+ Output_section*
+ get_output_section() const
+ { return this->os_; }
+
+ // Print for debugging.
+ void
+ print(FILE* f) const
+ {
+ fprintf(f, " marker for orphaned output section %s\n",
+ this->os_->name());
+ }
+
+ private:
+ Output_section* os_;
+};
+
+// Set section addresses.
+
+void
+Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
+ uint64_t* dot_value,
+ uint64_t* load_address)
+{
+ typedef std::list<Output_section::Simple_input_section> Input_section_list;
+
+ bool have_load_address = *load_address != *dot_value;
+
+ uint64_t address = *dot_value;
+ address = align_address(address, this->os_->addralign());
+
+ if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
+ {
+ this->os_->set_address(address);
+ if (have_load_address)
+ this->os_->set_load_address(align_address(*load_address,
+ this->os_->addralign()));
+ }
+
+ Input_section_list input_sections;
+ address += this->os_->get_input_sections(address, "", &input_sections);
+
+ for (Input_section_list::iterator p = input_sections.begin();
+ p != input_sections.end();
+ ++p)
+ {
+ uint64_t addralign;
+ uint64_t size;
+
+ // We know we are single-threaded, so it is OK to lock the
+ // object.
+ {
+ const Task* task = reinterpret_cast<const Task*>(-1);
+ Task_lock_obj<Object> tl(task, p->relobj());
+ addralign = p->relobj()->section_addralign(p->shndx());
+ if (p->is_relaxed_input_section())
+ // We use current data size because relxed section sizes may not
+ // have finalized yet.
+ size = p->relaxed_input_section()->current_data_size();
+ else
+ size = p->relobj()->section_size(p->shndx());
+ }
+
+ address = align_address(address, addralign);
+ this->os_->add_simple_input_section(*p, size, addralign);
+ address += size;
+ }
+
+ // An SHF_TLS/SHT_NOBITS section does not take up any address space.
+ if (this->os_ == NULL
+ || (this->os_->flags() & elfcpp::SHF_TLS) == 0
+ || this->os_->type() != elfcpp::SHT_NOBITS)
+ {
+ if (!have_load_address)
+ *load_address = address;
+ else
+ *load_address += address - *dot_value;
+
+ *dot_value = address;
+ }
+}
+
+// Get the list of segments to use for an allocated section when using
+// a PHDRS clause. If this is an allocated section, return the
+// Output_section. We don't change the list of segments.
+
+Output_section*
+Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
+{
+ if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
+ return NULL;
+ *orphan = true;
+ return this->os_;
+}
+
+// Class Phdrs_element. A program header from a PHDRS clause.
+
+class Phdrs_element
+{
+ public:
+ Phdrs_element(const char* name, size_t namelen, unsigned int type,
+ bool includes_filehdr, bool includes_phdrs,
+ bool is_flags_valid, unsigned int flags,
+ Expression* load_address)
+ : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
+ includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
+ flags_(flags), load_address_(load_address), load_address_value_(0),
+ segment_(NULL)
+ { }
+
+ // Return the name of this segment.
+ const std::string&
+ name() const
+ { return this->name_; }
+
+ // Return the type of the segment.
+ unsigned int
+ type() const
+ { return this->type_; }
+
+ // Whether to include the file header.
+ bool
+ includes_filehdr() const
+ { return this->includes_filehdr_; }
+
+ // Whether to include the program headers.
+ bool
+ includes_phdrs() const
+ { return this->includes_phdrs_; }
+
+ // Return whether there is a load address.
+ bool
+ has_load_address() const
+ { return this->load_address_ != NULL; }
+
+ // Evaluate the load address expression if there is one.
+ void
+ eval_load_address(Symbol_table* symtab, Layout* layout)
+ {
+ if (this->load_address_ != NULL)
+ this->load_address_value_ = this->load_address_->eval(symtab, layout,
+ true);
+ }
+
+ // Return the load address.
+ uint64_t
+ load_address() const
+ {
+ gold_assert(this->load_address_ != NULL);
+ return this->load_address_value_;
+ }
+
+ // Create the segment.
+ Output_segment*
+ create_segment(Layout* layout)
+ {
+ this->segment_ = layout->make_output_segment(this->type_, this->flags_);
+ return this->segment_;
+ }
+
+ // Return the segment.
+ Output_segment*
+ segment()
+ { return this->segment_; }
+
+ // Release the segment.
+ void
+ release_segment()
+ { this->segment_ = NULL; }
+
+ // Set the segment flags if appropriate.
+ void
+ set_flags_if_valid()
+ {
+ if (this->is_flags_valid_)
+ this->segment_->set_flags(this->flags_);
+ }
+
+ // Print for debugging.
+ void
+ print(FILE*) const;
+
+ private:
+ // The name used in the script.
+ std::string name_;
+ // The type of the segment (PT_LOAD, etc.).
+ unsigned int type_;
+ // Whether this segment includes the file header.
+ bool includes_filehdr_;
+ // Whether this segment includes the section headers.
+ bool includes_phdrs_;
+ // Whether the flags were explicitly specified.
+ bool is_flags_valid_;
+ // The flags for this segment (PF_R, etc.) if specified.
+ unsigned int flags_;
+ // The expression for the load address for this segment. This may
+ // be NULL.
+ Expression* load_address_;
+ // The actual load address from evaluating the expression.
+ uint64_t load_address_value_;
+ // The segment itself.
+ Output_segment* segment_;
+};
+
+// Print for debugging.
+
+void
+Phdrs_element::print(FILE* f) const
+{
+ fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
+ if (this->includes_filehdr_)
+ fprintf(f, " FILEHDR");
+ if (this->includes_phdrs_)
+ fprintf(f, " PHDRS");
+ if (this->is_flags_valid_)
+ fprintf(f, " FLAGS(%u)", this->flags_);
+ if (this->load_address_ != NULL)
+ {
+ fprintf(f, " AT(");
+ this->load_address_->print(f);
+ fprintf(f, ")");
+ }
+ fprintf(f, ";\n");
+}
+
+// Class Script_sections.
+
+Script_sections::Script_sections()
+ : saw_sections_clause_(false),
+ in_sections_clause_(false),
+ sections_elements_(NULL),
+ output_section_(NULL),
+ phdrs_elements_(NULL),
+ orphan_section_placement_(NULL),
+ data_segment_align_start_(),
+ saw_data_segment_align_(false),
+ saw_relro_end_(false),
+ saw_segment_start_expression_(false)
+{
+}
+
+// Start a SECTIONS clause.
+
+void
+Script_sections::start_sections()
+{
+ gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
+ this->saw_sections_clause_ = true;
+ this->in_sections_clause_ = true;
+ if (this->sections_elements_ == NULL)
+ this->sections_elements_ = new Sections_elements;
+}
+
+// Finish a SECTIONS clause.
+
+void
+Script_sections::finish_sections()
+{
+ gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
+ this->in_sections_clause_ = false;
+}
+
+// Add a symbol to be defined.
+
+void
+Script_sections::add_symbol_assignment(const char* name, size_t length,
+ Expression* val, bool provide,
+ bool hidden)
+{
+ if (this->output_section_ != NULL)
+ this->output_section_->add_symbol_assignment(name, length, val,
+ provide, hidden);
+ else
+ {
+ Sections_element* p = new Sections_element_assignment(name, length,
+ val, provide,
+ hidden);
+ this->sections_elements_->push_back(p);
+ }
+}
+
+// Add an assignment to the special dot symbol.
+
+void
+Script_sections::add_dot_assignment(Expression* val)
+{
+ if (this->output_section_ != NULL)
+ this->output_section_->add_dot_assignment(val);
+ else
+ {
+ // The GNU linker permits assignments to . to appears outside of
+ // a SECTIONS clause, and treats it as appearing inside, so
+ // sections_elements_ may be NULL here.
+ if (this->sections_elements_ == NULL)
+ {
+ this->sections_elements_ = new Sections_elements;
+ this->saw_sections_clause_ = true;
+ }
+
+ Sections_element* p = new Sections_element_dot_assignment(val);
+ this->sections_elements_->push_back(p);
+ }
+}
+
+// Add an assertion.
+
+void
+Script_sections::add_assertion(Expression* check, const char* message,
+ size_t messagelen)
+{
+ if (this->output_section_ != NULL)
+ this->output_section_->add_assertion(check, message, messagelen);
+ else
+ {
+ Sections_element* p = new Sections_element_assertion(check, message,
+ messagelen);
+ this->sections_elements_->push_back(p);
+ }
+}
+
+// Start processing entries for an output section.
+
+void
+Script_sections::start_output_section(
+ const char* name,
+ size_t namelen,
+ const Parser_output_section_header *header)
+{
+ Output_section_definition* posd = new Output_section_definition(name,
+ namelen,
+ header);
+ this->sections_elements_->push_back(posd);
+ gold_assert(this->output_section_ == NULL);
+ this->output_section_ = posd;
+}
+
+// Stop processing entries for an output section.
+
+void
+Script_sections::finish_output_section(
+ const Parser_output_section_trailer* trailer)
+{
+ gold_assert(this->output_section_ != NULL);
+ this->output_section_->finish(trailer);
+ this->output_section_ = NULL;
+}
+
+// Add a data item to the current output section.
+
+void
+Script_sections::add_data(int size, bool is_signed, Expression* val)
+{
+ gold_assert(this->output_section_ != NULL);
+ this->output_section_->add_data(size, is_signed, val);
+}
+
+// Add a fill value setting to the current output section.
+
+void
+Script_sections::add_fill(Expression* val)
+{
+ gold_assert(this->output_section_ != NULL);
+ this->output_section_->add_fill(val);
+}
+
+// Add an input section specification to the current output section.
+
+void
+Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
+{
+ gold_assert(this->output_section_ != NULL);
+ this->output_section_->add_input_section(spec, keep);
+}
+
+// This is called when we see DATA_SEGMENT_ALIGN. It means that any
+// subsequent output sections may be relro.
+
+void
+Script_sections::data_segment_align()
+{
+ if (this->saw_data_segment_align_)
+ gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
+ gold_assert(!this->sections_elements_->empty());
+ Sections_elements::iterator p = this->sections_elements_->end();
+ --p;
+ this->data_segment_align_start_ = p;
+ this->saw_data_segment_align_ = true;
+}
+
+// This is called when we see DATA_SEGMENT_RELRO_END. It means that
+// any output sections seen since DATA_SEGMENT_ALIGN are relro.
+
+void
+Script_sections::data_segment_relro_end()
+{
+ if (this->saw_relro_end_)
+ gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
+ "in a linker script"));
+ this->saw_relro_end_ = true;
+
+ if (!this->saw_data_segment_align_)
+ gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
+ else
+ {
+ Sections_elements::iterator p = this->data_segment_align_start_;
+ for (++p; p != this->sections_elements_->end(); ++p)
+ (*p)->set_is_relro();
+ }
+}
+
+// Create any required sections.
+
+void
+Script_sections::create_sections(Layout* layout)
+{
+ if (!this->saw_sections_clause_)
+ return;
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ (*p)->create_sections(layout);
+}
+
+// Add any symbols we are defining to the symbol table.
+
+void
+Script_sections::add_symbols_to_table(Symbol_table* symtab)
+{
+ if (!this->saw_sections_clause_)
+ return;
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ (*p)->add_symbols_to_table(symtab);
+}
+
+// Finalize symbols and check assertions.
+
+void
+Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
+{
+ if (!this->saw_sections_clause_)
+ return;
+ uint64_t dot_value = 0;
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ (*p)->finalize_symbols(symtab, layout, &dot_value);
+}
+
+// Return the name of the output section to use for an input file name
+// and section name.
+
+const char*
+Script_sections::output_section_name(
+ const char* file_name,
+ const char* section_name,
+ Output_section*** output_section_slot,
+ Script_sections::Section_type *psection_type)
+{
+ for (Sections_elements::const_iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ {
+ const char* ret = (*p)->output_section_name(file_name, section_name,
+ output_section_slot,
+ psection_type);
+
+ if (ret != NULL)
+ {
+ // The special name /DISCARD/ means that the input section
+ // should be discarded.
+ if (strcmp(ret, "/DISCARD/") == 0)
+ {
+ *output_section_slot = NULL;
+ *psection_type = Script_sections::ST_NONE;
+ return NULL;
+ }
+ return ret;
+ }
+ }
+
+ // If we couldn't find a mapping for the name, the output section
+ // gets the name of the input section.
+
+ *output_section_slot = NULL;
+ *psection_type = Script_sections::ST_NONE;
+
+ return section_name;
+}
+
+// Place a marker for an orphan output section into the SECTIONS
+// clause.
+
+void
+Script_sections::place_orphan(Output_section* os)
+{
+ Orphan_section_placement* osp = this->orphan_section_placement_;
+ if (osp == NULL)
+ {
+ // Initialize the Orphan_section_placement structure.
+ osp = new Orphan_section_placement();
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ (*p)->orphan_section_init(osp, p);
+ gold_assert(!this->sections_elements_->empty());
+ Sections_elements::iterator last = this->sections_elements_->end();
+ --last;
+ osp->last_init(last);
+ this->orphan_section_placement_ = osp;
+ }
+
+ Orphan_output_section* orphan = new Orphan_output_section(os);
+
+ // Look for where to put ORPHAN.
+ Sections_elements::iterator* where;
+ if (osp->find_place(os, &where))
+ {
+ if ((**where)->is_relro())
+ os->set_is_relro();
+ else
+ os->clear_is_relro();
+
+ // We want to insert ORPHAN after *WHERE, and then update *WHERE
+ // so that the next one goes after this one.
+ Sections_elements::iterator p = *where;
+ gold_assert(p != this->sections_elements_->end());
+ ++p;
+ *where = this->sections_elements_->insert(p, orphan);
+ }
+ else
+ {
+ os->clear_is_relro();
+ // We don't have a place to put this orphan section. Put it,
+ // and all other sections like it, at the end, but before the
+ // sections which always come at the end.
+ Sections_elements::iterator last = osp->last_place();
+ *where = this->sections_elements_->insert(last, orphan);
+ }
+}
+
+// Set the addresses of all the output sections. Walk through all the
+// elements, tracking the dot symbol. Apply assignments which set
+// absolute symbol values, in case they are used when setting dot.
+// Fill in data statement values. As we find output sections, set the
+// address, set the address of all associated input sections, and
+// update dot. Return the segment which should hold the file header
+// and segment headers, if any.
+
+Output_segment*
+Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
+{
+ gold_assert(this->saw_sections_clause_);
+
+ // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
+ // for our representation.
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ {
+ Output_section_definition* posd;
+ Section_constraint failed_constraint = (*p)->check_constraint(&posd);
+ if (failed_constraint != CONSTRAINT_NONE)
+ {
+ Sections_elements::iterator q;
+ for (q = this->sections_elements_->begin();
+ q != this->sections_elements_->end();
+ ++q)
+ {
+ if (q != p)
+ {
+ if ((*q)->alternate_constraint(posd, failed_constraint))
+ break;
+ }
+ }