Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -140,7 +140,7 @@
 public:
   LinkerScript();
   ~LinkerScript();
-  void createSections(OutputSectionFactory<ELFT> &Factory);
+  void createSections();
 
   std::vector<PhdrEntry<ELFT>> createPhdrs();
   bool ignoreInterpSection();
@@ -165,6 +165,9 @@
   std::vector<InputSectionBase<ELFT> *>
   createInputSectionList(OutputSectionCommand &Cmd);
 
+  std::pair<OutputSectionBase<ELFT> *, bool>
+  createOutputSection(InputSectionBase<ELFT> *C, StringRef Name);
+
   // "ScriptConfig" is a bit too long, so define a short name for it.
   ScriptConfiguration &Opt = *ScriptConfig;
 
@@ -174,6 +177,8 @@
 
   llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc;
 
+  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
+
   uintX_t Dot;
 };
 
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -259,7 +259,40 @@
 }
 
 template <class ELFT>
-void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
+std::pair<OutputSectionBase<ELFT> *, bool>
+LinkerScript<ELFT>::createOutputSection(InputSectionBase<ELFT> *C,
+                                        StringRef Name) {
+  typedef typename ELFT::Shdr Elf_Shdr;
+
+  bool IsNew = true;
+  OutputSectionBase<ELFT> *Sec = nullptr;
+  for (std::unique_ptr<OutputSectionBase<ELFT>> &S : OwningSections)
+    if (S->getName() == Name) {
+      Sec = S.get();
+      IsNew = false;
+      break;
+    }
+
+  const Elf_Shdr *H = C->getSectionHdr();
+  uintX_t Flags = H->sh_flags & ~SHF_GROUP & ~SHF_COMPRESSED;
+  uintX_t Alignment = 0;
+  if (isa<MergeInputSection<ELFT>>(C))
+    Alignment = std::max(H->sh_addralign, H->sh_entsize);
+  uint32_t Type = H->sh_type;
+
+  if (!Sec) {
+    std::tie(Sec, IsNew) =
+        allocSection<ELFT>(C->SectionKind, Name, Flags, Alignment, Type);
+    if (IsNew)
+      OwningSections.emplace_back(Sec);
+    return {Sec, IsNew};
+  }
+
+  return{ Sec, IsNew };
+}
+
+template <class ELFT>
+void LinkerScript<ELFT>::createSections() {
   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
       if (shouldDefine<ELFT>(Cmd))
@@ -280,7 +313,7 @@
 
       OutputSectionBase<ELFT> *OutSec;
       bool IsNew;
-      std::tie(OutSec, IsNew) = Factory.create(Head, Cmd->Name);
+      std::tie(OutSec, IsNew) = createOutputSection(Head, Cmd->Name);
       if (IsNew)
         OutputSections->push_back(OutSec);
 
@@ -302,7 +335,7 @@
         continue;
       OutputSectionBase<ELFT> *OutSec;
       bool IsNew;
-      std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S));
+      std::tie(OutSec, IsNew) = createOutputSection(S, getOutputSectionName(S));
       if (IsNew)
         OutputSections->push_back(OutSec);
       OutSec->addSection(S);
Index: ELF/OutputSections.h
===================================================================
--- ELF/OutputSections.h
+++ ELF/OutputSections.h
@@ -34,11 +34,17 @@
 template <class ELFT> class MergeInputSection;
 template <class ELFT> class MipsReginfoInputSection;
 template <class ELFT> class OutputSection;
+template <class ELFT> class OutputSectionBase;
 template <class ELFT> class ObjectFile;
 template <class ELFT> class SharedFile;
 template <class ELFT> class SharedSymbol;
 template <class ELFT> class DefinedRegular;
 
+template <class ELFT>
+std::pair<OutputSectionBase<ELFT> *, bool>
+allocSection(unsigned Kind, StringRef Name, typename ELFT::uint Flags,
+             typename ELFT::uint Alignment, uint32_t Type);
+
 // This represents a section in an output file.
 // Different sub classes represent different types of sections. Some contain
 // input sections, others are created by the linker.
