Skip to content

Commit

Permalink
[lld-macho][nfc] Remove InputSection::outSecFileOff
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
int3 committed Jun 13, 2021
1 parent f09e200 commit b2a0739
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 36 deletions.
5 changes: 2 additions & 3 deletions lld/MachO/ConcatOutputSection.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down
12 changes: 0 additions & 12 deletions lld/MachO/InputSection.cpp
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions lld/MachO/InputSection.h
Expand Up @@ -40,7 +40,6 @@ class InputSection {
// 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;
Expand Down Expand Up @@ -86,7 +85,6 @@ class ConcatInputSection : public InputSection {
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.
Expand All @@ -110,7 +108,6 @@ class ConcatInputSection : public InputSection {
// 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
Expand Down Expand Up @@ -145,7 +142,6 @@ class CStringInputSection : public InputSection {
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; }
Expand Down Expand Up @@ -177,7 +173,6 @@ class WordLiteralInputSection : public InputSection {
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];
Expand Down
9 changes: 0 additions & 9 deletions lld/MachO/Symbols.cpp
Expand Up @@ -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();
}
Expand Down
5 changes: 0 additions & 5 deletions lld/MachO/Symbols.h
Expand Up @@ -55,10 +55,6 @@ class Symbol {

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
Expand Down Expand Up @@ -140,7 +136,6 @@ class Defined : public Symbol {
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; }

Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/UnwindInfoSection.cpp
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/Writer.cpp
Expand Up @@ -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;
}
Expand Down

0 comments on commit b2a0739

Please sign in to comment.