* mips-tdep.c (mips_eabi_push_dummy_call): Place signed, rather
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
index e345ff8eccf8b90ca93053375458f4f9d2a8aa60..7fbfdaedc1884cb4f6c09cb63a1c30c4cf9bd62b 100644 (file)
@@ -1,6 +1,6 @@
 // dwarf_reader.cc -- parse dwarf2/3 debug information
 
-// Copyright 2007, 2008 Free Software Foundation, Inc.
+// Copyright 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -23,6 +23,7 @@
 #include "gold.h"
 
 #include <algorithm>
+#include <vector>
 
 #include "elfcpp_swap.h"
 #include "dwarf.h"
 #include "parameters.h"
 #include "reloc.h"
 #include "dwarf_reader.h"
-
-namespace {
-
-// Read an unsigned LEB128 number.  Each byte contains 7 bits of
-// information, plus one bit saying whether the number continues or
-// not.
-
-uint64_t
-read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
-{
-  uint64_t result = 0;
-  size_t num_read = 0;
-  unsigned int shift = 0;
-  unsigned char byte;
-
-  do
-    {
-      byte = *buffer++;
-      num_read++;
-      result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
-      shift += 7;
-    }
-  while (byte & 0x80);
-
-  *len = num_read;
-
-  return result;
-}
-
-// Read a signed LEB128 number.  These are like regular LEB128
-// numbers, except the last byte may have a sign bit set.
-
-int64_t
-read_signed_LEB_128(const unsigned char* buffer, size_t* len)
-{
-  int64_t result = 0;
-  int shift = 0;
-  size_t num_read = 0;
-  unsigned char byte;
-
-  do
-    {
-      byte = *buffer++;
-      num_read++;
-      result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
-      shift += 7;
-    }
-  while (byte & 0x80);
-
-  if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
-    result |= -((static_cast<int64_t>(1)) << shift);
-  *len = num_read;
-  return result;
-}
-
-} // End anonymous namespace.
-
+#include "int_encoding.h"
+#include "compressed_output.h"
 
 namespace gold {
 
-// This is the format of a DWARF2/3 line state machine that we process
-// opcodes using.  There is no need for anything outside the lineinfo
-// processor to know how this works.
-
 struct LineStateMachine
 {
   int file_num;
@@ -121,24 +63,42 @@ ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
 
 template<int size, bool big_endian>
 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
-                                                               off_t read_shndx)
+                                                               unsigned int read_shndx)
   : 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)
-    // FIXME: do this more efficiently: section_name() isn't super-fast
-    if (object->section_name(debug_shndx) == ".debug_line")
-      {
-        section_size_type buffer_size;
-        this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
-                                                false);
-        this->buffer_end_ = this->buffer_ + buffer_size;
-        break;
-      }
+  for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
+    {
+      // FIXME: do this more efficiently: section_name() isn't super-fast
+      std::string name = object->section_name(debug_shndx);
+      if (name == ".debug_line" || name == ".zdebug_line")
+       {
+         section_size_type buffer_size;
+         this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
+                                                  false);
+         this->buffer_end_ = this->buffer_ + buffer_size;
+         break;
+       }
+    }
   if (this->buffer_ == NULL)
     return;
 
+  section_size_type uncompressed_size = 0;
+  unsigned char* uncompressed_data = NULL;
+  if (object->section_is_compressed(debug_shndx, &uncompressed_size))
+    {
+      uncompressed_data = new unsigned char[uncompressed_size];
+      if (!decompress_input_section(this->buffer_,
+                                   this->buffer_end_ - this->buffer_,
+                                   uncompressed_data,
+                                   uncompressed_size))
+       object->error(_("could not decompress section %s"),
+                     object->section_name(debug_shndx).c_str());
+      this->buffer_ = uncompressed_data;
+      this->buffer_end_ = this->buffer_ + uncompressed_size;
+    }
+
   // Find the relocation section for ".debug_line".
   // We expect these for relobjs (.o's) but not dynobjs (.so's).
   bool got_relocs = false;
@@ -153,6 +113,7 @@ Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
        {
          got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
                                                       reloc_sh_type);
+         this->track_relocs_type_ = reloc_sh_type;
          break;
        }
     }
