// output.cc -- manage the output file for gold
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
#include "gold.h"
#include <cstdlib>
+#include <cstring>
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
uint64_t
Output_data::default_alignment()
{
- return Output_data::default_alignment_for_size(parameters->get_size());
+ return Output_data::default_alignment_for_size(
+ parameters->target().get_size());
}
// Return the default alignment for a size--32 or 64.
const Layout::Segment_list* segment_list,
const Layout::Section_list* section_list,
const Layout::Section_list* unattached_section_list,
- const Stringpool* secnamepool)
+ const Stringpool* secnamepool,
+ const Output_section* shstrtab_section)
: layout_(layout),
segment_list_(segment_list),
section_list_(section_list),
unattached_section_list_(unattached_section_list),
- secnamepool_(secnamepool)
+ secnamepool_(secnamepool),
+ shstrtab_section_(shstrtab_section)
{
// Count all the sections. Start with 1 for the null section.
off_t count = 1;
- if (!parameters->output_is_object())
+ if (!parameters->options().relocatable())
{
for (Layout::Segment_list::const_iterator p = segment_list->begin();
p != segment_list->end();
}
count += unattached_section_list->size();
- const int size = parameters->get_size();
+ const int size = parameters->target().get_size();
int shdr_size;
if (size == 32)
shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
void
Output_section_headers::do_write(Output_file* of)
{
- if (parameters->get_size() == 32)
+ switch (parameters->size_and_endianness())
{
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_32_BIG
- this->do_sized_write<32, true>(of);
-#else
- gold_unreachable();
-#endif
- }
- else
- {
#ifdef HAVE_TARGET_32_LITTLE
- this->do_sized_write<32, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_32_LITTLE:
+ this->do_sized_write<32, false>(of);
+ break;
#endif
- }
- }
- else if (parameters->get_size() == 64)
- {
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_64_BIG
- this->do_sized_write<64, true>(of);
-#else
- gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+ case Parameters::TARGET_32_BIG:
+ this->do_sized_write<32, true>(of);
+ break;
#endif
- }
- else
- {
#ifdef HAVE_TARGET_64_LITTLE
- this->do_sized_write<64, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_64_LITTLE:
+ this->do_sized_write<64, false>(of);
+ break;
#endif
- }
+#ifdef HAVE_TARGET_64_BIG
+ case Parameters::TARGET_64_BIG:
+ this->do_sized_write<64, true>(of);
+ break;
+#endif
+ default:
+ gold_unreachable();
}
- else
- gold_unreachable();
}
template<int size, bool big_endian>
oshdr.put_sh_flags(0);
oshdr.put_sh_addr(0);
oshdr.put_sh_offset(0);
- oshdr.put_sh_size(0);
- oshdr.put_sh_link(0);
+
+ size_t section_count = (this->data_size()
+ / elfcpp::Elf_sizes<size>::shdr_size);
+ if (section_count < elfcpp::SHN_LORESERVE)
+ oshdr.put_sh_size(0);
+ else
+ oshdr.put_sh_size(section_count);
+
+ unsigned int shstrndx = this->shstrtab_section_->out_shndx();
+ if (shstrndx < elfcpp::SHN_LORESERVE)
+ oshdr.put_sh_link(0);
+ else
+ oshdr.put_sh_link(shstrndx);
+
oshdr.put_sh_info(0);
oshdr.put_sh_addralign(0);
oshdr.put_sh_entsize(0);
v += shdr_size;
unsigned int shndx = 1;
- if (!parameters->output_is_object())
+ if (!parameters->options().relocatable())
{
for (Layout::Segment_list::const_iterator p =
this->segment_list_->begin();
// For a relocatable link, we did unallocated group sections
// above, since they have to come first.
if ((*p)->type() == elfcpp::SHT_GROUP
- && parameters->output_is_object())
+ && parameters->options().relocatable())
continue;
gold_assert(shndx == (*p)->out_shndx());
elfcpp::Shdr_write<size, big_endian> oshdr(v);
const Layout::Segment_list& segment_list)
: segment_list_(segment_list)
{
- const int size = parameters->get_size();
+ const int size = parameters->target().get_size();
int phdr_size;
if (size == 32)
phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
void
Output_segment_headers::do_write(Output_file* of)
{
- if (parameters->get_size() == 32)
+ switch (parameters->size_and_endianness())
{
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_32_BIG
- this->do_sized_write<32, true>(of);
-#else
- gold_unreachable();
-#endif
- }
- else
- {
#ifdef HAVE_TARGET_32_LITTLE
- this->do_sized_write<32, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_32_LITTLE:
+ this->do_sized_write<32, false>(of);
+ break;
#endif
- }
- }
- else if (parameters->get_size() == 64)
- {
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_64_BIG
- this->do_sized_write<64, true>(of);
-#else
- gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+ case Parameters::TARGET_32_BIG:
+ this->do_sized_write<32, true>(of);
+ break;
#endif
- }
- else
- {
#ifdef HAVE_TARGET_64_LITTLE
- this->do_sized_write<64, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_64_LITTLE:
+ this->do_sized_write<64, false>(of);
+ break;
#endif
- }
+#ifdef HAVE_TARGET_64_BIG
+ case Parameters::TARGET_64_BIG:
+ this->do_sized_write<64, true>(of);
+ break;
+#endif
+ default:
+ gold_unreachable();
}
- else
- gold_unreachable();
}
template<int size, bool big_endian>
shstrtab_(NULL),
entry_(entry)
{
- const int size = parameters->get_size();
+ const int size = parameters->target().get_size();
int ehdr_size;
if (size == 32)
ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
{
gold_assert(this->offset() == 0);
- if (parameters->get_size() == 32)
+ switch (parameters->size_and_endianness())
{
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_32_BIG
- this->do_sized_write<32, true>(of);
-#else
- gold_unreachable();
-#endif
- }
- else
- {
#ifdef HAVE_TARGET_32_LITTLE
- this->do_sized_write<32, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_32_LITTLE:
+ this->do_sized_write<32, false>(of);
+ break;
#endif
- }
- }
- else if (parameters->get_size() == 64)
- {
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_64_BIG
- this->do_sized_write<64, true>(of);
-#else
- gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+ case Parameters::TARGET_32_BIG:
+ this->do_sized_write<32, true>(of);
+ break;
#endif
- }
- else
- {
#ifdef HAVE_TARGET_64_LITTLE
- this->do_sized_write<64, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_64_LITTLE:
+ this->do_sized_write<64, false>(of);
+ break;
#endif
- }
+#ifdef HAVE_TARGET_64_BIG
+ case Parameters::TARGET_64_BIG:
+ this->do_sized_write<64, true>(of);
+ break;
+#endif
+ default:
+ gold_unreachable();
}
- else
- gold_unreachable();
}
// Write out the file header with appropriate size and endianess.
oehdr.put_e_ident(e_ident);
elfcpp::ET e_type;
- if (parameters->output_is_object())
+ if (parameters->options().relocatable())
e_type = elfcpp::ET_REL;
- else if (parameters->output_is_shared())
+ else if (parameters->options().shared())
e_type = elfcpp::ET_DYN;
else
e_type = elfcpp::ET_EXEC;
}
oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
- oehdr.put_e_shnum(this->section_header_->data_size()
- / elfcpp::Elf_sizes<size>::shdr_size);
- oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
+ size_t section_count = (this->section_header_->data_size()
+ / elfcpp::Elf_sizes<size>::shdr_size);
+
+ if (section_count < elfcpp::SHN_LORESERVE)
+ oehdr.put_e_shnum(this->section_header_->data_size()
+ / elfcpp::Elf_sizes<size>::shdr_size);
+ else
+ oehdr.put_e_shnum(0);
+
+ unsigned int shstrndx = this->shstrtab_->out_shndx();
+ if (shstrndx < elfcpp::SHN_LORESERVE)
+ oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
+ else
+ oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
of->write_output_view(0, ehdr_size, view);
}
Output_file_header::entry()
{
const bool should_issue_warning = (this->entry_ != NULL
- && parameters->output_is_executable());
+ && !parameters->options().relocatable()
+ && !parameters->options().shared());
// FIXME: Need to support target specific entry symbol.
const char* entry = this->entry_;
return this->output_section_->out_shndx();
}
+// Set the alignment, which means we may need to update the alignment
+// of the output section.
+
+void
+Output_section_data::set_addralign(uint64_t addralign)
+{
+ this->addralign_ = addralign;
+ if (this->output_section_ != NULL
+ && this->output_section_->addralign() < addralign)
+ this->output_section_->set_addralign(addralign);
+}
+
// Output_data_strtab methods.
// Set the final data size.
Address address,
bool is_relative)
: address_(address), local_sym_index_(GSYM_CODE), type_(type),
- is_relative_(is_relative), shndx_(INVALID_CODE)
+ is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
{
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.gsym = gsym;
this->u2_.od = od;
- if (dynamic && !is_relative)
- gsym->set_needs_dynsym_entry();
+ if (dynamic)
+ this->set_needs_dynsym_index();
}
template<bool dynamic, int size, bool big_endian>
Address address,
bool is_relative)
: address_(address), local_sym_index_(GSYM_CODE), type_(type),
- is_relative_(is_relative), shndx_(shndx)
+ is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
{
gold_assert(shndx != INVALID_CODE);
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.gsym = gsym;
this->u2_.relobj = relobj;
- if (dynamic && !is_relative)
- gsym->set_needs_dynsym_entry();
+ if (dynamic)
+ this->set_needs_dynsym_index();
}
// A reloc against a local symbol.
unsigned int type,
Output_data* od,
Address address,
- bool is_relative)
+ bool is_relative,
+ bool is_section_symbol)
: address_(address), local_sym_index_(local_sym_index), type_(type),
- is_relative_(is_relative), shndx_(INVALID_CODE)
+ is_relative_(is_relative), is_section_symbol_(is_section_symbol),
+ shndx_(INVALID_CODE)
{
gold_assert(local_sym_index != GSYM_CODE
&& local_sym_index != INVALID_CODE);
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.relobj = relobj;
this->u2_.od = od;
- if (dynamic && !is_relative)
- relobj->set_needs_output_dynsym_entry(local_sym_index);
+ if (dynamic)
+ this->set_needs_dynsym_index();
}
template<bool dynamic, int size, bool big_endian>
unsigned int type,
unsigned int shndx,
Address address,
- bool is_relative)
+ bool is_relative,
+ bool is_section_symbol)
: address_(address), local_sym_index_(local_sym_index), type_(type),
- is_relative_(is_relative), shndx_(shndx)
+ is_relative_(is_relative), is_section_symbol_(is_section_symbol),
+ shndx_(shndx)
{
gold_assert(local_sym_index != GSYM_CODE
&& local_sym_index != INVALID_CODE);
gold_assert(shndx != INVALID_CODE);
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.relobj = relobj;
this->u2_.relobj = relobj;
- if (dynamic && !is_relative)
- relobj->set_needs_output_dynsym_entry(local_sym_index);
+ if (dynamic)
+ this->set_needs_dynsym_index();
}
// A reloc against the STT_SECTION symbol of an output section.
Output_data* od,
Address address)
: address_(address), local_sym_index_(SECTION_CODE), type_(type),
- is_relative_(false), shndx_(INVALID_CODE)
+ is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
{
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.os = os;
this->u2_.od = od;
if (dynamic)
- os->set_needs_dynsym_index();
+ this->set_needs_dynsym_index();
+ else
+ os->set_needs_symtab_index();
}
template<bool dynamic, int size, bool big_endian>
unsigned int shndx,
Address address)
: address_(address), local_sym_index_(SECTION_CODE), type_(type),
- is_relative_(false), shndx_(shndx)
+ is_relative_(false), is_section_symbol_(true), shndx_(shndx)
{
gold_assert(shndx != INVALID_CODE);
+ // this->type_ is a bitfield; make sure TYPE fits.
+ gold_assert(this->type_ == type);
this->u1_.os = os;
this->u2_.relobj = relobj;
if (dynamic)
- os->set_needs_dynsym_index();
+ this->set_needs_dynsym_index();
+ else
+ os->set_needs_symtab_index();
+}
+
+// Record that we need a dynamic symbol index for this relocation.
+
+template<bool dynamic, int size, bool big_endian>
+void
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
+set_needs_dynsym_index()
+{
+ if (this->is_relative_)
+ return;
+ switch (this->local_sym_index_)
+ {
+ case INVALID_CODE:
+ gold_unreachable();
+
+ case GSYM_CODE:
+ this->u1_.gsym->set_needs_dynsym_entry();
+ break;
+
+ case SECTION_CODE:
+ this->u1_.os->set_needs_dynsym_index();
+ break;
+
+ case 0:
+ break;
+
+ default:
+ {
+ const unsigned int lsi = this->local_sym_index_;
+ if (!this->is_section_symbol_)
+ this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
+ else
+ {
+ section_offset_type dummy;
+ Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
+ gold_assert(os != NULL);
+ os->set_needs_dynsym_index();
+ }
+ }
+ break;
+ }
}
// Get the symbol index of a relocation.
break;
default:
- if (dynamic)
- index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
- else
- index = this->u1_.relobj->symtab_index(this->local_sym_index_);
+ {
+ const unsigned int lsi = this->local_sym_index_;
+ if (!this->is_section_symbol_)
+ {
+ if (dynamic)
+ index = this->u1_.relobj->dynsym_index(lsi);
+ else
+ index = this->u1_.relobj->symtab_index(lsi);
+ }
+ else
+ {
+ section_offset_type dummy;
+ Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
+ gold_assert(os != NULL);
+ if (dynamic)
+ index = os->dynsym_index();
+ else
+ index = os->symtab_index();
+ }
+ }
break;
}
gold_assert(index != -1U);
return index;
}
+// For a local section symbol, get the address of the offset ADDEND
+// within the input section.
+
+template<bool dynamic, int size, bool big_endian>
+section_offset_type
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
+ local_section_offset(Addend addend) const
+{
+ gold_assert(this->local_sym_index_ != GSYM_CODE
+ && this->local_sym_index_ != SECTION_CODE
+ && this->local_sym_index_ != INVALID_CODE
+ && this->is_section_symbol_);
+ const unsigned int lsi = this->local_sym_index_;
+ section_offset_type offset;
+ Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
+ gold_assert(os != NULL);
+ if (offset != -1)
+ return offset + addend;
+ // This is a merge section.
+ offset = os->output_address(this->u1_.relobj, lsi, addend);
+ gold_assert(offset != -1);
+ return offset;
+}
+
// Write out the offset and info fields of a Rel or Rela relocation
// entry.
template<bool dynamic, int size, bool big_endian>
typename elfcpp::Elf_types<size>::Elf_Addr
-Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
+Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
+ Addend addend) const
{
if (this->local_sym_index_ == GSYM_CODE)
{
const Sized_symbol<size>* sym;
sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
- return sym->value();
+ return sym->value() + addend;
}
gold_assert(this->local_sym_index_ != SECTION_CODE
- && this->local_sym_index_ != INVALID_CODE);
- const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
- return relobj->local_symbol_value(this->local_sym_index_);
+ && this->local_sym_index_ != INVALID_CODE
+ && !this->is_section_symbol_);
+ const unsigned int lsi = this->local_sym_index_;
+ const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
+ return symval->value(this->u1_.relobj, addend);
}
// Write out a Rela relocation.
elfcpp::Rela_write<size, big_endian> orel(pov);
this->rel_.write_rel(&orel);
Addend addend = this->addend_;
- if (rel_.is_relative())
- addend += rel_.symbol_value();
+ if (this->rel_.is_relative())
+ addend = this->rel_.symbol_value(addend);
+ else if (this->rel_.is_local_section_symbol())
+ addend = this->rel_.local_section_offset(addend);
orel.put_r_addend(addend);
}
Output_data_group<size, big_endian>::Output_data_group(
Sized_relobj<size, big_endian>* relobj,
section_size_type entry_count,
- const elfcpp::Elf_Word* contents)
+ elfcpp::Elf_Word flags,
+ std::vector<unsigned int>* input_shndxes)
: Output_section_data(entry_count * 4, 4),
- relobj_(relobj)
+ relobj_(relobj),
+ flags_(flags)
{
- this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
- for (section_size_type i = 1; i < entry_count; ++i)
- {
- unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
- this->input_sections_.push_back(shndx);
- }
+ this->input_shndxes_.swap(*input_shndxes);
}
// Write out the section group, which means translating the section
++contents;
for (std::vector<unsigned int>::const_iterator p =
- this->input_sections_.begin();
- p != this->input_sections_.end();
+ this->input_shndxes_.begin();
+ p != this->input_shndxes_.end();
++p, ++contents)
{
section_offset_type dummy;
of->write_output_view(off, oview_size, oview);
// We no longer need this information.
- this->input_sections_.clear();
+ this->input_shndxes_.clear();
}
// Output_data_got::Got_entry methods.
break;
default:
- val = this->u_.object->local_symbol_value(this->local_sym_index_);
+ {
+ const unsigned int lsi = this->local_sym_index_;
+ const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
+ val = symval->value(this->u_.object, 0);
+ }
break;
}
template<int size, bool big_endian>
bool
-Output_data_got<size, big_endian>::add_global(Symbol* gsym)
+Output_data_got<size, big_endian>::add_global(
+ Symbol* gsym,
+ unsigned int got_type)
{
- if (gsym->has_got_offset())
+ if (gsym->has_got_offset(got_type))
return false;
this->entries_.push_back(Got_entry(gsym));
this->set_got_size();
- gsym->set_got_offset(this->last_got_offset());
+ gsym->set_got_offset(got_type, this->last_got_offset());
return true;
}
void
Output_data_got<size, big_endian>::add_global_with_rel(
Symbol* gsym,
+ unsigned int got_type,
Rel_dyn* rel_dyn,
unsigned int r_type)
{
- if (gsym->has_got_offset())
+ if (gsym->has_got_offset(got_type))
return;
this->entries_.push_back(Got_entry());
this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- gsym->set_got_offset(got_offset);
+ gsym->set_got_offset(got_type, got_offset);
rel_dyn->add_global(gsym, r_type, this, got_offset);
}
void
Output_data_got<size, big_endian>::add_global_with_rela(
Symbol* gsym,
+ unsigned int got_type,
Rela_dyn* rela_dyn,
unsigned int r_type)
{
- if (gsym->has_got_offset())
+ if (gsym->has_got_offset(got_type))
return;
this->entries_.push_back(Got_entry());
this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- gsym->set_got_offset(got_offset);
+ gsym->set_got_offset(got_type, got_offset);
rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
}
-// Add an entry for a local symbol to the GOT. 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(
- Sized_relobj<size, big_endian>* object,
- unsigned int symndx)
-{
- 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 for a local symbol to the GOT, and add a dynamic
-// relocation of type R_TYPE for the GOT entry.
+// Add a pair of entries for a global symbol to the GOT, and add
+// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
+// If R_TYPE_2 == 0, add the second entry with no relocation.
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_local_with_rel(
- Sized_relobj<size, big_endian>* object,
- unsigned int symndx,
+Output_data_got<size, big_endian>::add_global_pair_with_rel(
+ Symbol* gsym,
+ unsigned int got_type,
Rel_dyn* rel_dyn,
- unsigned int r_type)
+ unsigned int r_type_1,
+ unsigned int r_type_2)
{
- if (object->local_has_got_offset(symndx))
+ if (gsym->has_got_offset(got_type))
return;
this->entries_.push_back(Got_entry());
- this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- object->set_local_got_offset(symndx, got_offset);
- rel_dyn->add_local(object, symndx, r_type, this, got_offset);
+ gsym->set_got_offset(got_type, got_offset);
+ rel_dyn->add_global(gsym, r_type_1, this, got_offset);
+
+ this->entries_.push_back(Got_entry());
+ if (r_type_2 != 0)
+ {
+ got_offset = this->last_got_offset();
+ rel_dyn->add_global(gsym, r_type_2, this, got_offset);
+ }
+
+ this->set_got_size();
}
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_local_with_rela(
- Sized_relobj<size, big_endian>* object,
- unsigned int symndx,
+Output_data_got<size, big_endian>::add_global_pair_with_rela(
+ Symbol* gsym,
+ unsigned int got_type,
Rela_dyn* rela_dyn,
- unsigned int r_type)
+ unsigned int r_type_1,
+ unsigned int r_type_2)
{
- if (object->local_has_got_offset(symndx))
+ if (gsym->has_got_offset(got_type))
return;
this->entries_.push_back(Got_entry());
- this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- object->set_local_got_offset(symndx, got_offset);
- rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
+ gsym->set_got_offset(got_type, got_offset);
+ rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
+
+ this->entries_.push_back(Got_entry());
+ if (r_type_2 != 0)
+ {
+ got_offset = this->last_got_offset();
+ rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
+ }
+
+ this->set_got_size();
}
-// 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.
+// Add an entry for a local symbol to the GOT. 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)
+Output_data_got<size, big_endian>::add_local(
+ Sized_relobj<size, big_endian>* object,
+ unsigned int symndx,
+ unsigned int got_type)
{
- if (gsym->has_tls_got_offset(need_pair))
+ if (object->local_has_got_offset(symndx, got_type))
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->entries_.push_back(Got_entry(object, symndx));
this->set_got_size();
+ object->set_local_got_offset(symndx, got_type, this->last_got_offset());
return true;
}
-// Add an entry for a global TLS symbol to the GOT, and add a dynamic
-// relocation of type R_TYPE.
+// Add an entry for a local symbol to the GOT, and add a dynamic
+// relocation of type R_TYPE for the GOT entry.
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_global_tls_with_rel(
- Symbol* gsym,
+Output_data_got<size, big_endian>::add_local_with_rel(
+ Sized_relobj<size, big_endian>* object,
+ unsigned int symndx,
+ unsigned int got_type,
Rel_dyn* rel_dyn,
unsigned int r_type)
{
- if (gsym->has_tls_got_offset(false))
+ if (object->local_has_got_offset(symndx, got_type))
return;
this->entries_.push_back(Got_entry());
this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- gsym->set_tls_got_offset(got_offset, false);
- rel_dyn->add_global(gsym, r_type, this, got_offset);
+ object->set_local_got_offset(symndx, got_type, got_offset);
+ rel_dyn->add_local(object, symndx, r_type, this, got_offset);
}
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_global_tls_with_rela(
- Symbol* gsym,
+Output_data_got<size, big_endian>::add_local_with_rela(
+ Sized_relobj<size, big_endian>* object,
+ unsigned int symndx,
+ unsigned int got_type,
Rela_dyn* rela_dyn,
unsigned int r_type)
{
- if (gsym->has_tls_got_offset(false))
+ if (object->local_has_got_offset(symndx, got_type))
return;
this->entries_.push_back(Got_entry());
this->set_got_size();
unsigned int got_offset = this->last_got_offset();
- gsym->set_tls_got_offset(got_offset, false);
- rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
-}
-
-// Add a pair of entries for a global TLS symbol to the GOT, and add
-// dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
-template<int size, bool big_endian>
-void
-Output_data_got<size, big_endian>::add_global_tls_with_rel(
- Symbol* gsym,
- Rel_dyn* rel_dyn,
- unsigned int mod_r_type,
- unsigned int dtv_r_type)
-{
- if (gsym->has_tls_got_offset(true))
- return;
-
- this->entries_.push_back(Got_entry());
- unsigned int got_offset = this->last_got_offset();
- gsym->set_tls_got_offset(got_offset, true);
- rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
-
- this->entries_.push_back(Got_entry());
- this->set_got_size();
- got_offset = this->last_got_offset();
- rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
-}
-
-template<int size, bool big_endian>
-void
-Output_data_got<size, big_endian>::add_global_tls_with_rela(
- Symbol* gsym,
- Rela_dyn* rela_dyn,
- unsigned int mod_r_type,
- unsigned int dtv_r_type)
-{
- if (gsym->has_tls_got_offset(true))
- return;
-
- this->entries_.push_back(Got_entry());
- unsigned int got_offset = this->last_got_offset();
- gsym->set_tls_got_offset(got_offset, true);
- rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
-
- this->entries_.push_back(Got_entry());
- this->set_got_size();
- got_offset = this->last_got_offset();
- rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
-}
-
-// 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;
+ object->set_local_got_offset(symndx, got_type, got_offset);
+ rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
}
-// Add an entry (or pair of entries) for a local TLS symbol to the GOT,
-// and add a dynamic relocation of type R_TYPE for the first GOT entry.
-// Because this is a local symbol, the first GOT entry can be relocated
-// relative to a section symbol, and the second GOT entry will have an
-// dtv-relative value that can be computed at link time.
+// Add a pair of entries for a local symbol to the GOT, and add
+// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
+// If R_TYPE_2 == 0, add the second entry with no relocation.
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_local_tls_with_rel(
+Output_data_got<size, big_endian>::add_local_pair_with_rel(
Sized_relobj<size, big_endian>* object,
unsigned int symndx,
unsigned int shndx,
- bool need_pair,
+ unsigned int got_type,
Rel_dyn* rel_dyn,
- unsigned int r_type)
+ unsigned int r_type_1,
+ unsigned int r_type_2)
{
- if (object->local_has_tls_got_offset(symndx, need_pair))
+ if (object->local_has_got_offset(symndx, got_type))
return;
this->entries_.push_back(Got_entry());
unsigned int got_offset = this->last_got_offset();
- object->set_local_tls_got_offset(symndx, got_offset, need_pair);
+ object->set_local_got_offset(symndx, got_type, got_offset);
section_offset_type off;
Output_section* os = object->output_section(shndx, &off);
- rel_dyn->add_output_section(os, r_type, this, got_offset);
+ rel_dyn->add_output_section(os, r_type_1, this, got_offset);
- // The second entry of the pair will be statically initialized
- // with the TLS offset of the symbol.
- if (need_pair)
- this->entries_.push_back(Got_entry(object, symndx));
+ this->entries_.push_back(Got_entry(object, symndx));
+ if (r_type_2 != 0)
+ {
+ got_offset = this->last_got_offset();
+ rel_dyn->add_output_section(os, r_type_2, this, got_offset);
+ }
this->set_got_size();
}
template<int size, bool big_endian>
void
-Output_data_got<size, big_endian>::add_local_tls_with_rela(
+Output_data_got<size, big_endian>::add_local_pair_with_rela(
Sized_relobj<size, big_endian>* object,
unsigned int symndx,
unsigned int shndx,
- bool need_pair,
+ unsigned int got_type,
Rela_dyn* rela_dyn,
- unsigned int r_type)
+ unsigned int r_type_1,
+ unsigned int r_type_2)
{
- if (object->local_has_tls_got_offset(symndx, need_pair))
+ if (object->local_has_got_offset(symndx, got_type))
return;
this->entries_.push_back(Got_entry());
unsigned int got_offset = this->last_got_offset();
- object->set_local_tls_got_offset(symndx, got_offset, need_pair);
+ object->set_local_got_offset(symndx, got_type, got_offset);
section_offset_type off;
Output_section* os = object->output_section(shndx, &off);
- rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
+ rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
- // The second entry of the pair will be statically initialized
- // with the TLS offset of the symbol.
- if (need_pair)
- this->entries_.push_back(Got_entry(object, symndx));
+ this->entries_.push_back(Got_entry(object, symndx));
+ if (r_type_2 != 0)
+ {
+ got_offset = this->last_got_offset();
+ rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
+ }
this->set_got_size();
}
void
Output_data_dynamic::Dynamic_entry::write(
unsigned char* pov,
- const Stringpool* pool
- ACCEPT_SIZE_ENDIAN) const
+ const Stringpool* pool) const
{
typename elfcpp::Elf_types<size>::Elf_WXword val;
- switch (this->classification_)
+ switch (this->offset_)
{
case DYNAMIC_NUMBER:
val = this->u_.val;
break;
- case DYNAMIC_SECTION_ADDRESS:
- val = this->u_.od->address();
- break;
-
case DYNAMIC_SECTION_SIZE:
val = this->u_.od->data_size();
break;
break;
default:
- gold_unreachable();
+ val = this->u_.od->address() + this->offset_;
+ break;
}
elfcpp::Dyn_write<size, big_endian> dw(pov);
void
Output_data_dynamic::do_adjust_output_section(Output_section* os)
{
- if (parameters->get_size() == 32)
+ if (parameters->target().get_size() == 32)
os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
- else if (parameters->get_size() == 64)
+ else if (parameters->target().get_size() == 64)
os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
else
gold_unreachable();
this->add_constant(elfcpp::DT_NULL, 0);
int dyn_size;
- if (parameters->get_size() == 32)
+ if (parameters->target().get_size() == 32)
dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
- else if (parameters->get_size() == 64)
+ else if (parameters->target().get_size() == 64)
dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
else
gold_unreachable();
void
Output_data_dynamic::do_write(Output_file* of)
{
- if (parameters->get_size() == 32)
+ switch (parameters->size_and_endianness())
{
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_32_BIG
- this->sized_write<32, true>(of);
-#else
- gold_unreachable();
-#endif
- }
- else
- {
#ifdef HAVE_TARGET_32_LITTLE
- this->sized_write<32, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_32_LITTLE:
+ this->sized_write<32, false>(of);
+ break;
#endif
- }
- }
- else if (parameters->get_size() == 64)
- {
- if (parameters->is_big_endian())
- {
-#ifdef HAVE_TARGET_64_BIG
- this->sized_write<64, true>(of);
-#else
- gold_unreachable();
+#ifdef HAVE_TARGET_32_BIG
+ case Parameters::TARGET_32_BIG:
+ this->sized_write<32, true>(of);
+ break;
#endif
- }
- else
- {
#ifdef HAVE_TARGET_64_LITTLE
- this->sized_write<64, false>(of);
-#else
- gold_unreachable();
+ case Parameters::TARGET_64_LITTLE:
+ this->sized_write<64, false>(of);
+ break;
#endif
- }
+#ifdef HAVE_TARGET_64_BIG
+ case Parameters::TARGET_64_BIG:
+ this->sized_write<64, true>(of);
+ break;
+#endif
+ default:
+ gold_unreachable();
}
- else
- gold_unreachable();
}
template<int size, bool big_endian>
p != this->entries_.end();
++p)
{
- p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
- pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
+ p->write<size, big_endian>(pov, this->pool_);
pov += dyn_size;
}
this->entries_.clear();
}
+// Class Output_symtab_xindex.
+
+void
+Output_symtab_xindex::do_write(Output_file* of)
+{
+ const off_t offset = this->offset();
+ const off_t oview_size = this->data_size();
+ unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+ memset(oview, 0, oview_size);
+
+ if (parameters->target().is_big_endian())
+ this->endian_do_write<true>(oview);
+ else
+ this->endian_do_write<false>(oview);
+
+ of->write_output_view(offset, oview_size, oview);
+
+ // We no longer need the data.
+ this->entries_.clear();
+}
+
+template<bool big_endian>
+void
+Output_symtab_xindex::endian_do_write(unsigned char* const oview)
+{
+ for (Xindex_entries::const_iterator p = this->entries_.begin();
+ p != this->entries_.end();
+ ++p)
+ elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
+}
+
// Output_section::Input_section methods.
// Return the data size. For an input section we store the size here.
found_in_sections_clause_(false),
has_load_address_(false),
info_uses_section_index_(false),
+ may_sort_attached_input_sections_(false),
+ must_sort_attached_input_sections_(false),
+ attached_input_sections_are_sorted_(false),
tls_offset_(0)
{
// An unallocated section has no address. Forcing this means that
this->addralign_ = addralign;
typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
- this->flags_ |= (sh_flags
- & (elfcpp::SHF_WRITE
- | elfcpp::SHF_ALLOC
- | elfcpp::SHF_EXECINSTR));
+ this->update_flags_for_input_section(sh_flags);
uint64_t entsize = shdr.get_sh_entsize();
+ 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
+ // track of sections, or if we are relaxing. Also, if this is a
+ // section which requires sorting, or which may require sorting in
+ // the future, we keep track of the sections. FIXME: Add test for
// relaxing.
- if (have_sections_script || !this->input_sections_.empty())
+ if (have_sections_script
+ || !this->input_sections_.empty()
+ || this->may_sort_attached_input_sections()
+ || this->must_sort_attached_input_sections())
this->input_sections_.push_back(Input_section(object, shndx,
shdr.get_sh_size(),
addralign));
return;
}
+ if (this->must_sort_attached_input_sections())
+ this->sort_attached_input_sections();
+
uint64_t address = this->address();
off_t startoff = this->offset();
off_t off = startoff + this->first_input_offset_;
this->tls_offset_ = this->address() - tls_base;
}
+// In a few cases we need to sort the input sections attached to an
+// output section. This is used to implement the type of constructor
+// priority ordering implemented by the GNU linker, in which the
+// priority becomes part of the section name and the sections are
+// sorted by name. We only do this for an output section if we see an
+// attached input section matching ".ctor.*", ".dtor.*",
+// ".init_array.*" or ".fini_array.*".
+
+class Output_section::Input_section_sort_entry
+{
+ public:
+ Input_section_sort_entry()
+ : input_section_(), index_(-1U), section_has_name_(false),
+ section_name_()
+ { }
+
+ Input_section_sort_entry(const Input_section& input_section,
+ unsigned int index)
+ : input_section_(input_section), index_(index),
+ section_has_name_(input_section.is_input_section())
+ {
+ if (this->section_has_name_)
+ {
+ // This is only called single-threaded from Layout::finalize,
+ // so it is OK to lock. Unfortunately we have no way to pass
+ // in a Task token.
+ const Task* dummy_task = reinterpret_cast<const Task*>(-1);
+ Object* obj = input_section.relobj();
+ Task_lock_obj<Object> tl(dummy_task, obj);
+
+ // This is a slow operation, which should be cached in
+ // Layout::layout if this becomes a speed problem.
+ this->section_name_ = obj->section_name(input_section.shndx());
+ }
+ }
+
+ // Return the Input_section.
+ const Input_section&
+ input_section() const
+ {
+ gold_assert(this->index_ != -1U);
+ return this->input_section_;
+ }
+
+ // The index of this entry in the original list. This is used to
+ // make the sort stable.
+ unsigned int
+ index() const
+ {
+ gold_assert(this->index_ != -1U);
+ return this->index_;
+ }
+
+ // Whether there is a section name.
+ bool
+ section_has_name() const
+ { return this->section_has_name_; }
+
+ // The section name.
+ const std::string&
+ section_name() const
+ {
+ gold_assert(this->section_has_name_);
+ return this->section_name_;
+ }
+
+ // Return true if the section name has a priority. This is assumed
+ // to be true if it has a dot after the initial dot.
+ bool
+ has_priority() const
+ {
+ gold_assert(this->section_has_name_);
+ return this->section_name_.find('.', 1);
+ }
+
+ // Return true if this an input file whose base name matches
+ // FILE_NAME. The base name must have an extension of ".o", and
+ // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
+ // This is to match crtbegin.o as well as crtbeginS.o without
+ // getting confused by other possibilities. Overall matching the
+ // file name this way is a dreadful hack, but the GNU linker does it
+ // in order to better support gcc, and we need to be compatible.
+ bool
+ match_file_name(const char* match_file_name) const
+ {
+ const std::string& file_name(this->input_section_.relobj()->name());
+ const char* base_name = lbasename(file_name.c_str());
+ size_t match_len = strlen(match_file_name);
+ if (strncmp(base_name, match_file_name, match_len) != 0)
+ return false;
+ size_t base_len = strlen(base_name);
+ if (base_len != match_len + 2 && base_len != match_len + 3)
+ return false;
+ return memcmp(base_name + base_len - 2, ".o", 2) == 0;
+ }
+
+ private:
+ // The Input_section we are sorting.
+ Input_section input_section_;
+ // The index of this Input_section in the original list.
+ unsigned int index_;
+ // Whether this Input_section has a section name--it won't if this
+ // is some random Output_section_data.
+ bool section_has_name_;
+ // The section name if there is one.
+ std::string section_name_;
+};
+
+// Return true if S1 should come before S2 in the output section.
+
+bool
+Output_section::Input_section_sort_compare::operator()(
+ const Output_section::Input_section_sort_entry& s1,
+ const Output_section::Input_section_sort_entry& s2) const
+{
+ // crtbegin.o must come first.
+ bool s1_begin = s1.match_file_name("crtbegin");
+ bool s2_begin = s2.match_file_name("crtbegin");
+ if (s1_begin || s2_begin)
+ {
+ if (!s1_begin)
+ return false;
+ if (!s2_begin)
+ return true;
+ return s1.index() < s2.index();
+ }
+
+ // crtend.o must come last.
+ bool s1_end = s1.match_file_name("crtend");
+ bool s2_end = s2.match_file_name("crtend");
+ if (s1_end || s2_end)
+ {
+ if (!s1_end)
+ return true;
+ if (!s2_end)
+ return false;
+ return s1.index() < s2.index();
+ }
+
+ // We sort all the sections with no names to the end.
+ if (!s1.section_has_name() || !s2.section_has_name())
+ {
+ if (s1.section_has_name())
+ return true;
+ if (s2.section_has_name())
+ return false;
+ return s1.index() < s2.index();
+ }
+
+ // A section with a priority follows a section without a priority.
+ // The GNU linker does this for all but .init_array sections; until
+ // further notice we'll assume that that is an mistake.
+ bool s1_has_priority = s1.has_priority();
+ bool s2_has_priority = s2.has_priority();
+ if (s1_has_priority && !s2_has_priority)
+ return false;
+ if (!s1_has_priority && s2_has_priority)
+ return true;
+
+ // Otherwise we sort by name.
+ int compare = s1.section_name().compare(s2.section_name());
+ if (compare != 0)
+ return compare < 0;
+
+ // Otherwise we keep the input order.
+ return s1.index() < s2.index();
+}
+
+// Sort the input sections attached to an output section.
+
+void
+Output_section::sort_attached_input_sections()
+{
+ if (this->attached_input_sections_are_sorted_)
+ return;
+
+ // The only thing we know about an input section is the object and
+ // the section index. We need the section name. Recomputing this
+ // is slow but this is an unusual case. If this becomes a speed
+ // problem we can cache the names as required in Layout::layout.
+
+ // We start by building a larger vector holding a copy of each
+ // Input_section, plus its current index in the list and its name.
+ std::vector<Input_section_sort_entry> sort_list;
+
+ unsigned int i = 0;
+ for (Input_section_list::iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p, ++i)
+ sort_list.push_back(Input_section_sort_entry(*p, i));
+
+ // Sort the input sections.
+ std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
+
+ // Copy the sorted input sections back to our list.
+ this->input_sections_.clear();
+ for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
+ p != sort_list.end();
+ ++p)
+ this->input_sections_.push_back(p->input_section());
+
+ // Remember that we sorted the input sections, since we might get
+ // called again.
+ this->attached_input_sections_are_sorted_ = true;
+}
+
// Write the section header to *OSHDR.
template<int size, bool big_endian>
p != this->fills_.end();
++p)
{
- std::string fill_data(parameters->target()->code_fill(p->length()));
+ std::string fill_data(parameters->target().code_fill(p->length()));
of->write(output_section_file_offset + p->section_offset(),
fill_data.data(), fill_data.size());
}
{
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()));
+ std::string fill_data(parameters->target().code_fill(p->length()));
memcpy(buffer + p->section_offset(), fill_data.data(),
fill_data.size());
}
pdl->push_back(os);
}
+// Remove an Output_section from this segment. It is an error if it
+// is not present.
+
+void
+Output_segment::remove_output_section(Output_section* os)
+{
+ // We only need this for SHT_PROGBITS.
+ gold_assert(os->type() == elfcpp::SHT_PROGBITS);
+ for (Output_data_list::iterator p = this->output_data_.begin();
+ p != this->output_data_.end();
+ ++p)
+ {
+ if (*p == os)
+ {
+ this->output_data_.erase(p);
+ return;
+ }
+ }
+ gold_unreachable();
+}
+
// Add an Output_data (which is not an Output_section) to the start of
// a segment.
// *POFF and *PSHNDX.
uint64_t
-Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
+Output_segment::set_section_addresses(const Layout* layout, bool reset,
+ uint64_t addr, off_t* poff,
unsigned int* pshndx)
{
gold_assert(this->type_ == elfcpp::PT_LOAD);
this->are_addresses_set_ = true;
}
+ bool in_tls = false;
+
off_t orig_off = *poff;
this->offset_ = orig_off;
- addr = this->set_section_list_addresses(reset, &this->output_data_,
- addr, poff, pshndx);
+ addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
+ addr, poff, pshndx, &in_tls);
this->filesz_ = *poff - orig_off;
off_t off = *poff;
- uint64_t ret = this->set_section_list_addresses(reset, &this->output_bss_,
- addr, poff, pshndx);
+ uint64_t ret = this->set_section_list_addresses(layout, reset,
+ &this->output_bss_,
+ addr, poff, pshndx,
+ &in_tls);
+
+ // If the last section was a TLS section, align upward to the
+ // alignment of the TLS segment, so that the overall size of the TLS
+ // segment is aligned.
+ if (in_tls)
+ {
+ uint64_t segment_align = layout->tls_segment()->maximum_alignment();
+ *poff = align_address(*poff, segment_align);
+ }
+
this->memsz_ = *poff - orig_off;
// Ignore the file offset adjustments made by the BSS Output_data
// structures.
uint64_t
-Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
+Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
+ Output_data_list* pdl,
uint64_t addr, off_t* poff,
- unsigned int* pshndx)
+ unsigned int* pshndx,
+ bool* in_tls)
{
off_t startoff = *poff;
// already have an address.
if (!(*p)->is_address_valid())
{
- off = align_address(off, (*p)->addralign());
+ uint64_t align = (*p)->addralign();
+
+ if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
+ {
+ // Give the first TLS section the alignment of the
+ // entire TLS segment. Otherwise the TLS segment as a
+ // whole may be misaligned.
+ if (!*in_tls)
+ {
+ Output_segment* tls_segment = layout->tls_segment();
+ gold_assert(tls_segment != NULL);
+ uint64_t segment_align = tls_segment->maximum_alignment();
+ gold_assert(segment_align >= align);
+ align = segment_align;
+
+ *in_tls = true;
+ }
+ }
+ else
+ {
+ // If this is the first section after the TLS segment,
+ // align it to at least the alignment of the TLS
+ // segment, so that the size of the overall TLS segment
+ // is aligned.
+ if (*in_tls)
+ {
+ uint64_t segment_align =
+ layout->tls_segment()->maximum_alignment();
+ if (segment_align > align)
+ align = segment_align;
+
+ *in_tls = false;
+ }
+ }
+
+ off = align_address(off, align);
(*p)->set_address_and_file_offset(addr + (off - startoff), off);
}
else
(*p)->finalize_data_size();
}
- // 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
- // affect the size of a PT_LOAD segment.
- if (this->type_ == elfcpp::PT_TLS
- || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
+ // We want to ignore the size of a SHF_TLS or SHT_NOBITS
+ // section. Such a section does not affect the size of a
+ // PT_LOAD segment.
+ if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
|| !(*p)->is_section_type(elfcpp::SHT_NOBITS))
off += (*p)->data_size();
this->memsz_ = (last->address()
+ last->data_size()
- this->vaddr_);
+
+ // If this is a TLS segment, align the memory size. The code in
+ // set_section_list ensures that the section after the TLS segment
+ // is aligned to give us room.
+ if (this->type_ == elfcpp::PT_TLS)
+ {
+ uint64_t segment_align = this->maximum_alignment();
+ gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
+ this->memsz_ = align_address(this->memsz_, segment_align);
+ }
}
// Set the TLS offsets of the sections in the PT_TLS segment.
Output_segment::write_section_headers(const Layout* layout,
const Stringpool* secnamepool,
unsigned char* v,
- unsigned int *pshndx
- ACCEPT_SIZE_ENDIAN) const
+ unsigned int *pshndx) const
{
// Every section that is attached to a segment must be attached to a
// PT_LOAD segment, so we only write out section headers for PT_LOAD
if (this->type_ != elfcpp::PT_LOAD)
return v;
- v = this->write_section_headers_list
- SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
- layout, secnamepool, &this->output_data_, v, pshndx
- SELECT_SIZE_ENDIAN(size, big_endian));
- v = this->write_section_headers_list
- SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
- layout, secnamepool, &this->output_bss_, v, pshndx
- SELECT_SIZE_ENDIAN(size, big_endian));
+ v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
+ &this->output_data_,
+ v, pshndx);
+ v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
+ &this->output_bss_,
+ v, pshndx);
return v;
}
const Stringpool* secnamepool,
const Output_data_list* pdl,
unsigned char* v,
- unsigned int* pshndx
- ACCEPT_SIZE_ENDIAN) const
+ unsigned int* pshndx) const
{
const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
for (Output_data_list::const_iterator p = pdl->begin();
o_(-1),
file_size_(0),
base_(NULL),
- map_is_anonymous_(false)
+ map_is_anonymous_(false),
+ is_temporary_(false)
{
}
// to improve the odds for open().
// We let the name "-" mean "stdout"
- if (strcmp(this->name_, "-") == 0)
- this->o_ = STDOUT_FILENO;
- else
+ if (!this->is_temporary_)
{
- struct stat s;
- if (::stat(this->name_, &s) == 0 && s.st_size != 0)
- unlink_if_ordinary(this->name_);
-
- int mode = parameters->output_is_object() ? 0666 : 0777;
- int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
- if (o < 0)
- gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
- this->o_ = o;
+ if (strcmp(this->name_, "-") == 0)
+ this->o_ = STDOUT_FILENO;
+ else
+ {
+ struct stat s;
+ if (::stat(this->name_, &s) == 0 && s.st_size != 0)
+ unlink_if_ordinary(this->name_);
+
+ int mode = parameters->options().relocatable() ? 0666 : 0777;
+ int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
+ if (o < 0)
+ gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
+ this->o_ = o;
+ }
}
this->map();
struct stat statbuf;
if (o == STDOUT_FILENO || o == STDERR_FILENO
|| ::fstat(o, &statbuf) != 0
- || !S_ISREG(statbuf.st_mode))
+ || !S_ISREG(statbuf.st_mode)
+ || this->is_temporary_)
{
this->map_is_anonymous_ = true;
base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
Output_file::close()
{
// If the map isn't file-backed, we need to write it now.
- if (this->map_is_anonymous_)
+ if (this->map_is_anonymous_ && !this->is_temporary_)
{
size_t bytes_to_write = this->file_size_;
while (bytes_to_write > 0)
this->unmap();
// We don't close stdout or stderr
- if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
+ if (this->o_ != STDOUT_FILENO
+ && this->o_ != STDERR_FILENO
+ && !this->is_temporary_)
if (::close(this->o_) < 0)
gold_error(_("%s: close: %s"), this->name_, strerror(errno));
this->o_ = -1;