[llvm] r217207 - Refactor to avoid code duplication. NFC.

Rafael Espindola rafael.espindola at gmail.com
Thu Sep 4 17:02:51 PDT 2014


Author: rafael
Date: Thu Sep  4 19:02:50 2014
New Revision: 217207

URL: http://llvm.org/viewvc/llvm-project?rev=217207&view=rev
Log:
Refactor to avoid code duplication. NFC.

Modified:
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=217207&r1=217206&r2=217207&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Thu Sep  4 19:02:50 2014
@@ -358,8 +358,11 @@ TargetLoweringObjectFileELF::getSectionF
   return DataRelROSection;
 }
 
-const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
-    unsigned Priority, const MCSymbol *KeySym) const {
+static const MCSectionELF *getStaticStructorSection(MCContext &Ctx,
+                                                    bool UseInitArray,
+                                                    bool IsCtor,
+                                                    unsigned Priority,
+                                                    const MCSymbol *KeySym) {
   std::string Name;
   unsigned Type;
   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
@@ -370,16 +373,24 @@ const MCSection *TargetLoweringObjectFil
     Flags |= ELF::SHF_GROUP;
 
   if (UseInitArray) {
-    Name = ".init_array";
+    if (IsCtor) {
+      Type = ELF::SHT_INIT_ARRAY;
+      Name = ".init_array";
+    } else {
+      Type = ELF::SHT_FINI_ARRAY;
+      Name = ".fini_array";
+    }
     if (Priority != 65535) {
       Name += '.';
       Name += utostr(Priority);
     }
-    Type = ELF::SHT_INIT_ARRAY;
   } else {
     // The default scheme is .ctor / .dtor, so we have to invert the priority
     // numbering.
-    Name = std::string(".ctors");
+    if (IsCtor)
+      Name = ".ctors";
+    else
+      Name = ".dtors";
     if (Priority != 65535) {
       Name += '.';
       Name += utostr(65535 - Priority);
@@ -387,39 +398,19 @@ const MCSection *TargetLoweringObjectFil
     Type = ELF::SHT_PROGBITS;
   }
 
-  return getContext().getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
+  return Ctx.getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
 }
 
-const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
+const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
     unsigned Priority, const MCSymbol *KeySym) const {
-  std::string Name;
-  unsigned Type;
-  unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
-  SectionKind Kind = SectionKind::getDataRel();
-  StringRef COMDAT = KeySym ? KeySym->getName() : "";
-
-  if (KeySym)
-    Flags |= ELF::SHF_GROUP;
-
-  if (UseInitArray) {
-    Name = ".fini_array";
-    if (Priority != 65535) {
-      Name += '.';
-      Name += utostr(Priority);
-    }
-    Type = ELF::SHT_FINI_ARRAY;
-  } else {
-    // The default scheme is .ctor / .dtor, so we have to invert the priority
-    // numbering.
-    Name = ".dtors";
-    if (Priority != 65535) {
-      Name += '.';
-      Name += utostr(65535 - Priority);
-    }
-    Type = ELF::SHT_PROGBITS;
-  }
+  return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
+                                  KeySym);
+}
 
-  return getContext().getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
+const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
+    unsigned Priority, const MCSymbol *KeySym) const {
+  return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
+                                  KeySym);
 }
 
 void





More information about the llvm-commits mailing list