@@ -175,7 +136,7 @@ Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
   // Now that we have successfully read all the data, parse the debug
   // info.
   this->data_valid_ = true;
-  this->read_line_mappings(read_shndx);
+  this->read_line_mappings(object, read_shndx);
 }
 
 // Read the DWARF header.
@@ -310,10 +271,6 @@ Sized_dwarf_line_info<size, big_endian>::read_header_tables(
 
 // Process a single opcode in the .debug.line structure.
 
-// Templating on size and big_endian would yield more efficient (and
-// simpler) code, but would bloat the binary.  Speed isn't important
-// here.
-
 template<int size, bool big_endian>
 bool
 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
@@ -432,13 +389,21 @@ Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
 
           case elfcpp::DW_LNE_set_address:
             {
-              lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start);
+              lsm->address =
+               elfcpp::Swap_unaligned<size, big_endian>::readval(start);
               typename Reloc_map::const_iterator it
-                  = reloc_map_.find(start - this->buffer_);
+                  = this->reloc_map_.find(start - this->buffer_);
               if (it != reloc_map_.end())
                 {
-                  // value + addend.
-                  lsm->address += it->second.second;
+                 // If this is a SHT_RELA section, then ignore the
+                 // section contents.  This assumes that this is a
+                 // straight reloc which just uses the reloc addend.
+                 // The reloc addend has already been included in the
+                 // symbol value.
+                 if (this->track_relocs_type_ == elfcpp::SHT_RELA)
+                   lsm->address = 0;
+                 // Add in the symbol value.
+                 lsm->address += it->second.second;
                   lsm->shndx = it->second.first;
                 }
               else
@@ -500,7 +465,7 @@ Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
 template<int size, bool big_endian>
 unsigned const char*
 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
-                                                    off_t shndx)
+                                                    unsigned int shndx)
 {
   struct LineStateMachine lsm;
 
@@ -542,21 +507,23 @@ Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr
 template<int size, bool big_endian>
 unsigned int
 Sized_dwarf_line_info<size, big_endian>::symbol_section(
+    Object* object,
     unsigned int sym,
-    typename elfcpp::Elf_types<size>::Elf_Addr* value)
+    typename elfcpp::Elf_types<size>::Elf_Addr* value,
+    bool* is_ordinary)
 {
   const int symsize = elfcpp::Elf_sizes<size>::sym_size;
   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();
+  return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
 }
 
 // Read the relocations into a Reloc_map.
 
 template<int size, bool big_endian>
 void
-Sized_dwarf_line_info<size, big_endian>::read_relocs()
+Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
 {
   if (this->symtab_buffer_ == NULL)
     return;
@@ -566,8 +533,19 @@ Sized_dwarf_line_info<size, big_endian>::read_relocs()
   while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
     {
       const unsigned int sym = this->track_relocs_.next_symndx();
-      const unsigned int shndx = this->symbol_section(sym, &value);
-      this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
+
+      bool is_ordinary;
+      const unsigned int shndx = this->symbol_section(object, sym, &value,
+                                                     &is_ordinary);
+
+      // There is no reason to record non-ordinary section indexes, or
+      // SHN_UNDEF, because they will never match the real section.
+      if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
+       {
+         value += this->track_relocs_.next_addend();
+         this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
+       }
+
       this->track_relocs_.advance(reloc_offset + 1);
     }
 }
@@ -576,11 +554,12 @@ Sized_dwarf_line_info<size, big_endian>::read_relocs()
 
 template<int size, bool big_endian>
 void