@@ -93,6 +99,7 @@
   uintX_t getSize() const { return Header.sh_size; }
   void setSize(uintX_t Val) { Header.sh_size = Val; }
   uintX_t getFlags() const { return Header.sh_flags; }
+  void setFlags(uintX_t Val) { Header.sh_flags = Val; }
   uint32_t getPhdrFlags() const;
   uintX_t getFileOff() const { return Header.sh_offset; }
   uintX_t getAlignment() const { return Header.sh_addralign; }
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -1784,22 +1784,17 @@
 
 template <class ELFT>
 std::pair<OutputSectionBase<ELFT> *, bool>
-OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
-                                   StringRef OutsecName) {
-  SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
-  OutputSectionBase<ELFT> *&Sec = Map[Key];
-  if (Sec)
-    return {Sec, false};
-
-  switch (C->SectionKind) {
+elf::allocSection(unsigned Kind, StringRef Name, typename ELFT::uint Flags,
+                  typename ELFT::uint Alignment, uint32_t Type) {
+  OutputSectionBase<ELFT> *Sec;
+  switch (Kind) {
   case InputSectionBase<ELFT>::Regular:
-    Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
+    Sec = new OutputSection<ELFT>(Name, Type, Flags);
     break;
   case InputSectionBase<ELFT>::EHFrame:
     return {Out<ELFT>::EhFrame, false};
   case InputSectionBase<ELFT>::Merge:
-    Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags,
-                                       Key.Alignment);
+    Sec = new MergeOutputSection<ELFT>(Name, Type, Flags, Alignment);
     break;
   case InputSectionBase<ELFT>::MipsReginfo:
     Sec = new MipsReginfoOutputSection<ELFT>();
@@ -1810,9 +1805,26 @@
   case InputSectionBase<ELFT>::MipsAbiFlags:
     Sec = new MipsAbiFlagsOutputSection<ELFT>();
     break;
-  case InputSectionBase<ELFT>::Layout:
+  default:
     llvm_unreachable("Invalid section type");
   }
+  return {Sec, true};
+}
+
+template <class ELFT>
+std::pair<OutputSectionBase<ELFT> *, bool>
+OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
+                                   StringRef OutsecName) {
+  SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
+  OutputSectionBase<ELFT> *&Sec = Map[Key];
+  if (Sec)
+    return {Sec, false};
+
+  bool Allocated;
+  std::tie(Sec, Allocated) = allocSection<ELFT>(
+      C->SectionKind, Key.Name, Key.Flags, Key.Alignment, Key.Type);
+  if (!Allocated)
+    return {Sec, false};
   Out<ELFT>::Pool.emplace_back(Sec);
   return {Sec, true};
 }
@@ -2005,3 +2017,16 @@
 template class OutputSectionFactory<ELF64BE>;
 }
 }
+
+template std::pair<OutputSectionBase<ELF32LE> *, bool>
+elf::allocSection(unsigned Kind, StringRef Name, ELF32LE::uint Flags,
+                  ELF32LE::uint Alignment, uint32_t Type);
+template std::pair<OutputSectionBase<ELF32BE> *, bool>
+elf::allocSection(unsigned Kind, StringRef Name, ELF32BE::uint Flags,
+                  ELF32BE::uint Alignment, uint32_t Type);
+template std::pair<OutputSectionBase<ELF64LE> *, bool>
+elf::allocSection(unsigned Kind, StringRef Name, ELF64LE::uint Flags,
+                  ELF64LE::uint Alignment, uint32_t Type);
+template std::pair<OutputSectionBase<ELF64BE> *, bool>
+elf::allocSection(unsigned Kind, StringRef Name, ELF64BE::uint Flags,
+                  ELF64BE::uint Alignment, uint32_t Type);
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -248,7 +248,7 @@
 
   Script<ELFT>::X->OutputSections = &OutputSections;
   if (ScriptConfig->HasContents)
-    Script<ELFT>::X->createSections(Factory);
+    Script<ELFT>::X->createSections();
   else
     createSections();