2020-04-03 02:54:05 +08:00
|
|
|
//===- InputSection.cpp ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputSection.h"
|
2020-08-13 10:50:09 +08:00
|
|
|
#include "InputFiles.h"
|
2020-04-28 03:50:59 +08:00
|
|
|
#include "OutputSegment.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "Symbols.h"
|
2021-01-19 23:44:42 +08:00
|
|
|
#include "SyntheticSections.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "Target.h"
|
2021-01-09 07:47:40 +08:00
|
|
|
#include "Writer.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
2020-05-19 06:46:33 +08:00
|
|
|
using namespace llvm;
|
2020-04-03 02:54:05 +08:00
|
|
|
using namespace llvm::MachO;
|
|
|
|
using namespace llvm::support;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
|
|
|
std::vector<InputSection *> macho::inputSections;
|
|
|
|
|
2020-04-28 03:50:59 +08:00
|
|
|
uint64_t InputSection::getFileOffset() const {
|
2020-05-02 07:29:06 +08:00
|
|
|
return parent->fileOff + outSecFileOff;
|
2020-04-28 03:50:59 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 11:57:37 +08:00
|
|
|
uint64_t InputSection::getFileSize() const {
|
|
|
|
return isZeroFill(flags) ? 0 : getSize();
|
|
|
|
}
|
|
|
|
|
2020-05-02 07:29:06 +08:00
|
|
|
uint64_t InputSection::getVA() const { return parent->addr + outSecOff; }
|
|
|
|
|
2021-01-19 23:44:42 +08:00
|
|
|
static uint64_t resolveSymbolVA(uint8_t *loc, const lld::macho::Symbol &sym,
|
|
|
|
uint8_t type) {
|
|
|
|
const TargetInfo::RelocAttrs &relocAttrs = target->getRelocAttrs(type);
|
|
|
|
if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
|
|
|
|
if (sym.isInStubs())
|
|
|
|
return in.stubs->addr + sym.stubsIndex * target->stubSize;
|
|
|
|
} else if (relocAttrs.hasAttr(RelocAttrBits::GOT | RelocAttrBits::LOAD)) {
|
|
|
|
if (sym.isInGot())
|
|
|
|
return in.got->addr + sym.gotIndex * WordSize;
|
|
|
|
} else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
|
|
|
|
return in.got->addr + sym.gotIndex * WordSize;
|
|
|
|
} else if (relocAttrs.hasAttr(RelocAttrBits::TLV | RelocAttrBits::LOAD)) {
|
|
|
|
if (sym.isInGot())
|
|
|
|
return in.tlvPointers->addr + sym.gotIndex * WordSize;
|
|
|
|
assert(isa<Defined>(&sym));
|
|
|
|
}
|
|
|
|
return sym.getVA();
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
void InputSection::writeTo(uint8_t *buf) {
|
[lld-macho] Ensure __bss sections we output have file offset of zero
Summary:
llvm-mc emits `__bss` sections with an offset of zero, but we weren't expecting
that in our input, so we were copying non-zero data from the start of the file and
putting it in `__bss`, with obviously undesirable runtime results. (It appears that
the kernel will copy those nonzero bytes as long as the offset is nonzero, regardless
of whether S_ZERO_FILL is set.)
I debated on whether to make a special ZeroFillSection -- separate from a
regular InputSection -- but it seemed like too much work for now. But I'm happy
to refactor if anyone feels strongly about having it as a separate class.
Depends on D80857.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Reviewed By: smeenai
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80859
2020-06-14 11:00:36 +08:00
|
|
|
if (getFileSize() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(buf, data.data(), data.size());
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-01-19 23:44:42 +08:00
|
|
|
for (size_t i = 0; i < relocs.size(); i++) {
|
|
|
|
const Reloc &r = relocs[i];
|
|
|
|
uint8_t *loc = buf + r.offset;
|
|
|
|
auto *fromSym = target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)
|
|
|
|
? relocs[i++].referent.dyn_cast<Symbol *>()
|
|
|
|
: nullptr;
|
2020-09-13 11:45:00 +08:00
|
|
|
uint64_t referentVA = 0;
|
2021-01-19 23:44:42 +08:00
|
|
|
if (fromSym) {
|
|
|
|
auto *toSym = r.referent.dyn_cast<Symbol *>();
|
|
|
|
referentVA = toSym->getVA() - fromSym->getVA();
|
|
|
|
} else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
|
|
|
|
if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
|
|
|
|
!referentSym->isInGot())
|
|
|
|
target->relaxGotLoad(loc, r.type);
|
|
|
|
referentVA = resolveSymbolVA(loc, *referentSym, r.type);
|
2020-08-08 02:04:52 +08:00
|
|
|
|
|
|
|
if (isThreadLocalVariables(flags)) {
|
2021-01-09 07:47:40 +08:00
|
|
|
// References from thread-local variable sections are treated as offsets
|
|
|
|
// relative to the start of the thread-local data memory area, which
|
|
|
|
// is initialized via copying all the TLV data sections (which are all
|
|
|
|
// contiguous).
|
2021-01-14 05:32:40 +08:00
|
|
|
if (isa<Defined>(referentSym))
|
2021-01-09 07:47:40 +08:00
|
|
|
referentVA -= firstTLVDataSection->addr;
|
2020-08-08 02:04:52 +08:00
|
|
|
}
|
2020-09-13 11:45:00 +08:00
|
|
|
} else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
|
|
|
|
referentVA = referentIsec->getVA();
|
2020-08-08 02:04:52 +08:00
|
|
|
}
|
2021-01-19 23:44:42 +08:00
|
|
|
target->relocateOne(loc, r, referentVA, getVA() + r.offset);
|
2020-04-03 02:54:05 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 10:50:09 +08:00
|
|
|
|
2020-12-02 06:45:13 +08:00
|
|
|
bool macho::isCodeSection(InputSection *isec) {
|
|
|
|
uint32_t type = isec->flags & MachO::SECTION_TYPE;
|
|
|
|
if (type != S_REGULAR && type != S_COALESCED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32_t attr = isec->flags & MachO::SECTION_ATTRIBUTES_USR;
|
|
|
|
if (attr == S_ATTR_PURE_INSTRUCTIONS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isec->segname == segment_names::text)
|
|
|
|
return StringSwitch<bool>(isec->name)
|
|
|
|
.Cases("__textcoal_nt", "__StaticInit", true)
|
|
|
|
.Default(false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
std::string lld::toString(const InputSection *isec) {
|
|
|
|
return (toString(isec->file) + ":(" + isec->name + ")").str();
|
|
|
|
}
|