-Sized_dwarf_line_info<size, big_endian>::read_line_mappings(off_t shndx)
+Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
+                                                           unsigned int shndx)
 {
   gold_assert(this->data_valid_ == true);
 
-  read_relocs();
+  this->read_relocs(object);
   while (this->buffer_ < this->buffer_end_)
     {
       const unsigned char* lineptr = this->buffer_;
@@ -788,35 +767,120 @@ Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
 
 // Dwarf_line_info routines.
 
+static unsigned int next_generation_count = 0;
+
+struct Addr2line_cache_entry
+{
+  Object* object;
+  unsigned int shndx;
+  Dwarf_line_info* dwarf_line_info;
+  unsigned int generation_count;
+  unsigned int access_count;
+
+  Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
+      : object(o), shndx(s), dwarf_line_info(d),
+        generation_count(next_generation_count), access_count(0)
+  {
+    if (next_generation_count < (1U << 31))
+      ++next_generation_count;
+  }
+};
+// We expect this cache to be small, so don't bother with a hashtable
+// or priority queue or anything: just use a simple vector.
+static std::vector<Addr2line_cache_entry> addr2line_cache;
+
 std::string
 Dwarf_line_info::one_addr2line(Object* object,
-                               unsigned int shndx, off_t offset)
+                               unsigned int shndx, off_t offset,
+                               size_t cache_size)
 {
-  switch (parameters->size_and_endianness())
+  Dwarf_line_info* lineinfo = NULL;
+  std::vector<Addr2line_cache_entry>::iterator it;
+
+  // First, check the cache.  If we hit, update the counts.
+  for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
     {
+      if (it->object == object && it->shndx == shndx)
+        {
+          lineinfo = it->dwarf_line_info;
+          it->generation_count = next_generation_count;
+          // We cap generation_count at 2^31 -1 to avoid overflow.
+          if (next_generation_count < (1U << 31))
+            ++next_generation_count;
+          // We cap access_count at 31 so 2^access_count doesn't overflow
+          if (it->access_count < 31)
+            ++it->access_count;
+          break;
+        }
+    }
+
+  // If we don't hit the cache, create a new object and insert into the
+  // cache.
+  if (lineinfo == NULL)
+  {
+    switch (parameters->size_and_endianness())
+      {
 #ifdef HAVE_TARGET_32_LITTLE
-    case Parameters::TARGET_32_LITTLE:
-      return Sized_dwarf_line_info<32, false>(object, shndx).addr2line(shndx,
-                                                                       offset);
+        case Parameters::TARGET_32_LITTLE:
+          lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
 #endif
 #ifdef HAVE_TARGET_32_BIG
-    case Parameters::TARGET_32_BIG:
-      return Sized_dwarf_line_info<32, true>(object, shndx).addr2line(shndx,
-                                                                      offset);
+        case Parameters::TARGET_32_BIG:
+          lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
 #endif
 #ifdef HAVE_TARGET_64_LITTLE
-    case Parameters::TARGET_64_LITTLE:
-      return Sized_dwarf_line_info<64, false>(object, shndx).addr2line(shndx,
-                                                                       offset);
+        case Parameters::TARGET_64_LITTLE:
+          lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
 #endif
 #ifdef HAVE_TARGET_64_BIG
-    case Parameters::TARGET_64_BIG:
-      return Sized_dwarf_line_info<64, true>(object, shndx).addr2line(shndx,
-                                                                      offset);
+        case Parameters::TARGET_64_BIG:
+          lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
 #endif
-    default:
-      gold_unreachable();
+        default:
+          gold_unreachable();
+      }
+    addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
+  }
+
+  // Now that we have our object, figure out the answer
+  std::string retval = lineinfo->addr2line(shndx, offset);
+
+  // Finally, if our cache has grown too big, delete old objects.  We
+  // assume the common (probably only) case is deleting only one object.
+  // We use a pretty simple scheme to evict: function of LRU and MFU.
+  while (addr2line_cache.size() > cache_size)
+    {
+      unsigned int lowest_score = ~0U;
+      std::vector<Addr2line_cache_entry>::iterator lowest
+          = addr2line_cache.end();
+      for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
+        {
+          const unsigned int score = (it->generation_count
+                                      + (1U << it->access_count));
+          if (score < lowest_score)
+            {
+              lowest_score = score;
+              lowest = it;
+            }
+        }
+      if (lowest != addr2line_cache.end())
+        {
+          delete lowest->dwarf_line_info;
+          addr2line_cache.erase(lowest);
+        }
     }
+
+  return retval;
+}
+
+void
+Dwarf_line_info::clear_addr2line_cache()
+{
+  for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
+       it != addr2line_cache.end();
+       ++it)
+    delete it->dwarf_line_info;
+  addr2line_cache.clear();
 }
 
 #ifdef HAVE_TARGET_32_LITTLE
This page took 0.026858 seconds and 4 git commands to generate.