12f09f445SMaksim Panchenko //===- bolt/Core/BinaryData.cpp - Objects in a binary file ----------------===//
2a34c753fSRafael Auler //
3a34c753fSRafael Auler // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a34c753fSRafael Auler // See https://llvm.org/LICENSE.txt for license information.
5a34c753fSRafael Auler // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a34c753fSRafael Auler //
7a34c753fSRafael Auler //===----------------------------------------------------------------------===//
8a34c753fSRafael Auler //
92f09f445SMaksim Panchenko // This file implements the BinaryData class.
102f09f445SMaksim Panchenko //
11a34c753fSRafael Auler //===----------------------------------------------------------------------===//
12a34c753fSRafael Auler
13a34c753fSRafael Auler #include "bolt/Core/BinaryData.h"
14a34c753fSRafael Auler #include "bolt/Core/BinarySection.h"
15a34c753fSRafael Auler #include "llvm/Support/CommandLine.h"
16a34c753fSRafael Auler #include "llvm/Support/Regex.h"
17a34c753fSRafael Auler
18a34c753fSRafael Auler using namespace llvm;
19a34c753fSRafael Auler using namespace bolt;
20a34c753fSRafael Auler
21a34c753fSRafael Auler #define DEBUG_TYPE "bolt"
22a34c753fSRafael Auler
23a34c753fSRafael Auler namespace opts {
24a34c753fSRafael Auler extern cl::OptionCategory BoltCategory;
25a34c753fSRafael Auler extern cl::opt<unsigned> Verbosity;
26a34c753fSRafael Auler
27a34c753fSRafael Auler cl::opt<bool>
28a34c753fSRafael Auler PrintSymbolAliases("print-aliases",
29a34c753fSRafael Auler cl::desc("print aliases when printing objects"),
30b92436efSFangrui Song cl::Hidden, cl::cat(BoltCategory));
31a34c753fSRafael Auler }
32a34c753fSRafael Auler
isAbsolute() const3340c2e0faSMaksim Panchenko bool BinaryData::isAbsolute() const { return Flags & SymbolRef::SF_Absolute; }
34a34c753fSRafael Auler
isMoveable() const35a34c753fSRafael Auler bool BinaryData::isMoveable() const {
3640c2e0faSMaksim Panchenko return (!isAbsolute() && (IsMoveable && (!Parent || isTopLevelJumpTable())));
37a34c753fSRafael Auler }
38a34c753fSRafael Auler
merge(const BinaryData * Other)39a34c753fSRafael Auler void BinaryData::merge(const BinaryData *Other) {
40a34c753fSRafael Auler assert(!Size || !Other->Size || Size == Other->Size);
41a34c753fSRafael Auler assert(Address == Other->Address);
42a34c753fSRafael Auler assert(*Section == *Other->Section);
43a34c753fSRafael Auler assert(OutputOffset == Other->OutputOffset);
44a34c753fSRafael Auler assert(OutputSection == Other->OutputSection);
45a34c753fSRafael Auler Symbols.insert(Symbols.end(), Other->Symbols.begin(), Other->Symbols.end());
46a34c753fSRafael Auler Flags |= Other->Flags;
47a34c753fSRafael Auler if (!Size)
48a34c753fSRafael Auler Size = Other->Size;
49a34c753fSRafael Auler }
50a34c753fSRafael Auler
hasName(StringRef Name) const51a34c753fSRafael Auler bool BinaryData::hasName(StringRef Name) const {
523652483cSRafael Auler for (const MCSymbol *Symbol : Symbols)
53a34c753fSRafael Auler if (Name == Symbol->getName())
54a34c753fSRafael Auler return true;
55a34c753fSRafael Auler return false;
56a34c753fSRafael Auler }
57a34c753fSRafael Auler
nameStartsWith(StringRef Prefix) const58a34c753fSRafael Auler bool BinaryData::nameStartsWith(StringRef Prefix) const {
593652483cSRafael Auler for (const MCSymbol *Symbol : Symbols)
60*ad8fd5b1SKazu Hirata if (Symbol->getName().starts_with(Prefix))
61a34c753fSRafael Auler return true;
62a34c753fSRafael Auler return false;
63a34c753fSRafael Auler }
64a34c753fSRafael Auler
getSectionName() const6540c2e0faSMaksim Panchenko StringRef BinaryData::getSectionName() const { return getSection().getName(); }
66a34c753fSRafael Auler
getOutputSectionName() const67a34c753fSRafael Auler StringRef BinaryData::getOutputSectionName() const {
68a34c753fSRafael Auler return getOutputSection().getName();
69a34c753fSRafael Auler }
70a34c753fSRafael Auler
getOutputAddress() const71a34c753fSRafael Auler uint64_t BinaryData::getOutputAddress() const {
72a34c753fSRafael Auler assert(OutputSection->getOutputAddress());
73a34c753fSRafael Auler return OutputSection->getOutputAddress() + OutputOffset;
74a34c753fSRafael Auler }
75a34c753fSRafael Auler
getOffset() const76a34c753fSRafael Auler uint64_t BinaryData::getOffset() const {
77a34c753fSRafael Auler return Address - getSection().getAddress();
78a34c753fSRafael Auler }
79a34c753fSRafael Auler
setSection(BinarySection & NewSection)80a34c753fSRafael Auler void BinaryData::setSection(BinarySection &NewSection) {
81a34c753fSRafael Auler if (OutputSection == Section)
82a34c753fSRafael Auler OutputSection = &NewSection;
83a34c753fSRafael Auler Section = &NewSection;
84a34c753fSRafael Auler }
85a34c753fSRafael Auler
isMoved() const86a34c753fSRafael Auler bool BinaryData::isMoved() const {
87a34c753fSRafael Auler return (getOffset() != OutputOffset || OutputSection != Section);
88a34c753fSRafael Auler }
89a34c753fSRafael Auler
print(raw_ostream & OS) const9040c2e0faSMaksim Panchenko void BinaryData::print(raw_ostream &OS) const { printBrief(OS); }
91a34c753fSRafael Auler
printBrief(raw_ostream & OS) const92a34c753fSRafael Auler void BinaryData::printBrief(raw_ostream &OS) const {
93a34c753fSRafael Auler OS << "(";
94a34c753fSRafael Auler
95a34c753fSRafael Auler if (isJumpTable())
96a34c753fSRafael Auler OS << "jump-table: ";
97a34c753fSRafael Auler else
98a34c753fSRafael Auler OS << "object: ";
99a34c753fSRafael Auler
100a34c753fSRafael Auler OS << getName();
101a34c753fSRafael Auler
102a34c753fSRafael Auler if ((opts::PrintSymbolAliases || opts::Verbosity > 1) && Symbols.size() > 1) {
103a34c753fSRafael Auler OS << ", aliases:";
104a34c753fSRafael Auler for (unsigned I = 1u; I < Symbols.size(); ++I) {
105a34c753fSRafael Auler OS << (I == 1 ? " (" : ", ") << Symbols[I]->getName();
106a34c753fSRafael Auler }
107a34c753fSRafael Auler OS << ")";
108a34c753fSRafael Auler }
109a34c753fSRafael Auler
110a34c753fSRafael Auler if (Parent) {
111a34c753fSRafael Auler OS << " (parent: ";
112a34c753fSRafael Auler Parent->printBrief(OS);
113a34c753fSRafael Auler OS << ")";
114a34c753fSRafael Auler }
115a34c753fSRafael Auler
11640c2e0faSMaksim Panchenko OS << ", 0x" << Twine::utohexstr(getAddress()) << ":0x"
11740c2e0faSMaksim Panchenko << Twine::utohexstr(getEndAddress()) << "/" << getSize() << "/"
11840c2e0faSMaksim Panchenko << getAlignment() << "/0x" << Twine::utohexstr(Flags);
119a34c753fSRafael Auler
120a34c753fSRafael Auler OS << ")";
121a34c753fSRafael Auler }
122a34c753fSRafael Auler
BinaryData(MCSymbol & Symbol,uint64_t Address,uint64_t Size,uint16_t Alignment,BinarySection & Section,unsigned Flags)12340c2e0faSMaksim Panchenko BinaryData::BinaryData(MCSymbol &Symbol, uint64_t Address, uint64_t Size,
12440c2e0faSMaksim Panchenko uint16_t Alignment, BinarySection &Section,
125a34c753fSRafael Auler unsigned Flags)
12640c2e0faSMaksim Panchenko : Section(&Section), Address(Address), Size(Size), Alignment(Alignment),
12740c2e0faSMaksim Panchenko Flags(Flags), OutputSection(&Section), OutputOffset(getOffset()) {
128a34c753fSRafael Auler Symbols.push_back(&Symbol);
129a34c753fSRafael Auler }
130