Add support for -e and for ENTRY in linker scripts.
[deliverable/binutils-gdb.git] / gold / layout.cc
index 39008cd46fe2cfc42560fa31b8006dc522537eab..a717ed241e12fd228d2102040949f2cf12b5b68a 100644 (file)
@@ -44,10 +44,11 @@ namespace gold
 // have been read.
 
 void
-Layout_task_runner::run(Workqueue* workqueue)
+Layout_task_runner::run(Workqueue* workqueue, const Task* task)
 {
   off_t file_size = this->layout_->finalize(this->input_objects_,
-                                           this->symtab_);
+                                           this->symtab_,
+                                           task);
 
   // Now we know the final size of the output file and we know where
   // each piece of information goes.
@@ -63,7 +64,8 @@ Layout_task_runner::run(Workqueue* workqueue)
 // Layout methods.
 
 Layout::Layout(const General_options& options)
-  : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
+  : options_(options), entry_(options.entry()), namepool_(), sympool_(),
+    dynpool_(), signatures_(),
     section_name_map_(), segment_list_(), section_list_(),
     unattached_section_list_(), special_output_list_(),
     section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
@@ -71,7 +73,9 @@ Layout::Layout(const General_options& options)
     eh_frame_section_(NULL), output_file_size_(-1),
     input_requires_executable_stack_(false),
     input_with_gnu_stack_note_(false),
-    input_without_gnu_stack_note_(false)
+    input_without_gnu_stack_note_(false),
+    has_static_tls_(false),
+    any_postprocessing_sections_(false)
 {
   // 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.
@@ -266,7 +270,7 @@ Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
 
   // Canonicalize the section name.
   Stringpool::Key name_key;
-  name = this->namepool_.add_prefix(name, len, &name_key);
+  name = this->namepool_.add_with_length(name, len, true, &name_key);
 
   // Find the output section.  The output section is selected based on
   // the section name, type, and flags.
@@ -652,12 +656,15 @@ Layout::find_first_load_seg()
 // This function returns the size of the output file.
 
 off_t
-Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
+Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
+                const Task* task)
 {
   Target* const target = input_objects->target();
 
   target->finalize_sections(this);
 
+  this->count_local_symbols(task, input_objects);
+
   this->create_gold_note();
   this->create_executable_stack_info(target);
 
@@ -677,7 +684,7 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
       std::vector<Symbol*> dynamic_symbols;
       unsigned int local_dynamic_count;
       Versions versions;
-      this->create_dynamic_symtab(target, symtab, &dynstr,
+      this->create_dynamic_symtab(input_objects, target, symtab, &dynstr,
                                  &local_dynamic_count, &dynamic_symbols,
                                  &versions);
 
@@ -714,7 +721,8 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
 
   // Lay out the file header.
   Output_file_header* file_header;
-  file_header = new Output_file_header(target, symtab, segment_headers);
+  file_header = new Output_file_header(target, symtab, segment_headers,
+                                      this->entry_);
   load_seg->add_initial_output_data(file_header);
   this->special_output_list_.push_back(file_header);
 
@@ -726,14 +734,22 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
   // they contain.
   off_t off = this->set_segment_offsets(target, load_seg, &shndx);
 
+  // Set the file offsets of all the non-data sections we've seen so
+  // far which don't have to wait for the input sections.  We need
+  // this in order to finalize local symbols in non-allocated
+  // sections.
+  off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
+
   // Create the symbol table sections.
   this->create_symtab_sections(input_objects, symtab, &off);
+  if (!parameters->doing_static_link())
+    this->assign_local_dynsym_offsets(input_objects);
 
   // Create the .shstrtab section.
   Output_section* shstrtab_section = this->create_shstrtab();
 
-  // Set the file offsets of all the non-data sections which don't
-  // have to wait for the input sections.
+  // Set the file offsets of the rest of the non-data sections which
+  // don't have to wait for the input sections.
   off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
 
   // Now that all sections have been created, set the section indexes.
@@ -742,6 +758,12 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
   // Create the section table header.
   this->create_shdrs(&off);
 
+  // If there are no sections which require postprocessing, we can
+  // handle the section names now, and avoid a resize later.
+  if (!this->any_postprocessing_sections_)
+    off = this->set_section_offsets(off,
+                                   STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
+
   file_header->set_section_info(this->section_headers_, shstrtab_section);
 
   // Now we know exactly where everything goes in the output file
@@ -988,7 +1010,9 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
   // Find the PT_LOAD segments, and set their addresses and offsets
   // and their section's addresses and offsets.
   uint64_t addr;
-  if (options_.user_set_text_segment_address())
+  if (parameters->output_is_shared())
+    addr = 0;
+  else if (options_.user_set_text_segment_address())
     addr = options_.text_segment_address();
   else
     addr = target->default_text_segment_address();
@@ -1076,6 +1100,10 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
        (*p)->set_offset();
     }
 
+  // Set the TLS offsets for each section in the PT_TLS segment.
+  if (this->tls_segment_ != NULL)
+    this->tls_segment_->set_tls_offsets();
+
   return off;
 }
 
@@ -1093,18 +1121,25 @@ Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
       if (*p == this->symtab_section_)
        continue;
 
+      // If we've already set the data size, don't set it again.
+      if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
+       continue;
+
       if (pass == BEFORE_INPUT_SECTIONS_PASS
          && (*p)->requires_postprocessing())
-       (*p)->create_postprocessing_buffer();
+       {
+         (*p)->create_postprocessing_buffer();
+         this->any_postprocessing_sections_ = true;
+       }
 
       if (pass == BEFORE_INPUT_SECTIONS_PASS
           && (*p)->after_input_sections())
         continue;
-      else if (pass == AFTER_INPUT_SECTIONS_PASS
+      else if (pass == POSTPROCESSING_SECTIONS_PASS
                && (!(*p)->after_input_sections()
                    || (*p)->type() == elfcpp::SHT_STRTAB))
         continue;
-      else if (pass == STRTAB_AFTER_INPUT_SECTIONS_PASS
+      else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
                && (!(*p)->after_input_sections()
                    || (*p)->type() != elfcpp::SHT_STRTAB))
         continue;
@@ -1115,7 +1150,7 @@ Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
       off += (*p)->data_size();
 
       // At this point the name must be set.
-      if (pass != STRTAB_AFTER_INPUT_SECTIONS_PASS)
+      if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
        this->namepool_.add((*p)->name(), false, NULL);
     }
   return off;
@@ -1137,6 +1172,41 @@ Layout::set_section_indexes(unsigned int shndx)
   return shndx;
 }
 
