Use computeSymbolSizes in llvm-symbolize.
[oota-llvm.git] / lib / Object / SymbolSize.cpp
1 //===- SymbolSize.cpp -----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Object/SymbolSize.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Object/COFF.h"
13 #include "llvm/Object/ELFObjectFile.h"
14 #include "llvm/Object/MachO.h"
15
16 using namespace llvm;
17 using namespace object;
18
19 namespace {
20 struct SymEntry {
21   symbol_iterator I;
22   uint64_t Address;
23   unsigned Number;
24   unsigned SectionID;
25 };
26 }
27
28 static int compareAddress(const SymEntry *A, const SymEntry *B) {
29   if (A->SectionID != B->SectionID)
30     return A->SectionID - B->SectionID;
31   return A->Address - B->Address;
32 }
33
34 static int compareNumber(const SymEntry *A, const SymEntry *B) {
35   return A->Number - B->Number;
36 }
37
38 static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
39   if (auto *M = dyn_cast<MachOObjectFile>(&O))
40     return M->getSectionID(Sec);
41   return cast<COFFObjectFile>(O).getSectionID(Sec);
42 }
43
44 static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
45   if (auto *M = dyn_cast<MachOObjectFile>(&O))
46     return M->getSymbolSectionID(Sym);
47   return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
48 }
49
50 std::vector<std::pair<SymbolRef, uint64_t>>
51 llvm::object::computeSymbolSizes(const ObjectFile &O) {
52   std::vector<std::pair<SymbolRef, uint64_t>> Ret;
53
54   if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
55     auto Syms = E->symbols();
56     if (Syms.begin() == Syms.end())
57       Syms = E->getDynamicSymbolIterators();
58     for (SymbolRef Sym : Syms)
59       Ret.push_back({Sym, E->getSymbolSize(Sym)});
60     return Ret;
61   }
62
63   // Collect sorted symbol addresses. Include dummy addresses for the end
64   // of each section.
65   std::vector<SymEntry> Addresses;
66   unsigned SymNum = 0;
67   for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
68     SymbolRef Sym = *I;
69     uint64_t Value = Sym.getValue();
70     Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)});
71     ++SymNum;
72   }
73   for (SectionRef Sec : O.sections()) {
74     uint64_t Address = Sec.getAddress();
75     uint64_t Size = Sec.getSize();
76     Addresses.push_back(
77         {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
78   }
79   array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
80
81   // Compute the size as the gap to the next symbol
82   for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) {
83     auto &P = Addresses[I];
84     if (P.I == O.symbol_end())
85       continue;
86
87     // If multiple symbol have the same address, give both the same size.
88     unsigned NextI = I + 1;
89     while (NextI < N && Addresses[NextI].Address == P.Address)
90       ++NextI;
91
92     uint64_t Size = Addresses[NextI].Address - P.Address;
93     P.Address = Size;
94   }
95
96   // Put back in the original order and copy the result
97   array_pod_sort(Addresses.begin(), Addresses.end(), compareNumber);
98   for (SymEntry &P : Addresses) {
99     if (P.I == O.symbol_end())
100       continue;
101     Ret.push_back({*P.I, P.Address});
102   }
103   return Ret;
104 }