+// A region of memory.
+class Memory_region
+{
+ public:
+ Memory_region(const char* name, size_t namelen, unsigned int attributes,
+ Expression* start, Expression* length)
+ : name_(name, namelen),
+ attributes_(attributes),
+ start_(start),
+ length_(length),
+ current_offset_(0),
+ vma_sections_(),
+ lma_sections_(),
+ last_section_(NULL)
+ { }
+
+ // Return the name of this region.
+ const std::string&
+ name() const
+ { return this->name_; }
+
+ // Return the start address of this region.
+ Expression*
+ start_address() const
+ { return this->start_; }
+
+ // Return the length of this region.
+ Expression*
+ length() const
+ { return this->length_; }
+
+ // Print the region (when debugging).
+ void
+ print(FILE*) const;
+
+ // Return true if <name,namelen> matches this region.
+ bool
+ name_match(const char* name, size_t namelen)
+ {
+ return (this->name_.length() == namelen
+ && strncmp(this->name_.c_str(), name, namelen) == 0);
+ }
+
+ Expression*
+ get_current_address() const
+ {
+ return
+ script_exp_binary_add(this->start_,
+ script_exp_integer(this->current_offset_));
+ }
+
+ void
+ set_address(uint64_t addr, const Symbol_table* symtab, const Layout* layout)
+ {
+ uint64_t start = this->start_->eval(symtab, layout, false);
+ uint64_t len = this->length_->eval(symtab, layout, false);
+ if (addr < start || addr >= start + len)
+ gold_error(_("address 0x%llx is not within region %s"),
+ static_cast<unsigned long long>(addr),
+ this->name_.c_str());
+ else if (addr < start + this->current_offset_)
+ gold_error(_("address 0x%llx moves dot backwards in region %s"),
+ static_cast<unsigned long long>(addr),
+ this->name_.c_str());
+ this->current_offset_ = addr - start;
+ }
+
+ void
+ increment_offset(std::string section_name, uint64_t amount,
+ const Symbol_table* symtab, const Layout* layout)
+ {
+ this->current_offset_ += amount;
+
+ if (this->current_offset_
+ > this->length_->eval(symtab, layout, false))
+ gold_error(_("section %s overflows end of region %s"),
+ section_name.c_str(), this->name_.c_str());
+ }
+
+ // Returns true iff there is room left in this region
+ // for AMOUNT more bytes of data.
+ bool
+ has_room_for(const Symbol_table* symtab, const Layout* layout,
+ uint64_t amount) const
+ {
+ return (this->current_offset_ + amount
+ < this->length_->eval(symtab, layout, false));
+ }
+
+ // Return true if the provided section flags
+ // are compatible with this region's attributes.
+ bool
+ attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
+
+ void
+ add_section(Output_section_definition* sec, bool vma)
+ {
+ if (vma)
+ this->vma_sections_.push_back(sec);
+ else
+ this->lma_sections_.push_back(sec);
+ }
+
+ typedef std::vector<Output_section_definition*> Section_list;
+
+ // Return the start of the list of sections
+ // whose VMAs are taken from this region.
+ Section_list::const_iterator
+ get_vma_section_list_start() const
+ { return this->vma_sections_.begin(); }
+
+ // Return the start of the list of sections
+ // whose LMAs are taken from this region.
+ Section_list::const_iterator
+ get_lma_section_list_start() const
+ { return this->lma_sections_.begin(); }
+
+ // Return the end of the list of sections
+ // whose VMAs are taken from this region.
+ Section_list::const_iterator
+ get_vma_section_list_end() const
+ { return this->vma_sections_.end(); }
+
+ // Return the end of the list of sections
+ // whose LMAs are taken from this region.
+ Section_list::const_iterator
+ get_lma_section_list_end() const
+ { return this->lma_sections_.end(); }
+
+ Output_section_definition*
+ get_last_section() const
+ { return this->last_section_; }
+
+ void
+ set_last_section(Output_section_definition* sec)
+ { this->last_section_ = sec; }
+
+ private:
+
+ std::string name_;
+ unsigned int attributes_;
+ Expression* start_;
+ Expression* length_;
+ // The offset to the next free byte in the region.
+ // Note - for compatibility with GNU LD we only maintain one offset
+ // regardless of whether the region is being used for VMA values,
+ // LMA values, or both.
+ uint64_t current_offset_;
+ // A list of sections whose VMAs are set inside this region.
+ Section_list vma_sections_;
+ // A list of sections whose LMAs are set inside this region.
+ Section_list lma_sections_;
+ // The latest section to make use of this region.
+ Output_section_definition* last_section_;
+};
+
+// Return true if the provided section flags
+// are compatible with this region's attributes.
+
+bool
+Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
+ elfcpp::Elf_Xword type) const
+{
+ unsigned int attrs = this->attributes_;
+
+ // No attributes means that this region is not compatible with anything.
+ if (attrs == 0)
+ return false;
+
+ bool match = true;
+ do
+ {
+ switch (attrs & - attrs)
+ {
+ case MEM_EXECUTABLE:
+ if ((flags & elfcpp::SHF_EXECINSTR) == 0)
+ match = false;
+ break;
+
+ case MEM_WRITEABLE:
+ if ((flags & elfcpp::SHF_WRITE) == 0)
+ match = false;
+ break;
+
+ case MEM_READABLE:
+ // All sections are presumed readable.
+ break;
+
+ case MEM_ALLOCATABLE:
+ if ((flags & elfcpp::SHF_ALLOC) == 0)
+ match = false;
+ break;
+
+ case MEM_INITIALIZED:
+ if ((type & elfcpp::SHT_NOBITS) != 0)
+ match = false;
+ break;
+ }
+ attrs &= ~ (attrs & - attrs);
+ }
+ while (attrs != 0);
+
+ return match;
+}
+
+// Print a memory region.
+
+void
+Memory_region::print(FILE* f) const
+{
+ fprintf(f, " %s", this->name_.c_str());
+
+ unsigned int attrs = this->attributes_;
+ if (attrs != 0)
+ {
+ fprintf(f, " (");
+ do
+ {
+ switch (attrs & - attrs)
+ {
+ case MEM_EXECUTABLE: fputc('x', f); break;
+ case MEM_WRITEABLE: fputc('w', f); break;
+ case MEM_READABLE: fputc('r', f); break;
+ case MEM_ALLOCATABLE: fputc('a', f); break;
+ case MEM_INITIALIZED: fputc('i', f); break;
+ default:
+ gold_unreachable();
+ }
+ attrs &= ~ (attrs & - attrs);
+ }
+ while (attrs != 0);
+ fputc(')', f);
+ }
+
+ fprintf(f, " : origin = ");
+ this->start_->print(f);
+ fprintf(f, ", length = ");
+ this->length_->print(f);
+ fprintf(f, "\n");
+}
+