LLVM 20.0.0git
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#include "llvm/Object/Wasm.h"
16
17using namespace llvm;
18using namespace object;
19
20// Orders increasingly by (SectionID, Address).
22 if (A->SectionID != B->SectionID)
23 return A->SectionID < B->SectionID ? -1 : 1;
24 if (A->Address != B->Address)
25 return A->Address < B->Address ? -1 : 1;
26 return 0;
27}
28
29static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
30 if (auto *M = dyn_cast<MachOObjectFile>(&O))
31 return M->getSectionID(Sec);
32 if (isa<WasmObjectFile>(&O))
33 return Sec.getIndex();
34 if (isa<XCOFFObjectFile>(&O))
35 return Sec.getIndex();
36 return cast<COFFObjectFile>(O).getSectionID(Sec);
37}
38
39static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
40 if (auto *M = dyn_cast<MachOObjectFile>(&O))
41 return M->getSymbolSectionID(Sym);
42 if (const auto *M = dyn_cast<WasmObjectFile>(&O))
43 return M->getSymbolSectionId(Sym);
44 if (const auto *M = dyn_cast<XCOFFObjectFile>(&O))
45 return M->getSymbolSectionID(Sym);
46 return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
47}
48
49std::vector<std::pair<SymbolRef, uint64_t>>
51 std::vector<std::pair<SymbolRef, uint64_t>> Ret;
52
53 if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
54 auto Syms = E->symbols();
55 if (Syms.empty())
56 Syms = E->getDynamicSymbolIterators();
57 for (ELFSymbolRef Sym : Syms)
58 Ret.push_back({Sym, Sym.getSize()});
59 return Ret;
60 }
61
62 if (const auto *E = dyn_cast<XCOFFObjectFile>(&O)) {
63 for (XCOFFSymbolRef Sym : E->symbols())
64 Ret.push_back({Sym, Sym.getSize()});
65 return Ret;
66 }
67
68 if (const auto *E = dyn_cast<WasmObjectFile>(&O)) {
69 for (SymbolRef Sym : E->symbols()) {
70 Ret.push_back({Sym, E->getSymbolSize(Sym)});
71 }
72 return Ret;
73 }
74
75 // Collect sorted symbol addresses. Include dummy addresses for the end
76 // of each section.
77 std::vector<SymEntry> Addresses;
78 unsigned SymNum = 0;
79 for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
80 SymbolRef Sym = *I;
81 Expected<uint64_t> ValueOrErr = Sym.getValue();
82 if (!ValueOrErr)
83 // TODO: Actually report errors helpfully.
84 report_fatal_error(ValueOrErr.takeError());
85 Addresses.push_back({I, *ValueOrErr, SymNum, getSymbolSectionID(O, Sym)});
86 ++SymNum;
87 }
88 for (SectionRef Sec : O.sections()) {
89 uint64_t Address = Sec.getAddress();
90 uint64_t Size = Sec.getSize();
91 Addresses.push_back(
92 {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
93 }
94
95 if (Addresses.empty())
96 return Ret;
97
98 array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
99
100 // Compute the size as the gap to the next symbol. If multiple symbols have
101 // the same address, give both the same size. Because Addresses is sorted,
102 // use two pointers to keep track of the current symbol vs. the next symbol
103 // that doesn't have the same address for size computation.
104 for (unsigned I = 0, NextI = 0, N = Addresses.size() - 1; I < N; ++I) {
105 auto &P = Addresses[I];
106 if (P.I == O.symbol_end())
107 continue;
108
109 // If the next pointer is behind, update it to the next symbol.
110 if (NextI <= I) {
111 NextI = I + 1;
112 while (NextI < N && Addresses[NextI].Address == P.Address)
113 ++NextI;
114 }
115
116 uint64_t Size = Addresses[NextI].Address - P.Address;
117 P.Address = Size;
118 }
119
120 // Assign the sorted symbols in the original order.
121 Ret.resize(SymNum);
122 for (SymEntry &P : Addresses) {
123 if (P.I == O.symbol_end())
124 continue;
125 Ret[P.Number] = {*P.I, P.Address};
126 }
127 return Ret;
128}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
Definition: SymbolSize.cpp:39
static unsigned getSectionID(const ObjectFile &O, SectionRef Sec)
Definition: SymbolSize.cpp:29
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
uint64_t getIndex() const
Definition: ObjectFile.h:525
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
int compareAddress(const SymEntry *A, const SymEntry *B)
Definition: SymbolSize.cpp:21
std::vector< std::pair< SymbolRef, uint64_t > > computeSymbolSizes(const ObjectFile &O)
Definition: SymbolSize.cpp:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
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:1607
#define N