[MIPS/GAS] Split Loongson EXT Instructions from loongson3a.
[deliverable/binutils-gdb.git] / elfcpp / elfcpp_file.h
index 00ab28b36e11f000f9869dca338af2b0335552d9..66c2ffaf978c21ac3918ba997b03e60aa32c0ed7 100644 (file)
@@ -1,6 +1,6 @@
 // elfcpp_file.h -- file access for elfcpp   -*- C++ -*-
 
-// Copyright 2006, 2007, Free Software Foundation, Inc.
+// Copyright (C) 2006-2018 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of elfcpp.
@@ -60,6 +60,8 @@
 #include <cstdio>
 #include <cstring>
 
+#include "elfcpp.h"
+
 namespace elfcpp
 {
 
@@ -142,6 +144,15 @@ class Elf_file
     return this->shnum_;
   }
 
+  unsigned int
+  shnum() const
+  {
+    if (this->shnum_ == 0 && this->shoff_ != 0)
+      this->file_->error(_("ELF file has not been initialized yet"
+                          " (internal error)"));
+    return this->shnum_;
+  }
+
   // Return the section index of the section name string table.
   unsigned int
   shstrndx()
@@ -150,6 +161,18 @@ class Elf_file
     return this->shstrndx_;
   }
 
+  unsigned int
+  shstrndx() const
+  {
+    if (this->shstrndx_ == SHN_XINDEX && this->shoff_ != 0)
+      {
+       this->file_->error(_("ELF file has not been initialized yet"
+                            " (internal error)"));
+       return 0;
+      }
+    return this->shstrndx_;
+  }
+
   // Return the value to subtract from section indexes >=
   // SHN_LORESERVE.  See the comment in initialize_shnum.
   int
@@ -159,6 +182,15 @@ class Elf_file
     return this->large_shndx_offset_;
   }
 
+  int
+  large_shndx_offset() const
+  {
+    if (this->shstrndx_ == SHN_XINDEX && this->shoff_ != 0)
+      this->file_->error(_("ELF file has not been initialized yet"
+                          " (internal error)"));
+    return this->large_shndx_offset_;
+  }
+
   // Return the location of the header of section SHNDX.
   typename File::Location
   section_header(unsigned int shndx)
@@ -169,7 +201,7 @@ class Elf_file
 
   // Return the name of section SHNDX.
   std::string
-  section_name(unsigned int shndx);
+  section_name(unsigned int shndx) const;
 
   // Return the location of the contents of section SHNDX.
   typename File::Location
@@ -214,7 +246,7 @@ class Elf_file
 
   // Return the file offset of the header of section SHNDX.
   off_t
-  section_header_offset(unsigned int shndx);
+  section_header_offset(unsigned int shndx) const;
 
   // The file we are reading.
   File* file_;
@@ -228,6 +260,33 @@ class Elf_file
   int large_shndx_offset_;
 };
 
+// A small wrapper around SHT_STRTAB data mapped to memory. It checks that the
+// index is not out of bounds and the string is NULL-terminated.
+
+class Elf_strtab
+{
+ public:
+  // Construct an Elf_strtab for a section with contents *P and size SIZE.
+  Elf_strtab(const unsigned char* p, size_t size);
+
+  // Return the file offset to the section headers.
+  bool
+  get_c_string(size_t offset, const char** cstring) const
+  {
+    if (offset >= this->usable_size_)
+      return false;
+    *cstring = this->base_ + offset;
+    return true;
+  }
+
+ private:
+  // Contents of the section mapped to memory.
+  const char* base_;
+  // One larger that the position of the last NULL character in the section.
+  // For valid SHT_STRTAB sections, this is the size of the section.
+  size_t usable_size_;
+};
+
 // Inline function definitions.
 
 // Check for presence of the ELF magic number.
@@ -436,7 +495,7 @@ Elf_file<size, big_endian, File>::find_section_by_type(unsigned int type)
 
 template<int size, bool big_endian, typename File>
 off_t
-Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
+Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx) const
 {
   if (shndx >= this->shnum())
     this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
@@ -448,7 +507,7 @@ Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
 
 template<int size, bool big_endian, typename File>
 std::string
-Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
+Elf_file<size, big_endian, File>::section_name(unsigned int shndx) const
 {
   File* const file = this->file_;
 
@@ -463,7 +522,7 @@ Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
 
   // Get the file offset for the section name string table data.
   off_t shstr_off;
-  off_t shstr_size;
+  typename Elf_types<size>::Elf_WXword shstr_size;
   {
     const unsigned int shstrndx = this->shstrndx_;
     typename File::View v(file->view(this->section_header_offset(shstrndx),
@@ -642,6 +701,18 @@ Elf_file<size, big_endian, File>::section_addralign(unsigned int shndx)
   return shdr.get_sh_addralign();
 }
 
+inline
+Elf_strtab::Elf_strtab(const unsigned char* p, size_t size)
+{
+  // Check if the section is NUL-terminated. If it isn't, we ignore
+  // the last part to make sure we don't return non-NUL-terminated
+  // strings.
+  while (size > 0 && p[size - 1] != 0)
+    size--;
+  this->base_ = reinterpret_cast<const char*>(p);
+  this->usable_size_ = size;
+}
+
 } // End namespace elfcpp.
 
 #endif // !defined(ELFCPP_FILE_H)
This page took 0.026122 seconds and 4 git commands to generate.