LLVM  9.0.0svn
SymbolSize.cpp
Go to the documentation of this file.
1 //===- SymbolSize.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Object/COFF.h"
13 #include "llvm/Object/MachO.h"
14 
15 using namespace llvm;
16 using namespace object;
17 
18 // Orders increasingly by (SectionID, Address).
20  if (A->SectionID != B->SectionID)
21  return A->SectionID < B->SectionID ? -1 : 1;
22  if (A->Address != B->Address)
23  return A->Address < B->Address ? -1 : 1;
24  return 0;
25 }
26 
27 static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
28  if (auto *M = dyn_cast<MachOObjectFile>(&O))
29  return M->getSectionID(Sec);
30  return cast<COFFObjectFile>(O).getSectionID(Sec);
31 }
32 
33 static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
34  if (auto *M = dyn_cast<MachOObjectFile>(&O))
35  return M->getSymbolSectionID(Sym);
36  return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
37 }
38 
39 std::vector<std::pair<SymbolRef, uint64_t>>
41  std::vector<std::pair<SymbolRef, uint64_t>> Ret;
42 
43  if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
44  auto Syms = E->symbols();
45  if (Syms.begin() == Syms.end())
46  Syms = E->getDynamicSymbolIterators();
47  for (ELFSymbolRef Sym : Syms)
48  Ret.push_back({Sym, Sym.getSize()});
49  return Ret;
50  }
51 
52  // Collect sorted symbol addresses. Include dummy addresses for the end
53  // of each section.
54  std::vector<SymEntry> Addresses;
55  unsigned SymNum = 0;
56  for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
57  SymbolRef Sym = *I;
58  uint64_t Value = Sym.getValue();
59  Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)});
60  ++SymNum;
61  }
62  for (SectionRef Sec : O.sections()) {
63  uint64_t Address = Sec.getAddress();
64  uint64_t Size = Sec.getSize();
65  Addresses.push_back(
66  {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
67  }
68 
69  if (Addresses.empty())
70  return Ret;
71 
72  array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
73 
74  // Compute the size as the gap to the next symbol
75  for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) {
76  auto &P = Addresses[I];
77  if (P.I == O.symbol_end())
78  continue;
79 
80  // If multiple symbol have the same address, give both the same size.
81  unsigned NextI = I + 1;
82  while (NextI < N && Addresses[NextI].Address == P.Address)
83  ++NextI;
84 
85  uint64_t Size = Addresses[NextI].Address - P.Address;
86  P.Address = Size;
87  }
88 
89  // Assign the sorted symbols in the original order.
90  Ret.resize(SymNum);
91  for (SymEntry &P : Addresses) {
92  if (P.I == O.symbol_end())
93  continue;
94  Ret[P.Number] = {*P.I, P.Address};
95  }
96  return Ret;
97 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:388
This class is the base class for all object file types.
Definition: ObjectFile.h:226
int compareAddress(const SymEntry *A, const SymEntry *B)
Definition: SymbolSize.cpp:19
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
Definition: SymbolSize.cpp:33
virtual basic_symbol_iterator symbol_begin() const =0
section_iterator_range sections() const
Definition: ObjectFile.h:315
#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:1089
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:27
std::vector< std::pair< SymbolRef, uint64_t > > computeSymbolSizes(const ObjectFile &O)
Definition: SymbolSize.cpp:40
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:165
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
uint32_t Size
Definition: Profile.cpp:46
LLVM Value Representation.
Definition: Value.h:72
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81