]> granicus.if.org Git - llvm/commitdiff
Thread Expected<...> up from libObject’s getSymbolAddress() for symbols to allow
authorKevin Enderby <enderby@apple.com>
Fri, 24 Jun 2016 18:24:42 +0000 (18:24 +0000)
committerKevin Enderby <enderby@apple.com>
Fri, 24 Jun 2016 18:24:42 +0000 (18:24 +0000)
a good error message to be produced.

This is nearly the last libObject interface that used ErrorOr and the last one
that appears in llvm/include/llvm/Object/MachO.h .  For Mach-O objects this is
just a clean up because it’s version of getSymbolAddress() can’t return an
error.

I will leave it to the experts on COFF and ELF to actually add meaning full
error messages in their tests if they wish.  And also leave it to these experts
to change the last two ErrorOr interfaces in llvm/include/llvm/Object/ObjectFile.h
for createCOFFObjectFile() and createELFObjectFile() if they wish.

Since there are no test cases for COFF and ELF error cases with respect to
getSymbolAddress() in the test suite this is no functional change (NFC).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273701 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
include/llvm/Object/COFF.h
include/llvm/Object/ELFObjectFile.h
include/llvm/Object/MachO.h
include/llvm/Object/ObjectFile.h
lib/DebugInfo/DWARF/DWARFContext.cpp
lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
lib/Object/COFFObjectFile.cpp
lib/Object/MachOObjectFile.cpp
lib/Object/Object.cpp
tools/llvm-cxxdump/llvm-cxxdump.cpp
tools/llvm-nm/llvm-nm.cpp
tools/llvm-objdump/COFFDump.cpp
tools/llvm-objdump/llvm-objdump.cpp
tools/llvm-readobj/ARMWinEHPrinter.cpp
tools/llvm-readobj/Win64EHDumper.cpp
tools/llvm-rtdyld/llvm-rtdyld.cpp
tools/sancov/sancov.cc

index ebf9bb82f387af709c5b27e56b6ff3e38c3cc69d..a87f7651a2ec5f4522fb73fd009ad9f319e4a4c9 100644 (file)
@@ -722,7 +722,7 @@ public:
 protected:
   void moveSymbolNext(DataRefImpl &Symb) const override;
   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
-  ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
+  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
index f93008810a6a2d8d5d87ac8e48fe14489e8d8b69..6dcd2d486813a1ac3159a8371360cd2fc5ef5145 100644 (file)
@@ -207,7 +207,7 @@ protected:
 
   void moveSymbolNext(DataRefImpl &Symb) const override;
   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
-  ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
+  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
@@ -397,7 +397,7 @@ uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
 }
 
 template <class ELFT>
-ErrorOr<uint64_t>
+Expected<uint64_t>
 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
   uint64_t Result = getSymbolValue(Symb);
   const Elf_Sym *ESym = getSymbol(Symb);
@@ -415,7 +415,7 @@ ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
     ErrorOr<const Elf_Shdr *> SectionOrErr =
         EF.getSection(ESym, SymTab, ShndxTable);
     if (std::error_code EC = SectionOrErr.getError())
-      return EC;
+      return errorCodeToError(EC);
     const Elf_Shdr *Section = *SectionOrErr;
     if (Section)
       Result += Section->sh_addr;
index 53915a9f0b553bcfda9ddc886a4a1e91f522f14d..7906db1e8a77df2244836fceec40967d8c4ff1c9 100644 (file)
@@ -205,7 +205,7 @@ public:
   std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
   unsigned getSectionType(SectionRef Sec) const;
 
-  ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
+  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
   Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
index 838495657b9231db82ccc9e5258604e8debfbd71..6272a5f056eb8c8c267767a401a763e59fc35ecd 100644 (file)
@@ -135,7 +135,7 @@ public:
   Expected<StringRef> getName() const;
   /// Returns the symbol virtual address (i.e. address at which it will be
   /// mapped).
-  ErrorOr<uint64_t> getAddress() const;
+  Expected<uint64_t> getAddress() const;
 
   /// Return the value of the symbol depending on the object this can be an
   /// offset or a virtual address.
@@ -198,7 +198,7 @@ protected:
   virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
   std::error_code printSymbolName(raw_ostream &OS,
                                   DataRefImpl Symb) const override;
-  virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
+  virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
   virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
   virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
   virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