+// Count the local symbols in the regular symbol table and the dynamic
+// symbol table, and build the respective string pools.
+
+void
+Layout::count_local_symbols(const Task* task,
+                           const Input_objects* input_objects)
+{
+  // First, figure out an upper bound on the number of symbols we'll
+  // be inserting into each pool.  This helps us create the pools with
+  // the right size, to avoid unnecessary hashtable resizing.
+  unsigned int symbol_count = 0;
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    symbol_count += (*p)->local_symbol_count();
+
+  // Go from "upper bound" to "estimate."  We overcount for two
+  // reasons: we double-count symbols that occur in more than one
+  // object file, and we count symbols that are dropped from the
+  // output.  Add it all together and assume we overcount by 100%.
+  symbol_count /= 2;
+
+  // We assume all symbols will go into both the sympool and dynpool.
+  this->sympool_.reserve(symbol_count);
+  this->dynpool_.reserve(symbol_count);
+
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      Task_lock_obj<Object> tlo(task, *p);
+      (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
+    }
+}
+
 // Create the symbol table sections.  Here we also set the final
 // values of the symbols.  At this point all the loadable sections are
 // fully laid out.
@@ -1189,10 +1259,8 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
        p != input_objects->relobj_end();
        ++p)
     {
-      Task_lock_obj<Object> tlo(**p);
       unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
-                                                       off,
-                                                       &this->sympool_);
+                                                        off);
       off += (index - local_symbol_index) * symsize;
       local_symbol_index = index;
     }
@@ -1300,7 +1368,8 @@ Layout::create_shdrs(off_t* poff)
 // Create the dynamic symbol table.
 
 void
-Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
+Layout::create_dynamic_symtab(const Input_objects* input_objects,
+                              const Target* target, Symbol_table* symtab,
                              Output_section **pdynstr,
                              unsigned int* plocal_dynamic_count,
                              std::vector<Symbol*>* pdynamic_symbols,
@@ -1326,10 +1395,15 @@ Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
        }
     }
 
-  // FIXME: Some targets apparently require local symbols in the
-  // dynamic symbol table.  Here is where we will have to count them,
-  // and set the dynamic symbol indexes, and add the names to
-  // this->dynpool_.
+  // Count the local symbols that need to go in the dynamic symbol table,
+  // and set the dynamic symbol indexes.
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
+      index = new_index;
+    }
 
   unsigned int local_symcount = index;
   *plocal_dynamic_count = local_symcount;
@@ -1419,6 +1493,28 @@ Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
   odyn->add_section_address(elfcpp::DT_HASH, hashsec);
 }
 
+// Assign offsets to each local portion of the dynamic symbol table.
+
+void
+Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
+{
+  Output_section* dynsym = this->dynsym_section_;
+  gold_assert(dynsym != NULL);
+
+  off_t off = dynsym->offset();
+
+  // Skip the dummy symbol at the start of the section.
+  off += dynsym->entsize();
+
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      unsigned int count = (*p)->set_local_dynsym_offset(off);
+      off += count * dynsym->entsize();
+    }
+}
+
 // Create the version sections.
 
 void
@@ -1619,6 +1715,13 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
       odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
     }
 
+  if (parameters->output_is_shared())
+    {
+      const char* soname = this->options_.soname();
+      if (soname != NULL)
+       odyn->add_string(elfcpp::DT_SONAME, soname);
+    }
+
   // FIXME: Support --init and --fini.
   Symbol* sym = symtab->lookup("_init");
   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
@@ -1677,7 +1780,13 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
   // post-link tools can easily modify these flags if desired.
   unsigned int flags = 0;
   if (have_textrel)
-    flags |= elfcpp::DF_TEXTREL;
+    {
+      // Add a DT_TEXTREL for compatibility with older loaders.
+      odyn->add_constant(elfcpp::DT_TEXTREL, 0);
+      flags |= elfcpp::DF_TEXTREL;
+    }
+  if (parameters->output_is_shared() && this->has_static_tls())
+    flags |= elfcpp::DF_STATIC_TLS;
   odyn->add_constant(elfcpp::DT_FLAGS, flags);
 }
 
@@ -1927,16 +2036,21 @@ Layout::write_sections_after_input_sections(Output_file* of)
   // file size.  Note we finalize the .shstrab last, to allow the
   // after_input_section sections to modify their section-names before
   // writing.
-  off_t off = this->output_file_size_;
-  off = this->set_section_offsets(off, AFTER_INPUT_SECTIONS_PASS);
-
-  // Now that we've finalized the names, we can finalize the shstrab.
-  off = this->set_section_offsets(off, STRTAB_AFTER_INPUT_SECTIONS_PASS);
-
-  if (off > this->output_file_size_)
+  if (this->any_postprocessing_sections_)
     {
-      of->resize(off);
-      this->output_file_size_ = off;
+      off_t off = this->output_file_size_;
+      off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
+      
+      // Now that we've finalized the names, we can finalize the shstrab.
+      off =
+       this->set_section_offsets(off,
+                                 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
+
+      if (off > this->output_file_size_)
+       {
+         of->resize(off);
+         this->output_file_size_ = off;
+       }
     }
 
   for (Section_list::const_iterator p = this->section_list_.begin();
@@ -1947,51 +2061,42 @@ Layout::write_sections_after_input_sections(Output_file* of)
        (*p)->write(of);
     }
 
-  for (Section_list::const_iterator p = this->unattached_section_list_.begin();
-       p != this->unattached_section_list_.end();
-       ++p)
-    {
-      if ((*p)->after_input_sections())
-       (*p)->write(of);
-    }
-
   this->section_headers_->write(of);
 }
 
