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 9 #include "llvm/Object/SymbolSize.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/Object/COFF.h" 12 #include "llvm/Object/ELFObjectFile.h" 13 #include "llvm/Object/MachO.h" 14 #include "llvm/Object/Wasm.h" 15 16 using namespace llvm; 17 using namespace object; 18 19 // Orders increasingly by (SectionID, Address). 20 int llvm::object::compareAddress(const SymEntry *A, const SymEntry *B) { 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 if (isa<WasmObjectFile>(&O)) 32 return Sec.getIndex(); 33 34 return cast<COFFObjectFile>(O).getSectionID(Sec); 35 } 36 37 static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) { 38 if (auto *M = dyn_cast<MachOObjectFile>(&O)) 39 return M->getSymbolSectionID(Sym); 40 if (const auto *M = dyn_cast<WasmObjectFile>(&O)) 41 return M->getSymbolSectionId(Sym); 42 return cast<COFFObjectFile>(O).getSymbolSectionID(Sym); 43 } 44 45 std::vector<std::pair<SymbolRef, uint64_t>> 46 llvm::object::computeSymbolSizes(const ObjectFile &O) { 47 std::vector<std::pair<SymbolRef, uint64_t>> Ret; 48 49 if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) { 50 auto Syms = E->symbols(); 51 if (Syms.begin() == Syms.end()) 52 Syms = E->getDynamicSymbolIterators(); 53 for (ELFSymbolRef Sym : Syms) 54 Ret.push_back({Sym, Sym.getSize()}); 55 return Ret; 56 } 57 58 // Collect sorted symbol addresses. Include dummy addresses for the end 59 // of each section. 60 std::vector<SymEntry> Addresses; 61 unsigned SymNum = 0; 62 for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) { 63 SymbolRef Sym = *I; 64 uint64_t Value = Sym.getValue(); 65 Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)}); 66 ++SymNum; 67 } 68 for (SectionRef Sec : O.sections()) { 69 uint64_t Address = Sec.getAddress(); 70 uint64_t Size = Sec.getSize(); 71 Addresses.push_back( 72 {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)}); 73 } 74 75 if (Addresses.empty()) 76 return Ret; 77 78 array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress); 79 80 // Compute the size as the gap to the next symbol 81 for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) { 82 auto &P = Addresses[I]; 83 if (P.I == O.symbol_end()) 84 continue; 85 86 // If multiple symbol have the same address, give both the same size. 87 unsigned NextI = I + 1; 88 while (NextI < N && Addresses[NextI].Address == P.Address) 89 ++NextI; 90 91 uint64_t Size = Addresses[NextI].Address - P.Address; 92 P.Address = Size; 93 } 94 95 // Assign the sorted symbols in the original order. 96 Ret.resize(SymNum); 97 for (SymEntry &P : Addresses) { 98 if (P.I == O.symbol_end()) 99 continue; 100 Ret[P.Number] = {*P.I, P.Address}; 101 } 102 return Ret; 103 } 104