#include "OutputSections.h"
#include "Config.h"
#include "InputFiles.h"
#include "LinkerScript.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "lld/Common/Arrays.h"
#include "lld/Common/Memory.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"
#if LLVM_ENABLE_ZLIB
#define NOMINMAX
#include <zlib.h>
#endif
#if LLVM_ENABLE_ZSTD
#include <zstd.h>
#endif
using namespace llvm;
using namespace llvm::dwarf;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;
uint8_t *Out::bufferStart;
PhdrEntry *Out::tlsPhdr;
OutputSection *Out::elfHeader;
OutputSection *Out::programHeaders;
OutputSection *Out::preinitArray;
OutputSection *Out::initArray;
OutputSection *Out::finiArray;
SmallVector<OutputSection *, 0> elf::outputSections;
uint32_t OutputSection::getPhdrFlags() const {
uint32_t ret = 0;
if (config->emachine != EM_ARM || !(flags & SHF_ARM_PURECODE))
ret |= PF_R;
if (flags & SHF_WRITE)
ret |= PF_W;
if (flags & SHF_EXECINSTR)
ret |= PF_X;
return ret;
}
template <class ELFT>
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
shdr->sh_entsize = entsize;
shdr->sh_addralign = addralign;
shdr->sh_type = type;
shdr->sh_offset = offset;
shdr->sh_flags = flags;
shdr->sh_info = info;
shdr->sh_link = link;
shdr->sh_addr = addr;
shdr->sh_size = size;
shdr->sh_name = shName;
}
OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
: SectionBase(Output, name, flags, 0, 1, type,
0, 0) {}
static bool canMergeToProgbits(unsigned type) {
return type == SHT_NOBITS || type == SHT_PROGBITS || type == SHT_INIT_ARRAY ||
type == SHT_PREINIT_ARRAY || type == SHT_FINI_ARRAY ||
type == SHT_NOTE ||
(type == SHT_X86_64_UNWIND && config->emachine == EM_X86_64);
}
void OutputSection::recordSection(InputSectionBase *isec) {
partition = isec->partition;
isec->parent = this;
if (commands.empty() || !isa<InputSectionDescription>(commands.back()))
commands.push_back(make<InputSectionDescription>(""));
auto *isd = cast<InputSectionDescription>(commands.back());
isd->sectionBases.push_back(isec);
}
void OutputSection::commitSection(InputSection *isec) {
if (LLVM_UNLIKELY(type != isec->type)) {
if (!hasInputSections && !typeIsSet) {
type = isec->type;
} else if (isStaticRelSecType(type) && isStaticRelSecType(isec->type) &&
(type == SHT_CREL) != (isec->type == SHT_CREL)) {
type = SHT_CREL;
if (type == SHT_REL) {
if (name.consume_front(".rel"))
name = saver().save(".crel" + name);
} else if (name.consume_front(".rela")) {
name = saver().save(".crel" + name);
}
} else {
if (typeIsSet || !canMergeToProgbits(type) ||
!canMergeToProgbits(isec->type)) {
if (type != SHT_NOBITS) {
errorOrWarn("section type mismatch for " + isec->name + "\n>>> " +
toString(isec) + ": " +
getELFSectionTypeName(config->emachine, isec->type) +
"\n>>> output section " + name + ": " +
getELFSectionTypeName(config->emachine, type));
}
}
if (!typeIsSet)
type = SHT_PROGBITS;
}
}
if (!hasInputSections) {
hasInputSections = true;
entsize = isec->entsize;
flags = isec->flags;
} else {
if ((flags ^ isec->flags) & SHF_TLS)
error("incompatible section flags for " + name + "\n>>> " +
toString(isec) + ": 0x" + utohexstr(isec->flags) +
"\n>>> output section " + name + ": 0x" + utohexstr(flags));
}
isec->parent = this;
uint64_t andMask =
config->emachine == EM_ARM ? (uint64_t)SHF_ARM_PURECODE : 0;
uint64_t orMask = ~andMask;
uint64_t andFlags = (flags & isec->flags) & andMask;
uint64_t orFlags = (flags | isec->flags) & orMask;
flags = andFlags | orFlags;
if (nonAlloc)
flags &= ~(uint64_t)SHF_ALLOC;
addralign = std::max(addralign, isec->addralign);
if (entsize != isec->entsize)
entsize = 0;
}
static MergeSyntheticSection *createMergeSynthetic(StringRef name,
uint32_t type,
uint64_t flags,
uint32_t addralign) {
if ((flags & SHF_STRINGS) && config->optimize >= 2)
return make<MergeTailSection>(name, type, flags, addralign);
return make<MergeNoTailSection>(name, type, flags, addralign);
}
void OutputSection::finalizeInputSections(LinkerScript *script) {
std::vector<MergeSyntheticSection *> mergeSections;
for (SectionCommand *cmd : commands) {
auto *isd = dyn_cast<InputSectionDescription>(cmd);
if (!isd)
continue;
isd->sections.reserve(isd->sectionBases.size());
for (InputSectionBase *s : isd->sectionBases) {
MergeInputSection *ms = dyn_cast<MergeInputSection>(s);
if (!ms) {
isd->sections.push_back(cast<InputSection>(s));
continue;
}
if (!ms->isLive())
continue;
auto i = llvm::find_if(mergeSections, [=](MergeSyntheticSection *sec) {
return sec->flags == ms->flags && sec->entsize == ms->entsize &&
(sec->addralign == ms->addralign || !(sec->flags & SHF_STRINGS));
});
if (i == mergeSections.end()) {
MergeSyntheticSection *syn =
createMergeSynthetic(s->name, ms->type, ms->flags, ms->addralign);
mergeSections.push_back(syn);
i = std::prev(mergeSections.end());
syn->entsize = ms->entsize;
isd->sections.push_back(syn);
auto it = script->potentialSpillLists.find(ms);
if (it != script->potentialSpillLists.end())
script->potentialSpillLists.try_emplace(syn, it->second);
}
(*i)->addSection(ms);
}
isd->sectionBases.clear();
for (InputSection *s : isd->sections)
commitSection(s);
}
for (auto *ms : mergeSections)
ms->finalizeContents();
}
static void sortByOrder(MutableArrayRef<InputSection *> in,
llvm::function_ref<int(InputSectionBase *s)> order) {
std::vector<std::pair<int, InputSection *>> v;
for (InputSection *s : in)
v.emplace_back(order(s), s);
llvm::stable_sort(v, less_first());
for (size_t i = 0; i < v.size(); ++i)
in[i] = v[i].second;
}
uint64_t elf::getHeaderSize() {
if (config->oFormatBinary)
return 0;
return Out::elfHeader->size + Out::programHeaders->size;
}
void OutputSection::sort(llvm::function_ref<int(InputSectionBase *s)> order) {
assert(isLive());
for (SectionCommand *b : commands)
if (auto *isd = dyn_cast<InputSectionDescription>(b))
sortByOrder(isd->sections, order);
}
static void nopInstrFill(uint8_t *buf, size_t size) {
if (size == 0)
return;
unsigned i = 0;
if (size == 0)
return;
std::vector<std::vector<uint8_t>> nopFiller = *target->nopInstrs;
unsigned num = size / nopFiller.back().size();
for (unsigned c = 0; c < num; ++c) {
memcpy(buf + i, nopFiller.back().data(), nopFiller.back().size());
i += nopFiller.back().size();
}
unsigned remaining = size - i;
if (!remaining)
return;
assert(nopFiller[remaining - 1].size() == remaining);
memcpy(buf + i, nopFiller[remaining - 1].data(), remaining);
}
static void fill(uint8_t *buf, size_t size,
const std::array<uint8_t, 4> &filler) {
size_t i = 0;
for (; i + 4 < size; i += 4)
memcpy(buf + i, filler.data(), 4);
memcpy(buf + i, filler.data(), size - i);
}
#if LLVM_ENABLE_ZLIB
static SmallVector<uint8_t, 0> deflateShard(ArrayRef<uint8_t> in, int level,
int flush) {
z_stream s = {};
auto res = deflateInit2(&s, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
if (res != 0) {
errorOrWarn("--compress-sections: deflateInit2 returned " + Twine(res));
return {};
}
s.next_in = const_cast<uint8_t *>(in.data());
s.avail_in = in.size();
SmallVector<uint8_t, 0> out;
size_t pos = 0;
out.resize_for_overwrite(std::max<size_t>(in.size() / 2, 64));
do {
if (pos == out.size())
out.resize_for_overwrite(out.size() * 3 / 2);
s.next_out = out.data() + pos;
s.avail_out = out.size() - pos;
(void)deflate(&s, flush);
pos = s.next_out - out.data();
} while (s.avail_out == 0);
assert(s.avail_in == 0);
out.truncate(pos);
deflateEnd(&s);
return out;
}
#endif
template <class ELFT> void OutputSection::maybeCompress() {
using Elf_Chdr = typename ELFT::Chdr;
(void)sizeof(Elf_Chdr);
DebugCompressionType ctype = DebugCompressionType::None;
size_t compressedSize = sizeof(Elf_Chdr);
unsigned level = 0;
if (!(flags & SHF_ALLOC) && config->compressDebugSections &&
name.starts_with(".debug_"))
ctype = *config->compressDebugSections;
for (auto &[glob, t, l] : config->compressSections)
if (glob.match(name))
std::tie(ctype, level) = {t, l};
if (ctype == DebugCompressionType::None)
return;
if (flags & SHF_ALLOC) {
errorOrWarn("--compress-sections: section '" + name +
"' with the SHF_ALLOC flag cannot be compressed");
return;
}
llvm::TimeTraceScope timeScope("Compress sections");
auto buf = std::make_unique<uint8_t[]>(size);
{
parallel::TaskGroup tg;
writeTo<ELFT>(buf.get(), tg);
}
addralign = 1;
[[maybe_unused]] constexpr size_t shardSize = 1 << 20;
auto shardsIn = split(ArrayRef<uint8_t>(buf.get(), size), shardSize);
const size_t numShards = shardsIn.size();
auto shardsOut = std::make_unique<SmallVector<uint8_t, 0>[]>(numShards);
#if LLVM_ENABLE_ZSTD
if (ctype == DebugCompressionType::Zstd) {
parallelFor(0, numShards, [&](size_t i) {
SmallVector<uint8_t, 0> out;
ZSTD_CCtx *cctx = ZSTD_createCCtx();
ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level);
ZSTD_inBuffer zib = {shardsIn[i].data(), shardsIn[i].size(), 0};
ZSTD_outBuffer zob = {nullptr, 0, 0};
size_t size;
do {
if (zob.pos == zob.size) {
out.resize_for_overwrite(
zob.size ? zob.size * 3 / 2 : std::max<size_t>(zib.size / 4, 64));
zob = {out.data(), out.size(), zob.pos};
}
size = ZSTD_compressStream2(cctx, &zob, &zib, ZSTD_e_end);
assert(!ZSTD_isError(size));
} while (size != 0);
out.truncate(zob.pos);
ZSTD_freeCCtx(cctx);
shardsOut[i] = std::move(out);
});
compressed.type = ELFCOMPRESS_ZSTD;
for (size_t i = 0; i != numShards; ++i)
compressedSize += shardsOut[i].size();
}
#endif
#if LLVM_ENABLE_ZLIB
if (ctype == DebugCompressionType::Zlib) {
if (!level)
level = Z_BEST_SPEED;
auto shardsAdler = std::make_unique<uint32_t[]>(numShards);
parallelFor(0, numShards, [&](size_t i) {
shardsOut[i] = deflateShard(shardsIn[i], level,
i != numShards - 1 ? Z_SYNC_FLUSH : Z_FINISH);
shardsAdler[i] = adler32(1, shardsIn[i].data(), shardsIn[i].size());
});
uint32_t checksum = 1;
compressedSize += 2;
for (size_t i = 0; i != numShards; ++i) {
compressedSize += shardsOut[i].size();
checksum = adler32_combine(checksum, shardsAdler[i], shardsIn[i].size());
}
compressedSize += 4;
compressed.type = ELFCOMPRESS_ZLIB;
compressed.checksum = checksum;
}
#endif
if (compressedSize >= size)
return;
compressed.uncompressedSize = size;
compressed.shards = std::move(shardsOut);
compressed.numShards = numShards;
size = compressedSize;
flags |= SHF_COMPRESSED;
}
static void writeInt(uint8_t *buf, uint64_t data, uint64_t size) {
if (size == 1)
*buf = data;
else if (size == 2)
write16(buf, data);
else if (size == 4)
write32(buf, data);
else if (size == 8)
write64(buf, data);
else
llvm_unreachable("unsupported Size argument");
}
template <class ELFT>
void OutputSection::writeTo(uint8_t *buf, parallel::TaskGroup &tg) {
llvm::TimeTraceScope timeScope("Write sections", name);
if (type == SHT_NOBITS)
return;
if (type == SHT_CREL && !(flags & SHF_ALLOC)) {
buf += encodeULEB128(crelHeader, buf);
memcpy(buf, crelBody.data(), crelBody.size());
return;
}
if (compressed.shards) {
auto *chdr = reinterpret_cast<typename ELFT::Chdr *>(buf);
chdr->ch_type = compressed.type;
chdr->ch_size = compressed.uncompressedSize;
chdr->ch_addralign = addralign;
buf += sizeof(*chdr);
auto offsets = std::make_unique<size_t[]>(compressed.numShards);
if (compressed.type == ELFCOMPRESS_ZLIB) {
buf[0] = 0x78;
buf[1] = 0x01;
offsets[0] = 2;
write32be(buf + (size - sizeof(*chdr) - 4), compressed.checksum);
}
for (size_t i = 1; i != compressed.numShards; ++i)
offsets[i] = offsets[i - 1] + compressed.shards[i - 1].size();
parallelFor(0, compressed.numShards, [&](size_t i) {
memcpy(buf + offsets[i], compressed.shards[i].data(),
compressed.shards[i].size());
});
return;
}
ArrayRef<InputSection *> sections = getInputSections(*this, storage);
std::array<uint8_t, 4> filler = getFiller();
bool nonZeroFiller = read32(filler.data()) != 0;
if (nonZeroFiller)
fill(buf, sections.empty() ? size : sections[0]->outSecOff, filler);
if (type == SHT_CREL && !(flags & SHF_ALLOC)) {
buf += encodeULEB128(crelHeader, buf);
memcpy(buf, crelBody.data(), crelBody.size());
return;
}
auto fn = [=](size_t begin, size_t end) {
size_t numSections = sections.size();
for (size_t i = begin; i != end; ++i) {
InputSection *isec = sections[i];
if (auto *s = dyn_cast<SyntheticSection>(isec))
s->writeTo(buf + isec->outSecOff);
else
isec->writeTo<ELFT>(buf + isec->outSecOff);
if (config->emachine == EM_ARM && !config->isLE && config->armBe8 &&
(flags & SHF_EXECINSTR))
convertArmInstructionstoBE8(isec, buf + isec->outSecOff);
if (nonZeroFiller) {
uint8_t *start = buf + isec->outSecOff + isec->getSize();
uint8_t *end;
if (i + 1 == numSections)
end = buf + size;
else
end = buf + sections[i + 1]->outSecOff;
if (isec->nopFiller) {
assert(target->nopInstrs);
nopInstrFill(start, end - start);
} else
fill(start, end - start, filler);
}
}
};
bool written = false;
size_t numSections = sections.size();
for (SectionCommand *cmd : commands)
if (auto *data = dyn_cast<ByteCommand>(cmd)) {
if (!std::exchange(written, true))
fn(0, numSections);
writeInt(buf + data->offset, data->expression().getValue(), data->size);
}
if (written || !numSections)
return;
const size_t taskSizeLimit = 4 << 20;
for (size_t begin = 0, i = 0, taskSize = 0;;) {
taskSize += sections[i]->getSize();
bool done = ++i == numSections;
if (done || taskSize >= taskSizeLimit) {
tg.spawn([=] { fn(begin, i); });
if (done)
break;
begin = i;
taskSize = 0;
}
}
}
static void finalizeShtGroup(OutputSection *os, InputSection *section) {
os->link = in.symTab->getParent()->sectionIndex;
if (!section)
return;
ArrayRef<Symbol *> symbols = section->file->getSymbols();
os->info = in.symTab->getSymbolIndex(*symbols[section->info]);
DenseSet<uint32_t> seen;
ArrayRef<InputSectionBase *> sections = section->file->getSections();
for (const uint32_t &idx : section->getDataAs<uint32_t>().slice(1))
if (OutputSection *osec = sections[read32(&idx)]->getOutputSection())
seen.insert(osec->sectionIndex);
os->size = (1 + seen.size()) * sizeof(uint32_t);
}
template <class uint>
LLVM_ATTRIBUTE_ALWAYS_INLINE static void
encodeOneCrel(raw_svector_ostream &os, Elf_Crel<sizeof(uint) == 8> &out,
uint offset, const Symbol &sym, uint32_t type, uint addend) {
const auto deltaOffset = static_cast<uint64_t>(offset - out.r_offset);
out.r_offset = offset;
int64_t symidx = in.symTab->getSymbolIndex(sym);
if (sym.type == STT_SECTION) {
auto *d = dyn_cast<Defined>(&sym);
if (d) {
SectionBase *section = d->section;
assert(section->isLive());
addend = sym.getVA(addend) - section->getOutputSection()->addr;
} else {
symidx = type = addend = 0;
}
}
uint8_t b = deltaOffset * 8 + (out.r_symidx != symidx) +
(out.r_type != type ? 2 : 0) +
(uint(out.r_addend) != addend ? 4 : 0);
if (deltaOffset < 0x10) {
os << char(b);
} else {
os << char(b | 0x80);
encodeULEB128(deltaOffset >> 4, os);
}
if (b & 1) {
encodeSLEB128(static_cast<int32_t>(symidx - out.r_symidx), os);
out.r_symidx = symidx;
}
if (b & 2) {
encodeSLEB128(static_cast<int32_t>(type - out.r_type), os);
out.r_type = type;
}
if (b & 4) {
encodeSLEB128(std::make_signed_t<uint>(addend - out.r_addend), os);
out.r_addend = addend;
}
}
template <class ELFT>
static size_t relToCrel(raw_svector_ostream &os, Elf_Crel<ELFT::Is64Bits> &out,
InputSection *relSec, InputSectionBase *sec) {
const auto &file = *cast<ELFFileBase>(relSec->file);
if (relSec->type == SHT_REL) {
errorOrWarn(toString(relSec) + ": REL cannot be converted to CREL");
return 0;
}
auto rels = relSec->getDataAs<typename ELFT::Rela>();
for (auto rel : rels) {
encodeOneCrel<typename ELFT::uint>(
os, out, sec->getVA(rel.r_offset), file.getRelocTargetSym(rel),
rel.getType(config->isMips64EL), getAddend<ELFT>(rel));
}
return rels.size();
}
template <bool is64> void OutputSection::finalizeNonAllocCrel() {
using uint = typename Elf_Crel_Impl<is64>::uint;
raw_svector_ostream os(crelBody);
uint64_t totalCount = 0;
Elf_Crel<is64> out{};
assert(commands.size() == 1);
auto *isd = cast<InputSectionDescription>(commands[0]);
for (InputSection *relSec : isd->sections) {
const auto &file = *cast<ELFFileBase>(relSec->file);
InputSectionBase *sec = relSec->getRelocatedSection();
if (relSec->type == SHT_CREL) {
RelocsCrel<is64> entries(relSec->content_);
totalCount += entries.size();
for (Elf_Crel_Impl<is64> r : entries) {
encodeOneCrel<uint>(os, out, uint(sec->getVA(r.r_offset)),
file.getSymbol(r.r_symidx), r.r_type, r.r_addend);
}
continue;
}
if constexpr (is64) {
totalCount += config->isLE ? relToCrel<ELF64LE>(os, out, relSec, sec)
: relToCrel<ELF64BE>(os, out, relSec, sec);
} else {
totalCount += config->isLE ? relToCrel<ELF32LE>(os, out, relSec, sec)
: relToCrel<ELF32BE>(os, out, relSec, sec);
}
}
crelHeader = totalCount * 8 + 4;
size = getULEB128Size(crelHeader) + crelBody.size();
}
void OutputSection::finalize() {
InputSection *first = getFirstInputSection(this);
if (flags & SHF_LINK_ORDER) {
if (auto *ex = dyn_cast<ARMExidxSyntheticSection>(first))
link = ex->getLinkOrderDep()->getParent()->sectionIndex;
else if (first->flags & SHF_LINK_ORDER)
if (auto *d = first->getLinkOrderDep())
link = d->getParent()->sectionIndex;
}
if (type == SHT_GROUP) {
finalizeShtGroup(this, first);
return;
}
if (!config->copyRelocs || !isStaticRelSecType(type))
return;
if (!first || isa<SyntheticSection>(first))
return;
link = in.symTab->getParent()->sectionIndex;
InputSectionBase *s = first->getRelocatedSection();
info = s->getOutputSection()->sectionIndex;
flags |= SHF_INFO_LINK;
if (type == SHT_CREL) {
if (config->is64)
finalizeNonAllocCrel<true>();
else
finalizeNonAllocCrel<false>();
}
}
static bool isCrt(StringRef s, StringRef beginEnd) {
s = sys::path::filename(s);
if (!s.consume_back(".o"))
return false;
if (s.consume_front("clang_rt."))
return s.consume_front(beginEnd);
return s.consume_front(beginEnd) && s.size() <= 1;
}
static bool compCtors(const InputSection *a, const InputSection *b) {
bool beginA = isCrt(a->file->getName(), "crtbegin");
bool beginB = isCrt(b->file->getName(), "crtbegin");
if (beginA != beginB)
return beginA;
bool endA = isCrt(a->file->getName(), "crtend");
bool endB = isCrt(b->file->getName(), "crtend");
if (endA != endB)
return endB;
return getPriority(a->name) > getPriority(b->name);
}
void OutputSection::sortCtorsDtors() {
assert(commands.size() == 1);
auto *isd = cast<InputSectionDescription>(commands[0]);
llvm::stable_sort(isd->sections, compCtors);
}
int elf::getPriority(StringRef s) {
size_t pos = s.rfind('.');
if (pos == StringRef::npos)
return 65536;
int v = 65536;
if (to_integer(s.substr(pos + 1), v, 10) &&
(pos == 6 && (s.starts_with(".ctors") || s.starts_with(".dtors"))))
v = 65535 - v;
return v;
}
InputSection *elf::getFirstInputSection(const OutputSection *os) {
for (SectionCommand *cmd : os->commands)
if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
if (!isd->sections.empty())
return isd->sections[0];
return nullptr;
}
ArrayRef<InputSection *>
elf::getInputSections(const OutputSection &os,
SmallVector<InputSection *, 0> &storage) {
ArrayRef<InputSection *> ret;
storage.clear();
for (SectionCommand *cmd : os.commands) {
auto *isd = dyn_cast<InputSectionDescription>(cmd);
if (!isd)
continue;
if (ret.empty()) {
ret = isd->sections;
} else {
if (storage.empty())
storage.assign(ret.begin(), ret.end());
storage.insert(storage.end(), isd->sections.begin(), isd->sections.end());
}
}
return storage.empty() ? ret : ArrayRef(storage);
}
void OutputSection::sortInitFini() {
sort([](InputSectionBase *s) { return getPriority(s->name); });
}
std::array<uint8_t, 4> OutputSection::getFiller() {
if (filler)
return *filler;
if (flags & SHF_EXECINSTR)
return target->trapInstr;
return {0, 0, 0, 0};
}
void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
assert(config->writeAddends && config->checkDynamicRelocs);
assert(isStaticRelSecType(type));
SmallVector<InputSection *, 0> storage;
ArrayRef<InputSection *> sections = getInputSections(*this, storage);
parallelFor(0, sections.size(), [&](size_t i) {
const auto *sec = dyn_cast<RelocationBaseSection>(sections[i]);
if (!sec)
return;
for (const DynamicReloc &rel : sec->relocs) {
int64_t addend = rel.addend;
const OutputSection *relOsec = rel.inputSec->getOutputSection();
assert(relOsec != nullptr && "missing output section for relocation");
if (is_contained({EM_PPC, EM_PPC64}, config->emachine) &&
(rel.inputSec == in.ppc64LongBranchTarget.get() ||
rel.inputSec == in.igotPlt.get()))
continue;
const uint8_t *relocTarget =
bufStart + relOsec->offset + rel.inputSec->getOffset(rel.offsetInSec);
int64_t writtenAddend =
relOsec->type == SHT_NOBITS
? 0
: target->getImplicitAddend(relocTarget, rel.type);
if (addend != writtenAddend)
internalLinkerError(
getErrorLocation(relocTarget),
"wrote incorrect addend value 0x" + utohexstr(writtenAddend) +
" instead of 0x" + utohexstr(addend) +
" for dynamic relocation " + toString(rel.type) +
" at offset 0x" + utohexstr(rel.getOffset()) +
(rel.sym ? " against symbol " + toString(*rel.sym) : ""));
}
});
}
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
template void OutputSection::writeTo<ELF32LE>(uint8_t *,
llvm::parallel::TaskGroup &);
template void OutputSection::writeTo<ELF32BE>(uint8_t *,
llvm::parallel::TaskGroup &);
template void OutputSection::writeTo<ELF64LE>(uint8_t *,
llvm::parallel::TaskGroup &);
template void OutputSection::writeTo<ELF64BE>(uint8_t *,
llvm::parallel::TaskGroup &);
template void OutputSection::maybeCompress<ELF32LE>();
template void OutputSection::maybeCompress<ELF32BE>();
template void OutputSection::maybeCompress<ELF64LE>();
template void OutputSection::maybeCompress<ELF64BE>();