+// Print statistical information to stderr.  This is used for --stats.
+
+void
+Layout::print_stats() const
+{
+  this->namepool_.print_stats("section name pool");
+  this->sympool_.print_stats("output symbol name pool");
+  this->dynpool_.print_stats("dynamic name pool");
+
+  for (Section_list::const_iterator p = this->section_list_.begin();
+       p != this->section_list_.end();
+       ++p)
+    (*p)->print_merge_stats();
+}
+
 // Write_sections_task methods.
 
 // We can always run this task.
 
-Task::Is_runnable_type
-Write_sections_task::is_runnable(Workqueue*)
+Task_token*
+Write_sections_task::is_runnable()
 {
-  return IS_RUNNABLE;
+  return NULL;
 }
 
 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
 // when finished.
 
-class Write_sections_task::Write_sections_locker : public Task_locker
-{
- public:
-  Write_sections_locker(Task_token& output_sections_blocker,
-                       Task_token& final_blocker,
-                       Workqueue* workqueue)
-    : output_sections_block_(output_sections_blocker, workqueue),
-      final_block_(final_blocker, workqueue)
-  { }
-
- private:
-  Task_block_token output_sections_block_;
-  Task_block_token final_block_;
-};
-
-Task_locker*
-Write_sections_task::locks(Workqueue* workqueue)
+void
+Write_sections_task::locks(Task_locker* tl)
 {
-  return new Write_sections_locker(*this->output_sections_blocker_,
-                                  *this->final_blocker_,
-                                  workqueue);
+  tl->add(this, this->output_sections_blocker_);
+  tl->add(this, this->final_blocker_);
 }
 
 // Run the task--write out the data.
@@ -2006,18 +2111,18 @@ Write_sections_task::run(Workqueue*)
 
 // We can always run this task.
 
-Task::Is_runnable_type
-Write_data_task::is_runnable(Workqueue*)
+Task_token*
+Write_data_task::is_runnable()
 {
-  return IS_RUNNABLE;
+  return NULL;
 }
 
 // We need to unlock FINAL_BLOCKER when finished.
 
-Task_locker*
-Write_data_task::locks(Workqueue* workqueue)
+void
+Write_data_task::locks(Task_locker* tl)
 {
-  return new Task_locker_block(*this->final_blocker_, workqueue);
+  tl->add(this, this->final_blocker_);
 }
 
 // Run the task--write out the data.
@@ -2032,18 +2137,18 @@ Write_data_task::run(Workqueue*)
 
 // We can always run this task.
 
-Task::Is_runnable_type
-Write_symbols_task::is_runnable(Workqueue*)
+Task_token*
+Write_symbols_task::is_runnable()
 {
-  return IS_RUNNABLE;
+  return NULL;
 }
 
 // We need to unlock FINAL_BLOCKER when finished.
 
-Task_locker*
-Write_symbols_task::locks(Workqueue* workqueue)
+void
+Write_symbols_task::locks(Task_locker* tl)
 {
-  return new Task_locker_block(*this->final_blocker_, workqueue);
+  tl->add(this, this->final_blocker_);
 }
 
 // Run the task--write out the symbols.
@@ -2059,20 +2164,20 @@ Write_symbols_task::run(Workqueue*)
 
 // We can only run this task after the input sections have completed.
 
-Task::Is_runnable_type
-Write_after_input_sections_task::is_runnable(Workqueue*)
+Task_token*
+Write_after_input_sections_task::is_runnable()
 {
   if (this->input_sections_blocker_->is_blocked())
-    return IS_BLOCKED;
-  return IS_RUNNABLE;
+    return this->input_sections_blocker_;
+  return NULL;
 }
 
 // We need to unlock FINAL_BLOCKER when finished.
 
-Task_locker*
-Write_after_input_sections_task::locks(Workqueue* workqueue)
+void
+Write_after_input_sections_task::locks(Task_locker* tl)
 {
-  return new Task_locker_block(*this->final_blocker_, workqueue);
+  tl->add(this, this->final_blocker_);
 }
 
 // Run the task.
@@ -2088,7 +2193,7 @@ Write_after_input_sections_task::run(Workqueue*)
 // Run the task--close the file.
 
 void
-Close_task_runner::run(Workqueue*)
+Close_task_runner::run(Workqueue*, const Task*)
 {
   this->of_->close();
 }
This page took 0.031491 seconds and 4 git commands to generate.