2015-06-23 02:08:48 +00:00
|
|
|
//===- SymbolSize.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/SymbolSize.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-06-24 19:57:32 +00:00
|
|
|
#include "llvm/Object/COFF.h"
|
2015-06-23 02:08:48 +00:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2015-06-24 19:57:32 +00:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-06-23 02:08:48 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct SymEntry {
|
|
|
|
symbol_iterator I;
|
|
|
|
uint64_t Address;
|
|
|
|
unsigned Number;
|
2015-06-24 19:57:32 +00:00
|
|
|
unsigned SectionID;
|
2015-06-23 02:08:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compareAddress(const SymEntry *A, const SymEntry *B) {
|
2015-06-24 19:57:32 +00:00
|
|
|
if (A->SectionID != B->SectionID)
|
|
|
|
return A->SectionID - B->SectionID;
|
|
|
|
return A->Address - B->Address;
|
2015-06-23 02:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:57:32 +00:00
|
|
|
static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
|
|
|
|
if (auto *M = dyn_cast<MachOObjectFile>(&O))
|
|
|
|
return M->getSectionID(Sec);
|
|
|
|
return cast<COFFObjectFile>(O).getSectionID(Sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
|
|
|
|
if (auto *M = dyn_cast<MachOObjectFile>(&O))
|
|
|
|
return M->getSymbolSectionID(Sym);
|
|
|
|
return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<SymbolRef, uint64_t>>
|
2015-06-23 02:08:48 +00:00
|
|
|
llvm::object::computeSymbolSizes(const ObjectFile &O) {
|
|
|
|
std::vector<std::pair<SymbolRef, uint64_t>> Ret;
|
|
|
|
|
2015-06-24 10:20:30 +00:00
|
|
|
if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
|
2015-06-25 15:06:38 +00:00
|
|
|
auto Syms = E->symbols();
|
|
|
|
if (Syms.begin() == Syms.end())
|
|
|
|
Syms = E->getDynamicSymbolIterators();
|
2015-06-25 22:10:04 +00:00
|
|
|
for (ELFSymbolRef Sym : Syms)
|
|
|
|
Ret.push_back({Sym, Sym.getSize()});
|
2015-06-23 02:08:48 +00:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sorted symbol addresses. Include dummy addresses for the end
|
|
|
|
// of each section.
|
|
|
|
std::vector<SymEntry> Addresses;
|
|
|
|
unsigned SymNum = 0;
|
|
|
|
for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
|
|
|
|
SymbolRef Sym = *I;
|
2015-06-24 19:32:52 +00:00
|
|
|
uint64_t Value = Sym.getValue();
|
2015-06-24 19:57:32 +00:00
|
|
|
Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)});
|
2015-06-23 02:08:48 +00:00
|
|
|
++SymNum;
|
|
|
|
}
|
2015-06-24 19:57:32 +00:00
|
|
|
for (SectionRef Sec : O.sections()) {
|
2015-06-23 02:08:48 +00:00
|
|
|
uint64_t Address = Sec.getAddress();
|
|
|
|
uint64_t Size = Sec.getSize();
|
2015-06-24 19:57:32 +00:00
|
|
|
Addresses.push_back(
|
|
|
|
{O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
|
2015-06-23 02:08:48 +00:00
|
|
|
}
|
|
|
|
array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
|
|
|
|
|
|
|
|
// Compute the size as the gap to the next symbol
|
|
|
|
for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) {
|
|
|
|
auto &P = Addresses[I];
|
|
|
|
if (P.I == O.symbol_end())
|
|
|
|
continue;
|
2015-06-23 03:36:08 +00:00
|
|
|
|
|
|
|
// If multiple symbol have the same address, give both the same size.
|
|
|
|
unsigned NextI = I + 1;
|
|
|
|
while (NextI < N && Addresses[NextI].Address == P.Address)
|
|
|
|
++NextI;
|
|
|
|
|
|
|
|
uint64_t Size = Addresses[NextI].Address - P.Address;
|
2015-06-23 02:08:48 +00:00
|
|
|
P.Address = Size;
|
|
|
|
}
|
|
|
|
|
2015-06-29 16:05:00 +00:00
|
|
|
// Assign the sorted symbols in the original order.
|
|
|
|
Ret.resize(SymNum);
|
2015-06-23 02:08:48 +00:00
|
|
|
for (SymEntry &P : Addresses) {
|
|
|
|
if (P.I == O.symbol_end())
|
|
|
|
continue;
|
2015-06-29 16:05:00 +00:00
|
|
|
Ret[P.Number] = {*P.I, P.Address};
|
2015-06-23 02:08:48 +00:00
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|