2011-03-04 Michael Snyder <msnyder@msnyder-server.eng.vmware.com>
[deliverable/binutils-gdb.git] / gold / dwarf_reader.h
index a36771554a7d7636aa5f7984726c3610aebb1311..8fe7898a55ab7e8e435276ecb397c358489de579 100644 (file)
@@ -1,6 +1,6 @@
 // dwarf_reader.h -- parse dwarf2/3 debug information for gold  -*- C++ -*-
 
-// Copyright 2007 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.
 #define GOLD_DWARF_READER_H
 
 #include <vector>
+#include <map>
 
+#include "elfcpp.h"
 #include "elfcpp_swap.h"
 #include "dwarf.h"
+#include "reloc.h"
 
 namespace gold
 {
 
+template<int size, bool big_endian>
+class Track_relocs;
 struct LineStateMachine;
 
+// We can't do better than to keep the offsets in a sorted vector.
+// Here, offset is the key, and file_num/line_num is the value.
+struct Offset_to_lineno_entry
+{
+  off_t offset;
+  int header_num;  // which file-list to use (i.e. which .o file are we in)
+  int file_num;    // a pointer into files_
+  int line_num;    // the line number in the source file
+
+  // When we add entries to the table, we always use the last entry
+  // with a given offset.  Given proper DWARF info, this should ensure
+  // that the offset is a sufficient sort key.
+  bool operator<(const Offset_to_lineno_entry& that) const
+  { return this->offset < that.offset; }
+};
+
 // This class is used to read the line information from the debugging
 // section of an object file.
 
 class Dwarf_line_info
 {
  public:
-  // Initializes a .debug_line reader. Buffer and buffer length point
-  // to the beginning and length of the line information to read.
-  // Reader is a ByteReader class that has the endianness set
-  // properly.
-  Dwarf_line_info(const unsigned char* buffer, off_t buffer_length)
-    : buffer_(buffer), buffer_end_(buffer + buffer_length),
-      directories_(1), files_(1)
+  Dwarf_line_info()
   { }
 
-  // Start processing line info, and populates the offset_map_.
-  template<int size, bool big_endian>
-  void
-  read_line_mappings()
-  {
-    while (buffer_ < buffer_end_)
-      {
-        const unsigned char* lineptr = buffer_;
-        lineptr = this->read_header_prolog<size, big_endian>(lineptr);
-        lineptr = this->read_header_tables(lineptr);
-        lineptr = this->read_lines(size, big_endian, lineptr);
-        buffer_ = lineptr;
-      }
-    finalize_line_number_map();
-  }
+  virtual
+  ~Dwarf_line_info()
+  { }
 
   // Given a section number and an offset, returns the associated
   // file and line-number, as a string: "file:lineno".  If unable
   // to do the mapping, returns the empty string.  You must call
   // read_line_mappings() before calling this function.
   std::string
-  addr2line(unsigned int shndx, off_t offset);
+  addr2line(unsigned int shndx, off_t offset)
+  { return do_addr2line(shndx, offset); }
+
+  // A helper function for a single addr2line lookup.  It also keeps a
+  // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
+  // set to 0 not to cache at all.  The larger CACHE_SIZE is, the more
+  // chance this routine won't have to re-create a Dwarf_line_info
+  // object for its addr2line computation; such creations are slow.
+  // NOTE: Not thread-safe, so only call from one thread at a time.
+  static std::string
+  one_addr2line(Object* object, unsigned int shndx, off_t offset,
+                size_t cache_size);
+
+  // This reclaims all the memory that one_addr2line may have cached.
+  // Use this when you know you will not be calling one_addr2line again.
+  static void
+  clear_addr2line_cache();
 
  private:
+  virtual std::string
+  do_addr2line(unsigned int shndx, off_t offset) = 0;
+};
+
+template<int size, bool big_endian>
+class Sized_dwarf_line_info : public Dwarf_line_info
+{
+ public:
+  // Initializes a .debug_line reader for a given object file.
+  // If SHNDX is specified and non-negative, only read the debug
+  // information that pertains to the specified section.
+  Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
+
+ private:
+  std::string
+  do_addr2line(unsigned int shndx, off_t offset);
+
+  // Start processing line info, and populates the offset_map_.
+  // If SHNDX is non-negative, only store debug information that
+  // pertains to the specified section.
+  void
+  read_line_mappings(Object*, unsigned int shndx);
+
+  // Reads the relocation section associated with .debug_line and
+  // stores relocation information in reloc_map_.
+  void
+  read_relocs(Object*);
+
+  // Looks in the symtab to see what section a symbol is in.
+  unsigned int
+  symbol_section(Object*, unsigned int sym,
+                 typename elfcpp::Elf_types<size>::Elf_Addr* value,
+                bool* is_ordinary);
+
   // Reads the DWARF2/3 header for this line info.  Each takes as input
   // a starting buffer position, and returns the ending position.
-  template<int size, bool big_endian>
   const unsigned char*
   read_header_prolog(const unsigned char* lineptr);
 
   const unsigned char*
   read_header_tables(const unsigned char* lineptr);
 
-  // Reads the DWARF2/3 line information.
+  // Reads the DWARF2/3 line information.  If shndx is non-negative,
+  // discard all line information that doesn't pertain to the given
+  // section.
   const unsigned char*
-  read_lines(int size, bool big_endian, const unsigned char* lineptr);
+  read_lines(const unsigned char* lineptr, unsigned int shndx);
 
   // Process a single line info opcode at START using the state
   // machine at LSM.  Return true if we should define a line using the
   // current state of the line state machine.  Place the length of the
   // opcode in LEN.
   bool
-  process_one_opcode(int size, bool big_endian,
-                     const unsigned char* start,
+  process_one_opcode(const unsigned char* start,
                      struct LineStateMachine* lsm, size_t* len);
 
-  // Called after all line number have been read, to ready
-  // line_number_map_ for calls to addr2line().
-  void
-  finalize_line_number_map();
+  // Some parts of processing differ depending on whether the input
+  // was a .o file or not.
+  bool input_is_relobj();
+
+  // If we saw anything amiss while parsing, we set this to false.
+  // Then addr2line will always fail (rather than return possibly-
+  // corrupt data).
+  bool data_valid_;
 
   // A DWARF2/3 line info header.  This is not the same size as in the
   // actual file, as the one in the file may have a 32 bit or 64 bit
@@ -120,24 +177,35 @@ class Dwarf_line_info
   // buffer is the buffer for our line info, starting at exactly where
   // the line info to read is.
   const unsigned char* buffer_;
-  const unsigned char* const buffer_end_;
+  const unsigned char* buffer_end_;
+
+  // This has relocations that point into buffer.
+  Track_relocs<size, big_endian> track_relocs_;
+  // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
+  unsigned int track_relocs_type_;
+
+  // This is used to figure out what section to apply a relocation to.
+  const unsigned char* symtab_buffer_;
+  section_size_type symtab_buffer_size_;
 
-  // Holds the directories and files as we see them.
-  std::vector<std::string> directories_;
+  // Holds the directories and files as we see them.  We have an array
+  // of directory-lists, one for each .o file we're reading (usually
+  // there will just be one, but there may be more if input is a .so).
+  std::vector<std::vector<std::string> > directories_;
   // The first part is an index into directories_, the second the filename.
-  std::vector< std::pair<int, std::string> > files_;
+  std::vector<std::vector< std::pair<int, std::string> > > files_;
+
+  // An index into the current directories_ and files_ vectors.
+  int current_header_index_;
+
+  // A sorted map from offset of the relocation target to the shndx
+  // and addend for the relocation.
+  typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr,
+                   std::pair<unsigned int,
+                             typename elfcpp::Elf_types<size>::Elf_Swxword> >
+  Reloc_map;
+  Reloc_map reloc_map_;
 
-  // We can't do better than to keep the offsets in a sorted vector.
-  // Here, offset is the key, and file_num/line_num is the value.
-  struct Offset_to_lineno_entry
-  {
-    off_t offset;
-    int file_num;    // a pointer into files_
-    int line_num;
-    // Offsets are unique within a section, so that's a sufficient sort key.
-    bool operator<(const Offset_to_lineno_entry& that) const
-    { return this->offset < that.offset; }
-  };
   // We have a vector of offset->lineno entries for every input section.
   typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
   Lineno_map;
This page took 0.024866 seconds and 4 git commands to generate.