Make computeSymbolSizes never fail.
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 24 Jun 2015 19:57:32 +0000 (19:57 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 24 Jun 2015 19:57:32 +0000 (19:57 +0000)
On ELF that was already the case since getting the size of a symbol
never fails.

On MachO and COFF we could fail trying to get the section of a symbol. But
we don't really need the section, just the section number to know if two
symbols are in the same section or not.

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

include/llvm/Object/COFF.h
include/llvm/Object/MachO.h
include/llvm/Object/SymbolSize.h
lib/Object/COFFObjectFile.cpp
lib/Object/MachOObjectFile.cpp
lib/Object/SymbolSize.cpp
tools/llvm-cxxdump/llvm-cxxdump.cpp
tools/llvm-rtdyld/llvm-rtdyld.cpp

index 3432f630dea2432ac966c0aa3f16d2ef9c8ade69..6755ae0333ba4cf62429e7b5edfb530ac5c7c35b 100644 (file)
@@ -681,6 +681,8 @@ public:
   COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
   COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
   const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
+  unsigned getSectionID(SectionRef Sec) const;
+  unsigned getSymbolSectionID(SymbolRef Sym) const;
 
   uint8_t getBytesInAddress() const override;
   StringRef getFileFormatName() const override;
index a297969944750c962740e342b44d1cb4730293b5..ea1b63132127786ddffb42908fa8549d45ea0b98 100644 (file)
@@ -216,6 +216,8 @@ public:
   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
   std::error_code getSymbolSection(DataRefImpl Symb,
                                    section_iterator &Res) const override;
+  unsigned getSymbolSectionID(SymbolRef Symb) const;
+  unsigned getSectionID(SectionRef Sec) const;
 
   void moveSectionNext(DataRefImpl &Sec) const override;
   std::error_code getSectionName(DataRefImpl Sec,
index b461c94c44b24c1491c94afd2f46774461103a37..f2ce70f4208def7ba838460ea3a2c1afb37eaaf0 100644 (file)
@@ -15,7 +15,7 @@
 
 namespace llvm {
 namespace object {
-ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>>
+std::vector<std::pair<SymbolRef, uint64_t>>
 computeSymbolSizes(const ObjectFile &O);
 }
 } // namespace llvm
index 07f9d6ed2321b18191f548d6cc917e6d926f92f7..797ee1a7cdafe9f9a84a5ba42672b2d888e768b7 100644 (file)
@@ -258,6 +258,11 @@ COFFObjectFile::getSymbolSection(DataRefImpl Ref,
   return std::error_code();
 }
 
+unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
+  COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
+  return Symb.getSectionNumber();
+}
+
 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
   const coff_section *Sec = toSec(Ref);
   Sec += 1;
@@ -311,6 +316,13 @@ bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
   return (Sec->Characteristics & BssFlags) == BssFlags;
 }
 
+unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
+  uintptr_t Offset =
+      uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
+  assert((Offset % sizeof(coff_section)) == 0);
+  return (Offset / sizeof(coff_section)) + 1;
+}
+
 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
   const coff_section *Sec = toSec(Ref);
   // In COFF, a virtual section won't have any in-file 
index cb98f05bfccf1e4893bc7db10b9c7cd44efbc089..62a0d60f3384a0a36dc9351ce327dadbb9047b6e 100644 (file)
@@ -483,6 +483,12 @@ std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
   return std::error_code();
 }
 
+unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
+  MachO::nlist_base Entry =
+      getSymbolTableEntryBase(this, Sym.getRawDataRefImpl());
+  return Entry.n_sect - 1;
+}
+
 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
   Sec.d.a++;
 }
@@ -559,6 +565,10 @@ bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
           SectionType == MachO::S_GB_ZEROFILL);
 }
 
+unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
+  return Sec.getRawDataRefImpl().d.a;
+}
+
 bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
   // FIXME: Unimplemented.
   return false;
