Stringpool stats. Also make Symbol_table support functions inline.
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
index 3bcf9a727afef6dfe0288178ba4fd9bb9ebefc0e..efded0ed17d5d153e29e5147e233295494758a74 100644 (file)
@@ -25,6 +25,7 @@
 #include "elfcpp_swap.h"
 #include "dwarf.h"
 #include "object.h"
+#include "parameters.h"
 #include "reloc.h"
 #include "dwarf_reader.h"
 
@@ -117,10 +118,9 @@ ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
 }
 
 template<int size, bool big_endian>
-Dwarf_line_info<size, big_endian>::Dwarf_line_info(
-  Sized_relobj<size, big_endian>* object)
-  : data_valid_(true), buffer_(NULL), symtab_buffer_(NULL),
-    directories_(1), files_(1)
+Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object)
+  : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
+    directories_(), files_(), current_header_index_(-1)
 {
   unsigned int debug_shndx;
   for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
@@ -133,12 +133,10 @@ Dwarf_line_info<size, big_endian>::Dwarf_line_info(
         break;
       }
   if (this->buffer_ == NULL)
-    {
-      this->data_valid_ = false;
-      return;
-    }
+    return;
 
   // Find the relocation section for ".debug_line".
+  // We expect these for relobjs (.o's) but not dynobjs (.so's).
   bool got_relocs = false;
   for (unsigned int reloc_shndx = 0;
        reloc_shndx < object->shnum();
@@ -154,31 +152,25 @@ Dwarf_line_info<size, big_endian>::Dwarf_line_info(
          break;
        }
     }
-  if (!got_relocs)
-    {
-      this->data_valid_ = false;
-      return;
-    }
 
   // Finally, we need the symtab section to interpret the relocs.
-  unsigned int symtab_shndx;
-  for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
-    if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
-      {
-        off_t symtab_size;
-        this->symtab_buffer_ = object->section_contents(
-            symtab_shndx, &symtab_size, false);
-        this->symtab_buffer_end_ = this->symtab_buffer_ + symtab_size;
-        break;
-      }
-  if (this->symtab_buffer_ == NULL)
+  if (got_relocs)
     {
-      this->data_valid_ = false;
-      return;
+      unsigned int symtab_shndx;
+      for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
+        if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
+          {
+            this->symtab_buffer_ = object->section_contents(
+                symtab_shndx, &this->symtab_buffer_size_, false);
+            break;
+          }
+      if (this->symtab_buffer_ == NULL)
+        return;
     }
 
   // Now that we have successfully read all the data, parse the debug
   // info.
+  this->data_valid_ = true;
   this->read_line_mappings();
 }
 
