LLVM  8.0.1
SymbolSize.cpp
Go to the documentation of this file.
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"
14 #include "llvm/Object/MachO.h"
15 
16 using namespace llvm;
17 using namespace object;
18 
19 // Orders increasingly by (SectionID, Address).
21  if (A->SectionID != B->SectionID)
22  return A->SectionID < B->SectionID ? -1 : 1;
23  if (A->Address != B->Address)
24  return A->Address < B->Address ? -1 : 1;
25  return 0;
26 }
27 
28 static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
29  if (auto *M = dyn_cast<MachOObjectFile>(&O))
30  return M->getSectionID(Sec);
31  return cast<COFFObjectFile>(O).getSectionID(Sec);
32 }
33 
34 static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
35  if (auto *M = dyn_cast<MachOObjectFile>(&O))
36  return M->getSymbolSectionID(Sym);
37  return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
38 }
39 
40 std::vector<std::pair<SymbolRef, uint64_t>>
42  std::vector<std::pair<SymbolRef, uint64_t>> Ret;
43 
44  if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
45  auto Syms = E->symbols();
46  if (Syms.begin() == Syms.end())
47  Syms = E->getDynamicSymbolIterators();
48  for (ELFSymbolRef Sym : Syms)
49  Ret.push_back({Sym, Sym.getSize()});
50  return Ret;
51  }
52 
53  // Collect sorted symbol addresses. Include dummy addresses for the end
54  // of each section.
55  std::vector<SymEntry> Addresses;
56  unsigned SymNum = 0;
57  for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
58  SymbolRef Sym = *I;
59  uint64_t Value = Sym.getValue();
60  Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)});
61  ++SymNum;
62  }
63  for (SectionRef Sec : O.sections()) {
64  uint64_t Address = Sec.getAddress();
65  uint64_t Size = Sec.getSize();
66  Addresses.push_back(
67  {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
68  }
69 
70  if (Addresses.empty())
71  return Ret;
72 
73  array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
74 
75  // Compute the size as the gap to the next symbol
76  for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) {
77  auto &P = Addresses[I];
78  if (P.I == O.symbol_end())
79  continue;
80 
81  // If multiple symbol have the same address, give both the same size.
82  unsigned NextI = I + 1;
83  while (NextI < N && Addresses[NextI].Address == P.Address)
84  ++NextI;
85 
86  uint64_t Size = Addresses[NextI].Address - P.Address;
87  P.Address = Size;
88  }
89 
90  // Assign the sorted symbols in the original order.
91  Ret.resize(SymNum);
92  for (SymEntry &P : Addresses) {
93  if (P.I == O.symbol_end())
94  continue;
95  Ret[P.Number] = {*P.I, P.Address};
96  }
97  return Ret;
98 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
uint64_t getValue() const
Return the value of the symbol depending on the object this can be an offset or a virtual address...
Definition: ObjectFile.h:367
This class is the base class for all object file types.
Definition: ObjectFile.h:202
int compareAddress(const SymEntry *A, const SymEntry *B)
Definition: SymbolSize.cpp:20
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
Definition: SymbolSize.cpp:34
virtual basic_symbol_iterator symbol_begin() const =0
section_iterator_range sections() const
Definition: ObjectFile.h:292
#define P(N)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1083
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual basic_symbol_iterator symbol_end() const =0
static unsigned getSectionID(const ObjectFile &O, SectionRef Sec)
Definition: SymbolSize.cpp:28
std::vector< std::pair< SymbolRef, uint64_t > > computeSymbolSizes(const ObjectFile &O)
Definition: SymbolSize.cpp:41
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:141
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
uint32_t Size
Definition: Profile.cpp:47
LLVM Value Representation.
Definition: Value.h:73
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81