@@ -312,7 +312,7 @@ inline Expected<StringRef> SymbolRef::getName() const {
   return getObject()->getSymbolName(getRawDataRefImpl());
 }
 
-inline ErrorOr<uint64_t> SymbolRef::getAddress() const {
+inline Expected<uint64_t> SymbolRef::getAddress() const {
   return getObject()->getSymbolAddress(getRawDataRefImpl());
 }
 
index 27b8c564d910545ed1b7996fa063bb41209a613d..3f03e63ce03ab0b714ff21f09316e903b6c32a6a 100644 (file)
@@ -793,10 +793,14 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
         // First calculate the address of the symbol or section as it appears
         // in the objct file
         if (Sym != Obj.symbol_end()) {
-          ErrorOr<uint64_t> SymAddrOrErr = Sym->getAddress();
-          if (std::error_code EC = SymAddrOrErr.getError()) {
+          Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
+          if (!SymAddrOrErr) {
+            std::string Buf;
+            raw_string_ostream OS(Buf);
+            logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
+            OS.flush();
             errs() << "error: failed to compute symbol address: "
-                   << EC.message() << '\n';
+                   << Buf << '\n';
             continue;
           }
           SymAddr = *SymAddrOrErr;
index a66722cb2c5e2e950f7afca0401ac07e8512d30e..9661b4d85e70895a67e158f84fbf643ae59a54e8 100644 (file)
@@ -125,9 +125,9 @@ std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
     return std::error_code();
-  ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
-  if (auto EC = SymbolAddressOrErr.getError())
-    return EC;
+  Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
+  if (!SymbolAddressOrErr)
+    return errorToErrorCode(SymbolAddressOrErr.takeError());
   uint64_t SymbolAddress = *SymbolAddressOrErr;
   if (OpdExtractor) {
     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
index 4a282cb66e5534bc4b3916c9442c38a15ec06eeb..dcbcec8c1d9f5616e93c715ff7bcd7b19315e95b 100644 (file)
@@ -163,9 +163,9 @@ void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
 
 static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
                        uint64_t &Result) {
-  ErrorOr<uint64_t> AddressOrErr = Sym.getAddress();
-  if (std::error_code EC = AddressOrErr.getError())
-    return errorCodeToError(EC);
+  Expected<uint64_t> AddressOrErr = Sym.getAddress();
+  if (!AddressOrErr)
+    return AddressOrErr.takeError();
   Result = *AddressOrErr - Sec.getAddress();
   return Error::success();
 }
@@ -236,7 +236,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
         if (auto AddrOrErr = I->getAddress())
           Addr = *AddrOrErr;
         else
-          return errorCodeToError(AddrOrErr.getError());
+          return AddrOrErr.takeError();
 
         unsigned SectionID = AbsoluteSymbolSection;
 
index 28627cd1d42a8f78087266cca9f9988c1959f185..878b93fcda0d17a41d1fa3570ffd2dbfa37e12ba 100644 (file)
@@ -157,7 +157,7 @@ uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
   return getCOFFSymbol(Ref).getValue();
 }
 
-ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
+Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
   uint64_t Result = getSymbolValue(Ref);
   COFFSymbolRef Symb = getCOFFSymbol(Ref);
   int32_t SectionNumber = Symb.getSectionNumber();
@@ -168,7 +168,7 @@ ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
 
   const coff_section *Section = nullptr;
   if (std::error_code EC = getSection(SectionNumber, Section))
-    return EC;
+    return errorCodeToError(EC);
   Result += Section->VirtualAddress;
 
   // The section VirtualAddress does not include ImageBase, and we want to
index d7ec3e57a58be93946af5f8e3bf9fa249d047f7d..3c88f9fb38241185d703a71c35d65c1b6fe4d3ea 100644 (file)
@@ -473,7 +473,7 @@ uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
   return getNValue(Sym);
 }
 
-ErrorOr<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
+Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
   return getSymbolValue(Sym);
 }
 
index 6777b322da39ab29852ff4b80dbf9b9b60d2584f..6df481b060e100c5f5f54058dd2df15b227fd808 100644 (file)
@@ -195,9 +195,14 @@ const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
 }
 
 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
