[BOLT] Detect Linux kernel version if the binary is a Linux kernel (#119088)
[llvm-project.git] / bolt / lib / Core / BinaryData.cpp
blobe9ddf08d8695f468907a34db47610ea2cd9865de
1 //===- bolt/Core/BinaryData.cpp - Objects in a binary file ----------------===//
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 // 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"
18 using namespace llvm;
19 using namespace bolt;
21 #define DEBUG_TYPE "bolt"
23 namespace opts {
24 extern cl::OptionCategory BoltCategory;
25 extern cl::opt<unsigned> Verbosity;
27 cl::opt<bool>
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;
47 if (!Size)
48 Size = Other->Size;
51 bool BinaryData::hasName(StringRef Name) const {
52 for (const MCSymbol *Symbol : Symbols)
53 if (Name == Symbol->getName())
54 return true;
55 return false;
58 bool BinaryData::nameStartsWith(StringRef Prefix) const {
59 for (const MCSymbol *Symbol : Symbols)
60 if (Symbol->getName().starts_with(Prefix))
61 return true;
62 return false;
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 {
93 OS << "(";
95 if (isJumpTable())
96 OS << "jump-table: ";
97 else
98 OS << "object: ";
100 OS << getName();
102 if ((opts::PrintSymbolAliases || opts::Verbosity > 1) && Symbols.size() > 1) {
103 OS << ", aliases:";
104 for (unsigned I = 1u; I < Symbols.size(); ++I) {
105 OS << (I == 1 ? " (" : ", ") << Symbols[I]->getName();
107 OS << ")";
110 if (Parent) {
111 OS << " (parent: ";
112 Parent->printBrief(OS);
113 OS << ")";
116 OS << ", 0x" << Twine::utohexstr(getAddress()) << ":0x"
117 << Twine::utohexstr(getEndAddress()) << "/" << getSize() << "/"
118 << getAlignment() << "/0x" << Twine::utohexstr(Flags);
120 OS << ")";
123 BinaryData::BinaryData(MCSymbol &Symbol, uint64_t Address, uint64_t Size,
124 uint16_t Alignment, BinarySection &Section,
125 unsigned Flags)
126 : Section(&Section), Address(Address), Size(Size), Alignment(Alignment),
127 Flags(Flags), OutputSection(&Section), OutputOffset(getOffset()) {
128 Symbols.push_back(&Symbol);