[PATCH] D79460: [lld-macho] Follow-up to D77893

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 5 17:49:39 PDT 2020


int3 created this revision.
int3 added reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
int3 added a child revision: D78270: [lld-macho] Support calls to functions in dylibs.

1. Don't have isHidden() depend on isNeeded(). Whether a section is hidden is orthogonal from whether it is needed: hidden sections will never have a header regardless of whether they have a body. (I know we override this method with return false for synthetic sections, but regardless I think it's confusing to write it this way for non-synthetic sections.)

2. Don't call writeTo() on unneeded sections. D78270 <https://reviews.llvm.org/D78270> assumes that this is true when implementing the stub helper section.

3. Remove assumption in test that the referenced file has no other symbols. (We should create separate input files for future tests to avoid such issues.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79460

Files:
  lld/MachO/OutputSection.h
  lld/MachO/OutputSegment.cpp
  lld/MachO/Writer.cpp
  lld/test/MachO/section-merge.s


Index: lld/test/MachO/section-merge.s
===================================================================
--- lld/test/MachO/section-merge.s
+++ lld/test/MachO/section-merge.s
@@ -23,9 +23,9 @@
 # DATA:        {{0*}}[[#%x,BASE:]] <_some_function>:
 # DATA-NEXT:             [[#BASE]]: 48 c7 c0 01 00 00 00          movq    $1, %rax
 # DATA-NEXT:       [[#BASE + 0x7]]: c3                            retq
-# DATA:      {{0*}}[[#BASE + 0x8]] <_main>:
-# DATA-NEXT:       [[#BASE + 0x8]]: 48 c7 c0 00 00 00 00          movq    $0, %rax
-# DATA-NEXT:       [[#BASE + 0xf]]: c3                            retq
+# DATA:        {{0*}}[[#%x,MAIN:]] <_main>:
+# DATA-NEXT:             [[#MAIN]]: 48 c7 c0 00 00 00 00          movq    $0, %rax
+# DATA-NEXT:       [[#MAIN + 0x7]]: c3                            retq
 
 .section __TEXT,__text
 .global _main
Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -114,7 +114,7 @@
     c->maxprot = seg->maxProt;
     c->initprot = seg->initProt;
 
-    if (!seg->isNeeded())
+    if (seg->getSections().empty())
       return;
 
     c->vmaddr = seg->firstSection()->addr;
@@ -125,6 +125,9 @@
     for (auto &p : seg->getSections()) {
       StringRef s = p.first;
       OutputSection *section = p.second;
+      if (!section->isNeeded())
+        continue;
+
       c->filesize += section->getFileSize();
       if (section->isHidden())
         continue;
@@ -366,7 +369,8 @@
   for (OutputSegment *seg : outputSegments) {
     for (auto &p : seg->getSections()) {
       OutputSection *section = p.second;
-      section->writeTo(buf + section->fileOff);
+      if (section->isNeeded())
+        section->writeTo(buf + section->fileOff);
     }
   }
 }
Index: lld/MachO/OutputSegment.cpp
===================================================================
--- lld/MachO/OutputSegment.cpp
+++ lld/MachO/OutputSegment.cpp
@@ -40,7 +40,7 @@
   size_t count = 0;
   for (const OutputSegment::SectionMapEntry &i : sections) {
     OutputSection *os = i.second;
-    count += (os->isHidden() ? 0 : 1);
+    count += (os->isNeeded() && !os->isHidden() ? 1 : 0);
   }
   return count;
 }
@@ -138,7 +138,7 @@
     seg->sortOutputSections(&comparator);
     for (auto &p : seg->getSections()) {
       OutputSection *section = p.second;
-      if (!section->isHidden()) {
+      if (section->isNeeded() && !section->isHidden()) {
         section->index = ++sectionIndex;
       }
     }
Index: lld/MachO/OutputSection.h
===================================================================
--- lld/MachO/OutputSection.h
+++ lld/MachO/OutputSection.h
@@ -36,8 +36,8 @@
   // as-is so their file size is the same as their address space size.
   virtual uint64_t getFileSize() const { return getSize(); }
 
-  // Hidden sections omit header content, but body content is still present.
-  virtual bool isHidden() const { return !this->isNeeded(); }
+  // Hidden sections omit header content, but body content may still be present.
+  virtual bool isHidden() const { return false; }
   // Unneeded sections are omitted entirely (header and body).
   virtual bool isNeeded() const { return true; }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79460.262271.patch
Type: text/x-patch
Size: 3219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200506/8a0cfbc9/attachment.bin>


More information about the llvm-commits mailing list