@@ -186,7 +178,7 @@ Dwarf_line_info<size, big_endian>::Dwarf_line_info(
 
 template<int size, bool big_endian>
 const unsigned char*
-Dwarf_line_info<size, big_endian>::read_header_prolog(
+Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
     const unsigned char* lineptr)
 {
   uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
@@ -247,19 +239,32 @@ Dwarf_line_info<size, big_endian>::read_header_prolog(
 
 template<int size, bool big_endian>
 const unsigned char*
-Dwarf_line_info<size, big_endian>::read_header_tables(
+Sized_dwarf_line_info<size, big_endian>::read_header_tables(
     const unsigned char* lineptr)
 {
+  ++this->current_header_index_;
+
+  // Create a new directories_ entry and a new files_ entry for our new
+  // header.  We initialize each with a single empty element, because
+  // dwarf indexes directory and filenames starting at 1.
+  gold_assert(static_cast<int>(this->directories_.size())
+             == this->current_header_index_);
+  gold_assert(static_cast<int>(this->files_.size())
+             == this->current_header_index_);
+  this->directories_.push_back(std::vector<std::string>(1));
+  this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
+
   // It is legal for the directory entry table to be empty.
   if (*lineptr)
     {
       int dirindex = 1;
       while (*lineptr)
         {
-          const unsigned char* dirname = lineptr;
-          gold_assert(dirindex == static_cast<int>(directories_.size()));
-          directories_.push_back(reinterpret_cast<const char*>(dirname));
-          lineptr += directories_.back().size() + 1;
+         const char* dirname = reinterpret_cast<const char*>(lineptr);
+          gold_assert(dirindex
+                     == static_cast<int>(this->directories_.back().size()));
+          this->directories_.back().push_back(dirname);
+          lineptr += this->directories_.back().back().size() + 1;
           dirindex++;
         }
     }
@@ -276,18 +281,21 @@ Dwarf_line_info<size, big_endian>::read_header_tables(
           lineptr += strlen(filename) + 1;
 
           uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
-          if (dirindex >= directories_.size())
-            dirindex = 0;
           lineptr += len;
 
+          if (dirindex >= this->directories_.back().size())
+            dirindex = 0;
+         int dirindexi = static_cast<int>(dirindex);
+
           read_unsigned_LEB_128(lineptr, &len);   // mod_time
           lineptr += len;
 
           read_unsigned_LEB_128(lineptr, &len);   // filelength
           lineptr += len;
 
-          gold_assert(fileindex == static_cast<int>(files_.size()));
-          files_.push_back(std::pair<int, std::string>(dirindex, filename));
+          gold_assert(fileindex
+                     == static_cast<int>(this->files_.back().size()));
+          this->files_.back().push_back(std::make_pair(dirindexi, filename));
           fileindex++;
         }
     }
@@ -304,7 +312,7 @@ Dwarf_line_info<size, big_endian>::read_header_tables(
 
 template<int size, bool big_endian>
 bool
-Dwarf_line_info<size, big_endian>::process_one_opcode(
+Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
     const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
 {
   size_t oplen = 0;
@@ -410,27 +418,31 @@ Dwarf_line_info<size, big_endian>::process_one_opcode(
         switch (extended_op)
           {
           case elfcpp::DW_LNE_end_sequence:
+            // This means that the current byte is the one immediately
+            // after a set of instructions.  Record the current line
+            // for up to one less than the current address.
+            lsm->line_num = -1;
             lsm->end_sequence = true;
             *len = oplen;
             return true;
 
           case elfcpp::DW_LNE_set_address:
             {
+              lsm->address = elfcpp::Swap<size, big_endian>::readval(start);
               typename Reloc_map::const_iterator it
                   = reloc_map_.find(start - this->buffer_);
               if (it != reloc_map_.end())
                 {
                   // value + addend.
-                  lsm->address =
-                   (elfcpp::Swap<size, big_endian>::readval(start)
-                    + it->second.second);
+                  lsm->address += it->second.second;
                   lsm->shndx = it->second.first;
                 }
               else
                 {
-                  // Every set_address should have an associated
-                  // relocation.
-                  this->data_valid_ = false;
+                  // If we're a normal .o file, with relocs, every
+                  // set_address should have an associated relocation.
+                 if (this->input_is_relobj())
+                    this->data_valid_ = false;
                 }
               break;
             }
@@ -441,17 +453,19 @@ Dwarf_line_info<size, big_endian>::process_one_opcode(
               start += templen;
 
               uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
-              if (dirindex >= directories_.size())
-                dirindex = 0;
               oplen += templen;
 
+              if (dirindex >= this->directories_.back().size())
+                dirindex = 0;
+             int dirindexi = static_cast<int>(dirindex);
+
               read_unsigned_LEB_128(start, &templen);   // mod_time
               oplen += templen;
 
               read_unsigned_LEB_128(start, &templen);   // filelength
               oplen += templen;
 
-              files_.push_back(std::pair<int, std::string>(dirindex,
+              this->files_.back().push_back(std::make_pair(dirindexi,
                                                           filename));
             }
             break;
@@ -481,7 +495,7 @@ Dwarf_line_info<size, big_endian>::process_one_opcode(
 
 template<int size, bool big_endian>
 unsigned const char*
-Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
+Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
 {
   struct LineStateMachine lsm;
 
@@ -506,7 +520,8 @@ Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
           if (add_line)
             {
               Offset_to_lineno_entry entry
-                  = { lsm.address, lsm.file_num, lsm.line_num };
+                  = { lsm.address, this->current_header_index_,
+                      lsm.file_num, lsm.line_num };
               line_number_map_[lsm.shndx].push_back(entry);
             }
           lineptr += oplength;
@@ -520,12 +535,12 @@ Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
 
 template<int size, bool big_endian>
 unsigned int
-Dwarf_line_info<size, big_endian>::symbol_section(
+Sized_dwarf_line_info<size, big_endian>::symbol_section(
     unsigned int sym,
     typename elfcpp::Elf_types<size>::Elf_Addr* value)
 {
   const int symsize = elfcpp::Elf_sizes<size>::sym_size;
-  gold_assert(this->symtab_buffer_ + sym * symsize < this->symtab_buffer_end_);
+  gold_assert(sym * symsize < this->symtab_buffer_size_);
   elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
   *value = elfsym.get_st_value();
   return elfsym.get_st_shndx();
@@ -535,7 +550,7 @@ Dwarf_line_info<size, big_endian>::symbol_section(
 
 template<int size, bool big_endian>
 void
-Dwarf_line_info<size, big_endian>::read_relocs()
+Sized_dwarf_line_info<size, big_endian>::read_relocs()
 {
   if (this->symtab_buffer_ == NULL)
     return;
@@ -555,10 +570,9 @@ Dwarf_line_info<size, big_endian>::read_relocs()
 
 template<int size, bool big_endian>
 void
-Dwarf_line_info<size, big_endian>::read_line_mappings()
+Sized_dwarf_line_info<size, big_endian>::read_line_mappings()
 {
-  if (this->data_valid_ == false)
-    return;
+  gold_assert(this->data_valid_ == true);
 
   read_relocs();
   while (this->buffer_ < this->buffer_end_)
@@ -578,40 +592,177 @@ Dwarf_line_info<size, big_endian>::read_line_mappings()
     std::sort(it->second.begin(), it->second.end());
 }
 
+// Some processing depends on whether the input is a .o file or not.
+// For instance, .o files have relocs, and have .debug_lines
+// information on a per section basis.  .so files, on the other hand,
+// lack relocs, and offsets are unique, so we can ignore the section
+// information.
+
+template<int size, bool big_endian>
+bool
+Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
+{
+  // Only .o files have relocs and the symtab buffer that goes with them.
+  return this->symtab_buffer_ != NULL;
+}
+
+// Given an Offset_to_lineno_entry vector, and an offset, figure out
+// if the offset points into a function according to the vector (see
+// comments below for the algorithm).  If it does, return an iterator
+// into the vector that points to the line-number that contains that
+// offset.  If not, it returns vector::end().
+
+static std::vector<Offset_to_lineno_entry>::const_iterator
+offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
+                   off_t offset)
+{
+  const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
+
+  // lower_bound() returns the smallest offset which is >= lookup_key.
+  // If no offset in offsets is >= lookup_key, returns end().
+  std::vector<Offset_to_lineno_entry>::const_iterator it
+      = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
+
+  // This code is easiest to understand with a concrete example.
+  // Here's a possible offsets array:
+  // {{offset = 3211, header_num = 0, file_num = 1, line_num = 16},  // 0
+  //  {offset = 3224, header_num = 0, file_num = 1, line_num = 20},  // 1
+  //  {offset = 3226, header_num = 0, file_num = 1, line_num = 22},  // 2
+  //  {offset = 3231, header_num = 0, file_num = 1, line_num = 25},  // 3
+  //  {offset = 3232, header_num = 0, file_num = 1, line_num = -1},  // 4
+  //  {offset = 3232, header_num = 0, file_num = 1, line_num = 65},  // 5
+  //  {offset = 3235, header_num = 0, file_num = 1, line_num = 66},  // 6
+  //  {offset = 3236, header_num = 0, file_num = 1, line_num = -1},  // 7
+  //  {offset = 5764, header_num = 0, file_num = 1, line_num = 47},  // 8
+  //  {offset = 5765, header_num = 0, file_num = 1, line_num = 48},  // 9
+  //  {offset = 5767, header_num = 0, file_num = 1, line_num = 49},  // 10
+  //  {offset = 5768, header_num = 0, file_num = 1, line_num = 50},  // 11
+  //  {offset = 5773, header_num = 0, file_num = 1, line_num = -1},  // 12
+  //  {offset = 5787, header_num = 1, file_num = 1, line_num = 19},  // 13
+  //  {offset = 5790, header_num = 1, file_num = 1, line_num = 20},  // 14
+  //  {offset = 5793, header_num = 1, file_num = 1, line_num = 67},  // 15
+  //  {offset = 5793, header_num = 1, file_num = 1, line_num = -1},  // 16
+  //  {offset = 5795, header_num = 1, file_num = 1, line_num = 68},  // 17
+  //  {offset = 5798, header_num = 1, file_num = 1, line_num = -1},  // 18
+  // The entries with line_num == -1 mark the end of a function: the
+  // associated offset is one past the last instruction in the
+  // function.  This can correspond to the beginning of the next
+  // function (as is true for offset 3232); alternately, there can be
+  // a gap between the end of one function and the start of the next
+  // (as is true for some others, most obviously from 3236->5764).
+  //
+  // Case 1: lookup_key has offset == 10.  lower_bound returns
+  //         offsets[0].  Since it's not an exact match and we're
+  //         at the beginning of offsets, we return end() (invalid).
+  // Case 2: lookup_key has offset 10000.  lower_bound returns
+  //         offset[19] (end()).  We return end() (invalid).
+  // Case 3: lookup_key has offset == 3211.  lower_bound matches
+  //         offsets[0] exactly, and that's the entry we return.
+  // Case 4: lookup_key has offset == 3232.  lower_bound returns
+  //         offsets[4].  That's an exact match, but indicates
+  //         end-of-function.  We check if offsets[5] is also an
+  //         exact match but not end-of-function.  It is, so we
+  //         return offsets[5].
+  // Case 5: lookup_key has offset == 3214.  lower_bound returns
+  //         offsets[1].  Since it's not an exact match, we back
+  //         up to the offset that's < lookup_key, offsets[0].
+  //         We note offsets[0] is a valid entry (not end-of-function),
+  //         so that's the entry we return.
+  // Case 6: lookup_key has offset == 4000.  lower_bound returns
+  //         offsets[8].  Since it's not an exact match, we back
+  //         up to offsets[7].  Since offsets[7] indicates
+  //         end-of-function, we know lookup_key is between
+  //         functions, so we return end() (not a valid offset).
+  // Case 7: lookup_key has offset == 5794.  lower_bound returns
+  //         offsets[17].  Since it's not an exact match, we back
+  //         up to offsets[15].  Note we back up to the *first*
+  //         entry with offset 5793, not just offsets[17-1].
+  //         We note offsets[15] is a valid entry, so we return it.
+  //         If offsets[15] had had line_num == -1, we would have
+  //         checked offsets[16].  The reason for this is that
+  //         15 and 16 can be in an arbitrary order, since we sort
+  //         only by offset.  (Note it doesn't help to use line_number
+  //         as a secondary sort key, since sometimes we want the -1
+  //         to be first and sometimes we want it to be last.)
+
+  // This deals with cases (1) and (2).
+  if ((it == offsets->begin() && offset < it->offset)
+      || it == offsets->end())
+    return offsets->end();
+
+  // This deals with cases (3) and (4).
+  if (offset == it->offset)
+    {
+      while (it != offsets->end()
+             && it->offset == offset
+             && it->line_num == -1)
+        ++it;
+      if (it == offsets->end() || it->offset != offset)
+        return offsets->end();
+      else
+        return it;
+    }
+
+  // This handles the first part of case (7) -- we back up to the
+  // *first* entry that has the offset that's behind us.
+  gold_assert(it != offsets->begin());
+  std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
+  --it;
+  const off_t range_value = it->offset;
+  while (it != offsets->begin() && (it-1)->offset == range_value)
+    --it;
+
+  // This handles cases (5), (6), and (7): if any entry in the
+  // equal_range [it, range_end) has a line_num != -1, it's a valid
+  // match.  If not, we're not in a function.
+  for (; it != range_end; ++it)
+    if (it->line_num != -1)
+      return it;
+  return offsets->end();
+}
+
 // Return a string for a file name and line number.
 
 template<int size, bool big_endian>
 std::string
-Dwarf_line_info<size, big_endian>::addr2line(unsigned int shndx, off_t offset)
+Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
+                                                      off_t offset)
 {
   if (this->data_valid_ == false)
     return "";
 
-  const Offset_to_lineno_entry lookup_key = { offset, 0, 0 };
-  std::vector<Offset_to_lineno_entry>& offsets = this->line_number_map_[shndx];
-  if (offsets.empty())
+  const std::vector<Offset_to_lineno_entry>* offsets;
+  // If we do not have reloc information, then our input is a .so or
+  // some similar data structure where all the information is held in
+  // the offset.  In that case, we ignore the input shndx.
+  if (this->input_is_relobj())
+    offsets = &this->line_number_map_[shndx];
+  else
+    offsets = &this->line_number_map_[-1U];
+  if (offsets->empty())
     return "";
 
   typename std::vector<Offset_to_lineno_entry>::const_iterator it
-      = std::lower_bound(offsets.begin(), offsets.end(), lookup_key);
-
-  // If we found an exact match, great, otherwise find the last entry
-  // before the passed-in offset.
-  if (it->offset > offset)
-    {
-      if (it == offsets.begin())
-        return "";
-      --it;
-      gold_assert(it->offset < offset);
-    }
+      = offset_to_iterator(offsets, offset);
+  if (it == offsets->end())
+    return "";
 
   // Convert the file_num + line_num into a string.
   std::string ret;
-  gold_assert(it->file_num < static_cast<int>(files_.size()));
-  const std::pair<int, std::string>& filename_pair = files_[it->file_num];
-  gold_assert(filename_pair.first < static_cast<int>(directories_.size()));
-  const std::string& dirname = directories_[filename_pair.first];
+
+  gold_assert(it->header_num < static_cast<int>(this->files_.size()));
+  gold_assert(it->file_num
+             < static_cast<int>(this->files_[it->header_num].size()));
+  const std::pair<int, std::string>& filename_pair
+      = this->files_[it->header_num][it->file_num];
   const std::string& filename = filename_pair.second;
+
+  gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
+  gold_assert(filename_pair.first
+              < static_cast<int>(this->directories_[it->header_num].size()));
+  const std::string& dirname
+      = this->directories_[it->header_num][filename_pair.first];
+
   if (!dirname.empty())
     {
       ret += dirname;
@@ -629,24 +780,58 @@ Dwarf_line_info<size, big_endian>::addr2line(unsigned int shndx, off_t offset)
   return ret;
 }
 
+// Dwarf_line_info routines.
+
+std::string
+Dwarf_line_info::one_addr2line(Object* object,
+                               unsigned int shndx, off_t offset)
+{
+  if (parameters->get_size() == 32 && !parameters->is_big_endian())
+#ifdef HAVE_TARGET_32_LITTLE
+    return Sized_dwarf_line_info<32, false>(object).addr2line(shndx, offset);
+#else
+    gold_unreachable();
+#endif
+  else if (parameters->get_size() == 32 && parameters->is_big_endian())
+#ifdef HAVE_TARGET_32_BIG
+    return Sized_dwarf_line_info<32, true>(object).addr2line(shndx, offset);
+#else
+    gold_unreachable();
+#endif
+  else if (parameters->get_size() == 64 && !parameters->is_big_endian())
+#ifdef HAVE_TARGET_64_LITTLE
+    return Sized_dwarf_line_info<64, false>(object).addr2line(shndx, offset);
+#else
+    gold_unreachable();
+#endif
+  else if (parameters->get_size() == 64 && parameters->is_big_endian())
+#ifdef HAVE_TARGET_64_BIT
+    return Sized_dwarf_line_info<64, true>(object).addr2line(shndx, offset);
+#else
+    gold_unreachable();
+#endif
+  else
+    gold_unreachable();
+}
+
 #ifdef HAVE_TARGET_32_LITTLE
 template
-class Dwarf_line_info<32, false>;
+class Sized_dwarf_line_info<32, false>;
 #endif
 
 #ifdef HAVE_TARGET_32_BIG
 template
-class Dwarf_line_info<32, true>;
+class Sized_dwarf_line_info<32, true>;
 #endif
 
 #ifdef HAVE_TARGET_64_LITTLE
 template
-class Dwarf_line_info<64, false>;
+class Sized_dwarf_line_info<64, false>;
 #endif
 
 #ifdef HAVE_TARGET_64_BIG
 template
-class Dwarf_line_info<64, true>;
+class Sized_dwarf_line_info<64, true>;
 #endif
 
 } // End namespace gold.
This page took 0.028225 seconds and 4 git commands to generate.