#include <utility>
#include "output.h"
+#include "symtab.h"
#include "layout.h"
namespace gold
{
-// Layout_task methods.
-
-Layout_task::~Layout_task()
-{
-}
-
-// This task can be run when it is unblocked.
-
-Task::Is_runnable_type
-Layout_task::is_runnable(Workqueue*)
-{
- if (this->this_blocker_->is_blocked())
- return IS_BLOCKED;
- return IS_RUNNABLE;
-}
-
-// We don't need to hold any locks for the duration of this task. In
-// fact this task will be the only one running.
-
-Task_locker*
-Layout_task::locks(Workqueue*)
-{
- return NULL;
-}
+// Layout_task_runner methods.
// Lay out the sections. This is called after all the input objects
// have been read.
void
-Layout_task::run(Workqueue* workqueue)
+Layout_task_runner::run(Workqueue* workqueue)
{
off_t file_size = this->layout_->finalize(this->input_objects_,
this->symtab_);
// Layout methods.
Layout::Layout(const General_options& options)
- : options_(options), last_shndx_(0), namepool_(), sympool_(), signatures_(),
+ : options_(options), namepool_(), sympool_(), signatures_(),
section_name_map_(), segment_list_(), section_list_(),
- special_output_list_()
+ special_output_list_(), tls_segment_(NULL)
{
// Make space for more than enough segments for a typical file.
// This is just for efficiency--it's OK if we wind up needing more.
size_t
Layout::Hash_key::operator()(const Layout::Key& k) const
{
- return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
+ return k.first + k.second.first + k.second.second;
}
// Whether to include this section in the link.
}
}
-// Return the output section to use for input section NAME, with
-// header HEADER, from object OBJECT. Set *OFF to the offset of this
-// input section without the output section.
+// Return an output section named NAME, or NULL if there is none.
-template<int size, bool big_endian>
Output_section*
-Layout::layout(Object* object, const char* name,
- const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
+Layout::find_output_section(const char* name) const
{
- // We discard empty input sections.
- if (shdr.get_sh_size() == 0)
- return NULL;
-
- if (!this->include_section(object, name, shdr))
- return NULL;
-
- // Unless we are doing a relocateable link, .gnu.linkonce sections
- // are laid out as though they were named for the sections are
- // placed into.
- if (!this->options_.is_relocatable() && Layout::is_linkonce(name))
- name = Layout::linkonce_output_name(name);
+ for (Section_name_map::const_iterator p = this->section_name_map_.begin();
+ p != this->section_name_map_.end();
+ ++p)
+ if (strcmp(p->second->name(), name) == 0)
+ return p->second;
+ return NULL;
+}
- // FIXME: Handle SHF_OS_NONCONFORMING here.
+// Return an output segment of type TYPE, with segment flags SET set
+// and segment flags CLEAR clear. Return NULL if there is none.
- // Canonicalize the section name.
- name = this->namepool_.add(name);
+Output_segment*
+Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
+ elfcpp::Elf_Word clear) const
+{
+ for (Segment_list::const_iterator p = this->segment_list_.begin();
+ p != this->segment_list_.end();
+ ++p)
+ if (static_cast<elfcpp::PT>((*p)->type()) == type
+ && ((*p)->flags() & set) == set
+ && ((*p)->flags() & clear) == 0)
+ return *p;
+ return NULL;
+}
- // Find the output section. The output section is selected based on
- // the section name, type, and flags.
+// Return the output section to use for section NAME with type TYPE
+// and section flags FLAGS.
- // FIXME: If we want to do relaxation, we need to modify this
- // algorithm. We also build a list of input sections for each
- // output section. Then we relax all the input sections. Then we
- // walk down the list and adjust all the offsets.
+Output_section*
+Layout::get_output_section(const char* name, Stringpool::Key name_key,
+ elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
+{
+ // We should ignore some flags.
+ flags &= ~ (elfcpp::SHF_INFO_LINK
+ | elfcpp::SHF_LINK_ORDER
+ | elfcpp::SHF_GROUP);
- elfcpp::Elf_Word type = shdr.get_sh_type();
- elfcpp::Elf_Xword flags = shdr.get_sh_flags();
- const Key key(name, std::make_pair(type, flags));
+ const Key key(name_key, std::make_pair(type, flags));
const std::pair<Key, Output_section*> v(key, NULL);
std::pair<Section_name_map::iterator, bool> ins(
this->section_name_map_.insert(v));
- Output_section* os;
if (!ins.second)
- os = ins.first->second;
+ return ins.first->second;
else
{
// This is the first time we've seen this name/type/flags
// combination.
- os = this->make_output_section(name, type, flags);
+ Output_section* os = this->make_output_section(name, type, flags);
ins.first->second = os;
+ return os;
}
+}
+
+// Return the output section to use for input section SHNDX, with name
+// NAME, with header HEADER, from object OBJECT. Set *OFF to the
+// offset of this input section without the output section.
+
+template<int size, bool big_endian>
+Output_section*
+Layout::layout(Relobj* object, unsigned int shndx, const char* name,
+ const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
+{
+ if (!this->include_section(object, name, shdr))
+ return NULL;
+
+ // If we are not doing a relocateable link, choose the name to use
+ // for the output section.
+ size_t len = strlen(name);
+ if (!this->options_.is_relocatable())
+ name = Layout::output_section_name(name, &len);
+
+ // FIXME: Handle SHF_OS_NONCONFORMING here.
+
+ // Canonicalize the section name.
+ Stringpool::Key name_key;
+ name = this->namepool_.add(name, len, &name_key);
+
+ // Find the output section. The output section is selected based on
+ // the section name, type, and flags.
+ Output_section* os = this->get_output_section(name, name_key,
+ shdr.get_sh_type(),
+ shdr.get_sh_flags());
// FIXME: Handle SHF_LINK_ORDER somewhere.
- *off = os->add_input_section(object, name, shdr);
+ *off = os->add_input_section(object, shndx, name, shdr);
return os;
}
+// Add POSD to an output section using NAME, TYPE, and FLAGS.
+
+void
+Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
+ elfcpp::Elf_Xword flags,
+ Output_section_data* posd)
+{
+ // Canonicalize the name.
+ Stringpool::Key name_key;
+ name = this->namepool_.add(name, &name_key);
+
+ Output_section* os = this->get_output_section(name, name_key, type, flags);
+ os->add_output_section_data(posd);
+}
+
// Map section flags to segment flags.
elfcpp::Elf_Word
Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
elfcpp::Elf_Xword flags)
{
- ++this->last_shndx_;
- Output_section* os = new Output_section(name, type, flags,
- this->last_shndx_);
+ Output_section* os = new Output_section(name, type, flags, true);
if ((flags & elfcpp::SHF_ALLOC) == 0)
this->section_list_.push_back(os);
}
// If we see a loadable SHF_TLS section, we create a PT_TLS
- // segment.
+ // segment. There can only be one such segment.
if ((flags & elfcpp::SHF_TLS) != 0)
{
- // See if we already have an equivalent PT_TLS segment.
- for (p = this->segment_list_.begin();
- p != segment_list_.end();
- ++p)
- {
- if ((*p)->type() == elfcpp::PT_TLS
- && (((*p)->flags() & elfcpp::PF_W)
- == (seg_flags & elfcpp::PF_W)))
- {
- (*p)->add_output_section(os, seg_flags);
- break;
- }
- }
-
- if (p == this->segment_list_.end())
+ if (this->tls_segment_ == NULL)
{
- Output_segment* oseg = new Output_segment(elfcpp::PT_TLS,
- seg_flags);
- this->segment_list_.push_back(oseg);
- oseg->add_output_section(os, seg_flags);
+ this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
+ seg_flags);
+ this->segment_list_.push_back(this->tls_segment_);
}
+ this->tls_segment_->add_output_section(os, seg_flags);
}
}
off_t
Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
{
+ const int size = input_objects->target()->get_size();
+
+ Output_segment* phdr_seg = NULL;
if (input_objects->any_dynamic())
{
- // If there are any dynamic objects in the link, then we need
- // some additional segments: PT_PHDRS, PT_INTERP, and
- // PT_DYNAMIC. We also need to finalize the dynamic symbol
- // table and create the dynamic hash table.
- abort();
+ // There was a dynamic object in the link. We need to create
+ // some information for the dynamic linker.
+
+ // Create the PT_PHDR segment which will hold the program
+ // headers.
+ phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
+ this->segment_list_.push_back(phdr_seg);
+
+ // Create the dynamic symbol table, including the hash table,
+ // the dynamic relocations, and the version sections.
+ this->create_dynamic_symtab(size, symtab);
+
+ // Create the .dynamic section to hold the dynamic data, and put
+ // it in a PT_DYNAMIC segment.
+ this->create_dynamic_section();
+
+ // Create the .interp section to hold the name of the
+ // interpreter, and put it in a PT_INTERP segment.
+ this->create_interp(input_objects->target());
}
// FIXME: Handle PT_GNU_STACK.
Output_segment* load_seg = this->find_first_load_seg();
// Lay out the segment headers.
- int size = input_objects->target()->get_size();
bool big_endian = input_objects->target()->is_big_endian();
Output_segment_headers* segment_headers;
segment_headers = new Output_segment_headers(size, big_endian,
this->segment_list_);
load_seg->add_initial_output_data(segment_headers);
this->special_output_list_.push_back(segment_headers);
- // FIXME: Attach them to PT_PHDRS if necessary.
+ if (phdr_seg != NULL)
+ phdr_seg->add_initial_output_data(segment_headers);
// Lay out the file header.
Output_file_header* file_header;
load_seg->add_initial_output_data(file_header);
this->special_output_list_.push_back(file_header);
- // Set the file offsets of all the segments.
- off_t off = this->set_segment_offsets(input_objects->target(), load_seg);
+ // We set the output section indexes in set_segment_offsets and
+ // set_section_offsets.
+ unsigned int shndx = 1;
+
+ // Set the file offsets of all the segments, and all the sections
+ // they contain.
+ off_t off = this->set_segment_offsets(input_objects->target(), load_seg,
+ &shndx);
// Create the symbol table sections.
// FIXME: We don't need to do this if we are stripping symbols.
// Set the file offsets of all the sections not associated with
// segments.
- off = this->set_section_offsets(off);
+ off = this->set_section_offsets(off, &shndx);
+
+ // Now the section index of OSTRTAB is set.
+ osymtab->set_link(ostrtab->out_shndx());
// Create the section table header.
Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
return false;
+ // We put the PT_TLS segment last, because that is where the dynamic
+ // linker expects to find it (this is just for efficiency; other
+ // positions would also work correctly).
+ if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
+ return false;
+ if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
+ return true;
+
const elfcpp::Elf_Word flags1 = seg1->flags();
const elfcpp::Elf_Word flags2 = seg2->flags();
return paddr1 < paddr2;
}
-// Set the file offsets of all the segments. They have all been
-// created. LOAD_SEG must be be laid out first. Return the offset of
-// the data to follow.
+// Set the file offsets of all the segments, and all the sections they
+// contain. They have all been created. LOAD_SEG must be be laid out
+// first. Return the offset of the data to follow.
off_t
-Layout::set_segment_offsets(const Target* target, Output_segment* load_seg)
+Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
+ unsigned int *pshndx)
{
// Sort them into the final order.
std::sort(this->segment_list_.begin(), this->segment_list_.end(),
uint64_t abi_pagesize = target->abi_pagesize();
if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
{
- uint64_t align = (*p)->max_data_align();
+ uint64_t align = (*p)->addralign();
- addr = (addr + align - 1) & ~ (align - 1);
+ addr = align_address(addr, align);
aligned_addr = addr;
if ((addr & (abi_pagesize - 1)) != 0)
addr = addr + abi_pagesize;
}
+ unsigned int shndx_hold = *pshndx;
off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
- uint64_t new_addr = (*p)->set_section_addresses(addr, &off);
+ uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
// Now that we know the size of this segment, we may be able
// to save a page in memory, at the cost of wasting some
!= (new_addr & ~ (common_pagesize - 1)))
&& first_off + last_off <= common_pagesize)
{
- addr = ((aligned_addr + common_pagesize - 1)
- & ~ (common_pagesize - 1));
+ *pshndx = shndx_hold;
+ addr = align_address(aligned_addr, common_pagesize);
off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
- new_addr = (*p)->set_section_addresses(addr, &off);
+ new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
}
}
// segment.
off_t
-Layout::set_section_offsets(off_t off)
+Layout::set_section_offsets(off_t off, unsigned int* pshndx)
{
for (Layout::Section_list::iterator p = this->section_list_.begin();
p != this->section_list_.end();
++p)
{
+ (*p)->set_out_shndx(*pshndx);
+ ++*pshndx;
if ((*p)->offset() != -1)
continue;
- uint64_t addralign = (*p)->addralign();
- if (addralign != 0)
- off = (off + addralign - 1) & ~ (addralign - 1);
+ off = align_address(off, (*p)->addralign());
(*p)->set_address(0, off);
off += (*p)->data_size();
}
abort();
off_t off = *poff;
- off = (off + align - 1) & ~ (align - 1);
+ off = align_address(off, align);
off_t startoff = off;
// Save space for the dummy symbol at the start of the section. We
// never bother to write this out--it will just be left as zero.
off += symsize;
+ unsigned int local_symbol_index = 1;
- for (Input_objects::Object_list::const_iterator p = input_objects->begin();
- p != input_objects->end();
+ for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+ p != input_objects->relobj_end();
++p)
{
Task_lock_obj<Object> tlo(**p);
- off = (*p)->finalize_local_symbols(off, &this->sympool_);
+ unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
+ off,
+ &this->sympool_);
+ off += (index - local_symbol_index) * symsize;
+ local_symbol_index = index;
}
- unsigned int local_symcount = (off - startoff) / symsize;
+ unsigned int local_symcount = local_symbol_index;
assert(local_symcount * symsize == off - startoff);
- off = symtab->finalize(off, &this->sympool_);
+ off = symtab->finalize(local_symcount, off, &this->sympool_);
this->sympool_.set_string_offsets();
- ++this->last_shndx_;
- const char* symtab_name = this->namepool_.add(".symtab");
+ const char* symtab_name = this->namepool_.add(".symtab", NULL);
Output_section* osymtab = new Output_section_symtab(symtab_name,
- off - startoff,
- this->last_shndx_);
+ off - startoff);
this->section_list_.push_back(osymtab);
- ++this->last_shndx_;
- const char* strtab_name = this->namepool_.add(".strtab");
+ const char* strtab_name = this->namepool_.add(".strtab", NULL);
Output_section *ostrtab = new Output_section_strtab(strtab_name,
- &this->sympool_,
- this->last_shndx_);
+ &this->sympool_);
this->section_list_.push_back(ostrtab);
this->special_output_list_.push_back(ostrtab);
osymtab->set_address(0, startoff);
- osymtab->set_link(ostrtab->shndx());
osymtab->set_info(local_symcount);
osymtab->set_entsize(symsize);
osymtab->set_addralign(align);
// FIXME: We don't need to create a .shstrtab section if we are
// stripping everything.
- const char* name = this->namepool_.add(".shstrtab");
+ const char* name = this->namepool_.add(".shstrtab", NULL);
this->namepool_.set_string_offsets();
- ++this->last_shndx_;
- Output_section* os = new Output_section_strtab(name,
- &this->namepool_,
- this->last_shndx_);
+ Output_section* os = new Output_section_strtab(name, &this->namepool_);
this->section_list_.push_back(os);
this->special_output_list_.push_back(os);
oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
this->section_list_,
&this->namepool_);
- uint64_t addralign = oshdrs->addralign();
- off_t off = (*poff + addralign - 1) & ~ (addralign - 1);
+ off_t off = align_address(*poff, oshdrs->addralign());
oshdrs->set_address(0, off);
off += oshdrs->data_size();
*poff = off;
return oshdrs;
}
+// Create the dynamic symbol table.
+
+void
+Layout::create_dynamic_symtab(int, Symbol_table*)
+{
+ abort();
+}
+
+// Create the .dynamic section and PT_DYNAMIC segment.
+
+void
+Layout::create_dynamic_section()
+{
+ abort();
+}
+
+// Create the .interp section and PT_INTERP segment.
+
+void
+Layout::create_interp(const Target* target)
+{
+ const char* interp = this->options_.dynamic_linker();
+ if (interp == NULL)
+ {
+ interp = target->dynamic_linker();
+ assert(interp != NULL);
+ }
+
+ size_t len = strlen(interp) + 1;
+
+ Output_section_data* odata = new Output_data_const(interp, len, 1);
+
+ const char* interp_name = this->namepool_.add(".interp", NULL);
+ Output_section* osec = this->make_output_section(interp_name,
+ elfcpp::SHT_PROGBITS,
+ elfcpp::SHF_ALLOC);
+ osec->add_output_section_data(odata);
+
+ Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
+ this->segment_list_.push_back(oseg);
+ oseg->add_initial_output_section(osec, elfcpp::PF_R);
+}
+
// The mapping of .gnu.linkonce section names to real section names.
-#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
+#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
{
MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
// Return the name of the output section to use for a .gnu.linkonce
// section. This is based on the default ELF linker script of the old
// GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
-// to ".text".
+// to ".text". Set *PLEN to the length of the name. *PLEN is
+// initialized to the length of NAME.
const char*
-Layout::linkonce_output_name(const char* name)
+Layout::linkonce_output_name(const char* name, size_t *plen)
{
const char* s = name + sizeof(".gnu.linkonce") - 1;
if (*s != '.')
for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
{
if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
- return plm->to;
+ {
+ *plen = plm->tolen;
+ return plm->to;
+ }
+ }
+ return name;
+}
+
+// Choose the output section name to use given an input section name.
+// Set *PLEN to the length of the name. *PLEN is initialized to the
+// length of NAME.
+
+const char*
+Layout::output_section_name(const char* name, size_t* plen)
+{
+ if (Layout::is_linkonce(name))
+ {
+ // .gnu.linkonce sections are laid out as though they were named
+ // for the sections are placed into.
+ return Layout::linkonce_output_name(name, plen);
+ }
+
+ // If the section name has no '.', or only an initial '.', we use
+ // the name unchanged (i.e., ".text" is unchanged).
+
+ // Otherwise, if the section name does not include ".rel", we drop
+ // the last '.' and everything that follows (i.e., ".text.XXX"
+ // becomes ".text").
+
+ // Otherwise, if the section name has zero or one '.' after the
+ // ".rel", we use the name unchanged (i.e., ".rel.text" is
+ // unchanged).
+
+ // Otherwise, we drop the last '.' and everything that follows
+ // (i.e., ".rel.text.XXX" becomes ".rel.text").
+
+ const char* s = name;
+ if (*s == '.')
+ ++s;
+ const char* sdot = strchr(s, '.');
+ if (sdot == NULL)
+ return name;
+
+ const char* srel = strstr(s, ".rel");
+ if (srel == NULL)
+ {
+ *plen = sdot - name;
+ return name;
}
+
+ sdot = strchr(srel + 1, '.');
+ if (sdot == NULL)
+ return name;
+ sdot = strchr(sdot + 1, '.');
+ if (sdot == NULL)
+ return name;
+
+ *plen = sdot - name;
return name;
}
{
std::string sig(signature);
std::pair<Signatures::iterator, bool> ins(
- this->signatures_.insert(std::make_pair(signature, group)));
+ this->signatures_.insert(std::make_pair(sig, group)));
if (ins.second)
{
this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
}
-// Close_task methods.
-
-// We can't run until FINAL_BLOCKER is unblocked.
-
-Task::Is_runnable_type
-Close_task::is_runnable(Workqueue*)
-{
- if (this->final_blocker_->is_blocked())
- return IS_BLOCKED;
- return IS_RUNNABLE;
-}
-
-// We don't lock anything.
-
-Task_locker*
-Close_task::locks(Workqueue*)
-{
- return NULL;
-}
+// Close_task_runner methods.
// Run the task--close the file.
void
-Close_task::run(Workqueue*)
+Close_task_runner::run(Workqueue*)
{
this->of_->close();
}
template
Output_section*
-Layout::layout<32, false>(Object* object, const char* name,
+Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
const elfcpp::Shdr<32, false>& shdr, off_t*);
template
Output_section*
-Layout::layout<32, true>(Object* object, const char* name,
+Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
const elfcpp::Shdr<32, true>& shdr, off_t*);
template
Output_section*
-Layout::layout<64, false>(Object* object, const char* name,
+Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
const elfcpp::Shdr<64, false>& shdr, off_t*);
template
Output_section*
-Layout::layout<64, true>(Object* object, const char* name,
+Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
const elfcpp::Shdr<64, true>& shdr, off_t*);