Reverts wrong modification to MachineBlockPlacement & BranchFolding; uses a new strat...
[oota-llvm.git] / lib / Object / SymbolSize.cpp
index 121bf1baa7a1eaf35d6740e4551d51fc70dbef0c..1d5cd78e6d9cb587dbcb16d71fa563cf712abb8c 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,32 +21,38 @@ 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;
+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);
 }
 
-ErrorOr<std::vector<std::pair<SymbolRef, uint64_t>>>
+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;
 
-  if (isa<ELFObjectFileBase>(&O)) {
-    for (SymbolRef Sym : O.symbols()) {
+  if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
+    auto Syms = E->symbols();
+    if (Syms.begin() == Syms.end())
+      Syms = E->getDynamicSymbolIterators();
+    for (ELFSymbolRef Sym : Syms)
       Ret.push_back({Sym, Sym.getSize()});
-    }
     return Ret;
   }
 
@@ -54,19 +62,15 @@ llvm::object::computeSymbolSizes(const ObjectFile &O) {
   unsigned SymNum = 0;
   for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
     SymbolRef Sym = *I;
-    uint64_t Address;
-    if (std::error_code EC = Sym.getAddress(Address))
-      return EC;
-    section_iterator SecI = O.section_end();
-    if (std::error_code EC = Sym.getSection(SecI))
-      return EC;
-    Addresses.push_back({I, Address, SymNum, *SecI});
+    uint64_t Value = Sym.getValue();
+    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);
 
@@ -85,12 +89,12 @@ llvm::object::computeSymbolSizes(const ObjectFile &O) {
     P.Address = Size;
   }
 
-  // Put back in the original order and copy the result
-  array_pod_sort(Addresses.begin(), Addresses.end(), compareNumber);
+  // Assign the sorted symbols in the original order.
+  Ret.resize(SymNum);
   for (SymEntry &P : Addresses) {
     if (P.I == O.symbol_end())
       continue;
-    Ret.push_back({*P.I, P.Address});
+    Ret[P.Number] = {*P.I, P.Address};
   }
   return Ret;
 }