1 //===- bolt/Core/BinaryData.cpp - Objects in a binary file ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the BinaryData class.
11 //===----------------------------------------------------------------------===//
13 #include "bolt/Core/BinaryData.h"
14 #include "bolt/Core/BinarySection.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Regex.h"
21 #define DEBUG_TYPE "bolt"
24 extern cl::OptionCategory BoltCategory
;
25 extern cl::opt
<unsigned> Verbosity
;
28 PrintSymbolAliases("print-aliases",
29 cl::desc("print aliases when printing objects"),
30 cl::Hidden
, cl::cat(BoltCategory
));
33 bool BinaryData::isAbsolute() const { return Flags
& SymbolRef::SF_Absolute
; }
35 bool BinaryData::isMoveable() const {
36 return (!isAbsolute() && (IsMoveable
&& (!Parent
|| isTopLevelJumpTable())));
39 void BinaryData::merge(const BinaryData
*Other
) {
40 assert(!Size
|| !Other
->Size
|| Size
== Other
->Size
);
41 assert(Address
== Other
->Address
);
42 assert(*Section
== *Other
->Section
);
43 assert(OutputOffset
== Other
->OutputOffset
);
44 assert(OutputSection
== Other
->OutputSection
);
45 Symbols
.insert(Symbols
.end(), Other
->Symbols
.begin(), Other
->Symbols
.end());
46 Flags
|= Other
->Flags
;
51 bool BinaryData::hasName(StringRef Name
) const {
52 for (const MCSymbol
*Symbol
: Symbols
)
53 if (Name
== Symbol
->getName())
58 bool BinaryData::nameStartsWith(StringRef Prefix
) const {
59 for (const MCSymbol
*Symbol
: Symbols
)
60 if (Symbol
->getName().starts_with(Prefix
))
65 StringRef
BinaryData::getSectionName() const { return getSection().getName(); }
67 StringRef
BinaryData::getOutputSectionName() const {
68 return getOutputSection().getName();
71 uint64_t BinaryData::getOutputAddress() const {
72 assert(OutputSection
->getOutputAddress());
73 return OutputSection
->getOutputAddress() + OutputOffset
;
76 uint64_t BinaryData::getOffset() const {
77 return Address
- getSection().getAddress();
80 void BinaryData::setSection(BinarySection
&NewSection
) {
81 if (OutputSection
== Section
)
82 OutputSection
= &NewSection
;
83 Section
= &NewSection
;
86 bool BinaryData::isMoved() const {
87 return (getOffset() != OutputOffset
|| OutputSection
!= Section
);
90 void BinaryData::print(raw_ostream
&OS
) const { printBrief(OS
); }
92 void BinaryData::printBrief(raw_ostream
&OS
) const {
102 if ((opts::PrintSymbolAliases
|| opts::Verbosity
> 1) && Symbols
.size() > 1) {
104 for (unsigned I
= 1u; I
< Symbols
.size(); ++I
) {
105 OS
<< (I
== 1 ? " (" : ", ") << Symbols
[I
]->getName();
112 Parent
->printBrief(OS
);
116 OS
<< ", 0x" << Twine::utohexstr(getAddress()) << ":0x"
117 << Twine::utohexstr(getEndAddress()) << "/" << getSize() << "/"
118 << getAlignment() << "/0x" << Twine::utohexstr(Flags
);
123 BinaryData::BinaryData(MCSymbol
&Symbol
, uint64_t Address
, uint64_t Size
,
124 uint16_t Alignment
, BinarySection
&Section
,
126 : Section(&Section
), Address(Address
), Size(Size
), Alignment(Alignment
),
127 Flags(Flags
), OutputSection(&Section
), OutputOffset(getOffset()) {
128 Symbols
.push_back(&Symbol
);