[lld-macho][nfc] Remove InputSection::outSecFileOff
authorJez Ng <jezng@fb.com>
Sun, 13 Jun 2021 23:43:34 +0000 (19:43 -0400)
committerJez Ng <jezng@fb.com>
Sun, 13 Jun 2021 23:51:30 +0000 (19:51 -0400)
`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

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

index dc12722d336d15c3d8b56e9742e55e3dfc7c684e..7926503cfee57142b865f9095797b5e3bd088657 100644 (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;
     }
   }
index 909fe69f1605ab68c245e1aa886aaaa2a9e5af2c..9ae9ca34286f09b1e4f3d0f8b6a4ed42a8a20c26 100644 (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();
index fa4292f06e5c155eb9dd993c5aaf1d456eb9df35..24a1800a75e85e43f3252a0ddad55ed92e9a2637 100644 (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];
index ccb57ae2eafd199a68a685046004e9b024174e7c..26a4a873677b843580920833a52bc16786492d5c 100644 (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();
 }
index 02e27e1a45663ad235bef82eea795086c86e733a..3d3c84c799526c1f6dc6ae40692403723e1b2871 100644 (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; }
 
index 80f6212d97e3a72b440b546910aa2d6bac1bd9fb..8b3db310ca15220429c98909ac31873c3c3a5648 100644 (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) {
index b5048c463a40414a04fa6441bbfb706e7964e544..6626b527139ec292310917eaf9f6a32d809ebdc2 100644 (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;
   }