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

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 13 16:52:19 PDT 2021


Author: Jez Ng
Date: 2021-06-13T19:51:30-04:00
New Revision: b2a0739012163f5433c0f069e842ee4cea3100f7

URL: https://github.com/llvm/llvm-project/commit/b2a0739012163f5433c0f069e842ee4cea3100f7
DIFF: https://github.com/llvm/llvm-project/commit/b2a0739012163f5433c0f069e842ee4cea3100f7.diff

LOG: [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

Added: 
    

Modified: 
    lld/MachO/ConcatOutputSection.cpp
    lld/MachO/InputSection.cpp
    lld/MachO/InputSection.h
    lld/MachO/Symbols.cpp
    lld/MachO/Symbols.h
    lld/MachO/UnwindInfoSection.cpp
    lld/MachO/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/ConcatOutputSection.cpp b/lld/MachO/ConcatOutputSection.cpp
index dc12722d336d..7926503cfee5 100644
--- a/lld/MachO/ConcatOutputSection.cpp
+++ b/lld/MachO/ConcatOutputSection.cpp
@@ -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;
     }
   }

diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 909fe69f1605..9ae9ca34286f 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -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();

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index fa4292f06e5c..24a1800a75e8 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -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;
@@ -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.
@@ -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
@@ -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; }
@@ -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];

diff  --git a/lld/MachO/Symbols.cpp b/lld/MachO/Symbols.cpp
index ccb57ae2eafd..26a4a873677b 100644
--- a/lld/MachO/Symbols.cpp
+++ b/lld/MachO/Symbols.cpp
@@ -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();
 }

diff  --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h
index 02e27e1a4566..3d3c84c79952 100644
--- a/lld/MachO/Symbols.h
+++ b/lld/MachO/Symbols.h
@@ -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
@@ -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; }
 

diff  --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp
index 80f6212d97e3..8b3db310ca15 100644
--- a/lld/MachO/UnwindInfoSection.cpp
+++ b/lld/MachO/UnwindInfoSection.cpp
@@ -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) {

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index b5048c463a40..6626b527139e 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -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;
   }


        


More information about the llvm-commits mailing list