-  ErrorOr<uint64_t> Ret = (*unwrap(SI))->getAddress();
-  if (std::error_code EC = Ret.getError())
-    report_fatal_error(EC.message());
+  Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
+  if (!Ret) {
+    std::string Buf;
+    raw_string_ostream OS(Buf);
+    logAllUnhandledErrors(Ret.takeError(), OS, "");
+    OS.flush();
+    report_fatal_error(Buf);
+  }
   return *Ret;
 }
 
index 16ea629260d96b8a5fd8809738c4f855b8ef0a21..0c408ccb5baac7c28a36a337fe0355c356c4cb20 100644 (file)
@@ -190,8 +190,8 @@ static void dumpCXXData(const ObjectFile *Obj) {
       continue;
     StringRef SecContents;
     error(Sec.getContents(SecContents));
-    ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
-    error(SymAddressOrErr.getError());
+    Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
+    error(errorToErrorCode(SymAddressOrErr.takeError()));
     uint64_t SymAddress = *SymAddressOrErr;
     uint64_t SecAddress = Sec.getAddress();
     uint64_t SecSize = Sec.getSize();
index f31c318081114ec5423dd9aee47d1c33d928033b..9661f8e2020439a211b4d5477ae8db391006d66a 100644 (file)
@@ -1015,9 +1015,11 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
     }
     if (PrintAddress && isa<ObjectFile>(Obj)) {
       SymbolRef SymRef(Sym);
-      ErrorOr<uint64_t> AddressOrErr = SymRef.getAddress();
-      if (error(AddressOrErr.getError()))
+      Expected<uint64_t> AddressOrErr = SymRef.getAddress();
+      if (!AddressOrErr) {
+        consumeError(AddressOrErr.takeError());
         break;
+      }
       S.Address = *AddressOrErr;
     }
     S.TypeChar = getNMTypeChar(Obj, Sym);
index 62c9edcbe933715389f1e5d25c8dc18a6e7cc903..4f1b9b11913e9e02a35c07549f5c3256f86fd92a 100644 (file)
@@ -161,9 +161,9 @@ static std::error_code
 resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
                          const coff_section *&ResolvedSection,
                          uint64_t &ResolvedAddr) {
-  ErrorOr<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
-  if (std::error_code EC = ResolvedAddrOrErr.getError())
-    return EC;
+  Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
+  if (!ResolvedAddrOrErr)
+    return errorToErrorCode(ResolvedAddrOrErr.takeError());
   ResolvedAddr = *ResolvedAddrOrErr;
   Expected<section_iterator> Iter = Sym.getSection();
   if (!Iter)
index 3beb01ad6eb4a541ab6fd034e1d299c48dbeac60..3c536af5713952482877a29f55f5be022b7f8572 100644 (file)
@@ -649,9 +649,14 @@ static void printRelocationTargetName(const MachOObjectFile *O,
 
     for (const SymbolRef &Symbol : O->symbols()) {
       std::error_code ec;
-      ErrorOr<uint64_t> Addr = Symbol.getAddress();
-      if ((ec = Addr.getError()))
-        report_fatal_error(ec.message());
+      Expected<uint64_t> Addr = Symbol.getAddress();
+      if (!Addr) {
+        std::string Buf;
+        raw_string_ostream OS(Buf);
+        logAllUnhandledErrors(Addr.takeError(), OS, "");
+        OS.flush();
+        report_fatal_error(Buf);
+      }
       if (*Addr != Val)
         continue;
       Expected<StringRef> Name = Symbol.getName();
@@ -992,8 +997,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
   typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
   for (const SymbolRef &Symbol : Obj->symbols()) {
-    ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
-    error(AddressOrErr.getError());
+    Expected<uint64_t> AddressOrErr = Symbol.getAddress();
+    error(errorToErrorCode(AddressOrErr.takeError()));
     uint64_t Address = *AddressOrErr;
 
     Expected<StringRef> Name = Symbol.getName();
@@ -1389,8 +1394,9 @@ void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
     return;
   }
   for (const SymbolRef &Symbol : o->symbols()) {
-    ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
-    error(AddressOrError.getError());
+    Expected<uint64_t> AddressOrError = Symbol.getAddress();
+    if (!AddressOrError)
+      report_error(ArchiveName, o->getFileName(), AddressOrError.takeError());
     uint64_t Address = *AddressOrError;
     Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
     if (!TypeOrError)
index 9e14e69015d12fd7fcfdf983d908149db23f5276..1a033b1eb42e84f31cc166d8a13f5ee1b8207d5c 100644 (file)
@@ -204,9 +204,9 @@ ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
     if (FunctionOnly && *Type != SymbolRef::ST_Function)
       continue;
 
-    ErrorOr<uint64_t> Address = Symbol.getAddress();
-    if (std::error_code EC = Address.getError())
-      return EC;
+    Expected<uint64_t> Address = Symbol.getAddress();
+    if (!Address)
+      return errorToErrorCode(Address.takeError());
     if (*Address == VA)
       return Symbol;
   }
@@ -618,9 +618,14 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
       report_fatal_error(Buf);
     }
     FunctionName = *FunctionNameOrErr;
-    ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
-    if (std::error_code EC = FunctionAddressOrErr.getError())
-      report_fatal_error(EC.message());
+    Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
+    if (!FunctionAddressOrErr) {
+      std::string Buf;
+      llvm::raw_string_ostream OS(Buf);
+      logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
+      OS.flush();
+      report_fatal_error(Buf);
+    }
     FunctionAddress = *FunctionAddressOrErr;
   } else {
     const pe32_header *PEHeader;
@@ -641,9 +646,14 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
       report_fatal_error(Buf);
     }
 
-    ErrorOr<uint64_t> AddressOrErr = XDataRecord->getAddress();
-    if (std::error_code EC = AddressOrErr.getError())
-      report_fatal_error(EC.message());
+    Expected<uint64_t> AddressOrErr = XDataRecord->getAddress();
+    if (!AddressOrErr) {
+      std::string Buf;
+      llvm::raw_string_ostream OS(Buf);
+      logAllUnhandledErrors(AddressOrErr.takeError(), OS, "");
+      OS.flush();
+      report_fatal_error(Buf);
+    }
     uint64_t Address = *AddressOrErr;
 
     SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
@@ -698,7 +708,14 @@ bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
       report_fatal_error(Buf);
     }
     FunctionName = *FunctionNameOrErr;
