2018-01-23 15:10:24 -08:00
|
|
|
//===--- BinarySection.cpp - Interface for object file section -----------===//
|
|
|
|
|
//
|
2021-03-15 18:04:18 -07:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-23 15:10:24 -08:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "BinarySection.h"
|
2018-04-20 20:03:31 -07:00
|
|
|
#include "BinaryContext.h"
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
#include "Utils.h"
|
2018-02-01 16:33:43 -08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
2019-11-19 14:47:49 -08:00
|
|
|
#define DEBUG_TYPE "bolt"
|
2018-01-23 15:10:24 -08:00
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace bolt;
|
|
|
|
|
|
2018-02-01 16:33:43 -08:00
|
|
|
namespace opts {
|
|
|
|
|
extern cl::opt<bool> PrintRelocations;
|
2019-11-19 14:47:49 -08:00
|
|
|
extern cl::opt<bool> HotData;
|
2018-02-01 16:33:43 -08:00
|
|
|
}
|
|
|
|
|
|
2020-02-11 17:54:48 -08:00
|
|
|
bool BinarySection::isELF() const {
|
2020-05-26 04:21:04 -07:00
|
|
|
return BC.isELF();
|
2020-02-11 17:54:48 -08:00
|
|
|
}
|
|
|
|
|
|
2020-09-24 03:22:31 -07:00
|
|
|
bool BinarySection::isMachO() const {
|
|
|
|
|
return BC.isMachO();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 03:17:32 -07:00
|
|
|
uint64_t
|
|
|
|
|
BinarySection::hash(const BinaryData &BD,
|
|
|
|
|
std::map<const BinaryData *, uint64_t> &Cache) const {
|
|
|
|
|
auto Itr = Cache.find(&BD);
|
|
|
|
|
if (Itr != Cache.end())
|
|
|
|
|
return Itr->second;
|
|
|
|
|
|
|
|
|
|
Cache[&BD] = 0;
|
|
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
uint64_t Offset = BD.getAddress() - getAddress();
|
|
|
|
|
const uint64_t EndOffset = BD.getEndAddress() - getAddress();
|
2018-06-06 03:17:32 -07:00
|
|
|
auto Begin = Relocations.lower_bound(Relocation{Offset, 0, 0, 0, 0});
|
|
|
|
|
auto End = Relocations.upper_bound(Relocation{EndOffset, 0, 0, 0, 0});
|
2021-04-08 00:19:26 -07:00
|
|
|
const StringRef Contents = getContents();
|
2018-06-06 03:17:32 -07:00
|
|
|
|
|
|
|
|
hash_code Hash = hash_combine(hash_value(BD.getSize()),
|
|
|
|
|
hash_value(BD.getSectionName()));
|
|
|
|
|
|
|
|
|
|
while (Begin != End) {
|
2021-04-08 00:19:26 -07:00
|
|
|
const Relocation &Rel = *Begin++;
|
2018-06-06 03:17:32 -07:00
|
|
|
Hash = hash_combine(
|
|
|
|
|
Hash,
|
|
|
|
|
hash_value(Contents.substr(Offset, Begin->Offset - Offset)));
|
2021-04-08 00:19:26 -07:00
|
|
|
if (BinaryData *RelBD = BC.getBinaryDataByName(Rel.Symbol->getName())) {
|
2018-06-06 03:17:32 -07:00
|
|
|
Hash = hash_combine(Hash, hash(*RelBD, Cache));
|
|
|
|
|
}
|
|
|
|
|
Offset = Rel.Offset + Rel.getSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Hash = hash_combine(
|
|
|
|
|
Hash,
|
|
|
|
|
hash_value(Contents.substr(Offset, EndOffset - Offset)));
|
|
|
|
|
|
|
|
|
|
Cache[&BD] = Hash;
|
|
|
|
|
|
|
|
|
|
return Hash;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 14:47:49 -08:00
|
|
|
void BinarySection::emitAsData(MCStreamer &Streamer, StringRef NewName) const {
|
|
|
|
|
StringRef SectionName = !NewName.empty() ? NewName : getName();
|
|
|
|
|
StringRef SectionContents = getContents();
|
2021-04-08 00:19:26 -07:00
|
|
|
MCSectionELF *ELFSection =
|
|
|
|
|
BC.Ctx->getELFSection(SectionName, getELFType(), getELFFlags());
|
2019-11-19 14:47:49 -08:00
|
|
|
|
|
|
|
|
Streamer.SwitchSection(ELFSection);
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitValueToAlignment(getAlignment());
|
2019-11-19 14:47:49 -08:00
|
|
|
|
|
|
|
|
if (BC.HasRelocations && opts::HotData && isReordered())
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitLabel(BC.Ctx->getOrCreateSymbol("__hot_data_start"));
|
2019-11-19 14:47:49 -08:00
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitting "
|
|
|
|
|
<< (isAllocatable() ? "" : "non-")
|
|
|
|
|
<< "allocatable data section " << SectionName << '\n');
|
2019-11-19 14:47:49 -08:00
|
|
|
|
|
|
|
|
if (!hasRelocations()) {
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitBytes(SectionContents);
|
2019-11-19 14:47:49 -08:00
|
|
|
} else {
|
|
|
|
|
uint64_t SectionOffset = 0;
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const Relocation &Relocation : relocations()) {
|
2019-11-19 14:47:49 -08:00
|
|
|
assert(Relocation.Offset < SectionContents.size() && "overflow detected");
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
// Skip undefined symbols.
|
|
|
|
|
if (BC.UndefinedSymbols.count(Relocation.Symbol))
|
|
|
|
|
continue;
|
2019-11-19 14:47:49 -08:00
|
|
|
if (SectionOffset < Relocation.Offset) {
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitBytes(
|
2019-11-19 14:47:49 -08:00
|
|
|
SectionContents.substr(SectionOffset,
|
|
|
|
|
Relocation.Offset - SectionOffset));
|
|
|
|
|
SectionOffset = Relocation.Offset;
|
|
|
|
|
}
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitting relocation for symbol "
|
|
|
|
|
<< (Relocation.Symbol ? Relocation.Symbol->getName()
|
|
|
|
|
: StringRef("<none>"))
|
|
|
|
|
<< " at offset 0x"
|
|
|
|
|
<< Twine::utohexstr(Relocation.Offset) << " with size "
|
|
|
|
|
<< Relocation::getSizeForType(Relocation.Type) << '\n');
|
2021-04-08 00:19:26 -07:00
|
|
|
size_t RelocationSize = Relocation.emit(&Streamer);
|
2019-11-19 14:47:49 -08:00
|
|
|
SectionOffset += RelocationSize;
|
|
|
|
|
}
|
|
|
|
|
assert(SectionOffset <= SectionContents.size() && "overflow error");
|
|
|
|
|
if (SectionOffset < SectionContents.size()) {
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitBytes(SectionContents.substr(SectionOffset));
|
2019-11-19 14:47:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BC.HasRelocations && opts::HotData && isReordered())
|
2020-12-01 16:29:39 -08:00
|
|
|
Streamer.emitLabel(BC.Ctx->getOrCreateSymbol("__hot_data_end"));
|
2019-11-19 14:47:49 -08:00
|
|
|
}
|
|
|
|
|
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
void BinarySection::flushPendingRelocations(raw_pwrite_stream &OS,
|
|
|
|
|
SymbolResolverFuncTy Resolver) {
|
|
|
|
|
if (PendingRelocations.empty() && Patches.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const uint64_t SectionAddress = getAddress();
|
|
|
|
|
|
|
|
|
|
// We apply relocations to original section contents. For allocatable sections
|
|
|
|
|
// this means using their input file offsets, since the output file offset
|
|
|
|
|
// could change (e.g. for new instance of .text). For non-allocatable
|
|
|
|
|
// sections, the output offset should always be a valid one.
|
|
|
|
|
const uint64_t SectionFileOffset = isAllocatable() ? getInputFileOffset()
|
|
|
|
|
: getOutputFileOffset();
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
|
dbgs() << "BOLT-DEBUG: flushing pending relocations for section "
|
|
|
|
|
<< getName() << '\n'
|
|
|
|
|
<< " address: 0x" << Twine::utohexstr(SectionAddress) << '\n'
|
|
|
|
|
<< " offset: 0x" << Twine::utohexstr(SectionFileOffset) << '\n');
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
for (BinaryPatch &Patch : Patches) {
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
OS.pwrite(Patch.Bytes.data(),
|
|
|
|
|
Patch.Bytes.size(),
|
|
|
|
|
SectionFileOffset + Patch.Offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
for (Relocation &Reloc : PendingRelocations) {
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
uint64_t Value = Reloc.Addend;
|
|
|
|
|
if (Reloc.Symbol)
|
|
|
|
|
Value += Resolver(Reloc.Symbol);
|
|
|
|
|
switch(Reloc.Type) {
|
|
|
|
|
default:
|
|
|
|
|
llvm_unreachable(
|
|
|
|
|
"only R_X86_64_32 relocations are supported at the moment");
|
|
|
|
|
case ELF::R_X86_64_32: {
|
|
|
|
|
OS.pwrite(reinterpret_cast<const char*>(&Value),
|
|
|
|
|
Relocation::getSizeForType(Reloc.Type),
|
|
|
|
|
SectionFileOffset + Reloc.Offset);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ELF::R_X86_64_PC32: {
|
|
|
|
|
Value -= SectionAddress + Reloc.Offset;
|
|
|
|
|
OS.pwrite(reinterpret_cast<const char*>(&Value),
|
|
|
|
|
Relocation::getSizeForType(Reloc.Type),
|
|
|
|
|
SectionFileOffset + Reloc.Offset);
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: writing value 0x"
|
|
|
|
|
<< Twine::utohexstr(Value) << " of size "
|
|
|
|
|
<< Relocation::getSizeForType(Reloc.Type)
|
|
|
|
|
<< " at offset 0x" << Twine::utohexstr(Reloc.Offset)
|
|
|
|
|
<< " address 0x"
|
|
|
|
|
<< Twine::utohexstr(SectionAddress + Reloc.Offset)
|
|
|
|
|
<< " Offset 0x"
|
|
|
|
|
<< Twine::utohexstr(SectionFileOffset + Reloc.Offset)
|
|
|
|
|
<< '\n';);
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
|
dbgs() << "BOLT-DEBUG: writing value 0x" << Twine::utohexstr(Value)
|
|
|
|
|
<< " of size " << Relocation::getSizeForType(Reloc.Type)
|
|
|
|
|
<< " at section offset 0x" << Twine::utohexstr(Reloc.Offset)
|
|
|
|
|
<< " address 0x"
|
|
|
|
|
<< Twine::utohexstr(SectionAddress + Reloc.Offset)
|
|
|
|
|
<< " file offset 0x"
|
|
|
|
|
<< Twine::utohexstr(SectionFileOffset + Reloc.Offset) << '\n';);
|
2019-11-20 00:16:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearList(PendingRelocations);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 16:33:43 -08:00
|
|
|
BinarySection::~BinarySection() {
|
2018-04-20 20:03:31 -07:00
|
|
|
if (isReordered()) {
|
|
|
|
|
delete[] getData();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-23 15:49:36 -07:00
|
|
|
|
2018-02-01 16:33:43 -08:00
|
|
|
if (!isAllocatable() &&
|
|
|
|
|
(!hasSectionRef() ||
|
|
|
|
|
OutputContents.data() != getContents(Section).data())) {
|
|
|
|
|
delete[] getOutputData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
[BOLT] Support for lite mode with relocations
Summary:
Add '-lite' support for relocations for improved processing time,
memory consumption, and more resilient processing of binaries with
embedded assembly code.
In lite relocation mode, BOLT will skip full processing of functions
without a profile. It will run scanExternalRefs() on such functions
to discover external references and to create internal relocations
to update references to optimized functions.
Note that we could have relied on the compiler/linker to provide
relocations for function references. However, there's no assurance
that all such references are reported. E.g., the compiler can resolve
inter-procedural references internally, leaving no relocations
for the linker.
The scan process takes about <10 seconds per 100MB of code on modern
hardware. It's a reasonable overhead to live with considering the
flexibility it provides.
If BOLT fails to scan or disassemble a function, .e.g., due to a data
object embedded in code, or an unsupported instruction, it enables a
patching mode to guarantee that the failed function will call
optimized/moved versions of functions. The patching happens at original
function entry points.
'-skip=<func1,func2,...>' option now can be used to skip processing of
arbitrary functions in the relocation mode.
With '-use-old-text' or '-strict' we require all functions to be
processed. As such, it is incompatible with '-lite' option,
and '-skip' option will only disable optimizations of listed
functions, not their disassembly and emission.
(cherry picked from FBD22040717)
2020-06-15 00:15:47 -07:00
|
|
|
void BinarySection::clearRelocations() {
|
|
|
|
|
clearList(Relocations);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 16:33:43 -08:00
|
|
|
void BinarySection::print(raw_ostream &OS) const {
|
|
|
|
|
OS << getName() << ", "
|
|
|
|
|
<< "0x" << Twine::utohexstr(getAddress()) << ", "
|
|
|
|
|
<< getSize()
|
2019-03-14 18:51:05 -07:00
|
|
|
<< " (0x" << Twine::utohexstr(getOutputAddress()) << ", "
|
2018-02-01 16:33:43 -08:00
|
|
|
<< getOutputSize() << ")"
|
|
|
|
|
<< ", data = " << getData()
|
|
|
|
|
<< ", output data = " << getOutputData();
|
|
|
|
|
|
|
|
|
|
if (isAllocatable())
|
|
|
|
|
OS << " (allocatable)";
|
|
|
|
|
|
2017-11-14 20:05:11 -08:00
|
|
|
if (isVirtual())
|
|
|
|
|
OS << " (virtual)";
|
|
|
|
|
|
|
|
|
|
if (isTLS())
|
|
|
|
|
OS << " (tls)";
|
|
|
|
|
|
2018-02-01 16:33:43 -08:00
|
|
|
if (opts::PrintRelocations) {
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const Relocation &R : relocations())
|
2018-02-01 16:33:43 -08:00
|
|
|
OS << "\n " << R;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-20 20:03:31 -07:00
|
|
|
|
|
|
|
|
std::set<Relocation> BinarySection::reorderRelocations(bool Inplace) const {
|
|
|
|
|
assert(PendingRelocations.empty() &&
|
|
|
|
|
"reodering pending relocations not supported");
|
|
|
|
|
std::set<Relocation> NewRelocations;
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const Relocation &Rel : relocations()) {
|
|
|
|
|
uint64_t RelAddr = Rel.Offset + getAddress();
|
|
|
|
|
BinaryData *BD = BC.getBinaryDataContainingAddress(RelAddr);
|
2018-04-20 20:03:31 -07:00
|
|
|
BD = BD->getAtomicRoot();
|
|
|
|
|
assert(BD);
|
|
|
|
|
|
|
|
|
|
if ((!BD->isMoved() && !Inplace) || BD->isJumpTable())
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
Relocation NewRel(Rel);
|
|
|
|
|
uint64_t RelOffset = RelAddr - BD->getAddress();
|
2018-04-20 20:03:31 -07:00
|
|
|
NewRel.Offset = BD->getOutputOffset() + RelOffset;
|
|
|
|
|
assert(NewRel.Offset < getSize());
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: moving " << Rel << " -> " << NewRel
|
|
|
|
|
<< "\n");
|
2018-04-20 20:03:31 -07:00
|
|
|
auto Res = NewRelocations.emplace(std::move(NewRel));
|
|
|
|
|
(void)Res;
|
|
|
|
|
assert(Res.second && "Can't overwrite existing relocation");
|
|
|
|
|
}
|
|
|
|
|
return NewRelocations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinarySection::reorderContents(const std::vector<BinaryData *> &Order,
|
|
|
|
|
bool Inplace) {
|
|
|
|
|
IsReordered = true;
|
|
|
|
|
|
|
|
|
|
Relocations = reorderRelocations(Inplace);
|
|
|
|
|
|
|
|
|
|
std::string Str;
|
|
|
|
|
raw_string_ostream OS(Str);
|
2021-04-08 00:19:26 -07:00
|
|
|
const char *Src = Contents.data();
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: reorderContents for " << Name << "\n");
|
2021-04-08 00:19:26 -07:00
|
|
|
for (BinaryData *BD : Order) {
|
2018-04-20 20:03:31 -07:00
|
|
|
assert((BD->isMoved() || !Inplace) && !BD->isJumpTable());
|
|
|
|
|
assert(BD->isAtomic() && BD->isMoveable());
|
2021-04-08 00:19:26 -07:00
|
|
|
const uint64_t SrcOffset = BD->getAddress() - getAddress();
|
2018-04-20 20:03:31 -07:00
|
|
|
assert(SrcOffset < Contents.size());
|
|
|
|
|
assert(SrcOffset == BD->getOffset());
|
|
|
|
|
while (OS.tell() < BD->getOutputOffset()) {
|
|
|
|
|
OS.write((unsigned char)0);
|
|
|
|
|
}
|
2020-12-01 16:29:39 -08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: " << BD->getName() << " @ " << OS.tell()
|
|
|
|
|
<< "\n");
|
2018-04-20 20:03:31 -07:00
|
|
|
OS.write(&Src[SrcOffset], BD->getOutputSize());
|
|
|
|
|
}
|
|
|
|
|
if (Relocations.empty()) {
|
|
|
|
|
// If there are no existing relocations, tack a phony one at the end
|
|
|
|
|
// of the reordered segment to force LLVM to recognize and map this
|
|
|
|
|
// section.
|
2021-04-08 00:19:26 -07:00
|
|
|
MCSymbol *ZeroSym = BC.registerNameAtAddress("Zero", 0, 0, 0);
|
2018-04-20 20:03:31 -07:00
|
|
|
addRelocation(OS.tell(), ZeroSym, ELF::R_X86_64_64, 0xdeadbeef);
|
|
|
|
|
|
|
|
|
|
uint64_t Zero = 0;
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Zero), sizeof(Zero));
|
|
|
|
|
}
|
|
|
|
|
auto *NewData = reinterpret_cast<char *>(copyByteArray(OS.str()));
|
|
|
|
|
Contents = OutputContents = StringRef(NewData, OS.str().size());
|
|
|
|
|
OutputSize = Contents.size();
|
|
|
|
|
}
|
2019-08-02 11:20:13 -07:00
|
|
|
|
|
|
|
|
std::string BinarySection::encodeELFNote(StringRef NameStr, StringRef DescStr,
|
|
|
|
|
uint32_t Type) {
|
|
|
|
|
std::string Str;
|
|
|
|
|
raw_string_ostream OS(Str);
|
|
|
|
|
const uint32_t NameSz = NameStr.size() + 1;
|
|
|
|
|
const uint32_t DescSz = DescStr.size();
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&(NameSz)), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&(DescSz)), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&(Type)), 4);
|
|
|
|
|
OS << NameStr << '\0';
|
|
|
|
|
for (uint64_t I = NameSz; I < alignTo(NameSz, 4); ++I) {
|
|
|
|
|
OS << '\0';
|
|
|
|
|
}
|
|
|
|
|
OS << DescStr;
|
|
|
|
|
for (uint64_t I = DescStr.size(); I < alignTo(DescStr.size(), 4); ++I) {
|
|
|
|
|
OS << '\0';
|
|
|
|
|
}
|
|
|
|
|
return OS.str();
|
|
|
|
|
}
|