[llvm] r240810 - Fix error handling in getString and simplify callers.

Rafael Espindola rafael.espindola at gmail.com
Fri Jun 26 11:42:17 PDT 2015


Author: rafael
Date: Fri Jun 26 13:42:17 2015
New Revision: 240810

URL: http://llvm.org/viewvc/llvm-project?rev=240810&view=rev
Log:
Fix error handling in getString and simplify callers.

Modified:
    llvm/trunk/include/llvm/Object/ELF.h

Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=240810&r1=240809&r2=240810&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Fri Jun 26 13:42:17 2015
@@ -304,7 +304,7 @@ public:
   const T        *getEntry(uint32_t Section, uint32_t Entry) const;
   template <typename T>
   const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
-  const char     *getString(const Elf_Shdr *section, uint32_t offset) const;
+  ErrorOr<StringRef> getString(const Elf_Shdr *Section, uint32_t Offset) const;
   const char *getDynamicString(uintX_t Offset) const;
   ErrorOr<StringRef> getSymbolVersion(const Elf_Shdr *section,
                                       const Elf_Sym *Symb,
@@ -934,13 +934,12 @@ ELFFile<ELFT>::getSection(uint32_t index
 }
 
 template <class ELFT>
-const char *ELFFile<ELFT>::getString(const Elf_Shdr *section,
-                                     ELF::Elf32_Word offset) const {
-  assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
-  if (offset >= section->sh_size)
-    // FIXME: Proper error handling.
-    report_fatal_error("Symbol name offset outside of string table!");
-  return (const char *)base() + section->sh_offset + offset;
+ErrorOr<StringRef> ELFFile<ELFT>::getString(const Elf_Shdr *Section,
+                                            ELF::Elf32_Word Offset) const {
+  assert(Section && Section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
+  if (Offset >= Section->sh_size)
+    return object_error::parse_failed;
+  return StringRef((const char *)base() + Section->sh_offset + Offset);
 }
 
 template <class ELFT>
@@ -969,21 +968,14 @@ ELFFile<ELFT>::getStaticSymbolName(const
 template <class ELFT>
 ErrorOr<StringRef> ELFFile<ELFT>::getSymbolName(const Elf_Shdr *Section,
                                                 const Elf_Sym *Symb) const {
-  if (Symb->st_name == 0)
-    return StringRef("");
-
   const Elf_Shdr *StrTab = getSection(Section->sh_link);
-  if (Symb->st_name >= StrTab->sh_size)
-    return object_error::parse_failed;
-  return StringRef(getString(StrTab, Symb->st_name));
+  return getString(StrTab, Symb->st_name);
 }
 
 template <class ELFT>
 ErrorOr<StringRef>
 ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
-  if (Section->sh_name >= dot_shstrtab_sec->sh_size)
-    return object_error::parse_failed;
-  return StringRef(getString(dot_shstrtab_sec, Section->sh_name));
+  return getString(dot_shstrtab_sec, Section->sh_name);
 }
 
 template <class ELFT>





More information about the llvm-commits mailing list