+// Add a setting for the fill value.
+
+void
+Output_section_definition::add_fill(Expression* val)
+{
+ Output_section_element* p = new Output_section_element_fill(val);
+ this->elements_.push_back(p);
+}
+
+// Add an input section specification.
+
+void
+Output_section_definition::add_input_section(const Input_section_spec* spec,
+ bool keep)
+{
+ Output_section_element* p = new Output_section_element_input(spec, keep);
+ this->elements_.push_back(p);
+}
+
+// Create any required output sections. We need an output section if
+// there is a data statement here.
+
+void
+Output_section_definition::create_sections(Layout* layout)
+{
+ if (this->output_section_ != NULL)
+ return;
+ for (Output_section_elements::const_iterator p = this->elements_.begin();
+ p != this->elements_.end();
+ ++p)
+ {
+ if ((*p)->needs_output_section())
+ {
+ const char* name = this->name_.c_str();
+ this->output_section_ =
+ layout->make_output_section_for_script(name, this->section_type());
+ return;
+ }
+ }
+}
+
+// Add any symbols being defined to the symbol table.
+
+void
+Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
+{
+ for (Output_section_elements::iterator p = this->elements_.begin();
+ p != this->elements_.end();
+ ++p)
+ (*p)->add_symbols_to_table(symtab);
+}
+
+// Finalize symbols and check assertions.
+
+void
+Output_section_definition::finalize_symbols(Symbol_table* symtab,
+ const Layout* layout,
+ uint64_t* dot_value)
+{
+ if (this->output_section_ != NULL)
+ *dot_value = this->output_section_->address();
+ else
+ {
+ uint64_t address = *dot_value;
+ if (this->address_ != NULL)
+ {
+ address = this->address_->eval_with_dot(symtab, layout, true,
+ *dot_value, NULL,
+ NULL, NULL, false);
+ }
+ if (this->align_ != NULL)
+ {
+ uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
+ *dot_value, NULL,
+ NULL, NULL, false);
+ address = align_address(address, align);
+ }
+ *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)->finalize_symbols(symtab, layout, dot_value, &dot_section);
+}
+
+// Return the output section name to use for an input section name.
+
+const char*
+Output_section_definition::output_section_name(
+ const char* file_name,
+ const char* section_name,
+ Output_section*** slot,
+ Script_sections::Section_type* psection_type,
+ bool* keep,
+ bool match_input_spec)
+{
+ // If the section is a linker-created output section, just look for a match
+ // on the output section name.
+ if (!match_input_spec && this->name_ != "/DISCARD/")
+ {
+ if (this->name_ != section_name)
+ return NULL;
+ *slot = &this->output_section_;
+ *psection_type = this->section_type();
+ return this->name_.c_str();
+ }
+
+ // Ask each element whether it matches NAME.
+ for (Output_section_elements::const_iterator p = this->elements_.begin();
+ p != this->elements_.end();
+ ++p)
+ {
+ if ((*p)->match_name(file_name, section_name, keep))
+ {
+ // 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;
+}
+
+// Return true if memory from START to START + LENGTH is contained
+// within a memory region.
+
+bool
+Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
+ uint64_t start, uint64_t length) const
+{
+ if (this->memory_regions_ == NULL)
+ return false;
+
+ for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
+ mr != this->memory_regions_->end();
+ ++mr)
+ {
+ uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
+ uint64_t l = (*mr)->length()->eval(symtab, layout, false);
+
+ if (s <= start
+ && (s + l) >= (start + length))
+ return true;
+ }
+
+ return false;
+}
+
+// Find a memory region that should be used by a given output SECTION.
+// If provided set PREVIOUS_SECTION_RETURN to point to the last section
+// that used the return memory region.
+
+Memory_region*
+Script_sections::find_memory_region(
+ Output_section_definition* section,
+ bool find_vma_region,
+ bool explicit_only,
+ Output_section_definition** previous_section_return)
+{
+ if (previous_section_return != NULL)
+ * previous_section_return = NULL;
+
+ // Walk the memory regions specified in this script, if any.
+ if (this->memory_regions_ == NULL)
+ return NULL;
+
+ // The /DISCARD/ section never gets assigned to any region.
+ if (section->get_section_name() == "/DISCARD/")
+ return NULL;
+
+ Memory_region* first_match = NULL;
+
+ // First check to see if a region has been assigned to this section.
+ for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
+ mr != this->memory_regions_->end();
+ ++mr)
+ {
+ if (find_vma_region)
+ {
+ for (Memory_region::Section_list::const_iterator s =
+ (*mr)->get_vma_section_list_start();
+ s != (*mr)->get_vma_section_list_end();
+ ++s)
+ if ((*s) == section)
+ {
+ (*mr)->set_last_section(section);
+ return *mr;
+ }
+ }
+ else
+ {
+ for (Memory_region::Section_list::const_iterator s =
+ (*mr)->get_lma_section_list_start();
+ s != (*mr)->get_lma_section_list_end();
+ ++s)
+ if ((*s) == section)
+ {
+ (*mr)->set_last_section(section);
+ return *mr;
+ }
+ }
+
+ if (!explicit_only)
+ {
+ // Make a note of the first memory region whose attributes
+ // are compatible with the section. If we do not find an
+ // explicit region assignment, then we will return this region.
+ Output_section* out_sec = section->get_output_section();
+ if (first_match == NULL
+ && out_sec != NULL
+ && (*mr)->attributes_compatible(out_sec->flags(),
+ out_sec->type()))
+ first_match = *mr;
+ }
+ }
+
+ // With LMA computations, if an explicit region has not been specified then
+ // we will want to set the difference between the VMA and the LMA of the
+ // section were searching for to be the same as the difference between the
+ // VMA and LMA of the last section to be added to first matched region.
+ // Hence, if it was asked for, we return a pointer to the last section
+ // known to be used by the first matched region.
+ if (first_match != NULL
+ && previous_section_return != NULL)
+ *previous_section_return = first_match->get_last_section();
+
+ return first_match;
+}
+
+// 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* dot_alignment,
+ uint64_t* load_address)
+{
+ Memory_region* vma_region = NULL;
+ Memory_region* lma_region = NULL;
+ Script_sections* script_sections =
+ layout->script_options()->script_sections();
+ uint64_t address;
+ uint64_t old_dot_value = *dot_value;
+ uint64_t old_load_address = *load_address;
+
+ // If input section sorting is requested via --section-ordering-file or
+ // linker plugins, then do it here. This is important because we want
+ // any sorting specified in the linker scripts, which will be done after
+ // this, to take precedence. The final order of input sections is then
+ // guaranteed to be according to the linker script specification.
+ if (this->output_section_ != NULL
+ && this->output_section_->input_section_order_specified())
+ this->output_section_->sort_attached_input_sections();
+
+ // Decide the start address for the section. The algorithm is:
+ // 1) If an address has been specified in a linker script, use that.
+ // 2) Otherwise if a memory region has been specified for the section,
+ // use the next free address in the region.
+ // 3) Otherwise if memory regions have been specified find the first
+ // region whose attributes are compatible with this section and
+ // install it into that region.
+ // 4) Otherwise use the current location counter.
+
+ if (this->output_section_ != NULL
+ // Check for --section-start.
+ && parameters->options().section_start(this->output_section_->name(),
+ &address))
+ ;
+ else if (this->address_ == NULL)
+ {
+ vma_region = script_sections->find_memory_region(this, true, false, NULL);
+ if (vma_region != NULL)
+ address = vma_region->get_current_address()->eval(symtab, layout,
+ false);
+ else
+ address = *dot_value;
+ }
+ else
+ {
+ vma_region = script_sections->find_memory_region(this, true, true, NULL);
+ address = this->address_->eval_with_dot(symtab, layout, true,
+ *dot_value, NULL, NULL,
+ dot_alignment, false);
+ if (vma_region != NULL)
+ vma_region->set_address(address, symtab, layout);
+ }
+
+ 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, NULL, false);
+ 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);
+ }
+
+ 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, NULL,
+ false);
+ if (subalign_section != NULL)
+ gold_warning(_("subalign of section %s is not absolute"),
+ this->name_.c_str());
+
+ // Reserve a value of 0 to mean there is no SUBALIGN property.
+ if (subalign == 0)
+ subalign = 1;
+
+ // The external alignment of the output section must be at least
+ // as large as that of the input sections. If there is no
+ // explicit ALIGN property, we set the output section alignment
+ // to match the input section alignment.
+ if (align < subalign || this->align_ == NULL)
+ {
+ align = subalign;
+ 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;
+
+ uint64_t laddr;
+
+ if (this->load_address_ == NULL)
+ {
+ Output_section_definition* previous_section;
+
+ // Determine if an LMA region has been set for this section.
+ lma_region = script_sections->find_memory_region(this, false, false,
+ &previous_section);
+
+ if (lma_region != NULL)
+ {
+ if (previous_section == NULL)
+ // The LMA address was explicitly set to the given region.
+ laddr = lma_region->get_current_address()->eval(symtab, layout,
+ false);
+ else
+ {
+ // We are not going to use the discovered lma_region, so
+ // make sure that we do not update it in the code below.
+ lma_region = NULL;
+
+ if (this->address_ != NULL || previous_section == this)
+ {
+ // Either an explicit VMA address has been set, or an
+ // explicit VMA region has been set, so set the LMA equal to
+ // the VMA.
+ laddr = address;
+ }
+ else
+ {
+ // The LMA address was not explicitly or implicitly set.
+ //
+ // We have been given the first memory region that is
+ // compatible with the current section and a pointer to the
+ // last section to use this region. Set the LMA of this
+ // section so that the difference between its' VMA and LMA
+ // is the same as the difference between the VMA and LMA of
+ // the last section in the given region.
+ laddr = address + (previous_section->evaluated_load_address_
+ - previous_section->evaluated_address_);
+ }
+ }
+
+ if (this->output_section_ != NULL)
+ this->output_section_->set_load_address(laddr);
+ }
+ else
+ {
+ // Do not set the load address of the output section, if one exists.
+ // This allows future sections to determine what the load address
+ // should be. If none is ever set, it will default to being the
+ // same as the vma address.
+ laddr = address;
+ }
+ }
+ else
+ {
+ laddr = this->load_address_->eval_with_dot(symtab, layout, true,
+ *dot_value,
+ this->output_section_,
+ NULL, NULL, false);
+ if (this->output_section_ != NULL)
+ this->output_section_->set_load_address(laddr);
+ }
+
+ this->evaluated_load_address_ = laddr;
+
+ 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,
+ NULL, false);
+ 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_alignment,
+ &dot_section, &fill, &input_sections);
+
+ gold_assert(input_sections.empty());
+
+ if (vma_region != NULL)
+ {
+ // Update the VMA region being used by the section now that we know how
+ // big it is. Use the current address in the region, rather than
+ // start_address because that might have been aligned upwards and we
+ // need to allow for the padding.
+ Expression* addr = vma_region->get_current_address();
+ uint64_t size = *dot_value - addr->eval(symtab, layout, false);
+
+ vma_region->increment_offset(this->get_section_name(), size,
+ symtab, layout);
+ }
+
+ // If the LMA region is different from the VMA region, then increment the
+ // offset there as well. Note that we use the same "dot_value -
+ // start_address" formula that is used in the load_address assignment below.
+ if (lma_region != NULL && lma_region != vma_region)
+ lma_region->increment_offset(this->get_section_name(),
+ *dot_value - start_address,
+ symtab, layout);
+
+ // Compute the load address for the following section.
+ if (this->output_section_ == NULL)
+ *load_address = *dot_value;
+ else if (this->load_address_ == NULL)
+ {
+ if (lma_region == NULL)
+ *load_address = *dot_value;
+ else
+ *load_address =
+ lma_region->get_current_address()->eval(symtab, layout, false);
+ }
+ 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)
+{
+ // Update phdrs_list even if we don't have an output section. It
+ // might be used by the following sections.
+ if (this->phdrs_ != NULL)
+ *phdrs_list = this->phdrs_;
+
+ if (this->output_section_ == NULL)
+ return NULL;
+ if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
+ return NULL;
+ *orphan = false;
+ 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();
+ }
+}
+
+void
+Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
+{
+ gold_assert(mr != NULL);
+ // Add the current section to the specified region's list.
+ mr->add_section(this, set_vma);
+}
+
+// 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*,
+ 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*,
+ uint64_t* load_address)
+{
+ typedef std::list<Output_section::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 input section sorting is requested via --section-ordering-file or
+ // linker plugins, then do it here. This is important because we want
+ // any sorting specified in the linker scripts, which will be done after
+ // this, to take precedence. The final order of input sections is then
+ // guaranteed to be according to the linker script specification.
+ if (this->os_ != NULL
+ && this->os_->input_section_order_specified())
+ this->os_->sort_attached_input_sections();
+
+ // For a relocatable link, all orphan sections are put at
+ // address 0. In general we expect all sections to be at
+ // address 0 for a relocatable link, but we permit the linker
+ // script to override that for specific output sections.
+ if (parameters->options().relocatable())
+ {
+ address = 0;
+ *load_address = 0;
+ have_load_address = false;
+ }
+
+ 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 = p->addralign();
+ if (!p->is_input_section())
+ p->output_section_data()->finalize_data_size();
+ uint64_t size = p->data_size();
+ address = align_address(address, addralign);
+ this->os_->add_script_input_section(*p);
+ address += size;
+ }
+
+ if (parameters->options().relocatable())
+ {
+ // For a relocatable link, reset DOT_VALUE to 0.
+ *dot_value = 0;
+ *load_address = 0;
+ }
+ else if (this->os_ == NULL
+ || (this->os_->flags() & elfcpp::SHF_TLS) == 0
+ || this->os_->type() != elfcpp::SHT_NOBITS)
+ {
+ // An SHF_TLS/SHT_NOBITS section does not take up any address space.
+ 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");
+}
+
+// Add a memory region.
+
+void
+Script_sections::add_memory_region(const char* name, size_t namelen,
+ unsigned int attributes,
+ Expression* start, Expression* length)
+{
+ if (this->memory_regions_ == NULL)
+ this->memory_regions_ = new Memory_regions();
+ else if (this->find_memory_region(name, namelen))
+ {
+ gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
+ name);
+ // FIXME: Add a GOLD extension to allow multiple regions with the same
+ // name. This would amount to a single region covering disjoint blocks
+ // of memory, which is useful for embedded devices.
+ }
+
+ // FIXME: Check the length and start values. Currently we allow
+ // non-constant expressions for these values, whereas LD does not.
+
+ // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
+ // describe a region that packs from the end address going down, rather
+ // than the start address going up. This would be useful for embedded
+ // devices.
+
+ this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
+ start, length));
+}
+
+// Find a memory region.
+
+Memory_region*
+Script_sections::find_memory_region(const char* name, size_t namelen)
+{
+ if (this->memory_regions_ == NULL)
+ return NULL;
+
+ for (Memory_regions::const_iterator m = this->memory_regions_->begin();
+ m != this->memory_regions_->end();
+ ++m)
+ if ((*m)->name_match(name, namelen))
+ return *m;
+
+ return NULL;
+}
+
+// Find a memory region's origin.
+
+Expression*
+Script_sections::find_memory_region_origin(const char* name, size_t namelen)
+{
+ Memory_region* mr = find_memory_region(name, namelen);
+ if (mr == NULL)
+ return NULL;
+
+ return mr->start_address();
+}
+
+// Find a memory region's length.
+
+Expression*
+Script_sections::find_memory_region_length(const char* name, size_t namelen)
+{
+ Memory_region* mr = find_memory_region(name, namelen);
+ if (mr == NULL)
+ return NULL;
+
+ return mr->length();
+}
+
+// Set the memory region to use for the current section.
+
+void
+Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
+{
+ gold_assert(!this->sections_elements_->empty());
+ this->sections_elements_->back()->set_memory_region(mr, set_vma);
+}
+
+// Class Script_sections.
+
+Script_sections::Script_sections()
+ : saw_sections_clause_(false),
+ in_sections_clause_(false),
+ sections_elements_(NULL),
+ output_section_(NULL),
+ memory_regions_(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),
+ segments_created_(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,
+ bool* keep,
+ bool is_input_section)
+{
+ 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, keep,
+ is_input_section);
+
+ 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;
+ }
+ }
+
+ // We have an orphan section.
+ *output_section_slot = NULL;
+ *psection_type = Script_sections::ST_NONE;
+ *keep = false;
+
+ General_options::Orphan_handling orphan_handling =
+ parameters->options().orphan_handling_enum();
+ if (orphan_handling == General_options::ORPHAN_DISCARD)
+ return NULL;
+ if (orphan_handling == General_options::ORPHAN_ERROR)
+ {
+ if (file_name == NULL)
+ gold_error(_("unplaced orphan section '%s'"), section_name);
+ else
+ gold_error(_("unplaced orphan section '%s' from '%s'"),
+ section_name, file_name);
+ return NULL;
+ }
+ if (orphan_handling == General_options::ORPHAN_WARN)
+ {
+ if (file_name == NULL)
+ gold_warning(_("orphan section '%s' is being placed in section '%s'"),
+ section_name, section_name);
+ else
+ gold_warning(_("orphan section '%s' from '%s' is being placed "
+ "in section '%s'"),
+ section_name, file_name, section_name);
+ }
+
+ // If we couldn't find a mapping for the name, the output section
+ // gets the name of the input section.
+ 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);
+ }
+
+ if ((os->flags() & elfcpp::SHF_ALLOC) != 0)
+ osp->update_last_alloc(*where);
+}
+
+// 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;
+ }
+ }
+
+ if (q == this->sections_elements_->end())
+ gold_error(_("no matching section constraint"));
+ }
+ }
+
+ // Force the alignment of the first TLS section to be the maximum
+ // alignment of all TLS sections.
+ Output_section* first_tls = NULL;
+ uint64_t tls_align = 0;
+ for (Sections_elements::const_iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ {
+ Output_section* os = (*p)->get_output_section();
+ if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
+ {
+ if (first_tls == NULL)
+ first_tls = os;
+ if (os->addralign() > tls_align)
+ tls_align = os->addralign();
+ }
+ }
+ if (first_tls != NULL)
+ first_tls->set_addralign(tls_align);
+
+ // For a relocatable link, we implicitly set dot to zero.
+ uint64_t dot_value = 0;
+ uint64_t dot_alignment = 0;
+ uint64_t load_address = 0;
+
+ // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
+ // to set section addresses. If the script has any SEGMENT_START
+ // expression, we do not set the section addresses.
+ bool use_tsection_options =
+ (!this->saw_segment_start_expression_
+ && (parameters->options().user_set_Ttext()
+ || parameters->options().user_set_Tdata()
+ || parameters->options().user_set_Tbss()));
+
+ for (Sections_elements::iterator p = this->sections_elements_->begin();
+ p != this->sections_elements_->end();
+ ++p)
+ {
+ Output_section* os = (*p)->get_output_section();
+
+ // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
+ // the special sections by names and doing dot assignments.
+ if (use_tsection_options
+ && os != NULL
+ && (os->flags() & elfcpp::SHF_ALLOC) != 0)
+ {
+ uint64_t new_dot_value = dot_value;
+
+ if (parameters->options().user_set_Ttext()
+ && strcmp(os->name(), ".text") == 0)
+ new_dot_value = parameters->options().Ttext();
+ else if (parameters->options().user_set_Tdata()
+ && strcmp(os->name(), ".data") == 0)
+ new_dot_value = parameters->options().Tdata();
+ else if (parameters->options().user_set_Tbss()
+ && strcmp(os->name(), ".bss") == 0)
+ new_dot_value = parameters->options().Tbss();
+
+ // Update dot and load address if necessary.
+ if (new_dot_value < dot_value)
+ gold_error(_("dot may not move backward"));
+ else if (new_dot_value != dot_value)
+ {
+ dot_value = new_dot_value;
+ load_address = new_dot_value;
+ }
+ }
+
+ (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
+ &load_address);
+ }
+
+ if (this->phdrs_elements_ != NULL)
+ {
+ for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
+ p != this->phdrs_elements_->end();
+ ++p)
+ (*p)->eval_load_address(symtab, layout);
+ }
+
+ return this->create_segments(layout, dot_alignment);
+}
+
+// Sort the sections in order to put them into segments.
+
+class Sort_output_sections
+{
+ public:
+ Sort_output_sections(const Script_sections::Sections_elements* elements)
+ : elements_(elements)
+ { }
+
+ bool
+ operator()(const Output_section* os1, const Output_section* os2) const;
+
+ private:
+ int
+ script_compare(const Output_section* os1, const Output_section* os2) const;
+
+ private:
+ const Script_sections::Sections_elements* elements_;
+};
+
+bool
+Sort_output_sections::operator()(const Output_section* os1,
+ const Output_section* os2) const
+{
+ // Sort first by the load address.
+ uint64_t lma1 = (os1->has_load_address()
+ ? os1->load_address()
+ : os1->address());
+ uint64_t lma2 = (os2->has_load_address()
+ ? os2->load_address()
+ : os2->address());
+ if (lma1 != lma2)
+ return lma1 < lma2;
+
+ // Then sort by the virtual address.
+ if (os1->address() != os2->address())
+ return os1->address() < os2->address();
+
+ // If the linker script says which of these sections is first, go
+ // with what it says.
+ int i = this->script_compare(os1, os2);
+ if (i != 0)
+ return i < 0;
+
+ // Sort PROGBITS before NOBITS.
+ bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
+ bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
+ if (nobits1 != nobits2)
+ return nobits2;
+
+ // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
+ // beginning.
+ bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
+ bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
+ if (tls1 != tls2)
+ return nobits1 ? tls1 : tls2;
+
+ // Sort non-NOLOAD before NOLOAD.
+ if (os1->is_noload() && !os2->is_noload())
+ return true;
+ if (!os1->is_noload() && os2->is_noload())
+ return true;
+
+ // The sections seem practically identical. Sort by name to get a
+ // stable sort.
+ return os1->name() < os2->name();
+}
+
+// Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
+// if either OS1 or OS2 is not mentioned. This ensures that we keep
+// empty sections in the order in which they appear in a linker
+// script.
+
+int
+Sort_output_sections::script_compare(const Output_section* os1,
+ const Output_section* os2) const
+{
+ if (this->elements_ == NULL)
+ return 0;
+
+ bool found_os1 = false;
+ bool found_os2 = false;
+ for (Script_sections::Sections_elements::const_iterator
+ p = this->elements_->begin();
+ p != this->elements_->end();
+ ++p)
+ {
+ if (os2 == (*p)->get_output_section())
+ {
+ if (found_os1)
+ return -1;
+ found_os2 = true;
+ }
+ else if (os1 == (*p)->get_output_section())
+ {
+ if (found_os2)
+ return 1;
+ found_os1 = true;
+ }
+ }
+
+ return 0;
+}
+
+// Return whether OS is a BSS section. This is a SHT_NOBITS section.
+// We treat a section with the SHF_TLS flag set as taking up space
+// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
+// space for them in the file.
+
+bool
+Script_sections::is_bss_section(const Output_section* os)
+{
+ return (os->type() == elfcpp::SHT_NOBITS
+ && (os->flags() & elfcpp::SHF_TLS) == 0);
+}
+
+// Return the size taken by the file header and the program headers.