index 2da71f1fa24a553f3fd1b9e805f52967c9897900..276deaaddd7f353a4c0927b1eb0b974e642fea66 100644 (file)
@@ -9,7 +9,9 @@
 
 #include "llvm/Object/SymbolSize.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Object/COFF.h"
 #include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/MachO.h"
 
 using namespace llvm;
 using namespace object;
@@ -19,25 +21,33 @@ struct SymEntry {
   symbol_iterator I;
   uint64_t Address;
   unsigned Number;
-  SectionRef Section;
+  unsigned SectionID;
 };
 }
 
 static int compareAddress(const SymEntry *A, const SymEntry *B) {
-  if (A->Section == B->Section)
-    return A->Address - B->Address;
-  if (A->Section < B->Section)
-    return -1;
-  if (A->Section == B->Section)
-    return 0;
-  return 1;
+  if (A->SectionID != B->SectionID)
+    return A->SectionID - B->SectionID;
+  return A->Address - B->Address;
 }
 
 static int compareNumber(const SymEntry *A, const SymEntry *B) {
   return A->Number - B->Number;
 }
 
-ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>>
+static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
+  if (auto *M = dyn_cast<MachOObjectFile>(&O))
+    return M->getSectionID(Sec);
+  return cast<COFFObjectFile>(O).getSectionID(Sec);
+}
+
+static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
+  if (auto *M = dyn_cast<MachOObjectFile>(&O))
+    return M->getSymbolSectionID(Sym);
+  return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
+}
+
+std::vector<std::pair<SymbolRef, uint64_t>>
 llvm::object::computeSymbolSizes(const ObjectFile &O) {
   std::vector<std::pair<SymbolRef, uint64_t>> Ret;
 
@@ -54,16 +64,14 @@ llvm::object::computeSymbolSizes(const ObjectFile &O) {
   for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
     SymbolRef Sym = *I;
     uint64_t Value = Sym.getValue();
-    section_iterator SecI = O.section_end();
-    if (std::error_code EC = Sym.getSection(SecI))
-      return EC;
-    Addresses.push_back({I, Value, SymNum, *SecI});
+    Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)});
     ++SymNum;
   }
-  for (const SectionRef Sec : O.sections()) {
+  for (SectionRef Sec : O.sections()) {
     uint64_t Address = Sec.getAddress();
     uint64_t Size = Sec.getSize();
-    Addresses.push_back({O.symbol_end(), Address + Size, 0, Sec});
+    Addresses.push_back(
+        {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
   }
   array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
 
index bd9cf7d5df2533b4d82c6803c5181272181e69fa..e4d32abb95c9a3c24f40cb4200abdcaa067bfa8f 100644 (file)
@@ -188,12 +188,10 @@ static void dumpCXXData(const ObjectFile *Obj) {
 
   uint8_t BytesInAddress = Obj->getBytesInAddress();
 
-  ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>> SymAddrOrErr =
+  std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
       object::computeSymbolSizes(*Obj);
-  if (error(SymAddrOrErr.getError()))
-    return;
 
-  for (auto &P : *SymAddrOrErr) {
+  for (auto &P : SymAddr) {
     object::SymbolRef Sym = P.first;
     uint64_t SymSize = P.second;
     StringRef SymName;
index addea5884726ed499c1bad80388716d4bf983162..65033a2e0cc6678ac94bb46df36cd0c70655a27e 100644 (file)
@@ -259,13 +259,11 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
     std::unique_ptr<DIContext> Context(
       new DWARFContextInMemory(*SymbolObj,LoadedObjInfo.get()));
 
-    ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>> SymAddrOrErr =
+    std::vector<std::pair<SymbolRef, uint64_t>> SymAddr =
         object::computeSymbolSizes(*SymbolObj);
-    if (std::error_code EC = SymAddrOrErr.getError())
-      return Error(EC.message());
 
     // Use symbol info to iterate functions in the object.
-    for (const auto &P : *SymAddrOrErr) {
+    for (const auto &P : SymAddr) {
       object::SymbolRef Sym = P.first;
       object::SymbolRef::Type SymType;
       if (Sym.getType(SymType))