<p dir="ltr">This looks great. Thanks!</p>
<div class="gmail_extra"><br><div class="gmail_quote">On Nov 3, 2016 1:26 PM, "Rafael Espindola via llvm-commits" <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rafael<br>
Date: Thu Nov  3 15:16:53 2016<br>
New Revision: 285961<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=285961&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=285961&view=rev</a><br>
Log:<br>
Don't error in the ELFFile constructor.<br>
<br>
All error checking now happens when the information is needed. The<br>
only thing left is the minimum size of the buffer and that can be just<br>
a precondition. I will add an ErrorOr create method in a followup<br>
commit.<br>
<br>
Also don't store a pointer to the Header, since it is just a trivial<br>
cast.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/<wbr>Object/ELF.h<br>
    llvm/trunk/include/llvm/<wbr>Object/ELFObjectFile.h<br>
<br>
Modified: llvm/trunk/include/llvm/<wbr>Object/ELF.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=285961&r1=285960&r2=285961&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/include/<wbr>llvm/Object/ELF.h?rev=285961&<wbr>r1=285960&r2=285961&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/include/llvm/<wbr>Object/ELF.h (original)<br>
+++ llvm/trunk/include/llvm/<wbr>Object/ELF.h Thu Nov  3 15:16:53 2016<br>
@@ -69,9 +69,11 @@ private:<br>
<br>
   StringRef Buf;<br>
<br>
-  const Elf_Ehdr *Header;<br>
-<br>
 public:<br>
+  const Elf_Ehdr *getHeader() const {<br>
+    return reinterpret_cast<const Elf_Ehdr *>(base());<br>
+  }<br>
+<br>
   template <typename T><br>
   ErrorOr<const T *> getEntry(uint32_t Section, uint32_t Entry) const;<br>
   template <typename T><br>
@@ -96,17 +98,17 @@ public:<br>
   ErrorOr<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,<br>
                                                const Elf_Shdr *SymTab) const;<br>
<br>
-  ELFFile(StringRef Object, std::error_code &EC);<br>
+  ELFFile(StringRef Object);<br>
<br>
   bool isMipsELF64() const {<br>
-    return Header->e_machine == ELF::EM_MIPS &&<br>
-      Header->getFileClass() == ELF::ELFCLASS64;<br>
+    return getHeader()->e_machine == ELF::EM_MIPS &&<br>
+           getHeader()->getFileClass() == ELF::ELFCLASS64;<br>
   }<br>
<br>
   bool isMips64EL() const {<br>
-    return Header->e_machine == ELF::EM_MIPS &&<br>
-      Header->getFileClass() == ELF::ELFCLASS64 &&<br>
-      Header->getDataEncoding() == ELF::ELFDATA2LSB;<br>
+    return getHeader()->e_machine == ELF::EM_MIPS &&<br>
+           getHeader()->getFileClass() == ELF::ELFCLASS64 &&<br>
+           getHeader()->getDataEncoding() == ELF::ELFDATA2LSB;<br>
   }<br>
<br>
   ErrorOr<Elf_Shdr_Range> sections() const;<br>
@@ -127,14 +129,14 @@ public:<br>
<br>
   /// \brief Iterate over program header table.<br>
   ErrorOr<Elf_Phdr_Range> program_headers() const {<br>
-    if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))<br>
+    if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr))<br>
       return object_error::parse_failed;<br>
-    auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + Header->e_phoff);<br>
-    return makeArrayRef(Begin, Begin+Header->e_phnum);<br>
+    auto *Begin =<br>
+        reinterpret_cast<const Elf_Phdr *>(base() + getHeader()->e_phoff);<br>
+    return makeArrayRef(Begin, Begin + getHeader()->e_phnum);<br>
   }<br>
<br>
   ErrorOr<StringRef> getSectionStringTable(Elf_<wbr>Shdr_Range Sections) const;<br>
-  const Elf_Ehdr *getHeader() const { return Header; }<br>
   ErrorOr<uint32_t> getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,<br>
                                     ArrayRef<Elf_Word> ShndxTable) const;<br>
   ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *Sym,<br>
