[lld-macho][nfc] Remove InputSection::outSecFileOff

`outSecFileOff` and the associated `getFileOffset()` accessors were
unnecessary.

For all the cases we care about, `outSecFileOff` is the same as
`outSecOff`. The only time they deviate is if there are zerofill
sections within a given segment. But since zerofill sections are always
at the end of a segment, the only sections where the two values deviate
are zerofill sections themselves. And we never actually query the
outSecFileOff of zerofill sections.

As for `getFileOffset()`, the only place it was being used was to
calculate the offset of the entry symbol. However, we can compute that
value by just taking the difference between the address of the entry
symbol and the address of the Mach-O header. In fact, this appears to be
what ld64 itself does. This difference is the same as the file offset as
long as there are no intervening zerofill sections, but since `__text`
is the first section in `__TEXT`, this never happens, so our previous
use of `getFileOffset()` was not wrong -- just inefficient.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D104177
This commit is contained in:
Jez Ng 2021-06-13 19:43:34 -04:00
parent f09e200b31
commit b2a0739012
7 changed files with 4 additions and 36 deletions

View File

@ -192,7 +192,6 @@ void ConcatOutputSection::finalize() {
isecAddr = alignTo(isecAddr, isec->align);
isecFileOff = alignTo(isecFileOff, isec->align);
isec->outSecOff = isecAddr - addr;
isec->outSecFileOff = isecFileOff - fileOff;
isec->isFinal = true;
isecAddr += isec->getSize();
isecFileOff += isec->getFileSize();
@ -323,11 +322,11 @@ void ConcatOutputSection::writeTo(uint8_t *buf) const {
while (i < ie || t < te) {
while (i < ie && (t == te || inputs[i]->getSize() == 0 ||
inputs[i]->outSecOff < thunks[t]->outSecOff)) {
inputs[i]->writeTo(buf + inputs[i]->outSecFileOff);
inputs[i]->writeTo(buf + inputs[i]->outSecOff);
++i;
}
while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) {
thunks[t]->writeTo(buf + thunks[t]->outSecFileOff);
thunks[t]->writeTo(buf + thunks[t]->outSecOff);
++t;
}
}

View File

@ -25,10 +25,6 @@ using namespace lld::macho;
std::vector<InputSection *> macho::inputSections;
uint64_t ConcatInputSection::getFileOffset(uint64_t off) const {
return parent->fileOff + outSecFileOff + off;
}
uint64_t InputSection::getFileSize() const {
return isZeroFill(flags) ? 0 : getSize();
}
@ -119,10 +115,6 @@ const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
return const_cast<CStringInputSection *>(this)->getStringPiece(off);
}
uint64_t CStringInputSection::getFileOffset(uint64_t off) const {
return parent->fileOff + getOffset(off);
}
uint64_t CStringInputSection::getOffset(uint64_t off) const {
const StringPiece &piece = getStringPiece(off);
uint64_t addend = off - piece.inSecOff;
@ -152,10 +144,6 @@ WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
}
uint64_t WordLiteralInputSection::getFileOffset(uint64_t off) const {
return parent->fileOff + getOffset(off);
}
uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
auto *osec = cast<WordLiteralSection>(parent);
const uint8_t *buf = data.data();

View File

@ -40,7 +40,6 @@ public:
// offset from the beginning of its parent OutputSection.
virtual uint64_t getOffset(uint64_t off) const = 0;
// The offset from the beginning of the file.
virtual uint64_t getFileOffset(uint64_t off) const = 0;
uint64_t getVA(uint64_t off) const;
// Whether the data at \p off in this InputSection is live.
virtual bool isLive(uint64_t off) const = 0;
@ -86,7 +85,6 @@ public:
ArrayRef<uint8_t> data, uint32_t align, uint32_t flags)
: InputSection(ConcatKind, segname, name, file, data, align, flags) {}
uint64_t getFileOffset(uint64_t off) const override;
uint64_t getOffset(uint64_t off) const override { return outSecOff + off; }
uint64_t getVA() const { return InputSection::getVA(0); }
// ConcatInputSections are entirely live or dead, so the offset is irrelevant.
@ -110,7 +108,6 @@ public:
// How many symbols refer to this InputSection.
uint32_t numRefs = 0;
uint64_t outSecOff = 0;
uint64_t outSecFileOff = 0;
};
// We allocate a lot of these and binary search on them, so they should be as
@ -145,7 +142,6 @@ public:
ArrayRef<uint8_t> data, uint32_t align, uint32_t flags)
: InputSection(CStringLiteralKind, segname, name, file, data, align,
flags) {}
uint64_t getFileOffset(uint64_t off) const override;
uint64_t getOffset(uint64_t off) const override;
bool isLive(uint64_t off) const override { return getStringPiece(off).live; }
void markLive(uint64_t off) override { getStringPiece(off).live = true; }
@ -177,7 +173,6 @@ public:
WordLiteralInputSection(StringRef segname, StringRef name, InputFile *file,
ArrayRef<uint8_t> data, uint32_t align,
uint32_t flags);
uint64_t getFileOffset(uint64_t off) const override;
uint64_t getOffset(uint64_t off) const override;
bool isLive(uint64_t off) const override {
return live[off >> power2LiteralSize];

View File

@ -71,15 +71,6 @@ uint64_t Defined::getVA() const {
return isec->getVA(value);
}
uint64_t Defined::getFileOffset() const {
if (isAbsolute()) {
error("absolute symbol " + toString(*this) +
" does not have a file offset");
return 0;
}
return isec->getFileOffset(value);
}
uint64_t DylibSymbol::getVA() const {
return isInStubs() ? getStubVA() : Symbol::getVA();
}

View File

@ -55,10 +55,6 @@ public:
virtual uint64_t getVA() const { return 0; }
virtual uint64_t getFileOffset() const {
llvm_unreachable("attempt to get an offset from a non-defined symbol");
}
virtual bool isWeakDef() const { llvm_unreachable("cannot be weak def"); }
// Only undefined or dylib symbols can be weak references. A weak reference
@ -140,7 +136,6 @@ public:
bool isAbsolute() const { return isec == nullptr; }
uint64_t getVA() const override;
uint64_t getFileOffset() const override;
static bool classof(const Symbol *s) { return s->kind() == DefinedKind; }

View File

@ -218,7 +218,7 @@ relocateCompactUnwind(ConcatOutputSection *compactUnwindSection,
assert(isec->parent == compactUnwindSection);
uint8_t *buf =
reinterpret_cast<uint8_t *>(cuVector.data()) + isec->outSecFileOff;
reinterpret_cast<uint8_t *>(cuVector.data()) + isec->outSecOff;
memcpy(buf, isec->data.data(), isec->data.size());
for (const Reloc &r : isec->relocs) {

View File

@ -240,7 +240,7 @@ class LCMain : public LoadCommand {
c->entryoff =
in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
else
c->entryoff = config->entry->getFileOffset();
c->entryoff = config->entry->getVA() - in.header->addr;
c->stacksize = 0;
}