[lld] r257976 - Only emit files with subsections_via_symbols if all inputs had that set.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 15 17:09:23 PST 2016


Author: pete
Date: Fri Jan 15 19:09:23 2016
New Revision: 257976

URL: http://llvm.org/viewvc/llvm-project?rev=257976&view=rev
Log:
Only emit files with subsections_via_symbols if all inputs had that set.

When generating a relocatable file, its only valid to set this flag if
all of the inputs also had the flag.  Otherwise we may atomize incorrectly
when we link the relocatable file again.

Reviewed by Lang Hames.

Differential Revision: http://reviews.llvm.org/D16018

Modified:
    lld/trunk/lib/ReaderWriter/MachO/File.h
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
    lld/trunk/test/mach-o/parse-data-relocs-x86_64.yaml

Modified: lld/trunk/lib/ReaderWriter/MachO/File.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/File.h?rev=257976&r1=257975&r2=257976&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/File.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/File.h Fri Jan 15 19:09:23 2016
@@ -197,6 +197,11 @@ public:
   uint32_t swiftVersion() const { return _swiftVersion; }
   void setSwiftVersion(uint32_t v) { _swiftVersion = v; }
 
+  bool subsectionsViaSymbols() const {
+    return _flags & llvm::MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
+  }
+  void setFlags(normalized::FileFlags v) { _flags = v; }
+
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const File *F) {
     return F->kind() == File::kindMachObject;
@@ -238,6 +243,7 @@ private:
   MachOLinkingContext::Arch      _arch = MachOLinkingContext::arch_unknown;
   MachOLinkingContext::OS        _os = MachOLinkingContext::OS::unknown;
   uint32_t                       _swiftVersion = 0;
+  normalized::FileFlags          _flags;
 };
 
 class MachODylibFile : public SharedLibraryFile {

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=257976&r1=257975&r2=257976&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Fri Jan 15 19:09:23 2016
@@ -105,10 +105,12 @@ class Util {
 public:
   Util(const MachOLinkingContext &ctxt)
       : _ctx(ctxt), _archHandler(ctxt.archHandler()), _entryAtom(nullptr),
-        _hasTLVDescriptors(false) {}
+        _hasTLVDescriptors(false), _subsectionsViaSymbols(true) {}
   ~Util();
 
-  void      assignAtomsToSections(const lld::File &atomFile);
+  void      processDefinedAtoms(const lld::File &atomFile);
+  void      processAtomAttributes(const DefinedAtom *atom);
+  void      assignAtomToSection(const DefinedAtom *atom);
   void      organizeSections();
   void      assignAddressesToSections(const NormalizedFile &file);
   uint32_t  fileFlags();
@@ -180,6 +182,7 @@ private:
   AtomToIndex                   _atomToSymbolIndex;
   std::vector<const Atom *>     _machHeaderAliasAtoms;
   bool                          _hasTLVDescriptors;
+  bool                          _subsectionsViaSymbols;
 };
 
 Util::~Util() {
@@ -366,15 +369,28 @@ void Util::appendAtom(SectionInfo *sect,
   sect->size = offset + atom->size();
 }
 
-void Util::assignAtomsToSections(const lld::File &atomFile) {
+void Util::processDefinedAtoms(const lld::File &atomFile) {
   for (const DefinedAtom *atom : atomFile.defined()) {
-    if (atom->contentType() == DefinedAtom::typeMachHeader)
-      _machHeaderAliasAtoms.push_back(atom);
-    else
-      appendAtom(sectionForAtom(atom), atom);
+    processAtomAttributes(atom);
+    assignAtomToSection(atom);
   }
 }
 
+void Util::processAtomAttributes(const DefinedAtom *atom) {
+  auto *machoFile = static_cast<const mach_o::MachOFile *>(&atom->file());
+  // If the file doesn't use subsections via symbols, then make sure we don't
+  // add that flag to the final output file if we have a relocatable file.
+  if (!machoFile->subsectionsViaSymbols())
+    _subsectionsViaSymbols = false;
+}
+
+void Util::assignAtomToSection(const DefinedAtom *atom) {
+  if (atom->contentType() == DefinedAtom::typeMachHeader)
+    _machHeaderAliasAtoms.push_back(atom);
+  else
+    appendAtom(sectionForAtom(atom), atom);
+}
+
 SegmentInfo *Util::segmentForName(StringRef segName) {
   for (SegmentInfo *si : _segmentInfos) {
     if ( si->name.equals(segName) )
@@ -1183,7 +1199,7 @@ void Util::addExportInfo(const lld::File
 uint32_t Util::fileFlags() {
   // FIXME: these need to determined at runtime.
   if (_ctx.outputMachOType() == MH_OBJECT) {
-    return MH_SUBSECTIONS_VIA_SYMBOLS;
+    return _subsectionsViaSymbols ? MH_SUBSECTIONS_VIA_SYMBOLS : 0;
   } else {
     uint32_t flags = MH_DYLDLINK;
     if (!_ctx.useFlatNamespace())
@@ -1208,7 +1224,7 @@ normalizedFromAtoms(const lld::File &ato
                                            const MachOLinkingContext &context) {
   // The util object buffers info until the normalized file can be made.
   Util util(context);
-  util.assignAtomsToSections(atomFile);
+  util.processDefinedAtoms(atomFile);
   util.organizeSections();
 
   std::unique_ptr<NormalizedFile> f(new NormalizedFile());

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=257976&r1=257975&r2=257976&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Fri Jan 15 19:09:23 2016
@@ -1056,6 +1056,7 @@ normalizedObjectToAtoms(MachOFile *file,
   }
 
   // Cache some attributes on the file for use later.
+  file->setFlags(normalizedFile.flags);
   file->setArch(normalizedFile.arch);
   file->setOS(normalizedFile.os);
 

Modified: lld/trunk/test/mach-o/parse-data-relocs-x86_64.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/parse-data-relocs-x86_64.yaml?rev=257976&r1=257975&r2=257976&view=diff
==============================================================================
--- lld/trunk/test/mach-o/parse-data-relocs-x86_64.yaml (original)
+++ lld/trunk/test/mach-o/parse-data-relocs-x86_64.yaml Fri Jan 15 19:09:23 2016
@@ -323,4 +323,5 @@ page-size:       0x00000000
 # CHECK:    content:         [ 00, 00, 00, 00, 00, 00, 00, 00 ]
 # CHECK:    section-choice:  custom-required
 # CHECK:    section-name:    __DATA/__custom
+# CHECK:    dead-strip:      never
 




More information about the llvm-commits mailing list