From 6bf322101bebac03b63fb068fcd3158f795bcee3 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 24 Jun 2015 19:57:32 +0000 Subject: Make computeSymbolSizes never fail. 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. llvm-svn: 240580 --- llvm/lib/Object/COFFObjectFile.cpp | 12 ++++++++++++ llvm/lib/Object/MachOObjectFile.cpp | 10 ++++++++++ llvm/lib/Object/SymbolSize.cpp | 38 ++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 15 deletions(-) (limited to 'llvm/lib/Object') diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index 07f9d6ed232..797ee1a7cda 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/lib/Object/COFFObjectFile.cpp @@ -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 diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index cb98f05bfcc..62a0d60f338 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -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; diff --git a/llvm/lib/Object/SymbolSize.cpp b/llvm/lib/Object/SymbolSize.cpp index 2da71f1fa24..276deaaddd7 100644 --- a/llvm/lib/Object/SymbolSize.cpp +++ b/llvm/lib/Object/SymbolSize.cpp @@ -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>> +static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) { + if (auto *M = dyn_cast(&O)) + return M->getSectionID(Sec); + return cast(O).getSectionID(Sec); +} + +static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) { + if (auto *M = dyn_cast(&O)) + return M->getSymbolSectionID(Sym); + return cast(O).getSymbolSectionID(Sym); +} + +std::vector> llvm::object::computeSymbolSizes(const ObjectFile &O) { std::vector> 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); -- cgit v1.2.3