// Output_data variables.
-bool Output_data::sizes_are_fixed;
+bool Output_data::allocated_sizes_are_fixed;
// Output_data methods.
{
}
-// Set the address and offset.
-
-void
-Output_data::set_address(uint64_t addr, off_t off)
-{
- this->address_ = addr;
- this->offset_ = off;
-
- // Let the child class know.
- this->do_set_address(addr, off);
-}
-
// Return the default alignment for the target size.
uint64_t
void
Output_file_header::do_write(Output_file* of)
{
+ gold_assert(this->offset() == 0);
+
if (parameters->get_size() == 32)
{
if (parameters->is_big_endian())
// Output_data_strtab methods.
-// Set the address. We don't actually care about the address, but we
-// do set our final size.
+// Set the final data size.
void
-Output_data_strtab::do_set_address(uint64_t, off_t)
+Output_data_strtab::set_final_data_size()
{
this->strtab_->set_string_offsets();
this->set_data_size(this->strtab_->get_strtab_size());
{
if (object->local_has_got_offset(symndx))
return false;
+
this->entries_.push_back(Got_entry(object, symndx));
this->set_got_size();
object->set_local_got_offset(symndx, this->last_got_offset());
return true;
}
+// Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
+// In a pair of entries, the first value in the pair will be used for the
+// module index, and the second value will be used for the dtv-relative
+// offset. This returns true if this is a new GOT entry, false if the symbol
+// already has a GOT entry.
+
+template<int size, bool big_endian>
+bool
+Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym,
+ bool need_pair)
+{
+ if (gsym->has_tls_got_offset(need_pair))
+ return false;
+
+ this->entries_.push_back(Got_entry(gsym));
+ gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
+ if (need_pair)
+ this->entries_.push_back(Got_entry(gsym));
+ this->set_got_size();
+ return true;
+}
+
+// Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
+// In a pair of entries, the first value in the pair will be used for the
+// module index, and the second value will be used for the dtv-relative
+// offset. This returns true if this is a new GOT entry, false if the symbol
+// already has a GOT entry.
+
+template<int size, bool big_endian>
+bool
+Output_data_got<size, big_endian>::add_local_tls(
+ Sized_relobj<size, big_endian>* object,
+ unsigned int symndx,
+ bool need_pair)
+{
+ if (object->local_has_tls_got_offset(symndx, need_pair))
+ return false;
+
+ this->entries_.push_back(Got_entry(object, symndx));
+ object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
+ if (need_pair)
+ this->entries_.push_back(Got_entry(object, symndx));
+ this->set_got_size();
+ return true;
+}
+
// Write out the GOT.
template<int size, bool big_endian>
// Set the final data size.
void
-Output_data_dynamic::do_set_address(uint64_t, off_t)
+Output_data_dynamic::set_final_data_size()
{
// Add the terminating entry.
this->add_constant(elfcpp::DT_NULL, 0);
// Set the address and file offset.
void
-Output_section::Input_section::set_address(uint64_t addr, off_t off,
- off_t secoff)
+Output_section::Input_section::set_address_and_file_offset(
+ uint64_t address,
+ off_t file_offset,
+ off_t section_file_offset)
{
if (this->is_input_section())
- this->u2_.object->set_section_offset(this->shndx_, off - secoff);
+ this->u2_.object->set_section_offset(this->shndx_,
+ file_offset - section_file_offset);
else
- this->u2_.posd->set_address(addr, off);
+ this->u2_.posd->set_address_and_file_offset(address, file_offset);
+}
+
+// Finalize the data size.
+
+void
+Output_section::Input_section::finalize_data_size()
+{
+ if (!this->is_input_section())
+ this->u2_.posd->finalize_data_size();
}
// Try to turn an input offset into an output offset.
this->u2_.posd->write(of);
}
+// Write the data to a buffer. As for write(), we don't have to do
+// anything for an input section.
+
+void
+Output_section::Input_section::write_to_buffer(unsigned char* buffer)
+{
+ if (!this->is_input_section())
+ this->u2_.posd->write_to_buffer(buffer);
+}
+
// Output_section methods.
// Construct an Output_section. NAME will point into a Stringpool.
input_sections_(),
first_input_offset_(0),
fills_(),
+ postprocessing_buffer_(NULL),
needs_symtab_index_(false),
needs_dynsym_index_(false),
should_link_to_symtab_(false),
should_link_to_dynsym_(false),
- after_input_sections_(false)
+ after_input_sections_(false),
+ requires_postprocessing_(false)
{
+ // An unallocated section has no address. Forcing this means that
+ // we don't need special treatment for symbols defined in debug
+ // sections.
+ if ((flags & elfcpp::SHF_ALLOC) == 0)
+ this->set_address(0);
}
Output_section::~Output_section()
if (addralign > this->addralign_)
this->addralign_ = addralign;
+ typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
+ uint64_t entsize = shdr.get_sh_entsize();
+
+ // .debug_str is a mergeable string section, but is not always so
+ // marked by compilers. Mark manually here so we can optimize.
+ if (strcmp(secname, ".debug_str") == 0)
+ {
+ sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
+ entsize = 1;
+ }
+
// If this is a SHF_MERGE section, we pass all the input sections to
// a Output_data_merge. We don't try to handle relocations for such
// a section.
- if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0
+ if ((sh_flags & elfcpp::SHF_MERGE) != 0
&& reloc_shndx == 0)
{
- if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
- shdr.get_sh_entsize(),
- addralign))
+ if (this->add_merge_input_section(object, shndx, sh_flags,
+ entsize, addralign))
{
// Tell the relocation routines that they need to call the
// output_offset method to determine the final address.
}
}
- off_t offset_in_section = this->data_size();
+ off_t offset_in_section = this->current_data_size_for_child();
off_t aligned_offset_in_section = align_address(offset_in_section,
addralign);
if (aligned_offset_in_section > offset_in_section
- && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
+ && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
&& object->target()->has_code_fill())
{
// We need to add some fill data. Using fill_list_ when
}
}
- this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
+ this->set_current_data_size_for_child(aligned_offset_in_section
+ + shdr.get_sh_size());
// We need to keep track of this section if we are already keeping
// track of sections, or if we are relaxing. FIXME: Add test for
Output_section::add_output_section_data(Input_section* inp)
{
if (this->input_sections_.empty())
- this->first_input_offset_ = this->data_size();
+ this->first_input_offset_ = this->current_data_size_for_child();
this->input_sections_.push_back(*inp);
p != this->input_sections_.end();
++p)
if (p->is_merge_section(is_string, entsize, addralign))
- break;
+ {
+ p->add_input_section(object, shndx);
+ return true;
+ }
// We handle the actual constant merging in Output_merge_data or
// Output_merge_string_data.
- if (p != this->input_sections_.end())
- p->add_input_section(object, shndx);
+ Output_section_data* posd;
+ if (!is_string)
+ posd = new Output_merge_data(entsize, addralign);
else
{
- Output_section_data* posd;
- if (!is_string)
- posd = new Output_merge_data(entsize, addralign);
- else if (entsize == 1)
- posd = new Output_merge_string<char>(addralign);
- else if (entsize == 2)
- posd = new Output_merge_string<uint16_t>(addralign);
- else if (entsize == 4)
- posd = new Output_merge_string<uint32_t>(addralign);
- else
- return false;
-
- this->add_output_merge_section(posd, is_string, entsize);
- posd->add_input_section(object, shndx);
+ switch (entsize)
+ {
+ case 1:
+ posd = new Output_merge_string<char>(addralign);
+ break;
+ case 2:
+ posd = new Output_merge_string<uint16_t>(addralign);
+ break;
+ case 4:
+ posd = new Output_merge_string<uint32_t>(addralign);
+ break;
+ default:
+ return false;
+ }
}
+ this->add_output_merge_section(posd, is_string, entsize);
+ posd->add_input_section(object, shndx);
+
return true;
}
gold_unreachable();
}
-// Set the address of an Output_section. This is where we handle
+// Set the data size of an Output_section. This is where we handle
// setting the addresses of any Output_section_data objects.
void
-Output_section::do_set_address(uint64_t address, off_t startoff)
+Output_section::set_final_data_size()
{
if (this->input_sections_.empty())
- return;
+ {
+ this->set_data_size(this->current_data_size_for_child());
+ return;
+ }
+ uint64_t address = this->address();
+ off_t startoff = this->offset();
off_t off = startoff + this->first_input_offset_;
for (Input_section_list::iterator p = this->input_sections_.begin();
p != this->input_sections_.end();
++p)
{
off = align_address(off, p->addralign());
- p->set_address(address + (off - startoff), off, startoff);
+ p->set_address_and_file_offset(address + (off - startoff), off,
+ startoff);
off += p->data_size();
}
void
Output_section::do_write(Output_file* of)
{
+ gold_assert(!this->requires_postprocessing());
+
off_t output_section_file_offset = this->offset();
for (Fill_list::iterator p = this->fills_.begin();
p != this->fills_.end();
p->write(of);
}
+// If a section requires postprocessing, create the buffer to use.
+
+void
+Output_section::create_postprocessing_buffer()
+{
+ gold_assert(this->requires_postprocessing());
+ gold_assert(this->postprocessing_buffer_ == NULL);
+
+ if (!this->input_sections_.empty())
+ {
+ off_t off = this->first_input_offset_;
+ for (Input_section_list::iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p)
+ {
+ off = align_address(off, p->addralign());
+ p->finalize_data_size();
+ off += p->data_size();
+ }
+ this->set_current_data_size_for_child(off);
+ }
+
+ off_t buffer_size = this->current_data_size_for_child();
+ this->postprocessing_buffer_ = new unsigned char[buffer_size];
+}
+
+// Write all the data of an Output_section into the postprocessing
+// buffer. This is used for sections which require postprocessing,
+// such as compression. Input sections are handled by
+// Object::Relocate.
+
+void
+Output_section::write_to_postprocessing_buffer()
+{
+ gold_assert(this->requires_postprocessing());
+
+ Target* target = parameters->target();
+ unsigned char* buffer = this->postprocessing_buffer();
+ for (Fill_list::iterator p = this->fills_.begin();
+ p != this->fills_.end();
+ ++p)
+ {
+ std::string fill_data(target->code_fill(p->length()));
+ memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
+ }
+
+ off_t off = this->first_input_offset_;
+ for (Input_section_list::iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p)
+ {
+ off = align_address(off, p->addralign());
+ p->write_to_buffer(buffer + off);
+ off += p->data_size();
+ }
+}
+
// Output segment methods.
Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
// SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
// case: we group the SHF_TLS/SHT_NOBITS sections right after the
// SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
- // correctly.
- if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
+ // correctly. SHF_TLS sections get added to both a PT_LOAD segment
+ // and the PT_TLS segment -- we do this grouping only for the
+ // PT_LOAD segment.
+ if (this->type_ != elfcpp::PT_TLS
+ && (os->flags() & elfcpp::SHF_TLS) != 0
+ && !this->output_data_.empty())
{
pdl = &this->output_data_;
bool nobits = os->type() == elfcpp::SHT_NOBITS;
return ret;
}
+// Return the number of dynamic relocs applied to this segment.
+
+unsigned int
+Output_segment::dynamic_reloc_count() const
+{
+ return (this->dynamic_reloc_count_list(&this->output_data_)
+ + this->dynamic_reloc_count_list(&this->output_bss_));
+}
+
+// Return the number of dynamic relocs applied to an Output_data_list.
+
+unsigned int
+Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
+{
+ unsigned int count = 0;
+ for (Output_data_list::const_iterator p = pdl->begin();
+ p != pdl->end();
+ ++p)
+ count += (*p)->dynamic_reloc_count();
+ return count;
+}
+
// Set the section addresses for an Output_segment. ADDR is the
// address and *POFF is the file offset. Set the section indexes
// starting with *PSHNDX. Return the address of the immediately
++p)
{
off = align_address(off, (*p)->addralign());
- (*p)->set_address(addr + (off - startoff), off);
+ (*p)->set_address_and_file_offset(addr + (off - startoff), off);
// Unless this is a PT_TLS segment, we want to ignore the size
// of a SHF_TLS/SHT_NOBITS section. Such a section does not
gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
this->o_ = o;
+ this->map();
+}
+
+// Resize the output file.
+
+void
+Output_file::resize(off_t file_size)
+{
+ if (::munmap(this->base_, this->file_size_) < 0)
+ gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
+ this->file_size_ = file_size;
+ this->map();
+}
+
+// Map the file into memory.
+
+void
+Output_file::map()
+{
+ int o = this->o_;
+
// Write out one byte to make the file the right size.
- if (::lseek(o, file_size - 1, SEEK_SET) < 0)
+ if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
char b = 0;
if (::write(o, &b, 1) != 1)
gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
// Map the file into memory.
- void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
+ void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
MAP_SHARED, o, 0);
if (base == MAP_FAILED)
gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));