// Output_data variables.
-bool Output_data::sizes_are_fixed;
+bool Output_data::allocated_sizes_are_fixed;
// Output_data methods.
{
}
-// Set the address and offset.
+// Return the default alignment for the target size.
-void
-Output_data::set_address(uint64_t addr, off_t off)
+uint64_t
+Output_data::default_alignment()
{
- this->address_ = addr;
- this->offset_ = off;
-
- // Let the child class know.
- this->do_set_address(addr, off);
+ return Output_data::default_alignment_for_size(parameters->get_size());
}
// Return the default alignment for a size--32 or 64.
uint64_t
-Output_data::default_alignment(int size)
+Output_data::default_alignment_for_size(int size)
{
if (size == 32)
return 4;
void
Output_file_header::do_write(Output_file* of)
{
+ gold_assert(this->offset() == 0);
+
if (parameters->get_size() == 32)
{
if (parameters->is_big_endian())
oehdr.put_e_ident(e_ident);
elfcpp::ET e_type;
- // FIXME: ET_DYN.
if (parameters->output_is_object())
e_type = elfcpp::ET_REL;
+ else if (parameters->output_is_shared())
+ e_type = elfcpp::ET_DYN;
else
e_type = elfcpp::ET_EXEC;
oehdr.put_e_type(e_type);
// 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());
index = this->u1_.os->symtab_index();
break;
+ case 0:
+ // Relocations without symbols use a symbol index of 0.
+ index = 0;
+ break;
+
default:
if (dynamic)
{
Output_section* os = this->u2_.relobj->output_section(this->shndx_,
&off);
gold_assert(os != NULL);
- address += os->address() + off;
+ if (off != -1)
+ address += os->address() + off;
+ else
+ {
+ address = os->output_address(this->u2_.relobj, this->shndx_,
+ address);
+ gold_assert(address != -1U);
+ }
}
else if (this->u2_.od != NULL)
address += this->u2_.od->address();
// If the symbol is resolved locally, we need to write out its
// value. Otherwise we just write zero. The target code is
// responsible for creating a relocation entry to fill in the
- // value at runtime.
- if (gsym->final_value_is_known())
+ // value at runtime. For non-preemptible symbols in a shared
+ // library, the target will need to record whether or not the
+ // value should be written (e.g., it may use a RELATIVE
+ // relocation type).
+ if (gsym->final_value_is_known() || gsym->needs_value_in_got())
{
Sized_symbol<size>* sgsym;
// This cast is a bit ugly. We don't want to put a
{
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 address into an output address.
+// Try to turn an input offset into an output offset.
bool
-Output_section::Input_section::output_address(const Relobj* object,
- unsigned int shndx,
- off_t offset,
- uint64_t output_section_address,
- uint64_t *poutput) const
+Output_section::Input_section::output_offset(const Relobj* object,
+ unsigned int shndx,
+ off_t offset,
+ off_t *poutput) const
{
if (!this->is_input_section())
- return this->u2_.posd->output_address(object, shndx, offset,
- output_section_address, poutput);
+ return this->u2_.posd->output_offset(object, shndx, offset, poutput);
else
{
- if (this->shndx_ != shndx
- || this->u2_.object != object)
+ if (this->shndx_ != shndx || this->u2_.object != object)
return false;
off_t output_offset;
Output_section* os = object->output_section(shndx, &output_offset);
gold_assert(os != NULL);
- *poutput = output_section_address + output_offset + offset;
+ gold_assert(output_offset != -1);
+ *poutput = output_offset + offset;
return true;
}
}
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.
info_(0),
type_(type),
flags_(flags),
- out_shndx_(0),
+ out_shndx_(-1U),
symtab_index_(0),
dynsym_index_(0),
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)
+ should_link_to_dynsym_(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()
}
// Add the input section SHNDX, with header SHDR, named SECNAME, in
-// OBJECT, to the Output_section. Return the offset of the input
-// section within the output section. We don't always keep track of
-// input sections for an Output_section. Instead, each Object keeps
-// track of the Output_section for each of its input sections.
+// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
+// relocation section which applies to this section, or 0 if none, or
+// -1U if more than one. Return the offset of the input section
+// within the output section. Return -1 if the input section will
+// receive special handling. In the normal case we don't always keep
+// track of input sections for an Output_section. Instead, each
+// Object keeps track of the Output_section for each of its input
+// sections.
template<int size, bool big_endian>
off_t
-Output_section::add_input_section(Relobj* object, unsigned int shndx,
+Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
+ unsigned int shndx,
const char* secname,
- const elfcpp::Shdr<size, big_endian>& shdr)
+ const elfcpp::Shdr<size, big_endian>& shdr,
+ unsigned int reloc_shndx)
{
elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
if ((addralign & (addralign - 1)) != 0)
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.
- if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
+ // a Output_data_merge. We don't try to handle relocations for such
+ // a section.
+ 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_address method to determine the final address.
+ // output_offset method to determine the final address.
return -1;
}
}
- 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);
uint64_t flags, uint64_t entsize,
uint64_t addralign)
{
- // We only merge constants if the alignment is not more than the
- // entry size. This could be handled, but it's unusual.
- if (addralign > entsize)
+ bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
+
+ // We only merge strings if the alignment is not more than the
+ // character size. This could be handled, but it's unusual.
+ if (is_string && addralign > entsize)
return false;
- bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
Input_section_list::iterator p;
for (p = this->input_sections_.begin();
p != this->input_sections_.end();
++p)
- if (p->is_merge_section(is_string, entsize))
- break;
+ if (p->is_merge_section(is_string, entsize, addralign))
+ {
+ 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);
- else if (entsize == 1)
- posd = new Output_merge_string<char>();
- else if (entsize == 2)
- posd = new Output_merge_string<uint16_t>();
- else if (entsize == 4)
- posd = new Output_merge_string<uint32_t>();
- else
- return false;
+ 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;
+}
+
+// Given an address OFFSET relative to the start of input section
+// SHNDX in OBJECT, return whether this address is being included in
+// the final link. This should only be called if SHNDX in OBJECT has
+// a special mapping.
- this->add_output_merge_section(posd, is_string, entsize);
- posd->add_input_section(object, shndx);
+bool
+Output_section::is_input_address_mapped(const Relobj* object,
+ unsigned int shndx,
+ off_t offset) const
+{
+ gold_assert(object->is_section_specially_mapped(shndx));
+
+ for (Input_section_list::const_iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p)
+ {
+ off_t output_offset;
+ if (p->output_offset(object, shndx, offset, &output_offset))
+ return output_offset != -1;
}
+ // By default we assume that the address is mapped. This should
+ // only be called after we have passed all sections to Layout. At
+ // that point we should know what we are discarding.
return true;
}
+// Given an address OFFSET relative to the start of input section
+// SHNDX in object OBJECT, return the output offset relative to the
+// start of the section. This should only be called if SHNDX in
+// OBJECT has a special mapping.
+
+off_t
+Output_section::output_offset(const Relobj* object, unsigned int shndx,
+ off_t offset) const
+{
+ gold_assert(object->is_section_specially_mapped(shndx));
+ // This can only be called meaningfully when layout is complete.
+ gold_assert(Output_data::is_layout_complete());
+
+ for (Input_section_list::const_iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p)
+ {
+ off_t output_offset;
+ if (p->output_offset(object, shndx, offset, &output_offset))
+ return output_offset;
+ }
+ gold_unreachable();
+}
+
// Return the output virtual address of OFFSET relative to the start
// of input section SHNDX in object OBJECT.
Output_section::output_address(const Relobj* object, unsigned int shndx,
off_t offset) const
{
+ gold_assert(object->is_section_specially_mapped(shndx));
+ // This can only be called meaningfully when layout is complete.
+ gold_assert(Output_data::is_layout_complete());
+
uint64_t addr = this->address() + this->first_input_offset_;
for (Input_section_list::const_iterator p = this->input_sections_.begin();
p != this->input_sections_.end();
++p)
{
addr = align_address(addr, p->addralign());
- uint64_t output;
- if (p->output_address(object, shndx, offset, addr, &output))
- return output;
+ off_t output_offset;
+ if (p->output_offset(object, shndx, offset, &output_offset))
+ {
+ if (output_offset == -1)
+ return -1U;
+ return addr + output_offset;
+ }
addr += p->data_size();
}
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));
template
off_t
Output_section::add_input_section<32, false>(
- Relobj* object,
+ Sized_relobj<32, false>* object,
unsigned int shndx,
const char* secname,
- const elfcpp::Shdr<32, false>& shdr);
+ const elfcpp::Shdr<32, false>& shdr,
+ unsigned int reloc_shndx);
#endif
#ifdef HAVE_TARGET_32_BIG
template
off_t
Output_section::add_input_section<32, true>(
- Relobj* object,
+ Sized_relobj<32, true>* object,
unsigned int shndx,
const char* secname,
- const elfcpp::Shdr<32, true>& shdr);
+ const elfcpp::Shdr<32, true>& shdr,
+ unsigned int reloc_shndx);
#endif
#ifdef HAVE_TARGET_64_LITTLE
template
off_t
Output_section::add_input_section<64, false>(
- Relobj* object,
+ Sized_relobj<64, false>* object,
unsigned int shndx,
const char* secname,
- const elfcpp::Shdr<64, false>& shdr);
+ const elfcpp::Shdr<64, false>& shdr,
+ unsigned int reloc_shndx);
#endif
#ifdef HAVE_TARGET_64_BIG
template
off_t
Output_section::add_input_section<64, true>(
- Relobj* object,
+ Sized_relobj<64, true>* object,
unsigned int shndx,
const char* secname,
- const elfcpp::Shdr<64, true>& shdr);
+ const elfcpp::Shdr<64, true>& shdr,
+ unsigned int reloc_shndx);
#endif
#ifdef HAVE_TARGET_32_LITTLE