[lld] 681cfeb - [lld-macho][nfc] Have InputSection ctors take some parameters

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 11 16:50:40 PDT 2021


Author: Jez Ng
Date: 2021-06-11T19:50:09-04:00
New Revision: 681cfeb59119f2ddc5418e4e3a48c632dcd1aa3e

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

LOG: [lld-macho][nfc] Have InputSection ctors take some parameters

This is motivated by an upcoming diff in which the
WordLiteralInputSection ctor sets itself up based on the value of its
section flags. As such, it needs to be passed the `flags` value as part
of its ctor parameters, instead of having them assigned after the fact
in `parseSection()`. While refactoring code to make that possible, I
figured it would make sense for the other InputSections to also take
their initial values as ctor parameters.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D103978

Added: 
    

Modified: 
    lld/MachO/ConcatOutputSection.cpp
    lld/MachO/Driver.cpp
    lld/MachO/InputFiles.cpp
    lld/MachO/InputSection.cpp
    lld/MachO/InputSection.h
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/ConcatOutputSection.cpp b/lld/MachO/ConcatOutputSection.cpp
index 3b5a31d8be70..dc12722d336d 100644
--- a/lld/MachO/ConcatOutputSection.cpp
+++ b/lld/MachO/ConcatOutputSection.cpp
@@ -290,9 +290,7 @@ void ConcatOutputSection::finalize() {
         // unfinalized inputs[finalIdx].
         fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
       }