-    ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
+    Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
+    if (!FunctionAddressOrErr) {
+      std::string Buf;
+      llvm::raw_string_ostream OS(Buf);
+      logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
+      OS.flush();
+      report_fatal_error(Buf);
+    }
     FunctionAddress = *FunctionAddressOrErr;
   } else {
     const pe32_header *PEHeader;
index f797d4baa4050cef393d3bf433fcb110efe88f01..f7e56b3615425c8abe6b685fe7bf388f79ff9e9b 100644 (file)
@@ -148,9 +148,9 @@ static std::error_code resolveRelocation(const Dumper::Context &Ctx,
           Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
     return EC;
 
-  ErrorOr<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
-  if (std::error_code EC = ResolvedAddressOrErr.getError())
-    return EC;
+  Expected<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
+  if (!ResolvedAddressOrErr)
+    return errorToErrorCode(ResolvedAddressOrErr.takeError());
   ResolvedAddress = *ResolvedAddressOrErr;
 
   Expected<section_iterator> SI = Symbol.getSection();
index 249dbd28674647a5f464a06a692da923b1a2c581..b1460e35de80fbdb7735121f85e88242306f67a2 100644 (file)
@@ -349,9 +349,12 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
           consumeError(Name.takeError());
           continue;
         }
-        ErrorOr<uint64_t> AddrOrErr = Sym.getAddress();
-        if (!AddrOrErr)
+        Expected<uint64_t> AddrOrErr = Sym.getAddress();
+        if (!AddrOrErr) {
+          // TODO: Actually report errors helpfully.
+          consumeError(AddrOrErr.takeError());
           continue;
+        }
         uint64_t Addr = *AddrOrErr;
 
         uint64_t Size = P.second;
index 4f8df41f2f181fc5e975435803f21299f75a662e..8423d29e751e9f8aa7b1402b667e692d86c3b129 100644 (file)
@@ -321,8 +321,8 @@ findSanitizerCovFunctions(const object::ObjectFile &O) {
   std::set<uint64_t> Result;
 
   for (const object::SymbolRef &Symbol : O.symbols()) {
-    ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
-    FailIfError(AddressOrErr);
+    Expected<uint64_t> AddressOrErr = Symbol.getAddress();
+    FailIfError(errorToErrorCode(AddressOrErr.takeError()));
 
     Expected<StringRef> NameOrErr = Symbol.getName();
     FailIfError(errorToErrorCode(NameOrErr.takeError()));