2011-03-04 Michael Snyder <msnyder@msnyder-server.eng.vmware.com>
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
index ec697e428efde3f2c342d88c6ceb8a125e1cbae6..8110f38143522181152917c7268a228220d2cf14 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.
 #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;
@@ -122,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;
@@ -154,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;
        }
     }
@@ -311,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(
@@ -433,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
@@ -501,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;
 
@@ -529,7 +493,18 @@ Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr
               Offset_to_lineno_entry entry
                   = { lsm.address, this->current_header_index_,
                       lsm.file_num, lsm.line_num };
-              line_number_map_[lsm.shndx].push_back(entry);
+             std::vector<Offset_to_lineno_entry>&
+               map(this->line_number_map_[lsm.shndx]);
+             // If we see two consecutive entries with the same
+             // offset and a real line number, then always use the
+             // second one.
+             if (!map.empty()
+                 && (map.back().offset == static_cast<off_t>(lsm.address))
+                 && lsm.line_num != -1
+                 && map.back().line_num != -1)
+               map.back() = entry;
+             else
+               map.push_back(entry);
             }
           lineptr += oplength;
         }
@@ -577,7 +552,10 @@ Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
       // 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)
-       this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
+       {
+         value += this->track_relocs_.next_addend();
+         this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
+       }
 
       this->track_relocs_.advance(reloc_offset + 1);
     }
@@ -588,7 +566,7 @@ Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
 template<int size, bool big_endian>
 void
 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
-                                                           off_t shndx)
+                                                           unsigned int shndx)
 {
   gold_assert(this->data_valid_ == true);
 
This page took 0.026462 seconds and 4 git commands to generate.