@@ -269,7 +271,7 @@ ELFFile<ELFT>::<wbr>getSectionContents(const<br>
<br>
 template <class ELFT><br>
 StringRef ELFFile<ELFT>::<wbr>getRelocationTypeName(uint32_t Type) const {<br>
-  return getELFRelocationTypeName(<wbr>Header->e_machine, Type);<br>
+  return getELFRelocationTypeName(<wbr>getHeader()->e_machine, Type);<br>
 }<br>
<br>
 template <class ELFT><br>
@@ -316,7 +318,7 @@ ELFFile<ELFT>::<wbr>getRelocationSymbol(const<br>
 template <class ELFT><br>
 ErrorOr<StringRef><br>
 ELFFile<ELFT>::<wbr>getSectionStringTable(Elf_<wbr>Shdr_Range Sections) const {<br>
-  uint32_t Index = Header->e_shstrndx;<br>
+  uint32_t Index = getHeader()->e_shstrndx;<br>
   if (Index == ELF::SHN_XINDEX)<br>
     Index = Sections[0].sh_link;<br>
<br>
@@ -328,26 +330,8 @@ ELFFile<ELFT>::<wbr>getSectionStringTable(Elf<br>
 }<br>
<br>
 template <class ELFT><br>
-ELFFile<ELFT>::ELFFile(<wbr>StringRef Object, std::error_code &EC) : Buf(Object) {<br>
-  const uint64_t FileSize = Buf.size();<br>
-<br>
-  if (sizeof(Elf_Ehdr) > FileSize) {<br>
-    // File too short!<br>
-    EC = object_error::parse_failed;<br>
-    return;<br>
-  }<br>
-<br>
-  Header = reinterpret_cast<const Elf_Ehdr *>(base());<br>
-<br>
-  if (Header->e_shoff == 0) {<br>
-    if (Header->e_shnum != 0) {<br>
-      // e_shnum should be zero if a file has no section header table<br>
-      EC = object_error::parse_failed;<br>
-    }<br>
-    return;<br>
-  }<br>
-<br>
-  EC = std::error_code();<br>
+ELFFile<ELFT>::ELFFile(<wbr>StringRef Object) : Buf(Object) {<br>
+  assert(sizeof(Elf_Ehdr) <= Buf.size() && "Invalid buffer");<br>
 }<br>
<br>
 template <class ELFT><br>
@@ -357,12 +341,12 @@ static bool compareAddr(uint64_t VAddr,<br>
<br>
 template <class ELFT><br>
 ErrorOr<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {<br>
-  const uintX_t SectionTableOffset = Header->e_shoff;<br>
+  const uintX_t SectionTableOffset = getHeader()->e_shoff;<br>
   if (SectionTableOffset == 0)<br>
     return ArrayRef<Elf_Shdr>();<br>
<br>
   // Invalid section header entry size (e_shentsize) in ELF header<br>
-  if (Header->e_shentsize != sizeof(Elf_Shdr))<br>
+  if (getHeader()->e_shentsize != sizeof(Elf_Shdr))<br>
     return object_error::parse_failed;<br>
<br>
   const uint64_t FileSize = Buf.size();<br>
@@ -378,7 +362,7 @@ ErrorOr<typename ELFT::ShdrRange> ELFFil<br>
   const Elf_Shdr *First =<br>
       reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);<br>
<br>
-  uintX_t NumSections = Header->e_shnum;<br>
+  uintX_t NumSections = getHeader()->e_shnum;<br>
   if (NumSections == 0)<br>
     NumSections = First->sh_size;<br>
<br>
<br>
Modified: llvm/trunk/include/llvm/<wbr>Object/ELFObjectFile.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=285961&r1=285960&r2=285961&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/include/<wbr>llvm/Object/ELFObjectFile.h?<wbr>rev=285961&r1=285960&r2=<wbr>285961&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/include/llvm/<wbr>Object/ELFObjectFile.h (original)<br>
+++ llvm/trunk/include/llvm/<wbr>Object/ELFObjectFile.h Thu Nov  3 15:16:53 2016<br>
@@ -773,9 +773,7 @@ ELFObjectFile<ELFT>::<wbr>ELFObjectFile(Memor<br>
     : ELFObjectFileBase(<br>
           getELFType(ELFT::<wbr>TargetEndianness == support::little, ELFT::Is64Bits),<br>
           Object),<br>
-      EF(Data.getBuffer(), EC) {<br>
-  if (EC)<br>
-    return;<br>
+      EF(Data.getBuffer()) {<br>
   auto SectionsOrErr = EF.sections();<br>
   if ((EC = SectionsOrErr.getError()))<br>
     return;<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>