-      thunkInfo.isec = make<ConcatInputSection>();
-      thunkInfo.isec->name = isec->name;
-      thunkInfo.isec->segname = isec->segname;
+      thunkInfo.isec = make<ConcatInputSection>(isec->segname, isec->name);
       thunkInfo.isec->parent = this;
       StringRef thunkName = saver.save(funcSym->getName() + ".thunk." +
                                        std::to_string(thunkInfo.sequence++));

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 23c297b7cfe7..4fc269320b7b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -533,10 +533,9 @@ static void replaceCommonSymbols() {
     if (common == nullptr)
       continue;
 
-    auto *isec = make<ConcatInputSection>();
+    auto *isec =
+        make<ConcatInputSection>(segment_names::data, section_names::common);
     isec->file = common->getFile();
-    isec->name = section_names::common;
-    isec->segname = segment_names::data;
     isec->align = common->align;
     // Casting to size_t will truncate large values on 32-bit architectures,
     // but it's not really worth supporting the linking of 64-bit programs on

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 4d93878955bf..0b5129295476 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -241,30 +241,25 @@ Optional<MemoryBufferRef> macho::readFile(StringRef path) {
 InputFile::InputFile(Kind kind, const InterfaceFile &interface)
     : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
 
-template <class Section>
-static void parseSection(ObjFile *file, const uint8_t *buf, const Section &sec,
-                         InputSection *isec) {
-  isec->file = file;
-  isec->name =
-      StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
-  isec->segname =
-      StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
-  isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset,
-                static_cast<size_t>(sec.size)};
-  if (sec.align >= 32)
-    error("alignment " + std::to_string(sec.align) + " of section " +
-          isec->name + " is too large");
-  else
-    isec->align = 1 << sec.align;
-  isec->flags = sec.flags;
-}
-
 template <class Section>
 void ObjFile::parseSections(ArrayRef<Section> sections) {
   subsections.reserve(sections.size());
   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
 
   for (const Section &sec : sections) {
+    StringRef name =
+        StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
+    StringRef segname =
+        StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
+    ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
+                                                    : buf + sec.offset,
+                              static_cast<size_t>(sec.size)};
+    if (sec.align >= 32)
+      error("alignment " + std::to_string(sec.align) + " of section " + name +
+            " is too large");
+    uint32_t align = 1 << sec.align;
+    uint32_t flags = sec.flags;
+
     if (config->dedupLiterals &&
         (sectionType(sec.flags) == S_CSTRING_LITERALS ||
          isWordLiteralSection(sec.flags))) {
@@ -276,18 +271,18 @@ void ObjFile::parseSections(ArrayRef<Section> sections) {
 
       InputSection *isec;
       if (sectionType(sec.flags) == S_CSTRING_LITERALS) {
-        isec = make<CStringInputSection>();
-        parseSection(this, buf, sec, isec);
+        isec =
+            make<CStringInputSection>(segname, name, this, data, align, flags);
         // FIXME: parallelize this?
         cast<CStringInputSection>(isec)->splitIntoPieces();
       } else {
-        isec = make<WordLiteralInputSection>();
-        parseSection(this, buf, sec, isec);
+        isec = make<WordLiteralInputSection>(segname, name, this, data, align,
+                                             flags);
       }
       subsections.push_back({{0, isec}});
     } else {
-      auto *isec = make<ConcatInputSection>();
-      parseSection(this, buf, sec, isec);
+      auto *isec =
+          make<ConcatInputSection>(segname, name, this, data, align, flags);
       if (!(isDebugSection(isec->flags) &&
             isec->segname == segment_names::dwarf)) {
         subsections.push_back({{0, isec}});
@@ -667,10 +662,9 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
                        StringRef sectName)
     : InputFile(OpaqueKind, mb) {
-  ConcatInputSection *isec = make<ConcatInputSection>();
+  ConcatInputSection *isec =
+      make<ConcatInputSection>(segName.take_front(16), sectName.take_front(16));
   isec->file = this;
-  isec->name = sectName.take_front(16);
-  isec->segname = segName.take_front(16);
   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
   isec->data = {buf, mb.getBufferSize()};
   isec->live = true;

diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 29e504585070..42f4a988731b 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -127,6 +127,13 @@ uint64_t CStringInputSection::getOffset(uint64_t off) const {
   return piece.outSecOff + addend;
 }
 
+WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
+                                                 StringRef name,
+                                                 InputFile *file,
+                                                 ArrayRef<uint8_t> data,
+                                                 uint32_t align, uint32_t flags)
+    : InputSection(WordLiteralKind, segname, name, file, data, align, flags) {}
+
 uint64_t WordLiteralInputSection::getFileOffset(uint64_t off) const {
   return parent->fileOff + getOffset(off);
 }

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index bd9a2323a1fa..44ee1f998ec6 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -62,7 +62,13 @@ class InputSection {
   std::vector<Reloc> relocs;
 
 protected:
-  explicit InputSection(Kind kind) : sectionKind(kind) {}
+  InputSection(Kind kind, StringRef segname, StringRef name)
+      : name(name), segname(segname), sectionKind(kind) {}
+
+  InputSection(Kind kind, StringRef segname, StringRef name, InputFile *file,
+               ArrayRef<uint8_t> data, uint32_t align, uint32_t flags)
+      : file(file), name(name), segname(segname), align(align), flags(flags),
+        data(data), sectionKind(kind) {}
 
 private:
   Kind sectionKind;
@@ -73,7 +79,13 @@ class InputSection {
 // contents merged before output.
 class ConcatInputSection : public InputSection {
 public:
-  ConcatInputSection() : InputSection(ConcatKind) {}
+  ConcatInputSection(StringRef segname, StringRef name)
+      : InputSection(ConcatKind, segname, name) {}
+
+  ConcatInputSection(StringRef segname, StringRef name, InputFile *file,
+                     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); }
@@ -123,7 +135,10 @@ struct StringPiece {
 // conservative behavior we can certainly implement that.
 class CStringInputSection : public InputSection {
 public:
-  CStringInputSection() : InputSection(CStringLiteralKind) {}
+  CStringInputSection(StringRef segname, StringRef name, InputFile *file,
+                      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;
   // FIXME implement this
@@ -152,7 +167,9 @@ class CStringInputSection : public InputSection {
 
 class WordLiteralInputSection : public InputSection {
 public:
-  WordLiteralInputSection() : InputSection(WordLiteralKind) {}
+  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;
   // FIXME implement this

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 570a2dac71c8..9183f1820d85 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -48,9 +48,7 @@ std::vector<SyntheticSection *> macho::syntheticSections;
 
 SyntheticSection::SyntheticSection(const char *segname, const char *name)
     : OutputSection(SyntheticKind, name), segname(segname) {
-  isec = make<ConcatInputSection>();
-  isec->segname = segname;
-  isec->name = name;
+  isec = make<ConcatInputSection>(segname, name);
   isec->parent = this;
   syntheticSections.push_back(this);
 }
@@ -479,9 +477,8 @@ void StubHelperSection::setup() {
                     /*noDeadStrip=*/false);
 }
 
-ImageLoaderCacheSection::ImageLoaderCacheSection() {
-  segname = segment_names::data;
-  name = section_names::data;
+ImageLoaderCacheSection::ImageLoaderCacheSection()
+    : ConcatInputSection(segment_names::data, section_names::data) {
   uint8_t *arr = bAlloc.Allocate<uint8_t>(target->wordSize);
   memset(arr, 0, target->wordSize);
   data = {arr, target->wordSize};


        


More information about the llvm-commits mailing list